void FCEUSS_Save(char *fname) { MEM_TYPE *st = NULL; char *fn; if (geniestage == 1) { FCEU_DispMessage("Cannot save FCS in GG screen."); return; } #ifdef HAVE_MEMSTREAM st = memstream_open(1); #else if (fname) st = FCEUD_UTF8fopen(fname, "wb"); else { st = FCEUD_UTF8fopen(fn = FCEU_MakeFName(FCEUMKF_STATE, CurrentState, 0), "wb"); free(fn); } if (st == NULL) { FCEU_DispMessage("State %d save error.", CurrentState); return; } #endif FCEUSS_SaveFP(st); SaveStateStatus[CurrentState] = 1; fclose(st); #ifndef HAVE_MEMSTREAM FCEU_DispMessage("State %d saved.", CurrentState); #endif }
void FCEUSS_Save(char *fname) { FILE *st=NULL; char *fn; if(geniestage==1) { FCEU_DispMessage("Cannot save FCS in GG screen."); return; } if(fname) st=FCEUD_UTF8fopen(fname, "wb"); else { st=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_STATE,CurrentState,0),"wb"); free(fn); } if(st == NULL) { FCEU_DispMessage("State %d save error.",CurrentState); return; } FCEUSS_SaveFP(st); SaveStateStatus[CurrentState]=1; fclose(st); FCEU_DispMessage("State %d saved.",CurrentState); }
void FCEU_LoadGamePalette(void) { uint8 ptmp[192]; FILE *fp; char *fn; ipalette=0; fn=FCEU_MakeFName(FCEUMKF_PALETTE,0,0); if((fp=FCEUD_UTF8fopen(fn,"rb"))) { int x; fread(ptmp,1,192,fp); fclose(fp); for(x=0;x<64;x++) { palettei[x].r=ptmp[x+x+x]; palettei[x].g=ptmp[x+x+x+1]; palettei[x].b=ptmp[x+x+x+2]; } ipalette=1; } free(fn); }
/** * Read a custom pallete from a file and load it into the core. */ int LoadCPalette(const std::string &file) { uint8 tmpp[192]; FILE *fp; if(!(fp = FCEUD_UTF8fopen(file.c_str(), "rb"))) { char errorMsg[256]; strcpy(errorMsg, "Error loading custom palette from file: "); strcat(errorMsg, file.c_str()); FCEUD_PrintError(errorMsg); return 0; } size_t result = fread(tmpp, 1, 192, fp); if(result != 192) { char errorMsg[256]; strcpy(errorMsg, "Error loading custom palette from file: "); strcat(errorMsg, file.c_str()); FCEUD_PrintError(errorMsg); return 0; } FCEUI_SetUserPalette(tmpp, result/3); fclose(fp); return 1; }
void FCEUI_DumpMem(const char *fname, uint32 start, uint32 end) { FILE *fp = FCEUD_UTF8fopen(fname, "wb"); fceuindbg = 1; for (; start <= end; start++) fputc(ARead[start](start), fp); fclose(fp); fceuindbg = 0; }
void FCEUI_DumpVid(const char *fname, uint32 start, uint32 end) { FILE *fp = FCEUD_UTF8fopen(fname, "wb"); fceuindbg = 1; start &= 0x3FFF; end &= 0x3FFF; for (; start <= end; start++) fputc(VPage[start >> 10][start], fp); fclose(fp); fceuindbg = 0; }
int FCEUSS_Load(char *fname) { MEM_TYPE *st; char *fn; #ifdef HAVE_MEMSTREAM st = memstream_open(0); #else if (geniestage == 1) { FCEU_DispMessage("Cannot load FCS in GG screen."); return(0); } if (fname) st = FCEUD_UTF8fopen(fname, "rb"); else { st = FCEUD_UTF8fopen(fn = FCEU_MakeFName(FCEUMKF_STATE, CurrentState, fname), "rb"); free(fn); } if (st == NULL) { FCEU_DispMessage("State %d load error.", CurrentState); SaveStateStatus[CurrentState] = 0; return(0); } #endif if (FCEUSS_LoadFP(st)) { #ifndef HAVE_MEMSTREAM SaveStateStatus[CurrentState] = 1; FCEU_DispMessage("State %d loaded.", CurrentState); #endif SaveStateStatus[CurrentState] = 1; fclose(st); return(1); } else { SaveStateStatus[CurrentState] = 1; #ifndef HAVE_MEMSTREAM FCEU_DispMessage("Error(s) reading state %d!", CurrentState); #endif fclose(st); return(0); } }
int FCEUSS_Load(char *fname) { FILE *st; char *fn; if(geniestage==1) { FCEU_DispMessage("Cannot load FCS in GG screen."); return(0); } if(fname) st=FCEUD_UTF8fopen(fname, "rb"); else { st=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_STATE,CurrentState,fname),"rb"); free(fn); } if(st == NULL) { FCEU_DispMessage("State %d load error.",CurrentState); SaveStateStatus[CurrentState]=0; return(0); } if(FCEUSS_LoadFP(st)) { SaveStateStatus[CurrentState]=1; FCEU_DispMessage("State %d loaded.",CurrentState); SaveStateStatus[CurrentState]=1; fclose(st); return(1); } else { SaveStateStatus[CurrentState]=1; FCEU_DispMessage("Error(s) reading state %d!",CurrentState); fclose(st); return(0); } }
static int truncate_existing(const char* filename) { // this is only here because AVIFileOpen doesn't seem to do it for us FILE* fd = FCEUD_UTF8fopen(filename, "wb"); if(fd) { fclose(fd); return 1; } return 0; }
void FCEUI_LoadMem(const char *fname, uint32 start, int hl) { int t; FILE *fp = FCEUD_UTF8fopen(fname, "rb"); while ((t = fgetc(fp)) >= 0) { if (start > 0xFFFF) break; if (hl) { extern uint8 *Page[32]; if (Page[start / 2048]) Page[start / 2048][start] = t; } else BWrite[start](start, t); start++; } fclose(fp); }
bool SetPalette(const char* nameo) { FILE *fp; if((fp = FCEUD_UTF8fopen(nameo, "rb"))) { fread(cpalette, 1, 192, fp); fclose(fp); FCEUI_SetPaletteArray(cpalette); eoptions |= EO_CPALETTE; return true; } else { FCEUD_PrintError("Error opening palette file!"); return false; } }
/** * Read a custom pallete from a file and load it into the core. */ int LoadCPalette(const std::string &file) { uint8 tmpp[192]; FILE *fp; if (!(fp = FCEUD_UTF8fopen(file.c_str(), "rb"))) { printf(" Error loading custom palette from file: %s\n", file.c_str()); return 0; } size_t result = fread(tmpp, 1, 192, fp); if (result != 192) { printf(" Error reading custom palette from file: %s\n", file.c_str()); return 0; } FCEUI_SetPaletteArray(tmpp); fclose(fp); return 1; }
bool FCEUI_BeginWaveRecord(const char *fn) { int r; if(!(soundlog=FCEUD_UTF8fopen(fn,"wb"))) return false; wsize=0; /* Write the header. */ fputs("RIFF",soundlog); fseek(soundlog,4,SEEK_CUR); // Skip size fputs("WAVEfmt ",soundlog); fputc(0x10,soundlog); fputc(0,soundlog); fputc(0,soundlog); fputc(0,soundlog); fputc(1,soundlog); // PCM fputc(0,soundlog); fputc(1,soundlog); // Monophonic fputc(0,soundlog); r=FSettings.SndRate; fputc(r&0xFF,soundlog); fputc((r>>8)&0xFF,soundlog); fputc((r>>16)&0xFF,soundlog); fputc((r>>24)&0xFF,soundlog); r<<=1; fputc(r&0xFF,soundlog); fputc((r>>8)&0xFF,soundlog); fputc((r>>16)&0xFF,soundlog); fputc((r>>24)&0xFF,soundlog); fputc(2,soundlog); fputc(0,soundlog); fputc(16,soundlog); fputc(0,soundlog); fputs("data",soundlog); fseek(soundlog,4,SEEK_CUR); return true; }
void FCEUSS_CheckStates(void) { MEM_TYPE *st = NULL; char *fn; int ssel; for (ssel = 0; ssel < 10; ssel++) { st = FCEUD_UTF8fopen(fn = FCEU_MakeFName(FCEUMKF_STATE, ssel, 0), "rb"); free(fn); if (st) { SaveStateStatus[ssel] = 1; fclose(st); } else SaveStateStatus[ssel] = 0; } CurrentState = 0; StateShow = 0; }
int FCEUNET_SendFile(uint8 cmd, char *fn) { #ifdef NETPLAY_ENABLED uint32 len; uLongf clen; char *buf, *cbuf; FILE *fp; struct stat sb; if(!(fp=FCEUD_UTF8fopen(fn,"rb"))) return(0); fstat(fileno(fp),&sb); len = sb.st_size; buf = malloc(len); fread(buf, 1, len, fp); fclose(fp); cbuf = malloc(4 + len + len / 1000 + 12); FCEU_en32lsb(cbuf, len); compress2(cbuf + 4, &clen, buf, len, 7); free(buf); //printf("Sending file: %s, %d, %d\n",fn,len,clen); len = clen + 4; #ifdef NETWORK if(!FCEUNET_SendCommand(cmd,len)) { free(cbuf); return(0); } if(!FCEUD_SendData(cbuf, len)) { NetError(); free(cbuf); return(0); } #endif free(cbuf); #endif return(1); }
void FCEU_LoadGamePalette(void) { palette_game_available = false; std::string path = FCEU_MakeFName(FCEUMKF_PALETTE,0,0); FILE* fp = FCEUD_UTF8fopen(path,"rb"); if(fp) { int readed = fread(palette_game,1,64*8*3,fp); int nEntries = readed/3; fclose(fp); //if palette is incomplete, generate deemph entries if(nEntries != 512) ApplyDeemphasisComplete(palette_game); palette_game_available = true; } //not sure whether this is needed FCEU_ResetPalette(); }
int FCEUNET_SendFile(uint8 cmd, char *fn) { uint32 len; uLongf clen; char *buf, *cbuf; FILE *fp; struct stat sb; if(!(fp=FCEUD_UTF8fopen(fn,"rb"))) return(0); FCEUX_fstat(fileno(fp),&sb); len = sb.st_size; buf = (char*)FCEU_dmalloc(len); //mbg merge 7/17/06 added cast fread(buf, 1, len, fp); fclose(fp); cbuf = (char*)FCEU_dmalloc(4 + len + len / 1000 + 12); //mbg merge 7/17/06 added cast FCEU_en32lsb((uint8*)cbuf, len); //mbg merge 7/17/06 added cast compress2((uint8*)cbuf + 4, &clen, (uint8*)buf, len, 7); //mbg merge 7/17/06 added casts free(buf); //printf("Sending file: %s, %d, %d\n",fn,len,clen); len = clen + 4; if(!FCEUNET_SendCommand(cmd,len)) { free(cbuf); return(0); } if(!FCEUD_SendData(cbuf, len)) { NetError(); free(cbuf); return(0); } free(cbuf); return(1); }
void FCEUI_SaveMovie(char *fname, uint8 flags, const char* metadata) { FILE *fp; char *fn; int poweron=0; uint8 padding[4] = {0,0,0,0}; int n_padding; FCEUI_StopMovie(); char origname[512]; if(fname) { fp = FCEUD_UTF8fopen(fname, "wb"); strcpy(origname,fname); } else { fp=FCEUD_UTF8fopen(fn=FCEU_MakeFName(FCEUMKF_MOVIE,CurrentMovie,0),"wb"); strcpy(origname,fn); free(fn); } if(!fp) return; // don't need the movieSyncHackOn sync hack for newly recorded movies flags |= MOVIE_FLAG_NOSYNCHACK; resetDMCacc=movieSyncHackOn=0; // add PAL flag if(FCEUI_GetCurrentVidSystem(0,0)) flags |= MOVIE_FLAG_PAL; if(flags & MOVIE_FLAG_FROM_POWERON) { poweron=1; flags &= ~MOVIE_FLAG_FROM_POWERON; flags |= MOVIE_FLAG_FROM_RESET; } // write header write32le(MOVIE_MAGIC, fp); write32le(MOVIE_VERSION, fp); fputc(flags, fp); fputc(0, fp); // reserved fputc(0, fp); // reserved fputc(0, fp); // reserved write32le(0, fp); // leave room for length frames write32le(0, fp); // leave room for rerecord count write32le(0, fp); // leave room for movie data size write32le(0, fp); // leave room for savestate_offset write32le(0, fp); // leave room for offset_to_controller_data fwrite(FCEUGameInfo->MD5, 1, 16, fp); // write ROM checksum write32le(FCEU_VERSION_NUMERIC, fp); // write emu version used fputs(FileBase, fp); // write ROM name used fputc(0, fp); if(metadata) { if(strlen(metadata) < MOVIE_MAX_METADATA) fputs(metadata, fp); else fwrite(metadata, 1, MOVIE_MAX_METADATA-1, fp); } fputc(0, fp); // add padding n_padding = (4 - (ftell(fp) & 0x3)) & 0x3; fwrite(padding, 1, n_padding, fp); if(flags & MOVIE_FLAG_FROM_RESET) { if(poweron) { // make a for-movie-recording power-on clear the game's save data, too // (note: FCEU makes a save state immediately after this and that loads that on movie playback) extern char lastLoadedGameName [2048]; extern int disableBatteryLoading, suppressAddPowerCommand; suppressAddPowerCommand=1; disableBatteryLoading=1; suppressMovieStop=1; { // NOTE: this will NOT write an FCEUNPCMD_POWER into the movie file FCEUGI * gi = FCEUI_LoadGame(lastLoadedGameName); if(!gi) PowerNES(); // and neither will this, if it can even happen } suppressMovieStop=0; disableBatteryLoading=0; suppressAddPowerCommand=0; } } savestate_offset = ftell(fp); FCEUSS_SaveFP(fp); fseek(fp, 0, SEEK_END); ResetInputTypes(); // add padding n_padding = (4 - (ftell(fp) & 0x3)) & 0x3; fwrite(padding, 1, n_padding, fp); firstframeoffset = ftell(fp); // finish header fseek(fp, 24, SEEK_SET); // offset_to_savestate offset write32le(savestate_offset, fp); write32le(firstframeoffset, fp); fseek(fp, firstframeoffset, SEEK_SET); // set recording flag current=CurrentMovie; movie_readonly = 0; frameptr = 0; framecount = 0; rerecord_count = 0; slots[current] = fp; memset(joop,0,sizeof(joop)); current++; framets=0; nextd = -1; // trigger a reset if(flags & MOVIE_FLAG_FROM_RESET) { if(poweron) { PowerNES(); // NOTE: this will write an FCEUNPCMD_POWER into the movie file } else ResetNES(); // NOTE: this will write an FCEUNPCMD_RESET into the movie file } if(!fname) FCEUI_SelectMovie(CurrentMovie,1); /* Quick hack to display status. */ else FCEU_DispMessage("Movie recording started."); strcpy(curMovieFilename, origname); }
FCEUFILE * FCEU_fopen(const char *path, const char *ipsfn, char *mode, char *ext) { FILE *ipsfile=0; FCEUFILE *fceufp; void *t; if(strchr(mode,'r')) ipsfile=FCEUD_UTF8fopen(ipsfn,"rb"); fceufp=(FCEUFILE *)malloc(sizeof(FCEUFILE)); { unzFile tz; if((tz=unzOpen(path))) // If it's not a zip file, use regular file handlers. // Assuming file type by extension usually works, // but I don't like it. :) { if(unzGoToFirstFile(tz)==UNZ_OK) { for(;;) { char tempu[512]; // Longer filenames might be possible, but I don't // think people would name files that long in zip files... unzGetCurrentFileInfo(tz,0,tempu,512,0,0,0,0); tempu[511]=0; if(strlen(tempu)>=4) { char *za=tempu+strlen(tempu)-4; if(!ext) { if(!strcasecmp(za,".nes") || !strcasecmp(za,".fds") || !strcasecmp(za,".nsf") || !strcasecmp(za,".unf") || !strcasecmp(za,".nez")) break; } else if(!strcasecmp(za,ext)) break; } if(strlen(tempu)>=5) { if(!strcasecmp(tempu+strlen(tempu)-5,".unif")) break; } if(unzGoToNextFile(tz)!=UNZ_OK) { if(unzGoToFirstFile(tz)!=UNZ_OK) goto zpfail; break; } } if(unzOpenCurrentFile(tz)!=UNZ_OK) goto zpfail; } else { zpfail: free(fceufp); unzClose(tz); return 0; } if(!(fceufp->fp=MakeMemWrap(tz,2))) { free(fceufp); return(0); } fceufp->type=2; if(ipsfile) ApplyIPS(ipsfile,(MEMWRAP *)fceufp->fp); return(fceufp); } } if((t=FCEUD_UTF8fopen(path,"rb"))) { uint32 magic; magic=fgetc((FILE *)t); magic|=fgetc((FILE *)t)<<8; magic|=fgetc((FILE *)t)<<16; if(magic!=0x088b1f) /* Not gzip... */ fclose((FILE *)t); else /* Probably gzip */ { int fd; fd = dup(fileno( (FILE *)t)); fclose(t); lseek(fd, 0, SEEK_SET); if((t=gzdopen(fd,mode))) { fceufp->type=1; fceufp->fp=t; if(ipsfile) { fceufp->fp=MakeMemWrap(t,1); gzclose(t); if(fceufp->fp) { free(fceufp); return(0); } fceufp->type=3; ApplyIPS(ipsfile,(MEMWRAP *)fceufp->fp); } return(fceufp); } close(fd); } } if((t=FCEUD_UTF8fopen(path,mode))) { fseek((FILE *)t,0,SEEK_SET); fceufp->type=0; fceufp->fp=t; if(ipsfile) { if(!(fceufp->fp=MakeMemWrap(t,0))) { free(fceufp); return(0); } fceufp->type=3; ApplyIPS(ipsfile,(MEMWRAP *)fceufp->fp); } return(fceufp); } free(fceufp); return 0; }
static const char* convertToFCM(const char *fname, char *buffer) { #ifdef WIN32 justAutoConverted=0; // convert to fcm if not already const char* dot = strrchr(fname, '.'); if(dot) { int fmv = !stricmp(dot, ".fmv"); int nmv = !stricmp(dot, ".nmv"); int vmv = !stricmp(dot, ".vmv"); if(fmv || nmv || vmv) { strcpy(buffer, fname); buffer[dot-fname]='\0'; strcat(buffer,"-autoconverted.fcm"); int fceuver=0; if(fmv) fceuver=1; else if(nmv) fceuver=2; else if(vmv) fceuver=3; extern char lastLoadedGameName [2048]; char cmd [1024], offset[64], romInfo[1024]; if(movieConvertOK) sprintf(romInfo, "-smd5=\"%s\" -sromname=\"%s (MAYBE)\" -s", lastLoadedGameName, FileBase); else sprintf(romInfo, "-sromname=\"(unknown)\" -s"); if(movieConvertOffset2) sprintf(offset, "-o %d:%d", movieConvertOffset2,movieConvertOffset1); else sprintf(offset, "-o %d", movieConvertOffset1); sprintf(cmd, ".\\util\\nesmock\\nesmock.exe %s %s -spal=%c -sfceuver=%d \"%s\" \"%s\" ", offset, romInfo, FCEUI_GetCurrentVidSystem(0,0)?'1':'0', fceuver, fname, buffer); // FCEU_PrintError(cmd); executeCommand(cmd); FILE* file = FCEUD_UTF8fopen(buffer,"rb"); if(file) { fseek(file, 12, SEEK_SET); int frames=0; read32le(&frames, file); if(frames) { fname = buffer; justAutoConverted=1; } else { static int errAlready=0; if(!errAlready) { errAlready=1; FCEU_PrintError("For some reason, nesmock was unable to create a valid FCM from the given file.\nThe command given was:\n%s\nPerhaps the file specified is not a movie file or contains no input data,\nor perhaps it is a movie file of a version unsupported by nesmock.\n\n(This error message will self-destruct until you restart FCEU.)", cmd); } } fclose(file); } else { char str [512]; str[0] = '\0'; GetCurrentDirectory(512,str); strcat(str, "\\util\\nesmock\\nesmock.exe"); file = FCEUD_UTF8fopen(str, "rb"); if(file) { static int errAlready=0; if(!errAlready) { errAlready=1; FCEU_PrintError("For some reason, nesmock was unable to convert the movie to FCM format.\nThe command given was:\n%s\n\n(This error message will self-destruct until you restart FCEU.)", cmd); fclose(file); } } else { static int errAlready=0; if(!errAlready) { errAlready=1; FCEU_PrintError("Nesmock not found, so the movie could not be converted to FCM format.\nYou must place nesmock.exe at this location so FCEU can find it:\n%s\n\n(This error message will self-destruct until you restart FCEU.)", str); } } } } } #endif return fname; }