Skip to content

Commit 777ac8a

Browse files
committed
Add reload_all with new scripts support
1 parent fbf60c2 commit 777ac8a

File tree

7 files changed

+45
-29
lines changed

7 files changed

+45
-29
lines changed

examples/ReloadAll.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import libstd.common as common
2-
import libstd.pool as pool
32
import libstd.script as script
43

54
script.name("ReloadAll")
@@ -12,13 +11,7 @@
1211
while common.key_pressed(0xA2) and common.key_pressed(0x52):
1312
common.wait(5)
1413

15-
# Reloading other scripts
16-
name : str = script.get_file_name()
17-
for script_name in pool.script():
18-
if script_name != name:
19-
script.reload(script_name)
20-
21-
script.reload() # Reloading current script
14+
script.reload_all()
2215

2316
common.wait(0)
2417

runtime/PyLoader/libstd/memory.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def set_raw(address :int, data :str, size :int, virtual_protect :bool = True) ->
2525
_memory.set_raw(address, data, size, virtual_protect)
2626

2727
def call_function(address :int, num_args:int = 0, pop :int = 0, *arg) -> int:
28-
'''Calls the function from the address. More info https://gtagmodding.com/opcode-database/opcode/0AA5/'''
28+
'''Calls the function from the address. Arguments are passed left to right. More info https://gtagmodding.com/opcode-database/opcode/0AA5/'''
2929

3030
return _memory.call_function(address, num_args, pop, *arg)
3131

3232
def call_method(address :int, struct :int, num_args :int = 0, pop :int = 0, *arg) -> int:
33-
'''Calls the method from the address. More info https://gtagmodding.com/opcode-database/opcode/0AA6/'''
33+
'''Calls the method from the address. Arguments are passed left to right. More info https://gtagmodding.com/opcode-database/opcode/0AA6/'''
3434

3535
return _memory.call_method(address, struct, num_args, pop, *arg)
3636

runtime/PyLoader/libstd/script.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def reload(script_name :str = "") -> None:
6565

6666
_script.reload(script_name)
6767

68+
def reload_all(script_name :str = "") -> None:
69+
'''Unloads all scripts, then loads them again from PyLoader directory. Loads new scripts too'''
70+
71+
_script.reload_all(script_name)
72+
6873
def properties(*argv) -> bool:
6974
'''Sets script specific property flags.\n\nValid properties:\n\nno_reload: Disables script reloading'''
7075

src/PyLoader.cpp

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,27 @@ void PyLoader::CheckUpdate()
9797
std::remove(path);
9898
}
9999

100+
void PyLoader::LoadScripts()
101+
{
102+
HANDLE dir;
103+
WIN32_FIND_DATA fileData;
104+
dir = FindFirstFileA((LPCSTR)PLUGIN_PATH((char*)"/PyLoader/*.py"), &fileData);
105+
106+
// Loading .py scripts
107+
if (dir != INVALID_HANDLE_VALUE)
108+
{
109+
do
110+
{
111+
std::string* fileName = new std::string("PyLoader." + std::string(fileData.cFileName));
112+
113+
// remove the extension
114+
fileName->erase(fileName->end() - 3, fileName->end());
115+
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::ExecuteScript, fileName, NULL, NULL);
116+
Sleep(50);
117+
} while (FindNextFile(dir, &fileData));
118+
}
119+
}
120+
100121
void PyLoader::PyMain(void* param)
101122
{
102123
plugin::Events::processScriptsEvent += []
@@ -111,9 +132,6 @@ void PyLoader::PyMain(void* param)
111132

112133
gLog << "------------------------------"<< std::endl;
113134

114-
HANDLE dir;
115-
WIN32_FIND_DATA fileData;
116-
117135
/*
118136
Load all the .dll modules from lib and libstd folder
119137
libstd folder is reserved for first party modules only
@@ -122,8 +140,6 @@ void PyLoader::PyMain(void* param)
122140
LoadPlugins("lib");
123141
LoadPlugins("libstd");
124142

125-
dir = FindFirstFileA("./PyLoader/*.py", &fileData);
126-
127143
// Init our modules
128144
PyImport_AppendInittab("_bass", &PyBass::Init);
129145
PyImport_AppendInittab("_common", &PyCommon::Init);
@@ -142,20 +158,8 @@ void PyLoader::PyMain(void* param)
142158
SoundSystem.Inject();
143159
SoundSystem.Init(RsGlobal.ps->window);
144160

145-
// Loading .py scripts
146-
if (dir != INVALID_HANDLE_VALUE)
147-
{
148-
do
149-
{
150-
std::string* fileName = new std::string("PyLoader." + std::string(fileData.cFileName));
151-
152-
// remove the extension
153-
fileName->erase(fileName->end() - 3, fileName->end());
154-
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::ExecuteScript, fileName, NULL, NULL);
155-
Sleep(100);
156-
} while (FindNextFile(dir, &fileData));
157-
}
158-
161+
LoadScripts();
162+
159163
while (true)
160164
{
161165
// Check for infinite looping scripts

src/PyLoader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class PyLoader
99

1010
static void CheckUpdate();
1111
static int ExecuteScript(std::string *file_name);
12+
static void LoadScripts();
1213
static void LoadPlugins(std::string&& dir_name);
1314
static void PyMain(void* param);
1415
};

src/sdk/PyScript.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ PyObject* PyScript::Reload(PyObject* self, PyObject* args)
4545
return PyBool_FromLong(0);
4646
}
4747

48+
PyObject* PyScript::ReloadAll(PyObject* self, PyObject* args)
49+
{
50+
for (auto it = ScriptData::scripts->begin(); it != ScriptData::scripts->end(); ++it)
51+
{
52+
PyEvents::ScriptTerminate((*it)->m_pModule);
53+
gLog << "Unloading script " << (*it)->fileName << std::endl;
54+
}
55+
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::LoadScripts, NULL, NULL, NULL);
56+
return PyBool_FromLong(0);
57+
}
58+
4859
PyObject* PyScript::Unload(PyObject* self, PyObject* args)
4960
{
5061
char* str = NULL;

src/sdk/PyScript.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class PyScript
1616
static PyObject* MinRequiredVersion(PyObject* self, PyObject* args);
1717
static PyObject* Load(PyObject* self, PyObject* args);
1818
static PyObject* Reload(PyObject* self, PyObject* args);
19+
static PyObject* ReloadAll(PyObject* self, PyObject* args);
1920
static PyObject* Unload(PyObject* self, PyObject* args);
2021
static PyObject* SetProperties(PyObject* self, PyObject* args);
2122
static inline PyMethodDef Methods[] =
@@ -32,6 +33,7 @@ class PyScript
3233
{"minimum_version", MinRequiredVersion, METH_VARARGS},
3334
{"load", Load, METH_VARARGS},
3435
{"reload", Reload, METH_VARARGS},
36+
{"reload_all", ReloadAll, METH_VARARGS},
3537
{"unload", Unload, METH_VARARGS},
3638
{"properties", SetProperties, METH_VARARGS},
3739
{} // sentinel

0 commit comments

Comments
 (0)