Skip to content

Commit 587f866

Browse files
committed
feat: implement download classifiers and download libraries funcs
Signed-off-by: Kaeeraa <[email protected]>
1 parent 8b65915 commit 587f866

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/lib/launcher/core/download.ts

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,29 @@ import {
44
type Artifact,
55
type Asset,
66
type AssetIndex,
7+
type Classifiers,
8+
type Library,
79
type LoggingConfig,
810
type Manifest,
911
type VersionMetaModern,
1012
} from "@/lib/schemas/minecrafts-schemas";
1113
import { download } from "@tauri-apps/plugin-upload";
1214
import Value from "typebox/value";
13-
import { TreeAssetIndexes, TreeAssetObjects, TreeLogging } from "@/constants/application";
15+
import {
16+
TreeAssetIndexes,
17+
TreeAssetObjects,
18+
TreeLibraries,
19+
TreeLogging,
20+
TreeNatives,
21+
} from "@/constants/application";
1422
import { readTextFile } from "@tauri-apps/plugin-fs";
1523
import { extractError } from "@/lib/helpers/extract-error";
1624
import { log } from "@/lib/handlers/log";
1725
import { validateFileSize } from "@/lib/helpers/validate-file-size";
1826
import { checksum } from "@/lib/helpers/checksum";
27+
import { evaluateRules } from "@/lib/helpers/parse-rules";
28+
import { transformPlatform } from "@/lib/helpers/transform-platform";
29+
import { unzipFile } from "@/lib/helpers/unzip-file";
1930

2031

2132
async function fetchVersionManifest(): Promise<Manifest> {
@@ -101,3 +112,40 @@ async function downloadAssets(path: string) {
101112
log.error(`Downloading asset (${path}) failed: ${error.reason}`);
102113
}
103114
}
115+
116+
async function downloadClassifiers(classifiers: Classifiers, version: string, extract: boolean) {
117+
const platform = transformPlatform();
118+
const key = `natives-${platform}` as keyof typeof classifiers;
119+
const native = classifiers[key];
120+
121+
if (!native) {
122+
throw new Error(`Expected native (${key}) not found`);
123+
}
124+
125+
await downloadArtifact(native, `${TreeLibraries}`);
126+
127+
if (extract) {
128+
const path = `${TreeLibraries}/${native.path}`;
129+
130+
await unzipFile(path, `${TreeNatives}/${version}`);
131+
}
132+
}
133+
134+
async function downloadLibraries(libraries: Library[], version: string) {
135+
for (const library of libraries) {
136+
const rules = library.rules;
137+
138+
if (rules && !await evaluateRules(rules)) {
139+
continue;
140+
}
141+
await downloadArtifact(library.downloads.artifact, TreeLibraries);
142+
143+
if (library.downloads.classifiers) {
144+
await downloadClassifiers(
145+
library.downloads.classifiers,
146+
version,
147+
library.extract != undefined,
148+
);
149+
}
150+
}
151+
}

src/lib/schemas/minecrafts-schemas.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,25 @@ export const ArtifactSchema = Type.Object({
6464
});
6565
export type Artifact = Static<typeof ArtifactSchema>;
6666

67+
export const ClassifiersSchema = Type.Object({
68+
"natives-windows": Type.Optional(ArtifactSchema),
69+
"natives-linux" : Type.Optional(ArtifactSchema),
70+
"natives-osx" : Type.Optional(ArtifactSchema),
71+
});
72+
export type Classifiers = Static<typeof ClassifiersSchema>;
73+
6774
export const DownloadSchema = Type.Object({
6875
"artifact" : ArtifactSchema,
69-
"classifiers": Type.Optional(
70-
Type.Record(Type.String(), ArtifactSchema),
71-
),
76+
"classifiers": Type.Optional(ClassifiersSchema),
7277
});
7378

7479
export const LibrarySchema = Type.Object({
7580
"name" : Type.String(),
7681
"downloads": DownloadSchema,
7782
"rules" : Type.Optional(Type.Array(RuleSchema)),
83+
"extract" : Type.Optional(Type.Object({
84+
"exclude": Type.Any(),
85+
})),
7886
});
7987
export type Library = Static<typeof LibrarySchema>;
8088
export const LibraryValidator = Compile(LibrarySchema);

0 commit comments

Comments
 (0)