forked from M-1-RLG/io_scene_cmb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gar.py
172 lines (139 loc) · 5.92 KB
/
gar.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
import io, os
from io import BufferedReader
from .utils import *
from .ctxb import loadCtxb
class SystemFileGroup:
Ids: list[int] = []
def __init__(self, f: BufferedReader):
self.FileCount = readUInt32(f)
self.Unknown = readUInt32(f)
self.InfoOffset = readUInt32(f)
self.Name = readOffsetString(f,readUInt32(f))
self.OffsetToUnknown = readUInt32(f)
f.seek(f.tell()+12) # padding
class SystemFileInfo:
def __init__(self, f: BufferedReader, ext):
self.Ext = ext
self.DataSize = readUInt32(f)
self.DataOffset = readUInt32(f)
self.Name = readOffsetString(f,readUInt32(f))
f.seek(f.tell()+4) # padding
class FileGroup:
def __init__(self, f):
self.FileCount = readUInt32(f)
self.DataOffset = readUInt32(f)
self.InfoOffset = readUInt32(f)
f.seek(f.tell()+4) # padding
self.Ids = []
class FileInfo:
def __init__(self, f: BufferedReader, isZarFormat: bool):
self.DataSize = readUInt32(f)
self.Name = None if isZarFormat else readOffsetString(f,readUInt32(f))
tokens = os.path.splitext(readOffsetString(f,readUInt32(f)))
self.FileName = tokens[0]
self.Ext = tokens[1]
class FileEntry:
def __init__(self, name, ext, data):
self.FileName = name
self.Ext = ext
self.Data = data
class GAR:
class VersionMagic:
ZAR1 = 0 # OOT3D
GAR2 = 1 # MM3D
GAR5 = 2 # LM3DS
Files: list[FileEntry]
FileGroups: list[SystemFileGroup]
FileInfos: list[FileInfo]
def __init__(self, f: BufferedReader):
self.Files = []
self.FileGroups = []
self.FileInfos = []
self.Signature = readString(f, 4)
if self.Signature == "ZAR\x01":
self.Version = self.VersionMagic.ZAR1
elif self.Signature == "GAR\x02":
self.Version = self.VersionMagic.GAR2
elif self.Signature == "GAR\x05":
self.Version = self.VersionMagic.GAR5
self.FileSize = readUInt32(f)
self.FileGroupCount = readUShort(f)
self.FileCount = readUShort(f)
self.FileGroupOffset = readUInt32(f)
self.FileInfoOffset = readUInt32(f)
self.DataOffset = readUInt32(f)
self.Codename = readString(f, 0x08)
match self.Codename:
case "queen" | "jenkins":
self.readZeldaArchive(f)
case "agora" | "SYSTEM":
self.readSystemGrezzoArchive(f)
case _:
raise Exception(f"Unexpected codename! {self.Codename}")
def readSystemGrezzoArchive(self, f: BufferedReader):
f.seek(self.FileGroupOffset)
for i in range(self.FileGroupCount):
self.FileGroups.append(SystemFileGroup(f))
f.seek(self.FileInfoOffset)
for i in range(self.FileGroupCount):
for _ in range(self.FileGroups[i].FileCount):
self.FileInfos.append(SystemFileInfo(f, self.FileGroups[i].Name))
f.seek(self.DataOffset)
for i in range(self.FileCount):
info = self.FileInfos[i]
self.Files.append(FileEntry(info.Name, info.Ext, self.getSection(f, info.DataOffset, info.DataSize)))
def readZeldaArchive(self, f: BufferedReader):
f.seek(self.FileGroupOffset)
for i in range(self.FileGroupCount):
self.FileGroups.append(FileGroup(f))
for i in range(self.FileGroupCount):
self.FileGroups[i].Ids = readArray(f, self.FileGroups[i].FileCount, DataTypes.UInt)
f.seek(self.FileInfoOffset)
for i in range(self.FileGroupCount):
for f in range(self.FileGroups[i].FileCount):
self.FileInfos.append(FileInfo(f, self.Version == self.VersionMagic.ZAR1))
f.seek(self.DataOffset)
Offsets = readArray(f, self.FileCount, DataTypes.UInt)
for i in range(len(self.FileInfos)):
self.Files.append(FileEntry(self.FileInfos[i].FileName, self.getSection(f, Offsets[i], self.FileInfos[i].DataSize)))
def getSection(self, f: BufferedReader, offset, size):
f.seek(offset, io.SEEK_SET)
return f.read(size)
def loadGar(garReader: BufferedReader, folderName, collection, parent):
gar = GAR(garReader)
firstModel = None
group = None
for file in reversed(gar.Files):
if file.Ext == "ctxb":
if not os.path.exists(folderName):
os.mkdir(folderName)
loadCtxb(io.BufferedReader(io.BytesIO(file.Data)), folderName, file.FileName)
if file.Ext == "cmb":
from .import_cmb import loadCmbSafe
model = loadCmbSafe(io.BufferedReader(io.BytesIO(file.Data)), file.FileName, folderName, collection, parent)
if firstModel == None:
firstModel = model
else:
if group == None:
group = bpy.data.objects.new(folderName.replace(os.path.dirname(folderName),"").strip(os.path.sep), None)
collection.objects.link(group)
group.parent = parent
firstModel.parent = group
model.parent = group
if file.Ext == "gar":
childFolderName = os.path.join(folderName, file.FileName.replace("_tex", ""))
if not os.path.exists(folderName):
os.mkdir(folderName)
loadGar(io.BufferedReader(io.BytesIO(file.Data)), childFolderName, collection, parent)
return firstModel if group == None else group
def loadGarFiles(operator):
root = get_or_add_root()
dirname = os.path.dirname(operator.filepath)
for file in operator.files:
path = os.path.join(dirname, file.name)
folderName = os.path.splitext(path)[0]
if not os.path.exists(folderName):
os.mkdir(folderName)
with open(path, "rb") as reader:
loadGar(reader, folderName, bpy.context.scene.collection, root)
return {"FINISHED"}