-
Notifications
You must be signed in to change notification settings - Fork 0
/
fmt_ht_rmp_res.py
281 lines (213 loc) · 10.7 KB
/
fmt_ht_rmp_res.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from inc_noesis import *
import os
import noewin
def registerNoesisTypes():
handle = noesis.register("Hard Truck 1/2 Resource archive", ".res;.rmp;.pro")
noesis.setHandlerExtractArc(handle, resExtractRes)
toolHandle = noesis.registerTool("Pack HT Resource", \
htResourcePackerToolMethod, "Pack files to game archive.")
noesis.setToolFlags(toolHandle, noesis.NTOOLFLAG_CONTEXTITEM)
noesis.setToolVisibleCallback(toolHandle, htArchivePackerVisible)
return 1
class proFile:
def __init__(self, filename):
self.filename = filename
self.sections = {}
def read(self):
with open(self.filename, "r") as textFile:
try:
for line in textFile:
name, count = line.split(" ")
sectionItems = []
for i in range(int(count)):
line = textFile.readline()
sectionItems.append(line[0:-1])
self.sections.update({name:sectionItems})
except:
noesis.messagePrompt( \
"Can't read {} file.".format(self.filename))
return 0
#noesis.logPopup()
#for key, value in self.sections.items():
# print(key)
return 1
class htArchivePacker:
def __init__(self, filename):
self.path = os.path.dirname(filename)
self.proFile = proFile(filename)
self.proFile.read()
self.filesSectionName = ("TEXTUREFILES", "PALETTEFILES", "SOUNDFILES", \
"BACKFILES", "MASKFILES")
def outputLog(self, logStrings):
with open(self.proFile.filename + ".log", "w") as text:
text.writelines(logStrings)
def checkFiles(self):
log = []
#noesis.logPopup()
for section, items in self.proFile.sections.items():
if section in self.filesSectionName:
log.append("Checking '{}' files...\n".format(section))
warningCount = 0
for name in items:
name = name.split(" ")[0]
if not os.path.isfile(os.path.join(self.path, name)):
log.append( \
"Warning: file {} doesn't exist.\n".format(name))
warningCount += 1
if warningCount == 0:
log.append("Result: All OK. \n")
else:
log.append(\
"Result: {} problem files.\n".format(warningCount))
self.outputLog(log)
return log
def packArchive(self, extension = ""):
log = []
if extension != "":
archiveName = self.proFile.filename.split(".")[0] + extension
else:
filename, archiveExtension, pro = self.proFile.filename.split(".")
archiveName = filename + '.' + archiveExtension
warningCount = 0
with open(archiveName, "wb") as archiveFile:
#noesis.logPopup()
for section, items in self.proFile.sections.items():
str = "{} {}\0".format(section, len(items))
archiveFile.write(str.encode("ascii"))
if section in self.filesSectionName:
for name in items:
try:
with open(os.path.join(\
self.path, name.split(" ")[0]), "rb") as htFile:
file = htFile.read()
# write filename
archiveFile.write((name + '\0').encode("ascii"))
# write file size
fileSize = len(file).to_bytes(4, byteorder='little')
archiveFile.write(fileSize)
# write file itself
archiveFile.write(file)
except:
log.append( \
"File {} failed to add.\n".format( \
os.path.join(self.path, name)))
warningCount += 1
else:
for item in items:
try:
archiveFile.write((item + "\0").encode("ascii"))
except:
log.append("Item {} failed to add.\n".format(item))
warningCount += 1
if warningCount == 0:
log.append("Archive {} packed successfully.\n".format(archiveName))
self.outputLog(log)
return log
class archivePackerDialogWindow():
def __init__(self, filename):
self.isCanceled = True
self.filename = filename
def archivePackerButtonCheck(self, noeWnd, controlId, wParam, lParam):
output = ""
for line in self.packer.checkFiles():
output += line
self.outputEdit.setText("")
self.outputEdit.setText(output)
return True
def archivePackerButtonPack(self, noeWnd, controlId, wParam, lParam):
output = ""
for line in self.packer.packArchive():
output += line
self.outputEdit.setText("")
self.outputEdit.setText(output)
self.isCanceled = False
return True
def archivePackerButtonCancel(self, noeWnd, controlId, wParam, lParam):
self.noeWnd.closeWindow()
return True
def create(self):
self.noeWnd = noewin.NoeUserWindow( \
"Hard Truck archive packer (.res, .rmp)", "HTPackerWindowClass", \
420, 255)
noeWindowRect = noewin.getNoesisWindowRect()
if noeWindowRect:
windowMargin = 100
self.noeWnd.x = noeWindowRect[0] + windowMargin
self.noeWnd.y = noeWindowRect[1] + windowMargin
if self.noeWnd.createWindow():
self.noeWnd.setFont("Arial", 12)
self.noeWnd.createStatic("File path:", 5, 5, 50, 20)
#
index = self.noeWnd.createEditBox(60, 5, 350, 20, "", None, \
False, True)
self.pathEdit = self.noeWnd.getControlByIndex(index)
self.pathEdit.setText(self.filename)
self.noeWnd.createStatic("Exten.:", 5, 32, 110, 20)
#
index = self.noeWnd.createComboBox(60, 32, 130, 20)
self.extComboBox = self.noeWnd.getControlByIndex(index)
extList = ("", "Hard Truck 1 (.rmp)", "Hard Truck 2 (.res)")
for extension in extList:
self.extComboBox.addString(extension)
self.extComboBox.selectString(extList[0])
self.noeWnd.createStatic("Output:", 5, 65, 60, 20)
#
index = self.noeWnd.createEditBox(60, 65, 350, 120, "", None, \
True, True)
self.outputEdit = self.noeWnd.getControlByIndex(index)
self.noeWnd.createButton("Check files", 60, 195, 80, 30, \
self.archivePackerButtonCheck)
self.noeWnd.createButton("Pack archive", 145, 195, 80, 30, \
self.archivePackerButtonPack)
self.noeWnd.createButton("Cancel", 330, 195, 80, 30, \
self.archivePackerButtonCancel)
self.packer = htArchivePacker(self.filename)
self.noeWnd.doModal()
#see if the pvr reorder context tool should be visible
def htArchivePackerVisible(toolIndex, selectedFile):
if selectedFile is None or \
os.path.splitext(selectedFile)[1].lower() != ".pro":
return 0
return 1
def htResourcePackerToolMethod(toolIndex):
srcPath = noesis.getSelectedFile()
if srcPath is None or os.path.exists(srcPath) is not True:
noesis.messagePrompt( \
"Selected file isn't readable through the standard filesystem.")
return 0
archivePackerDialogWindow(srcPath).create()
return 1
def resExtractRes(fileName, fileLen, justChecking):
if os.path.splitext(fileName)[1].lower() == ".pro":
archivePackerDialogWindow(fileName).create()
else:
with open(fileName, "rb") as f:
if justChecking: #it's valid
return 1
fileSections = ("PALETTEFILES", "SOUNDFILES", "TEXTUREFILES", \
"BACKFILES", "MASKFILES")
propertySections = ("COLORS", "MATERIALS", "SOUNDS")
filereader = NoeBitStream(f.read());
proStrings = []
while filereader.tell() < filereader.getSize():
str = filereader.readString()
proStrings.append(str + '\n')
name, count = str.split(" ")
if name in fileSections:
for x in range(int(count)):
fname = filereader.readString()
proStrings.append(fname + '\n')
size = filereader.readUInt()
fdata = filereader.readBytes(size)
print("Extracting " + fname.split(" ")[0])
rapi.exportArchiveFile(fname.split(" ")[0], fdata)
elif name in propertySections:
for x in range(int(count)):
proStrings.append(filereader.readString() + '\n')
else:
noesis.doException("Error: Wrong format.")
# write .pro file
proFilename = rapi.getOutputName() + os.path.basename(fileName) + '.pro'
with open(proFilename, "w") as text:
text.writelines(proStrings)
return 1