forked from MontagueM/Phonon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dynamic.cs
146 lines (132 loc) · 5.91 KB
/
Dynamic.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
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 class Dynamic
{
public uint Hash;
public string HashString;
public int MeshCount;
public bool bHasSkeleton;
public List<float[]> Vertices;
public List<uint[]> Faces;
public Dynamic(uint DynamicHash)
{
Hash = DynamicHash;
}
static string LittleEndian(uint number)
{
byte[] bytes = BitConverter.GetBytes(number);
string retval = "";
foreach (byte b in bytes)
retval += b.ToString("X2");
return retval;
}
public string GetHashString()
{
HashString = LittleEndian(Hash);
return HashString;
}
public bool GetDynamicInfo(string PackagesPath, PhononType ePhononType)
{
GetHashString();
// TODO do this on a separate thread as to not lag the UI
bool status = false;
[DllImport("DestinyDynamicExtractorBL.dll", EntryPoint="RequestDynamicInformation")]
static extern bool RequestDynamicInformationBL([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath, ref int MeshCount, ref bool bHasSkeleton);
[DllImport("DestinyDynamicExtractorPREBL.dll", EntryPoint = "RequestDynamicInformation")]
static extern bool RequestDynamicInformationPREBL([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath, ref int MeshCount, ref bool bHasSkeleton);
[DllImport("DestinyDynamicExtractorD1.dll", EntryPoint = "RequestDynamicInformation")]
static extern bool RequestDynamicInformationD1([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath, ref int MeshCount, ref bool bHasSkeleton);
if (ePhononType == PhononType.Destiny2BL)
{
status = RequestDynamicInformationBL(HashString, PackagesPath, ref MeshCount, ref bHasSkeleton);
}
else if (ePhononType == PhononType.Destiny2PREBL)
{
status = RequestDynamicInformationPREBL(HashString, PackagesPath, ref MeshCount, ref bHasSkeleton);
}
else if (ePhononType == PhononType.Destiny1)
{
status = RequestDynamicInformationD1(HashString, PackagesPath, ref MeshCount, ref bHasSkeleton);
}
return status && (MeshCount > 0);
}
public bool GetDynamicMesh(string PackagesPath, PhononType ePhononType)
{
Vertices = new List<float[]>();
Faces = new List<uint[]>();
[DllImport("DestinyDynamicExtractorBL.dll", EntryPoint = "RequestSaveDynamicMeshData")]
static extern bool RequestSaveDynamicMeshDataBL([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath);
[DllImport("DestinyDynamicExtractorPREBL.dll", EntryPoint = "RequestSaveDynamicMeshData")]
static extern bool RequestSaveDynamicMeshDataPREBL([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath);
[DllImport("DestinyDynamicExtractorD1.dll", EntryPoint = "RequestSaveDynamicMeshData")]
static extern bool RequestSaveDynamicMeshDataD1([MarshalAs(UnmanagedType.LPStr)] string DynamicHash, [MarshalAs(UnmanagedType.LPStr)] string pkgsPath);
var a = Hash.ToString("X8");
bool status = false;
if (ePhononType == PhononType.Destiny2BL)
{
status = RequestSaveDynamicMeshDataBL(a, PackagesPath);
}
else if (ePhononType == PhononType.Destiny2PREBL)
{
status = RequestSaveDynamicMeshDataPREBL(a, PackagesPath);
}
else if (ePhononType == PhononType.Destiny1)
{
status = RequestSaveDynamicMeshDataD1(a, PackagesPath);
}
return ReadMeshData(Vertices, Faces) && status;
}
public bool ReadMeshData(List<float[]> Vertices, List<uint[]> Faces)
{
BinaryReader Handle = new BinaryReader(File.Open("msh.tmp", FileMode.Open));
// We'll import it as one big mesh
uint FaceCounter = 0;
while (Handle.BaseStream.Position != Handle.BaseStream.Length)
{
uint VertexCount = 0;
try
{
VertexCount = Handle.ReadUInt32();
}
catch (System.IO.EndOfStreamException e)
{
System.Windows.MessageBox.Show("Mesh file broken, deleting");
Handle.Close();
File.Delete("msh.tmp");
return false;
}
for (int i = 0; i < VertexCount; i++)
{
float[] Vertex = new float[3];
for (int j = 0; j < 3; j++)
{
Vertex[j] = Handle.ReadSingle();
}
Vertices.Add(Vertex);
}
uint FaceCount = Handle.ReadUInt32();
for (int i = 0; i < FaceCount; i++)
{
uint[] Face = new uint[3];
for (int j = 0; j < 3; j++)
{
uint f = Handle.ReadUInt32();
Face[j] = FaceCounter + f; // Account for it being one mesh
}
Faces.Add(Face);
}
FaceCounter += VertexCount;
//if (q++ == 1) break;
}
Handle.Close();
return true;
}
}
}