From 1a7a544ba7891471fd94f2626a1c1af2c3427228 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 7 Jul 2017 14:09:32 -0700 Subject: [PATCH] Core: Add basic scripting bridge --- CMakeLists.txt | 10 ++++++++++ include/mgba/debugger/debugger.h | 2 ++ src/debugger/cli-debugger.c | 24 ++++++++++++++++++++++++ src/debugger/debugger.c | 13 +++++++++++++ src/platform/python/_builder.h | 3 +++ src/platform/python/_builder.py | 1 + src/platform/sdl/main.c | 15 +++++++++++++++ 7 files changed, 68 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3df844ad8..e25edad5a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -19,6 +19,7 @@ set(USE_SQLITE3 ON CACHE BOOL "Whether or not to enable SQLite3 support") set(M_CORE_GBA ON CACHE BOOL "Build Game Boy Advance core") set(M_CORE_GB ON CACHE BOOL "Build Game Boy core") set(USE_LZMA ON CACHE BOOL "Whether or not to enable 7-Zip support") +set(ENABLE_SCRIPTING ON CACHE BOOL "Whether or not to enable scripting support") set(BUILD_QT ON CACHE BOOL "Build Qt frontend") set(BUILD_SDL ON CACHE BOOL "Build SDL frontend") set(BUILD_LIBRETRO OFF CACHE BOOL "Build libretro core") @@ -353,6 +354,7 @@ endif() # Feature dependencies set(FEATURE_DEFINES) set(FEATURES) +set(ENABLES) if(CMAKE_SYSTEM_NAME MATCHES .*BSD) set(LIBEDIT_LIBRARIES -ledit) if (CMAKE_SYSTEM_NAME STREQUAL OpenBSD) @@ -599,6 +601,10 @@ if(USE_SQLITE3) list(APPEND FEATURE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/src/feature/sqlite3/no-intro.c") endif() +if(ENABLE_SCRIPTING) + list(APPEND ENABLES SCRIPTING) +endif() + set(TEST_SRC ${CORE_TEST_SRC}) if(M_CORE_GB) add_definitions(-DM_CORE_GB) @@ -646,6 +652,10 @@ foreach(FEATURE IN LISTS FEATURES) list(APPEND FEATURE_DEFINES "USE_${FEATURE}") endforeach() +foreach(ENABLE IN LISTS ENABLES) + list(APPEND FEATURE_DEFINES "ENABLE_${ENABLE}") +endforeach() + source_group("Virtual files" FILES ${CORE_VFS_SRC} ${VFS_SRC}) source_group("Extra features" FILES ${FEATURE_SRC}) source_group("Third-party code" FILES ${THIRD_PARTY_SRC}) diff --git a/include/mgba/debugger/debugger.h b/include/mgba/debugger/debugger.h index bef61c208..74c1ad89c 100644 --- a/include/mgba/debugger/debugger.h +++ b/include/mgba/debugger/debugger.h @@ -30,6 +30,7 @@ enum mDebuggerState { DEBUGGER_PAUSED, DEBUGGER_RUNNING, DEBUGGER_CUSTOM, + DEBUGGER_SCRIPT, DEBUGGER_SHUTDOWN }; @@ -92,6 +93,7 @@ struct mDebugger { struct mDebuggerPlatform* platform; enum mDebuggerState state; struct mCore* core; + struct mScriptBridge* bridge; void (*init)(struct mDebugger*); void (*deinit)(struct mDebugger*); diff --git a/src/debugger/cli-debugger.c b/src/debugger/cli-debugger.c index cc911d8c7..281d6e123 100644 --- a/src/debugger/cli-debugger.c +++ b/src/debugger/cli-debugger.c @@ -12,6 +12,10 @@ #include #include +#if ENABLE_SCRIPTING +#include +#endif + #if !defined(NDEBUG) && !defined(_WIN32) #include #endif @@ -51,6 +55,9 @@ static void _writeWord(struct CLIDebugger*, struct CLIDebugVector*); static void _dumpByte(struct CLIDebugger*, struct CLIDebugVector*); static void _dumpHalfword(struct CLIDebugger*, struct CLIDebugVector*); static void _dumpWord(struct CLIDebugger*, struct CLIDebugVector*); +#ifdef ENABLE_SCRIPTING +static void _source(struct CLIDebugger*, struct CLIDebugVector*); +#endif static struct CLIDebuggerCommandSummary _debuggerCommands[] = { { "b", _setBreakpoint, CLIDVParse, "Set a breakpoint" }, @@ -92,6 +99,9 @@ static struct CLIDebuggerCommandSummary _debuggerCommands[] = { { "x/1", _dumpByte, CLIDVParse, "Examine bytes at a specified offset" }, { "x/2", _dumpHalfword, CLIDVParse, "Examine halfwords at a specified offset" }, { "x/4", _dumpWord, CLIDVParse, "Examine words at a specified offset" }, +#ifdef ENABLE_SCRIPTING + { "source", _source, CLIDVStringParse, "Load a script" }, +#endif #if !defined(NDEBUG) && !defined(_WIN32) { "!", _breakInto, 0, "Break into attached debugger (for developers)" }, #endif @@ -411,6 +421,20 @@ static void _dumpWord(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { } } +#ifdef ENABLE_SCRIPTING +static void _source(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { + if (!dv) { + debugger->backend->printf(debugger->backend, "Needs a filename\n"); + return; + } + if (debugger->d.bridge && mScriptBridgeLoadScript(debugger->d.bridge, dv->charValue)) { + debugger->d.state = DEBUGGER_SCRIPT; + } else { + debugger->backend->printf(debugger->backend, "Failed to load script\n"); + } +} +#endif + static void _setBreakpoint(struct CLIDebugger* debugger, struct CLIDebugVector* dv) { if (!dv || dv->type != CLIDV_INT_TYPE) { debugger->backend->printf(debugger->backend, "%s\n", ERROR_MISSING_ARGS); diff --git a/src/debugger/debugger.c b/src/debugger/debugger.c index bac157f7c..63fdc5062 100644 --- a/src/debugger/debugger.c +++ b/src/debugger/debugger.c @@ -13,6 +13,10 @@ #include #endif +#if ENABLE_SCRIPTING +#include +#endif + const uint32_t DEBUGGER_ID = 0xDEADBEEF; mLOG_DEFINE_CATEGORY(DEBUGGER, "Debugger", "core.debugger"); @@ -34,6 +38,7 @@ struct mDebugger* mDebuggerCreate(enum mDebuggerType type, struct mCore* core) { }; union DebugUnion* debugger = malloc(sizeof(union DebugUnion)); + memset(debugger, 0, sizeof(*debugger)); switch (type) { case DEBUGGER_CLI: @@ -92,6 +97,14 @@ void mDebuggerRun(struct mDebugger* debugger) { debugger->state = DEBUGGER_RUNNING; } break; + case DEBUGGER_SCRIPT: +#ifdef ENABLE_SCRIPTING + mScriptBridgeRun(debugger->bridge); +#endif + if (debugger->state == DEBUGGER_SCRIPT) { + debugger->state = DEBUGGER_RUNNING; + } + break; case DEBUGGER_SHUTDOWN: return; } diff --git a/src/platform/python/_builder.h b/src/platform/python/_builder.h index fcd3d21c2..1de1ce32f 100644 --- a/src/platform/python/_builder.h +++ b/src/platform/python/_builder.h @@ -55,3 +55,6 @@ void free(void*); #include #include #endif +#ifdef USE_DEBUGGERS +#include +#endif diff --git a/src/platform/python/_builder.py b/src/platform/python/_builder.py index 7415ad5f2..f52b45050 100644 --- a/src/platform/python/_builder.py +++ b/src/platform/python/_builder.py @@ -26,6 +26,7 @@ ffi.set_source("mgba._pylib", """ #include #include #include +#include #include #include #include diff --git a/src/platform/sdl/main.c b/src/platform/sdl/main.c index d949951df..469a68260 100644 --- a/src/platform/sdl/main.c +++ b/src/platform/sdl/main.c @@ -13,6 +13,9 @@ #ifdef USE_EDITLINE #include "feature/editline/cli-el-backend.h" #endif +#ifdef ENABLE_SCRIPTING +#include +#endif #include #include @@ -159,6 +162,10 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) { return 1; } mCoreAutoloadSave(renderer->core); +#ifdef ENABLE_SCRIPTING + struct mScriptBridge* bridge = mScriptBridgeCreate(); +#endif + #ifdef USE_DEBUGGERS struct mDebugger* debugger = mDebuggerCreate(args->debuggerType, renderer->core); if (debugger) { @@ -171,6 +178,9 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) { mDebuggerAttach(debugger, renderer->core); mDebuggerEnter(debugger, DEBUGGER_ENTER_MANUAL, NULL); } +#ifdef ENABLE_SCRIPTING + mScriptBridgeSetDebugger(bridge, debugger); +#endif #endif if (args->patch) { @@ -212,6 +222,11 @@ int mSDLRun(struct mSDLRenderer* renderer, struct mArguments* args) { printf("Could not run game. Are you sure the file exists and is a compatible game?\n"); } renderer->core->unloadROM(renderer->core); + +#ifdef ENABLE_SCRIPTING + mScriptBridgeDestroy(bridge); +#endif + return didFail; }