Skip to content

Commit c55f7f7

Browse files
refactor python scripting files
1 parent 196fd69 commit c55f7f7

6 files changed

Lines changed: 103 additions & 642 deletions

File tree

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ set(PROCESS_SOURCES
8585
# Scripting system
8686
set(SCRIPTING_SOURCES
8787
src/scripting/PythonAPI.cpp
88+
#src/scripting/PythonEvent.cpp
89+
src/scripting/PythonModule.cpp
8890
src/scripting/PythonScripting.cpp
91+
src/scripting/ScriptHotReloader.cpp
8992
)
9093

9194
# Network system

include/scripting/PythonEvent.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
#pragma once
22

3-
#include <queue>
3+
#include <atomic>
44
#include <condition_variable>
5+
#include <functional>
6+
#include <memory>
7+
#include <mutex>
8+
#include <queue>
59
#include <thread>
6-
#include <atomic>
10+
#include <shared_mutex>
711
#include <string>
812
#include <vector>
9-
#include <functional>
1013
#include <unordered_map>
14+
1115
#include <nlohmann/json.hpp>
12-
#include <memory>
13-
#include <mutex>
1416

1517
// Event handler interface
1618
class IEventHandler {

include/scripting/PythonModule.hpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
#pragma once
22

3-
#include <Python.h>
3+
#include <mutex>
4+
#include <shared_mutex>
45
#include <string>
6+
57
#include <nlohmann/json.hpp>
6-
#include <mutex>
8+
#include <Python.h>
79

810

11+
// Python GIL helper
12+
class PyGILGuard {
13+
public:
14+
PyGILGuard() : state_(PyGILState_Ensure()) {}
15+
~PyGILGuard() { PyGILState_Release(state_); }
16+
17+
private:
18+
PyGILState_STATE state_;
19+
};
20+
921
class PythonModule {
1022
public:
1123
PythonModule(const std::string& moduleName, const std::string& filePath);

include/scripting/PythonScripting.hpp

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <nlohmann/json.hpp>
1616

1717
#include "scripting/PythonAPI.hpp"
18+
#include "scripting/PythonModule.hpp"
1819

1920
// Forward declarations
2021
//class Player;
@@ -36,16 +37,6 @@ class PyObjectRef {
3637
PyObject* obj_;
3738
};
3839

39-
// Python GIL helper
40-
class PyGILGuard {
41-
public:
42-
PyGILGuard() : state_(PyGILState_Ensure()) {}
43-
~PyGILGuard() { PyGILState_Release(state_); }
44-
45-
private:
46-
PyGILState_STATE state_;
47-
};
48-
4940
// Event types that can be handled by Python
5041
enum class EventType {
5142
PLAYER_LOGIN,
@@ -110,40 +101,6 @@ struct GameEvent {
110101
// Callback from Python to C++
111102
using PyCallback = std::function<void(const nlohmann::json&)>;
112103

113-
// Python module definition
114-
class PythonModule {
115-
public:
116-
PythonModule(const std::string& moduleName, const std::string& filePath);
117-
PythonModule(PythonModule&& other) noexcept;
118-
~PythonModule();
119-
PythonModule& operator=(PythonModule&& other) noexcept;
120-
121-
bool Load();
122-
bool Reload();
123-
void Unload();
124-
125-
bool CallFunction(const std::string& funcName, const nlohmann::json& args = {});
126-
nlohmann::json CallFunctionWithResult(const std::string& funcName,
127-
const nlohmann::json& args = {});
128-
129-
bool HasFunction(const std::string& funcName) const;
130-
std::string GetLastError() const;
131-
void SetError(const std::string& error);
132-
void ClearError();
133-
bool CheckPythonError();
134-
135-
private:
136-
std::string moduleName_;
137-
std::string filePath_;
138-
PyObject* module_;
139-
mutable std::mutex mutex_;
140-
std::string lastError_;
141-
142-
PyObject* CreatePyArgs(const nlohmann::json& args);
143-
nlohmann::json PyObjectToJson(PyObject* obj);
144-
PyObject* JsonToPyObject(const nlohmann::json& json);
145-
std::string PyObjectToString(PyObject* obj);
146-
};
147104

148105
// Main Python scripting engine
149106
class PythonScripting {

0 commit comments

Comments
 (0)