-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathvphys_parser.cpp
198 lines (166 loc) · 7.4 KB
/
vphys_parser.cpp
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
#include "kv3-parser.hpp"
#include <fstream>
#include <stdlib.h>
#include <chrono>
#include <filesystem>
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;
namespace fs = std::filesystem;
// credits tni & learn_more (pasted from www.unknowncheats.me/forum/3868338-post34.html)
#define INRANGE(x,a,b) (x >= a && x <= b)
#define getBits( x ) (INRANGE(x,'0','9') ? (x - '0') : ((x&(~0x20)) - 'A' + 0xa))
#define get_byte( x ) (getBits(x[0]) << 4 | getBits(x[1]))
template <typename Ty>
vector<Ty> bytes_to_vec(const string& bytes)
{
const auto num_bytes = bytes.size() / 3;
const auto num_elements = num_bytes / sizeof(Ty);
vector<Ty> vec;
vec.resize(num_elements + 1);
const char* p1 = bytes.c_str();
uint8_t* p2 = reinterpret_cast<uint8_t*>(vec.data());
while (*p1 != '\0')
{
if (*p1 == ' ')
{
++p1;
}
else
{
*p2++ = get_byte(p1);
p1 += 2;
}
}
return vec;
}
typedef struct Vector3 {
float x, y, z;
};
typedef struct Triangle {
Vector3 p1, p2, p3;
};
typedef struct Edge {
uint8_t next, twin, origin, face;
};
vector<string> get_vphys_files() {
vector<string> vphys_files;
for (const auto& entry : fs::directory_iterator(".")) {
if (entry.path().extension() == ".vphys") {
vphys_files.push_back(entry.path().string());
}
}
return vphys_files;
}
int main()
{
vector<string> vphys_files = get_vphys_files();
for (const auto& file_name : vphys_files) {
string export_file_name = fs::path(file_name).stem().string() + ".tri";
c_kv3_parser parser;
vector<Triangle> triangles;
ifstream in(file_name, ios::in);
istreambuf_iterator<char> beg(in), end;
string strdata(beg, end);
in.close();
parser.parse(strdata);
string().swap(strdata);
int index = 0;
int count_hulls = 0;
int count_meshes = 0;
//check hulls
while (true) {
string index_str = to_string(index);
string collision_index_str = parser.get_value("m_parts[0].m_rnShape.m_hulls[" + index_str + "].m_nCollisionAttributeIndex");
if (collision_index_str != "") {
int collision_index = atoi(collision_index_str.c_str());
if (collision_index == 0) {
vector<float> vertex_processed{};
string vertex_positions_str = parser.get_value("m_parts[0].m_rnShape.m_hulls[" + index_str + "].m_Hull.m_VertexPositions");
if (!vertex_positions_str.empty())
vertex_processed = bytes_to_vec<float>(vertex_positions_str);
else
vertex_processed = bytes_to_vec<float>(parser.get_value("m_parts[0].m_rnShape.m_hulls[" + index_str + "].m_Hull.m_Vertices"));
vector<Vector3> vertices;
for (int i = 0; i < vertex_processed.size(); i += 3) {
vertices.push_back({ vertex_processed[i], vertex_processed[i + 1], vertex_processed[i + 2] });
}
vector<float>().swap(vertex_processed);
vector<uint8_t> faces_processed = bytes_to_vec<uint8_t>(parser.get_value("m_parts[0].m_rnShape.m_hulls[" + index_str + "].m_Hull.m_Faces"));
vector<uint8_t> edges_tmp = bytes_to_vec<uint8_t>(parser.get_value("m_parts[0].m_rnShape.m_hulls[" + index_str + "].m_Hull.m_Edges"));
vector<Edge> edges_processed;
for (int i = 0; i < edges_tmp.size(); i += 4) {
edges_processed.push_back({ edges_tmp[i], edges_tmp[i + 1], edges_tmp[i + 2], edges_tmp[i + 3] });
}
vector<uint8_t>().swap(edges_tmp);
for (auto start_edge : faces_processed) {
int edge = edges_processed[start_edge].next;
while (edge != start_edge) {
int nextEdge = edges_processed[edge].next;
triangles.push_back(
{
vertices[edges_processed[start_edge].origin],
vertices[edges_processed[edge].origin],
vertices[edges_processed[nextEdge].origin]
}
);
edge = nextEdge;
}
}
vector<uint8_t>().swap(faces_processed);
vector<Edge>().swap(edges_processed);
vector<Vector3>().swap(vertices);
count_hulls++;
}
}
else {
cout << endl << "Hulls: " << index << " (Total)" << endl;
cout << endl << "Founded " << count_hulls << " hulls with tag 0" << endl;
break;
}
index++;
}
//reset index and check meshes
index = 0;
while (true) {
string index_str = to_string(index);
string collision_index_str = parser.get_value("m_parts[0].m_rnShape.m_meshes[" + index_str + "].m_nCollisionAttributeIndex");
if (collision_index_str != "") {
int collision_index = atoi(collision_index_str.c_str());
if (collision_index == 0) {
vector<int> triangle_processed = bytes_to_vec<int>(parser.get_value("m_parts[0].m_rnShape.m_meshes.[" + index_str + "].m_Mesh.m_Triangles"));
vector<float> vertex_processed = bytes_to_vec<float>(parser.get_value("m_parts[0].m_rnShape.m_meshes.[" + index_str + "].m_Mesh.m_Vertices"));
vector<Vector3> vertices;
for (int i = 0; i < vertex_processed.size(); i += 3) {
vertices.push_back({ vertex_processed[i], vertex_processed[i + 1], vertex_processed[i + 2] });
}
vector<float>().swap(vertex_processed);
for (int i = 0; i < triangle_processed.size(); i += 3) {
triangles.push_back({ vertices[triangle_processed[i]], vertices[triangle_processed[i + 1]], vertices[triangle_processed[i + 2]] });
}
vector<int>().swap(triangle_processed);
vector<Vector3>().swap(vertices);
count_meshes++;
}
}
else {
cout << endl << "Meshes: " << index << " (Total)" << endl;
cout << endl << "Founded " << count_meshes << " meshes with tag 0" << endl;
break;
}
index++;
}
parser.~c_kv3_parser();
ofstream out(export_file_name, ios::out | ios::binary);
out.write(reinterpret_cast<const char*>(triangles.data()), triangles.size() * sizeof(Triangle));
out.close();
cout << "Processed file: " << file_name << " -> " << export_file_name << endl;
// Clear variables for next iteration
triangles.clear();
}
system("pause");
return 0;
}