-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScripts.h
More file actions
227 lines (190 loc) 路 6.52 KB
/
Copy pathScripts.h
File metadata and controls
227 lines (190 loc) 路 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#pragma once
#include <iostream>
#include <string>
#include <filesystem>
#include <vector>
#include <fstream>
#include "../utils/Logger.h"
#include "../settings/Settings.h"
#include <Python.h>
#include "../external/pybind11/include/pybind11/embed.h"
#include "modules/Logger.h"
#include "modules/Settings.h"
#include "modules/Events.h"
#include "modules/Actors.h"
#include "modules/Player.h"
#include "modules/Engine.h"
#include "modules/Render.h"
#include "modules/Input.h"
#include "modules/Game.h"
/**
* @file
* @brief Embedded CPython scripting host (pybind11).
*
* Owns the interpreter and discovers/loads user scripts from the `UserScripts` folder. The
* `SplitgateInternal` embedded module (defined here) exposes the Logger/Settings/Events
* submodules to those scripts.
*/
namespace py = pybind11;
namespace fs = std::filesystem;
/// The `SplitgateInternal` module user scripts import; wires up the Logger/Settings/Events
/// submodules on the embedded interpreter.
PYBIND11_EMBEDDED_MODULE(SplitgateInternal, m)
{
m.doc() = "Splitgate Internal plugin";
Scripts::Modules::Logger(m);
Scripts::Modules::Settings(m);
Scripts::Modules::Events(m);
Scripts::Modules::Actors(m);
Scripts::Modules::Player(m);
Scripts::Modules::Engine(m);
Scripts::Modules::Render(m);
Scripts::Modules::Input(m);
Scripts::Modules::Game(m);
}
namespace Scripts
{
std::vector<std::string> scriptList{}; ///< discovered *.py filenames under scriptsPath
std::vector<pybind11::module_> loadedScripts{}; ///< successfully imported script modules
fs::path scriptsPath; ///< the UserScripts folder
py::scoped_interpreter guard{}; ///< owns the embedded interpreter for the DLL's lifetime
/// Resolves the UserScripts folder, discovers every `*.py` (except `__init__.py`), and
/// imports each into @ref loadedScripts. Creates the folder if missing. Import errors are
/// logged and skipped, never thrown.
void Init()
{
fs::path scriptsFolder("UserScripts");
scriptsPath = Shared::AppDataPath(SettingsHelper::AppFolder) / scriptsFolder;
Logger::Log("INFO", std::format("Loading scripts from {}", scriptsPath.string()));
if (!fs::exists(scriptsPath) || !fs::is_directory(scriptsPath))
{
Logger::Log("ERROR", "UserScripts directory does not exist or is not a valid directory.");
fs::create_directories(scriptsPath);
return;
}
try
{
for (const auto& entry : fs::directory_iterator(scriptsPath))
{
fs::path filename = entry.path().filename();
if (filename.string() != "__init__.py" && filename.string().find(".py") != std::string::npos)
{
scriptList.push_back(filename.string());
}
}
}
catch (const std::exception& e)
{
Logger::Log("ERROR", e.what());
}
for (int i = 0; i < scriptList.size(); i++)
{
try
{
std::string filename = scriptList.at(i);
std::size_t ext = filename.find(".py");
std::string scriptName = std::string(filename);
if (ext != std::string::npos)
scriptName.erase(ext, 3);
std::string userScriptModule = "UserScripts." + scriptName;
auto scriptModule = py::module::import(userScriptModule.c_str());
loadedScripts.push_back(scriptModule);
Logger::Log("INFO", std::format("UserScript {} loaded", filename));
}
catch (py::error_already_set& e)
{
Logger::Log("ERROR", e.what());
}
}
Logger::Log("SUCCESS", std::format("Loaded {} scripts", loadedScripts.size()));
};
/// Calls `main()` on an already-loaded script. Out-of-range indices and Python errors are
/// ignored/logged.
/// @param loadedScriptIndex index into @ref loadedScripts.
void Execute(int loadedScriptIndex)
{
if (loadedScriptIndex >= loadedScripts.size()) return;
try
{
auto script = loadedScripts.at(loadedScriptIndex);
auto function = script.attr("main");
function();
}
catch (py::error_already_set& e)
{
Logger::Log("ERROR", e.what());
}
};
/// Imports a script by filename and calls its `main()` immediately, without adding it to
/// @ref loadedScripts. Used to run a script that wasn't loaded at startup.
/// @param filename the script's file name (with or without the `.py` extension).
void ExecuteUnloaded(std::string filename)
{
try
{
std::size_t ext = filename.find(".py");
std::string scriptName = std::string(filename);
if (ext != std::string::npos)
scriptName.erase(ext, 3);
std::string userScriptModule = "UserScripts." + scriptName;
auto scriptModule = py::module::import(userScriptModule.c_str());
Logger::Log("INFO", std::format("UserScript {} loaded", filename));
auto function = scriptModule.attr("main");
function();
return;
}
catch (py::error_already_set& e)
{
Logger::Log("ERROR", e.what());
}
}
/// Hot-reload: re-discover the UserScripts folder and re-import every script (via
/// `importlib.reload`, so edited files take effect without a relaunch). Both script-registered
/// bus subscriptions (`Events.on`) and custom-event handlers (`Events.on_custom`) are cleared
/// first, so re-importing doesn't stack duplicate handlers. New `.py` files are picked up;
/// deleted ones stop running (their handlers were cleared and won't re-register).
void Reload()
{
Modules::ClearCustomEvents();
Modules::ClearScriptHandlers(); // drop the previous import's bus subscriptions so they don't stack
loadedScripts.clear();
scriptList.clear();
if (!fs::exists(scriptsPath) || !fs::is_directory(scriptsPath)) return;
try
{
for (const auto& entry : fs::directory_iterator(scriptsPath))
{
const std::string filename = entry.path().filename().string();
if (filename != "__init__.py" && filename.find(".py") != std::string::npos)
scriptList.push_back(filename);
}
}
catch (const std::exception& e)
{
Logger::Log("ERROR", e.what());
return;
}
for (const auto& filename : scriptList)
{
try
{
std::string scriptName = filename;
const std::size_t ext = scriptName.find(".py");
if (ext != std::string::npos) scriptName.erase(ext, 3);
const std::string moduleName = "UserScripts." + scriptName;
auto sysModules = py::module_::import("sys").attr("modules");
py::module_ scriptModule;
if (sysModules.contains(moduleName))
scriptModule = py::module_::import("importlib").attr("reload")(sysModules[py::str(moduleName)]);
else
scriptModule = py::module_::import(moduleName.c_str());
loadedScripts.push_back(scriptModule);
}
catch (py::error_already_set& e)
{
Logger::Log("ERROR", e.what());
}
}
Logger::Log("SUCCESS", std::format("Reloaded {} scripts", loadedScripts.size()));
}
} // namespace Scripts