Skip to content

Commit fdf370b

Browse files
committed
make getByDataHash() take a hash as argument
1 parent 036de97 commit fdf370b

6 files changed

Lines changed: 16 additions & 22 deletions

File tree

mclib/src/finder/LocalSolutionFinder.test.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,12 @@ class MockRepository implements IRepository {
4141
}
4242

4343
async hashModData(modData: Uint8Array): Promise<string> {
44-
return Array.from(modData.slice(0, 8)).join('-'); // Simple hash for testing
44+
// For testing, we'll use a simple key based on the first few bytes
45+
return Array.from(modData.slice(0, 8)).join('-');
4546
}
4647

47-
async getByDataHash(modData: Uint8Array): Promise<ModRepoMetadata | null> {
48-
// For testing, we'll use a simple key based on the first few bytes
49-
const key = await this.hashModData(modData);
50-
return this.dataHashes[key] || null;
48+
async getByDataHash(hash: string): Promise<ModRepoMetadata | null> {
49+
return this.dataHashes[hash] || null;
5150
}
5251

5352
getRepositoryName(): ModRepositoryName {

mclib/src/query/ModQueryService.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class ModQueryService implements IModQueryService {
8888
for (const repo of this.repositories) {
8989
logger.debug("getModByDataHash(size = %s, %s)", modData.length, repo.getRepositoryName())
9090
try {
91-
const result = await repo.getByDataHash(modData);
91+
const result = await repo.getByDataHash(await repo.hashModData(modData));
9292
if (result) {
9393
logger.debug("getModByDataHash(size = %s, %s) = %s (%s)", modData.length, repo.getRepositoryName(), result.id, result.name);
9494
results.push(result);

mclib/src/repos/CurseForgeRepository.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,15 @@ export class CurseForgeRepository implements IRepository {
9797
return (await modResp.json()).data as ModInfoData;
9898
}
9999

100-
async getByDataHash(modData: Uint8Array): Promise<ModRepoMetadata | null> {
101-
// Calculate CurseForge fingerprint
102-
const start = Date.now();
103-
const fingerprint: number = cf_fingerprint(modData);
104-
const duration = Date.now() - start;
105-
logger.debug(`cf_fingerprint(${modData.length} bytes): ${duration}ms`);
106-
100+
async getByDataHash(hash: string): Promise<ModRepoMetadata | null> {
107101
// Use the CurseForge API to get file info by fingerprint
108102
const resp = await this.fetchClient(`${CurseForgeRepository.BASE_URL}/fingerprints`, {
109103
method: 'POST',
110104
headers: {
111105
'Content-Type': 'application/json',
112106
},
113107
body: JSON.stringify({
114-
fingerprints: [fingerprint]
108+
fingerprints: [parseInt(hash)]
115109
})
116110
});
117111

@@ -141,7 +135,11 @@ export class CurseForgeRepository implements IRepository {
141135
}
142136

143137
async hashModData(modData: Uint8Array): Promise<string> {
144-
return cf_fingerprint(modData).toString();
138+
const start = Date.now();
139+
const fingerprint: number = cf_fingerprint(modData);
140+
const duration = Date.now() - start;
141+
logger.debug(`cf_fingerprint(${modData.length} bytes): ${duration}ms`);
142+
return fingerprint.toString();
145143
}
146144

147145
getRepositoryName(): ModRepositoryName {

mclib/src/repos/IRepository.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ export interface IRepository {
2727

2828
/**
2929
* Get mod information by file hash/fingerprint from mod file bytes.
30-
* @param modData The complete mod file data as bytes.
30+
* @param hash The hash provided by hashModData().
3131
* @returns A promise resolving to mod search metadata, or null if not found.
3232
*/
33-
getByDataHash(modData: Uint8Array): Promise<ModRepoMetadata | null>;
33+
getByDataHash(hash: string): Promise<ModRepoMetadata | null>;
3434

3535
/**
3636
* Returns the repository name (ModRepositoryName enum value)

mclib/src/repos/ModrinthRepository.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,7 @@ export class ModrinthRepository implements IRepository {
8181
}));
8282
}
8383

84-
async getByDataHash(modData: Uint8Array): Promise<ModRepoMetadata | null> {
85-
// Calculate SHA-1 hash of the mod data for Modrinth
86-
const hash = await this.calculateSHA1(modData);
87-
84+
async getByDataHash(hash: string): Promise<ModRepoMetadata | null> {
8885
// Get version info using the hash
8986
const versionResp = await this.fetchClient(`https://api.modrinth.com/v2/version_file/${hash}`);
9087
if (!versionResp.ok) return null;

web/src/components/FileDropZone.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
// Try to identify the mod using each repository
7171
for (const repo of repositories) {
7272
try {
73-
const modInfo = await repo.getByDataHash(modData);
73+
const modInfo = await repo.getByDataHash(await repo.hashModData(modData));
7474
if (modInfo) {
7575
console.log(`Found mod: ${modInfo.name} from ${repo.getRepositoryName()}`);
7676
add_mod_to_list(modInfo);

0 commit comments

Comments
 (0)