-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more events #1, Check [here](https://user-grinch.gitbook.io/pyl…
…oader/api/events) for list
- Loading branch information
1 parent
0470fa1
commit 7bc3958
Showing
10 changed files
with
276 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
#pragma once | ||
|
||
namespace PyEventsInternal | ||
{ | ||
class EventBase | ||
{ | ||
protected: | ||
std::vector<PyObject*> pFuncs; | ||
|
||
public: | ||
void AddModule(PyObject* pModule, std::string func_name) | ||
{ | ||
PyObject* pFunc = PyObject_GetAttrString(pModule, func_name.c_str()); | ||
if (pFunc) | ||
{ | ||
pFuncs.push_back(pFunc); | ||
Py_INCREF(pFunc); | ||
} | ||
} | ||
|
||
void RemoveFunc(PyObject* pFunc) | ||
{ | ||
for (auto it = pFuncs.begin(); it != pFuncs.end(); ++it) | ||
{ | ||
if (*it == pFunc) | ||
{ | ||
pFuncs.erase(it); | ||
Py_XDECREF(*it); | ||
} | ||
} | ||
} | ||
|
||
virtual void Callback(void* ptr) = 0; | ||
virtual void Init() = 0; | ||
}; | ||
|
||
class VehEvent : public EventBase | ||
{ | ||
public: | ||
void Callback(void* ptr) | ||
{ | ||
if (!ptr) | ||
return; | ||
|
||
CVehicle* pVeh = (CVehicle*)ptr; | ||
PyGILState_STATE gstate = PyGILState_Ensure(); | ||
|
||
for (auto it = pFuncs.begin(); it != pFuncs.end(); ++it) | ||
{ | ||
PyObject* pFunc = *it; | ||
if (pFunc && PyCallable_Check(pFunc)) | ||
{ | ||
PyObject* pArgs = PyTuple_New(1); | ||
PyObject_CallFunction(pFunc, "i", CPools::GetVehicleRef(pVeh)); | ||
} | ||
} | ||
PyGILState_Release(gstate); | ||
} | ||
}; | ||
|
||
class PedEvent : public EventBase | ||
{ | ||
public: | ||
void Callback(void* ptr) | ||
{ | ||
if (!ptr) | ||
return; | ||
|
||
CPed* pPed = (CPed*)ptr; | ||
PyGILState_STATE gstate = PyGILState_Ensure(); | ||
|
||
for (auto it = pFuncs.begin(); it != pFuncs.end(); ++it) | ||
{ | ||
PyObject* pFunc = *it; | ||
if (pFunc && PyCallable_Check(pFunc)) | ||
{ | ||
PyObject* pArgs = PyTuple_New(1); | ||
PyObject_CallFunction(pFunc, "i", CPools::GetPedRef(pPed)); | ||
} | ||
} | ||
PyGILState_Release(gstate); | ||
} | ||
}; | ||
|
||
class ObjEvent : public EventBase | ||
{ | ||
public: | ||
void Callback(void* ptr) | ||
{ | ||
if (!ptr) | ||
return; | ||
|
||
CObject* pObj = (CObject*)ptr; | ||
PyGILState_STATE gstate = PyGILState_Ensure(); | ||
|
||
for (auto it = pFuncs.begin(); it != pFuncs.end(); ++it) | ||
{ | ||
PyObject* pFunc = *it; | ||
if (pFunc && PyCallable_Check(pFunc)) | ||
{ | ||
PyObject* pArgs = PyTuple_New(1); | ||
PyObject_CallFunction(pFunc, "i", CPools::GetObjectRef(pObj)); | ||
} | ||
} | ||
PyGILState_Release(gstate); | ||
} | ||
}; | ||
|
||
class VehCreateEvent : public VehEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::vehicleCtorEvent += [&](CVehicle* pVeh) | ||
{ | ||
Callback(pVeh); | ||
}; | ||
} | ||
}; | ||
|
||
class VehRenderEvent : public VehEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::vehicleRenderEvent += [&](CVehicle* pVeh) | ||
{ | ||
Callback(pVeh); | ||
}; | ||
} | ||
}; | ||
|
||
class VehDestroyEvent : public VehEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::vehicleDtorEvent += [&](CVehicle* pVeh) | ||
{ | ||
Callback(pVeh); | ||
}; | ||
} | ||
}; | ||
|
||
class PedCreateEvent : public PedEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::pedCtorEvent += [&](CPed* pPed) | ||
{ | ||
Callback(pPed); | ||
}; | ||
} | ||
}; | ||
|
||
class PedRenderEvent : public PedEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::pedRenderEvent += [&](CPed* pPed) | ||
{ | ||
Callback(pPed); | ||
}; | ||
} | ||
}; | ||
|
||
class PedDestroyEvent : public PedEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::pedDtorEvent += [&](CPed* pPed) | ||
{ | ||
Callback(pPed); | ||
}; | ||
} | ||
}; | ||
|
||
class ObjCreateEvent : public ObjEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::objectCtorEvent += [&](CObject* pObj) | ||
{ | ||
Callback(pObj); | ||
}; | ||
} | ||
}; | ||
|
||
class ObjRenderEvent : public ObjEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::objectRenderEvent += [&](CObject* pObj) | ||
{ | ||
Callback(pObj); | ||
}; | ||
} | ||
}; | ||
|
||
class ObjDestroyEvent : public ObjEvent | ||
{ | ||
public: | ||
void Init() | ||
{ | ||
plugin::Events::objectDtorEvent += [&](CObject* pObj) | ||
{ | ||
Callback(pObj); | ||
}; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import common | ||
|
||
# Can be used for necessary cleanup | ||
def on_script_terminate(error_occured: bool): | ||
if error_occured: | ||
print("Script terminated") | ||
else: | ||
print("Script exited") | ||
|
||
while True: | ||
if common.key_pressed(0x09): | ||
break | ||
|
||
common.wait(0) | ||
|
||
|
||
|
||
|
||
|