// DIP Switches void dip_handle() { // Handle the DIP switch file DipSwitches dipswitches(emulator); Xml xml; char dippath[512]; snprintf(dippath, sizeof(dippath), "%s%s.dip", nstpaths.savedir, nstpaths.gamename); std::ifstream dipfile(dippath, std::ifstream::in|std::ifstream::binary); if (dipfile.is_open()) { xml.Read(dipfile); if (xml.GetRoot().IsType(L"dipswitches")) { Xml::Node root(xml.GetRoot()); Xml::Node node(root.GetFirstChild()); for (int i = 0; i < root.NumChildren(L"dip"); i++) { if (node.GetChild(L"value")) { dipswitches.SetValue(i, node.GetChild(L"value").GetUnsignedValue()); } node = node.GetNextSibling(); } } dipfile.close(); } else { Xml::Node root(xml.GetRoot()); root = xml.Create(L"dipswitches"); root.AddAttribute(L"version", L"1.0"); wchar_t wbuf[32]; char buf[2]; int numdips = dipswitches.NumDips(); if (numdips > 0) { for (int i = 0; i < numdips; i++) { Xml::Node node(root.AddChild(L"dip")); mbstowcs(wbuf, dipswitches.GetDipName(i), sizeof(wbuf)); node.AddChild(L"description", wbuf); snprintf(buf, sizeof(buf), "%d", dipswitches.GetValue(i)); mbstowcs(wbuf, buf, sizeof(buf)); node.AddChild(L"value", wbuf); } } std::ofstream dipout(dippath, std::ifstream::out|std::ifstream::binary); if (dipout.is_open()) { xml.Write(root, dipout); } dipout.close(); } }
void Netplay::LoadFile() { try { //const Path path( Application::Instance::GetExePath(L"netplaylist.xml") ); const Path path( Application::Instance::GetConfigPath(L"netplaylist.xml") );//bg if (path.FileExists()) { typedef Nes::Core::Xml Xml; Xml xml; { Io::Stream::In stream( path ); xml.Read( stream ); } if (!xml.GetRoot().IsType( L"netplaylist" )) throw 1; for (Xml::Node node(xml.GetRoot().GetFirstChild()); node; node=node.GetNextSibling()) { if (!node.IsType( L"file" )) throw 1; Add( node.GetValue() ); } Io::Log() << "Netplay: loaded game list from \"netplaylist.xml\"\r\n"; } else { Io::Log() << "Netplay: game list file \"netplaylist.xml\" not present..\r\n"; } } catch (...) { games.state = Games::DIRTY; Io::Log() << "Netplay: warning, couldn't load game list \"netplaylist.xml\"!\r\n"; } }
void cheats_init() { // Initialize cheat engine Cheats cheats(emulator); Xml xml; cheats.ClearCodes(); std::ifstream cheatfile(nstpaths.cheatpath, std::ifstream::in|std::ifstream::binary); if (cheatfile.is_open()) { xml.Read(cheatfile); if (xml.GetRoot().IsType(L"cheats")) { Xml::Node root(xml.GetRoot()); Xml::Node node(root.GetFirstChild()); for (int i = 0; i < root.NumChildren(L"cheat"); i++) { if (node.GetAttribute(L"enabled").IsValue(L"1")) { if (node.GetChild(L"genie")) { // Game Genie cheats_code_gg_add(node.GetChild(L"genie").GetValue()); } else if (node.GetChild(L"rocky")) { // Pro Action Rocky cheats_code_par_add(node.GetChild(L"rocky").GetValue()); } else if (node.GetChild(L"address")) { // Raw cheats_code_raw_add(node); } //fprintf(stderr, "Cheat: %ls\n", node.GetChild(L"description").GetValue()); } node = node.GetNextSibling(); } } cheatfile.close(); } }
void gtkui_cheats_save() { // Save the cheat list std::ofstream cheatfile(nstpaths.cheatpath, std::ifstream::out|std::ifstream::binary); if (cheatfile.is_open()) { GtkTreeModel *model; model = gtk_tree_view_get_model(GTK_TREE_VIEW(treeview)); saveroot = (savexml.GetRoot()); saveroot = savexml.Create( L"cheats" ); saveroot.AddAttribute( L"version", L"1.0" ); gtk_tree_model_foreach(GTK_TREE_MODEL(model), gtkui_cheats_write_list, NULL); savexml.Write(saveroot, cheatfile); cheatfile.close(); } else { return; } }
void on_cheatsave_clicked(GtkButton *button, gpointer user_data) { GtkWidget *dialog; char defname[512]; Cheats cheats(emulator); // nothing to export? if (cheats.NumCodes() == 0) { return; } defname[0] = '\0'; strcpy(defname, rootname); strcat(defname, ".xml"); dialog = gtk_file_chooser_dialog_new ("Export cheats (.xml)", GTK_WINDOW(mainwindow), GTK_FILE_CHOOSER_ACTION_SAVE, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL); gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), defname); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); std::ofstream stream( filename, std::ifstream::out|std::ifstream::binary ); if (stream.is_open()) { int i; Xml xml; Xml::Node root(xml.GetRoot()); root = xml.Create( L"cheats" ); root.AddAttribute( L"version", L"1.0" ); for (i = 0; i < cheatlist.size(); i++) { Cheats::Code code; char buffer[9]; wchar_t wbuffer[64]; code = cheatlist[i].code; Xml::Node node( root.AddChild( L"cheat" ) ); node.AddAttribute( L"enabled", cheatlist[i].enabled ? L"1" : L"0" ); if (cheatlist[i].crc) { sprintf(buffer, "%08X", cheatlist[i].crc); mbstowcs(wbuffer, buffer, 63); node.AddAttribute( L"game", wbuffer ); } if (NES_SUCCEEDED(cheats.GameGenieEncode(code, buffer))) { mbstowcs(wbuffer, buffer, 63); node.AddChild( L"genie", wbuffer ); } if (NES_SUCCEEDED(cheats.ProActionRockyEncode(code, buffer))) { mbstowcs(wbuffer, buffer, 63); node.AddChild( L"rocky", wbuffer ); } sprintf(buffer, "0x%04X", code.address); mbstowcs(wbuffer, buffer, 63); node.AddChild( L"address", wbuffer ); sprintf(buffer, "0x%02X", code.value); mbstowcs(wbuffer, buffer, 63); node.AddChild( L"value", wbuffer ); if (code.useCompare) { sprintf(buffer, "0x%02X", code.compare); mbstowcs(wbuffer, buffer, 63); node.AddChild( L"compare", wbuffer ); } if (strlen(cheatlist[i].description)) { mbstowcs(wbuffer, cheatlist[i].description, 63); node.AddChild( L"description", wbuffer ); } } xml.Write( root, stream ); } g_free (filename); } gtk_widget_destroy(dialog); }
void on_cheatopen_clicked(GtkButton *button, gpointer user_data) { GtkWidget *dialog; GtkFileFilter *filter; Xml xml; Cheats cheats(emulator); CheatMgr *cmgr = (CheatMgr *)user_data; char description[64]; dialog = gtk_file_chooser_dialog_new ("Import cheats (.xml)", GTK_WINDOW(mainwindow), GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL); filter = gtk_file_filter_new(); gtk_file_filter_set_name(filter, "NEStopia XML cheats"); gtk_file_filter_add_pattern(filter, "*.xml"); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { char *filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog)); std::ifstream stream( filename, std::ifstream::in|std::ifstream::binary ); if (stream.is_open()) { xml.Read( stream ); if (xml.GetRoot().IsType( L"cheats" )) { for (Xml::Node node(xml.GetRoot().GetFirstChild()); node; node=node.GetNextSibling()) { if (!node.IsType( L"cheat" )) continue; Cheats::Code code; code.useCompare = false; if (const Xml::Node address=node.GetChild( L"address" )) { uint v; if (0xFFFF < (v=address.GetUnsignedValue())) continue; code.address = v; if (const Xml::Node value=node.GetChild( L"value" )) { if (0xFF < (v=value.GetUnsignedValue())) continue; code.value = v; } if (const Xml::Node compare=node.GetChild( L"compare" )) { if (0xFF < (v=compare.GetUnsignedValue())) continue; code.compare = v; code.useCompare = true; } } else { char codestr[512]; if (const Xml::Node genie=node.GetChild( L"genie" )) { wcstombs(codestr, genie.GetValue(), 511); if (cheats.GameGenieDecode(codestr, code) != Nes::RESULT_OK) { continue; } } else if (const Xml::Node rocky=node.GetChild( L"rocky" )) { wcstombs(codestr, rocky.GetValue(), 511); if (cheats.ProActionRockyDecode(codestr, code) != Nes::RESULT_OK) { continue; } } else { continue; } } // add the code, with a description if it's got one if (node.GetChild( L"description" )) { wcstombs(description, node.GetChild( L"description" ).GetValue(), 63); cmgr->AddCode(code, true, node.GetAttribute( L"enabled" ).IsValue( L"0" ) ? false : true, description); } else { cmgr->AddCode(code, true, node.GetAttribute( L"enabled" ).IsValue( L"0" ) ? false : true); } // poke in the CRC too if there is one cheatlist[cheatlist.size()-1].crc = node.GetAttribute( L"game" ).GetUnsignedValue(); } } } g_free (filename); } gtk_widget_destroy(dialog); }
void gtkui_cheats_fill_tree(char *filename) { // Fill the cheat list Xml xml; GtkTreeIter iter; bool enabled = false; char codebuf[9]; char descbuf[512]; gtkui_cheats_clear(); std::ifstream cheatfile(filename, std::ifstream::in|std::ifstream::binary); if (cheatfile.is_open()) { xml.Read(cheatfile); if (xml.GetRoot().IsType(L"cheats")) { Xml::Node root(xml.GetRoot()); Xml::Node node(root.GetFirstChild()); for (int i = 0; i < root.NumChildren(L"cheat"); i++) { wcstombs(descbuf, node.GetChild(L"description").GetValue(), sizeof(descbuf)); // Check if the cheat is enabled node.GetAttribute(L"enabled").IsValue(L"1") ? enabled = true : enabled = false; // Add the cheats to the list if (node.GetChild(L"genie")) { // Game Genie wcstombs(codebuf, node.GetChild(L"genie").GetValue(), sizeof(codebuf)); gtk_tree_store_append(treestore, &iter, NULL); gtk_tree_store_set(treestore, &iter, 0, enabled, 1, codebuf, 4, descbuf, -1); if (enabled) { cheats_code_gg_add(node.GetChild(L"genie").GetValue()); } } else if (node.GetChild(L"rocky")) { // Pro Action Rocky wcstombs(codebuf, node.GetChild(L"rocky").GetValue(), sizeof(codebuf)); gtk_tree_store_append(treestore, &iter, NULL); gtk_tree_store_set(treestore, &iter, 0, enabled, 2, codebuf, 4, descbuf, -1); if (enabled) { cheats_code_par_add(node.GetChild(L"rocky").GetValue()); } } else if (node.GetChild(L"address")) { // Raw char rawbuf[11]; snprintf(rawbuf, sizeof(rawbuf), "%04lu %02lu %02lu", node.GetChild(L"address").GetUnsignedValue(), node.GetChild(L"value").GetUnsignedValue(), node.GetChild(L"compare").GetUnsignedValue()); gtk_tree_store_append(treestore, &iter, NULL); gtk_tree_store_set(treestore, &iter, 0, enabled, 3, rawbuf, 4, descbuf, -1); if (enabled) { cheats_code_raw_add(node); } } node = node.GetNextSibling(); } } cheatfile.close(); } }