📄 fileman.cpp
字号:
switch (fm [t].datastruct) {
case FGLOBAL:
addr = (byte *) fm[t].offset;
break;
case FROMSETTING:
addr = (((byte *) curgs) + fm[t].offset);
break;
case FJOYPADSETTING:
addr = (((byte *) &joykey[curjoy]) + fm[t].offset);
break;
case FCHEAT:
addr = (((byte *) curgs->cheat[curcheat]) + fm[t].offset);
break;
case FWINDOWPOS:
addr = (((byte *) &ws[curwin]) + fm[t].offset);
break;
}
if (fm [t].datatype == FSTRING || fm [t].datatype == FSTRINGALLOC) {
if (l[ep] != '"' || l[ep + strlen(l + ep) - 1] != '"') {
printf ("Warning! Line %d: Missing or misplaced quote mark(s) - Ignoring\n", lline);
printf (" Note! ROM file spec was: %s\n", l + ep);
} else {
//debug0 ("FSTRING %s", l+ep);
delstr (l + ep, 0, 1);
delstr (l + ep, strlen(l + ep) - 1, 1);
//debug0 ("FSTRING trimmed %s", l+ep); //stopped here..
//debug0 ("String %s", l);
if (fm [t].datatype == FSTRINGALLOC) {
//debug0 ("Allocating %d bytes for %s (curcheat=%d)", strlen (l + ep) + 1, l+ep, curcheat);
if (*(char**)addr != NULL) {
debug0 ("Freeing %P!", *(void**)addr);
free (*(void**)addr);
}
*(char **)addr = (char *) malloc (strlen (l + ep) + 1);
//debug0 ("Copied");
strcpy (*(char **) addr, l + ep);
//debug0 ("Copied");
} else
strcpy ((char *) addr, l + ep);
}
} else {
x = atoi (l + ep);
//debug0 ("Setting variable %s to %d (fm[%d])", l, x, t);
switch (fm [t].datatype) {
case FBYTE:
*addr = x;
break;
case FWORD:
*(word *)addr = x;
break;
case FDWORD:
*(dword *)addr = x;
break;
}
}
}
} else {
printf ("Warning! Line %d: Syntax error (Assignment or NEW expected) - Ignoring\n", lline);
printf (" Note! Line was: %s\n", l);
debug0 (" Warning on line %d which was: %s", lline, l);
}
} while (!feof (fp));
fclose (fp);
debug0 ("Exiting loadsettings(). Restoring curgsnum to %d", prevgsnum);
curgsnum = prevgsnum;
curgs = gs[curgsnum];
}
void savetag (FILE *fp, int t, int e = 0, int c = 0)
{
byte *addr = NULL;
fprintf (fp, "%s=", fm[t].tag);
switch (fm[t].datastruct) {
case FGLOBAL:
addr = (byte *) fm[t].offset;
break;
case FROMSETTING:
addr = (((byte *) gs[e]) + fm[t].offset);
break;
case FJOYPADSETTING:
addr = (((byte *) &joykey[e]) + fm[t].offset);
break;
case FCHEAT:
addr = (((byte *) gs[e]->cheat[c]) + fm[t].offset);
break;
case FWINDOWPOS:
addr = (((byte *) &ws[e]) + fm[t].offset);
break;
}
switch (fm[t].datatype) {
case FBYTE:
fprintf (fp, "%d\n", *addr);
break;
case FWORD:
fprintf (fp, "%d\n", *((word *)addr));
break;
case FDWORD:
fprintf (fp, "%d\n", *((dword *)addr));
break;
case FSTRING:
fprintf (fp, "\"%s\"\n", addr);
break;
case FSTRINGALLOC:
fprintf (fp, "\"%s\"\n", *((char **)addr));
break;
}
}
void savesettings (char *filename)
{
FILE *fp;
char full[80];
int x, y, z, prevgsnum = curgsnum;
debug0 ("About to save config file");
strcpy (full, filename);
getabspath (full);
printf ("Saving configuration file: %s\n", full);
if ((fp = fopen (full, "wt")) == NULL) {
debug0 ("Error opening config file (%s) to save!", full);
printf ("Error opening config file (%s) to save! Operation aborted.", full);
return;
}
debug0 ("Open config file to save");
fprintf (fp, ";\n; %s configuration file - automatically generated by %s.\n;\n", PROGRAMNAME, PROGRAMNAME);
fprintf (fp, "\n; Misc. Global definitions\n");
for (y = 0; y < FTAGS; y++) {
fflush (fp);
if (fm[y].datastruct == FGLOBAL)
savetag (fp, y);
}
fprintf (fp, "\n; ROM settings: If the ROM database gets too large (>250 entries),\n; %s will begin to malfunction. If this happens, delete the option sets for a few\n; of your ROMs that you never use. (and don't tell me you play more than 250 games.)\n", PROGRAMNAME);
for (x = 0; x < gsentries; x++) {
curgs = gs[x];
curgsnum = x;
fprintf (fp, "\nNew ROM \"%s\"\n", gs[x]->filename);
fflush (fp);
for (y = 0; y < FTAGS; y++) {
if (fm[y].datastruct == FROMSETTING)
savetag (fp, y, x);
if (fm[y].datastruct == FCHEAT) {
for (z = 0; z < gs[x]->patches; z++) {
fprintf (fp, "New Cheat%s %s\n", curgs->cheat[z]->enabled ? " On" : "", getpatchname (z, false));
savetag (fp, y, x, z);
}
}
fflush (fp);
}
}
fprintf (fp, "\n; Joypad settings: The scancodes for each of the six Joypad configurations.\n");
for (x = 0; x < 6; x++) {
fprintf (fp, "\nNew Joypad %d\n", x);
for (y = 0; y < FTAGS; y++) {
if (fm[y].datastruct == FJOYPADSETTING)
savetag (fp, y, x);
fflush (fp);
}
}
fprintf (fp, "\n; Window positions for the GUI windows.\n");
for (x = 0; x < NUMWINDOWS; x++) {
fprintf (fp, "\nNew Window %d\n", x);
for (y = 0; y < FTAGS; y++) {
if (fm[y].datastruct == FWINDOWPOS)
savetag (fp, y, x);
fflush (fp);
}
}
fprintf (fp, "\n; End of file - finally!\n");
fclose (fp);
curgsnum = prevgsnum;
curgs = gs[curgsnum];
}
void storewinsettings ()
{
int x, y;
//debug0 ("Storing win settings...");
for (y = 0; y < NUMWINDOWS; y++) {
ws [y].zorder = win[y]->zorder;
ws [y].x = win[y]->x;
ws [y].y = win[y]->y;
}
curgs->openwindows = 0;
for (x = 1; x < NUMWINDOWS; x++) {
if (win[x]->zorder >= 0)
curgs->openwindows |= (1 << x);
}
}
void applywinsettings ()
{
int y;
signed char x;
for (y = 0; y < NUMWINDOWS; y++) {
win[y]->x = ws [y].x;
win[y]->y = ws [y].y;
}
for (x = 127; x > -100; x--) {
for (y = 1; y < NUMWINDOWS; y++) {
if (ws [y].zorder == x) {
if ((curgs->openwindows & (1 << y)) && (y != 7 || curgsnum == 0)) {
showwindow (y);
} else {
killwindow (y);
}
}
}
}
}
void loadromheaderdata (char *fn, struct headerdata *h, int forcerom)
{
word c[2];
byte b;
FILE *fp;
debug0 (" Loadromheaderdata entered.");
h->hadd = 512; // assumes there is a 512-byte header
if ((fp = fopen (fn, "rb")) == NULL) {
memset (h, 0, sizeof (struct headerdata));
debug0 ("Failed to open %s to load header data!", fn);
return;
}
fseek (fp, 65472+21+h->hadd, SEEK_SET);
b = (fgetc (fp) & 0x0F);
if (b == 1) {
fseek (fp, 65472+28+h->hadd, SEEK_SET);
fread (c, sizeof (word), 2, fp);
}
if ((b == 1 && (c[0] | c[1]) == 0xFFFF || forcerom == FORCEHIROM) && forcerom != FORCELOROM) {
h->hirom = true;
fseek (fp, 65530+h->hadd, SEEK_SET);
fread (&h->nmivector, 2, 1, fp);
fread (&h->resetvector, 2, 1, fp);
fseek (fp, 65472+h->hadd, SEEK_SET);
} else {
h->hirom = false;
fseek (fp, 32762+h->hadd, SEEK_SET);
fread (&h->nmivector, 2, 1, fp);
fread (&h->resetvector, 2, 1, fp);
fseek (fp, 32704+h->hadd, SEEK_SET);
}
fread (h->name, 21, 1, fp);
if (((b = fgetc (fp)) & 0x30) == 0x30) h->fastrom = true;
else h->fastrom = false;
fgetc (fp); // Ignore 'ROM type' byte
h->romsize = 1 << (fgetc (fp) - 7);
if (h->romsize == 0) h->romsize = 1;
debug0 (" Loaded h->romsize = %d", h->romsize);
h->sramsize = 1 << (fgetc (fp) + 3);
h->country = fgetc (fp);
h->license = fgetc (fp);
h->version = fgetc (fp);
if (h->resetvector == 0) h->resetvector = 0x8000;
fclose (fp);
}
void loadrom (char *fn, boolean gui, int forcerom)
{
int x, c, pos;
FILE *fp;
struct headerdata h;
boolean splitfile = false;
debug0 ("Loadrom() entered. (Current ROM %s; New ROM %s)", curgs->filename, fn);
c = fn [findfirstch (fn, '.') + 1];
if (c == '1' || c == 'A' || c == 'a')
splitfile = true;
if ((fp = fopen (fn, "rb")) == NULL) {
if (gui)
messagebox (fn, "There was an error opening the file!");
else {
printf ("There was an error opening %s!\n", fn);
getch ();
}
return;
}
debug0 (" curgs->filename %s after fopen", curgs->filename);
loadromheaderdata (fn, &h, forcerom);
debug0 (" curgs->filename %s after loadromheaderdata", curgs->filename);
if (gui) {
storewinsettings ();
debug0 (" curgs->filename %s After storewinsettings", curgs->filename);
for (x = 1; x < NUMWINDOWS; x++) {
killwindow (x);
}
mbc [2].hidden = false;
mbc [1].hidden = true;
sprintf (mbc[0].text[0], "%s", fn);
strcpy (messagewindow.title, "Loading, please wait...");
debug0 (" curgs->filename %s After strcpy. h.romsize = %d", curgs->filename, h.romsize);
mbc[2].max = h.romsize;
showwindow (&messagewindow);
debug0 (" curgs->filename %s After showwindow", curgs->filename);
} else {
printf ("Loading ROM: %s\n", fn);
}
debug0 ("Before unloadrom");
unloadcurrentrom ();
debug0 ("got here! after unloadcurrentrom");
if (forcerom == FORCELOROM) rom = (byte *) malloc (3145728); // 3 MB or up to 24 Mbits
else if (forcerom == FORCEHIROM || h.hirom) rom = (byte *) malloc (4194304); // 4 MB or up to 32 Mbits
else rom = (byte *) malloc (3145728); // 3 MB or up to 24 Mbits
romloaded = true;
debug0 ("got here! after malloc");
if (rom == NULL) {
if (gui) {
messagebox ("ROM Load Error", "Out of memory!!!");
} else {
printf ("Out of memory!!! Can't load ROM!!!\n");
getch ();
}
fclose (fp);
return;
}
fseek (fp, h.hadd, SEEK_SET);
debug0 ("got here! after fseek h.hadd");
for (pos = 0; !feof (fp); pos += 32768) {
x = fread (rom + pos, 1, 32768, fp);
if ((x < 32768 || feof (fp)) && splitfile) {
c = findfirstch (fn, '.') + 1;
fn[c]++;
if ((fp = fopen (fn, "rb")) == NULL)
break;
fread (rom + pos + x, 1, 32768 - x, fp);
}
if (pos % (1024*128) == 0) {
debug0 ("Loaded Megabit #%d", pos / (1024*128));
if (gui) {
mbc[2].choice = (pos / (1024*128));
showgui ();
} else {
printf ("Loaded Megabit #%d\n", pos / (1024*128));
}
}
}
fclose (fp);
createromentry (fn);
curheader = h;
debug0 ("Before reset");
resetsystem (false); // not a warm boot - reset everything
loadsram ();
if (gui) {
killwindow (&messagewindow);
debug0 ("b4 applywinsettings");
applywinsettings ();
}
for (x = 0; x < curgs->patches; x++) {
if (curgs->cheat[x]->enabled) {
curgs->cheat[x]->enabled = false;
togglecheat (x); // Get original value... after this call, cheat is toggled on
}
}
debug0 ("Finished loading ROM");
}
void unloadcurrentrom ()
{
if (romloaded) {
savesram ();
resetsystem (true);
free (rom);
dealloc_autobacktrack();
romloaded = false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -