Skip to content

Commit

Permalink
Add reload_all with new scripts support
Browse files Browse the repository at this point in the history
  • Loading branch information
user-grinch committed Oct 5, 2021
1 parent fbf60c2 commit 777ac8a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 29 deletions.
9 changes: 1 addition & 8 deletions examples/ReloadAll.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import libstd.common as common
import libstd.pool as pool
import libstd.script as script

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

# Reloading other scripts
name : str = script.get_file_name()
for script_name in pool.script():
if script_name != name:
script.reload(script_name)

script.reload() # Reloading current script
script.reload_all()

common.wait(0)

Expand Down
4 changes: 2 additions & 2 deletions runtime/PyLoader/libstd/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ def set_raw(address :int, data :str, size :int, virtual_protect :bool = True) ->
_memory.set_raw(address, data, size, virtual_protect)

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

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

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

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

Expand Down
5 changes: 5 additions & 0 deletions runtime/PyLoader/libstd/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ def reload(script_name :str = "") -> None:

_script.reload(script_name)

def reload_all(script_name :str = "") -> None:
'''Unloads all scripts, then loads them again from PyLoader directory. Loads new scripts too'''

_script.reload_all(script_name)

def properties(*argv) -> bool:
'''Sets script specific property flags.\n\nValid properties:\n\nno_reload: Disables script reloading'''

Expand Down
42 changes: 23 additions & 19 deletions src/PyLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ void PyLoader::CheckUpdate()
std::remove(path);
}

void PyLoader::LoadScripts()
{
HANDLE dir;
WIN32_FIND_DATA fileData;
dir = FindFirstFileA((LPCSTR)PLUGIN_PATH((char*)"/PyLoader/*.py"), &fileData);

// Loading .py scripts
if (dir != INVALID_HANDLE_VALUE)
{
do
{
std::string* fileName = new std::string("PyLoader." + std::string(fileData.cFileName));

// remove the extension
fileName->erase(fileName->end() - 3, fileName->end());
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::ExecuteScript, fileName, NULL, NULL);
Sleep(50);
} while (FindNextFile(dir, &fileData));
}
}

void PyLoader::PyMain(void* param)
{
plugin::Events::processScriptsEvent += []
Expand All @@ -111,9 +132,6 @@ void PyLoader::PyMain(void* param)

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

HANDLE dir;
WIN32_FIND_DATA fileData;

/*
Load all the .dll modules from lib and libstd folder
libstd folder is reserved for first party modules only
Expand All @@ -122,8 +140,6 @@ void PyLoader::PyMain(void* param)
LoadPlugins("lib");
LoadPlugins("libstd");

dir = FindFirstFileA("./PyLoader/*.py", &fileData);

// Init our modules
PyImport_AppendInittab("_bass", &PyBass::Init);
PyImport_AppendInittab("_common", &PyCommon::Init);
Expand All @@ -142,20 +158,8 @@ void PyLoader::PyMain(void* param)
SoundSystem.Inject();
SoundSystem.Init(RsGlobal.ps->window);

// Loading .py scripts
if (dir != INVALID_HANDLE_VALUE)
{
do
{
std::string* fileName = new std::string("PyLoader." + std::string(fileData.cFileName));

// remove the extension
fileName->erase(fileName->end() - 3, fileName->end());
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::ExecuteScript, fileName, NULL, NULL);
Sleep(100);
} while (FindNextFile(dir, &fileData));
}

LoadScripts();

while (true)
{
// Check for infinite looping scripts
Expand Down
1 change: 1 addition & 0 deletions src/PyLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class PyLoader

static void CheckUpdate();
static int ExecuteScript(std::string *file_name);
static void LoadScripts();
static void LoadPlugins(std::string&& dir_name);
static void PyMain(void* param);
};
11 changes: 11 additions & 0 deletions src/sdk/PyScript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,17 @@ PyObject* PyScript::Reload(PyObject* self, PyObject* args)
return PyBool_FromLong(0);
}

PyObject* PyScript::ReloadAll(PyObject* self, PyObject* args)
{
for (auto it = ScriptData::scripts->begin(); it != ScriptData::scripts->end(); ++it)
{
PyEvents::ScriptTerminate((*it)->m_pModule);
gLog << "Unloading script " << (*it)->fileName << std::endl;
}
CreateThread(NULL, NULL, (LPTHREAD_START_ROUTINE)&PyLoader::LoadScripts, NULL, NULL, NULL);
return PyBool_FromLong(0);
}

PyObject* PyScript::Unload(PyObject* self, PyObject* args)
{
char* str = NULL;
Expand Down
2 changes: 2 additions & 0 deletions src/sdk/PyScript.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class PyScript
static PyObject* MinRequiredVersion(PyObject* self, PyObject* args);
static PyObject* Load(PyObject* self, PyObject* args);
static PyObject* Reload(PyObject* self, PyObject* args);
static PyObject* ReloadAll(PyObject* self, PyObject* args);
static PyObject* Unload(PyObject* self, PyObject* args);
static PyObject* SetProperties(PyObject* self, PyObject* args);
static inline PyMethodDef Methods[] =
Expand All @@ -32,6 +33,7 @@ class PyScript
{"minimum_version", MinRequiredVersion, METH_VARARGS},
{"load", Load, METH_VARARGS},
{"reload", Reload, METH_VARARGS},
{"reload_all", ReloadAll, METH_VARARGS},
{"unload", Unload, METH_VARARGS},
{"properties", SetProperties, METH_VARARGS},
{} // sentinel
Expand Down

0 comments on commit 777ac8a

Please sign in to comment.