/**************************************************************************** * Snes9x 1.51 Nintendo Wii/Gamecube Port * * Tantric 2008-2009 * * preferences.cpp * * Preferences save/load to XML file ***************************************************************************/ #include #include #include #include #include #include "snes9xGX.h" #include "s9xconfig.h" #include "menu.h" #include "memcardop.h" #include "fileop.h" #include "filebrowser.h" #include "input.h" #include "button_mapping.h" /**************************************************************************** * Prepare Preferences Data * * This sets up the save buffer for saving. ***************************************************************************/ static mxml_node_t *xml = NULL; static mxml_node_t *data = NULL; static mxml_node_t *section = NULL; static mxml_node_t *item = NULL; static mxml_node_t *elem = NULL; static char temp[200]; static const char * toStr(int i) { sprintf(temp, "%d", i); return temp; } static const char * FtoStr(float i) { sprintf(temp, "%.2f", i); return temp; } static void createXMLSection(const char * name, const char * description) { section = mxmlNewElement(data, "section"); mxmlElementSetAttr(section, "name", name); mxmlElementSetAttr(section, "description", description); } static 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); } static 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 for(int i=0; i < MAXJP; i++) { elem = mxmlNewElement(item, "button"); mxmlElementSetAttr(elem, "number", toStr(i)); mxmlElementSetAttr(elem, "assignment", toStr(controller[i])); } } static 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); } static int preparePrefsData () { xml = mxmlNewXML("1.0"); mxmlSetWrapMargin(0); // disable line wrapping data = mxmlNewElement(xml, "file"); mxmlElementSetAttr(data, "app", APPNAME); mxmlElementSetAttr(data, "version", APPVERSION); 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("Video", "Video Settings"); createXMLSetting("videomode", "Video Mode", toStr(GCSettings.videomode)); createXMLSetting("ZoomLevel", "Zoom Level", FtoStr(GCSettings.ZoomLevel)); createXMLSetting("render", "Video Filtering", toStr(GCSettings.render)); createXMLSetting("widescreen", "Aspect Ratio Correction", toStr(GCSettings.widescreen)); createXMLSetting("FilterMethod", "Filter Method", toStr(GCSettings.FilterMethod)); createXMLSetting("xshift", "Horizontal Video Shift", toStr(GCSettings.xshift)); createXMLSetting("yshift", "Vertical Video Shift", toStr(GCSettings.yshift)); createXMLSection("Menu", "Menu Settings"); createXMLSetting("WiimoteOrientation", "Wiimote Orientation", toStr(GCSettings.WiimoteOrientation)); createXMLSetting("ExitAction", "Exit Action", toStr(GCSettings.ExitAction)); createXMLSetting("MusicVolume", "Music Volume", toStr(GCSettings.MusicVolume)); createXMLSetting("SFXVolume", "Sound Effects Volume", toStr(GCSettings.SFXVolume)); createXMLSetting("Rumble", "Rumble", toStr(GCSettings.Rumble)); createXMLSection("Controller", "Controller Settings"); createXMLSetting("Controller", "Controller", toStr(GCSettings.Controller)); createXMLController(btnmap[CTRL_PAD][CTRLR_GCPAD], "btnmap_pad_gcpad", "SNES Pad - GameCube Controller"); createXMLController(btnmap[CTRL_PAD][CTRLR_WIIMOTE], "btnmap_pad_wiimote", "SNES Pad - Wiimote"); createXMLController(btnmap[CTRL_PAD][CTRLR_CLASSIC], "btnmap_pad_classic", "SNES Pad - Classic Controller"); createXMLController(btnmap[CTRL_PAD][CTRLR_NUNCHUK], "btnmap_pad_nunchuk", "SNES Pad - Nunchuk + Wiimote"); createXMLController(btnmap[CTRL_SCOPE][CTRLR_GCPAD], "btnmap_scope_gcpad", "Superscope - GameCube Controller"); createXMLController(btnmap[CTRL_SCOPE][CTRLR_WIIMOTE], "btnmap_scope_wiimote", "Superscope - Wiimote"); createXMLController(btnmap[CTRL_MOUSE][CTRLR_GCPAD], "btnmap_mouse_gcpad", "Mouse - GameCube Controller"); createXMLController(btnmap[CTRL_MOUSE][CTRLR_WIIMOTE], "btnmap_mouse_wiimote", "Mouse - Wiimote"); createXMLController(btnmap[CTRL_JUST][CTRLR_GCPAD], "btnmap_just_gcpad", "Justifier - GameCube Controller"); createXMLController(btnmap[CTRL_JUST][CTRLR_WIIMOTE], "btnmap_just_wiimote", "Justifier - Wiimote"); int datasize = mxmlSaveString(xml, (char *)savebuffer, SAVEBUFFERSIZE, XMLSaveCallback); mxmlDelete(xml); return datasize; } /**************************************************************************** * loadXMLSetting * * Load XML elements into variables for an individual variable ***************************************************************************/ static void loadXMLSetting(char * var, const char * name, int maxsize) { item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND); if(item) { const char * tmp = mxmlElementGetAttr(item, "value"); if(tmp) snprintf(var, maxsize, "%s", tmp); } } static void loadXMLSetting(int * var, const char * name) { item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND); if(item) { const char * tmp = mxmlElementGetAttr(item, "value"); if(tmp) *var = atoi(tmp); } } static void loadXMLSetting(float * var, const char * name) { item = mxmlFindElement(xml, xml, "setting", "name", name, MXML_DESCEND); if(item) { const char * tmp = mxmlElementGetAttr(item, "value"); if(tmp) *var = atof(tmp); } } /**************************************************************************** * loadXMLController * * Load XML elements into variables for a controller mapping ***************************************************************************/ static void loadXMLController(unsigned int controller[], const char * name) { item = mxmlFindElement(xml, xml, "controller", "name", name, MXML_DESCEND); if(item) { // populate buttons for(int i=0; i < MAXJP; i++) { elem = mxmlFindElement(item, xml, "button", "number", toStr(i), MXML_DESCEND); if(elem) { const char * tmp = mxmlElementGetAttr(elem, "assignment"); if(tmp) controller[i] = atoi(tmp); } } } } /**************************************************************************** * decodePrefsData * * Decodes preferences - parses XML and loads preferences into the variables ***************************************************************************/ static bool decodePrefsData () { bool result = false; xml = mxmlLoadString(NULL, (char *)savebuffer, MXML_TEXT_CALLBACK); if(xml) { // check settings version item = mxmlFindElement(xml, xml, "file", "version", NULL, MXML_DESCEND); if(item) // a version entry exists { const char * version = mxmlElementGetAttr(item, "version"); if(version && strlen(version) == 5) { // this code assumes version in format X.X.X // XX.X.X, X.XX.X, or X.X.XX will NOT work int verMajor = version[0] - '0'; int verMinor = version[2] - '0'; int verPoint = version[4] - '0'; int curMajor = APPVERSION[0] - '0'; int curMinor = APPVERSION[2] - '0'; int curPoint = APPVERSION[4] - '0'; // first we'll check that the versioning is valid if(!(verMajor >= 0 && verMajor <= 9 && verMinor >= 0 && verMinor <= 9 && verPoint >= 0 && verPoint <= 9)) result = false; else if(verMajor < 4) // less than version 4.0.0 result = false; // reset settings else if(verMajor == 4 && verMinor == 0 && verPoint < 2) // anything less than 4.0.2 result = false; // reset settings else if(verMajor > curMajor || verMinor > curMinor || verPoint > curPoint) // some future version result = false; // reset settings else result = true; } } if(result) { // File Settings loadXMLSetting(&GCSettings.AutoLoad, "AutoLoad"); loadXMLSetting(&GCSettings.AutoSave, "AutoSave"); loadXMLSetting(&GCSettings.LoadMethod, "LoadMethod"); loadXMLSetting(&GCSettings.SaveMethod, "SaveMethod"); loadXMLSetting(GCSettings.LoadFolder, "LoadFolder", sizeof(GCSettings.LoadFolder)); loadXMLSetting(GCSettings.SaveFolder, "SaveFolder", sizeof(GCSettings.SaveFolder)); loadXMLSetting(GCSettings.CheatFolder, "CheatFolder", sizeof(GCSettings.CheatFolder)); loadXMLSetting(&GCSettings.VerifySaves, "VerifySaves"); // Network Settings loadXMLSetting(GCSettings.smbip, "smbip", sizeof(GCSettings.smbip)); loadXMLSetting(GCSettings.smbshare, "smbshare", sizeof(GCSettings.smbshare)); loadXMLSetting(GCSettings.smbuser, "smbuser", sizeof(GCSettings.smbuser)); loadXMLSetting(GCSettings.smbpwd, "smbpwd", sizeof(GCSettings.smbpwd)); // Video Settings loadXMLSetting(&GCSettings.videomode, "videomode"); loadXMLSetting(&GCSettings.ZoomLevel, "ZoomLevel"); loadXMLSetting(&GCSettings.render, "render"); loadXMLSetting(&GCSettings.widescreen, "widescreen"); loadXMLSetting(&GCSettings.FilterMethod, "FilterMethod"); loadXMLSetting(&GCSettings.xshift, "xshift"); loadXMLSetting(&GCSettings.yshift, "yshift"); // Menu Settings loadXMLSetting(&GCSettings.WiimoteOrientation, "WiimoteOrientation"); loadXMLSetting(&GCSettings.ExitAction, "ExitAction"); loadXMLSetting(&GCSettings.MusicVolume, "MusicVolume"); loadXMLSetting(&GCSettings.SFXVolume, "SFXVolume"); loadXMLSetting(&GCSettings.Rumble, "Rumble"); // Controller Settings loadXMLSetting(&GCSettings.Controller, "Controller"); loadXMLController(btnmap[CTRL_PAD][CTRLR_GCPAD], "btnmap_pad_gcpad"); loadXMLController(btnmap[CTRL_PAD][CTRLR_WIIMOTE], "btnmap_pad_wiimote"); loadXMLController(btnmap[CTRL_PAD][CTRLR_CLASSIC], "btnmap_pad_classic"); loadXMLController(btnmap[CTRL_PAD][CTRLR_NUNCHUK], "btnmap_pad_nunchuk"); loadXMLController(btnmap[CTRL_SCOPE][CTRLR_GCPAD], "btnmap_scope_gcpad"); loadXMLController(btnmap[CTRL_SCOPE][CTRLR_WIIMOTE], "btnmap_scope_wiimote"); loadXMLController(btnmap[CTRL_MOUSE][CTRLR_GCPAD], "btnmap_mouse_gcpad"); loadXMLController(btnmap[CTRL_MOUSE][CTRLR_WIIMOTE], "btnmap_mouse_wiimote"); loadXMLController(btnmap[CTRL_JUST][CTRLR_GCPAD], "btnmap_just_gcpad"); loadXMLController(btnmap[CTRL_JUST][CTRLR_WIIMOTE], "btnmap_just_wiimote"); } mxmlDelete(xml); } return result; } /**************************************************************************** * Save Preferences ***************************************************************************/ static char prefpath[MAXPATHLEN] = { 0 }; bool SavePrefs (bool silent) { char filepath[MAXPATHLEN]; int datasize; int offset = 0; int device = 0; if(prefpath[0] != 0) { strcpy(filepath, prefpath); FindDevice(filepath, &device); } else if(appPath[0] != 0) { sprintf(filepath, "%s%s", appPath, PREF_FILE_NAME); FindDevice(filepath, &device); } else { device = autoLoadMethod(); if(device == 0) return false; if(device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB) sprintf(filepath, "%s%s", pathPrefix[device], PREF_FILE_NAME); else sprintf(filepath, "%ssnes9x/%s", pathPrefix[device], PREF_FILE_NAME); } if(device == 0) return false; if (!silent) ShowAction ("Saving preferences..."); FixInvalidSettings(); AllocSaveBuffer (); datasize = preparePrefsData (); if(device == DEVICE_MC_SLOTA || device == DEVICE_MC_SLOTB) { // Set the comments char prefscomment[2][32]; memset(prefscomment, 0, 64); sprintf (prefscomment[0], "%s Prefs", APPNAME); sprintf (prefscomment[1], "Preferences"); SetMCSaveComments(prefscomment); } offset = SaveFile(filepath, datasize, silent); FreeSaveBuffer (); CancelAction(); if (offset > 0) { if (!silent) InfoPrompt("Preferences saved"); return true; } return false; } /**************************************************************************** * Load Preferences from specified filepath ***************************************************************************/ bool LoadPrefsFromMethod (char * filepath) { bool retval = false; int offset = 0; AllocSaveBuffer (); offset = LoadFile(filepath, SILENT); if (offset > 0) retval = decodePrefsData (); FreeSaveBuffer (); if(retval) { strcpy(prefpath, filepath); } return retval; } /**************************************************************************** * Load Preferences * Checks sources consecutively until we find a preference file ***************************************************************************/ static bool prefLoaded = false; bool LoadPrefs() { if(prefLoaded) // already attempted loading return true; bool prefFound = false; char filepath[4][MAXPATHLEN]; int numDevices; #ifdef HW_RVL numDevices = 3; sprintf(filepath[0], "%s/%s", appPath, PREF_FILE_NAME); sprintf(filepath[1], "sd:/%s/%s", APPFOLDER, PREF_FILE_NAME); sprintf(filepath[2], "usb:/%s/%s", APPFOLDER, PREF_FILE_NAME); #else numDevices = 4; sprintf(filepath[0], "carda:/%s/%s", APPFOLDER, PREF_FILE_NAME); sprintf(filepath[1], "cardb:/%s/%s", APPFOLDER, PREF_FILE_NAME); sprintf(filepath[2], "mca:/%s", PREF_FILE_NAME); sprintf(filepath[3], "mcb:/%s", PREF_FILE_NAME); #endif for(int i=0; i