mirror of
https://github.com/coop-deluxe/sm64coopdx.git
synced 2025-10-30 08:01:01 +00:00
Revert "add a more accurate way to know the last active mod"
This reverts commit bff72db960.
This commit is contained in:
parent
c36852356d
commit
0e00fe7ad3
6 changed files with 21 additions and 70 deletions
|
|
@ -304,15 +304,14 @@ struct LuaObjectField* smlua_get_custom_field(lua_State* L, u32 lot, int keyInde
|
||||||
static struct LuaObjectField lof = { 0 };
|
static struct LuaObjectField lof = { 0 };
|
||||||
if (lot != LOT_OBJECT) { return NULL; }
|
if (lot != LOT_OBJECT) { return NULL; }
|
||||||
|
|
||||||
struct Mod *mod = smlua_get_last_active_mod(L);
|
if (gLuaActiveMod == NULL) {
|
||||||
if (mod == NULL) {
|
|
||||||
LOG_LUA_LINE("Failed to retrieve active mod entry.");
|
LOG_LUA_LINE("Failed to retrieve active mod entry.");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get _custom_object_fields
|
// get _custom_object_fields
|
||||||
lua_getglobal(L, "_G"); // get global table
|
lua_getglobal(L, "_G"); // get global table
|
||||||
lua_getfield(L, LUA_REGISTRYINDEX, mod->relativePath); // push file's "global" table
|
lua_getfield(L, LUA_REGISTRYINDEX, gLuaActiveMod->relativePath); // push file's "global" table
|
||||||
int fileGlobalIndex = lua_gettop(L);
|
int fileGlobalIndex = lua_gettop(L);
|
||||||
lua_getfield(L, fileGlobalIndex, "_custom_object_fields");
|
lua_getfield(L, fileGlobalIndex, "_custom_object_fields");
|
||||||
lua_remove(L, -2); // remove file's "global" table
|
lua_remove(L, -2); // remove file's "global" table
|
||||||
|
|
|
||||||
|
|
@ -739,71 +739,27 @@ void smlua_dump_table(int index) {
|
||||||
printf("--------------\n");
|
printf("--------------\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the folder and file
|
|
||||||
// in the format: "folder/file.lua"
|
|
||||||
static const char *smlua_lua_path_to_relative(const char *src) {
|
|
||||||
int slashCount = 0;
|
|
||||||
for (const char* p = src + strlen(src); p > src; --p) {
|
|
||||||
if (*p == '/' || *p == '\\') {
|
|
||||||
if (++slashCount == 2) {
|
|
||||||
return p + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return src;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the folder from the path
|
|
||||||
static const char *smlua_lua_path_to_folder(const char *src) {
|
|
||||||
static char convertedBuffer[SYS_MAX_PATH];
|
|
||||||
memcpy(convertedBuffer, src, strlen(src)); // Assumes the sub string will be smaller
|
|
||||||
int slashCount = 0;
|
|
||||||
char *lastSlash = convertedBuffer;
|
|
||||||
for (char* p = convertedBuffer + strlen(convertedBuffer); p > convertedBuffer; --p) {
|
|
||||||
if (*p == '/' || *p == '\\') {
|
|
||||||
if (++slashCount == 2) {
|
|
||||||
*lastSlash = '\0'; // Insert a null terminator
|
|
||||||
return p + 1;
|
|
||||||
}
|
|
||||||
lastSlash = p;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return src;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the last run mod via stack trace
|
|
||||||
// not the most efficient way, but its the most accurate
|
|
||||||
// try to call as little as possible, and use gLuaActiveMod
|
|
||||||
struct Mod *smlua_get_last_active_mod() {
|
|
||||||
lua_State* L = gLuaState;
|
|
||||||
lua_Debug info;
|
|
||||||
for (int level = 0; lua_getstack(L, level, &info); level++) {
|
|
||||||
if (!lua_getinfo(L, "S", &info)) { break; }
|
|
||||||
if (strcmp(info.what, "C") == 0) { continue; } // Skip C functions
|
|
||||||
|
|
||||||
// We found the first instance of Lua code
|
|
||||||
// compare the folder to all active mods
|
|
||||||
const char *modFolder = smlua_lua_path_to_folder(info.source);
|
|
||||||
for (u16 i = 0; i < gLocalMods.entryCount; i++) {
|
|
||||||
struct Mod *mod = gLocalMods.entries[i];
|
|
||||||
if (!mod->enabled) { continue; }
|
|
||||||
if (strcmp(mod->relativePath, modFolder) == 0) {
|
|
||||||
gLuaActiveMod = mod;
|
|
||||||
return mod;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
void smlua_logline(void) {
|
void smlua_logline(void) {
|
||||||
lua_State* L = gLuaState;
|
lua_State* L = gLuaState;
|
||||||
lua_Debug info;
|
lua_Debug info;
|
||||||
int level = 0;
|
int level = 0;
|
||||||
while (lua_getstack(L, level, &info)) {
|
while (lua_getstack(L, level, &info)) {
|
||||||
lua_getinfo(L, "nSl", &info);
|
lua_getinfo(L, "nSl", &info);
|
||||||
const char* folderStart = smlua_lua_path_to_relative(info.source);
|
|
||||||
|
// Get the folder and file
|
||||||
|
// in the format: "folder/file.lua"
|
||||||
|
const char* src = info.source;
|
||||||
|
int slashCount = 0;
|
||||||
|
const char* folderStart = NULL;
|
||||||
|
for (const char* p = src + strlen(src); p > src; --p) {
|
||||||
|
if (*p == '/' || *p == '\\') {
|
||||||
|
if (++slashCount == 2) {
|
||||||
|
folderStart = p + 1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
LOG_LUA(" [%d] '%s':%d -- %s [%s]",
|
LOG_LUA(" [%d] '%s':%d -- %s [%s]",
|
||||||
level, (folderStart ? folderStart : info.short_src), info.currentline,
|
level, (folderStart ? folderStart : info.short_src), info.currentline,
|
||||||
(info.name ? info.name : "<unknown>"), info.what);
|
(info.name ? info.name : "<unknown>"), info.what);
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ s64 smlua_get_any_integer_mod_variable(const char* variable);
|
||||||
LuaFunction smlua_get_function_mod_variable(u16 modIndex, const char *variable);
|
LuaFunction smlua_get_function_mod_variable(u16 modIndex, const char *variable);
|
||||||
LuaFunction smlua_get_any_function_mod_variable(const char *variable);
|
LuaFunction smlua_get_any_function_mod_variable(const char *variable);
|
||||||
|
|
||||||
struct Mod *smlua_get_last_active_mod();
|
|
||||||
void smlua_logline(void);
|
void smlua_logline(void);
|
||||||
void smlua_dump_stack(void);
|
void smlua_dump_stack(void);
|
||||||
void smlua_dump_globals(void);
|
void smlua_dump_globals(void);
|
||||||
|
|
|
||||||
|
|
@ -515,7 +515,7 @@ bool mod_file_exists(const char* filename) {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Mod* get_active_mod(void) {
|
struct Mod* get_active_mod(void) {
|
||||||
return smlua_get_last_active_mod();
|
return gLuaActiveMod;
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
|
|
|
||||||
|
|
@ -58,10 +58,8 @@ bool char_valid(const char* buffer, bool isKey) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void mod_storage_get_filename(char* dest) {
|
void mod_storage_get_filename(char* dest) {
|
||||||
struct Mod *mod = smlua_get_last_active_mod();
|
|
||||||
if (mod == NULL) { return; }
|
|
||||||
const char* path = fs_get_write_path(SAVE_DIRECTORY); // get user path
|
const char* path = fs_get_write_path(SAVE_DIRECTORY); // get user path
|
||||||
snprintf(dest, SYS_MAX_PATH - 1, "%s/%s", path, mod->relativePath); // append sav folder
|
snprintf(dest, SYS_MAX_PATH - 1, "%s/%s", path, gLuaActiveMod->relativePath); // append sav folder
|
||||||
strdelete(dest, ".lua"); // delete ".lua" from sav name
|
strdelete(dest, ".lua"); // delete ".lua" from sav name
|
||||||
strcat(dest, SAVE_EXTENSION); // append SAVE_EXTENSION
|
strcat(dest, SAVE_EXTENSION); // append SAVE_EXTENSION
|
||||||
normalize_path(dest); // fix any out of place slashes
|
normalize_path(dest); // fix any out of place slashes
|
||||||
|
|
|
||||||
|
|
@ -17,12 +17,11 @@ void network_send_lua_custom(bool broadcast) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// figure out mod index
|
// figure out mod index
|
||||||
struct Mod *activeMod = smlua_get_last_active_mod();
|
if (gLuaActiveMod == NULL) {
|
||||||
if (activeMod == NULL) {
|
|
||||||
LOG_LUA_LINE("Could not figure out the current active mod!");
|
LOG_LUA_LINE("Could not figure out the current active mod!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
u16 modIndex = activeMod->index;
|
u16 modIndex = gLuaActiveMod->index;
|
||||||
|
|
||||||
// get local index
|
// get local index
|
||||||
s32 toLocalIndex = 0;
|
s32 toLocalIndex = 0;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue