forked from johnzero7/XNALaraMesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_ops.py
122 lines (83 loc) · 2.49 KB
/
bin_ops.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import struct
from . import xps_const
# Format
class TypeFormat:
SByte = '<b'
Byte = '<B'
Int16 = '<h'
UInt16 = '<H'
Int32 = '<i'
UInt32 = '<I'
Int64 = '<l'
UInt64 = '<L'
Single = '<f'
Double = '<d'
def roundToMultiple(numToRound, multiple):
return (numToRound + multiple - 1) // multiple * multiple
def readByte(file):
numberBin = file.read(1)
number = struct.unpack(TypeFormat.Byte, numberBin)[0]
return number
def writeByte(number):
bytesBin = struct.pack(TypeFormat.Byte, number)
return bytesBin
def readUInt16(file):
numberBin = file.read(2)
number = struct.unpack(TypeFormat.UInt16, numberBin)[0]
return number
def writeUInt16(number):
uInt16 = struct.pack(TypeFormat.UInt16, number)
return uInt16
def readInt16(file):
numberBin = file.read(2)
number = struct.unpack(TypeFormat.Int16, numberBin)[0]
return number
def writeInt16(number):
int16 = struct.pack(TypeFormat.Int16, number)
return int16
def readUInt32(file):
numberBin = file.read(4)
number = struct.unpack(TypeFormat.UInt32, numberBin)[0]
return number
def writeUInt32(number):
uInt32 = struct.pack(TypeFormat.UInt32, number)
return uInt32
def readSingle(file):
numberBin = file.read(4)
single = struct.unpack(TypeFormat.Single, numberBin)[0]
return single
def writeSingle(number):
single = struct.pack(TypeFormat.Single, number)
return single
def readString(file, length):
try:
pos1 = file.tell()
byteString = file.read(length)
pos2 = file.tell()
string = ''
string = decodeBytes(byteString)
except Exception:
print('*' * 40)
print('pos len', pos1)
print('pos str', pos2)
print('pos', file.tell())
print('len', length)
print('str', byteString)
string = decodeBytes(byteString)
return string
def writeString(string):
# String Lenght
byteString = encodeString(string)
return byteString
def decodeBytes(bytes):
# print(bytes)
return bytes.decode(xps_const.ENCODING_READ)
def encodeString(string):
# print(string)
return string.encode(xps_const.ENCODING_WRITE)
def hasHeader(fileformat='.xps'):
return fileformat == '.xps'
def hasTangentVersion(verMayor, verMinor, hasHeader=True):
return (verMinor <= 12 and verMayor <= 2) if hasHeader else True
def hasVariableWeights(verMayor, verMinor, hasHeader=True):
return (verMayor >= 3) if hasHeader else False