portablemc 5.0.3

Developer-oriented crate for launching Minecraft quickly and reliably with included support for Mojang versions and popular mod loaders. See portablemc-cli for the reference implementation.
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
//! Extension to the base installer with verification and installation of missing
//! Mojang versions, it also provides support for common arguments and fixes on legacy
//! versions.

pub(crate) mod serde;

use std::io::{Write as _, BufReader};
use std::path::{Path, PathBuf};
use std::collections::HashSet;
use std::env;
use std::fs;

use chrono::{DateTime, FixedOffset};
use regex::Regex;
use uuid::Uuid;

use crate::base::{self, check_file_advanced, Game, HandlerInto as _, LibraryDownload, LoadedLibrary, VersionChannel, LIBRARIES_URL};
use crate::maven::Gav;
use crate::download;
use crate::msa;


/// Static URL to the version manifest provided by Mojang.
pub(crate) const VERSION_MANIFEST_URL: &str = "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json";

/// An installer for supporting Mojang-provided versions. It provides support for various
/// standard arguments such as demo mode, window resolution and quick play, it also 
/// provides various fixes for known issues of old versions.
/// 
/// By default, this installer tries to handle every version that is not found in the
/// hierarchy, if a version with that name exists in the Mojang's versions manifest, then
/// it is fetched. This means that every missing version will try to fetch the manifest
/// (using the cached version if relevant). This behavior can be changed by excluding 
/// 
/// Notes about various versions:
/// - 1.19.3 metadata adds no parameter to specify extract directory for LWJGL (version
///   3.3.1-build-7), therefore natives are extracted to 
///   '/tmp/lwjgl<username>/<version>'.
#[derive(Debug, Clone)]
pub struct Installer {
    /// The underlying base installer logic.
    base: base::Installer,
    /// Inner installer data, put in a sub struct to fix borrow issue.
    inner: InstallerInner,
}

/// Internal installer data.
#[derive(Debug, Clone)]
struct InstallerInner {
    version: Version,
    fetch_excludes: Vec<FetchExclude>,
    demo: bool,
    quick_play: Option<QuickPlay>,
    resolution: Option<(u16, u16)>,
    disable_multiplayer: bool,
    disable_chat: bool,
    auth_type: String,  // Empty to trigger default auth.
    auth_uuid: Uuid,
    auth_username: String,
    auth_token: String,
    auth_xuid: String,  // Apparently used for Minecraft Telemetry
    client_id: String,  // Apparently used for Minecraft Telemetry
    fix_legacy_quick_play: bool,
    fix_legacy_proxy: bool,
    fix_legacy_merge_sort: bool,
    fix_legacy_resolution: bool,
    fix_broken_authlib: bool,
    fix_lwjgl: Option<String>,
}

impl Installer {

    /// Create a new installer with default configuration, using defaults directories.
    /// 
    /// This Mojang installer has all fixes enabled except LWJGL and missing version 
    /// fetching is enabled.
    pub fn new(version: impl Into<Version>) -> Self {
        Self {
            base: base::Installer::new(String::new()),
            inner: InstallerInner {
                version: version.into(),
                fetch_excludes: Vec::new(),  // No exclude by default.
                demo: false,
                quick_play: None,
                resolution: None,
                disable_multiplayer: false,
                disable_chat: false,
                auth_type: String::new(),
                auth_uuid: Uuid::nil(),
                auth_username: String::new(),
                auth_token: String::new(),
                auth_xuid: String::new(),
                client_id: String::new(),
                fix_legacy_quick_play: true,
                fix_legacy_proxy: true,
                fix_legacy_merge_sort: true,
                fix_legacy_resolution: true,
                fix_broken_authlib: true,
                fix_lwjgl: None,
            }
        }
    }

    /// Same as [`Self::new`] but targets latest release version by default.
    pub fn new_with_release() -> Self {
        Self::new(Version::Release)
    }

    /// Get the underlying base installer.
    #[inline]
    pub fn base(&self) -> &base::Installer {
        &self.base
    }

    /// Get the underlying base installer through mutable reference.
    /// 
    /// *Note that the `version` property will be overwritten when installing.*
    #[inline]
    pub fn base_mut(&mut self) -> &mut base::Installer {
        &mut self.base
    }

    /// Get the mojang version to install.
    #[inline]
    pub fn version(&self) -> &Version {
        &self.inner.version
    }

    /// Set the mojang version to install.
    #[inline]
    pub fn set_version(&mut self, version: impl Into<Version>) -> &mut Self {
        self.inner.version = version.into();
        self
    }

    /// Return the list of filters 
    #[inline]
    pub fn fetch_excludes(&self) -> &[FetchExclude] {
        &self.inner.fetch_excludes
    }

    /// Clear all fetch exclude filters. See [`Self::fetch_excludes`] and 
    /// [`Self::add_fetch_exclude`]. **This is the default state when constructed.**
    pub fn clear_fetch_exclude(&mut self) -> &mut Self {
        self.inner.fetch_excludes.clear();
        self
    }

    /// Append the given filter to the fetch exclude list.
    pub fn add_fetch_exclude(&mut self, exclude: FetchExclude) -> &mut Self {
        self.inner.fetch_excludes.push(exclude);
        self
    }

    /// Get if the demo mode is enabled for the game.
    #[inline]
    pub fn demo(&self) -> bool {
        self.inner.demo
    }

    /// Set if the demo mode should be enabled for the game.
    #[inline]
    pub fn set_demo(&mut self, demo: bool) -> &mut Self {
        self.inner.demo = demo;
        self
    }

    /// If enabled, get the Quick Play configuration when launching the game.
    #[inline]
    pub fn quick_play(&self) -> Option<&QuickPlay> {
        self.inner.quick_play.as_ref()
    }

    /// Enables Quick Play when launching the game, from 1.20 (23w14a).
    #[inline]
    pub fn set_quick_play(&mut self, quick_play: QuickPlay) -> &mut Self {
        self.inner.quick_play = Some(quick_play);
        self
    }

    /// Remove Quick Play when launching, this is the default.
    #[inline]
    pub fn remove_quick_play(&mut self) -> &mut Self {
        self.inner.quick_play = None;
        self
    }

    /// If enabled, get the initial game's window resolution.
    #[inline]
    pub fn resolution(&self) -> Option<(u16, u16)> {
        self.inner.resolution
    }

    /// Set an initial resolution for the game's window.
    #[inline]
    pub fn set_resolution(&mut self, width: u16, height: u16) -> &mut Self {
        self.inner.resolution = Some((width, height));
        self
    }

    /// Remove initial resolution for the game's window, this is the default.
    #[inline]
    pub fn remove_resolution(&mut self) -> &mut Self {
        self.inner.resolution = None;
        self
    }

    /// Get if multiplayer should be disabled when launching the game.
    #[inline]
    pub fn disable_multiplayer(&self) -> bool {
        self.inner.disable_multiplayer
    }

    /// Disable or not the multiplayer when launching the game.
    #[inline]
    pub fn set_disable_multiplayer(&mut self, disable_multiplayer: bool) -> &mut Self {
        self.inner.disable_multiplayer = disable_multiplayer;
        self
    }

    /// Get if the chat should be disabled when launching the game.
    #[inline]
    pub fn disable_chat(&self) -> bool {
        self.inner.disable_chat
    }

    /// Disable or not the chat when launching the game.
    #[inline]
    pub fn set_disable_chat(&mut self, disable_chat: bool) -> &mut Self {
        self.inner.disable_chat = disable_chat;
        self
    }

    /// Get the currently configured authentication UUID, may be nil (zero-filled) if not 
    /// configured.
    /// 
    /// Note that when installing, if this is nil then it will be defined using 
    /// [`Self::set_auth_offline_hostname`].
    #[inline]
    pub fn auth_uuid(&self) -> Uuid {
        self.inner.auth_uuid
    }

    /// Get the currently configured authentication UUID, may be empty if not configured.
    /// 
    /// Note that when installing, if this is nil then it will be defined using 
    /// [`Self::set_auth_offline_hostname`].
    #[inline]
    pub fn auth_username(&self) -> &str {
        &self.inner.auth_username
    }

    /// Internal function to reset to zero-length all online-related auth variables.
    fn reset_auth_online(&mut self) -> &mut Self {
        self.inner.auth_type = String::new();
        self.inner.auth_token = String::new();
        self.inner.auth_xuid = String::new();
        self
    }

    /// Use offline session with the given UUID and username, note that the username will
    /// be truncated 16 bytes at most (this function will panic if the truncation is not 
    /// on a valid UTF-8 character boundary).
    pub fn set_auth_offline(&mut self, uuid: Uuid, username: impl Into<String>) -> &mut Self {
        self.inner.auth_uuid = uuid;
        self.inner.auth_username = username.into();
        self.inner.auth_username.truncate(16);
        self.reset_auth_online()
    }

    /// Use offline session with the given UUID, the username is derived from the first
    /// 8 characters of the rendered UUID.
    pub fn set_auth_offline_uuid(&mut self, uuid: Uuid) -> &mut Self {
        self.inner.auth_uuid = uuid;
        self.inner.auth_username = uuid.to_string();
        self.inner.auth_username.truncate(8);
        self.reset_auth_online()
    }

    /// Use offline session with the given username (initially truncated to 16 chars), 
    /// the UUID is then derived from this username using the same derivation used by 
    /// most Mojang clients (versions to be defined), this produces a MD5 (v3) UUID 
    /// with `OfflinePlayer:{username}` as the hashed string.
    /// 
    /// The advantage of this method is to produce the same UUID as the one that will
    /// be produced by Mojang's authlib when connecting to an offline-mode multiplayer
    /// server.
    pub fn set_auth_offline_username(&mut self, username: impl Into<String>) -> &mut Self {
        
        self.inner.auth_username = username.into();
        self.inner.auth_username.truncate(16);

        let mut context = md5::Context::new();
        context.write_fmt(format_args!("OfflinePlayer:{}", self.inner.auth_username)).unwrap();
        
        self.inner.auth_uuid = uuid::Builder::from_bytes(context.compute().0)
            .with_variant(uuid::Variant::RFC4122)
            .with_version(uuid::Version::Md5)
            .into_uuid();

        self.reset_auth_online()

    }

    /// Use offline session with the given username (initially truncated to 16 chars), 
    /// the UUID is then derived from this username using a PMC-specific derivation of 
    /// the username and the PMC namespace with SHA-1 (UUID v5).
    /// 
    /// Note that the produced UUID will not be used when playing on multiplayer servers
    /// (the server must also be in offline-mode), in this case the server gives you an
    /// arbitrary UUID that is not the one your game has been launched with. Most servers
    /// uses the UUID derivation embedded in Mojang's authlib, deriving the UUID from the
    /// username, if you want the UUID to be coherent with this derivation, you can use
    /// [`Self::set_auth_offline_username`] instead.
    pub fn set_auth_offline_username_legacy(&mut self, username: impl Into<String>) -> &mut Self {
        self.inner.auth_username = username.into();
        self.inner.auth_username.truncate(16);
        self.inner.auth_uuid = Uuid::new_v5(&base::UUID_NAMESPACE, self.inner.auth_username.as_bytes());
        self.reset_auth_online()
    }

    /// Use offline session with a deterministic UUID, derived from this machine's 
    /// hostname, the username is then derived from the UUID following the same logic
    /// as for [`Self::set_auth_offline_uuid`].
    /// 
    /// **This is the default UUID/username used if no auth is specified, so you don't
    /// need to call this function, except if you want to override previous auth.**
    pub fn set_auth_offline_hostname(&mut self) -> &mut Self {
        self.set_auth_offline_uuid(Uuid::new_v5(&base::UUID_NAMESPACE, gethostname::gethostname().as_encoded_bytes()))
    }

    /// Use online authentication with the given Microsoft Account.
    pub fn set_auth_msa(&mut self, account: &msa::Account) -> &mut Self {
        self.inner.auth_uuid = account.uuid();
        self.inner.auth_username = account.username().to_string();
        self.inner.auth_token = account.access_token().to_string();
        self.inner.auth_type = "msa".to_string();
        self.inner.auth_xuid = account.xuid().to_string();
        self
    }

    /// Get the client ID used for telemetry of the game, the default client id is empty
    /// and therefore the telemetry can't use it.
    #[inline]
    pub fn client_id(&self) -> &str {
        &self.inner.client_id
    }

    /// See [`Self::client_id`].
    #[inline]
    pub fn set_client_id(&mut self, client_id: impl Into<String>) -> &mut Self {
        self.inner.client_id = client_id.into();
        self
    }

    /// When starting versions older than 1.20 (23w14a) where Quick Play was not supported
    /// by the client, this fix tries to use legacy arguments instead, such as --server
    /// and --port, this is enabled by default.
    #[inline]
    pub fn fix_legacy_quick_play(&self) -> bool {
        self.inner.fix_legacy_quick_play
    }

    /// See [`Self::fix_legacy_quick_play`].
    #[inline]
    pub fn set_fix_legacy_quick_play(&mut self, fix: bool) -> &mut Self {
        self.inner.fix_legacy_quick_play = fix;
        self
    }

    /// When starting older alpha, beta and release up to 1.5, this allows legacy online
    /// resources such as skins to be properly requested. The implementation is currently 
    /// using `betacraft.uk` proxies, this is enabled by default.
    #[inline]
    pub fn fix_legacy_proxy(&self) -> bool {
        self.inner.fix_legacy_proxy
    }

    /// See [`Self::fix_legacy_proxy`].
    #[inline]
    pub fn set_fix_legacy_proxy(&mut self, fix: bool) -> &mut Self {
        self.inner.fix_legacy_proxy = fix;
        self
    }

    /// When starting older alpha and beta versions, this adds a JVM argument to use the
    /// legacy merge sort `java.util.Arrays.useLegacyMergeSort=true`, this is required on
    /// some old versions to avoid crashes, this is enabled by default.
    #[inline]
    pub fn fix_legacy_merge_sort(&self) -> bool {
        self.inner.fix_legacy_merge_sort
    }

    /// See [`Self::fix_legacy_merge_sort`].
    #[inline]
    pub fn set_fix_legacy_merge_sort(&mut self, fix: bool) -> &mut Self {
        self.inner.fix_legacy_merge_sort = fix;
        self
    }

    /// When starting older versions that don't support modern resolution arguments, this
    /// fix will add arguments to force resolution of the initial window, this is enabled 
    /// by default.
    #[inline]
    pub fn fix_legacy_resolution(&self) -> bool {
        self.inner.fix_legacy_resolution
    }

    /// See [`Self::fix_legacy_resolution`].
    #[inline]
    pub fn set_fix_legacy_resolution(&mut self, fix: bool) -> &mut Self {
        self.inner.fix_legacy_resolution = fix;
        self
    }

    /// Versions 1.16.4 and 1.16.5 uses authlib:2.1.28 which cause multiplayer button
    /// (and probably in-game chat) to be disabled, this can be fixed by switching to
    /// version 2.2.30 of authlib, this is enabled by default.
    #[inline]
    pub fn fix_broken_authlib(&self) -> bool {
        self.inner.fix_broken_authlib
    }

    /// See [`Self::fix_broken_authlib`].
    #[inline]
    pub fn set_fix_broken_authlib(&mut self, fix: bool) -> &mut Self {
        self.inner.fix_broken_authlib = fix;
        self
    }

    /// See [`Self::set_fix_lwjgl`].
    #[inline]
    pub fn fix_lwjgl(&self) -> Option<&str> {
        self.inner.fix_lwjgl.as_deref()
    }

    /// Changing the version of LWJGL, this support versions greater or equal to 3.2.3,
    /// and also provides ARM support when the LWJGL version supports it. It's not 
    /// guaranteed to work with every version of Minecraft, and downgrading LWJGL version
    /// is not recommended.
    /// 
    /// If the given version is less than 3.2.3 this will do nothing.
    #[inline]
    pub fn set_fix_lwjgl(&mut self, lwjgl_version: impl Into<String>) -> &mut Self {
        self.inner.fix_lwjgl = Some(lwjgl_version.into());
        self
    }
    
    /// Don't fix LWJGL version, see [`Self::set_fix_lwjgl`].
    #[inline]
    pub fn remove_fix_lwjgl(&mut self) -> &mut Self {
        self.inner.fix_lwjgl = None;
        self
    }

    /// Install the given Mojang version from its identifier. This also supports alias
    /// identifiers such as "release" and "snapshot" that will be resolved, note that
    /// these identifiers are just those presents in the "latest" mapping of the
    /// Mojang versions manifest. 
    /// 
    /// If the given version is not found in the manifest then it's silently ignored and
    /// the version metadata must already exists.
    #[inline]
    pub fn install(&mut self, mut handler: impl Handler) -> Result<Game> {
        self.install_dyn(&mut handler)
    }

    #[inline(never)]
    fn install_dyn(&mut self, handler: &mut dyn Handler) -> Result<Game> {
        
        // Apply default offline auth, derived from hostname.
        if self.inner.auth_uuid.is_nil() || self.inner.auth_username.is_empty() {
            self.set_auth_offline_hostname();
        }

        let &mut Self {
            ref mut base,
            ref inner,
        } = self;

        let manifest = match self.inner.version {
            Version::Release | 
            Version::Snapshot => Some(Manifest::request((&mut *handler).into_download())?),
            _ => None
        };

        let version = match &self.inner.version {
            Version::Release => manifest.as_ref().unwrap().latest_release_name(),
            Version::Snapshot => manifest.as_ref().unwrap().latest_snapshot_name(),
            Version::Name(name) => name.as_str(),
        };

        base.set_version(version);
        
        // Let the handler find the "leaf" version.
        let mut leaf_version = String::new();

        // Scoping the temporary internal handler.
        let mut game = {

            let mut handler = InternalHandler {
                inner: &mut *handler,
                installer: &inner,
                error: Ok(()),
                manifest,
                leaf_version: &mut leaf_version,
            };
    
            // Same as above, we are giving a &mut dyn ref to avoid huge monomorphization.
            let res = base.install(&mut handler);
            handler.error?;
            res?

        };

        // Apply auth parameters.
        game.replace_args(|arg| {
            Some(match arg {
                "auth_player_name" => inner.auth_username.clone(),
                "auth_uuid" => inner.auth_uuid.as_simple().to_string(),
                "auth_access_token" => inner.auth_token.clone(),
                "auth_xuid" => inner.auth_xuid.clone(),
                // Legacy parameter
                "auth_session" if !inner.auth_token.is_empty() => 
                    format!("token:{}:{}", inner.auth_token, inner.auth_uuid.as_simple()),
                "auth_session" => String::new(),
                "user_type" => inner.auth_type.clone(),
                "user_properties" => format!("{{}}"),
                "clientid" => inner.client_id.clone(),
                _ => return None
            })
        });

        // If Quick Play is enabled, we know that the feature has been enabled by the
        // handler, and if the feature is actually present (1.20 and after), if not
        // present we can try to use legacy arguments for supported quick play types.
        if let Some(quick_play) = &inner.quick_play {

            let quick_play_arg = match quick_play {
                QuickPlay::Path { .. } => "quickPlayPath",
                QuickPlay::Singleplayer { .. } => "quickPlaySingleplayer",
                QuickPlay::Multiplayer { .. } => "quickPlayMultiplayer",
                QuickPlay::Realms { .. } => "quickPlayRealms",
            };

            let mut quick_play_supported = false;
            game.replace_args(|arg| {
                if arg == quick_play_arg {
                    quick_play_supported = true;
                    Some(match quick_play {
                        QuickPlay::Path { path } => path.display().to_string(),
                        QuickPlay::Singleplayer { name } => name.clone(),
                        QuickPlay::Multiplayer { host, port } => format!("{host}:{port}"),
                        QuickPlay::Realms { id } => id.clone(),
                    })
                } else {
                    None
                }
            });

            if !quick_play_supported && inner.fix_legacy_quick_play {
                if let QuickPlay::Multiplayer { host, port } = quick_play {

                    game.game_args.extend([
                        "--server".to_string(), host.clone(),
                        "--port".to_string(), port.to_string(),
                    ]);

                    quick_play_supported = true;
                    handler.on_event(Event::FixedLegacyQuickPlay);

                }
            }

            if !quick_play_supported {
                handler.on_event(Event::WarnUnsupportedQuickPlay {  });
            }

        }

        if inner.fix_legacy_proxy {

            // Checking as bytes because it's ASCII and we simply matching.
            let proxy_port = match leaf_version.as_bytes() {
                [b'1', b'.', b'0' | b'1' | b'3' | b'4' | b'5'] |
                [b'1', b'.', b'2' | b'3' | b'4' | b'5', b'.', ..] |
                b"13w16a" | b"13w16b" => Some(11707),
                id if id.starts_with(b"a1.0.") => Some(80),
                id if id.starts_with(b"a1.1.") => Some(11702),
                id if id.starts_with(b"a1.") => Some(11705),
                id if id.starts_with(b"b1.") => Some(11705),
                _ => None,
            };

            if let Some(proxy_port) = proxy_port {
                game.jvm_args.push(format!("-Dhttp.proxyHost=betacraft.uk"));
                game.jvm_args.push(format!("-Dhttp.proxyPort={proxy_port}"));
                handler.on_event(Event::FixedLegacyProxy { host: "betacraft.uk", port: proxy_port });
            }

        }

        if inner.fix_legacy_merge_sort && (leaf_version.starts_with("a1.") || leaf_version.starts_with("b1.")) {
            game.jvm_args.push("-Djava.util.Arrays.useLegacyMergeSort=true".to_string());
            handler.on_event(Event::FixedLegacyMergeSort);
        }

        if let Some((width, height)) = inner.resolution {

            let mut resolution_supported = false;
            game.replace_args(|arg| {
                let repl = match arg {
                    "resolution_width" => width.to_string(),
                    "resolution_height" => height.to_string(),
                    _ => return None
                };
                resolution_supported = true;
                Some(repl)
            });

            if !resolution_supported && inner.fix_legacy_resolution {

                game.game_args.extend([
                    "--width".to_string(), width.to_string(),
                    "--height".to_string(), height.to_string(),
                ]);

                resolution_supported = true;
                handler.on_event(Event::FixedLegacyResolution);

            }

            if !resolution_supported {
                handler.on_event(Event::WarnUnsupportedResolution);
            }

        }

        if inner.disable_multiplayer {
            game.game_args.push("--disableMultiplayer".to_string());
        }

        if inner.disable_chat {
            game.game_args.push("--disableChat".to_string());
        }

        Ok(game)

    }

}

/// Events happening when installing.
#[derive(Debug)]
#[non_exhaustive]
pub enum Event<'a> {
    /// Forwarding a base event.
    Base(base::Event<'a>),
    /// When the given version is being loaded but the file has an invalid size,
    /// SHA-1, or any other invalidating reason, it has been removed in order to 
    /// download an up-to-date version.
    InvalidatedVersion { version: &'a str },
    /// The required version metadata is missing and so will be fetched.
    FetchVersion { version: &'a str },
    /// The version has been fetched.
    FetchedVersion { version: &'a str },
    /// Quick play has been fixed.
    FixedLegacyQuickPlay,
    /// Legacy proxy has been defined to fix legacy versions.
    FixedLegacyProxy { host: &'a str, port: u16 },
    /// Legacy merge sort has been fixed.
    FixedLegacyMergeSort,
    /// Legacy resolution arguments have been added.
    FixedLegacyResolution,
    /// Notification of a fix of authlib:2.1.28 has happened.
    FixedBrokenAuthlib,
    /// A quick play mode is requested by is not supported by this version, or the fix
    /// has been disabled. This is just a warning.
    WarnUnsupportedQuickPlay,
    /// A specific initial window resolution has been requested but it's not supported
    /// by the current version and the fix is disabled. This is just a warning.
    WarnUnsupportedResolution,
}

/// A handle for watching an installation.
pub trait Handler {
    /// Handle a single event.
    fn on_event(&mut self, event: Event);
}

// Mutable implementation.
impl<H: Handler + ?Sized> Handler for &mut H {
    #[inline]
    fn on_event(&mut self, event: Event) {
        (**self).on_event(event)
    }
}

impl Handler for () {
    fn on_event(&mut self, event: Event) {
        let _ = event;
    }
}

/// Internal adapter trait for using it like other handlers.
pub(crate) trait HandlerInto: Handler + Sized {
    
    #[inline]
    fn into_base(self) -> impl base::Handler {
        pub(crate) struct Adapter<H: Handler>(pub H);
        impl<H: Handler> base::Handler for Adapter<H> {
            fn on_event(&mut self, event: base::Event) {
                self.0.on_event(Event::Base(event));
            }
        }
        Adapter(self)
    }

    #[inline]
    fn into_download(self) -> impl download::Handler {
        self.into_base().into_download()
    }

}

impl<H: Handler> HandlerInto for H {}

/// The Mojang installer could not proceed to the installation of a version.
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum Error {
    /// Error from the base installer.
    #[error("base: {0}")]
    Base(#[source] base::Error),
    /// The LWJGL fix is enabled with a version that is not supported, maybe because
    /// it is too old (< 3.2.3) or because of your system not being supported.
    #[error("lwjgl fix not found: {version}")]
    LwjglFixNotFound {
        version: String,
    },
}

impl<T: Into<base::Error>> From<T> for Error {
    fn from(value: T) -> Self {
        Error::Base(value.into())
    }
}

/// Type alias for a result with the Mojang error type.
pub type Result<T> = std::result::Result<T, Error>;

/// The version to install.
#[derive(Debug, Clone)]
pub enum Version {
    /// Install the latest Mojang release.
    Release,
    /// Install the latest Mojang snapshot.
    Snapshot,
    /// Install this specific game version, if not a Mojang-provided version, it should
    /// be already installed in the versions directory.
    Name(String),
}

/// An impl so that we can give string-like objects to the builder.
impl<T: Into<String>> From<T> for Version {
    fn from(value: T) -> Self {
        Self::Name(value.into())
    }
}

/// This represent the optional Quick Play mode when launching the game. This is usually 
/// not supported on versions older than 1.20 (23w14a), however a fix exists for 
/// supporting multiplayer Quick Play on older versions, other modes are unsupported.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QuickPlay {
    /// Launch the game and follow instruction for Quick Play in the given path, relative
    /// to the working directory.
    Path {
        path: PathBuf,
    },
    /// Launch the game and directly join the world given its name.
    Singleplayer {
        name: String,
    },
    /// Launch the game and directly join the specified server address.
    Multiplayer {
        host: String,
        port: u16,
    },
    /// Launch the game and directly join the realm given its id.
    Realms {
        id: String,
    },
}

/// The different kind of patterns for filtering which versions are fetched or not.
#[derive(Debug, Clone)]
pub enum FetchExclude {
    /// Exclude all versions from being fetched from Mojang's manifest. It overrides
    /// all other excludes.
    All,
    /// Exclude a specific version name.
    Exact(String),
    /// Exclude a version's name that matches the given regex.
    Regex(Regex),
}

/// A handle to the Mojang versions manifest.
#[derive(Debug)]
pub struct Manifest {
    inner: Box<serde::MojangManifest>,
}

impl Manifest {

    /// Request the Mojang versions' manifest.
    pub fn request(mut handler: impl download::Handler) -> Result<Self> {
        return Self::request_dyn(&mut handler)
    }

    fn request_dyn(handler: &mut dyn download::Handler) -> Result<Self> {

        let mut entry = download::single_cached(VERSION_MANIFEST_URL)
            .set_keep_open()
            .download(handler)?;

        let reader = BufReader::new(entry.take_handle().unwrap());
        let mut deserializer = serde_json::Deserializer::from_reader(reader);
        let manifest = serde_path_to_error::deserialize::<_, Box<serde::MojangManifest>>(&mut deserializer)
            .map_err(|e| base::Error::new_json_file(e, entry.file()))?;

        Ok(Self { inner: manifest })

    }

    /// Iterator over all versions in the manifest.
    /// 
    /// This method currently returns an abstract iterator because the API is not 
    /// stabilized yet.
    pub fn iter(&self) -> impl Iterator<Item = ManifestVersion<'_>> + use<'_> {
        self.inner.versions.iter()
            .map(ManifestVersion)
    }

    /// Return the latest release version name.
    #[inline]
    pub fn latest_release_name(&self) -> &str {
        &self.inner.latest.release
    }

    /// Return the latest snapshot version name.
    #[inline]
    pub fn latest_snapshot_name(&self) -> &str {
        &self.inner.latest.snapshot
    }

    /// Find the index of a version given its name.
    pub fn find_index_of_name(&self, name: &str) -> Option<usize> {
        self.inner.versions.iter().position(|v| v.id == name)
    }

    /// Get a version from its index within the manifest.
    pub fn find_by_index(&self, index: usize) -> Option<ManifestVersion<'_>> {
        self.inner.versions.get(index).map(ManifestVersion)
    }

    /// Get a handle to a version information from its name.
    pub fn find_by_name(&self, name: &str) -> Option<ManifestVersion<'_>> {
        self.inner.versions.iter()
            .find(|v| v.id == name)
            .map(ManifestVersion)
    }

}

/// A handle to a version in the Mojang versions manifest.
#[derive(Debug)]
pub struct ManifestVersion<'a>(&'a serde::MojangManifestVersion);

impl<'a> ManifestVersion<'a> {

    /// The name of this version.
    /// 
    /// See [`base::LoadedVersion::name`] for more information on the naming.
    pub fn name(&self) -> &'a str {
        &self.0.id
    }

    /// The release channel of this version.
    pub fn channel(&self) -> VersionChannel {
        VersionChannel::from(self.0.r#type)
    }

    /// The last update time for this version.
    pub fn time(&self) -> &'a DateTime<FixedOffset> {
        &self.0.time
    }

    /// The release time for this version. 
    pub fn release_time(&self) -> &'a DateTime<FixedOffset> {
        &self.0.release_time
    }

    /// Return the download URL to this version metadata.
    pub fn url(&self) -> &'a str {
        &self.0.download.url
    }

    /// Return the expected size of this version metadata, if any.
    pub fn size(&self) -> Option<u32> {
        self.0.download.size
    }

    /// Return the expected SHA-1 of this version metadata, if any.
    pub fn sha1(&self) -> Option<&'a [u8; 20]> {
        self.0.download.sha1.as_deref()
    }

}

// ========================== //
// Following code is internal //
// ========================== //

/// Internal handler given to the base installer.
struct InternalHandler<'a> {
    /// Inner handler.
    inner: &'a mut dyn Handler,
    /// Back-reference to the installer to know its configuration.
    installer: &'a InstallerInner,
    /// If there is an error in the handler.
    error: Result<()>,
    /// If fetching is enabled, then this contains the manifest to use.
    manifest: Option<Manifest>,
    /// Id of the "leaf" version, the last version without inherited version.
    leaf_version: &'a mut String,
}

impl<'a> base::Handler for InternalHandler<'a> {
    
    fn on_event(&mut self, mut event: base::Event) {
        
        let ret = match event {
            base::Event::FilterFeatures { 
                ref mut features,
            } => self.filter_features(*features),
            base::Event::LoadedHierarchy {
                hierarchy,
            } => self.loaded_hierarchy(hierarchy),
            base::Event::LoadVersion { 
                version, 
                file,
            } => self.load_version(version, file),
            base::Event::NeedVersion { 
                version, 
                file, 
                ref mut retry,
            } => match self.need_version(version, file) {
                Ok(true) => {
                    **retry = true;
                    Ok(())
                }
                Ok(false) => Ok(()),
                Err(e) => Err(e),
            }
            base::Event::FilterLibraries { 
                ref mut libraries,
            } => self.filter_libraries(*libraries),
            _ => Ok(())
        };
        
        if let Err(e) = ret {
            self.error = Err(e);
            return;
        }

        self.inner.on_event(Event::Base(event));

    }

}

impl InternalHandler<'_> {

    fn filter_features(&mut self, features: &mut HashSet<String>) -> Result<()> {
        
        if self.installer.demo {
            features.insert("is_demo_user".to_string());
        }

        if self.installer.resolution.is_some() {
            features.insert("has_custom_resolution".to_string());
        }

        if let Some(quick_play) = &self.installer.quick_play {
            features.insert(match quick_play {
                QuickPlay::Path { .. } => "has_quick_plays_support",
                QuickPlay::Singleplayer { .. } => "is_quick_play_singleplayer",
                QuickPlay::Multiplayer { .. } => "is_quick_play_multiplayer",
                QuickPlay::Realms { .. } => "is_quick_play_realms",
            }.to_string());
        }

        Ok(())

    }

    fn loaded_hierarchy(&mut self, hierarchy: &[base::LoadedVersion]) -> Result<()> {
        *self.leaf_version = hierarchy.last().unwrap().name().to_string();
        Ok(())
    }

    fn load_version(&mut self, version: &str, file: &Path) -> Result<()> {

        // If any pattern matches, return Ok.
        for pattern in &self.installer.fetch_excludes {
            match pattern {
                FetchExclude::All => 
                    return Ok(()),
                FetchExclude::Exact(name) if name == version => 
                    return Ok(()),
                FetchExclude::Regex(regex) if regex.is_match(version) => 
                    return Ok(()),
                _ => (),
            }
        }

        // Only ensure that the manifest is loaded after checking fetch exclude.
        let manifest = match self.manifest {
            Some(ref manifest) => manifest,
            None => self.manifest.insert(Manifest::request((&mut *self.inner).into_download())?)
        };

        // Unwrap because we checked the manifest in the condition.
        let Some(version) = manifest.find_by_name(version) else {
            return Ok(());
        };

        if !check_file_advanced(file, version.size(), version.sha1(), true)? {
            
            fs::remove_file(file)
                .map_err(|e| base::Error::new_io_file(e, file))?;
            
            self.inner.on_event(Event::InvalidatedVersion { version: version.name() });
        
        }

        Ok(())

    }

    fn need_version(&mut self, version: &str, file: &Path) -> Result<bool> {

        let Some(manifest) = self.manifest.as_ref() else {
            return Ok(false);
        };
        
        let Some(version) = manifest.find_by_name(version) else {
            return Ok(false);
        };
        
        self.inner.on_event(Event::FetchVersion { version: version.name() });
        
        download::single(version.url(), file)
            .set_expected_size(version.size())
            .set_expected_sha1(version.sha1().copied())
            .download((&mut *self.inner).into_download())?;

        self.inner.on_event(Event::FetchedVersion { version: version.name() });

        Ok(true)

    }

    fn filter_libraries(&mut self, libraries: &mut Vec<LoadedLibrary>) -> Result<()> {
        
        if self.installer.fix_broken_authlib {
            self.apply_fix_broken_authlib(&mut *libraries)?;
        }

        if let Some(lwjgl_version) = self.installer.fix_lwjgl.as_deref() {
            self.apply_fix_lwjgl(&mut *libraries, lwjgl_version)?;
        }

        Ok(())

    }

    fn apply_fix_broken_authlib(&mut self, libraries: &mut Vec<LoadedLibrary>) -> Result<()> {

        // Unwrap because we don't exceed length limit for sure.
        let target_gav = Gav::new("com.mojang", "authlib", "2.1.28", None, None).unwrap();
        let pos = libraries.iter().position(|lib| lib.name == target_gav);
    
        if let Some(pos) = pos {

            libraries[pos].path = None;  // Ensure that the path is recomputed.
            libraries[pos].name = libraries[pos].name.with_version("2.2.30").unwrap();
            libraries[pos].download = Some(LibraryDownload {
                url: format!("{LIBRARIES_URL}{}", libraries[pos].name.url()),
                size: Some(87497),
                sha1: Some([0xd6, 0xe6, 0x77, 0x19, 0x9a, 0xa6, 0xb1, 0x9c, 0x4a, 0x9a, 0x2e, 0x72, 0x50, 0x34, 0x14, 0x9e, 0xb3, 0xe7, 0x46, 0xf8]),
            });

            self.inner.on_event(Event::FixedBrokenAuthlib);

        }

        Ok(())
    
    }
    
    fn apply_fix_lwjgl(&mut self, libraries: &mut Vec<LoadedLibrary>, version: &str) -> Result<()> {
    
        if version != "3.2.3" && !version.starts_with("3.3.") {
            return Err(Error::LwjglFixNotFound { 
                version: version.to_string(),
            });
        }
    
        let classifier = match (env::consts::OS, env::consts::ARCH) {
            ("windows", "x86") => "natives-windows-x86",
            ("windows", "x86_64") => "natives-windows",
            ("windows", "aarch64") if version != "3.2.3" => "natives-windows-arm64",
            ("linux", "x86" | "x86_64") => "natives-linux",
            ("linux", "arm") => "natives-linux-arm32",
            ("linux", "aarch64") => "natives-linux-arm64",
            ("macos", "x86_64") => "natives-macos",
            ("macos", "aarch64") if version != "3.2.3" => "natives-macos-arm64",
            _ => return Err(Error::LwjglFixNotFound { 
                version: version.to_string(),
            })
        };
    
        // Contains to-be-expected unique LWJGL libraries, with the classifier.
        let mut lwjgl_libs = Vec::new();
    
        // Start by not retaining libraries with classifiers (natives).
        libraries.retain_mut(|lib| {
            if let ("org.lwjgl", "jar") = (lib.name.group(), lib.name.extension()) {
                if lib.name.classifier().is_none() {
                    if let Some(new_name) = lib.name.with_version(version) 
                    && let Some(new_classifier_name) = new_name.with_classifier(Some(classifier)) {
                        lib.path = None;
                        lib.download = None;  // Will be updated afterward.
                        lib.name = new_name;
                        lwjgl_libs.push(new_classifier_name);
                        true
                    } else {
                        // Do not retain if we got an error create the new version, it 
                        // might be too long and we don't want to unwrap.
                        false
                    }
                } else {
                    // Libraries with classifiers are not retained.
                    false
                }
            } else {
                true
            }
        });
    
        // Now we add the classifiers for each LWJGL lib.
        libraries.extend(lwjgl_libs.into_iter().map(|gav| {
            LoadedLibrary {
                name: gav,
                path: None,
                download: None, // Will be set in the loop just after.
                natives: false,
            }
        }));
    
        // Finally we update the download source.
        for lib in libraries {
            if let ("org.lwjgl", "jar") = (lib.name.group(), lib.name.extension()) {
                let url = format!("https://repo1.maven.org/maven2/{}", lib.name.url());
                lib.download = Some(LibraryDownload { url, size: None, sha1: None });
            }
        }

        Ok(())
    
    }

}