Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions cheats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,63 @@ void cheats_toggle()
}
}

static int cheats_find_by_name(const char *name)
{
if (!name || !*name) return -1;

for (int i = 0; i < cheats_available(); i++)
{
if (!strcmp(cheats[i].name, name)) return i;
}

return -1;
}

int cheats_toggle_by_name(const char *name)
{
if (!cheats_available()) return CHEATS_CMD_NO_CHEATS;

int idx = cheats_find_by_name(name);
if (idx < 0) return CHEATS_CMD_NOT_FOUND;

bool was_enabled = cheats[idx].enabled;
int old_entry = iSelectedEntry;
iSelectedEntry = idx;
cheats_toggle();
iSelectedEntry = old_entry;

return (cheats[idx].enabled != was_enabled) ? CHEATS_CMD_OK : CHEATS_CMD_LOAD_FAILED;
}

int cheats_set_enabled_by_name(const char *name, bool enabled)
{
if (!cheats_available()) return CHEATS_CMD_NO_CHEATS;

int idx = cheats_find_by_name(name);
if (idx < 0) return CHEATS_CMD_NOT_FOUND;
if (cheats[idx].enabled == enabled) return CHEATS_CMD_OK;

return cheats_toggle_by_name(name);
}

int cheats_clear_enabled()
{
if (!cheats_available()) return CHEATS_CMD_NO_CHEATS;

bool changedCheats = false;
for (int i = 0; i < cheats_available(); i++)
{
if (cheats[i].enabled)
{
cheats[i].enabled = false;
changedCheats = true;
}
}

if (changedCheats) cheats_send();
return CHEATS_CMD_OK;
}

int cheats_loaded()
{
return loaded;
Expand Down
13 changes: 13 additions & 0 deletions cheats.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ void cheats_print();
void cheats_toggle();
int cheats_loaded();

enum
{
CHEATS_CMD_OK = 0,
CHEATS_CMD_NO_CHEATS = -1,
CHEATS_CMD_NOT_FOUND = -2,
CHEATS_CMD_LOAD_FAILED = -3,
CHEATS_CMD_NO_ROOM = -4
};

int cheats_set_enabled_by_name(const char *name, bool enabled);
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
int cheats_toggle_by_name(const char *name);
int cheats_clear_enabled();

void cheats_init_arcade(int unit_size, int max_active);
void cheats_add_arcade(const char *name, const char *cheatData, int cheatSize);
void cheats_finalize_arcade();
Expand Down
2 changes: 2 additions & 0 deletions input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "frame_timer.h"
#include "scaler.h"
#include "file_io.h"
#include "support/zaparoo/cheat_cmd.h"
#include "support/zaparoo/alt_launcher.h"

#define NUMDEV 30
Expand Down Expand Up @@ -6238,6 +6239,7 @@ int input_test(int getchar)
printf("MiSTer_cmd: %s\n", cmd);
if (!strncmp(cmd, "fb_cmd", 6)) video_cmd(cmd);
else if (!strncmp(cmd, "video_mode ", 11)) video_mode_cmd(cmd + 11);
else if (!strncmp(cmd, "cheat ", 6)) zaparoo_cheat_cmd(cmd + 6);
else if (!strncmp(cmd, "load_core ", 10))
{
if(isXmlName(cmd)) xml_load(cmd + 10);
Expand Down
79 changes: 79 additions & 0 deletions support/zaparoo/cheat_cmd.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include "cheat_cmd.h"

#include <ctype.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "cheats.h"

static const char *skip_space(const char *s)
{
while (*s && isspace((unsigned char)*s)) s++;
return s;
}

static bool parse_token(const char **cmd, char *token, size_t token_size)
{
const char *s = skip_space(*cmd);
size_t len = 0;

while (s[len] && !isspace((unsigned char)s[len])) len++;
if (!len || len >= token_size) return false;

memcpy(token, s, len);
token[len] = 0;
*cmd = s + len;
return true;
}

static const char *cheat_cmd_status(int status)
{
switch (status)
{
case CHEATS_CMD_OK: return "ok";
case CHEATS_CMD_NO_CHEATS: return "no cheats loaded";
case CHEATS_CMD_NOT_FOUND: return "not found";
case CHEATS_CMD_LOAD_FAILED: return "load failed";
case CHEATS_CMD_NO_ROOM: return "no room";
default: return "error";
}
}

void zaparoo_cheat_cmd(const char *cmd)
{
char action[16];

if (!parse_token(&cmd, action, sizeof(action)))
{
printf("MiSTer_cmd cheat: missing command\n");
return;
}

if (!strcmp(action, "clear"))
{
int status = cheats_clear_enabled();
printf("MiSTer_cmd cheat clear: %s\n", cheat_cmd_status(status));
return;
}

const char *name = skip_space(cmd);
if (!*name)
{
printf("MiSTer_cmd cheat %s: missing name\n", action);
return;
}

int status;
if (!strcmp(action, "on")) status = cheats_set_enabled_by_name(name, true);
else if (!strcmp(action, "off")) status = cheats_set_enabled_by_name(name, false);
else if (!strcmp(action, "toggle")) status = cheats_toggle_by_name(name);
else
{
printf("MiSTer_cmd cheat: unknown command '%s'\n", action);
return;
}

printf("MiSTer_cmd cheat %s '%s': %s\n", action, name, cheat_cmd_status(status));
}
3 changes: 3 additions & 0 deletions support/zaparoo/cheat_cmd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#pragma once

void zaparoo_cheat_cmd(const char *cmd);