Now vanilla (or with Fabric loader) profile creation is implemented.

This commit is contained in:
wei
2026-07-31 22:14:34 +08:00
parent 8d865c92a9
commit b5df1ec1ca
7 changed files with 668 additions and 127 deletions
+16 -7
View File
@@ -13,6 +13,9 @@ FABRIC_META_LOADER_VERSION_ENDPOINT_V2 = "https://meta.fabricmc.net/v2/versions/
FABRIC_META_LOADER_MANIFEST_ENDPOINT_V2 = "https://meta.fabricmc.net/v2/versions/loader/{}/{}/profile/json"
FABRIC_MAVEN_URL = "https://maven.fabricmc.net/"
def get_full_version_string(version: str, loader_version: str) -> str:
return f"fabric-loader-{loader_version}-{version}"
def fetch_fabric_version_list(version_manifest_url: str=FABRIC_META_VERSION_ENDPOINT_V2, timeout: int=5) -> list[dict]:
try:
r = requests.get(version_manifest_url, timeout=timeout)
@@ -66,6 +69,7 @@ def get_version_support_loader_list(target_version: str, loader_version_url: str
-> list[dict]:
try:
r = requests.get(loader_version_url.format(target_version), timeout=timeout)
r.raise_for_status()
except requests.exceptions.Timeout:
raise VersionManifestFetchException(
"Timeout while fetch from {}".format(loader_version_url)
@@ -75,9 +79,15 @@ def get_version_support_loader_list(target_version: str, loader_version_url: str
"Connection error while fetch from {}: {}".format(loader_version_url, e)
)
except requests.exceptions.HTTPError as e:
raise VersionManifestFetchException(
"HTTP error while fetch from {}: HTTP Status: {}".format(loader_version_url, e.response.status_code)
)
if e.response.status_code == 404 or e.response.status_code == 400:
raise VersionManifestFetchException(
"HTTP error while fetch from {}: {}".format(loader_version_url, e),
user_message="Target version {} no fabric version found".format(target_version)
)
else:
raise VersionManifestFetchException(
"HTTP error while fetch from {}: HTTP Status: {}".format(loader_version_url, e.response.status_code)
)
except Exception as e:
raise VersionManifestFetchException(
"Unknown error while fetch from {}: {}".format(loader_version_url, e)
@@ -102,7 +112,8 @@ def get_version_support_loader_list(target_version: str, loader_version_url: str
if not loaders:
raise VersionManifestException(
"No available loader found in {}".format(loader_version_url)
"No available loader found in {}".format(loader_version_url),
user_message="Target version {} no fabric version found".format(target_version)
)
return loaders
@@ -121,6 +132,7 @@ def get_loader_manifest_from_loader_data(target_version: str, loader_data: dict,
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
except requests.exceptions.Timeout:
raise VersionManifestFetchException(
"Timeout while fetch from {}".format(url)
@@ -150,6 +162,3 @@ def get_loader_manifest_from_loader_data(target_version: str, loader_data: dict,
"Unknown error while fetch from {}: {}".format(url, e)
)
+43 -33
View File
@@ -400,6 +400,48 @@ def get_core_file_download_url_and_hash(version_manifest: dict, is_server: bool=
return download_url, sha1
def save_version_manifest(manifest: dict, dest: Path, sha1: str | None=None) -> dict:
"""
Save version manifest with hash verify
:param manifest:
:param dest:
:param sha1:
:return:
data: dict
"""
file = FileObject(
path=dest,
sha1=sha1,
)
dest.parent.mkdir(parents=True, exist_ok=True)
try:
method = "wb" if isinstance(manifest, bytes) else "w"
with dest.open(method) as f:
if isinstance(manifest, bytes):
f.write(manifest)
elif isinstance(manifest, dict):
json.dump(manifest, f)
if sha1 is not None and not file.verify_sha1(sha1):
raise HashMismatchException(
"sha1",
file.sha1,
sha1,
file.posix_path
)
if isinstance(manifest, bytes):
# Convert the data back to dict
return json.loads(manifest.decode("utf-8"))
else:
return manifest
except Exception as e:
logger.exception(e)
raise VersionManifestSaveException(
"Error saving version manifest:\n{}".format(e)
)
def fetch_and_save_version_manifest(dest: Path, target_version: str=None, root_manifest=None) -> dict | None:
"""
@@ -423,39 +465,7 @@ def fetch_and_save_version_manifest(dest: Path, target_version: str=None, root_m
data = root_manifest
sha1 = None
file = FileObject(
path=dest,
sha1=sha1,
)
dest.parent.mkdir(parents=True, exist_ok=True)
try:
method = "wb" if isinstance(data, bytes) else "w"
with dest.open(method) as f:
if isinstance(data, bytes):
f.write(data)
elif isinstance(data, dict):
json.dump(data, f)
if sha1 is not None and not file.verify_sha1(sha1):
raise HashMismatchException(
"sha1",
file.sha1,
sha1,
file.posix_path
)
if isinstance(data, bytes):
# Convert the data back to dict
return json.loads(data.decode("utf-8"))
else:
return data
except Exception as e:
logger.exception(e)
raise VersionManifestSaveException(
"Error saving version manifest:\n{}".format(e)
)
return save_version_manifest(data, dest, sha1)
def download_core_file(manifest: dict, destination: Path, is_server=False, raise_for_null_hash=False,
progress_callback: Callable[[str, float], None]=None) -> FileObject: