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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
//! GD lists
use std::{
collections::HashMap,
fs::{read, write},
io::Cursor,
path::PathBuf,
};
use anyhow::Result;
use plist::{Dictionary, Value};
use crate::{
cclocallevels::gdlevel::{
GDLevel, PLIST_FOOTER, PLIST_HEADER,
enums::{GDListDifficulty, GDListType},
parse_csv, serialise_bool_fields, serialise_fields, serialise_optional_fields, to_csv,
},
core::{
GDError, b64_decode, b64_encode,
io::{stringify_xml, vec_as_str},
proper_plist_tags,
structs::KCEKValue,
},
};
/// GD list object
///
/// Properties sourced from <https://wyliemaster.github.io/gddocs/#/resources/client/gamesave/list>.
#[derive(Debug, Clone)]
pub struct GDList {
/// ID of the list.
///
/// Internal key: `k1`
pub id: i32,
/// The name of the list.
///
/// Internal key: `k2`
pub name: String,
/// Description of the list decoded from base-64. The description is internally stored as the base-64 encoded version of the original string, however it is decoded in this struct and re-encoded when serialising.
///
/// Internal key: `k3`
pub description: String,
/// Creator of the list
///
/// Internal key: `k5`
pub creator: String,
/// Given difficulty of the list. See [`GDListDifficulty`]
///
/// Internal key: `k7`
pub difficulty: GDListDifficulty,
/// Amount of times this list has been downloaded.
///
/// Internal key: `k11`
pub downloads: i32,
/// Whether the list is uploaded to the servers.
///
/// Internal key: `k15`
pub is_uploaded: bool,
/// Version number of this list.
///
/// Internal key: `k16`
pub version: i32,
/// Type of GDList: [`GDListType`].
///
/// Internal key: `k21`
pub list_type: GDListType,
/// The list's like rating (likes - dislikes).
///
/// Internal key: `k22`
pub likes: i32,
/// Whether this list is rated a demon or not.
///
/// Internal key: `k25`
pub is_demon: bool,
/// Amount of stars rewarded for beating the list.
///
/// Internal key: `k26`
pub stars: i32,
/// Whether the list is featured or not.
///
/// Internal key: `k27`
pub featured: bool,
/// ID of original list (if the list was copied).
///
/// Internal key: `k42`
pub original: Option<i32>,
/// The revision of the list.
///
/// Internal key: `k46`
pub list_revision: i32,
/// The creator's account ID.
///
/// Internal key: `k60`
pub account_id: i32,
/// Whether the list can only be found by searching the ID.
///
/// Internal key: `k79`
pub unlisted: bool,
/// Whether the list is favourited.
///
/// Internal key: `k82`
pub favourited: bool,
/// List ordering.
///
/// Internal key: `k83`
pub order: i32,
/// Comma-separated list of all IDs in the list in order.
///
/// Internal key: `k96`
pub level_ids: Vec<i32>,
/// Dictionary of all the levels in the list. Levels in this dictionary are summaries and are missing many properties, notably, the level's object data.
///
/// Internal key: `k97`
pub levels: Vec<GDLevel>,
/// UNIX timestamp of the level's upload time in seconds.
///
/// Internal key: `k98`
pub upload_time: i32,
/// UNIX timestamp of the level's update time in seconds.
///
/// Internal key: `k99`
pub update_time: Option<i32>,
/// Amount of diamonds awarded upon beating the list.
///
/// Internal key: `k113`
pub diamond_reward: i32,
/// Amount of levels needed for the list to be considered beaten.
///
/// Internal key: `k114`
pub required_levels: i32,
/// This value is always [`KCEKValue::GDLevelList`]. See [`KCEKValue`]
///
/// Internal key: `kCEK`
pub kcek: KCEKValue,
/// Residual properties that are unused or unaccounted for.
pub other_properties: HashMap<String, Value>,
}
impl Default for GDList {
fn default() -> Self {
Self {
kcek: KCEKValue::GJLevelList,
id: 0,
name: String::new(),
description: String::new(),
creator: String::new(),
difficulty: GDListDifficulty::default(),
downloads: 0,
is_uploaded: false,
version: 0,
list_type: GDListType::default(),
likes: 0,
is_demon: false,
stars: 0,
featured: false,
original: None,
list_revision: 0,
account_id: 0,
unlisted: false,
favourited: false,
order: 0,
level_ids: vec![],
levels: vec![],
upload_time: 0,
update_time: None,
diamond_reward: 0,
required_levels: 0,
other_properties: HashMap::new(),
}
}
}
impl GDList {
/// Parses a plist dictionary a GD list object
pub fn from_dictionary(d: &Dictionary) -> Option<Self> {
/* Input dict structure
* kXX: regular list values
* k97: dict of levels: {"id": level, ...}
*/
let mut list = Self::default();
for (k, v) in d {
if k == "kCEK" {
list.kcek = KCEKValue::try_from(v.as_signed_integer().unwrap() as i32).unwrap();
continue;
}
let key_id = match k[1..].parse::<i32>() {
Ok(n) => n,
Err(_) => {
list.other_properties.insert(k.clone(), v.clone());
continue;
}
};
match key_id {
1 => list.id = v.as_signed_integer()? as i32,
2 => list.name = v.as_string()?.to_owned(),
3 => {
// special case: b64 encoded string
list.description = vec_as_str(&b64_decode(v.as_string()?).ok()?[..])
}
5 => list.creator = v.as_string()?.to_owned(),
7 => {
// special case: difficulty
list.difficulty =
GDListDifficulty::try_from(v.as_signed_integer()? as i32).ok()?
}
11 => list.downloads = v.as_signed_integer()? as i32,
15 => list.is_uploaded = v.as_boolean()?,
16 => list.version = v.as_signed_integer()? as i32,
21 => {
// special case: list type
list.list_type = GDListType::try_from(v.as_signed_integer()? as i32).ok()?
}
22 => list.likes = v.as_signed_integer()? as i32,
25 => list.is_demon = v.as_boolean()?,
26 => list.stars = v.as_signed_integer()? as i32,
27 => list.featured = v.as_boolean()?,
42 => list.original = Some(v.as_signed_integer()? as i32),
46 => list.list_revision = v.as_signed_integer()? as i32,
60 => list.account_id = v.as_signed_integer()? as i32,
79 => list.unlisted = v.as_boolean()?,
82 => list.favourited = v.as_boolean()?,
83 => list.order = v.as_signed_integer()? as i32,
96 => {
// special case: comma-separated list of ids
list.level_ids = parse_csv(v)?
}
97 => {
// special case: levels dictionary
// omit id (key) since it is found in the level anyway
list.levels = v
.as_dictionary()
.unwrap()
.iter()
.map(|(_id, level_dict)| {
match GDLevel::from_dict(level_dict.as_dictionary().unwrap()) {
Some(l) => l,
None => {
panic!("couldnt parse {level_dict:#?}")
}
}
})
.collect::<Vec<GDLevel>>();
}
98 => list.upload_time = v.as_signed_integer()? as i32,
99 => list.update_time = Some(v.as_signed_integer()? as i32),
113 => list.diamond_reward = v.as_signed_integer()? as i32,
114 => list.required_levels = v.as_signed_integer()? as i32,
_ => {
list.other_properties.insert(k.clone(), v.clone());
}
}
}
Some(list)
}
/// Serialises this object into a [`plist::Dictionary`]
pub fn to_dict(&self) -> Dictionary {
/* Field types
* i32: 1, 11, 16, 22, 26, 46, 60, 83, 98, 113, 114
* Option<i32>: 42, 99
* string: 2, 5
* bool: 15, 25, 27, 79, 82
*
* special cases:
* b64 string: 3
* list difficulty: 7
* list type: 21,
* csv (Vec<i32>): 96
* {id: level}: 97
*/
let mut d = Dictionary::new();
// i32 + difficulty (7) + type (21)
serialise_fields(
&mut d,
&[
("k1", self.id),
("k7", self.difficulty.to_num()),
("k11", self.downloads),
("k16", self.version),
("k21", self.list_type.to_num()),
("k22", self.likes),
("k26", self.stars),
("k46", self.list_revision),
("k60", self.account_id),
("k83", self.order),
("k98", self.upload_time),
("k113", self.diamond_reward),
("k114", self.required_levels),
],
);
// Option<i32>
serialise_optional_fields(&mut d, &[("k42", self.original), ("k99", self.update_time)]);
serialise_bool_fields(
&mut d,
&[
("k15", self.is_uploaded),
("k25", self.is_demon),
("k27", self.featured),
("k79", self.unlisted),
("k82", self.favourited),
],
);
// strings + b64 string (3) + csv (96)
serialise_fields(
&mut d,
&[
("k2", &self.name[..]),
("k3", &b64_encode(self.description.as_bytes())[..]),
("k5", &self.creator[..]),
("k96", &to_csv(&self.level_ids)[..]),
],
);
// k97
d.insert(
"k97".into(),
Value::Dictionary(Dictionary::from_iter(
self.levels
.iter()
.map(|l| (l.identity.id.to_string(), l.to_dict())),
)),
);
d.insert("kCEK".into(), Value::from(self.kcek.to_num()));
d.extend(self.other_properties.clone());
d
}
/// Parses a .gmd file to a `Self` object
pub fn from_gmdl<T: Into<PathBuf>>(path: T) -> Result<Self, GDError> {
let file = proper_plist_tags(vec_as_str(&read(path.into())?))?;
let xmltree = Value::from_reader_xml(Cursor::new(file.as_bytes()))?;
Ok(
Self::from_dictionary(xmltree.as_dictionary().unwrap()).ok_or(
GDError::CorruptedSavefile("Unable to parse file to valid level.".into()),
)?,
)
}
/// Exports the level to a .gmd file
pub fn export_to_gmdl<T: Into<PathBuf>>(&self, path: T) -> Result<(), GDError> {
let export_str = format!(
"{PLIST_HEADER}{}{PLIST_FOOTER}",
stringify_xml(&self.to_dict(), true)
);
write(path.into(), export_str)?;
Ok(())
}
}