-
Notifications
You must be signed in to change notification settings - Fork 6
/
AssetPackage.cs
179 lines (134 loc) · 5.47 KB
/
AssetPackage.cs
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
namespace Marathon.Formats.Package
{
/// <summary>
/// File base for the *.pkg format.
/// <para>Used in SONIC THE HEDGEHOG for defining specific assets with friendly names.</para>
/// </summary>
public class AssetPackage : FileBase
{
public AssetPackage() { }
public AssetPackage(string file, bool serialise = false)
{
switch (Path.GetExtension(file))
{
case ".json":
{
Types = JsonDeserialise<List<AssetType>>(file);
// Save extension-less JSON (exploiting .NET weirdness, because it doesn't omit all extensions).
if (serialise)
Save(Path.GetFileNameWithoutExtension(file));
break;
}
default:
{
Load(file);
if (serialise)
JsonSerialise(Types);
break;
}
}
}
public override string Extension { get; } = ".pkg";
public List<AssetType> Types { get; set; } = new();
public override void Load(Stream stream)
{
BINAReader reader = new(stream);
// File related header stuff.
uint fileCount = reader.ReadUInt32();
uint fileEntriesPos = reader.ReadUInt32();
// Type related header stuff.
uint typeCount = reader.ReadUInt32();
uint typeEntriesPos = reader.ReadUInt32();
// Read Types.
reader.JumpTo(typeEntriesPos, true);
for (int i = 0; i < typeCount; i++)
{
AssetType type = new();
// Store offsets for later.
uint namePos = reader.ReadUInt32();
uint typeFileCount = reader.ReadUInt32();
uint filesPos = reader.ReadUInt32();
// Store position in file.
long position = reader.BaseStream.Position;
// Jump to namePos and read null terminated string for type name.
reader.JumpTo(namePos, true);
type.Name = reader.ReadNullTerminatedString();
reader.JumpTo(filesPos, true);
// Read objects within this type.
for (int f = 0; f < typeFileCount; f++)
{
AssetFile file = new();
uint friendlyNamePos = reader.ReadUInt32();
uint filePathPos = reader.ReadUInt32();
// Jump to offsets and read strings for the file name and path.
long iteratorPosition = reader.BaseStream.Position;
reader.JumpTo(friendlyNamePos, true);
file.Name = reader.ReadNullTerminatedString();
reader.JumpTo(filePathPos, true);
file.File = reader.ReadNullTerminatedString();
// Save file entry and return to the previously stored position.
type.Files.Add(file);
reader.JumpTo(iteratorPosition);
}
Types.Add(type);
reader.JumpTo(position);
}
}
public override void Save(Stream stream)
{
BINAWriter writer = new(stream);
// Storage for calculated file count.
uint filesCount = 0;
// Calculate what we should set the file count to.
for (int i = 0; i < Types.Count; i++)
for (int c = 0; c < Types[i].Files.Count; c++)
filesCount++;
// Write file count.
writer.Write(filesCount);
// Store offset for file entries.
writer.AddOffset("fileEntriesPos");
// Write type count.
writer.Write(Types.Count);
// Store offset for type entries.
writer.AddOffset("typeEntriesPos");
// Fill in types offset just before we write the type data.
writer.FillOffset("typeEntriesPos", true);
// Write type data.
for (int i = 0; i < Types.Count; i++)
{
writer.AddString($"typeName{i}", Types[i].Name);
writer.Write(Types[i].Files.Count);
writer.AddOffset($"typeFilesOffset{i}");
}
// Fill in files offset just before we write the file data.
writer.FillOffset("fileEntriesPos", true);
// Storage for number of files written.
int objectNum = 0;
// Write file data.
for (int i = 0; i < Types.Count; i++)
{
writer.FillOffset($"typeFilesOffset{i}", true);
for (int f = 0; f < Types[i].Files.Count; f++)
{
writer.AddString($"friendlyName{objectNum}", Types[i].Files[f].Name);
writer.AddString($"filePath{objectNum}", Types[i].Files[f].File);
objectNum++;
}
}
// Write the footer.
writer.FinishWrite();
}
}
public class AssetType
{
public string Name { get; set; }
public List<AssetFile> Files { get; set; } = new();
public override string ToString() => Name;
}
public class AssetFile
{
public string Name { get; set; }
public string File { get; set; }
public override string ToString() => Name;
}
}