polytrack_codes/v4/
mod.rs

1#![allow(clippy::cast_possible_truncation)]
2#[cfg(test)]
3mod tests;
4
5use crate::tools::{self, hash_vec, prelude::*};
6
7pub const CP_IDS: [u8; 4] = [52, 65, 75, 77];
8
9#[derive(Debug, PartialEq, Eq)]
10pub struct TrackInfo {
11    pub parts: Vec<Part>,
12}
13
14#[derive(Debug, PartialEq, Eq)]
15pub struct Part {
16    pub id: u8,
17    pub amount: u32,
18    pub blocks: Vec<Block>,
19}
20
21#[derive(Debug, PartialEq, Eq)]
22pub struct Block {
23    pub x: i32,
24    pub y: i32,
25    pub z: i32,
26
27    pub rotation: u8,
28    pub cp_order: Option<u16>,
29}
30
31#[must_use]
32pub fn decode_track_code(track_code: &str) -> Option<Track> {
33    let track_code = track_code.get(2..)?;
34    let metadata = tools::decode(track_code.get(..2)?)?;
35    let name_len = *metadata.first()? as usize;
36    let track_name_raw = tools::decode(track_code.get(2..2 + name_len)?)?;
37    let name = String::from_utf8(track_name_raw).ok()?;
38    let track_data = tools::decompress(&tools::decode(track_code.get(2 + name_len..)?)?)?;
39    Some(Track {
40        name,
41        author: None,
42        track_data,
43    })
44}
45
46#[must_use]
47/// Encodes the given track struct into a track code.
48/// Returns [`None`] if something failed in the process.
49///
50/// Output might differ slightly from Polytrack's output
51/// because of Zlib shenanigans, but is still compatible.
52pub fn encode_track_code(track: &Track) -> Option<String> {
53    let track_data = tools::encode(&tools::compress(&track.track_data)?)?;
54
55    let name_raw = track.name.as_bytes().to_vec();
56    let name = tools::encode(&name_raw)?;
57    let metadata = tools::encode(&[name.len() as u8])?;
58
59    // prepend the "v3"
60    let track_code = String::from("v3") + &metadata + &name + &track_data;
61    Some(track_code)
62}
63
64#[must_use]
65pub fn decode_track_data(data: &[u8]) -> Option<TrackInfo> {
66    let mut offset = 0;
67    let mut parts = Vec::new();
68    while offset < data.len() {
69        let id = read_u16(data, &mut offset)? as u8;
70        let amount = read_u32(data, &mut offset)?;
71
72        let mut blocks = Vec::new();
73        for _ in 0..amount {
74            let x = read_i24(data, &mut offset)? - i32::pow(2, 23);
75            let y = read_i24(data, &mut offset)?;
76            let z = read_i24(data, &mut offset)? - i32::pow(2, 23);
77
78            let rotation = read_u8(data, &mut offset)? & 3;
79
80            let cp_order = if CP_IDS.contains(&id) {
81                Some(read_u16(data, &mut offset)?)
82            } else {
83                None
84            };
85            blocks.push(Block {
86                x,
87                y,
88                z,
89                rotation,
90                cp_order,
91            });
92        }
93        parts.push(Part { id, amount, blocks });
94    }
95
96    Some(TrackInfo { parts })
97}
98
99#[must_use]
100/// Encodes the `TrackInfo` struct into raw binary data.
101pub fn encode_track_data(track_info: &TrackInfo) -> Option<Vec<u8>> {
102    let mut data = Vec::new();
103    for part in &track_info.parts {
104        write_u16(&mut data, part.id.into());
105        write_u32(&mut data, part.amount);
106        for block in &part.blocks {
107            write_u24(&mut data, (block.x + i32::pow(2, 23)).cast_unsigned());
108            write_u24(&mut data, block.y.cast_unsigned());
109            write_u24(&mut data, (block.z + i32::pow(2, 23)).cast_unsigned());
110            data.push(block.rotation);
111            if let Some(cp_order) = block.cp_order {
112                write_u16(&mut data, cp_order.into());
113            }
114        }
115    }
116
117    Some(data)
118}
119
120#[must_use]
121pub fn export_to_id(track_code: &str) -> Option<String> {
122    let track_data = decode_track_code(track_code)?;
123    let id = hash_vec(track_data.track_data);
124    Some(id)
125}