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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
//---------------------------------------------------------------------------//
// Copyright (c) 2017-2024 Ismael Gutiérrez González. All rights reserved.
//
// This file is part of the Rusted PackFile Manager (RPFM) project,
// which can be found here: https://github.com/Frodo45127/rpfm.
//
// This file is licensed under the MIT license, which can be found here:
// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
//---------------------------------------------------------------------------//
//! Module containing tests for decoding/encoding `Cs2Parsed` files.
use ;
use File;
//use cs2_collision::Cs2Collision;
//use cs2_parsed::Transform4x4;
use crateReadBytes;
use crate*;
use Cs2Parsed;
/*
#[test]
fn test_encode_cs2_parsed_migrate_to_rome_2() {
let games = SupportedGames::default();
let game = games.game(KEY_ROME_2).unwrap();
let paths = files_from_subdir(&PathBuf::from("../test_files/cs2_files/"), true).unwrap();
for path in &paths {
if path.to_string_lossy().ends_with("cs2.parsed") {
let mut reader = BufReader::new(File::open(&path).unwrap());
let decodeable_extra_data = DecodeableExtraData::default();
let mut data = Cs2Parsed::decode(&mut reader, &Some(decodeable_extra_data)).unwrap();
data.migrate_game(game).unwrap();
let mut after = vec![];
data.encode(&mut after, &None).unwrap();
let mut writer = BufWriter::new(File::create(path).unwrap());
writer.write_all(&after).unwrap();
}
}
}*/
/*
#[test]
fn test_assladers_begone() {
let remove_assladders = true;
let patch_walls = false;
let base_path = "C:/Users/frodo/Desktop/assladers/rigidmodels/buildings";
let patched_platforms_path = "C:/Users/frodo/Desktop/assladers/rigidmodels/buildings_prepatched_platforms";
let dest_path = PathBuf::from("C:/Users/frodo/Desktop/assladers/rigidmodels/buildings_fixed");
let paths = files_from_subdir(&PathBuf::from(base_path), true).unwrap();
let prepatched_paths = files_from_subdir(&PathBuf::from(patched_platforms_path), true).unwrap();
for path in &paths {
let file_name = path.file_name().unwrap().to_string_lossy().to_string();
if file_name.ends_with(".cs2.parsed") {
println!("Checking file: {}", path.to_string_lossy().to_string().replace("\\", "/"));
let mut reader = BufReader::new(File::open(&path).unwrap());
let decodeable_extra_data = DecodeableExtraData::default();
let data_len = reader.len().unwrap();
let before = reader.read_slice(data_len as usize, true).unwrap();
match Cs2Parsed::decode(&mut reader, &Some(decodeable_extra_data.clone())) {
Ok(mut data) => {
let mut after = vec![];
if remove_assladders {
// Remove ladder pipes.
for piece in &mut data.pieces {
for destruct in &mut piece.destructs {
dbg!(&destruct.pipes);
destruct.pipes.retain(|pipe| !pipe.name.contains("ladder"));
}
}
}
if patch_walls {
// Fixes for the wall pillars. These should be applied to any wall piece of the following type:
// - straigh_20m
if file_name.contains("_wall_straight_20m_") {
if let Some(piece) = data.pieces.first_mut() {
if let Some(destruct) = piece.destructs.last_mut() {
// Add left and right model data only to the last destruct of piece01.
if destruct.file_refs.is_empty() {
let mut file_name_reduced = file_name.to_owned();
file_name_reduced.truncate(file_name.find(".").unwrap());
let mut right = FileRef::default();
right.name = format!("{}_bridging_column_right", file_name_reduced);
right.key = format!("{}_file:{}", piece.name, right.name);
right.uk_1 = 19;
right.transform = Transform4x4::identity();
destruct.file_refs.push(right);
let mut left = FileRef::default();
left.name = format!("{}_bridging_column_left", file_name_reduced);
left.key = format!("{}_file:{}", piece.name, left.name);
left.uk_1 = 20;
left.transform = Transform4x4::identity();
destruct.file_refs.push(left);
}
// Remove all collisions.
destruct.collision_outlines.clear();
destruct.orange_thingies.clear();
// Check if we have prepatched platforms for it, and use them instead of their own.
let patched_path_end = path.strip_prefix(base_path).unwrap();
let patched_path = PathBuf::from(patched_platforms_path).join(patched_path_end);
// TODO: USE GLB instead of faction specific as a fallback.
if let Some(patched_path) = prepatched_paths.iter().find(|x| **x == patched_path) {
let mut reader = BufReader::new(File::open(&patched_path).unwrap());
if let Ok(data) = Cs2Parsed::decode(&mut reader, &Some(decodeable_extra_data)) {
if let Some(destruct_patched) = data.pieces.iter().find_map(|x| x.destructs.iter().find(|x| x.name == destruct.name)) {
println!("Platforms patched with prepatched file: {}", patched_path.to_string_lossy());
destruct.platforms.clear();
destruct.platforms = destruct_patched.platforms.to_vec();
}
}
}
/*
// Add new platforms to make the new collision-less sides walkable.
// The best way I have been able to come up with to do it:
// - Find the biggest vertex to start calculating the perimeter.
// - Find all perimeter vertex via the "look back and find the first line" trick.
// - Go through the vertex, and split them in concave areas.
// - Merge said areas with the border of the 20x20 cube.
if let Some((_, starting_point)) = destruct.platforms.iter()
.map(|x| {
let outline = x.vertices.outline();
outline.iter()
.map(|x| (((x.x() + x.z()) / 2.0), x))
.max_by(|x, y| x.0.total_cmp(&y.0)).unwrap()
})
.max_by(|x, y| {
x.0.total_cmp(&y.0)
}) {
let mut points = vec![starting_point.clone()];
loop {
let pre_vertex = if let Some(ver) = points.get((points.len() as i32 - 2) as usize) { ver.clone() } else { Point3d::new(20.0, 0.0, 6.0) };
let current_vertex = points.last().unwrap();
match destruct.platforms.iter()
.map(|x| x.vertices.outline())
.filter(|x| x.contains(¤t_vertex)) // Get only the platforms with our vertex, because we are searching one that has an external edge.
.map(|x| x.iter()
.filter(|y| !points.contains(y))
.filter_map(|y| {
// We need to get the angle between the pre-current and current-y vectors.
let vec_1 = pre_vertex - *current_vertex;
let vec_2 = *y - *current_vertex;
let mut angle = vec_1.z().atan2(*vec_1.x()) - vec_2.z().atan2(*vec_2.x());
if angle < 0.0 {
angle += 2.0 * PI;
}
if angle > 0.0 {
Some((angle, y))
} else {
None
}
})
.collect::<Vec<_>>()
)
.flatten()
.max_by(|x, y| x.0.total_cmp(&y.0)) {
Some((_, next_vertex)) => points.push(next_vertex.clone()),
None => break,
}
}
// TODO: calculate this based on the geometry of the model.
let border_corners_and_mid_points = vec![
Point3d::new(0.0, 0.0, -14.0),
Point3d::new(20.0, 0.0, -14.0),
Point3d::new(20.0, 0.0, 6.0),
Point3d::new(0.0, 0.0, 6.0),
//Point3d::new(0.0, 10.0, 0.0),
//Point3d::new(20.0, 10.0, 0.0),
//Point3d::new(10.0, 0.0, 0.0),
//Point3d::new(10.0, 20.0, 0.0),
];
// If everything worked as expected, we will have all the points that form the perimeter of the platforms, in sequence.
// Now we need to generate new platforms using the closest two corners of the bounding box and their edge's center as third vertex.
let point_default = Point3d::default();
let mut prev_closest_point = Point3d::default();
for i in 0..points.len() {
let j = if i as i32 == points.len() as i32 - 1 { 0 } else { i + 1 };
let current = &points[i];
let next = &points[j];
// To get to which corner or mid edge we need to join, we need to get the closest to the mid point of the edge between the vertices.
// Fucking maths.
let mid = ((current.x() + next.x()) / 2.0, (current.z() + next.z()) / 2.0);
let (closest_point, _) = border_corners_and_mid_points.par_iter()
.map(|x| (x, (x.x() - mid.0, x.z() - mid.1)))
.map(|x| (x.0, (x.1.0.powi(2) + x.1.1.powi(2)).sqrt()))
.min_by(|x, y| x.1.total_cmp(&y.1))
.unwrap();
// Fix for the first change in closest point.
if prev_closest_point == point_default {
prev_closest_point = closest_point.clone();
}
// If we're changing the closest point, we need to also generate the platform between the edges and the first vertex of our pair.
if prev_closest_point != *closest_point {
let mut platform = Platform::default();
platform.flag_2 = true;
platform.vertices.set_outline(vec![
closest_point.clone(),
prev_closest_point.clone(),
current.clone()
]);
platform.normal = {
let a = platform.vertices.outline()[1] - platform.vertices.outline()[0];
let b = platform.vertices.outline()[2] - platform.vertices.outline()[0];
Point3d::new(
a.y() * b.z() - a.z() * b.y(),
a.z() * b.x() - a.x() * b.z(),
a.x() * b.y() - a.y() * b.x()
)
};
destruct.platforms.push(platform);
prev_closest_point = closest_point.clone();
}
// Generate the platform.
let mut platform = Platform::default();
platform.flag_2 = true;
platform.vertices.set_outline(vec![
prev_closest_point.clone(),
current.clone(),
next.clone(),
]);
platform.normal = {
let a = platform.vertices.outline()[1] - platform.vertices.outline()[0];
let b = platform.vertices.outline()[2] - platform.vertices.outline()[0];
Point3d::new(
a.y() * b.z() - a.z() * b.y(),
a.z() * b.x() - a.x() * b.z(),
a.x() * b.y() - a.y() * b.x()
)
};
destruct.platforms.push(platform);
}
}*/
}
}
}
}
data.encode(&mut after, &None).unwrap();
if before != after {
let dest_path_end = path.strip_prefix(base_path).unwrap();
let dest_path = dest_path.join(dest_path_end);
let mut dest_path_parent = dest_path.to_path_buf();
dest_path_parent.pop();
DirBuilder::new().recursive(true).create(&dest_path_parent).unwrap();
let mut writer = BufWriter::new(File::create(&dest_path).unwrap());
writer.write_all(&after).unwrap();
println!("File edited: {}", dest_path.to_string_lossy().to_string());
}
}
Err(error) => {
let dest_path_end = path.strip_prefix(base_path).unwrap();
let dest_path = dest_path.join(dest_path_end);
println!("Failed to read file: {}, with error: {}", dest_path.to_string_lossy().to_string(), error);
}
}
}
if path.file_name().unwrap().to_string_lossy().to_string().ends_with(".cs2.collision") {
let mut reader = BufReader::new(File::open(&path).unwrap());
let decodeable_extra_data = DecodeableExtraData::default();
let data_len = reader.len().unwrap();
let before = reader.read_slice(data_len as usize, true).unwrap();
match Cs2Collision::decode(&mut reader, &Some(decodeable_extra_data)) {
Ok(mut data) => {
let mut after = vec![];
if patch_walls {
// Fixes for the wall pillars. These should be applied to any wall piece of the following type:
// - straigh_20m
if file_name.contains("_wall_straight_20m_") {
if let Some(collision_3d) = data.collisions_3d_mut().last_mut() {
// Remove all collision data from the last collision set.
collision_3d.triangles_mut().clear();
collision_3d.vertices_mut().clear();
}
}
}
data.encode(&mut after, &None).unwrap();
if before != after {
let dest_path_end = path.strip_prefix(base_path).unwrap();
let dest_path = dest_path.join(dest_path_end);
let mut dest_path_parent = dest_path.to_path_buf();
dest_path_parent.pop();
DirBuilder::new().recursive(true).create(&dest_path_parent).unwrap();
let mut writer = BufWriter::new(File::create(&dest_path).unwrap());
writer.write_all(&after).unwrap();
println!("File edited: {}", dest_path.to_string_lossy().to_string());
}
}
Err(error) => {
let dest_path_end = path.strip_prefix(base_path).unwrap();
let dest_path = dest_path.join(dest_path_end);
println!("Failed to read file: {}, with error: {}", dest_path.to_string_lossy().to_string(), error);
}
}
}
}
}
*/