tsproto_structs/
versions.rs

1use std::collections::HashMap;
2use std::result::Result;
3
4use crate::*;
5
6use once_cell::sync::Lazy;
7
8pub const DATA_STR: &str =
9	include_str!(concat!(env!("CARGO_MANIFEST_DIR"), "/declarations/Versions.csv"));
10
11pub static DATA: Lazy<Versions> = Lazy::new(|| {
12	let mut table = csv::Reader::from_reader(DATA_STR.as_bytes());
13	let mut vs = Versions(table.deserialize().collect::<Result<Vec<_>, _>>().unwrap());
14
15	// Add count if necessary
16	let mut counts: HashMap<_, u32> = HashMap::new();
17	for v in &vs.0 {
18		let key = VersionKey::new(v);
19		*counts.entry(key).or_default() += 1;
20	}
21	counts.retain(|_, c| *c > 1);
22
23	for v in vs.0.iter_mut().rev() {
24		let key = VersionKey::new(v);
25		if let Some(count) = counts.get_mut(&key) {
26			v.count = *count;
27			*count -= 1;
28		}
29	}
30
31	vs
32});
33
34#[derive(Debug, Deserialize, Clone)]
35#[serde(deny_unknown_fields)]
36pub struct Version {
37	pub version: String,
38	pub platform: String,
39	pub hash: String,
40	#[serde(default)]
41	count: u32,
42}
43
44impl Version {
45	pub fn get_enum_name(&self) -> String {
46		let mut res = String::new();
47		res.push_str(&self.platform.replace(' ', "_"));
48		let ver = self.version.split(' ').next().unwrap().replace('-', "_");
49		for num in ver.split('.') {
50			res.push('_');
51			if num != "?" {
52				res.push_str(&num.to_string());
53			} else {
54				res.push('X');
55			}
56		}
57		if self.count != 0 {
58			res.push_str("__");
59			res.push_str(&self.count.to_string());
60		}
61		res
62	}
63
64	pub fn get_sign_array(&self) -> String {
65		let mut res = String::new();
66		for b in ::base64::decode(&self.hash).unwrap() {
67			if !res.is_empty() {
68				res.push_str(", ");
69			}
70			res.push_str(&format!("{:#x}", b));
71		}
72		res
73	}
74}
75
76#[derive(Debug, Clone, PartialEq, Eq, Hash)]
77struct VersionKey {
78	pub version: String,
79	pub platform: String,
80}
81
82impl VersionKey {
83	fn new(v: &Version) -> Self {
84		Self {
85			version: v.version.split(' ').next().unwrap().to_string(),
86			platform: v.platform.clone(),
87		}
88	}
89}
90
91#[derive(Default, Debug)]
92pub struct Versions(pub Vec<Version>);