fceugx/source/ngc/preferences.c

399 lines
12 KiB
C
Raw Normal View History

2008-09-02 03:57:21 +02:00
/****************************************************************************
* FCE Ultra 0.98.12
* Nintendo Wii/Gamecube Port
*
* Tantric September 2008
*
* preferences.c
*
* Preferences save/load preferences utilities
****************************************************************************/
#include <gccore.h>
#include <stdio.h>
#include <string.h>
#include <ogcsys.h>
#include <mxml.h>
#include "menudraw.h"
#include "memcardop.h"
#include "fileop.h"
#include "fceugx.h"
2008-09-02 03:57:21 +02:00
#include "pad.h"
extern const unsigned short saveicon[1024];
extern int currconfig[4];
// button map configurations
extern unsigned int gcpadmap[];
extern unsigned int wmpadmap[];
extern unsigned int ccpadmap[];
extern unsigned int ncpadmap[];
char prefscomment[2][32];
/****************************************************************************
* Prepare Preferences Data
*
* This sets up the save buffer for saving.
****************************************************************************/
mxml_node_t *xml;
mxml_node_t *data;
mxml_node_t *section;
mxml_node_t *item;
mxml_node_t *elem;
char temp[200];
const char * toStr(int i)
{
sprintf(temp, "%d", i);
return temp;
}
2008-10-24 07:38:46 +02:00
const char * FtoStr(float i)
{
sprintf(temp, "%.2f", i);
return temp;
}
2008-09-02 03:57:21 +02:00
void createXMLSection(const char * name, const char * description)
{
section = mxmlNewElement(data, "section");
mxmlElementSetAttr(section, "name", name);
mxmlElementSetAttr(section, "description", description);
}
void createXMLSetting(const char * name, const char * description, const char * value)
{
item = mxmlNewElement(section, "setting");
mxmlElementSetAttr(item, "name", name);
mxmlElementSetAttr(item, "value", value);
mxmlElementSetAttr(item, "description", description);
}
void createXMLController(unsigned int controller[], const char * name, const char * description)
{
item = mxmlNewElement(section, "controller");
mxmlElementSetAttr(item, "name", name);
mxmlElementSetAttr(item, "description", description);
// create buttons
int i;
for(i=0; i < MAXJP; i++)
{
elem = mxmlNewElement(item, "button");
mxmlElementSetAttr(elem, "number", toStr(i));
mxmlElementSetAttr(elem, "assignment", toStr(controller[i]));
}
}
const char * XMLSaveCallback(mxml_node_t *node, int where)
{
const char *name;
name = node->value.element.name;
if(where == MXML_WS_BEFORE_CLOSE)
{
if(!strcmp(name, "file") || !strcmp(name, "section"))
return ("\n");
else if(!strcmp(name, "controller"))
return ("\n\t");
}
if (where == MXML_WS_BEFORE_OPEN)
{
if(!strcmp(name, "file"))
return ("\n");
else if(!strcmp(name, "section"))
return ("\n\n");
else if(!strcmp(name, "setting") || !strcmp(name, "controller"))
return ("\n\t");
else if(!strcmp(name, "button"))
return ("\n\t\t");
}
return (NULL);
}
int
preparePrefsData (int method)
{
int offset = 0;
memset (savebuffer, 0, SAVEBUFFERSIZE);
// add save icon and comments for Memory Card saves
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
offset = sizeof (saveicon);
// Copy in save icon
memcpy (savebuffer, saveicon, offset);
// And the comments
sprintf (prefscomment[0], "%s Prefs", VERSIONSTR);
sprintf (prefscomment[1], "Preferences");
memcpy (savebuffer + offset, prefscomment, 64);
offset += 64;
}
xml = mxmlNewXML("1.0");
mxmlSetWrapMargin(0); // disable line wrapping
data = mxmlNewElement(xml, "file");
mxmlElementSetAttr(data, "version",VERSIONSTR);
createXMLSection("File", "File Settings");
createXMLSetting("AutoLoad", "Auto Load", toStr(GCSettings.AutoLoad));
createXMLSetting("AutoSave", "Auto Save", toStr(GCSettings.AutoSave));
createXMLSetting("LoadMethod", "Load Method", toStr(GCSettings.LoadMethod));
createXMLSetting("SaveMethod", "Save Method", toStr(GCSettings.SaveMethod));
createXMLSetting("LoadFolder", "Load Folder", GCSettings.LoadFolder);
createXMLSetting("SaveFolder", "Save Folder", GCSettings.SaveFolder);
createXMLSetting("CheatFolder", "Cheats Folder", GCSettings.CheatFolder);
createXMLSetting("VerifySaves", "Verify Memory Card Saves", toStr(GCSettings.VerifySaves));
createXMLSection("Network", "Network Settings");
createXMLSetting("smbip", "Share Computer IP", GCSettings.smbip);
createXMLSetting("smbshare", "Share Name", GCSettings.smbshare);
createXMLSetting("smbuser", "Share Username", GCSettings.smbuser);
createXMLSetting("smbpwd", "Share Password", GCSettings.smbpwd);
createXMLSection("Emulation", "Emulation Settings");
createXMLSetting("currpal", "Palette", toStr(GCSettings.currpal));
createXMLSetting("timing", "Timing", toStr(GCSettings.timing));
createXMLSetting("slimit", "8 Sprite Limit", toStr(GCSettings.slimit));
2008-10-24 07:38:46 +02:00
createXMLSetting("Zoom", "Zoom On/Off", toStr(GCSettings.Zoom));
createXMLSetting("ZoomLevel", "Zoom Level", FtoStr(GCSettings.ZoomLevel));
createXMLSetting("render", "Video Filtering", toStr(GCSettings.render));
createXMLSetting("widescreen", "Aspect Ratio Correction", toStr(GCSettings.widescreen));
2008-11-20 17:58:44 +01:00
createXMLSetting("hideoverscan", "Video Cropping", toStr(GCSettings.hideoverscan));
2008-09-02 03:57:21 +02:00
createXMLSection("Controller", "Controller Settings");
2008-11-24 08:33:57 +01:00
createXMLSetting("FourScore", "Four Score", toStr(GCSettings.FourScore));
createXMLSetting("zapper", "Zapper", toStr(GCSettings.zapper));
2008-10-02 01:10:30 +02:00
createXMLSetting("crosshair", "Zapper Crosshair", toStr(GCSettings.crosshair));
2008-09-02 03:57:21 +02:00
createXMLController(gcpadmap, "gcpadmap", "GameCube Pad");
createXMLController(wmpadmap, "wmpadmap", "Wiimote");
createXMLController(ccpadmap, "ccpadmap", "Classic Controller");
createXMLController(ncpadmap, "ncpadmap", "Nunchuk");
int datasize = mxmlSaveString(xml, (char *)savebuffer, SAVEBUFFERSIZE, XMLSaveCallback);
mxmlDelete(xml);
return datasize;
}
/****************************************************************************
* Decode Preferences Data
****************************************************************************/
void loadXMLSettingStr(char * var, const char * name)
{
item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND);
if(item)
sprintf(var, "%s", mxmlElementGetAttr(item, "value"));
}
void loadXMLSettingInt(int * var, const char * name)
{
item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND);
if(item)
*var = atoi(mxmlElementGetAttr(item, "value"));
}
2008-10-24 07:38:46 +02:00
void loadXMLSettingFloat(float * var, const char * name)
{
item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND);
if(item)
*var = atof(mxmlElementGetAttr(item, "value"));
}
2008-09-02 03:57:21 +02:00
void loadXMLSettingBool(bool * var, const char * name)
{
item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND);
if(item)
*var = atoi(mxmlElementGetAttr(item, "value"));
}
void loadXMLController(unsigned int controller[], const char * name)
2008-09-02 03:57:21 +02:00
{
item = mxmlFindElement(xml, xml, "controller", "name", name, MXML_DESCEND);
if(item)
{
// populate buttons
int i;
for(i=0; i < MAXJP; i++)
{
elem = mxmlFindElement(item, xml, "button", "number", toStr(i), MXML_DESCEND);
if(elem)
controller[i] = atoi(mxmlElementGetAttr(elem, "assignment"));
}
}
}
bool
decodePrefsData (int method)
{
int offset = 0;
// skip save icon and comments for Memory Card saves
if(method == METHOD_MC_SLOTA || method == METHOD_MC_SLOTB)
{
offset = sizeof (saveicon);
offset += 64; // sizeof prefscomment
}
xml = mxmlLoadString(NULL, (char *)savebuffer+offset, MXML_TEXT_CALLBACK);
// check settings version
// we don't do anything with the version #, but we'll store it anyway
char * version;
item = mxmlFindElement(xml, xml, "file", "version", NULL, MXML_DESCEND);
if(item) // a version entry exists
version = (char *)mxmlElementGetAttr(item, "version");
else // version # not found, must be invalid
return false;
2008-09-20 03:05:33 +02:00
// this code assumes version in format X.X.X
// XX.X.X, X.XX.X, or X.X.XX will NOT work
char verMajor = version[13];
char verMinor = version[15];
char verPoint = version[17];
2008-09-19 23:24:31 +02:00
2008-11-19 08:11:06 +01:00
if(verMajor == '2' && verPoint < '7') // less than version 2.0.7
2008-09-19 23:24:31 +02:00
return false; // reset settings
2008-11-19 08:11:06 +01:00
else if(verMajor > '2' || verMinor > '0' || verPoint > '7') // some future version
2008-09-19 23:24:31 +02:00
return false; // reset settings
2008-09-02 03:57:21 +02:00
// File Settings
loadXMLSettingInt(&GCSettings.AutoLoad, "AutoLoad");
loadXMLSettingInt(&GCSettings.AutoSave, "AutoSave");
loadXMLSettingInt(&GCSettings.LoadMethod, "LoadMethod");
loadXMLSettingInt(&GCSettings.SaveMethod, "SaveMethod");
loadXMLSettingStr(GCSettings.LoadFolder, "LoadFolder");
loadXMLSettingStr(GCSettings.SaveFolder, "SaveFolder");
loadXMLSettingStr(GCSettings.CheatFolder, "CheatFolder");
loadXMLSettingInt(&GCSettings.VerifySaves, "VerifySaves");
// Network Settings
loadXMLSettingStr(GCSettings.smbip, "smbip");
loadXMLSettingStr(GCSettings.smbshare, "smbshare");
loadXMLSettingStr(GCSettings.smbuser, "smbuser");
loadXMLSettingStr(GCSettings.smbpwd, "smbpwd");
// Emulation Settings
loadXMLSettingInt(&GCSettings.currpal, "currpal");
loadXMLSettingInt(&GCSettings.timing, "timing");
loadXMLSettingInt(&GCSettings.slimit, "slimit");
2008-10-24 07:38:46 +02:00
loadXMLSettingInt(&GCSettings.Zoom, "Zoom");
loadXMLSettingFloat(&GCSettings.ZoomLevel, "ZoomLevel");
loadXMLSettingInt(&GCSettings.render, "render");
loadXMLSettingInt(&GCSettings.widescreen, "widescreen");
2008-11-20 17:58:44 +01:00
loadXMLSettingInt(&GCSettings.hideoverscan, "hideoverscan");
// Controller Settings
2008-11-24 08:33:57 +01:00
loadXMLSettingInt(&GCSettings.FourScore, "FSDisable");
loadXMLSettingInt(&GCSettings.zapper, "zapper");
2008-10-02 01:10:30 +02:00
loadXMLSettingInt(&GCSettings.crosshair, "crosshair");
2008-09-02 03:57:21 +02:00
loadXMLController(gcpadmap, "gcpadmap");
loadXMLController(wmpadmap, "wmpadmap");
loadXMLController(ccpadmap, "ccpadmap");
loadXMLController(ncpadmap, "ncpadmap");
mxmlDelete(xml);
return true;
}
/****************************************************************************
* Save Preferences
***************************************************************************/
2008-09-02 03:57:21 +02:00
bool
SavePrefs (bool silent)
2008-09-02 03:57:21 +02:00
{
2008-11-12 09:40:09 +01:00
char filepath[1024];
int datasize;
int offset = 0;
// We'll save using the first available method (probably SD) since this
// is the method preferences will be loaded from by default
int method = autoSaveMethod(silent);
2008-09-02 03:57:21 +02:00
2008-11-12 09:40:09 +01:00
if(!MakeFilePath(filepath, FILE_PREF, method))
return false;
2008-09-02 03:57:21 +02:00
if (!silent)
ShowAction ("Saving preferences...");
2008-09-02 03:57:21 +02:00
AllocSaveBuffer ();
2008-11-12 09:40:09 +01:00
datasize = preparePrefsData (method);
offset = SaveFile(filepath, datasize, method, silent);
2008-09-02 03:57:21 +02:00
FreeSaveBuffer ();
2008-09-02 03:57:21 +02:00
if (offset > 0)
{
if (!silent)
WaitPrompt ("Preferences saved");
2008-09-02 03:57:21 +02:00
return true;
}
return false;
}
/****************************************************************************
* Load Preferences from specified method
***************************************************************************/
2008-09-02 03:57:21 +02:00
bool
LoadPrefsFromMethod (int method)
2008-09-02 03:57:21 +02:00
{
bool retval = false;
char filepath[1024];
int offset = 0;
2008-11-12 09:40:09 +01:00
if(!MakeFilePath(filepath, FILE_PREF, method))
return false;
AllocSaveBuffer ();
2008-11-12 09:40:09 +01:00
offset = LoadFile(filepath, method, SILENT);
2008-09-02 03:57:21 +02:00
if (offset > 0)
retval = decodePrefsData (method);
FreeSaveBuffer ();
2008-09-02 03:57:21 +02:00
return retval;
}
/****************************************************************************
* Load Preferences
* Checks sources consecutively until we find a preference file
***************************************************************************/
bool LoadPrefs()
{
ShowAction ("Loading preferences...");
bool prefFound = false;
if(ChangeInterface(METHOD_SD, SILENT))
prefFound = LoadPrefsFromMethod(METHOD_SD);
if(!prefFound && ChangeInterface(METHOD_USB, SILENT))
prefFound = LoadPrefsFromMethod(METHOD_USB);
if(!prefFound && TestCard(CARD_SLOTA, SILENT))
prefFound = LoadPrefsFromMethod(METHOD_MC_SLOTA);
if(!prefFound && TestCard(CARD_SLOTB, SILENT))
prefFound = LoadPrefsFromMethod(METHOD_MC_SLOTB);
if(!prefFound && ChangeInterface(METHOD_SMB, SILENT))
prefFound = LoadPrefsFromMethod(METHOD_SMB);
return prefFound;
}