Skip to content

Commit

Permalink
More cheats stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
bearoso committed Apr 28, 2018
1 parent 5b2a754 commit 4edaba9
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 25 deletions.
2 changes: 1 addition & 1 deletion cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void S9xDeleteCheatGroup (uint32 index);
bool8 S9xLoadCheatFile (const char *filename);
bool8 S9xSaveCheatFile (const char *filename);
void S9xUpdateCheatsInMemory (void);
bool8 S9xImportCheatsFromDatabase (const char *filename);
int S9xImportCheatsFromDatabase(const char *filename);
void S9xCheatsDisable (void);
void S9xCheatsEnable (void);
char *S9xCheatValidate (char *cheat);
Expand Down
36 changes: 31 additions & 5 deletions cheats2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,32 @@ void S9xUpdateCheatsInMemory (void)
}
}

static int S9xCheatIsDuplicate (char *name, char *code)
{
unsigned int i;

for (i = 0; i < Cheat.g.size(); i++)
{
if (!strcmp (name, Cheat.g[i].name))
{
char *code_string = S9xCheatGroupToText (i);
char *validated = S9xCheatValidate (code);

if (validated && !strcmp (code_string, validated))
{
free (code_string);
free (validated);
return TRUE;
}

free (code_string);
free (validated);
}
}

return FALSE;
}

static void S9xLoadCheatsFromBMLNode (bml_node *n)
{
unsigned int i;
Expand All @@ -725,7 +751,7 @@ static void S9xLoadCheatsFromBMLNode (bml_node *n)
if (bml_find_sub(c, "enabled"))
enabled = true;

if (desc && code)
if (desc && code && !S9xCheatIsDuplicate (desc, code))
{
int index = S9xAddCheatGroup (desc, code);

Expand Down Expand Up @@ -878,7 +904,7 @@ void S9xCheatsEnable (void)
}
}

bool8 S9xImportCheatsFromDatabase (const char *filename)
int S9xImportCheatsFromDatabase (const char *filename)
{
bml_node *bml;
char sha256_txt[65];
Expand All @@ -888,7 +914,7 @@ bool8 S9xImportCheatsFromDatabase (const char *filename)
bml = bml_parse_file (filename);

if (!bml)
return FALSE;
return -1; /* No file */

for (i = 0; i < 32; i++)
{
Expand All @@ -909,13 +935,13 @@ bool8 S9xImportCheatsFromDatabase (const char *filename)
{
S9xLoadCheatsFromBMLNode (bml->child[i]);
bml_free_node (bml);
return TRUE;
return 0;
}
}
}
}

bml_free_node (bml);

return FALSE;
return -2; /* No codes */
}
2 changes: 1 addition & 1 deletion gtk/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ snes9x_gtk_CXXFLAGS = -fno-exceptions -fno-rtti
endif
noinst_LIBRARIES =

AM_CPPFLAGS = -I../apu/bapu -I$(top_srcdir) -I.. -DSNES9XLOCALEDIR=\""$(snes9xlocaledir)"\"
AM_CPPFLAGS = -I../apu/bapu -I$(top_srcdir) -I.. -DDATADIR=\""$(snes9xdatadir)"\" -DSNES9XLOCALEDIR=\""$(snes9xlocaledir)"\"

CLEANFILES = \
src/gtk_snes9x_ui.cpp \
Expand Down
9 changes: 6 additions & 3 deletions gtk/configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ AC_DEFINE_UNQUOTED(GETTEXT_PACKAGE, "$GETTEXT_PACKAGE")
AM_GLIB_GNU_GETTEXT

snes9xlocaledir='${prefix}/${DATADIRNAME}/locale'
snes9xdatadir='${prefix}/${DATADIRNAME}/snes9x'

AC_SUBST(snes9xlocaledir)
AC_SUBST(snes9xdatadir)

AC_ARG_WITH(debug,
[AS_HELP_STRING([--with(out)-debug],
Expand Down Expand Up @@ -152,9 +155,9 @@ AC_ARG_WITH(screenshot,

AC_ARG_WITH(gtk3,
[AS_HELP_STRING([--with(out)-gtk3],
[Build with GTK+ 3 if available (default: without)])],
[Build with GTK+ 3 if available (default: with)])],
[],
[with_gtk3=no])
[with_gtk3=yes])

if test yes = "$with_debug" ; then
CFLAGS="$CFLAGS -g"
Expand Down Expand Up @@ -499,7 +502,7 @@ echo "Snes9x will build with support for the following:"
echo ""

if test yes = "$GTK3_WARNING" ; then
echo " GTK+ 3.0 (experimental)"
echo " GTK+ 3.0"
else
echo " GTK+ 2.0"
fi
Expand Down
3 changes: 3 additions & 0 deletions gtk/data/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ icon32x32_DATA = snes9x_32x32.png
iconscalabledir = $(datadir)/icons/hicolor/scalable/apps
iconscalable_DATA = snes9x.svg

cheatsdir = $(datadir)/snes9x
cheats_DATA = ../../data/cheats.bml

install-data-hook:
mv -f $(DESTDIR)$(datadir)/icons/hicolor/16x16/apps/snes9x_16x16.png \
$(DESTDIR)$(datadir)/icons/hicolor/16x16/apps/snes9x.png
Expand Down
164 changes: 152 additions & 12 deletions gtk/src/gtk_cheat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,37 @@ event_remove_code (GtkButton *button, gpointer data)
((Snes9xCheats *) data)->remove_code ();
}

static void
event_search_database (GtkButton *button, gpointer data)
{
((Snes9xCheats *) data)->search_database ();
}

static void
event_delete_all_cheats (GtkButton *button, gpointer data)
{
((Snes9xCheats *) data)->delete_all_cheats ();
}

static void
event_code_toggled (GtkCellRendererToggle *cell_renderer,
gchar *path,
gpointer data)
{
((Snes9xCheats *) data)->toggle_code (path);
int enabled = !gtk_cell_renderer_toggle_get_active (cell_renderer);

((Snes9xCheats *) data)->toggle_code (path, enabled);

return;
}

void
event_row_activated (GtkTreeView *tree_view,
GtkTreePath *path,
GtkTreeViewColumn *column,
gpointer data)
{
((Snes9xCheats *) data)->row_activated (path);

return;
}
Expand All @@ -60,13 +85,17 @@ Snes9xCheats::Snes9xCheats (void)
{
{ "add_code", G_CALLBACK (event_add_code) },
{ "remove_code", G_CALLBACK (event_remove_code) },
{ "search_database", G_CALLBACK (event_search_database) },
{ "delete_all_cheats", G_CALLBACK (event_delete_all_cheats) },
{ NULL, NULL}
};

view = GTK_TREE_VIEW (get_widget ("cheat_treeview"));

g_signal_connect (view, "row-activated", G_CALLBACK (event_row_activated), (gpointer) this);

renderer = gtk_cell_renderer_toggle_new ();
gtk_cell_renderer_toggle_set_activatable (GTK_CELL_RENDERER_TOGGLE (renderer), TRUE);
gtk_tree_view_insert_column_with_attributes (view,
-1,
"",
Expand All @@ -86,6 +115,8 @@ Snes9xCheats::Snes9xCheats (void)
renderer,
"text", COLUMN_DESCRIPTION,
NULL);
GtkTreeViewColumn *column = gtk_tree_view_get_column (view, 1);
gtk_tree_view_column_set_resizable (column, TRUE);

renderer = gtk_cell_renderer_text_new ();
gtk_tree_view_insert_column_with_attributes (view,
Expand All @@ -94,6 +125,9 @@ Snes9xCheats::Snes9xCheats (void)
renderer,
"text", COLUMN_CHEAT,
NULL);
column = gtk_tree_view_get_column (view, 2);
gtk_tree_view_column_set_resizable (column, TRUE);


store = gtk_list_store_new (NUM_COLS,
G_TYPE_BOOLEAN,
Expand Down Expand Up @@ -182,14 +216,25 @@ void
Snes9xCheats::refresh_tree_view (void)
{
GtkTreeIter iter;
unsigned int list_size;

gtk_list_store_clear (store);
list_size = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (store), NULL);

if (Cheat.g.size () == 0)
return;

for (unsigned int i = 0; i < Cheat.g.size() - list_size; i++)
gtk_list_store_append (store, &iter);

gtk_tree_model_get_iter_first (GTK_TREE_MODEL (store), &iter);

for (unsigned int i = 0; i < Cheat.g.size (); i++)
{
char *str = S9xCheatGroupToText (i);

gtk_list_store_append (store, &iter);
if (i > 0)
gtk_tree_model_iter_next (GTK_TREE_MODEL (store), &iter);

gtk_list_store_set (store, &iter,
COLUMN_DESCRIPTION,
!strcmp (Cheat.g [i].name, "") ? _("No description")
Expand All @@ -200,7 +245,6 @@ Snes9xCheats::refresh_tree_view (void)
delete[] str;
}


return;
}

Expand Down Expand Up @@ -228,31 +272,127 @@ void
Snes9xCheats::remove_code (void)
{
int index = get_selected_index ();
GtkTreeIter iter;

if (index < 0)
return;

gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (store), &iter, NULL, index);
gtk_list_store_remove (store, &iter);

S9xDeleteCheatGroup (index);

refresh_tree_view ();
return;
}

void
Snes9xCheats::delete_all_cheats (void)
{
S9xDeleteCheats ();
gtk_list_store_clear (store);

return;
}

void
Snes9xCheats::toggle_code (const gchar *path)
Snes9xCheats::search_database (void)
{
int index = get_index_from_path (path);
std::string filename;
int result;
int reason = 0;

if (index < 0)
filename = S9xGetFilename ("cheats.bml", CHEAT_DIR);
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{
refresh_tree_view ();
return;
}

if (Cheat.g[index].enabled)
S9xDisableCheatGroup (index);
else
if (result < reason)
reason = result;

char *config_dir = get_config_dir ();
filename = std::string (config_dir) + "/cheats.bml";
free (config_dir);
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{
refresh_tree_view ();
return;
}

if (result < reason)
reason = result;


filename = std::string (DATADIR) + "/cheats.bml";
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{
refresh_tree_view ();
return;
}

if (result < reason)
reason = result;

filename = S9xGetFilename ("cheats.bml", ROM_DIR);
if (!(result = S9xImportCheatsFromDatabase (filename.c_str ())))
{
refresh_tree_view ();
return;
}

if (result < reason)
reason = result;

GtkMessageDialog *dialog;
GtkDialogFlags flags = GTK_DIALOG_DESTROY_WITH_PARENT;
dialog = GTK_MESSAGE_DIALOG (gtk_message_dialog_new (get_window (),
flags,
GTK_MESSAGE_INFO,
GTK_BUTTONS_CLOSE,
reason == -1 ? _("Couldn't Find Cheats Database") :
_("No Matching Game Found")));
gtk_message_dialog_format_secondary_markup(GTK_MESSAGE_DIALOG (dialog),
reason == -1 ?
_("The database file <b>cheats.bml</b> was not found. It is normally installed with "
"Snes9x, but you may also place a custom copy in your configuration or cheats directory.") :
_("No matching game was found in the databases. If you are using a non-official "
"translation or modified copy, you may be able to find and manually enter the codes."));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (GTK_WIDGET (dialog));

return;
}

void
Snes9xCheats::row_activated (GtkTreePath *path)
{
gint *indices = gtk_tree_path_get_indices (path);
char *cheat_text;

cheat_text = S9xCheatGroupToText (indices[0]);
set_entry_text ("code_entry", cheat_text);
delete[] cheat_text;
set_entry_text ("description_entry", Cheat.g[indices[0]].name);

return;
}

void
Snes9xCheats::toggle_code (const gchar *path, int enabled)
{
GtkTreeIter iter;
int index = get_index_from_path (path);

GtkTreePath *treepath = gtk_tree_path_new_from_string (path);
gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, treepath);
gtk_list_store_set (store, &iter, COLUMN_ENABLED, enabled, -1);

if (enabled)
S9xEnableCheatGroup (index);
else
S9xDisableCheatGroup (index);

refresh_tree_view ();

return;
}
Expand Down
5 changes: 4 additions & 1 deletion gtk/src/gtk_cheat.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class Snes9xCheats : public GtkBuilderWindow
void show (void);
void add_code (void);
void remove_code (void);
void toggle_code (const gchar *path);
void search_database (void);
void delete_all_cheats (void);
void toggle_code (const gchar *path, int enabled);
void row_activated (GtkTreePath *path);

private:
void refresh_tree_view (void);
Expand Down
Loading

0 comments on commit 4edaba9

Please sign in to comment.