Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Commit 7d625ef

Browse files
committed
Reshuffle lua loading into a modpack loading system, separate from the custom texture system.
1 parent 7e2d6f0 commit 7d625ef

File tree

8 files changed

+171
-58
lines changed

8 files changed

+171
-58
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,9 @@ ant.properties
7272
.externalNativeBuild
7373
gradle.properties
7474
shell/android-studio/reicast/src/main/assets/build
75+
76+
# CMake
77+
/CMakeFiles
78+
/reicast.dir
79+
/ALL_BUILD.*
80+
/ZERO_CHECK.*

libswirl/hw/pvr/Renderer_if.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "deps/crypto/md5.h"
1010

11-
#include "scripting/lua_bindings.h"
11+
#include "scripting/mods.h"
1212

1313
#if FEAT_HAS_NIXPROF
1414
#include "profiler/profiler.h"
@@ -282,7 +282,7 @@ bool rend_single_frame()
282282
// FIXME not here
283283
os_DoEvents();
284284

285-
luabindings_onframe();
285+
modpack_onframe();
286286

287287
#if !defined(TARGET_NO_THREADS)
288288
if (gui_is_open() || gui_state == VJoyEdit)

libswirl/libswirl.cpp

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525
#include "profiler/profiler.h"
2626
#include "input/gamepad_device.h"
2727
#include "rend/TexCache.h"
28+
#include "scripting/mods.h"
2829

2930
#define fault_printf(...)
3031

31-
#ifdef SCRIPTING
32-
#include "scripting/lua_bindings.h"
33-
#endif
34-
3532
void FlushCache();
3633
void LoadCustom();
3734

@@ -403,6 +400,8 @@ int dc_start_game(const char *path)
403400
#endif
404401
init_done = true;
405402

403+
modpack_gamestart();
404+
406405
dc_reset();
407406

408407
game_started = true;
@@ -425,9 +424,7 @@ void* dc_run(void*)
425424

426425
InitAudio();
427426

428-
#ifdef SCRIPTING
429-
luabindings_onstart();
430-
#endif
427+
modpack_onstart();
431428

432429
if (settings.dynarec.Enable)
433430
{
@@ -448,15 +445,11 @@ void* dc_run(void*)
448445
if (reset_requested)
449446
{
450447
dc_reset();
451-
#ifdef SCRIPTING
452-
luabindings_onreset();
453-
#endif
448+
modpack_onreset();
454449
}
455450
} while (reset_requested);
456451

457-
#ifdef SCRIPTING
458-
luabindings_onstop();
459-
#endif
452+
modpack_onstop();
460453

461454
TermAudio();
462455

libswirl/rend/gles/CustomTexture.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#include "deps/libpng/png.h"
3232
#include "reios/reios.h"
3333

34-
#include "scripting/lua_bindings.h"
34+
#include "scripting/mods.h"
3535

3636
void CustomTexture::LoaderThread()
3737
{
@@ -112,8 +112,6 @@ bool CustomTexture::Init()
112112
custom_textures_available = true;
113113
closedir(dir);
114114
loader_thread.Start();
115-
116-
luabindings_findscripts(textures_path);
117115
}
118116
}
119117
#endif
@@ -134,8 +132,6 @@ void CustomTexture::Terminate()
134132
loader_thread.WaitToEnd();
135133
#endif
136134
}
137-
138-
luabindings_close();
139135
}
140136

141137
u8* CustomTexture::LoadCustomTexture(u32 hash, int& width, int& height)

libswirl/scripting/lua_bindings.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,3 @@
55

66
void emulib_expose(lua_State* L);
77

8-
void luabindings_findscripts(std::string path);
9-
void luabindings_run(const char* fn);
10-
void luabindings_close();
11-
void luabindings_onframe();
12-
void luabindings_onstart();
13-
void luabindings_onstop();
14-
void luabindings_onreset();

libswirl/scripting/lua_loader.cpp

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
#include "types.h"
77
#include "stdclass.h"
88

9-
#ifdef SCRIPTING
9+
#include "mods.h"
10+
1011
// TODO: Allow more than one script
1112
class LuaScript {
1213
lua_State* L;
@@ -95,71 +96,74 @@ class LuaScript {
9596
};
9697

9798
std::vector<LuaScript*> loaded_scripts;
98-
#endif
9999

100-
void luabindings_findscripts(std::string path)
100+
static void luabindings_run(const char* fn)
101+
{
102+
loaded_scripts.push_back(new LuaScript(fn));
103+
}
104+
105+
static bool luabindings_findscripts(std::string path)
101106
{
107+
// TODO: config option to disable LUA loading
108+
102109
std::string lua_path = path + "/main.lua";
103110
if (file_exists(lua_path))
104111
{
105112
luabindings_run(lua_path.c_str());
113+
return true;
106114
}
115+
return false;
107116
}
108117

109-
void luabindings_run(const char* fn)
118+
static void luabindings_close()
110119
{
111-
#ifdef SCRIPTING
112-
loaded_scripts.push_back(new LuaScript(fn));
113-
#endif
114-
}
115-
116-
void luabindings_close()
117-
{
118-
#ifdef SCRIPTING
119-
for (auto script : loaded_scripts)
120+
for (auto& script : loaded_scripts)
120121
{
121122
delete script;
122123
}
123124
loaded_scripts.clear();
124-
#endif
125125
}
126126

127-
void luabindings_onframe()
127+
static void luabindings_onframe()
128128
{
129-
#ifdef SCRIPTING
130-
for (auto script : loaded_scripts)
129+
for (auto& script : loaded_scripts)
131130
{
132131
script->onframe();
133132
}
134-
#endif
135133
}
136134

137-
void luabindings_onstart()
135+
static void luabindings_onstart()
138136
{
139-
#ifdef SCRIPTING
140-
for (auto script : loaded_scripts)
137+
for (auto& script : loaded_scripts)
141138
{
142139
script->onstart();
143140
}
144-
#endif
145141
}
146142

147-
void luabindings_onstop()
143+
static void luabindings_onstop()
148144
{
149-
#ifdef SCRIPTING
150-
for (auto script : loaded_scripts)
145+
for (auto& script : loaded_scripts)
151146
{
152147
script->onstop();
153148
}
154-
#endif
155149
}
156150

157-
void luabindings_onreset()
151+
static void luabindings_onreset()
158152
{
159-
#ifdef SCRIPTING
160-
for (auto script : loaded_scripts)
153+
for (auto& script : loaded_scripts)
161154
{
162155
script->onreset();
163156
}
164-
#endif
165-
}
157+
}
158+
159+
static mod_handlers luamod = {
160+
luabindings_findscripts,
161+
luabindings_close,
162+
luabindings_onframe,
163+
luabindings_onstart,
164+
luabindings_onstop,
165+
luabindings_onreset
166+
};
167+
#ifdef SCRIPTING
168+
static bool registered = mod_handler_register(luamod);
169+
#endif

libswirl/scripting/mods.cpp

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "mods.h"
2+
#include "reios/reios.h"
3+
#ifdef _MSC_VER
4+
#include "dirent/dirent.h"
5+
#else
6+
#include <dirent.h>
7+
#endif
8+
9+
std::string mods_path;
10+
std::vector<mod_handlers>* modhandlers;
11+
12+
static std::string _getGameId()
13+
{
14+
std::string game_id = reios_product_number;
15+
const size_t str_end = game_id.find_last_not_of(" ");
16+
if (str_end == std::string::npos)
17+
return "";
18+
game_id = game_id.substr(0, str_end + 1);
19+
std::replace(game_id.begin(), game_id.end(), ' ', '_');
20+
21+
return game_id;
22+
}
23+
24+
bool mod_handler_register(mod_handlers& mod)
25+
{
26+
if (modhandlers == nullptr)
27+
modhandlers = new std::vector<mod_handlers>();
28+
modhandlers->push_back(mod);
29+
return true;
30+
}
31+
32+
void modpack_detect_mods(std::string path)
33+
{
34+
for (auto& mh : *modhandlers)
35+
{
36+
mh.active = mh.detect(path);
37+
}
38+
}
39+
40+
void modpack_gamestart()
41+
{
42+
std::string game_id = _getGameId();
43+
if (game_id.length() > 0)
44+
{
45+
mods_path = get_readonly_data_path(DATA_PATH) + "mods/" + game_id + "/";
46+
47+
DIR* dir = opendir(mods_path.c_str());
48+
if (dir != NULL)
49+
{
50+
printf("Found modpack directory: %s\n", mods_path.c_str());
51+
52+
closedir(dir);
53+
54+
modpack_detect_mods(mods_path);
55+
}
56+
}
57+
}
58+
59+
void modpack_close()
60+
{
61+
for (auto& mh : *modhandlers)
62+
{
63+
if (mh.active) mh.close();
64+
mh.active = false;
65+
}
66+
}
67+
void modpack_onframe()
68+
{
69+
for (auto& mh : *modhandlers)
70+
{
71+
if (mh.active && mh.onframe) mh.onframe();
72+
}
73+
}
74+
void modpack_onstart()
75+
{
76+
for (auto& mh : *modhandlers)
77+
{
78+
if (mh.active && mh.onstart) mh.onstart();
79+
}
80+
}
81+
void modpack_onstop()
82+
{
83+
for (auto& mh : *modhandlers)
84+
{
85+
if (mh.active && mh.onstop) mh.onstop();
86+
}
87+
}
88+
void modpack_onreset()
89+
{
90+
for (auto& mh : *modhandlers)
91+
{
92+
if (mh.active && mh.onreset) mh.onreset();
93+
}
94+
}

libswirl/scripting/mods.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
#include <stdclass.h>
6+
7+
struct mod_handlers {
8+
// required
9+
bool (*detect)(std::string);
10+
void (*close)();
11+
12+
// optional
13+
void (*onframe)();
14+
void (*onstart)();
15+
void (*onstop)();
16+
void (*onreset)();
17+
18+
bool active;
19+
};
20+
bool mod_handler_register(mod_handlers& mod);
21+
22+
void modpack_gamestart();
23+
void modpack_close();
24+
void modpack_onframe();
25+
void modpack_onstart();
26+
void modpack_onstop();
27+
void modpack_onreset();

0 commit comments

Comments
 (0)