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
use std::collections::HashMap;

use crate::*;
use lazy_static::lazy_static;

pub const DATA_STR: &str = include_str!(concat!(
	env!("CARGO_MANIFEST_DIR"),
	"/declarations/Versions.csv"
));

lazy_static! {
	pub static ref DATA: Versions = {
		let mut table = csv::Reader::from_reader(DATA_STR.as_bytes());
		let mut vs = Versions(
			table.deserialize().collect::<Result<Vec<_>, _>>().unwrap(),
		);

		// Add count if necessary
		let mut counts: HashMap<_, u32> = HashMap::new();
		for v in &vs.0 {
			let key = VersionKey::new(v);
			*counts.entry(key).or_default() += 1;
		}
		counts.retain(|_, c| *c > 1);

		for v in vs.0.iter_mut().rev() {
			let key = VersionKey::new(v);
			if let Some(count) = counts.get_mut(&key) {
				v.count = *count;
				*count -= 1;
			}
		}

		vs
	};
}

#[derive(Debug, Deserialize, Clone)]
#[serde(deny_unknown_fields)]
pub struct Version {
	pub version: String,
	pub platform: String,
	pub hash: String,
	// TODO Deprecated
	channel: String,
	#[serde(default)]
	count: u32,
}

impl Version {
	pub fn get_enum_name(&self) -> String {
		let mut res = String::new();
		res.push_str(&self.platform.replace(' ', "_"));
		let ver = self.version.split(' ').next().unwrap();
		for num in ver.split('.') {
			if num != "?" {
				res.push('_');
				res.push_str(&format!("{}", num));
			} else {
				res.push('_');
				res.push('X');
			}
		}
		if self.count != 0 {
			res.push_str("__");
			res.push_str(&self.count.to_string());
		}
		res
	}

	pub fn get_sign_array(&self) -> String {
		let mut res = String::new();
		for b in ::base64::decode(&self.hash).unwrap() {
			if !res.is_empty() {
				res.push_str(", ");
			}
			res.push_str(&format!("{:#x}", b));
		}
		res
	}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct VersionKey {
	pub version: String,
	pub platform: String,
}

impl VersionKey {
	fn new(v: &Version) -> Self {
		Self {
			version: v.version.split(' ').next().unwrap().to_string(),
			platform: v.platform.clone(),
		}
	}
}

#[derive(Default, Debug)]
pub struct Versions(pub Vec<Version>);