-
Notifications
You must be signed in to change notification settings - Fork 69
fix: reduce duplicate image downloads #908
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SlayerOrnstein
wants to merge
8
commits into
master
Choose a base branch
from
simple-image-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c4dbda2
fix: use texture internal name
SlayerOrnstein 2823f5b
chore: uncomment wikia data
SlayerOrnstein 7154a90
chore: fallback to item name if file already exists
SlayerOrnstein f151eca
fix: made `saveImage` smarter by reusing image names for duplicate ha…
SlayerOrnstein f220eac
chore: clean up component logic since they now use internal generic n…
SlayerOrnstein c7704ee
chore: cleaned up some comments
SlayerOrnstein 3d900f5
chore: items not being updated in the right place
SlayerOrnstein d2a1730
chore: readd these static images
SlayerOrnstein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,9 @@ import type { | |
| CategoryData, | ||
| ApiCategory, | ||
| } from './types/shared'; | ||
| import { existsSync } from 'node:fs'; | ||
| import sanitize from 'sanitize-filename'; | ||
| import { createHash } from 'node:crypto'; | ||
|
|
||
| let imageCache: CachedItem[] = []; | ||
|
|
||
|
|
@@ -63,9 +66,9 @@ class Build { | |
| const parsed = parser.parse(raw); | ||
| const data = this.applyCustomCategories(parsed.data); | ||
| const i18n = parser.applyI18n(data, raw.i18n); | ||
| const all = await this.saveJson(data, i18n); | ||
| await this.saveImages(data, raw.manifest, parsed.warnings); | ||
| await this.saveJson(data, i18n); | ||
| await this.saveWarnings(parsed.warnings); | ||
| await this.saveImages(all, raw.manifest); | ||
| await this.updateReadme(raw.patchlogs); | ||
|
|
||
| // Log number of warnings at the end of the script | ||
|
|
@@ -123,7 +126,7 @@ class Build { | |
| async saveJson( | ||
| categories: Record<string, Item[]>, | ||
| i18n: Record<string, Record<string, Partial<Item>>> | ||
| ): Promise<Item[]> { | ||
| ): Promise<void> { | ||
| let all: Item[] = []; | ||
| const sort = (a: Item, b: Item): number => { | ||
| if (!a.name) console.log(a); | ||
|
|
@@ -150,8 +153,6 @@ class Build { | |
| all.sort(sort); | ||
| await fs.writeFile(new URL('../data/json/All.json', import.meta.url), stringify(all)); | ||
| await fs.writeFile(new URL('../data/json/i18n.json', import.meta.url), JSON.stringify(JSON.parse(stringify(i18n)))); | ||
|
|
||
| return all; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -167,30 +168,40 @@ class Build { | |
| * @param items items to append images to | ||
| * @param manifest image manifest to look up items from | ||
| */ | ||
| async saveImages(items: Item[], manifest: ImageManifest): Promise<void> { | ||
| async saveImages(categories: Record<string, Item[]>, manifest: ImageManifest, warnings: Warnings): Promise<void> { | ||
| // No need to go through every item if the manifest didn't change. I'm | ||
| // guessing the `fileTime` key in each element works more or less like a | ||
| // hash, so any change to that changes the hash of the full thing. | ||
| if (!hashManager.hasChanged('Manifest')) return; | ||
| // if (!hashManager.hasChanged('Manifest')) return; | ||
| const items = Object.values(categories).flat(); | ||
| const bar = new Progress('Fetching Images', items.length); | ||
| const duplicates: string[] = []; // Don't download component images or relics twice | ||
|
|
||
| for (const item of items) { | ||
| // Save image for parent item | ||
| await this.saveImage(item, false, duplicates, manifest); | ||
| // Save images for components if necessary | ||
| if (item.components) { | ||
| for (const component of item.components) { | ||
| await this.saveImage(component, true, duplicates, manifest); | ||
| } | ||
| } | ||
| // Save images for abilities | ||
| if (item.abilities) { | ||
| for (const ability of item.abilities) { | ||
| await this.saveImage(ability as Item, false, duplicates, manifest); | ||
| const processed: Record<string, string> = {}; // Don't download component images or relics twice | ||
|
|
||
| for (const category of Object.keys(categories)) { | ||
| const categoryData = categories[category]; | ||
| if (!categoryData) continue; | ||
|
|
||
| for (const item of categoryData) { | ||
| try { | ||
| // Save image for parent item | ||
| await this.saveImage(item, false, processed, manifest); | ||
| // Save images for components if necessary | ||
| if (item.components) { | ||
| for (const component of item.components) { | ||
| await this.saveImage(component, true, processed, manifest); | ||
| } | ||
| } | ||
| // Save images for abilities | ||
| if (item.abilities) { | ||
| for (const ability of item.abilities) { | ||
| await this.saveImage(ability as Item, false, processed, manifest); | ||
| } | ||
| } | ||
| } catch { | ||
| warnings.missingImage.push(item.name); | ||
| } | ||
| bar.tick(); | ||
| } | ||
| bar.tick(); | ||
| } | ||
|
|
||
| // write the manifests after images have all succeeded | ||
|
|
@@ -208,41 +219,49 @@ class Build { | |
| * Download and save images for items or components. | ||
| * @param item to determine and save an image for | ||
| * @param isComponent whether the item is a component or a parent | ||
| * @param duplicates list of duplicated (already existing) image names | ||
| * @param processed list of duplicated (already existing) image names | ||
| * @param manifest image lookup list | ||
| */ | ||
| async saveImage(item: Item, isComponent: boolean, duplicates: string[], manifest: ImageManifest): Promise<void> { | ||
| let { uniqueName } = item; | ||
| if (item.type === 'Nightwave Act') { | ||
| uniqueName = item.uniqueName.replace(/[0-9]{1,3}$/, ''); | ||
| } | ||
|
|
||
| const imageBase = manifest.find((i) => i.uniqueName === uniqueName); | ||
| async saveImage( | ||
| item: Item, | ||
| isComponent: boolean, | ||
| processed: Record<string, string>, | ||
| manifest: ImageManifest | ||
| ): Promise<void> { | ||
| const imageBase = manifest.find((i) => i.uniqueName === item.uniqueName); | ||
| if (!imageBase) return; | ||
|
|
||
| const imageStub = imageBase.textureLocation.replace(/\\/g, '/').replace('xport/', ''); | ||
| const imageHash = /!00_([\S]+)/.exec(imageStub); | ||
| const imageUrl = `https://content.warframe.com/PublicExport/${imageStub}`; | ||
| const basePath = fileURLToPath(new URL('../data/img/', import.meta.url)); | ||
| const filePath = path.join(basePath, item.imageName); | ||
| const manifestItem = manifest.find((i) => i.uniqueName === item.uniqueName); | ||
| const hash = manifestItem?.fileTime ?? imageHash?.[1] ?? undefined; | ||
| let filePath = path.join(basePath, item.imageName); | ||
| const hash = | ||
| imageBase?.fileTime ?? imageHash?.[1] ?? createHash('md5').update(imageBase.textureLocation).digest('hex'); | ||
| const cached = imageCache.find((c) => c.uniqueName === item.uniqueName); | ||
|
|
||
| // We'll use a custom blueprint image | ||
| if (item.name === 'Blueprint' || item.name === 'Arcane') return; | ||
|
|
||
| // Don't download component images or relic images twice | ||
| if (isComponent || item.type === 'Relic') { | ||
| if (duplicates.includes(item.imageName)) { | ||
| return; | ||
| } | ||
| duplicates.push(item.imageName); | ||
| // Don't download texture images twice | ||
| const imageName = processed[hash]; | ||
| if (imageName !== undefined) { | ||
| item.imageName = imageName; | ||
| return; | ||
| } | ||
|
|
||
| processed[hash] = item.imageName; | ||
|
|
||
| // Check for an already exisitng file and fall back to item name | ||
| if (existsSync(filePath)) { | ||
| const [_, ext] = item.imageName.split('.'); | ||
| /* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */ | ||
| item.imageName = sanitize(`${item.name.replace(/[ /*]/g, '-')}.${ext!}`); | ||
| processed[hash] = item.imageName; | ||
| filePath = path.join(basePath, item.imageName); | ||
| } | ||
|
|
||
| // Check if the previous image was for a component because they might | ||
| // have different naming schemes like lex-prime | ||
| if (!cached || cached.hash !== hash || cached.isComponent !== isComponent) { | ||
| if (cached?.hash !== hash) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In which situation both values might be undefined here? |
||
| try { | ||
| const retry = (err: Error & { code?: string }): Promise<Buffer | string> => { | ||
| if (err.code === 'ENOTFOUND') { | ||
|
|
@@ -268,6 +287,8 @@ class Build { | |
| } catch (e) { | ||
| // swallow error | ||
| console.error(e); | ||
| item.imageName = 'missing.png'; | ||
| throw e; | ||
| } | ||
| } | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.