Skip to content

Commit

Permalink
refactor: make curseforge log more clear
Browse files Browse the repository at this point in the history
  • Loading branch information
ci010 committed Sep 1, 2020
1 parent 5b7c7a9 commit 6c9422d
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/main/service/CurseForgeService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ export default class CurseForgeService extends Service {

private searchProjectCache: Record<string, AddonInfo[]> = {};

private async fetchOrGetFromCache<K extends string | number, V>(cache: Record<K, V>, key: K, query: () => Promise<V>) {
private async fetchOrGetFromCache<K extends string | number, V>(cacheName: string, cache: Record<K, V>, key: K, query: () => Promise<V>) {
let timestamp = await getAddonDatabaseTimestamp({ userAgent: this.userAgent });
if (!cache[key] || new Date(timestamp) > new Date(this.projectTimestamp)) {
let value = await query();
this.projectTimestamp = timestamp;
cache[key] = value;
this.log(`Cache missed for "${key}"`);
this.log(`Cache missed for ${key} in ${cacheName}`);
return value;
}
this.log(`Cache hit for "${key}"`);
this.log(`Cache hit for ${key} in ${cacheName}`);
return cache[key];
}

Expand All @@ -83,22 +83,22 @@ export default class CurseForgeService extends Service {

async fetchProject(projectId: number) {
this.log(`Fetch project: ${projectId}`);
return this.fetchOrGetFromCache(this.projectCache, projectId, () => getAddonInfo(projectId, { userAgent: this.userAgent }));
return this.fetchOrGetFromCache('project', this.projectCache, projectId, () => getAddonInfo(projectId, { userAgent: this.userAgent }));
}

fetchProjectDescription(projectId: number) {
this.log(`Fetch project description: ${projectId}`);
return this.fetchOrGetFromCache(this.projectDescriptionCache, projectId, () => getAddonDescription(projectId, { userAgent: this.userAgent }));
return this.fetchOrGetFromCache('project description', this.projectDescriptionCache, projectId, () => getAddonDescription(projectId, { userAgent: this.userAgent }));
}

fetchProjectFiles(projectId: number) {
this.log(`Fetch project files: ${projectId}`);
return this.fetchOrGetFromCache(this.projectFilesCache, projectId, () => getAddonFiles(projectId, { userAgent: this.userAgent }).then(files => files.sort((a, b) => new Date(b.fileDate) - new Date(a.fileDate))));
return this.fetchOrGetFromCache('project files', this.projectFilesCache, projectId, () => getAddonFiles(projectId, { userAgent: this.userAgent }).then(files => files.sort((a, b) => new Date(b.fileDate) - new Date(a.fileDate))));
}

async searchProjects(searchOptions: SearchOptions) {
this.log(`Search project: section=${searchOptions.sectionId}, category=${searchOptions.categoryId}, keyword=${searchOptions.searchFilter}`);
const addons = await this.fetchOrGetFromCache(this.searchProjectCache, JSON.stringify(searchOptions), () => searchAddons(searchOptions, { userAgent: this.userAgent }));
const addons = await this.fetchOrGetFromCache('project search', this.searchProjectCache, JSON.stringify(searchOptions), () => searchAddons(searchOptions, { userAgent: this.userAgent }));
for (let addon of addons) {
this.projectCache[addon.id] = addon;
}
Expand All @@ -118,14 +118,16 @@ export default class CurseForgeService extends Service {
worlds: 'save',
modpacks: 'curseforge-modpack',
};
this.log(`Install file ${file.displayName}(${file.downloadUrl}) in type ${type}`);
let resource = this.resourceService.getResource({ url: [file.downloadUrl, `curseforge://${file.projectId}/${file.id}`] });
const urls = [file.downloadUrl, `curseforge://${projectId}/${file.id}`];
this.log(`Try install file ${file.displayName}(${file.downloadUrl}) in type ${type}`);
const resource = this.resourceService.getResource({ url: urls });
if (resource !== UNKNOWN_RESOURCE) {
this.log(`The curseforge file ${file.displayName}(${file.downloadUrl}) existed in cache!`);
return resource.path;
}
let destination = join(this.app.temporaryPath, basename(file.downloadUrl));
try {
let handle = this.submit(task('importResource', async (c) => {
const destination = join(this.app.temporaryPath, basename(file.downloadUrl));
const handle = this.submit(task('importResource', async (c) => {
c.update(0, 100);

await c.execute(task('download', this.networkManager.downloadFileTask({
Expand All @@ -135,20 +137,20 @@ export default class CurseForgeService extends Service {

// TODO: add tag from addon info
// let addonInf = await this.fetchProject(projectId);
resource = await c.execute(task('parsing', () => this.resourceService.importResource({
return c.execute(task('parsing', () => this.resourceService.importResource({
path: destination,
url: [file.downloadUrl, `curseforge://${projectId}/${file.id}`],
url: urls,
source: getCurseforgeSourceInfo(projectId, file.id),
type: typeHints[type],
})), 20);
}));

this.commit('curseforgeDownloadFileStart', { fileId: file.id, taskId: (handle.root as TaskState).id });
await handle.wait();
const result = await handle.wait();
this.log(`Install curseforge file ${file.displayName}(${file.downloadUrl}) success!`);
return result.path;
} finally {
this.commit('curseforgeDownloadFileEnd', file.id);
}

return resource.path;
}
}

0 comments on commit 6c9422d

Please sign in to comment.