Skip to content

Commit e2f2dd3

Browse files
committed
refactor the way mod directories and extensions are discovered
1 parent d55f02e commit e2f2dd3

File tree

2 files changed

+64
-42
lines changed

2 files changed

+64
-42
lines changed

runtime/src/resources.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ export async function loadJSON<T = unknown>(
110110

111111
let { resolvedPath, requestedAsset } = resolvePathAdvanced(path, options);
112112
let data: unknown = null!;
113-
let shouldFetchRealData = false;
113+
let shouldFetchRealData = true;
114114

115115
if ((options.allowGenerators ?? true) && requestedAsset != null) {
116116
let generators = jsonGenerators.forPath(requestedAsset);
117117
if (generators.length > 0) {
118118
let ctx: types.JSONGeneratorContext = { resolvedPath, requestedAsset, options };
119119
data = await runResourceGenerator('JSON file', path, generators, ctx);
120-
shouldFetchRealData = true;
120+
shouldFetchRealData = false;
121121
}
122122
}
123123

124-
if (!shouldFetchRealData) {
124+
if (shouldFetchRealData) {
125125
data = await resourcesPlain.loadJSON(wrapPathIntoURL(resolvedPath).href);
126126
}
127127

@@ -144,18 +144,18 @@ export async function loadImage(
144144

145145
let { resolvedPath, requestedAsset } = resolvePathAdvanced(path, options);
146146
let data: HTMLImageElement | HTMLCanvasElement = null!;
147-
let shouldFetchRealData = false;
147+
let shouldFetchRealData = true;
148148

149149
if ((options.allowGenerators ?? true) && requestedAsset != null) {
150150
let generators = imageGenerators.forPath(requestedAsset);
151151
if (generators.length > 0) {
152152
let ctx: types.ImageGeneratorContext = { resolvedPath, requestedAsset, options };
153153
data = await runResourceGenerator('image', path, generators, ctx);
154-
shouldFetchRealData = true;
154+
shouldFetchRealData = false;
155155
}
156156
}
157157

158-
if (!shouldFetchRealData) {
158+
if (shouldFetchRealData) {
159159
data = await resourcesPlain.loadImage(wrapPathIntoURL(resolvedPath).href);
160160
}
161161

src/modloader.ts

Lines changed: 58 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,6 @@ export async function boot(): Promise<void> {
5252
}
5353
console.log(`${runtimeMod.id} ${runtimeMod.version}`);
5454

55-
let installedMods = new Map<ModID, Mod>();
56-
installedMods.set(runtimeMod.id, runtimeMod);
57-
for (let dir of config.modsDirs) {
58-
let count = await loadAllModMetadataInDir(dir, installedMods);
59-
console.log(`found ${count} mods in '${dir}'`);
60-
}
61-
installedMods = dependencyResolver.sortModsInLoadOrder(runtimeMod, installedMods);
62-
63-
let loadedMods = new Map<ModID, Mod>();
64-
let loadedModsSetupPromises: Array<Promise<void>> = [];
65-
6655
let virtualPackages = new Map<ModID, semver.SemVer>()
6756
.set('crosscode', gameVersion)
6857
.set('ccloader', modloaderMetadata.version);
@@ -72,19 +61,7 @@ export async function boot(): Promise<void> {
7261

7362
let installedExtensions = new Map<ModID, semver.SemVer>();
7463
let enabledExtensions = new Map<ModID, semver.SemVer>();
75-
try {
76-
for (let extID of await files.getInstalledExtensions(config)) {
77-
let extVersion = gameVersion;
78-
if (extID.startsWith('-')) {
79-
installedExtensions.set(extID.slice(1), extVersion);
80-
} else {
81-
installedExtensions.set(extID, extVersion);
82-
enabledExtensions.set(extID, extVersion);
83-
}
84-
}
85-
} catch (err) {
86-
console.error('Failed to load the extensions list:', err);
87-
}
64+
await findAllExtensions(config, installedExtensions, enabledExtensions, gameVersion);
8865

8966
console.log(
9067
`found ${installedExtensions.size} extensions: ${Array.from(installedExtensions.keys())
@@ -96,6 +73,14 @@ export async function boot(): Promise<void> {
9673
.join(', ')}`,
9774
);
9875

76+
let installedMods = new Map<ModID, Mod>();
77+
installedMods.set(runtimeMod.id, runtimeMod);
78+
await loadAllModMetadata(config, installedMods);
79+
80+
installedMods = dependencyResolver.sortModsInLoadOrder(runtimeMod, installedMods);
81+
let loadedMods = new Map<ModID, Mod>();
82+
let loadedModsSetupPromises: Array<Promise<void>> = [];
83+
9984
for (let [modID, mod] of installedMods) {
10085
if (mod !== runtimeMod && !modDataStorage.isModEnabled(modID)) {
10186
continue;
@@ -192,14 +177,26 @@ async function loadModloaderMetadata(): Promise<{ version: semver.SemVer }> {
192177
return { version: new semver.SemVer(data.version) };
193178
}
194179

195-
async function loadAllModMetadataInDir(modsDir: string, installedMods: ModsMap): Promise<number> {
196-
let count = 0;
180+
async function loadAllModMetadata(config: configM.Config, installedMods: ModsMap): Promise<void> {
181+
let allModsList: Array<{ parentDir: string; dir: string }> = [];
182+
for (let dir of config.modsDirs) {
183+
let subdirsList: string[];
184+
try {
185+
subdirsList = await files.getModDirectoriesIn(dir);
186+
} catch (err) {
187+
console.warn(`Failed to load the list of mods in '${dir}':`, err);
188+
continue;
189+
}
190+
for (let subdir of subdirsList) {
191+
allModsList.push({ parentDir: dir, dir: subdir });
192+
}
193+
}
194+
195+
let modsCountPerDir = new Map<string, number>();
197196
await Promise.all(
198-
(
199-
await files.getModDirectoriesIn(modsDir)
200-
).map(async (fullPath) => {
197+
allModsList.map(async ({ parentDir, dir }) => {
201198
try {
202-
let mod = await loadModMetadata(fullPath);
199+
let mod = await loadModMetadata(dir);
203200
if (mod == null) return;
204201

205202
let modWithSameId = installedMods.get(mod.id);
@@ -210,16 +207,41 @@ async function loadAllModMetadataInDir(modsDir: string, installedMods: ModsMap):
210207
}
211208

212209
installedMods.set(mod.id, mod);
213-
count++;
210+
modsCountPerDir.set(parentDir, (modsCountPerDir.get(parentDir) ?? 0) + 1);
214211
} catch (err) {
215-
console.error(
216-
`An error occured while loading the metadata of a mod in '${fullPath}':`,
217-
err,
218-
);
212+
console.error(`An error occured while loading the metadata of a mod in '${dir}':`, err);
219213
}
220214
}),
221215
);
222-
return count;
216+
217+
for (let [dir, count] of modsCountPerDir) {
218+
console.log(`found ${count} mods in '${dir}'`);
219+
}
220+
}
221+
222+
async function findAllExtensions(
223+
config: configM.Config,
224+
installedExtensions: Map<string, semver.SemVer>,
225+
enabledExtensions: Map<string, semver.SemVer>,
226+
defaultExtVersion: semver.SemVer,
227+
): Promise<void> {
228+
let extList: string[];
229+
try {
230+
extList = await files.getInstalledExtensions(config);
231+
} catch (err) {
232+
console.warn('Failed to load the extensions list:', err);
233+
return;
234+
}
235+
for (let extID of extList) {
236+
let extVersion = new semver.SemVer(defaultExtVersion, defaultExtVersion.options);
237+
if (extID.startsWith('-')) {
238+
// Extension is disabled
239+
extID = extID.slice(1);
240+
} else {
241+
enabledExtensions.set(extID, extVersion);
242+
}
243+
installedExtensions.set(extID, extVersion);
244+
}
223245
}
224246

225247
let manifestValidator = new manifestM.Validator();

0 commit comments

Comments
 (0)