polytrack_codes/v3/
mod.rs1#![allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
2#[cfg(test)]
3mod tests;
4
5use crate::tools::{self, Track, hash_vec, read::*};
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_step1 = *metadata.first()? as usize;
36 let name_len = (name_len_step1 * 4).div_ceil(3);
37 let track_name_raw = tools::decode(track_code.get(2..2 + name_len)?)?;
38 let name = String::from_utf8(track_name_raw).ok()?;
39 let track_data = tools::decompress(&tools::decode(track_code.get(2 + name_len..)?)?)?;
40 Some(Track {
41 name,
42 author: None,
43 track_data,
44 })
45}
46
47#[must_use]
48pub fn decode_track_data(data: &[u8]) -> Option<TrackInfo> {
49 let mut offset = 0;
50 let mut parts = Vec::new();
51 while offset < data.len() {
52 let id = read_u16(data, &mut offset)? as u8;
53 let amount = read_u32(data, &mut offset)?;
54
55 let mut blocks = Vec::new();
56 for _ in 0..amount {
57 let x = read_i24(data, &mut offset)? - i32::pow(2, 23);
58 let y = read_i24(data, &mut offset)?;
59 let z = read_i24(data, &mut offset)? - i32::pow(2, 23);
60
61 let rotation = read_u8(data, &mut offset)? & 3;
62
63 let cp_order = if CP_IDS.contains(&id) {
64 Some(read_u16(data, &mut offset)?)
65 } else {
66 None
67 };
68 blocks.push(Block {
69 x,
70 y,
71 z,
72 rotation,
73 cp_order,
74 });
75 }
76 parts.push(Part { id, amount, blocks });
77 }
78
79 Some(TrackInfo { parts })
80}
81
82#[must_use]
83pub fn export_to_id(track_code: &str) -> Option<String> {
84 let track_data = decode_track_code(track_code)?;
85 let data = track_data.track_data;
86 let id = hash_vec(data);
87 Some(id)
88}