physis 0.6.0

Library for reading and writing FFXIV data.
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
// SPDX-FileCopyrightText: 2026 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later

use std::io::SeekFrom;

use binrw::{BinResult, BinWrite, binrw};

use crate::{
    common_file_operations::{read_bool_from, read_null_terminated_utf8, write_bool_as},
    layer::Layer,
    string_heap::{HeapPointer, HeapString, StringHeap},
    tmb::Tmb,
};

#[binrw::writer(writer, endian)]
pub(crate) fn write_scns(scns: &Vec<ScnSection>, string_heap: &mut StringHeap) -> BinResult<()> {
    for scn in scns {
        scn.write_options(writer, endian, (string_heap,))?;
    }

    Ok(())
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnLayerGroup {
    #[br(temp)]
    #[bw(ignore)]
    heap_pointer: HeapPointer,

    pub layer_group_id: u32,

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub name: HeapString,

    layer_offsets_start: i32,
    layer_offsets_count: i32,

    #[br(count = layer_offsets_count)]
    #[br(seek_before = SeekFrom::Current(layer_offsets_start as i64 - ScnLayerGroup::SIZE as i64))]
    #[br(restore_position)]
    offsets_layers: Vec<i32>,

    #[br(restore_position, parse_with = layers_from_offsets, args(&offsets_layers, string_heap))]
    #[br(seek_before = SeekFrom::Current(layer_offsets_start as i64 - ScnLayerGroup::SIZE as i64))]
    #[bw(ignore)] // TODO: support writing
    pub layers: Vec<Layer>,
}

impl ScnLayerGroup {
    pub(crate) const SIZE: usize = 0x10;
}

/// SCN1 section used in LVBs and SGBs.
#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
#[brw(magic = b"SCN1")]
pub struct ScnSection {
    /// Size of this header. Should be equal to `ScnHeader::SIZE`.
    total_size: u32,
    /// Offset to FileLayerGroupHeader[NumEmbeddedLayerGroups].
    pub(crate) offset_layer_groups: i32,
    /// Number of embedded layer groups.
    pub(crate) num_layer_groups: i32,
    /// Offset to FileSceneGeneral.
    offset_general: i32,
    /// Offset to ScnLayerSetsSection.
    offset_layer_sets: i32,
    /// Offset to FileSceneTimelineList.
    offset_timelines: i32,
    /// offset to a list of path offsets (ints)
    offset_layer_group_resources: i32,
    num_layer_group_resources: i32,
    unk2: i32,
    offset_action_descriptors: i32,
    unk4: i32,
    unk5: i32,
    offset_housing: i32,
    unk7: i32,
    unk8: i32,
    unk9: i32,
    unk10: i32,
    offset_unk2: i32, // Points to 39 bytes of data
    offset_unk3: i32, // Points to 64 bytes of data

    /// List of embedded layer groups.
    #[br(count = num_layer_groups, args { inner: (string_heap,) })]
    #[br(seek_before = SeekFrom::Current(offset_layer_groups as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    #[bw(ignore)] // TODO: support writing
    pub layer_groups: Vec<ScnLayerGroup>,

    /// General information.
    #[br(seek_before = SeekFrom::Current(offset_general as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    #[brw(args(string_heap))]
    pub general: ScnGeneralSection,

    /// Layer set information.
    #[br(seek_before = SeekFrom::Current(offset_layer_sets as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    #[brw(args(string_heap))]
    pub layer_sets: ScnLayerSetsSection,

    /// Embedded animation timelines.
    #[br(seek_before = SeekFrom::Current(offset_timelines as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    #[br(args(string_heap))]
    pub timelines: ScnTimelinesSection,

    #[br(count = num_layer_group_resources)]
    #[br(seek_before = SeekFrom::Current(offset_layer_group_resources as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    offset_path_layer_group_resources: Vec<i32>,

    /// Associated [crate::lgb] paths.
    #[br(parse_with = strings_from_offsets)]
    #[br(args(&offset_path_layer_group_resources))]
    #[br(restore_position)]
    #[br(seek_before = SeekFrom::Current(offset_layer_group_resources as i64 - ScnSection::SIZE as i64))]
    #[bw(ignore)] // TODO: support
    pub lgb_paths: Vec<String>,

    /// Animation action descriptors.
    #[br(seek_before = SeekFrom::Current(offset_action_descriptors as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    pub action_descriptors: ScnSGActionDescriptors,
    // TODO: re-enable once this is readable
    // #[br(seek_before = SeekFrom::Current(offset_unk3 as i64 - ScnSection::SIZE as i64))]
    // #[br(restore_position)]
    // unk3: ScnUnknown3Section,
    /// Housing information.
    #[br(seek_before = SeekFrom::Current(offset_housing as i64 - ScnSection::SIZE as i64))]
    #[br(restore_position)]
    pub housing: ScnHousingSettings,
}

impl ScnSection {
    pub(crate) const SIZE: usize = 0x48;
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnEnvSpace {
    #[br(temp)]
    #[bw(ignore)]
    heap_pointer: HeapPointer,

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub envb_path: HeapString,

    unk1: i32,
    /// ID to an EnvLocation InstanceObject in this scene.
    pub env_location_instance_id: i32,

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub essb_path: HeapString,

    // TODO: I have no idea, but there's 8 extra bytes unaccounted for here. Probably a mistake elsewhere.
    #[br(restore_position)]
    unk: u64,
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnGeneralSection {
    #[br(temp)]
    #[bw(ignore)]
    heap_pointer: HeapPointer,

    unk9: i32,

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub bg_path: HeapString,

    offset_env_spaces: i32,
    num_env_spaces: i32,

    unk1: i32,

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub svb_path: HeapString,

    unk2: f32,
    unk3: f32,
    unk4: f32,
    unk5: f32,
    unk6: f32,
    unk7: f32,
    unk8: i32, // points to 4 bytes in the string heap

    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub lcb_path: HeapString,

    unk10: i32,
    unk11: f32,
    unk12: i32,
    unk13: i32,
    unk14: f32,
    unk15: i32,
    unk16: f32,

    #[br(map = read_bool_from::<i32>)]
    #[bw(map = write_bool_as::<i32>)]
    have_lcbuw: bool,

    #[br(count = num_env_spaces)]
    #[br(seek_before = SeekFrom::Current(offset_env_spaces as i64 - ScnGeneralSection::SIZE as i64))]
    #[br(restore_position)]
    #[br(args { inner: (string_heap,) })]
    #[bw(write_with = write_env_spaces, args(string_heap))]
    pub env_spaces: Vec<ScnEnvSpace>,
}

#[binrw::writer(writer, endian)]
pub fn write_env_spaces(scns: &Vec<ScnEnvSpace>, string_heap: &mut StringHeap) -> BinResult<()> {
    for scn in scns {
        scn.write_options(writer, endian, (string_heap,))?;
    }

    Ok(())
}

impl ScnGeneralSection {
    pub(crate) const SIZE: usize = 0x58;
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[derive(Debug)]
pub struct ScnTimelinesSection {
    offset_entries: i32,
    num_entries: i32,

    #[br(seek_before = SeekFrom::Current(offset_entries as i64 - ScnTimelinesSection::SIZE as i64))]
    #[br(count = num_entries, restore_position, args { inner: (string_heap,) })]
    #[bw(ignore)] // TODO: support writing
    pub timelines: Vec<ScnTimeline>,
}

impl ScnTimelinesSection {
    pub(crate) const SIZE: usize = 0x8;
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnTimeline {
    #[br(temp)]
    #[bw(ignore)]
    heap_pointer: HeapPointer,

    pub sub_id: u32,
    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub animation_type: HeapString,
    offset_instances: i32,
    num_instances: i32,
    offset_action_timeline_key: i32, // TODO: may be be a string? or at least clientstructs claims its one
    offset_tmb: i32,
    tmb_size: i32,

    unk1: [u8; 4], // empty(?)
    #[br(map = read_bool_from::<u8>)]
    #[bw(map = write_bool_as::<u8>)]
    pub auto_play: bool,
    #[br(map = read_bool_from::<u8>)]
    #[bw(map = write_bool_as::<u8>)]
    pub loop_animation: bool,
    unk2: [u8; 10], // unsure

    /// Bytes of a TMLB file.
    #[br(seek_before = SeekFrom::Current(offset_tmb as i64 - ScnTimeline::SIZE as i64), restore_position)]
    #[brw(args(string_heap,))]
    pub tmb: Tmb,

    #[br(seek_before = SeekFrom::Current(offset_instances as i64 - ScnTimeline::SIZE as i64))]
    #[br(count = num_instances, restore_position)]
    pub instances: Vec<ScnTimelineInstance>,
}

impl ScnTimeline {
    pub(crate) const SIZE: usize = 0x2C;
}

#[binrw]
#[derive(Debug, Clone)]
#[repr(C)]
pub struct ScnTimelineInstance {
    /// Points to a [crate::tmb::Tmac] node with this `time`.
    pub tmac_time: i32,
    /// Points to an instance object ID in the embedded layer groups.
    pub instance_id: i32,
}

// TODO: header is definitely not correct
#[binrw]
#[derive(Debug)]
pub struct ScnSGActionDescriptors {
    unk: [u8; 16],
    timeline_offset: i32,
    unk2: [u8; 20],
    count: i32, // TODO: unsure
    unk3: [u8; 4],
    #[br(count = count)]
    pub descriptors: Vec<ScnSGActionControllerDescriptor>,
}

#[binrw]
#[repr(C)]
#[derive(Debug, Clone)]
pub enum ScnSGActionControllerDescriptor {
    #[brw(magic = 1i32)]
    Door(ScnDoorActionDescription),
    #[brw(magic = 2i32)]
    Rotation(ScnRotationActionDescription),
    Unknown(i32),
}

#[binrw]
#[repr(C)]
#[derive(Debug, Clone)]
pub struct ScnDoorActionDescription {
    check_if_1: u8, // client checks if this isn't 0, but im unsure what is after this
    unk: [u8; 11],  // seems to be empty, but not confirmed yet
    /// Instance ID of a BG object to animate.
    pub door_object_0: u8,
    /// Instance ID of a BG object to animate.
    pub door_object_1: u8,
    unk2: [u8; 2],
    pub door_type: i32,
    unk1: f32,
    pub max_rotation: f32,
    pub max_translation: f32,
    /// Instance ID of a sound to play.
    pub sound_0: u8,
    /// Instance ID of a sound to play.
    pub sound_1: u8,
    /// Instance ID of a BG object to animate.
    pub door_object_2: u8,
    /// Instance ID of a BG object to animate.
    pub door_object_3: u8,
    // TODO: unsure if there's other things here
}

#[binrw]
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub enum RotationAxis {
    #[brw(magic = 0i32)]
    X,
    #[brw(magic = 1i32)]
    Y,
    #[brw(magic = 2i32)]
    Z,
}

#[binrw]
#[repr(C)]
#[derive(Debug, Clone)]
pub struct ScnRotationActionDescription {
    check_if_1: u8, // client checks if this isn't 0, but im unsure what is after this
    unk: [u8; 11],  // seems to be empty, but not confirmed yet
    /// Instance ID of the BG object to animate.
    pub bg_part_id: u8,
    unk2: [u8; 3], // seems to be empty, but not confirmed yet
    pub axis: RotationAxis,
    // TODO: figure out the units
    pub duration: f32,
    /// How many degrees to add during the `duration`.
    pub value: f32,
    pub vfx_child1_id: u8,
    #[br(map = read_bool_from::<u8>)]
    #[bw(map = write_bool_as::<u8>)]
    pub vfx_has_child1: bool,
    unk_fields: [u8; 4], // read individually as bytes, but i don't know what they do,
    pub vfx_child_2_id: u8,
    #[br(map = read_bool_from::<u8>)]
    #[bw(map = write_bool_as::<u8>)]
    pub vfx_has_child2: bool,
}

// TODO: definitely not correct
#[binrw]
#[derive(Debug)]
pub struct ScnUnknown3Section {
    unk: [u8; 64],
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnLayerSetsSection {
    offset: i32,
    count: i32,

    #[br(seek_before = SeekFrom::Current(offset as i64 - ScnLayerSetsSection::SIZE as i64))]
    #[br(count = count, restore_position, args { inner: (string_heap,) })]
    #[bw(write_with = write_layersets, args(string_heap))]
    pub layer_sets: Vec<ScnLayerSet>,
}

#[binrw::writer(writer, endian)]
pub fn write_layersets(scns: &Vec<ScnLayerSet>, string_heap: &mut StringHeap) -> BinResult<()> {
    for scn in scns {
        scn.write_options(writer, endian, (string_heap,))?;
    }

    Ok(())
}

impl ScnLayerSetsSection {
    pub(crate) const SIZE: usize = 0x8;
}

#[binrw]
#[br(import(string_heap: &StringHeap))]
#[bw(import(string_heap: &mut StringHeap))]
#[derive(Debug)]
pub struct ScnLayerSet {
    #[br(temp)]
    #[bw(ignore)]
    heap_pointer: HeapPointer,

    /// Path to the `.nvm` file for this layer set.
    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub nvm_path: HeapString,

    /// The ID of this layer set.
    pub id: i32,
    unk2: i32,
    unk3: i32,

    /// Refers to a row in the TerritoryType Excel sheet.
    pub territory_type_id: u16,
    /// Refers to a row in the ContentFinderCondition Excel sheet.
    pub content_finder_condition_id: u16,

    unk5: i32,

    /// Path to the `.nvx` file for this layer set.
    #[br(args(heap_pointer, string_heap))]
    #[bw(args(string_heap))]
    pub nvx_path: HeapString,
}

#[binrw::parser(reader)]
fn strings_from_offsets(offsets: &Vec<i32>) -> BinResult<Vec<String>> {
    let base_offset = reader.stream_position()?;

    let mut strings: Vec<String> = vec![];

    for offset in offsets {
        let string_offset = *offset as u64;

        reader.seek(SeekFrom::Start(base_offset + string_offset))?;
        strings.push(read_null_terminated_utf8(reader));
    }

    Ok(strings)
}

#[binrw::parser(reader, endian)]
fn layers_from_offsets(offsets: &Vec<i32>, string_heap: &StringHeap) -> BinResult<Vec<Layer>> {
    let base_offset = reader.stream_position()?;

    let mut layers: Vec<Layer> = vec![];

    for offset in offsets {
        let layer_offset = *offset as u64;

        reader.seek(SeekFrom::Start(base_offset + layer_offset))?;
        // TODO: need separate data heap eventually
        layers.push(Layer::read(endian, reader, string_heap, string_heap).unwrap());
    }

    Ok(layers)
}

#[binrw]
#[derive(Debug)]
pub struct ScnHousingSettings {
    default_color_id: u16,
    unk1: u8,
    unk2: u8,
    unk3: u32,
    unk4: [u32; 6],
    unk5: u32,
    unk6: u32,
    unk7: u32,
    unk8: u32,
    unk9: u32,
    unk10: u8,
    unk11: [u8; 3],
    unk12: u32,
    unk13: u32,
    unk14: u32,
}