-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·222 lines (186 loc) · 6.78 KB
/
main.py
File metadata and controls
executable file
·222 lines (186 loc) · 6.78 KB
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
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python3
import sys
from lib import quality, IBitStream
from lib.character_class import CharacterClass
from lib.itemprops import decodeProp
from lib.itemtype import iTypeFromCode
def decodeProps(stream: IBitStream):
print("<{0:5d}> decode props:".format(stream.getOffset()))
nProps = 0
while not stream.isEnd():
result = decodeProp(stream)
if result is None:
break
nProps += 1
print("[{0:4d}]".format(result.id), "{0}".format(
result.formatted) if result.formatted else "")
for detail in result.details:
if detail.base and detail.base > 0:
print("\t{0}({1})={2}-{3}".format(detail.ptn,
detail.bits, detail.value, detail.base))
else:
print("\t{0}({1})={2}{3}".format(
detail.ptn, detail.bits, detail.value,
" ({0})".format(detail.name) if detail.name else ""))
print("nProps={0}".format(nProps))
def parseEar(stream: IBitStream):
iClass = stream.read(3)
iLevel = stream.read(7)
print("iClass={0}({1}),iLvl={2}".format(
CharacterClass.from_id(iClass), iClass, iLevel))
name = ""
while True:
c = stream.read(7)
if c == 0:
break
name += chr(c)
print("name={0}".format(name))
def parseD2I(data):
stream = IBitStream(data)
print(str(stream))
pad = 0
for v in str(stream)[-7:]:
if v == '0':
pad += 1
print("pad={0}/{1}".format(pad, len(stream)), str(stream)[-7:])
d = 0x4d4a
s = 16
v = ('{0:032b}'.format(d))[::-1][:s]
print("searching :{0}-{1},{2}".format(d, s, v))
for i in range(0, len(stream)):
if str(stream)[i:i+s] == v:
bits = int(str(stream)[i:i+s][::-1], 2)
print("\t", i, bits)
stream.skip(2*8+1+3)
bIdentified = stream.read(1)
stream.skip(27 - stream.getOffset())
bSocketed = stream.read(1)
print("bIdentified={0},bSocketed={1}".format(bIdentified, bSocketed))
stream.skip(2 + 1 + 1)
bEar = stream.read(1)
stream.skip(1 + 3)
bSimple = stream.read(1)
bEthereal = stream.read(1)
print("bEar={0},bSimple={1},bEthereal={2}".format(
bEar, bSimple, bEthereal))
stream.skip(1) # unk47
print("<{0:5d}> after unk47".format(stream.getOffset()))
bPersonalized = stream.read(1)
stream.skip(1) # unk51
bRuneWord = stream.read(1)
print("bPersonalized={0},bRune={1}".format(bPersonalized, bRuneWord))
stream.skip(5)
version = stream.read(10)
print("version={0}({1})".format(
"Expansion" if version == 0x64 else "<unknown>", version))
stream.skip(18)
print("<{0:5d}> base done".format(stream.getOffset()))
if bEar:
parseEar(stream)
return
code = stream.readString(4)
nGems = stream.read(3)
print("code={0},#gems={1}".format(code, nGems))
guid = stream.read(32)
iLevel = stream.read(7)
print("guid=0x{0:04x},iLvl={1}".format(guid, iLevel))
iQuality = stream.read(4)
print("iQuality={0}-{1}".format(iQuality, quality.byId[iQuality]))
bVarGfx = stream.read(1)
if bVarGfx:
iVarGfx = stream.read(3)
print("iVarGfx={0}".format(iVarGfx))
bClass = stream.read(1)
if bClass:
wClass = stream.read(11)
print("wClass={0}".format(wClass))
if iQuality == quality.High:
iQualitySubType = stream.read(3)
print("\tsubType={0}(0x{0:x})".format(iQualitySubType))
elif iQuality == quality.Magic:
prefix = stream.read(11)
assert prefix == 0, "Magic's prefix must be 0"
suffix = stream.read(11)
print("\tMagic: suffix={0}(0x{0:x})".format(suffix))
elif iQuality == quality.Unique:
iUniqId = stream.read(12)
print("\tUniqItem: id={0}(0x{0:x})".format(iUniqId))
elif iQuality == quality.PartOfSet:
iSetId = stream.read(12)
print("\tSetItem: id={0}(0x{0:x})".format(iSetId))
else:
print("\t?", iQuality)
if bRuneWord:
wRuneWord = stream.read(16)
stream.skip(7 - 16)
print("runeWord:", wRuneWord, "offset:", stream.getOffset())
bTimestamp = stream.read(1)
if bTimestamp:
dw3 = stream.read(32*3)
print("dwTimestamp?=0x{0:x}".format(dw3))
bits = "{0:08b}".format(stream.peek(8))
print("<{0:5d}> before type data".format(stream.getOffset()), bits[::-1])
sType, typeCfg = iTypeFromCode(code)
print("type: {0} - {1}".format(sType, typeCfg["name"]))
if sType == "weapon":
print("type specific:", str(stream)[
stream.getOffset():stream.getOffset()+17], stream.getOffset())
if typeCfg["nodurability"]:
d = stream.read(8)
stream.skip(9)
print("\tdurability: INDESTRUCTIBLE(pad=0x{0:x})".format(d))
else:
max_durability = stream.read(8)
if max_durability > 0:
durability = stream.read(9)
print(
"\tdurability: {0}/{1}".format(durability, max_durability))
else:
print("\tdurability: INDESTRUCTIBLE")
if typeCfg["maxstack"] > 0:
print("\tquantity:", stream.read(9))
elif sType == "armor":
iDefence = stream.read(11)
print("\tiDefence={0}".format(iDefence))
iMaxDur = stream.read(8)
if iMaxDur > 0:
iCurDur = stream.read(9)
print("\tdurability={0}/{1}".format(iCurDur, iMaxDur))
else:
print("\tIndestructible")
if bSocketed:
iSockets = stream.read(4)
print("\tsockets: {0}".format(iSockets))
bits = "{0:09b}".format(stream.peek(9))
print("<{0:5d}> before props data".format(stream.getOffset()), bits[::-1])
setBonusFlags = []
if iQuality == quality.PartOfSet:
for i in range(5):
setBonusFlags.append(stream.read(1))
print("Set Bonus Flags:", setBonusFlags)
if bRuneWord:
print(">>>> props from white:")
decodeProps(stream)
print(">>>> props from Rune Word:")
decodeProps(stream)
if iQuality == quality.PartOfSet:
for i, flag in enumerate(setBonusFlags):
if flag:
print(">>>> Set Bonus {0}:".format(i))
decodeProps(stream)
offset = stream.getOffset()
if offset+pad != len(stream):
print("<{0:5d}>[ERROR]{1}/{2}".format(offset, pad, len(stream)))
else:
print("<{0:5d}> done".format(offset))
if __name__ == "__main__":
from pathlib import Path
import re
raw = sys.argv[1]
# Git-Bash/MSYS 风格 /d/... -> D:/...
win = re.sub(r'^/([a-zA-Z])/', r'\1:/', raw)
path = Path(win).resolve()
print('映射后路径:', path)
print('exists:', path.exists())
with path.open('rb') as f:
parseD2I(f.read())