| 1 |
|
| 2 |
|
| 3 |
import xml.dom.minidom as dom |
| 4 |
import sys |
| 5 |
import struct |
| 6 |
|
| 7 |
WEAP_NUM = 780 |
| 8 |
|
| 9 |
struct_fmt = "<H BBHBBBB 8B8B8b8b8b8b8H bbBBBB" |
| 10 |
|
| 11 |
def pack_weapon(dict): |
| 12 |
l = [] |
| 13 |
|
| 14 |
l.append(dict['drain']) |
| 15 |
l.append(dict['shotRepeat']) |
| 16 |
l.append(dict['multi']) |
| 17 |
l.append(dict['weapAni']) |
| 18 |
l.append(dict['max']) |
| 19 |
l.append(dict['tx']) |
| 20 |
l.append(dict['ty']) |
| 21 |
l.append(dict['aim']) |
| 22 |
|
| 23 |
tmp = dict['patterns'] |
| 24 |
for j in range(8): |
| 25 |
l.append(tmp[j]['attack']) |
| 26 |
for j in range(8): |
| 27 |
l.append(tmp[j]['del']) |
| 28 |
for j in range(8): |
| 29 |
l.append(tmp[j]['sx']) |
| 30 |
for j in range(8): |
| 31 |
l.append(tmp[j]['sy']) |
| 32 |
for j in range(8): |
| 33 |
l.append(tmp[j]['bx']) |
| 34 |
for j in range(8): |
| 35 |
l.append(tmp[j]['by']) |
| 36 |
for j in range(8): |
| 37 |
l.append(tmp[j]['sg']) |
| 38 |
|
| 39 |
l.append(dict['acceleration']) |
| 40 |
l.append(dict['accelerationx']) |
| 41 |
l.append(dict['circleSize']) |
| 42 |
l.append(dict['sound']) |
| 43 |
l.append(dict['trail']) |
| 44 |
l.append(dict['shipBlastFilter']) |
| 45 |
|
| 46 |
return struct.pack(struct_fmt, *l) |
| 47 |
|
| 48 |
def unpack_weapon(str): |
| 49 |
tup = struct.unpack(struct_fmt, str) |
| 50 |
dict = {} |
| 51 |
|
| 52 |
dict['drain'] = tup[0] |
| 53 |
dict['shotRepeat'] = tup[1] |
| 54 |
dict['multi'] = tup[2] |
| 55 |
dict['weapAni'] = tup[3] |
| 56 |
dict['max'] = tup[4] |
| 57 |
dict['tx'] = tup[5] |
| 58 |
dict['ty'] = tup[6] |
| 59 |
dict['aim'] = tup[7] |
| 60 |
|
| 61 |
i = 8 |
| 62 |
|
| 63 |
tmp = [{} for j in range(8)] |
| 64 |
for j in range(8): |
| 65 |
tmp[j]['attack'] = tup[i] |
| 66 |
i += 1 |
| 67 |
for j in range(8): |
| 68 |
tmp[j]['del'] = tup[i] |
| 69 |
i += 1 |
| 70 |
for j in range(8): |
| 71 |
tmp[j]['sx'] = tup[i] |
| 72 |
i += 1 |
| 73 |
for j in range(8): |
| 74 |
tmp[j]['sy'] = tup[i] |
| 75 |
i += 1 |
| 76 |
for j in range(8): |
| 77 |
tmp[j]['bx'] = tup[i] |
| 78 |
i += 1 |
| 79 |
for j in range(8): |
| 80 |
tmp[j]['by'] = tup[i] |
| 81 |
i += 1 |
| 82 |
for j in range(8): |
| 83 |
tmp[j]['sg'] = tup[i] |
| 84 |
i += 1 |
| 85 |
dict['patterns'] = tmp |
| 86 |
|
| 87 |
dict['acceleration'] = tup[i] |
| 88 |
dict['accelerationx'] = tup[i+1] |
| 89 |
dict['circleSize'] = tup[i+2] |
| 90 |
dict['sound'] = tup[i+3] |
| 91 |
dict['trail'] = tup[i+4] |
| 92 |
dict['shipBlastFilter'] = tup[i+5] |
| 93 |
|
| 94 |
return dict |
| 95 |
|
| 96 |
def DOMToDict(doc, weap_node): |
| 97 |
dict = {} |
| 98 |
|
| 99 |
for i in weap_node.childNodes: |
| 100 |
if i.nodeType != i.ELEMENT_NODE: |
| 101 |
continue |
| 102 |
|
| 103 |
if i.hasAttribute("value"): |
| 104 |
dict[i.tagName] = int(i.getAttribute("value")) |
| 105 |
elif i.tagName == "patterns": |
| 106 |
dict['patterns'] = [{} for el in range(8)] |
| 107 |
index = 0 |
| 108 |
for j in i.childNodes: |
| 109 |
if j.nodeType != i.ELEMENT_NODE: |
| 110 |
continue |
| 111 |
|
| 112 |
attrs = [j.attributes.item(i) for i in range(j.attributes.length)] |
| 113 |
for i in attrs: |
| 114 |
dict['patterns'][index][i.name] = int(i.nodeValue) |
| 115 |
index += 1 |
| 116 |
|
| 117 |
return dict |
| 118 |
|
| 119 |
def dictToDOM(doc, root, dict, index=None): |
| 120 |
entry = doc.createElement("weapon") |
| 121 |
if index != None: |
| 122 |
entry.setAttribute("index", "%04X" % (index,)) |
| 123 |
|
| 124 |
keys = list(dict) |
| 125 |
keys.sort() |
| 126 |
for i in keys: |
| 127 |
node = doc.createElement(i) |
| 128 |
if isinstance(dict[i], list): |
| 129 |
for j in dict[i]: |
| 130 |
keys = list(j) |
| 131 |
keys.sort() |
| 132 |
|
| 133 |
n = doc.createElement("entry") |
| 134 |
for i in keys: |
| 135 |
n.setAttribute(i, str(j[i])) |
| 136 |
node.appendChild(n) |
| 137 |
else: |
| 138 |
node.setAttribute("value", str(dict[i])) |
| 139 |
entry.appendChild(node) |
| 140 |
|
| 141 |
root.appendChild(entry) |
| 142 |
|
| 143 |
def toXML(hdt, output): |
| 144 |
doc = dom.getDOMImplementation().createDocument(None, "TyrianHDT", None) |
| 145 |
|
| 146 |
try: |
| 147 |
f = open(hdt, "rb") |
| 148 |
except IOError: |
| 149 |
print("%s couldn't be opened for reading." % (hdt,)) |
| 150 |
sys.exit(1) |
| 151 |
|
| 152 |
try: |
| 153 |
outf = open(output, "w") |
| 154 |
except IOError: |
| 155 |
print("%s couldn't be opened for writing." % (output,)) |
| 156 |
sys.exit(1) |
| 157 |
|
| 158 |
f.seek(struct.unpack("<i", f.read(4))[0]) |
| 159 |
f.read(7*2) |
| 160 |
|
| 161 |
|
| 162 |
sys.stdout.write("Converting weapons") |
| 163 |
index = 0 |
| 164 |
|
| 165 |
for i in range(WEAP_NUM+1): |
| 166 |
tmp = f.read(struct.calcsize(struct_fmt)) |
| 167 |
shot = unpack_weapon(tmp) |
| 168 |
dictToDOM(doc, doc.documentElement, shot, index) |
| 169 |
index += 1 |
| 170 |
|
| 171 |
sys.stdout.write(".") |
| 172 |
sys.stdout.flush() |
| 173 |
|
| 174 |
sys.stdout.write("Done!\n") |
| 175 |
sys.stdout.write("Writing XML...") |
| 176 |
sys.stdout.flush() |
| 177 |
doc.writexml(outf, addindent="\t", newl="\n") |
| 178 |
sys.stdout.write("Done!\n") |
| 179 |
|
| 180 |
def toHDT(input, hdt): |
| 181 |
try: |
| 182 |
f = open(input, "r") |
| 183 |
except IOError: |
| 184 |
print("%s couldn't be opened for reading." % (input,)) |
| 185 |
sys.exit(1) |
| 186 |
|
| 187 |
try: |
| 188 |
outf = open(hdt, "r+b") |
| 189 |
except IOError: |
| 190 |
print("%s couldn't be opened for writing." % (hdt,)) |
| 191 |
sys.exit(1) |
| 192 |
|
| 193 |
outf.seek(struct.unpack("<i", outf.read(4))[0]) |
| 194 |
outf.read(7*2) |
| 195 |
|
| 196 |
sys.stdout.write("Reading XML...") |
| 197 |
sys.stdout.flush() |
| 198 |
doc = dom.parse(f) |
| 199 |
sys.stdout.write("Done!\n") |
| 200 |
|
| 201 |
sys.stdout.write("Writing weapons") |
| 202 |
|
| 203 |
for i in doc.documentElement.childNodes: |
| 204 |
if i.nodeType != i.ELEMENT_NODE: |
| 205 |
continue |
| 206 |
|
| 207 |
shot = DOMToDict(doc, i) |
| 208 |
str = pack_weapon(shot) |
| 209 |
|
| 210 |
outf.write(str) |
| 211 |
|
| 212 |
sys.stdout.write(".") |
| 213 |
sys.stdout.flush() |
| 214 |
|
| 215 |
sys.stdout.write("Done!\n") |
| 216 |
|
| 217 |
def printHelp(): |
| 218 |
print("Usage: weapon.py toxml path/to/tyrian.hdt output.xml") |
| 219 |
print(" weapon.py tohdt input.xml path/to/tyrian.hdt") |
| 220 |
sys.exit(1) |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
if __name__ == "__main__": |
| 225 |
if len(sys.argv) != 4: |
| 226 |
printHelp() |
| 227 |
|
| 228 |
if sys.argv[1] == "toxml": |
| 229 |
toXML(sys.argv[2], sys.argv[3]) |
| 230 |
elif sys.argv[1] == "tohdt": |
| 231 |
toHDT(sys.argv[2], sys.argv[3]) |
| 232 |
else: |
| 233 |
printHelp() |