From 4b0166031301edfd48b74b079028dfaf3b5483f0 Mon Sep 17 00:00:00 2001 From: Julien Constant Date: Sun, 7 Apr 2024 23:42:58 +0200 Subject: [PATCH] feat #18 : add textures aliases --- prisma/schema.prisma | 1 + .../contribute/drafts/drafts-modal.tsx | 60 ++++++++----------- src/server/actions/files.ts | 27 ++++++--- src/server/data/mods-version.ts | 24 ++++---- 4 files changed, 55 insertions(+), 57 deletions(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f3336fb..153dea9 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -129,6 +129,7 @@ model Resource { model Texture { id String @id @default(cuid()) name String + aliases String[] contributions Contribution[] filepath String hash String @unique diff --git a/src/components/contribute/drafts/drafts-modal.tsx b/src/components/contribute/drafts/drafts-modal.tsx index b0f6dea..d177464 100644 --- a/src/components/contribute/drafts/drafts-modal.tsx +++ b/src/components/contribute/drafts/drafts-modal.tsx @@ -71,21 +71,14 @@ export function ContributionDraftModal({ contribution, textures, onClose }: Cont
- {sanitizeTextureName(texture.name)} + {texture.name} {option.disabled && Already selected!} - {/* TODO for #18 {!option.disabled && {texture.aliases.join(', ')}} */} + {!option.disabled && {texture.aliases.join(', ')}}
); }; - /** - * @deprecated To be removed when #18 is implemented. - */ - const sanitizeTextureName = (name: string): string => { - return name.split('_')[1]?.split('.')[0]; - }; - const previousContribution = () => { if (selectedTextureContributionsIndex === 0) return; let index = selectedTextureContributionsIndex - 1; @@ -134,7 +127,7 @@ export function ContributionDraftModal({ contribution, textures, onClose }: Cont {contribution.filename} - The targeted texture. - - - c.id)} - style={windowWidth <= BREAKPOINT_MOBILE_LARGE - ? { width: '100%' } + style={windowWidth <= BREAKPOINT_MOBILE_LARGE + ? { width: '100%' } : { width: 'calc((100% - var(--mantine-spacing-md)) * .8)' } } /> @@ -296,4 +286,4 @@ export function ContributionDraftModal({ contribution, textures, onClose }: Cont ); -} \ No newline at end of file +} diff --git a/src/server/actions/files.ts b/src/server/actions/files.ts index 17c85df..f1c6df1 100644 --- a/src/server/actions/files.ts +++ b/src/server/actions/files.ts @@ -38,9 +38,9 @@ export async function remove(publicPath: `files/${string}`): Promise { /** * Extracts mcmod.info from a jar file - * + * * TODO: find behavior for Fabric mods - * + * * @param jar the jar file to extract the mcmod.info from * @returns The mcmod.info data */ @@ -96,7 +96,7 @@ function sanitizeMCModInfo(mcmodInfo: MCModInfoData): MCModInfo[] { * Extract mod(s) versions from given JAR file * - If mod(s) does not exist, create it * - If mod(s) version(s) does not exist, create it and extract the default resource pack - * + * * @param jar the jar file to extract the mod versions from * @returns The extracted mod versions */ @@ -138,9 +138,9 @@ export async function extractModVersionsFromJAR( /** * Extract blocks and items models, textures to the /public dir - * + * * TODO add support for models - * + * * @param jar The jar file to extract the resources from * @param modVersion The mod version to extract the resources from and to be linked to the extracted resources */ @@ -171,9 +171,12 @@ export async function extractDefaultResourcePack(jar: File, modVersion: ModVersi const buffer = await textureAsset.buffer(); const hash = calculateHash(buffer); + const fileExtension = textureAsset.path.split('.').pop()!; + const textureName = textureAsset.path.split('/').pop()!.split('.')[0]; + let texture = await findTexture({ hash }); if (!texture) { - const filename = `${uuid}_${textureAsset.path.split('/').pop()}`; + const filename = `${uuid}_${textureName}.${fileExtension}`; const filepath = join(fileDir, filename); writeFileSync(filepath, buffer); @@ -181,9 +184,17 @@ export async function extractDefaultResourcePack(jar: File, modVersion: ModVersi texture = await createTexture({ filepath: join('files', 'textures', 'default', filename), hash, - name: filename, + name: textureName, }); } + else { + if (texture.name !== textureName && !texture.aliases.includes(textureName)) { + await db.texture.update({ + where: { id: texture.id }, + data: { aliases: { set: [...texture.aliases, textureName] } }, + }) + } + } let resource = await getResource({ asset, modVersion }); if (!resource) resource = await createResource({ asset, modVersion }); @@ -196,4 +207,4 @@ function calculateHash(buffer: Buffer) { const hash = createHash('sha256'); hash.update(buffer); return hash.digest('hex'); -} \ No newline at end of file +} diff --git a/src/server/data/mods-version.ts b/src/server/data/mods-version.ts index f52a43e..59ce6b9 100644 --- a/src/server/data/mods-version.ts +++ b/src/server/data/mods-version.ts @@ -14,28 +14,24 @@ import { extractModVersionsFromJAR } from '../actions/files'; export async function getModVersionsWithModpacks(modId: string): Promise { const res: ModVersionWithModpacks[] = []; const modVersions = await db.modVersion.findMany({ where: { modId } }); - + for (const modVer of modVersions) { const modpacks = await db.modpackVersion.findMany({ where: { mods: { some: { id: modVer.id }}}, include: { modpack: true }}) .then((mvs) => mvs.map((mv) => mv.modpack)); res.push({ ...modVer, modpacks }); } - - return res; -} -function randInt(min: number, max: number) { - return Math.floor(Math.random() * (max - min + 1) + min); + return res; } export async function getModsVersionsProgression(): Promise { const modVersions = (await db.modVersion.findMany({ include: { mod: true, resources: true } })) - .map((modVer) => ({ - ...modVer, - ...EMPTY_PROGRESSION, - resources: modVer.resources.map((resource) => ({ - ...resource, + .map((modVer) => ({ + ...modVer, + ...EMPTY_PROGRESSION, + resources: modVer.resources.map((resource) => ({ + ...resource, ...EMPTY_PROGRESSION, })), })); @@ -45,8 +41,8 @@ export async function getModsVersionsProgression(): Promise lt.textureId) }, + where: { + id: { in: linkedTextures.map((lt) => lt.textureId) }, }, }); @@ -101,7 +97,7 @@ export async function addModVersionsFromJAR(jar: FormData): Promise { await canAccess(); - + return db.modVersion.create({ data: { modId: mod.id, version, mcVersion } }); }