2022-06-02 19:19:54 +00:00
|
|
|
#include "dynos.cpp.h"
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
#include "engine/behavior_script.h"
|
2022-08-06 03:55:31 +00:00
|
|
|
#include "pc/network/packets/packet.h"
|
|
|
|
#include "pc/lua/smlua_hooks.h"
|
2022-06-02 19:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Array<Pair<const char *, GfxData *>> &DynOS_Bhv_GetArray() {
|
|
|
|
static Array<Pair<const char *, GfxData *>> sDynosCustomBehaviorScripts;
|
|
|
|
return sDynosCustomBehaviorScripts;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Bhv_Activate(s32 modIndex, const SysPath &aFilename, const char *aBehaviorName) {
|
|
|
|
auto &_CustomBehaviorScripts = DynOS_Bhv_GetArray();
|
|
|
|
|
|
|
|
// check for duplicates
|
|
|
|
for (s32 i = 0; i < _CustomBehaviorScripts.Count(); ++i) {
|
|
|
|
if (!strcmp(_CustomBehaviorScripts[i].first, aBehaviorName)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 behaviorLen = strlen(aBehaviorName);
|
|
|
|
char *behaviorName = (char *)calloc(1, sizeof(char) * (behaviorLen + 1));
|
|
|
|
strcpy(behaviorName, aBehaviorName);
|
|
|
|
|
|
|
|
GfxData *_Node = DynOS_Bhv_LoadFromBinary(aFilename, behaviorName);
|
|
|
|
if (!_Node) {
|
|
|
|
free(behaviorName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember index
|
|
|
|
_Node->mModIndex = modIndex;
|
|
|
|
|
|
|
|
// Add to behaviors
|
|
|
|
_CustomBehaviorScripts.Add({ behaviorName, _Node });
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Bhv_ModShutdown() {
|
|
|
|
auto &_CustomBehaviorScripts = DynOS_Bhv_GetArray();
|
|
|
|
while (_CustomBehaviorScripts.Count() > 0) {
|
|
|
|
auto &pair = _CustomBehaviorScripts[0];
|
|
|
|
Delete(pair.second);
|
|
|
|
free((void *)pair.first);
|
|
|
|
_CustomBehaviorScripts.Remove(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
GfxData *DynOS_Bhv_GetActiveGfx(BehaviorScript *bhvScript) {
|
|
|
|
auto &_CustomBehaviorScripts = DynOS_Bhv_GetArray();
|
|
|
|
|
|
|
|
for (s32 i = 0; i < _CustomBehaviorScripts.Count(); ++i) {
|
|
|
|
auto &gfxData = _CustomBehaviorScripts[i].second;
|
|
|
|
auto &scripts = gfxData->mBehaviorScripts;
|
2023-02-22 20:12:09 +00:00
|
|
|
if (scripts.Count() == 0) { continue; }
|
2022-06-02 19:19:54 +00:00
|
|
|
if (bhvScript == scripts[scripts.Count() - 1]->mData) {
|
|
|
|
return gfxData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s32 DynOS_Bhv_GetActiveModIndex(BehaviorScript *bhvScript) {
|
|
|
|
auto &_CustomBehaviorScripts = DynOS_Bhv_GetArray();
|
|
|
|
|
|
|
|
for (s32 i = 0; i < _CustomBehaviorScripts.Count(); ++i) {
|
|
|
|
auto &gfxData = _CustomBehaviorScripts[i].second;
|
|
|
|
auto &scripts = gfxData->mBehaviorScripts;
|
2023-02-22 20:12:09 +00:00
|
|
|
if (scripts.Count() == 0) { continue; }
|
2022-06-02 19:19:54 +00:00
|
|
|
if (bhvScript == scripts[scripts.Count() - 1]->mData) {
|
|
|
|
return gfxData->mModIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *DynOS_Bhv_GetToken(BehaviorScript *bhvScript, u32 index) {
|
|
|
|
GfxData *gfxData = DynOS_Bhv_GetActiveGfx(bhvScript);
|
|
|
|
if (gfxData == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// have to 1-index due to to pointer read code
|
|
|
|
index = index - 1;
|
|
|
|
|
|
|
|
if (index >= gfxData->mLuaTokenList.Count()) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2022-06-03 02:33:28 +00:00
|
|
|
|
2022-06-02 19:19:54 +00:00
|
|
|
return gfxData->mLuaTokenList[index].begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Bhv_HookAllCustomBehaviors() {
|
|
|
|
auto &_CustomBehaviorScripts = DynOS_Bhv_GetArray();
|
|
|
|
|
|
|
|
for (s32 i = 0; i < _CustomBehaviorScripts.Count(); ++i) {
|
|
|
|
auto &scriptName = _CustomBehaviorScripts[i].first;
|
|
|
|
auto &aGfxData = _CustomBehaviorScripts[i].second;
|
2023-02-22 20:12:09 +00:00
|
|
|
if (aGfxData->mBehaviorScripts.Count() == 0) { continue; }
|
|
|
|
auto *node = aGfxData->mBehaviorScripts[aGfxData->mBehaviorScripts.Count() - 1];
|
|
|
|
if (node == nullptr) { continue; }
|
|
|
|
auto &script = node->mData;
|
2022-06-03 02:33:28 +00:00
|
|
|
|
2022-06-02 19:19:54 +00:00
|
|
|
// Theres currently no better place but to do this here.
|
2022-06-07 02:13:04 +00:00
|
|
|
if (smlua_hook_custom_bhv(script, scriptName) == 0) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintDataError(" ERROR: Failed to add custom behavior '%s'!", scriptName);
|
2022-06-02 19:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|