Made Lua mods show up in Discord Activity

This commit is contained in:
MysterD 2022-02-05 10:25:36 -08:00
parent a28f046eb9
commit 948e4283de
8 changed files with 49 additions and 9 deletions

View File

@ -90,7 +90,7 @@ void smlua_init(void) {
// load scripts
LOG_INFO("Loading scripts:");
struct ModTable* table = (gNetworkType == NT_SERVER) ? &gModTableLocal : &gModTableRemote;
struct ModTable* table = gModTableCurrent;
for (int i = 0; i < table->entryCount; i++) {
struct ModListEntry* entry = &table->entries[i];
if (!entry->enabled) { continue; }

View File

@ -155,9 +155,9 @@ int smlua_hook_mario_action(lua_State* L) {
return 0;
}
lua_Integer action = smlua_to_integer(L, -2);
if (action == 0 || gSmLuaConvertSuccess) {
LOG_LUA("Hook Action: tried to hook invalid action");
lua_Integer action = smlua_to_integer(L, 1);
if (action == 0 || !gSmLuaConvertSuccess) {
LOG_LUA("Hook Action: tried to hook invalid action: %lld, %u", action, gSmLuaConvertSuccess);
smlua_logline();
return 0;
}

View File

@ -51,6 +51,7 @@ bool smlua_to_boolean(lua_State* L, int index) {
lua_Integer smlua_to_integer(lua_State* L, int index) {
if (lua_type(L, index) == LUA_TBOOLEAN) {
gSmLuaConvertSuccess = true;
return lua_toboolean(L, index) ? 1 : 0;
} else if (lua_type(L, index) != LUA_TNUMBER) {
LOG_LUA("smlua_to_integer received improper type '%d'", lua_type(L, index));

View File

@ -10,6 +10,7 @@
struct ModTable gModTableLocal = { .entries = NULL, .entryCount = 0, .totalSize = 0, .isRemote = false };
struct ModTable gModTableRemote = { .entries = NULL, .entryCount = 0, .totalSize = 0, .isRemote = true };
struct ModTable* gModTableCurrent = &gModTableLocal;
static char sTmpSession[MAX_SESSION_CHARS] = { 0 };
static char sTmpPath[PATH_MAX] = { 0 };
@ -99,7 +100,7 @@ static char* extract_lua_field(char* fieldName, char* buffer) {
return NULL;
}
static void extract_lua_fields(struct ModListEntry* entry) {
void mod_list_extract_lua_fields(struct ModListEntry* entry) {
FILE* f = entry->fp;
char buffer[512] = { 0 };
@ -107,6 +108,8 @@ static void extract_lua_fields(struct ModListEntry* entry) {
entry->incompatible = NULL;
entry->description = NULL;
fseek(entry->fp, 0, SEEK_SET);
while (!feof(f)) {
file_get_line(buffer, 512, f);
@ -128,6 +131,7 @@ static void extract_lua_fields(struct ModListEntry* entry) {
snprintf(entry->description, 512, "%s", extracted);
}
}
}
static void mod_list_add_local(u16 index, const char* path, char* name) {
@ -142,7 +146,7 @@ static void mod_list_add_local(u16 index, const char* path, char* name) {
snprintf(entry->path, PATH_MAX - 1, "%s/%s", path, name);
entry->fp = fopen(entry->path, "rb");
extract_lua_fields(entry);
mod_list_extract_lua_fields(entry);
fseek(entry->fp, 0, SEEK_END);
entry->size = ftell(entry->fp);
@ -292,6 +296,7 @@ static void mod_list_load_local(const char* path) {
}
void mod_list_init(void) {
gModTableCurrent = &gModTableLocal;
srand(time(0));
snprintf(sTmpSession, MAX_SESSION_CHARS, "%06X", (u32)(rand() % 0xFFFFFF));
snprintf(sTmpPath, PATH_MAX - 1, "%s", fs_get_write_path("tmp"));

View File

@ -36,8 +36,10 @@ struct ModTable {
extern struct ModTable gModTableLocal;
extern struct ModTable gModTableRemote;
extern struct ModTable* gModTableCurrent;
void mod_list_add_tmp(u16 index, u16 remoteIndex, char* name, size_t size);
void mod_list_extract_lua_fields(struct ModListEntry* entry);
void mod_table_clear(struct ModTable* table);
void mod_list_alloc(struct ModTable* table, u16 count);

View File

@ -4,6 +4,7 @@
#include "pc/network/network.h"
#include "pc/network/version.h"
#include "pc/djui/djui.h"
#include "pc/mod_list.h"
#include "pc/logfile.h"
#define HASH_LENGTH 8
@ -79,17 +80,40 @@ void discord_activity_update(bool hosting) {
if (gCurActivity.details[0] == '\0') {
snprintf(gCurActivity.details, 128, "%s", get_version());
bool displayDash = true;
bool displayComma = false;
if (gRegisteredMods.string != NULL) {
strncat(gCurActivity.details, " - ", 127);
displayDash = false;
// add patches to activity
struct StringLinkedList* node = &gRegisteredMods;
while (node != NULL && node->string != NULL) {
if (displayComma) { strncat(gCurActivity.details, ", ", 127); }
strncat(gCurActivity.details, node->string, 127);
displayComma = true;
node = node->next;
if (node != NULL && node->string != NULL) {
strncat(gCurActivity.details, ", ", 127);
}
}
}
struct ModTable* table = gModTableCurrent;
if (table != NULL && table->entryCount > 0) {
// add mods to activity
for (int i = 0; i < table->entryCount; i++) {
struct ModListEntry* entry = &table->entries[i];
if (!entry->enabled) { continue; }
if (displayDash) { strncat(gCurActivity.details, " - ", 127); }
if (displayComma) { strncat(gCurActivity.details, ", ", 127); }
strncat(gCurActivity.details, entry->displayName ? entry->displayName : entry->name, 127);
displayDash = false;
displayComma = true;
}
}
}
app.activities->update_activity(app.activities, &gCurActivity, NULL, on_activity_update_callback);

View File

@ -185,6 +185,12 @@ void network_receive_download(struct Packet* p) {
if (sOffset[OFFSET_COUNT - 1] + CHUNK_SIZE >= entry->size) {
LOG_INFO("Finished download of '%s'", entry->name);
fclose(entry->fp);
// parse mod header
entry->fp = fopen(entry->path, "rb");
mod_list_extract_lua_fields(entry);
fclose(entry->fp);
entry->fp = NULL;
entry->complete = true;
}

View File

@ -91,6 +91,8 @@ void network_receive_mod_list(struct Packet* p) {
}
u16 modEntryCount = 0;
gModTableCurrent = &gModTableRemote;
packet_read(p, &modEntryCount, sizeof(u16));
mod_list_alloc(&gModTableRemote, modEntryCount);