Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 67b8c01

Browse files
committed
feat(unsupported warnings): begin work on downgrade warnings
BREAKING CHANGE: The format of the version file has been updated, and it will no longer work with older versions of Vanilla Installer. Fixes #80
1 parent f87c638 commit 67b8c01

File tree

5 files changed

+165
-40
lines changed

5 files changed

+165
-40
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description = "An installer of Fabulously Optimized for the vanilla launcher."
55
authors = [
66
"osfanbuff63 <[email protected]>",
77
8-
"Kichura <blobfox.coffee/@Kichura>"
8+
"Kichura <Kichura@blobfox.coffee>"
99
]
1010
documentation = "https://fabulously-optimized.gitbook.io/vanilla-installer"
1111
homepage = "https://fabulously-optimized.gitbook.io/vanilla-installer"
Lines changed: 68 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,69 @@
11
{
2-
"1.19.4": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.19.4/pack.toml",
3-
"1.18.2": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.18.2/pack.toml",
4-
"1.17.1": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.17.1/pack.toml",
5-
"1.16.5": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.16.5/pack.toml"
6-
}
2+
"format_version": 2,
3+
"versions": {
4+
"1.19.4": {
5+
"packwiz": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.19.4/pack.toml",
6+
"major_version": 4,
7+
"enabled": true
8+
},
9+
"1.19.3": {
10+
"major_version": 4,
11+
"enabled": false
12+
},
13+
"1.19.2": {
14+
"major_version": 4,
15+
"enabled": false
16+
},
17+
"1.19.1": {
18+
"major_version": 4,
19+
"enabled": false
20+
},
21+
"1.19": {
22+
"major_version": 4,
23+
"enabled": false
24+
},
25+
"1.18.2": {
26+
"packwiz": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.18.2/pack.toml",
27+
"major_version": 3,
28+
"enabled": true
29+
},
30+
"1.18.1": {
31+
"major_version": 3,
32+
"enabled": false
33+
},
34+
"1.18": {
35+
"major_version": 3,
36+
"enabled": false
37+
},
38+
"1.17.1": {
39+
"packwiz": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.17.1/pack.toml",
40+
"major_version": 2,
41+
"enabled": true
42+
},
43+
"1.17": {
44+
"major_version": 2,
45+
"enabled": false
46+
},
47+
"1.16.5": {
48+
"packwiz": "https://raw.githubusercontent.com/Fabulously-Optimized/fabulously-optimized/main/Packwiz/1.16.5/pack.toml",
49+
"major_version": 1,
50+
"enabled": true
51+
},
52+
"1.16.4": {
53+
"major_version": 1,
54+
"enabled": false
55+
},
56+
"1.16.3": {
57+
"major_version": 1,
58+
"enabled": false
59+
},
60+
"1.16.2": {
61+
"major_version": 1,
62+
"enabled": false
63+
},
64+
"1.16.1": {
65+
"major_version": 1,
66+
"enabled": false
67+
}
68+
}
69+
}

vanilla_installer/gui.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ def setupUi(self, MainWindow: QMainWindow) -> None:
203203
Ui_MainWindow.getAsset("settings.svg"), self.settingsButton
204204
)
205205
self.settingsButtonIcon.setGeometry(70, 0, 24, 24)
206-
207206
self.windowIcon = Ui_MainWindow.getAsset("icon.png")
208207
MainWindow.setWindowIcon(QIcon(self.windowIcon))
209208

vanilla_installer/log.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
import logging
88
import logging.handlers # pylance moment
9-
from pathlib import Path
109
import sys
10+
from pathlib import Path
1111

1212

1313
class LoggerWriter:
@@ -16,9 +16,9 @@ def __init__(self, logfct):
1616
self.buf = []
1717

1818
def write(self, msg):
19-
if msg.endswith('\n'):
20-
self.buf.append(msg.removesuffix('\n'))
21-
self.logfct(''.join(self.buf))
19+
if msg.endswith("\n"):
20+
self.buf.append(msg.removesuffix("\n"))
21+
self.logfct("".join(self.buf))
2222
self.buf = []
2323
else:
2424
self.buf.append(msg)
@@ -35,11 +35,11 @@ def flush(self):
3535
with logfile_path as file:
3636
open(file, "x", encoding="utf-8").write("")
3737
handler = logging.handlers.RotatingFileHandler(
38-
filename=logfile_path,
39-
encoding="utf-8",
40-
maxBytes=32 * 1024 * 1024, # 32 MiB
41-
backupCount=5, # Rotate through 5 files
42-
)
38+
filename=logfile_path,
39+
encoding="utf-8",
40+
maxBytes=32 * 1024 * 1024, # 32 MiB
41+
backupCount=5, # Rotate through 5 files
42+
)
4343

4444
dt_fmt = "%Y-%m-%d %H:%M:%S"
4545
formatter = logging.Formatter(

vanilla_installer/main.py

Lines changed: 87 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import subprocess
1414
import zipfile
1515
from pathlib import Path
16-
from typing import Optional
16+
from typing import Optional, Union
1717

1818
import click
1919
import minecraft_launcher_lib as mll
@@ -28,6 +28,12 @@
2828
FOLDER_LOC = ""
2929

3030

31+
class UnsupportedFormatVersion(Exception):
32+
"""The format version is not supported by this program version."""
33+
34+
pass
35+
36+
3137
def set_dir(path: str = mll.utils.get_minecraft_directory()) -> str | None:
3238
"""
3339
Sets the Minecraft game directory.
@@ -376,34 +382,79 @@ def create_profile(mc_dir: str, version_id: str) -> None:
376382
launcher_profiles_path.write_text(profiles_json)
377383

378384

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.
380393
"""
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.
382408
"""
409+
SUPPORTED_FORMAT_VERSION = 2
383410

384-
return_value = dict()
385411
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...")
386424
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)
402439

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
407458

408459

409460
def convert_version(input_mcver: str) -> str:
@@ -414,7 +465,7 @@ def convert_version(input_mcver: str) -> str:
414465
input_mcver (str): The Minecraft version to find.
415466
416467
Returns:
417-
str: The converted version as a direct JSDelivr URL.
468+
str: The converted version as a URL.
418469
"""
419470
versions = get_pack_mc_versions()
420471
return_value = versions.get(input_mcver)
@@ -424,6 +475,18 @@ def convert_version(input_mcver: str) -> str:
424475
return return_value
425476

426477

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+
427490
def run(
428491
mc_dir: str = mll.utils.get_minecraft_directory(),
429492
version: Optional[str] = None,

0 commit comments

Comments
 (0)