-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
296 lines (267 loc) · 11.9 KB
/
Program.cs
File metadata and controls
296 lines (267 loc) · 11.9 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
class Program
{
[StructLayout(LayoutKind.Sequential)]
public struct Vector3
{
public float X, Y, Z;
}
[StructLayout(LayoutKind.Sequential)]
public struct Triangle
{
public Vector3 P1, P2, P3;
}
[StructLayout(LayoutKind.Sequential)]
public struct Edge
{
public byte Next, Twin, Origin, Face;
}
private static bool InRange(char x, char a, char b) => x >= a && x <= b;
private static int GetBits(char x) => InRange(x, '0', '9') ? x - '0' : (x & (~0x20)) - 'A' + 0xA;
private static byte GetByte(string x, int index) => (byte)((GetBits(x[index]) << 4) | GetBits(x[index + 1]));
private static List<T> BytesToVec<T>(string bytes) where T : struct
{
try
{
var byteList = new List<byte>();
int i = 0;
while (i < bytes.Length)
{
if (bytes[i] == ' ')
{
i++;
}
else
{
if (i + 1 >= bytes.Length)
{
throw new FormatException("Invalid byte string format: incomplete byte pair.");
}
byteList.Add(GetByte(bytes, i));
i += 2;
}
}
var size = Marshal.SizeOf(typeof(T));
var result = new List<T>();
var buffer = new byte[size];
int bufferIndex = 0;
foreach (var b in byteList)
{
buffer[bufferIndex++] = b;
if (bufferIndex == size)
{
GCHandle handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
try
{
T value = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
result.Add(value);
}
finally
{
handle.Free();
}
bufferIndex = 0;
}
}
return result;
}
catch (Exception ex)
{
Console.WriteLine($"Error in BytesToVec: {ex.Message}");
throw;
}
}
private static List<int> GetCollisionAttributeIndices(Kv3Parser parser)
{
Console.WriteLine("Fetching collision attribute indices...");
var indices = new List<int>();
int index = 0;
while (true)
{
string collisionGroupString = parser.GetValue($"m_collisionAttributes[{index}].m_CollisionGroupString");
Console.WriteLine($"Checking m_collisionAttributes[{index}]: {collisionGroupString}");
if (string.IsNullOrEmpty(collisionGroupString))
{
break;
}
if (collisionGroupString == "\"default\"" || collisionGroupString == "\"Default\"")
{
indices.Add(index);
}
index++;
}
Console.WriteLine($"Found {indices.Count} collision attribute indices.");
return indices;
}
static void Main(string[] args)
{
try
{
Console.WriteLine("Starting .vphys file processing...");
if (args.Length == 0)
{
Console.WriteLine("Please drag and drop a .vphys file onto the executable.");
Console.ReadKey();
return;
}
string fileName = args[0];
Console.WriteLine($"Input file: {fileName}");
if (!File.Exists(fileName) || Path.GetExtension(fileName).ToLower() != ".vphys")
{
Console.WriteLine("Invalid or missing .vphys file.");
Console.ReadKey();
return;
}
string exportFileName = Path.ChangeExtension(fileName, ".tri");
Console.WriteLine($"Output file will be: {exportFileName}");
var parser = new Kv3Parser();
var triangles = new List<Triangle>();
Console.WriteLine("Reading file content...");
string content = File.ReadAllText(fileName);
Console.WriteLine($"File content length: {content.Length} characters");
Console.WriteLine("Parsing file content...");
parser.Parse(content);
Console.WriteLine("Getting collision attribute indices...");
var collisionAttributeIndices = GetCollisionAttributeIndices(parser);
// Process hulls
Console.WriteLine("Processing hulls...");
int index = 0;
int countHulls = 0;
while (true)
{
string collisionIndexStr = parser.GetValue($"m_parts[0].m_rnShape.m_hulls[{index}].m_nCollisionAttributeIndex");
if (string.IsNullOrEmpty(collisionIndexStr))
{
Console.WriteLine($"\nHulls: {index} (Total)");
Console.WriteLine($"Found {countHulls} hulls with tag 0");
break;
}
Console.WriteLine($"Processing hull[{index}] with collision index: {collisionIndexStr}");
int collisionIndex = int.Parse(collisionIndexStr);
if (collisionAttributeIndices.Contains(collisionIndex))
{
string vertexPositionsStr = parser.GetValue($"m_parts[0].m_rnShape.m_hulls[{index}].m_Hull.m_VertexPositions");
if (string.IsNullOrEmpty(vertexPositionsStr))
{
vertexPositionsStr = parser.GetValue($"m_parts[0].m_rnShape.m_hulls[{index}].m_Hull.m_Vertices");
}
Console.WriteLine($"Vertex positions string length: {vertexPositionsStr.Length}");
var vertexProcessed = BytesToVec<float>(vertexPositionsStr);
Console.WriteLine($"Vertices processed: {vertexProcessed.Count / 3}");
var vertices = new List<Vector3>();
for (int i = 0; i < vertexProcessed.Count; i += 3)
{
vertices.Add(new Vector3 { X = vertexProcessed[i], Y = vertexProcessed[i + 1], Z = vertexProcessed[i + 2] });
}
var facesProcessed = BytesToVec<byte>(parser.GetValue($"m_parts[0].m_rnShape.m_hulls[{index}].m_Hull.m_Faces"));
Console.WriteLine($"Faces processed: {facesProcessed.Count}");
var edgesTmp = BytesToVec<byte>(parser.GetValue($"m_parts[0].m_rnShape.m_hulls[{index}].m_Hull.m_Edges"));
var edgesProcessed = new List<Edge>();
for (int i = 0; i < edgesTmp.Count; i += 4)
{
edgesProcessed.Add(new Edge
{
Next = edgesTmp[i],
Twin = edgesTmp[i + 1],
Origin = edgesTmp[i + 2],
Face = edgesTmp[i + 3]
});
}
Console.WriteLine($"Edges processed: {edgesProcessed.Count}");
foreach (var startEdge in facesProcessed)
{
int edge = edgesProcessed[startEdge].Next;
int iteration = 0;
while (edge != startEdge)
{
if (iteration++ > 10000) // Prevent infinite loops
{
Console.WriteLine("Warning: Possible infinite loop detected in hull edge processing.");
break;
}
int nextEdge = edgesProcessed[edge].Next;
triangles.Add(new Triangle
{
P1 = vertices[edgesProcessed[startEdge].Origin],
P2 = vertices[edgesProcessed[edge].Origin],
P3 = vertices[edgesProcessed[nextEdge].Origin]
});
edge = nextEdge;
}
}
countHulls++;
}
index++;
}
// Process meshes
Console.WriteLine("Processing meshes...");
index = 0;
int countMeshes = 0;
while (true)
{
string collisionIndexStr = parser.GetValue($"m_parts[0].m_rnShape.m_meshes[{index}].m_nCollisionAttributeIndex");
if (string.IsNullOrEmpty(collisionIndexStr))
{
Console.WriteLine($"\nMeshes: {index} (Total)");
Console.WriteLine($"Found {countMeshes} meshes with tag 0");
break;
}
Console.WriteLine($"Processing mesh[{index}] with collision index: {collisionIndexStr}");
int collisionIndex = int.Parse(collisionIndexStr);
if (collisionAttributeIndices.Contains(collisionIndex))
{
var triangleProcessed = BytesToVec<int>(parser.GetValue($"m_parts[0].m_rnShape.m_meshes[{index}].m_Mesh.m_Triangles"));
Console.WriteLine($"Triangles processed: {triangleProcessed.Count / 3}");
var vertexProcessed = BytesToVec<float>(parser.GetValue($"m_parts[0].m_rnShape.m_meshes[{index}].m_Mesh.m_Vertices"));
Console.WriteLine($"Vertices processed: {vertexProcessed.Count / 3}");
var vertices = new List<Vector3>();
for (int i = 0; i < vertexProcessed.Count; i += 3)
{
vertices.Add(new Vector3 { X = vertexProcessed[i], Y = vertexProcessed[i + 1], Z = vertexProcessed[i + 2] });
}
for (int i = 0; i < triangleProcessed.Count; i += 3)
{
triangles.Add(new Triangle
{
P1 = vertices[triangleProcessed[i]],
P2 = vertices[triangleProcessed[i + 1]],
P3 = vertices[triangleProcessed[i + 2]]
});
}
countMeshes++;
}
index++;
}
// Write triangles to output file
Console.WriteLine($"Writing {triangles.Count} triangles to {exportFileName}...");
using (var fs = new FileStream(exportFileName, FileMode.Create))
using (var bw = new BinaryWriter(fs))
{
foreach (var triangle in triangles)
{
bw.Write(triangle.P1.X);
bw.Write(triangle.P1.Y);
bw.Write(triangle.P1.Z);
bw.Write(triangle.P2.X);
bw.Write(triangle.P2.Y);
bw.Write(triangle.P2.Z);
bw.Write(triangle.P3.X);
bw.Write(triangle.P3.Y);
bw.Write(triangle.P3.Z);
}
}
Console.WriteLine($"Processed file: {fileName} -> {exportFileName}");
}
catch (Exception ex)
{
Console.WriteLine($"Error processing file: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}