mirror of
https://github.com/Polprzewodnikowy/N64FlashcartMenu.git
synced 2024-12-02 07:24:13 +01:00
01968b55db
<!--- Provide a general summary of your changes in the Title above --> ## Description This PR implements cheat support (patcher + engine) with a simple API to provide Action Replay/Game Shark compatible cheats (with exception of cheats that utilize GS button). API consist of a single pointer to an array of the cheats ended with a double zero entry, For example, if you want to pass these cheats to the patcher: ``` D01F9B91 0020 // Majora's Mask (USA) Inventory Editor 803FDA3F 0002 ``` Put cheats in a `uint32_t` array as such (notice last two entries are zeros): ``` uint32_t cheats[] = { 0xD01F9B91, 0x0020, 0x803FDA3F, 0x0002, 0, 0, }; ``` And pass this array as a boot parameter: `menu->boot_params->cheat_list = cheats;` <!--- Describe your changes in detail --> ## Motivation and Context To provide users with ability to run game modifications in a easy way. <!--- What does this sample do? What problem does it solve? --> <!--- If it fixes/closes/resolves an open issue, please link to the issue here --> ## How Has This Been Tested? On a SummerCart64 flashcart + assembly instructions generation verified in ares emulator via GDB. <!-- (if applicable) --> <!--- Please describe in detail how you tested your sample/changes. --> <!--- Include details of your testing environment, and the tests you ran to --> <!--- see how your change affects other areas of the code, etc. --> ## Screenshots No screenshots <!-- (if appropriate): --> ## Types of changes <!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: --> - [x] Improvement (non-breaking change that adds a new feature) - [ ] Bug fix (fixes an issue) - [ ] Breaking change (breaking change) - [ ] Config and build (change in the configuration and build system, has no impact on code or features) ## Checklist: <!--- Go over all the following points, and put an `x` in all the boxes that apply. --> <!--- If you're unsure about any of these, don't hesitate to ask. We're here to help! --> - [x] My code follows the code style of this project. - [x] My change requires a change to the documentation. - [ ] I have updated the documentation accordingly. - [ ] I have added tests to cover my changes. - [ ] All new and existing tests passed. <!--- It would be nice if you could sign off your contribution by replacing the name with your GitHub user name and GitHub email contact. --> Signed-off-by: Polprzewodnikowy <sc@mateuszfaderewski.pl>
168 lines
3.9 KiB
C
168 lines
3.9 KiB
C
// NOTE: This code doesn't implement EverDrive-64 USB protocol.
|
|
// Main use of these functions is to aid menu development
|
|
// (for example replace files on the SD card or reboot menu).
|
|
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
#include <usb.h>
|
|
|
|
#include "usb_comm.h"
|
|
#include "utils/utils.h"
|
|
|
|
|
|
#define MAX_FILE_SIZE MiB(4)
|
|
|
|
|
|
/** @brief The supported USB commands structure. */
|
|
typedef struct {
|
|
/** @brief The command identifier. */
|
|
const char *id;
|
|
|
|
/** @brief The command operation. */
|
|
void (*op) (menu_t *menu);
|
|
} usb_comm_command_t;
|
|
|
|
|
|
static int usb_comm_get_char (void) {
|
|
char c;
|
|
|
|
if (USBHEADER_GETSIZE(usb_poll()) <= 0) {
|
|
return -1;
|
|
}
|
|
|
|
usb_read(&c, sizeof(c));
|
|
|
|
return (int) (c);
|
|
}
|
|
|
|
static bool usb_comm_read_string (char *string, int length, char end) {
|
|
for (int i = 0; i < length; i++) {
|
|
int c = usb_comm_get_char();
|
|
|
|
if (c < 0) {
|
|
return true;
|
|
}
|
|
|
|
string[i] = (char) (c);
|
|
|
|
if (c == '\0' || c == end) {
|
|
string[i] = '\0';
|
|
break;
|
|
}
|
|
|
|
if (i == (length - 1)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
static void usb_comm_send_error (const char *message) {
|
|
usb_purge();
|
|
usb_write(DATATYPE_TEXT, message, strlen(message));
|
|
}
|
|
|
|
|
|
static void command_reboot (menu_t *menu) {
|
|
menu->next_mode = MENU_MODE_BOOT;
|
|
|
|
menu->boot_params->device_type = BOOT_DEVICE_TYPE_ROM;
|
|
menu->boot_params->tv_type = BOOT_TV_TYPE_PASSTHROUGH;
|
|
menu->boot_params->detect_cic_seed = true;
|
|
menu->boot_params->cheat_list = NULL;
|
|
};
|
|
|
|
static void command_send_file (menu_t *menu) {
|
|
FILE *f;
|
|
char buffer[256];
|
|
uint8_t data[8192];
|
|
char length[8];
|
|
|
|
if (usb_comm_read_string(buffer, sizeof(buffer), ' ')) {
|
|
return usb_comm_send_error("Invalid path argument\n");
|
|
}
|
|
|
|
if (usb_comm_get_char() != '@') {
|
|
return usb_comm_send_error("Invalid argument\n");
|
|
}
|
|
|
|
if (usb_comm_read_string(length, sizeof(length), '@')) {
|
|
return usb_comm_send_error("Invalid file length argument\n");
|
|
}
|
|
|
|
path_t *path = path_init(menu->storage_prefix, buffer);
|
|
|
|
if ((f = fopen(path_get(path), "wb")) == NULL) {
|
|
path_free(path);
|
|
return usb_comm_send_error("Couldn't create file\n");
|
|
}
|
|
setbuf(f, NULL);
|
|
path_free(path);
|
|
|
|
int remaining = atoi(length);
|
|
|
|
if (remaining > MAX_FILE_SIZE) {
|
|
return usb_comm_send_error("File size too big\n");
|
|
}
|
|
|
|
while (remaining > 0) {
|
|
int block_size = MIN(remaining, sizeof(data));
|
|
usb_read(data, block_size);
|
|
if (fwrite(data, 1, block_size, f) != block_size) {
|
|
fclose(f);
|
|
return usb_comm_send_error("Couldn't write all required data to the file\n");
|
|
}
|
|
remaining -= block_size;
|
|
}
|
|
|
|
if (fclose(f)) {
|
|
return usb_comm_send_error("Couldn't flush data to the file\n");
|
|
}
|
|
|
|
if (usb_comm_get_char() != '\0') {
|
|
return usb_comm_send_error("Invalid token at the end of data stream\n");
|
|
}
|
|
}
|
|
|
|
static usb_comm_command_t commands[] = {
|
|
{ .id = "reboot", .op = command_reboot },
|
|
{ .id = "send-file", .op = command_send_file },
|
|
{ .id = NULL },
|
|
};
|
|
|
|
|
|
void usb_comm_poll (menu_t *menu) {
|
|
uint32_t header = usb_poll();
|
|
|
|
if (USBHEADER_GETTYPE(header) != DATATYPE_TEXT) {
|
|
usb_purge();
|
|
return;
|
|
}
|
|
|
|
if (USBHEADER_GETSIZE(header) > 0) {
|
|
char cmd_id[32];
|
|
|
|
if (usb_comm_read_string(cmd_id, sizeof(cmd_id), ' ')) {
|
|
usb_comm_send_error("Command id too long\n");
|
|
} else {
|
|
usb_comm_command_t *cmd = commands;
|
|
|
|
while (cmd->id != NULL) {
|
|
if (strcmp(cmd->id, cmd_id) == 0) {
|
|
cmd->op(menu);
|
|
break;
|
|
}
|
|
cmd++;
|
|
}
|
|
|
|
usb_purge();
|
|
|
|
if (cmd->id == NULL) {
|
|
usb_comm_send_error("Unknown command\n");
|
|
}
|
|
}
|
|
}
|
|
}
|