Lua plugins now reached a very early PoC state

This commit is contained in:
Henrik Rydgård
2025-10-03 14:28:26 -06:00
parent b860906aa5
commit b5269fbf93
4 changed files with 40 additions and 5 deletions
+3 -5
View File
@@ -74,8 +74,8 @@ public:
IFileSystem *GetSystem(std::string_view prefix);
IFileSystem *GetSystemFromFilename(std::string_view filename);
IFileSystem *GetHandleOwner(u32 handle) const;
FileSystemFlags FlagsFromFilename(std::string_view filename) {
IFileSystem *sys = GetSystemFromFilename(filename);
FileSystemFlags FlagsFromFilename(std::string_view &filename) {
const IFileSystem *sys = GetSystemFromFilename(filename);
return sys ? sys->Flags() : FileSystemFlags::NONE;
}
@@ -83,7 +83,7 @@ public:
void Shutdown();
u32 GetNewHandle() override {
u32 res = current++;
const u32 res = current++;
if (current < 0) {
// Some code assumes it'll never become 0.
current = 1;
@@ -101,9 +101,7 @@ public:
int error = MapFilePath(inpath, outpath, &mountPoint);
if (error == 0) {
*system = mountPoint->system.get();
return error;
}
return error;
}
+13
View File
@@ -32,6 +32,7 @@
#include "Core/ELF/ParamSFO.h"
#include "Core/HLE/Plugins.h"
#include "Core/HLE/sceKernelModule.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "ext/sol/sol.hpp"
namespace HLEPlugins {
@@ -237,6 +238,16 @@ bool Load(PSPModule *pluginWaitingModule, SceUID threadID) {
continue;
}
luaPlugin.context.reset(new LuaContext());
luaPlugin.context->Init();
// Load the file and inject into the context.
std::vector<u8> luaCodeBytes;
if (pspFileSystem.ReadEntireFile(luaPlugin.filename, luaCodeBytes, false) == 0) {
// Loaded the code. Let's copy it to a string.
std::string s;
s.resize(luaCodeBytes.size());
memcpy(s.data(), luaCodeBytes.data(), luaCodeBytes.size());
luaPlugin.context->RunCode(s);
}
}
std::lock_guard<std::mutex> guard(g_inputMutex);
@@ -263,6 +274,8 @@ void DoState(PointerWrap &p) {
// Remember if any were enabled.
Do(p, anyEnabled);
// Lua state needs to be serialized and reloaded. Ugh! Makes it hard to upgrade lua versions.
}
bool HasEnabled() {
+23
View File
@@ -5,9 +5,11 @@
#include "Common/StringUtils.h"
#include "Core/LuaContext.h"
#include "Core/MemMap.h"
#include "Core/System.h"
#include "Core/MIPS/MIPS.h"
#include "Core/MIPS/MIPSDebugInterface.h"
#include "Core/MIPS/MIPSAsm.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/RetroAchievements.h"
#include "Common/System/System.h"
#include "Common/System/Request.h"
@@ -335,6 +337,10 @@ void LuaContext::SetupContext(sol::state &lua) {
// Icache invalidation will not be automatic, unlike in cwcheats.
// Initialize useful constants.
lua["game"] = lua.create_table_with(
"ID", g_paramSFO.GetDiscID()
);
lua["btn"] = lua.create_table_with(
"SELECT", 0x00000001,
"START", 0x00000008,
@@ -394,6 +400,23 @@ end
)");
}
void LuaContext::RunCode(const std::string &code) {
if (!lua_) {
Print(LogLineType::Error, "Lua context not initialized.");
return;
}
try {
auto result = lua_->script(code);
if (!result.valid()) {
sol::error err = result;
Print(LogLineType::Error, err.what());
}
} catch (const sol::error& e) {
ERROR_LOG(Log::System, "Lua exception: %s", e.what());
Print(LogLineType::Error, e.what());
}
}
void LuaContext::Init() {
_dbg_assert_(lua_ == nullptr);
lua_.reset(new sol::state());
+1
View File
@@ -33,6 +33,7 @@ public:
void Init();
void Shutdown();
void RunCode(const std::string &code);
virtual void Print(LogLineType type, std::string_view text);
void PrintF(LogLineType type, const char *fmt, ...);