-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcommon.py
210 lines (152 loc) · 4.89 KB
/
common.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
from collections import OrderedDict
import struct
import xmltodict
def xmlToDict(file, process_namespaces=False, namespaces={}):
with open(file) as inf:
xml = inf.read()
return xmltodict.parse(xml, process_namespaces=process_namespaces, namespaces=namespaces)
def dictToXml(dict_, pretty=True):
return xmltodict.unparse(dict_, pretty=pretty)
def readString(data, offset=0, charWidth=1, encoding='utf-8'):
end = data.find(b'\0' * charWidth, offset)
while end != -1:
if (end - offset) % charWidth == 0:
break
end = data.find(b'\0' * charWidth, end + 1)
if end == -1:
return data[offset:].decode(encoding)
return data[offset:end].decode(encoding)
def roundUp(x, y):
return ((x - 1) | (y - 1)) + 1
class BlockHeader:
def __init__(self, file, pos):
self.magic, self.size = struct.unpack_from('>4sI', file, pos)
def save(self):
return struct.pack(
'>4sI',
self.magic,
self.size,
)
class Section:
def __init__(self, file, pos):
self.blockHeader = BlockHeader(file, pos)
self.data = file[pos + 8:pos + self.blockHeader.size]
def save(self):
self.blockHeader.size = 8 + len(self.data)
return b''.join([self.blockHeader.save(), self.data])
class Head:
user = "someone"
host = "somewhere"
date = "2019-01-19T22:30:54.551+09:00"
source = ""
title = None
comment = None
generatorName = "Layout Exporter U"
generatorVersion = "1.0.0"
def getAsDict(self):
_dict = OrderedDict()
_dict["create"] = OrderedDict()
_dict["create"]["@user"] = self.user
_dict["create"]["@host"] = self.host
_dict["create"]["@date"] = self.date
_dict["create"]["@source"] = self.source
_dict["title"] = self.title
_dict["comment"] = self.comment
_dict["generator"] = OrderedDict()
_dict["generator"]["@name"] = self.generatorName
_dict["generator"]["@version"] = self.generatorVersion
return _dict
class Color4:
def __init__(self):
self.r = 255
self.g = 255
self.b = 255
self.a = 255
def set(self, r, g, b, a):
self.r, self.g, self.b, self.a = r, g, b, a
def getAsDict(self):
return {
"@r": str(self.r),
"@g": str(self.g),
"@b": str(self.b),
"@a": str(self.a),
}
class MaterialName:
def __init__(self):
self.string = ""
def set(self, string):
if len(string) > 20:
print("Warning, material name '%s' must be less than 20 characters!" % string)
self.string = string
def get(self):
return self.string
class UserData:
class Item:
def __init__(self):
self.name = ""
self.type = ""
self.data = ""
def set(self, userData):
self.name = userData.name
type = userData.type
data = userData.data
if type == 0:
self.data = ' '.join(data)
self.type = "string"
elif type == 1:
self.data = ' '.join([str(_int) for _int in data])
self.type = "int"
elif type == 2:
self.data = ' '.join([str(_float) for _float in data])
self.type = "float"
def getAsDict(self):
if self.type:
return {
"@name": self.name,
"#text": self.data,
}
return None
def __init__(self):
self.string = []
self.int = []
self.float = []
def set(self, extUserData):
self.string = []
self.int = []
self.float = []
self.append(extUserData)
def append(self, extUserData):
for userData in extUserData:
item = self.Item()
item.set(userData)
if item.type == "string":
self.string.append(item.getAsDict())
elif item.type == "int":
self.int.append(item.getAsDict())
elif item.type == "float":
self.float.append(item.getAsDict())
def setSingleStr(self, string):
self.string = [{
"@name": "__BasicUserDataString",
"#text": string,
}]
self.int = []
self.float = []
def getAsDict(self):
_dict = {}
if self.string:
_dict["string"] = self.string
if self.int:
_dict["int"] = self.int
if self.float:
_dict["float"] = self.float
return _dict
class LRName:
def __init__(self):
self.string = ""
def set(self, string):
if len(string) > 24:
print("Warning, string '%s' must be less than 24 characters!" % string)
self.string = string
def get(self):
return self.string