forked from MontagueM/Phonon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Package.cs
189 lines (176 loc) · 6.32 KB
/
Package.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
180
181
182
183
184
185
186
187
188
189
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Phonon
{
public static class StructConverter
{
public static T ToStructure<T>(this byte[] bytes) where T : struct
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try { return (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); }
finally { handle.Free(); }
}
public static T ToClass<T>(this byte[] bytes) where T : class
{
GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);
try { return (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T)); }
finally { handle.Free(); }
}
}
public struct PkgHeader
{
public ushort PkgID;
public ushort PatchID;
public uint EntryTableOffset;
public uint EntryTableCount;
}
[StructLayout(LayoutKind.Sequential)]
public struct PkgEntry
{
public uint Reference;
public uint EntryB;
public uint EntryC;
public uint EntryD;
}
public class Package
{
public string Path;
public string Name; // The name excludes the patch ID
private BinaryReader Handle;
public PkgHeader Header;
private List<int> DynamicHashIndices;
public List<Dynamic> Dynamics;
public PhononType ePhononType;
public Package(PhononType ePhononType)
{
this.ePhononType = ePhononType;
}
public Package(string PkgPath, PhononType ePhononType)
{
Path = PkgPath;
this.ePhononType = ePhononType;
MakeName();
GetHandle();
ReadHeader();
CloseHandle();
}
public string MakeName()
{
Name = Path.Split("\\").Last();
Name = Name.Substring(4, Name.Length - 10);
return Name;
}
public void GetDynamics(string PackagesPath)
{
Dynamics = new List<Dynamic>();
foreach(int DynamicHashIndex in DynamicHashIndices)
{
Dynamic dynamic = new Dynamic((UInt32)(0x80800000 + DynamicHashIndex + Header.PkgID * 8192));
bool success = dynamic.GetDynamicInfo(PackagesPath, ePhononType);
if (success)
{
Dynamics.Add(dynamic);
}
}
}
public bool GetHandle()
{
Handle = new BinaryReader(File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read));
return true;
}
public bool CloseHandle()
{
if (Handle != null)
{
Handle.Close();
return true;
}
return false;
}
public void ReadHeader()
{
if (Header.PkgID > 0)
{
return;
}
Header = new PkgHeader();
if (ePhononType == PhononType.Destiny2BL)
{
Handle.BaseStream.Seek(0x10, SeekOrigin.Begin);
Header.PkgID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0x30, SeekOrigin.Begin);
Header.PatchID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0x44, SeekOrigin.Begin);
Header.EntryTableOffset = Handle.ReadUInt32();
Handle.BaseStream.Seek(0x60, SeekOrigin.Begin);
Header.EntryTableCount = Handle.ReadUInt32();
}
else if (ePhononType == PhononType.Destiny2PREBL)
{
Handle.BaseStream.Seek(0x04, SeekOrigin.Begin);
Header.PkgID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0x20, SeekOrigin.Begin);
Header.PatchID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0x110, SeekOrigin.Begin);
Header.EntryTableOffset = Handle.ReadUInt32() + 96;
Handle.BaseStream.Seek(0xB4, SeekOrigin.Begin);
Header.EntryTableCount = Handle.ReadUInt32();
}
else if (ePhononType == PhononType.Destiny1)
{
Handle.BaseStream.Seek(0x04, SeekOrigin.Begin);
Header.PkgID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0x20, SeekOrigin.Begin);
Header.PatchID = Handle.ReadUInt16();
Handle.BaseStream.Seek(0xB8, SeekOrigin.Begin);
Header.EntryTableOffset = Handle.ReadUInt32();
Handle.BaseStream.Seek(0xB4, SeekOrigin.Begin);
Header.EntryTableCount = Handle.ReadUInt32();
}
}
public bool GetDynamicIndices()
{
GetHandle();
ReadHeader();
// Check entries until we find a dynamic. If not, return false
DynamicHashIndices = new List<int>();
Handle.BaseStream.Seek(Header.EntryTableOffset, SeekOrigin.Begin);
for (int i = 0; i < Header.EntryTableCount; i++)
{
PkgEntry NewPkgEntry = new PkgEntry();
byte[] buffer = new byte[0x10];
Handle.BaseStream.Read(buffer, 0, 0x10);
NewPkgEntry = StructConverter.ToStructure<PkgEntry>(buffer);
uint DynamicRef = 0;
if (ePhononType == PhononType.Destiny2BL)
{
DynamicRef = 0x80809AD8;
}
else if (ePhononType == PhononType.Destiny2PREBL)
{
DynamicRef = 0x80809C0F;
}
else if (ePhononType == PhononType.Destiny1)
{
DynamicRef = 0x80800734;
}
if (NewPkgEntry.Reference == DynamicRef)
{
//System.Diagnostics.Debug.WriteLine("i: " + i + " Ref: " + NewPkgEntry.Reference + " Pkg: " + Header.PkgID + " Pkg entry offset: " + Header.EntryTableOffset);
DynamicHashIndices.Add(i);
}
}
CloseHandle();
if (DynamicHashIndices.Count > 0)
{
return true;
}
return false;
}
}
}