int main(int argc, char **argv) { struct icon icon; int32_t bpl = 0; int i; unsigned char *maskp, bm, *pp; if (argc<2) { Bfprintf(stderr, "generateicon <picture file>\n"); return 1; } memset(&icon, 0, sizeof(icon)); kpzload(argv[1], (intptr_t*)&icon.pixels, (int32_t*)&bpl, (int32_t*)&icon.width, (int32_t*)&icon.height); if (!icon.pixels) { Bfprintf(stderr, "Failure loading %s\n", argv[1]); return 1; } if (bpl != icon.width * 4) { Bfprintf(stderr, "bpl != icon.width * 4\n"); Bfree(icon.pixels); return 1; } icon.mask = (unsigned char *)Bcalloc(icon.height, (icon.width+7)/8); if (!icon.mask) { Bfprintf(stderr, "Out of memory\n"); Bfree(icon.pixels); return 1; } maskp = icon.mask; bm = 1; pp = (unsigned char *)icon.pixels; for (i=0; i<icon.height*icon.width; i++) { if (bm == 0) { bm = 1; maskp++; } { unsigned char c = pp[0]; pp[0] = pp[2]; pp[2] = c; } if (pp[3] > 0) *maskp |= bm; bm <<= 1; pp += 4; } writeicon(stdout, &icon); Bfree(icon.pixels); Bfree(icon.mask); return 0; }
static int32_t LoadGroupsCache(void) { struct grpcache *fg; int32_t fsize, fmtime, fcrcval; char *fname; scriptfile *script; script = scriptfile_fromfile(GRPCACHEFILE); if (!script) return -1; while (!scriptfile_eof(script)) { if (scriptfile_getstring(script, &fname)) break; // filename if (scriptfile_getnumber(script, &fsize)) break; // filesize if (scriptfile_getnumber(script, &fmtime)) break; // modification time if (scriptfile_getnumber(script, &fcrcval)) break; // crc checksum fg = (struct grpcache *)Bcalloc(1, sizeof(struct grpcache)); fg->next = grpcache; grpcache = fg; Bstrncpy(fg->name, fname, BMAX_PATH); fg->size = fsize; fg->mtime = fmtime; fg->crcval = fcrcval; } scriptfile_close(script); return 0; }
static void LoadGameList(void) { struct grpfile *fg; CACHE1D_FIND_REC *srch, *sidx; int32_t i; for (i = 0; i<NUMGRPFILES; i++) { fg = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); fg->name = Bstrdup(internalgrpfiles[i].name); fg->crcval = internalgrpfiles[i].crcval; fg->size = internalgrpfiles[i].size; fg->game = internalgrpfiles[i].game; fg->dependency = internalgrpfiles[i].dependency; if (internalgrpfiles[i].scriptname) fg->scriptname = dup_filename(internalgrpfiles[i].scriptname); if (internalgrpfiles[i].defname) fg->defname = dup_filename(internalgrpfiles[i].defname); fg->next = listgrps; listgrps = fg; } srch = klistpath("/", "*.grpinfo", CACHE1D_FIND_FILE); for (sidx = srch; sidx; sidx = sidx->next) LoadList(srch->name); klistfree(srch); }
// // initinput() -- init input system // int initinput(void) { int i,j; #ifdef __APPLE__ // force OS X to operate in >1 button mouse mode so that LMB isn't adulterated if (!getenv("SDL_HAS3BUTTONMOUSE")) putenv("SDL_HAS3BUTTONMOUSE=1"); #endif if (SDL_EnableKeyRepeat(250, 30)) initprintf("Error enabling keyboard repeat.\n"); inputdevices = 1|2; // keyboard (1) and mouse (2) mouseacquired = 0; SDL_EnableUNICODE(1); // let's hope this doesn't hit us too hard memset(keynames,0,sizeof(keynames)); for (i=0; i<SDLK_LAST; i++) { if (!keytranslation[i]) continue; strncpy((char *)keynames[ keytranslation[i] ], SDL_GetKeyName(i), sizeof(keynames[i])-1); } if (!SDL_InitSubSystem(SDL_INIT_JOYSTICK)) { i = SDL_NumJoysticks(); initprintf("%d joystick(s) found\n",i); for (j=0;j<i;j++) initprintf(" %d. %s\n", j+1, SDL_JoystickName(j)); joydev = SDL_JoystickOpen(0); if (joydev) { SDL_JoystickEventState(SDL_ENABLE); inputdevices |= 4; joynumaxes = SDL_JoystickNumAxes(joydev); joynumbuttons = min(32,SDL_JoystickNumButtons(joydev)); joynumhats = SDL_JoystickNumHats(joydev); initprintf("Joystick 1 has %d axes, %d buttons, and %d hat(s).\n", joynumaxes,joynumbuttons,joynumhats); joyaxis = (long *)Bcalloc(joynumaxes, sizeof(long)); joyhat = (long *)Bcalloc(joynumhats, sizeof(long)); } } return 0; }
static void LoadList(const char * filename) { struct grpfile *fg; char *grpend = NULL; scriptfile *script = scriptfile_fromfile(filename); if (!script) return; scriptfile_addsymbolvalue("GAMEFLAG_DUKE", GAMEFLAG_DUKE); scriptfile_addsymbolvalue("GAMEFLAG_ADDON", GAMEFLAG_DUKE|GAMEFLAG_ADDON); scriptfile_addsymbolvalue("DUKE15_CRC", DUKE15_CRC); scriptfile_addsymbolvalue("DUKE13_CRC", DUKE13_CRC); scriptfile_addsymbolvalue("DUKEDC_CRC", DUKEDC_CRC); scriptfile_addsymbolvalue("DUKECB_CRC", DUKECB_CRC); scriptfile_addsymbolvalue("DUKENW_CRC", DUKENW_CRC); while (!scriptfile_eof(script)) { enum { T_GRPINFO, T_GAMENAME, T_CRC, T_SIZE, T_DEPCRC, T_SCRIPTNAME, T_DEFNAME, T_FLAGS, }; static const tokenlist profiletokens[] = { { "grpinfo", T_GRPINFO }, }; int32_t token = getatoken(script,profiletokens,sizeof(profiletokens)/sizeof(tokenlist)); switch (token) { case T_GRPINFO: { int32_t gsize = 0, gcrcval = 0, gflags = GAMEFLAG_DUKE, gdepcrc = DUKE15_CRC; char *gname = NULL, *gscript = NULL, *gdef = NULL; static const tokenlist grpinfotokens[] = { { "name", T_GAMENAME }, { "scriptname", T_SCRIPTNAME }, { "defname", T_DEFNAME }, { "crc", T_CRC }, { "dependency", T_DEPCRC }, { "size", T_SIZE }, { "flags", T_FLAGS }, }; if (scriptfile_getbraces(script,&grpend)) break; while (script->textptr < grpend) { int32_t token = getatoken(script,grpinfotokens,sizeof(grpinfotokens)/sizeof(tokenlist)); switch (token) { case T_GAMENAME: scriptfile_getstring(script,&gname); break; case T_SCRIPTNAME: scriptfile_getstring(script,&gscript); break; case T_DEFNAME: scriptfile_getstring(script,&gdef); break; case T_FLAGS: scriptfile_getsymbol(script,&gflags); break; case T_DEPCRC: scriptfile_getsymbol(script,&gdepcrc); break; case T_CRC: scriptfile_getsymbol(script,&gcrcval); break; case T_SIZE: scriptfile_getnumber(script,&gsize); break; default: break; } fg = (struct grpfile *)Bcalloc(1, sizeof(struct grpfile)); fg->next = listgrps; listgrps = fg; if (gname) fg->name = Bstrdup(gname); fg->size = gsize; fg->crcval = gcrcval; fg->dependency = gdepcrc; fg->game = gflags; if (gscript) fg->scriptname = dup_filename(gscript); if (gdef) fg->defname = dup_filename(gdef); } break; } default: break; } } scriptfile_close(script); scriptfile_clearsymbols(); }
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; }