1use crate::mc::utils::command_builder::CommandVersionConfig;
2use crate::utils::io_utils::system::OperatingSystem;
3use crate::utils::manifest::manifest;
4use serde::Deserialize;
5use std::collections::HashMap;
6use std::fs::File;
7use std::io::Read;
8
9#[derive(Deserialize, Debug, Clone)]
10pub struct JsonVersion {
11 #[serde(skip, alias = "minecraftArguments")]
12 pub minecraft_arguments: (),
13 #[serde(skip)]
14 pub arguments: (),
15 #[serde(alias = "inheritsFrom")]
16 pub inherits_from: Option<String>,
17 #[serde(default, alias = "assetIndex")]
18 pub asset_index: AssetsIndex,
19 #[serde(default)]
20 pub assets: String,
21 #[serde(default, alias = "compilanceLevel")]
22 pub compilance_level: u32,
23 #[serde(default)]
24 pub downloads: ClientDownloads,
25 pub id: String,
26 #[serde(default, alias = "javaVersion")]
27 pub java_version: JavaVersion,
28 pub libraries: Vec<Library>,
29 #[serde(alias = "mainClass")]
30 pub main_class: String,
31 #[serde(alias = "minimumLauncherVersion")]
32 pub minimum_launcher_version: Option<u32>,
33 #[serde(alias = "releaseTime")]
34 pub release_time: String,
35 pub time: String,
36 #[serde(alias = "type")]
37 pub version_type: String,
38 #[serde(default)]
39 pub logging: Option<LogSettings>,
40}
41
42#[derive(Deserialize, Debug, Clone, Default)]
43pub struct AssetsIndex {
44 pub id: String,
45 pub sha1: String,
46 pub size: u64,
47 #[serde(alias = "totalSize")]
48 pub total_size: u64,
49 pub url: String,
50}
51#[derive(Deserialize, Debug, Clone, Default)]
52pub struct ClientDownloads {
53 pub client: Client,
54 #[serde(default)]
55 pub client_mappings: Client,
56 #[serde(skip)]
57 pub server: Client,
58 #[serde(default)]
59 pub server_mappings: Client,
60}
61#[derive(Deserialize, Debug, Clone, Default)]
62pub struct Client {
63 pub sha1: String,
64 pub size: u64,
65 pub url: String,
66}
67#[derive(Deserialize, Debug, Clone, Default)]
68pub struct JavaVersion {
69 pub component: String,
70 #[serde(alias = "majorVersion")]
71 pub major_version: u32,
72}
73pub fn default_vec_library_rules() -> Vec<LibraryRule> {
74 vec![]
75}
76#[derive(Debug, Deserialize, Clone)]
77pub struct LibraryDownloads {
78 pub(crate) artifact: Option<LibraryDownloadsArtifacts>,
79 pub(crate) classifiers: Option<HashMap<String, LibraryDownloadsArtifacts>>,
80}
81
82#[derive(Debug, Deserialize, Clone)]
83pub struct LibraryDownloadsArtifacts {
84 pub(crate) path: String,
85 pub(crate) sha1: String,
86 pub size: u64,
87 pub(crate) url: String,
88}
89
90#[derive(Debug, Deserialize, Clone, Default)]
91pub struct LibraryRuleOs {
92 pub(crate) name: String,
93 #[serde(skip)]
94 version: String,
95}
96
97#[derive(Debug, Deserialize, Clone)]
98pub struct LibraryRule {
99 pub(crate) action: String,
100 pub(crate) os: Option<LibraryRuleOs>,
101}
102
103impl LibraryRule {
104 pub fn allow(&self, os: &OperatingSystem) -> bool {
105 if self.action.eq("allow") && self.os.is_none() {
106 true
107 } else if self.action.eq("allow") && self.os.clone().unwrap().name.eq(os.name()) {
108 true
109 } else if !self.action.eq("allow") && !self.os.clone().unwrap().name.eq(os.name()) {
110 true
111 } else {
112 false
113 }
114 }
115}
116
117#[derive(Debug, Deserialize, Clone)]
118pub struct Library {
119 pub(crate) downloads: Option<LibraryDownloads>,
120 pub(crate) name: String,
121 pub(crate) rules: Option<Vec<LibraryRule>>,
122 #[serde(default)]
123 pub(crate) url: String,
124 pub natives: Option<LibraryNatives>,
125 #[serde(default)]
126 md5: String,
127 #[serde(default)]
128 sha1: String,
129 #[serde(default)]
130 sha256: String,
131 #[serde(default)]
132 sha521: String,
133 #[serde(default)]
134 size: usize,
135 pub(crate) extract: Option<LibraryExtract>,
136}
137#[derive(Deserialize, Debug, Clone)]
138pub struct LibraryExtract {
139 exclude: Vec<String>,
140}
141#[derive(Deserialize, Debug, Default, Clone)]
142pub struct LibraryNatives {
143 pub osx: Option<String>,
144 pub linux: Option<String>,
145 pub windows: Option<String>,
146}
147#[derive(Deserialize, Default, Debug, Clone)]
148#[serde(default)]
149pub struct LogSettings {
150 pub client: LogSettingsClient,
151}
152#[derive(Deserialize, Debug, Default, Clone)]
153pub struct LogSettingsClient {
154 pub argument: String,
155 pub file: LogSettingsClientFile,
156 #[serde(alias = "type")]
157 pub client_type: String,
158}
159#[derive(Deserialize, Debug, Default, Clone)]
160pub struct LogSettingsClientFile {
161 pub id: String,
162 pub sha1: String,
163 pub size: u64,
164 pub url: String,
165}
166
167pub fn load(file_str: &str) -> JsonVersion {
168 let mut file = File::open(file_str).unwrap();
169 let mut content = String::default();
170 file.read_to_string(&mut content).expect("error");
171 let obj = serde_json::from_str(content.as_str());
172
173 if obj.is_ok() {
174 let mut o: JsonVersion = obj.unwrap();
175 if let Some(ins) = o.inherits_from {
176 let mut vers = manifest()
177 .get(ins.as_str())
178 .unwrap()
179 .save_and_load(format!("{}.tmp", file_str).as_str());
180
181 vers.libraries.append(&mut o.libraries);
182
183 vers.id = o.id;
184 vers.main_class = o.main_class;
185
186 return vers;
187 }
188 return o;
189 }
190 obj.expect("s")
191}
192
193impl JsonVersion {
194 pub fn command_conf(&self) -> CommandVersionConfig {
195 CommandVersionConfig {
196 version_id: self.id.to_string(),
197 version_type: self.version_type.to_string(),
198 main_class: self.main_class.to_string(),
199 }
200 }
201}