brres 0.1.7

brres is a Rust crate designed for reading and writing .brres 3d model archives used in the Nintendo Wii games. The library provides C bindings, making it useful in both Rust and C/C++ based projects.
Documentation
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
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
//! JSON middle-layer for communication between the inner library (C++) and this Rust layer.
//!
//! Soon this JSON layer should be made private, and eventually the inner implementation be rewritten in Rust.
//!

use serde::Deserialize;
use serde::Serialize;

use std::fs::File;
use std::io::BufReader;

use crate::enums::*;

// This file has JSON schema

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONMatrixWeight {
    pub bone_id: u32,
    pub weight: f32,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONDrawMatrix {
    pub weights: Vec<JSONMatrixWeight>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONModelInfo {
    pub scaling_rule: String,
    pub texmtx_mode: String,
    pub source_location: String,
    pub evpmtx_mode: String,
    pub min: [f32; 3],
    pub max: [f32; 3],
}

impl Default for JSONModelInfo {
    fn default() -> Self {
        JSONModelInfo {
            scaling_rule: "Maya".to_string(),
            texmtx_mode: "Maya".to_string(),
            source_location: "".to_string(),
            evpmtx_mode: "Normal".to_string(),
            min: [0.0, 0.0, 0.0],
            max: [0.0, 0.0, 0.0],
        }
    }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonMesh {
    pub clr_buffer: Vec<String>,
    pub current_matrix: i32,
    pub mprims: Vec<JsonPrimitive>,
    pub name: String,
    pub nrm_buffer: String,
    pub pos_buffer: String,
    pub uv_buffer: Vec<String>,
    pub vcd: i32,
    pub visible: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonPrimitive {
    pub matrices: Vec<i32>,
    pub num_prims: i32,
    pub vertexDataBufferId: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonModel {
    pub name: String,
    pub info: JSONModelInfo,
    pub bones: Vec<JSONBoneData>,
    pub materials: Vec<JSONMaterial>,
    pub matrices: Vec<JSONDrawMatrix>,
    pub meshes: Vec<JsonMesh>,
    pub positions: Vec<JsonBufferData<[f32; 3]>>,
    pub normals: Vec<JsonBufferData<[f32; 3]>>,
    pub colors: Vec<JsonBufferData<[u8; 4]>>,
    pub texcoords: Vec<JsonBufferData<[f32; 2]>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonBufferData<T> {
    pub dataBufferId: i32,
    pub id: i32,
    pub name: String,
    pub q_comp: i32,
    pub q_divisor: i32,
    pub q_stride: i32,
    pub q_type: i32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cached_min: Option<T>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cached_max: Option<T>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonTexture {
    pub dataBufferId: i32,
    pub format: u32,
    pub height: u32,
    pub maxLod: f32,
    pub minLod: f32,
    pub name: String,
    pub number_of_images: u32,
    pub sourcePath: String,
    pub width: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JsonArchive {
    pub chrs: Vec<JSONChrData>,
    pub clrs: Vec<JSONClrAnim>,
    pub models: Vec<JsonModel>,
    pub pats: Vec<JSONPatAnim>,
    pub srts: Vec<JSONSrtData>,
    pub textures: Vec<JsonTexture>,
    pub viss: Vec<JSONVisData>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONDrawCall {
    pub material: u32,
    pub poly: u32,
    pub prio: i32,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONBoneData {
    pub name: String,
    pub ssc: bool,
    pub visible: bool,
    pub billboard_type: u32,
    pub scale: [f32; 3],
    pub rotate: [f32; 3],
    pub translate: [f32; 3],
    pub volume_min: [f32; 3],
    pub volume_max: [f32; 3],
    pub parent: i32,
    pub children: Vec<i32>,
    pub display_matrix: bool,
    pub draw_calls: Vec<JSONDrawCall>,
    pub force_display_matrix: bool,
    pub omit_from_node_mix: bool,
}

impl Default for JSONBoneData {
    fn default() -> Self {
        JSONBoneData {
            name: "Untitled Bone".to_string(),
            ssc: false,
            visible: true,
            billboard_type: 0,
            scale: [1.0, 1.0, 1.0],
            rotate: [0.0, 0.0, 0.0],
            translate: [0.0, 0.0, 0.0],
            volume_min: [0.0, 0.0, 0.0],
            volume_max: [0.0, 0.0, 0.0],
            parent: -1,
            children: Vec::new(),
            display_matrix: true,
            draw_calls: Vec::new(),
            force_display_matrix: false,
            omit_from_node_mix: false,
        }
    }
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
pub enum ChrQuantization {
    Track32,
    Track48,
    Track96,
    BakedTrack8,
    BakedTrack16,
    BakedTrack32,
    Const,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct JSONChrTrack {
    pub quant: ChrQuantization,
    pub scale: f32,
    pub offset: f32,
    pub framesDataBufferId: u32,
    pub numKeyFrames: u32,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct JSONChrNode {
    pub name: String,
    pub flags: u32,
    pub tracks: Vec<u32>,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct JSONChrData {
    pub nodes: Vec<JSONChrNode>,
    pub tracks: Vec<JSONChrTrack>,
    pub name: String,
    pub sourcePath: String,
    pub frameDuration: u16,
    pub wrapMode: u32,
    pub scaleRule: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct Color {
    pub r: u8,
    pub g: u8,
    pub b: u8,
    pub a: u8,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct ColorS10 {
    pub r: i16,
    pub g: i16,
    pub b: i16,
    pub a: i16,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONColorStage {
    pub constantSelection: TevKColorSel,
    pub a: TevColorArg,
    pub b: TevColorArg,
    pub c: TevColorArg,
    pub d: TevColorArg,
    pub formula: TevColorOp,
    pub bias: TevBias,
    pub scale: TevScale,
    pub clamp: bool,
    pub out: TevReg,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONAlphaStage {
    pub a: TevAlphaArg,
    pub b: TevAlphaArg,
    pub c: TevAlphaArg,
    pub d: TevAlphaArg,
    pub formula: TevAlphaOp,
    pub constantSelection: TevKAlphaSel,
    pub bias: TevBias,
    pub scale: TevScale,
    pub clamp: bool,
    pub out: TevReg,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONIndirectStageTev {
    pub indStageSel: u8,
    pub format: IndTexFormat,
    pub bias: IndTexBiasSel,
    pub matrix: IndTexMtxID,
    pub wrapU: IndTexWrap,
    pub wrapV: IndTexWrap,
    pub addPrev: bool,
    pub utcLod: bool,
    pub alpha: IndTexAlphaSel,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONTevStage {
    pub rasOrder: ColorSelChanApi,
    pub texMap: u8,
    pub texCoord: u8,
    pub rasSwap: u8,
    pub texMapSwap: u8,
    pub colorStage: JSONColorStage,
    pub alphaStage: JSONAlphaStage,
    pub indirectStage: JSONIndirectStageTev,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONTexMatrix {
    pub projection: TexGenType,
    pub scale: (f32, f32),
    pub rotate: f32,
    pub translate: (f32, f32),
    pub effectMatrix: [f32; 16],
    pub transformModel: CommonTransformModel,
    pub method: CommonMappingMethod,
    pub option: CommonMappingOption,
    pub camIdx: i8,
    pub lightIdx: i8,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONSamplerData {
    pub mTexture: String,
    pub mPalette: String,
    pub mWrapU: TextureWrapMode,
    pub mWrapV: TextureWrapMode,
    pub bEdgeLod: bool,
    pub bBiasClamp: bool,
    pub mMaxAniso: AnisotropyLevel,
    pub mMinFilter: TextureFilter,
    pub mMagFilter: TextureFilter,
    pub mLodBias: f32,
    pub btiId: u16,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONChannelControl {
    pub enabled: bool,
    pub Ambient: ColorSource,
    pub Material: ColorSource,
    pub lightMask: LightID,
    pub diffuseFn: DiffuseFunction,
    pub attenuationFn: AttenuationFunction,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONChannelData {
    pub matColor: Color,
    pub ambColor: Color,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONTexCoordGen {
    pub func: TexGenType,
    pub sourceParam: TexGenSrc,
    pub matrix: TexMatrix,
    pub normalize: bool,
    pub postMatrix: PostTexMatrix,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONMaterial {
    pub flag: u32,
    pub id: u32,
    pub lightSetIndex: i8,
    pub fogIndex: i8,
    pub name: String,
    pub texMatrices: Vec<JSONTexMatrix>, // MAX 10
    pub samplers: Vec<JSONSamplerData>,  // MAX 8
    pub cullMode: CullMode,
    pub chanData: Vec<JSONChannelData>,             // Max 2
    pub colorChanControls: Vec<JSONChannelControl>, // Max 4
    pub texGens: Vec<JSONTexCoordGen>,              // Max 8
    pub tevKonstColors: [Color; 4],
    pub tevColors: [ColorS10; 4],
    pub earlyZComparison: bool,
    pub zMode: JSONZMode,
    pub alphaCompare: JSONAlphaComparison,
    pub blendMode: JSONBlendMode,
    pub dstAlpha: JSONDstAlpha,
    pub xlu: bool,
    pub indirectStages: Vec<JSONIndirectStage>, // Max 4
    pub mIndMatrices: Vec<JSONIndirectMatrix>,  // Max 3
    pub mSwapTable: JSONSwapTable,
    pub mStages: Vec<JSONTevStage>, // Max 16
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONSwapTableEntry {
    pub r: ColorComponent,
    pub g: ColorComponent,
    pub b: ColorComponent,
    pub a: ColorComponent,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONSwapTable {
    pub entries: [JSONSwapTableEntry; 4],
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONIndOrder {
    pub refMap: u8,
    pub refCoord: u8,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONIndirectTextureScalePair {
    pub U: IndirectTextureScalePairSelection,
    pub V: IndirectTextureScalePairSelection,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONIndirectStage {
    pub scale: JSONIndirectTextureScalePair,
    pub order: JSONIndOrder,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONIndirectMatrix {
    pub scale: (f32, f32),
    pub rotate: f32,
    pub trans: (f32, f32),
    pub quant: i32,
    pub method: IndirectMatrixMethod,
    pub refLight: i8,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONZMode {
    pub compare: bool,
    pub function: Comparison,
    pub update: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONAlphaComparison {
    pub compLeft: Comparison,
    pub refLeft: u8,
    pub op: AlphaOp,
    pub compRight: Comparison,
    pub refRight: u8,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONBlendMode {
    #[serde(rename = "type")]
    pub type_: BlendModeType,
    pub source: BlendModeFactor,
    pub dest: BlendModeFactor,
    pub logic: LogicOp,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct JSONDstAlpha {
    pub enabled: bool,
    pub alpha: u8,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtKeyFrame {
    pub frame: f32,
    pub value: f32,
    pub tangent: f32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtTrack {
    pub keyframes: Vec<JSONSrtKeyFrame>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtMatrix {
    pub scaleX: JSONSrtTrack,
    pub scaleY: JSONSrtTrack,
    pub rot: JSONSrtTrack,
    pub transX: JSONSrtTrack,
    pub transY: JSONSrtTrack,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtTarget {
    pub materialName: String,
    pub indirect: bool,
    pub matrixIndex: i32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtTargetedMtx {
    pub target: JSONSrtTarget,
    pub matrix: JSONSrtMatrix,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONSrtData {
    pub matrices: Vec<JSONSrtTargetedMtx>,
    pub name: String,
    pub sourcePath: String,
    pub frameDuration: u16,
    pub xformModel: u32,
    pub wrapMode: AnimationWrapMode,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONPatKeyFrame {
    pub frame: f32,
    pub texture: u16,
    pub palette: u16,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONPatTrack {
    pub keyframes: Vec<JSONPatKeyFrame>,
    pub reserved: u16,
    pub progressPerFrame: f32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONPatSampler {
    pub name: String,
    pub flags: u32,
    pub tracks: Vec<u32>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONPatAnim {
    pub samplers: Vec<JSONPatSampler>,
    pub tracks: Vec<JSONPatTrack>,
    pub textureNames: Vec<String>,
    pub paletteNames: Vec<String>,
    pub runtimeTextures: Vec<u32>,
    pub runtimePalettes: Vec<u32>,
    pub name: String,
    pub sourcePath: String,
    pub frameDuration: u16,
    pub wrapMode: AnimationWrapMode,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONClrKeyFrame {
    pub data: u32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONClrTrack {
    pub keyframes: Vec<JSONClrKeyFrame>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONClrTarget {
    pub notAnimatedMask: u32,
    pub data: u32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONClrMaterial {
    pub name: String,
    pub flags: u32,
    pub targets: Vec<JSONClrTarget>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONClrAnim {
    pub materials: Vec<JSONClrMaterial>,
    pub tracks: Vec<JSONClrTrack>,
    pub name: String,
    pub sourcePath: String,
    pub frameDuration: u16,
    pub wrapMode: AnimationWrapMode,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONVisKeyFrame {
    pub data: u32,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONVisTrack {
    pub keyframes: Vec<JSONVisKeyFrame>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONVisBone {
    pub name: String,
    pub flags: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target: Option<JSONVisTrack>,
}

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub struct JSONVisData {
    pub bones: Vec<JSONVisBone>,
    pub name: String,
    pub sourcePath: String,
    pub frameDuration: u16,
    pub wrapMode: AnimationWrapMode,
}

pub fn read_json(file_path: &str) -> anyhow::Result<JsonArchive> {
    let file = File::open(file_path)?;
    let reader = BufReader::new(file);
    let root: JsonArchive = serde_json::from_reader(reader)?;
    Ok(root)
}

#[cfg(test)]
mod tests3 {
    use super::*;
    #[test]
    fn dummy_json() {
        let file_path = "dummy.json";
        match read_json(file_path) {
            Ok(root) => println!("{:#?}", root),
            Err(e) => panic!("Error reading JSON file: {}", e),
        }
    }
}