-
Notifications
You must be signed in to change notification settings - Fork 0
/
nemesis.py
198 lines (159 loc) · 7.79 KB
/
nemesis.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
# MIT License
#
# Copyright (c) 2021 Christopher Holzmann Pérez
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from typing import OrderedDict
from binary_reader import BinaryReader
import argparse
import json
import zlib
import os
def extractREGFILE (path, game):
metadata = OrderedDict()
f = open(path, "rb")
reader = BinaryReader(f.read())
if reader.read_str(4) != 'RGF.':
raise Exception('Incorrect magic. Expected RGF.')
if game == "X":
badCombinations = badCombinationsX
elif game == "XP":
badCombinations = badCombinationsXProto
elif game == "X2":
badCombinations = badCombinationsX2
#Create unpack folder
unpackFolderPath = path + ".unpack"
if not os.path.exists(unpackFolderPath):
os.makedirs(unpackFolderPath)
regfileSize = reader.read_uint32()
ptrAudioContainer = reader.read_uint32()
folderAmount = reader.read_uint32()
reader.seek(reader.pos() + 0x10)
#Get folder info
for folder in range(folderAmount):
folderName = reader.read_str(0xA)
metadata[folderName] = dict()
#Skies of Deception
if game == "X" or game == "XP":
folderUnknown1 = reader.read_uint16()
folderUnknown2 = reader.read_uint16()
folderUnknown3 = reader.read_uint16()
reader.seek(reader.pos() + 0x14) #Skip the 1 and the padding, always the same
folderUnknown4 = reader.read_uint16()
metadata[folderName]["folderUnknown1"] = folderUnknown1
metadata[folderName]["folderUnknown2"] = folderUnknown2
metadata[folderName]["folderUnknown3"] = folderUnknown3
metadata[folderName]["folderUnknown4"] = folderUnknown4
fileAmount = reader.read_uint16() #Files in the folder
ptrFileSection = reader.read_uint32()
folderUnknown5 = reader.read_uint16()
folderUnknown6 = reader.read_uint16()
metadata[folderName]["folderUnknown5"] = folderUnknown5
metadata[folderName]["folderUnknown6"] = folderUnknown6
#Create folder
folderPath = unpackFolderPath + "/" + folderName
if not os.path.exists(folderPath):
os.makedirs(folderPath)
print ("Folder", folderName, "(" + str(fileAmount) + " files)")
metadata[folderName]["Files"] = dict()
#Extract files
if fileAmount > 0:
posFolderTable = reader.pos()
reader.seek(ptrFileSection)
for i in range(fileAmount):
fileName = reader.read_str(0xC)
metadata[folderName]["Files"][fileName] = dict()
fileUnknown1 = reader.read_int16()
fileUnknown2 = reader.read_uint16()
metadata[folderName]["Files"][fileName]["fileUnknown1"] = fileUnknown1
metadata[folderName]["Files"][fileName]["fileUnknown2"] = fileUnknown2 #Possible ID
if [fileUnknown1, fileUnknown2] in badCombinations:
print("Bad combination. Dummy file", fileName)
fileData = bytearray()
else:
fileSize = reader.read_uint32() - 0x4
fileData = reader.read_bytes(fileSize)
try: #Check header and attempt decompression
readertemp = BinaryReader(fileData)
magic = readertemp.read_str(4)
if magic == "DEF.":
metadata[folderName]["Files"][fileName]["Compressed"] = True
readertemp.seek(0x10)
compressedFile = readertemp.read_bytes(fileSize-0x10)
fileData = zlib.decompress(compressedFile)
else:
metadata[folderName]["Files"][fileName]["Compressed"] = False
except:
metadata[folderName]["Files"][fileName]["Compressed"] = False
savePath = folderPath + "/" + fileName
with open(savePath , 'wb') as file:
file.write(fileData)
file.close()
reader.seek(posFolderTable)
#Extract audio section
print ("Extracting raw audio section...")
reader.seek(ptrAudioContainer)
audioSection = reader.read_bytes(reader.size()-ptrAudioContainer)
with open(unpackFolderPath + "/audio_section.dat" , 'wb') as file:
file.write(audioSection)
file.close()
#Save metadata
print("Saving metadata...")
with open(unpackFolderPath + "/metadata.json" , 'w') as file:
json.dump(metadata, file, indent=2, ensure_ascii=False)
file.close()
# Any of these combinations ([fileUnknown1, fileUnknown2]) correspond to a dummy file without size or data
badCombinationsX = [
[-256, 1280],
]
badCombinationsXProto = [
[-256, 1276],
]
badCombinationsX2 = [
[0, 1780],
[-256, 1781],
[-256, 7399],
[-256, 7400],
[-256, 7401],
[-256, 7402],
[-256, 7403],
[256, 1772],
[256, 1773],
]
if __name__ == '__main__':
print(r'''
███╗ ██╗███████╗███╗ ███╗███████╗███████╗██╗███████╗
████╗ ██║██╔════╝████╗ ████║██╔════╝██╔════╝██║██╔════╝
██╔██╗ ██║█████╗ ██╔████╔██║█████╗ ███████╗██║███████╗
██║╚██╗██║██╔══╝ ██║╚██╔╝██║██╔══╝ ╚════██║██║╚════██║
██║ ╚████║███████╗██║ ╚═╝ ██║███████╗███████║██║███████║
╚═╝ ╚═══╝╚══════╝╚═╝ ╚═╝╚══════╝╚══════╝╚═╝╚══════╝''' +'\n')
print("Ace Combat's REGFILE.CDI unpacker\n\n")
parser = argparse.ArgumentParser()
parser.add_argument("input", help='Input file (REGFILE.CDI) to unpack', type=str)
parser.add_argument("-g", "--game", required=False, help='X = Ace Combat X: Skies of Deception, XP = Ace Combat X: Skies of Deception [16-05-2006 Prototype], X2 = Ace Combat: Joint Assault')
args = parser.parse_args()
path = args.input
game = args.game
if game:
game = game.upper()
if os.path.isfile(path):
while game not in ["X", "XP", "X2"]:
game = input("Select origin game:\nX = Ace Combat X: Skies of Deception\nXP = Ace Combat X: Skies of Deception [16-05-2006 Prototype]\nX2 = Ace Combat: Joint Assault\n\nGame: ").upper()
extractREGFILE(path, game)