From 18a35b39287196ec3d51d5accb893c51a5f5c312 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 24 Nov 2023 22:12:40 -0800 Subject: [PATCH] Feature: Move command-line patch/cheats argument handling --- include/mgba/feature/commandline.h | 1 + src/feature/commandline.c | 27 +++++++++++++++++++++++++++ src/platform/sdl/main.c | 26 +------------------------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/include/mgba/feature/commandline.h b/include/mgba/feature/commandline.h index d4f402241..17af655fe 100644 --- a/include/mgba/feature/commandline.h +++ b/include/mgba/feature/commandline.h @@ -60,6 +60,7 @@ void version(const char* arg0); bool mArgumentsParse(struct mArguments* args, int argc, char* const* argv, struct mSubParser* subparsers, int nSubparsers); void mArgumentsApply(const struct mArguments* args, struct mSubParser* subparsers, int nSubparsers, struct mCoreConfig* config); bool mArgumentsApplyDebugger(const struct mArguments*, struct mCore*, struct mDebugger*); +void mArgumentsApplyFileLoads(const struct mArguments*, struct mCore*); void mArgumentsDeinit(struct mArguments* args); void mSubParserGraphicsInit(struct mSubParser* parser, struct mGraphicsOpts* opts); diff --git a/src/feature/commandline.c b/src/feature/commandline.c index 0fe2c887d..7e720886d 100644 --- a/src/feature/commandline.c +++ b/src/feature/commandline.c @@ -5,9 +5,12 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include +#include #include +#include #include #include +#include #ifdef USE_GDB_STUB #include @@ -241,6 +244,30 @@ bool mArgumentsApplyDebugger(const struct mArguments* args, struct mCore* core, return hasDebugger; } +void mArgumentsApplyFileLoads(const struct mArguments* args, struct mCore* core) { + if (args->patch) { + struct VFile* patch = VFileOpen(args->patch, O_RDONLY); + if (patch) { + core->loadPatch(core, patch); + patch->close(patch); + } + } else { + mCoreAutoloadPatch(core); + } + + struct mCheatDevice* device = NULL; + if (args->cheatsFile && (device = core->cheatDevice(core))) { + struct VFile* vf = VFileOpen(args->cheatsFile, O_RDONLY); + if (vf) { + mCheatDeviceClear(device); + mCheatParseFile(device, vf); + vf->close(vf); + } + } else { + mCoreAutoloadCheats(core); + } +} + void mArgumentsDeinit(struct mArguments* args) { free(args->fname); args->fname = 0; diff --git a/src/platform/sdl/main.c b/src/platform/sdl/main.c index 8f75384f0..f71f1cfc5 100644 --- a/src/platform/sdl/main.c +++ b/src/platform/sdl/main.c @@ -15,7 +15,6 @@ #endif #endif -#include #include #include #include @@ -110,16 +109,6 @@ int main(int argc, char** argv) { opts.width = renderer.width * renderer.ratio; opts.height = renderer.height * renderer.ratio; - struct mCheatDevice* device = NULL; - if (args.cheatsFile && (device = renderer.core->cheatDevice(renderer.core))) { - struct VFile* vf = VFileOpen(args.cheatsFile, O_RDONLY); - if (vf) { - mCheatDeviceClear(device); - mCheatParseFile(device, vf); - vf->close(vf); - } - } - mInputMapInit(&renderer.core->inputMap, &GBAInputInfo); mCoreInitConfig(renderer.core, PORT); mArgumentsApply(&args, &subparser, 1, &renderer.core->config); @@ -183,10 +172,6 @@ int main(int argc, char** argv) { mSDLDetachPlayer(&renderer.events, &renderer.player); mInputMapDeinit(&renderer.core->inputMap); - if (device) { - mCheatDeviceDestroy(device); - } - mSDLDeinit(&renderer); mStandardLoggerDeinit(&_logger); @@ -225,7 +210,7 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) { return 1; } mCoreAutoloadSave(renderer->core); - mCoreAutoloadCheats(renderer->core); + mArgumentsApplyFileLoads(args, renderer->core); #ifdef ENABLE_SCRIPTING struct mScriptBridge* bridge = mScriptBridgeCreate(); #ifdef ENABLE_PYTHON @@ -252,15 +237,6 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) { } #endif - if (args->patch) { - struct VFile* patch = VFileOpen(args->patch, O_RDONLY); - if (patch) { - renderer->core->loadPatch(renderer->core, patch); - } - } else { - mCoreAutoloadPatch(renderer->core); - } - renderer->audio.samples = renderer->core->opts.audioBuffers; renderer->audio.sampleRate = 44100; thread.logger.logger = &_logger.d;