// checks from path and in ZIPs, returns 1 if NOT found int32_t check_file_exist(const char *fn) { int32_t opsm = pathsearchmode; char *tfn; pathsearchmode = 1; if (findfrompath(fn,&tfn) < 0) { char buf[BMAX_PATH]; Bstrcpy(buf,fn); kzfindfilestart(buf); if (!kzfindfile(buf)) { initprintf("Error: file \"%s\" does not exist\n",fn); pathsearchmode = opsm; return 1; } } else Bfree(tfn); pathsearchmode = opsm; return 0; }
static void ProcessGroups(CACHE1D_FIND_REC *srch) { CACHE1D_FIND_REC *sidx; struct grpcache *fg, *fgg; char *fn; struct Bstat st; #define BUFFER_SIZE (1024 * 1024 * 8) uint8_t *buf = (uint8_t *)Xmalloc(BUFFER_SIZE); for (sidx = srch; sidx; sidx = sidx->next) { for (fg = grpcache; fg; fg = fg->next) { if (!Bstrcmp(fg->name, sidx->name)) break; } if (fg) { if (findfrompath(sidx->name, &fn)) continue; // failed to resolve the filename if (Bstat(fn, &st)) { Bfree(fn); continue; } // failed to stat the file Bfree(fn); if (fg->size == (int32_t)st.st_size && fg->mtime == (int32_t)st.st_mtime) { grpinfo_t const * const grptype = FindGrpInfo(fg->crcval, fg->size); if (grptype) { grpfile_t * const grp = (grpfile_t *)Xcalloc(1, sizeof(grpfile_t)); grp->filename = Xstrdup(sidx->name); grp->type = grptype; grp->next = foundgrps; foundgrps = grp; } fgg = (struct grpcache *)Xcalloc(1, sizeof(struct grpcache)); strcpy(fgg->name, fg->name); fgg->size = fg->size; fgg->mtime = fg->mtime; fgg->crcval = fg->crcval; fgg->next = usedgrpcache; usedgrpcache = fgg; continue; } } { int32_t b, fh; int32_t crcval = 0; fh = openfrompath(sidx->name, BO_RDONLY|BO_BINARY, BS_IREAD); if (fh < 0) continue; if (Bfstat(fh, &st)) continue; initprintf(" Checksumming %s...", sidx->name); do { b = read(fh, buf, BUFFER_SIZE); if (b > 0) crcval = Bcrc32((uint8_t *)buf, b, crcval); } while (b == BUFFER_SIZE); close(fh); initprintf(" Done\n"); grpinfo_t const * const grptype = FindGrpInfo(crcval, st.st_size); if (grptype) { grpfile_t * const grp = (grpfile_t *)Xcalloc(1, sizeof(grpfile_t)); grp->filename = Xstrdup(sidx->name); grp->type = grptype; grp->next = foundgrps; foundgrps = grp; } fgg = (struct grpcache *)Xcalloc(1, sizeof(struct grpcache)); Bstrncpy(fgg->name, sidx->name, BMAX_PATH); fgg->size = st.st_size; fgg->mtime = st.st_mtime; fgg->crcval = crcval; fgg->next = usedgrpcache; usedgrpcache = fgg; } } Bfree(buf); }
int32_t ScanGroups(void) { CACHE1D_FIND_REC *srch, *sidx; struct grpcache *fg, *fgg; struct grpfile *grp; char *fn; struct Bstat st; #define BUFFER_SIZE (1024 * 1024 * 8) uint8_t *buf = (uint8_t *)Bmalloc(BUFFER_SIZE); if (!buf) { initprintf("Error allocating %d byte buffer to scan GRPs!\n", BUFFER_SIZE); return 0; } initprintf("Searching for game data...\n"); LoadGameList(); //LoadGroupsCache(); SB: disabled to match local srch = klistpath("/", "*.grp", CACHE1D_FIND_FILE); for (sidx = srch; sidx; sidx = sidx->next) { for (fg = grpcache; fg; fg = fg->next) { if (!Bstrcmp(fg->name, sidx->name)) break; } if (fg) { if (findfrompath(sidx->name, &fn)) continue; // failed to resolve the filename if (Bstat(fn, &st)) { Bfree(fn); continue; } // failed to stat the file Bfree(fn); if (fg->size == st.st_size && fg->mtime == st.st_mtime) { grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); grp->name = Bstrdup(sidx->name); grp->crcval = fg->crcval; grp->size = fg->size; grp->next = foundgrps; foundgrps = grp; fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache)); strcpy(fgg->name, fg->name); fgg->size = fg->size; fgg->mtime = fg->mtime; fgg->crcval = fg->crcval; fgg->next = usedgrpcache; usedgrpcache = fgg; continue; } } { int32_t b, fh; int32_t crcval; fh = openfrompath(sidx->name, BO_RDONLY|BO_BINARY, BS_IREAD); if (fh < 0) continue; if (Bfstat(fh, &st)) continue; initprintf(" Checksumming %s...", sidx->name); crc32init((uint32_t *)&crcval); do { b = read(fh, buf, BUFFER_SIZE); if (b > 0) crc32block((uint32_t *)&crcval, (uint8_t *)buf, b); } while (b == BUFFER_SIZE); crc32finish((uint32_t *)&crcval); close(fh); initprintf(" Done\n"); grp = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); grp->name = Bstrdup(sidx->name); grp->crcval = crcval; grp->size = st.st_size; grp->next = foundgrps; foundgrps = grp; fgg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache)); Bstrncpy(fgg->name, sidx->name, BMAX_PATH); fgg->size = st.st_size; fgg->mtime = st.st_mtime; fgg->crcval = crcval; fgg->next = usedgrpcache; usedgrpcache = fgg; } } klistfree(srch); FreeGroupsCache(); for (grp = foundgrps; grp; /*grp=grp->next*/) { struct grpfile *igrp; for (igrp = listgrps; igrp; igrp=igrp->next) if (grp->crcval == igrp->crcval) break; if (igrp == NULL) { grp = grp->next; continue; } if (igrp->dependency) { struct grpfile *depgrp; //initprintf("found grp with dep\n"); for (depgrp = foundgrps; depgrp; depgrp=depgrp->next) if (depgrp->crcval == igrp->dependency) break; if (depgrp == NULL || depgrp->crcval != igrp->dependency) // couldn't find dependency { //initprintf("removing %s\n", grp->name); RemoveGroup(igrp->crcval); grp = foundgrps; continue; } } if (igrp->game && !grp->game) grp->game = igrp->game; grp=grp->next; } if (usedgrpcache) { int32_t i = 0; FILE *fp; fp = fopen(GRPCACHEFILE, "wt"); if (fp) { for (fg = usedgrpcache; fg; fg=fgg) { fgg = fg->next; fprintf(fp, "\"%s\" %d %d %d\n", fg->name, fg->size, fg->mtime, fg->crcval); Bfree(fg); i++; } fclose(fp); } // initprintf("Found %d recognized GRP %s.\n",i,i>1?"files":"file"); Bfree(buf); return 0; } initprintf("Found no recognized game data!\n"); Bfree(buf); return 0; }