13
13
import subprocess
14
14
import zipfile
15
15
from pathlib import Path
16
- from typing import Optional
16
+ from typing import Optional , Union
17
17
18
18
import click
19
19
import minecraft_launcher_lib as mll
28
28
FOLDER_LOC = ""
29
29
30
30
31
+ class UnsupportedFormatVersion (Exception ):
32
+ """The format version is not supported by this program version."""
33
+
34
+ pass
35
+
36
+
31
37
def set_dir (path : str = mll .utils .get_minecraft_directory ()) -> str | None :
32
38
"""
33
39
Sets the Minecraft game directory.
@@ -376,34 +382,79 @@ def create_profile(mc_dir: str, version_id: str) -> None:
376
382
launcher_profiles_path .write_text (profiles_json )
377
383
378
384
379
- def get_pack_mc_versions () -> dict :
385
+ def log_installed_version (
386
+ version : Union [str , int , float ], install_dir : Union [str , bytes , os .PathLike ]
387
+ ) -> None :
388
+ """Log the version of Minecraft that FO has been installed for. Used to find out later
389
+
390
+ Args:
391
+ version (Union[str, int, float]): The version to log.
392
+ install_dir (Union[str, bytes, os.PathLike]): The directory that FO was installed to.
380
393
"""
381
- Gets a list of all the versions FO currently supports.
394
+ dir_path = Path (install_dir ).resolve () / ".fabulously-optimized"
395
+ dir_path .mkdir (exist_ok = True )
396
+ file_path = dir_path / "mc_version.txt"
397
+ if file_path .exists () is False :
398
+ file_path .touch ()
399
+ with file_path .open ("w" ) as file :
400
+ file .write (str (version ))
401
+
402
+
403
+ def read_versions () -> dict :
404
+ """Reads the versions.json file, either over the Internet, or locally.
405
+
406
+ Returns:
407
+ dict: The JSON file, formatted as a dictionary.
382
408
"""
409
+ SUPPORTED_FORMAT_VERSION = 2
383
410
384
- return_value = dict ()
385
411
try :
412
+ response = requests .get (
413
+ "https://raw.githubusercontent.com/Fabulously-Optimized/vanilla-installer/main/vanilla_installer/assets/versions.json"
414
+ ).json ()
415
+ format_version = response ["format_version" ]
416
+ if response ["format_version" ] != SUPPORTED_FORMAT_VERSION :
417
+ raise UnsupportedFormatVersion (
418
+ f"Format version { format_version } is not supported by this version."
419
+ )
420
+ except requests .exceptions .RequestException or response .status_code != "200" :
421
+ # This should never happen unless a) there's no internet connection, b) the file was deleted or is missing in a development case.
422
+ # In this case, fall back to a local file.
423
+ logger .warning ("GitHub failed, falling back to local..." )
386
424
try :
387
- response = requests .get (
388
- "https://raw.githubusercontent.com/Fabulously-Optimized/vanilla-installer/main/vanilla_installer/assets/versions.json"
389
- ).json ()
390
- except requests .exceptions .RequestException or response .status_code != "200" :
391
- # This should never happen unless a) there's no internet connection, b) the file was deleted or is missing in a development case.
392
- # In this case, fall back to a local file since in the latter you'll likely have the whole repo cloned.
393
- # For this to work, you need to be in the root directory of the repository running this, otherwise the files will not be found.
394
- logger .warning ("GitHub failed, falling back to local..." )
395
- try :
396
- local_path = (
397
- Path ("vanilla_installer/assets" ).resolve () / "versions.json"
398
- )
399
- except :
400
- local_path = Path ("assets" ).resolve () / "versions.json"
401
- response = json .loads (local_path .read_bytes ())
425
+ local_path = Path ("vanilla_installer/assets" ).resolve () / "versions.json"
426
+ except :
427
+ local_path = Path ("assets" ).resolve () / "versions.json"
428
+ response = json .loads (local_path .read_bytes ())
429
+ except UnsupportedFormatVersion :
430
+ logger .exception (
431
+ "Format version was not supported - using local file. Update Vanilla Installer to fix this."
432
+ )
433
+ try :
434
+ local_path = Path ("vanilla_installer/assets" ).resolve () / "versions.json"
435
+ except :
436
+ local_path = Path ("assets" ).resolve () / "versions.json"
437
+ response = json .loads (local_path .read_bytes ())
438
+ return dict (response )
402
439
403
- return_value = dict (response )
404
- return return_value
405
- except requests .exceptions .RequestException as e :
406
- logger .exception (f"Couldn't get minecraft versions: { e } " )
440
+
441
+ def get_pack_mc_versions () -> dict :
442
+ """
443
+ Gets a list of all the versions FO currently supports.
444
+ """
445
+ return_value = dict ()
446
+ raw_dict = read_versions ()
447
+ for version in raw_dict ["versions" ]:
448
+ raw_version = raw_dict ["versions" ][version ]
449
+ if raw_version ["enabled" ] is True or raw_version ["enabled" ] == "true" :
450
+ return_value [version ] = raw_version
451
+ for key in return_value .keys ():
452
+ try :
453
+ if key ["packwiz" ] != "" :
454
+ pass
455
+ except KeyError :
456
+ return_value .pop (key )
457
+ return return_value
407
458
408
459
409
460
def convert_version (input_mcver : str ) -> str :
@@ -414,7 +465,7 @@ def convert_version(input_mcver: str) -> str:
414
465
input_mcver (str): The Minecraft version to find.
415
466
416
467
Returns:
417
- str: The converted version as a direct JSDelivr URL.
468
+ str: The converted version as a URL.
418
469
"""
419
470
versions = get_pack_mc_versions ()
420
471
return_value = versions .get (input_mcver )
@@ -424,6 +475,18 @@ def convert_version(input_mcver: str) -> str:
424
475
return return_value
425
476
426
477
478
+ def downgrade_check (version : Union [str , int , float ]) -> bool :
479
+ """Checks whether the given version is a downgrade from the one currently installed.
480
+
481
+ Args:
482
+ version (Union[str, int, float]): _description_
483
+
484
+ Returns:
485
+ bool: Whether this is a downgrade.
486
+ """
487
+ version_dict = read_versions ()
488
+
489
+
427
490
def run (
428
491
mc_dir : str = mll .utils .get_minecraft_directory (),
429
492
version : Optional [str ] = None ,
0 commit comments