gdlib 0.4.0

Rust library for editing Geometry Dash savefiles
Documentation
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! This module covers everything in the CCGameManager.dat file.

use std::{array, collections::HashMap, hash::BuildHasherDefault, io::Cursor};

use nohash_hasher::NoHashHasher;
use plist::{Dictionary, Value};

use crate::{
    cclocallevels::gdlevel::{GDLevel, PLIST_HEADER},
    core::{GDError, get_ccgamemanager_path, io::decrypt_file, proper_plist_tags},
    repr_t,
};

type IntMap<V> = HashMap<i32, V, BuildHasherDefault<NoHashHasher<i32>>>;

/// Container struct for the CCGameManager.dat file
#[derive(Debug, Default, Clone)]
pub struct CCGameManager {
    /// Info about the player. Namely, selected icons
    pub player_info: GDPlayerInfo,
    /// User statistics stored in the file
    pub stats: GDStatistics,
    /// User's configuration of the game (e.g. volume, resolution, texture quality, etc.)
    pub config: GDConfig,
    /// Account info
    pub account: GDAccount,
    /// Keybindings descriptors. Not much is known about these.
    ///
    /// Internal keys: `KBM_001`, `KBM_002` respectively
    pub keybinds: (Dictionary, Dictionary),
    /// Temporary state variables
    pub temp_state: GDCurrentValues,

    /// Unaccounted-for properties
    pub other_properties: HashMap<String, Value>,
}

impl CCGameManager {
    /// Parses the local CCGameManager.dat file if it exists and is a valid file.
    pub fn from_local() -> Result<Self, GDError> {
        let path = get_ccgamemanager_path().ok_or(GDError::MissingSavefile)?;
        Self::from_raw_string(decrypt_file(path).unwrap())
    }

    /// Parses a raw plist to this object
    pub fn from_raw_string(s: String) -> Result<Self, GDError> {
        if !s.starts_with(PLIST_HEADER) {
            return Err(GDError::CorruptedSavefile("Savefile header does not match the expected header. This may be due to a corrupted savefile or a savefile from a previous version of GD.".into()));
        };

        let xmltree = match Value::from_reader_xml(Cursor::new(proper_plist_tags(s)?.as_bytes())) {
            Ok(v) => v.into_dictionary().unwrap(),
            Err(e) => return Err(GDError::BadPlist(e)),
        };

        let mut this = Self::default();
        if let None = this.parse_dict(xmltree) {
            return Err(GDError::CorruptedSavefile(
                "Unable to parse corrupted savefile.".into(),
            ));
        }

        Ok(this)
    }

    fn parse_dict(&mut self, dict: Dictionary) -> Option<()> {
        let mut d = dict;
        // string
        parse_values(
            &mut d,
            &mut [
                ("playerUDID", &mut self.player_info.udid),
                ("playerName", &mut self.player_info.username),
                ("GJA_001", &mut self.account.username),
            ],
            |v| v.as_string().map(|v| v.to_string()),
        )?;

        // opt. string
        parse_values(
            &mut d,
            &mut [
                ("GJA_002", &mut self.account.plaintext_password),
                ("GJA_004", &mut self.account.session_id),
                ("GJA_005", &mut self.account.hashed_password),
            ],
            |v| v.as_string().map(|v| Some(v.to_string())),
        )?;

        // i32
        parse_values(
            &mut d,
            &mut [
                ("playerUserID", &mut self.player_info.user_id),
                ("playerFrame", &mut self.player_info.icon_cube),
                ("playerShip", &mut self.player_info.icon_ship),
                ("playerBall", &mut self.player_info.icon_ball),
                ("playerBird", &mut self.player_info.icon_ufo),
                ("playerDart", &mut self.player_info.icon_wave),
                ("playerRobot", &mut self.player_info.icon_robot),
                ("playerSpider", &mut self.player_info.icon_spider),
                ("playerSwing", &mut self.player_info.icon_swing),
                ("playerColor", &mut self.player_info.player_col1),
                ("playerColor2", &mut self.player_info.player_col2),
                ("playerColor3", &mut self.player_info.player_col_glow),
                ("playerStreak", &mut self.player_info.icon_streak),
                ("playerShipStreak", &mut self.player_info.ship_streak),
                ("playerDeathEffect", &mut self.player_info.death_effect),
                ("playerJetpack", &mut self.player_info.icon_jetpack),
                ("playerIconType", &mut self.player_info.icon_type),
                ("bootups", &mut self.stats.bootups),
                ("binaryVersion", &mut self.config.binary_version),
                ("timeOffset", &mut self.config.music_offset),
                ("GJA_003", &mut self.account.account_id),
                ("GS_20", &mut self.stats.demon_keys),
                ("GLM_11", &mut self.temp_state.current_daily_level),
                ("GLM_17", &mut self.temp_state.current_weekly_level),
            ],
            |v| v.as_signed_integer().map(|v| v as i32),
        )?;

        // bool
        parse_values(
            &mut d,
            &mut [
                ("playerGlow", &mut self.player_info.using_glow),
                ("hasRP", &mut self.player_info.is_moderator),
                ("showSongMarkers", &mut self.config.show_song_markers),
                ("showProgressBar", &mut self.config.show_progress_bar),
                ("clickedGarage", &mut self.config.has_clicked_garage),
                ("clickedEditor", &mut self.config.has_clicked_editor),
                ("clickedPractice", &mut self.config.has_clicked_practice),
                ("showedEditorGuide", &mut self.config.seen_editor_guide),
                ("showedLowDetailDialog", &mut self.config.seen_ldm_dialog),
                (
                    "showedRateStarDialog",
                    &mut self.config.seen_rate_star_dialog,
                ),
                ("hasRatedGame", &mut self.config.has_rated_game),
            ],
            |v| v.as_boolean(),
        )?;

        // f32
        parse_values(
            &mut d,
            &mut [
                ("bgVolume", &mut self.config.bgm_volume),
                ("sfxVolume", &mut self.config.sfx_volume),
                ("practicePosX", &mut self.config.practice_ui_pos.0),
                ("practicePosY", &mut self.config.practice_ui_pos.1),
                ("practiceOpacity", &mut self.config.practice_ui_opacity),
                ("customFPSTarget", &mut self.config.fps_target),
            ],
            |v| v.as_real().map(|v| v as f32),
        )?;

        for i in 0..5 {
            parse_val(&mut d, &format!("dpad0{}", i + 1), |v| {
                self.config.dpads[i] = GDPlatformerUI::from_str(v.as_string().unwrap());
                Some(())
            })?;
        }

        self.config.dpad_layout = d
            .get("dpad_layout")
            .map(|v| GDPlatformerUI::from_str(v.as_string().unwrap()));

        parse_val(&mut d, "resolution", |v| {
            self.config.resolution = Resolution::try_from(v.as_signed_integer()? as i32).ok()?;
            Some(())
        })?;
        parse_val(&mut d, "texQuality", |v| {
            self.config.text_quality =
                TextureQuality::try_from(v.as_signed_integer()? as i32).ok()?;
            Some(())
        })?;

        parse_val(&mut d, "KBM_001", |v| {
            self.keybinds.0 = v.as_dictionary()?.clone();
            Some(())
        })?;

        parse_val(&mut d, "KBM_002", |v| {
            self.keybinds.1 = v.as_dictionary()?.clone();
            Some(())
        })?;

        // {level_id: GDlevel}
        // common format for storing lists of levels
        // though, some level lists follow a slightly different schema for keys
        parse_values(
            &mut d,
            &mut [
                ("GLM_01", &mut self.stats.official_level_progresses),
                ("GLM_03", &mut self.stats.online_levels_played),
                ("GLM_16", &mut self.stats.gauntlet_levels_played),
            ],
            |v| parse_level_dict(&v),
        )?;

        // {i32: "1"}
        // robtop seems to use this format for lists of things
        parse_values(
            &mut d,
            &mut [
                ("GLM_06", &mut self.account.following_creators),
                ("GLM_07", &mut self.temp_state.last_played_levels),
                ("GLM_13", &mut self.stats.submitted_ratings),
                ("GLM_14", &mut self.account.reported_levels),
                ("GLM_15", &mut self.stats.submitted_ratings_demons),
            ],
            |v| {
                Some(
                    v.as_dictionary()?
                        .iter()
                        .map(|(k, _)| k.parse::<i32>().unwrap())
                        .collect::<Vec<_>>(),
                )
            },
        )?;

        parse_values(
            &mut d,
            &mut [
                ("GLM_18", &mut self.config.saved_levels_foldernames),
                ("GLM_19", &mut self.config.local_levels_foldernames),
            ],
            parse_foldernames,
        )?;

        parse_val(
            &mut d,
            "GLM_12",
            // keys always of the form `likes_a_b_c_d` where a, b, c, d are i32
            |v| {
                self.config.glm12_unknown = v
                    .as_dictionary()?
                    .iter()
                    .map(|(k, _)| {
                        let mut split = k.split("_").into_iter();
                        let _ = split.next(); // skip `like`
                        let keys =
                            array::from_fn(|_| split.next().unwrap().parse::<i32>().unwrap());
                        keys
                    })
                    .collect::<Vec<_>>();
                Some(())
            },
        )?;

        parse_val(&mut d, "GLM_10", |v| {
            self.stats.completed_dailies = v
                .as_dictionary()?
                .iter()
                .map(|(k, v)| {
                    (
                        k.parse::<i32>().unwrap(),
                        GDLevel::from_dict(v.as_dictionary().unwrap()).unwrap(),
                    )
                })
                .collect();

            Some(())
        })?;

        /* Values not parsed */
        // GLM_02, GLM_04, GS_8: These keys are unused and modern (2.2) GD savefiles.

        self.other_properties = d.into_iter().collect();

        Some(())
    }
}

// removes all valid values specified in `fields`
// this function ensures that `d` is left with only the unaccounted keys
#[must_use]
fn parse_values<F: Fn(Value) -> Option<R>, R>(
    d: &mut Dictionary,
    fields: &mut [(&str, &mut R)],
    parser: F,
) -> Option<()> {
    for (k, f) in fields {
        if let Some(v) = d.remove(k) {
            **f = parser(v)?;
        }
    }
    Some(())
}

#[must_use]
fn parse_val<F: FnMut(Value) -> Option<()>>(
    d: &mut Dictionary,
    key: &str,
    mut parser: F,
) -> Option<()> {
    if let Some(v) = d.remove(key) {
        parser(v)
    } else {
        Some(())
    }
}

fn parse_level_dict(v: &Value) -> Option<Vec<GDLevel>> {
    v.as_dictionary()?
        .iter()
        .map(|(_id, level_dict)| GDLevel::from_dict(level_dict.as_dictionary().unwrap()))
        .collect::<Option<Vec<GDLevel>>>()
}

fn parse_foldernames(v: Value) -> Option<Vec<(i32, String)>> {
    if v.as_dictionary()?.is_empty() {
        return Some(vec![]);
    }

    let mut raw_folders = v
        .as_dictionary()?
        .iter()
        .map(|(idx, name)| {
            (
                idx.parse::<i32>().unwrap(),
                name.as_string().unwrap().to_string(),
            )
        })
        .collect::<Vec<_>>();

    // this ensures that the folders are ordered by index
    raw_folders.sort_by(|(a, _), (b, _)| a.cmp(b));

    Some(raw_folders)
}

/// Player info: username, UDID, user id, all icon info
#[derive(Debug, Default, Clone)]
#[allow(missing_docs)]
pub struct GDPlayerInfo {
    pub username: String,
    pub udid: String,
    pub user_id: i32,
    /// Internally, `playerFrame`
    pub icon_cube: i32,
    pub icon_ship: i32,
    pub icon_ball: i32,
    /// Internally, `playerBird`
    pub icon_ufo: i32,
    /// Internally, `playerDart`
    pub icon_wave: i32,
    pub icon_robot: i32,
    pub icon_spider: i32,
    pub icon_swing: i32,
    pub player_col1: i32,
    pub player_col2: i32,
    pub player_col_glow: i32,
    pub icon_streak: i32,
    pub ship_streak: i32,
    pub death_effect: i32,
    pub icon_jetpack: i32,
    pub icon_type: i32,
    pub using_glow: bool,
    pub is_moderator: bool,
}

/// Player-specific statistics.
#[derive(Debug, Default, Clone)]
#[allow(missing_docs)]
pub struct GDStatistics {
    /// Number of times this player has launched GD
    pub bootups: i32,
    // todo: achievements, GLM_XX, GS_XX
    /// All official levels that the player has progress on.
    ///
    /// Internal key: `GLM_01`
    pub official_level_progresses: Vec<GDLevel>,
    pub online_levels_played: Vec<GDLevel>,
    pub demon_keys: i32,
    /// All levels the player has submitted ratings on
    pub submitted_ratings: Vec<i32>,
    /// All demon levels the player has submitted ratings on
    pub submitted_ratings_demons: Vec<i32>,
    /// All gauntlet levels that the player has progress on
    pub gauntlet_levels_played: Vec<GDLevel>,
    /// All completed dailies in the form: {timely id: level}
    pub completed_dailies: IntMap<GDLevel>,
}

/// User's configuration of the game.
#[derive(Debug, Default, Clone)]
#[allow(missing_docs)]
pub struct GDConfig {
    pub bgm_volume: f32,
    pub sfx_volume: f32,
    pub text_quality: TextureQuality,
    pub resolution: Resolution,
    pub show_song_markers: bool,
    pub show_progress_bar: bool,
    pub has_clicked_garage: bool,
    pub has_clicked_editor: bool,
    pub has_clicked_practice: bool,
    pub seen_editor_guide: bool,
    pub seen_ldm_dialog: bool,
    pub seen_rate_star_dialog: bool,
    pub has_rated_game: bool,
    pub binary_version: i32,
    pub practice_ui_pos: (f32, f32),
    pub practice_ui_opacity: f32,
    pub fps_target: f32,
    /// Music offset in milliseconds
    pub music_offset: i32,
    /// `dpadn` for n in 1..=5
    pub dpads: [GDPlatformerUI; 5],
    pub dpad_layout: Option<GDPlatformerUI>,
    /// List of folder names for saved online levels. Folder names are stored in order, starting from folder 1. If an unnamed folder is found at index >= 1, it is stored as a `None`.
    pub saved_levels_foldernames: Vec<(i32, String)>,
    /// List of folder names for locally created levels (found in the editor tab). Folder names are stored in order, starting from folder 1. If an unnamed folder is found at index >= 1, it is stored as a `None`.
    pub local_levels_foldernames: Vec<(i32, String)>,
    /// Raw GLM_12 key encoding optimized for size. This key has a purpose that is assumed to be related to likes, though it is unknown.
    ///
    /// Internal key: `GLM_12`
    pub glm12_unknown: Vec<[i32; 4]>,
}

repr_t!(
    strict TextureQuality: i32 {
        Auto = 0,
        Low = 1,
        Medium = 2,
        High = 3,
    } default Auto
);

repr_t!(
    strict Resolution: i32 {
        R640x480 = 1,     // 4:3
        R720x480 = 2,     // 3:2
        R720x576 = 3,     // 5:4
        R800x600 = 4,     // 4:3
        R1024x768 = 5,    // 4:3
        R1152x864 = 6,    // 4:3
        R1176x664 = 7,    // 147:83
        R1280x720 = 8,    // 16:9
        R1280x768 = 9,    // 5:3
        R1280x800 = 10,   // 16:10
        R1280x960 = 11,   // 4:3
        R1280x1024 = 12,  // 5:4
        R1360x768 = 13,   // 85:48
        R1366x768 = 14,   // 683:384
        R1440x900 = 15,   // 16:10
        R1600x900 = 16,   // 16:9
        R1600x1024 = 17,  // 25:16
        R1600x1200 = 18,  // 4:3
        R1680x1050 = 19,  // 16:10
        R1768x992 = 20,   // 221:124
        R1920x1080 = 21,  // 16:9
        R1920x1200 = 22,  // 16:10
        R1920x1440 = 23,  // 4:3
        R2048x1536 = 24,  // 4:3
        R2560x1440 = 25,  // 16:9
        R2560x1600 = 26,  // 16:10
        R3840x2160 = 27,  // 16:9
    } default R1920x1080
);

#[derive(Debug, Default, Clone)]
#[allow(missing_docs)]
/// Platformer controls UI config
pub struct GDPlatformerUI {
    pub width: i32,      // The width of the button hitboxl
    pub height: i32,     // The height of the button hitbox
    pub scale: f32,      // The scale of the buttons
    pub opacity: i32,    // The button opacity (from 0 to 255)
    pub pos: (f32, f32), // The position of the buttons
    pub mode_b: bool,    // The ModeB checkbox
    pub deadzone: f32,   // The deadzone between the buttons
    pub radius: f32,     // The distance between the buttons
    pub snap: bool,      // The Snap checkbox
    pub split: bool,     // The Split checkbox
}

impl GDPlatformerUI {
    /// Parses a comma-separated list of values to this object
    pub fn from_str(s: &str) -> Self {
        let mut this = Self::default();
        let fns = &[
            Self::parse_width,
            Self::parse_height,
            Self::parse_scale,
            Self::parse_opacity,
            Self::parse_pos_x,
            Self::parse_pos_y,
            Self::parse_mode_b,
            Self::parse_deadzone,
            Self::parse_radius,
            Self::parse_snap,
            Self::parse_split,
        ];
        s.split(",")
            .into_iter()
            .enumerate()
            .for_each(|(idx, s)| (fns[idx])(&mut this, s));

        this
    }

    fn parse_width(&mut self, s: &str) {
        self.width = s.parse::<i32>().unwrap();
    }
    fn parse_height(&mut self, s: &str) {
        self.height = s.parse::<i32>().unwrap();
    }
    fn parse_scale(&mut self, s: &str) {
        self.scale = s.parse::<f32>().unwrap();
    }
    fn parse_opacity(&mut self, s: &str) {
        self.opacity = s.parse::<i32>().unwrap();
    }
    fn parse_pos_x(&mut self, s: &str) {
        self.pos.0 = s.parse::<f32>().unwrap();
    }
    fn parse_pos_y(&mut self, s: &str) {
        self.pos.1 = s.parse::<f32>().unwrap();
    }
    fn parse_mode_b(&mut self, s: &str) {
        self.mode_b = s.parse::<i32>().unwrap() == 1;
    }
    fn parse_deadzone(&mut self, s: &str) {
        self.deadzone = s.parse::<f32>().unwrap();
    }
    fn parse_radius(&mut self, s: &str) {
        self.radius = s.parse::<f32>().unwrap();
    }
    fn parse_snap(&mut self, s: &str) {
        self.snap = s.parse::<i32>().unwrap() == 1;
    }
    fn parse_split(&mut self, s: &str) {
        self.split = s.parse::<i32>().unwrap() == 1;
    }
}

/// Configuration to do with the player's account and social settings
#[derive(Debug, Default, Clone)]
#[allow(missing_docs)]
pub struct GDAccount {
    pub username: String,
    /// Password in plaintext (used in 2.1 and below)
    pub plaintext_password: Option<String>,
    pub account_id: i32,
    /// Appears to be unused
    pub session_id: Option<String>,
    /// Password encrypted with GJP2 encryption. This can be generated with [`crate::core::crypto::generate_gjp2_hexdigest`]
    pub hashed_password: Option<String>,
    /// List of creators' account IDs that this player follows
    pub following_creators: Vec<i32>,
    /// List of levels that the player has reported.
    ///
    /// Internal key: `GLM_14`
    pub reported_levels: Vec<i32>,
}

/// Temporary variables stored in the savefile that are expected to be overwritten in the future
#[allow(missing_docs)]
#[derive(Debug, Default, Clone)]
pub struct GDCurrentValues {
    /// Levels that were played in the last session
    pub last_played_levels: Vec<i32>,
    /// The current daily level's TimelyID
    pub current_daily_level: i32,
    /// The current weekly level's TimelyID
    pub current_weekly_level: i32,
}

/* TODO: for GLM_08, make a GDSearchFilter struct. all fields are boolean, so use bitflags */
/* TODO 2: add a "Internal key: `...`" footer for every (sub)struct field docstring in CCGameManager */