2022-04-08 15:18:06 +00:00
|
|
|
#include "dynos.cpp.h"
|
2022-06-02 19:19:54 +00:00
|
|
|
|
2022-04-08 15:18:06 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "engine/level_script.h"
|
|
|
|
#include "game/skybox.h"
|
|
|
|
}
|
|
|
|
|
2022-04-09 06:01:41 +00:00
|
|
|
struct OverrideLevelScript {
|
|
|
|
const void* originalScript;
|
|
|
|
const void* newScript;
|
|
|
|
GfxData* gfxData;
|
|
|
|
};
|
|
|
|
|
2022-04-20 04:06:18 +00:00
|
|
|
static Array<struct OverrideLevelScript>& DynosOverrideLevelScripts() {
|
|
|
|
static Array<struct OverrideLevelScript> sDynosOverrideLevelScripts;
|
|
|
|
return sDynosOverrideLevelScripts;
|
|
|
|
}
|
2022-04-08 15:18:06 +00:00
|
|
|
|
|
|
|
Array<Pair<const char*, GfxData*>> &DynOS_Lvl_GetArray() {
|
2022-04-20 04:06:18 +00:00
|
|
|
static Array<Pair<const char*, GfxData*>> sDynosCustomLevelScripts;
|
2022-04-08 15:18:06 +00:00
|
|
|
return sDynosCustomLevelScripts;
|
|
|
|
}
|
|
|
|
|
2023-08-18 16:31:59 +00:00
|
|
|
LevelScript* DynOS_Lvl_GetScript(const char* aScriptEntryName) {
|
2022-06-06 04:55:31 +00:00
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
for (s32 i = 0; i < _CustomLevelScripts.Count(); ++i) {
|
|
|
|
auto& pair = _CustomLevelScripts[i];
|
|
|
|
if (!strcmp(pair.first, aScriptEntryName)) {
|
|
|
|
auto& newScripts = pair.second->mLevelScripts;
|
|
|
|
auto& newScriptNode = newScripts[newScripts.Count() - 1];
|
|
|
|
return newScriptNode->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-09 14:27:47 +00:00
|
|
|
void DynOS_Lvl_ModShutdown() {
|
|
|
|
DynOS_Level_Unoverride();
|
|
|
|
|
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
while (_CustomLevelScripts.Count() > 0) {
|
2023-06-27 22:38:08 +00:00
|
|
|
for (auto& pair : _CustomLevelScripts) {
|
|
|
|
DynOS_Tex_Invalid(pair.second);
|
|
|
|
Delete(pair.second);
|
|
|
|
free((void*)pair.first);
|
|
|
|
}
|
|
|
|
_CustomLevelScripts.Clear();
|
2022-05-09 14:27:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
auto& _OverrideLevelScripts = DynosOverrideLevelScripts();
|
|
|
|
_OverrideLevelScripts.Clear();
|
|
|
|
}
|
|
|
|
|
2022-04-16 20:05:10 +00:00
|
|
|
void DynOS_Lvl_Activate(s32 modIndex, const SysPath &aFilename, const char *aLevelName) {
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
auto& _OverrideLevelScripts = DynosOverrideLevelScripts();
|
|
|
|
|
2022-04-10 06:28:36 +00:00
|
|
|
// make sure vanilla levels were parsed
|
2023-10-27 23:42:27 +00:00
|
|
|
DynOS_Level_Init();
|
2022-04-10 06:28:36 +00:00
|
|
|
|
2022-04-08 15:18:06 +00:00
|
|
|
// check for duplicates
|
2022-04-20 04:06:18 +00:00
|
|
|
for (s32 i = 0; i < _CustomLevelScripts.Count(); ++i) {
|
|
|
|
if (!strcmp(_CustomLevelScripts[i].first, aLevelName)) {
|
2022-04-08 15:18:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u16 levelLen = strlen(aLevelName);
|
|
|
|
char* levelName = (char*)calloc(1, sizeof(char) * (levelLen + 1));
|
|
|
|
strcpy(levelName, aLevelName);
|
|
|
|
|
2022-04-16 20:05:10 +00:00
|
|
|
GfxData* _Node = DynOS_Lvl_LoadFromBinary(aFilename, levelName);
|
2022-04-08 15:18:06 +00:00
|
|
|
if (!_Node) {
|
|
|
|
free(levelName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// remember index
|
|
|
|
_Node->mModIndex = modIndex;
|
|
|
|
|
|
|
|
// Add to levels
|
2022-04-20 04:06:18 +00:00
|
|
|
_CustomLevelScripts.Add({ levelName, _Node });
|
2022-06-06 04:55:31 +00:00
|
|
|
DynOS_Tex_Valid(_Node);
|
2022-04-08 15:18:06 +00:00
|
|
|
|
2022-04-09 02:29:16 +00:00
|
|
|
// Override vanilla script
|
2022-04-10 07:30:47 +00:00
|
|
|
auto& newScripts = _Node->mLevelScripts;
|
2022-04-09 02:29:16 +00:00
|
|
|
auto& newScriptNode = newScripts[newScripts.Count() - 1];
|
|
|
|
const void* originalScript = DynOS_Builtin_ScriptPtr_GetFromName(newScriptNode->mName.begin());
|
|
|
|
if (originalScript == NULL) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintError("Could not find level to override: '%s'", newScriptNode->mName.begin());
|
2022-04-09 02:29:16 +00:00
|
|
|
return;
|
2022-04-08 15:18:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 23:55:29 +00:00
|
|
|
DynOS_Level_Override((void*)originalScript, newScriptNode->mData, modIndex);
|
2022-04-20 04:06:18 +00:00
|
|
|
_OverrideLevelScripts.Add({ originalScript, newScriptNode->mData, _Node});
|
2022-04-08 15:18:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-09 08:47:20 +00:00
|
|
|
GfxData* DynOS_Lvl_GetActiveGfx(void) {
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
for (s32 i = 0; i < _CustomLevelScripts.Count(); ++i) {
|
|
|
|
auto& gfxData = _CustomLevelScripts[i].second;
|
2022-04-08 15:18:06 +00:00
|
|
|
auto& scripts = gfxData->mLevelScripts;
|
2023-06-05 22:19:19 +00:00
|
|
|
for (auto& s : scripts) {
|
|
|
|
if (gLevelScriptActive == s->mData) {
|
|
|
|
return gfxData;
|
|
|
|
}
|
2022-04-08 15:18:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* DynOS_Lvl_GetToken(u32 index) {
|
|
|
|
GfxData* gfxData = DynOS_Lvl_GetActiveGfx();
|
|
|
|
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;
|
|
|
|
}
|
2023-10-27 23:42:27 +00:00
|
|
|
|
2022-04-08 15:18:06 +00:00
|
|
|
return gfxData->mLuaTokenList[index].begin();
|
|
|
|
}
|
|
|
|
|
2022-04-09 06:01:41 +00:00
|
|
|
Trajectory* DynOS_Lvl_GetTrajectory(const char* aName) {
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
|
|
|
|
for (auto& script : _CustomLevelScripts) {
|
2022-04-09 06:01:41 +00:00
|
|
|
for (auto& trajectoryNode : script.second->mTrajectories) {
|
|
|
|
if (trajectoryNode->mName == aName) {
|
|
|
|
return trajectoryNode->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-08 15:18:06 +00:00
|
|
|
void DynOS_Lvl_LoadBackground(void *aPtr) {
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
|
2022-04-08 15:18:06 +00:00
|
|
|
// ensure this texture list exists
|
|
|
|
GfxData* foundGfxData = NULL;
|
|
|
|
DataNode<TexData*>* foundList = NULL;
|
2022-04-20 04:06:18 +00:00
|
|
|
for (auto& script : _CustomLevelScripts) {
|
2022-04-08 15:18:06 +00:00
|
|
|
auto &textureLists = script.second->mTextureLists;
|
|
|
|
for (auto& textureList : textureLists) {
|
|
|
|
if (textureList == aPtr) {
|
|
|
|
foundGfxData = script.second;
|
|
|
|
foundList = textureList;
|
|
|
|
goto double_break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
double_break:
|
|
|
|
|
|
|
|
if (foundList == NULL) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintError("Could not find custom background");
|
2022-04-08 15:18:06 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load up custom background
|
|
|
|
for (s32 i = 0; i < 80; i++) {
|
|
|
|
// find texture
|
|
|
|
for (auto& tex : foundGfxData->mTextures) {
|
|
|
|
if (tex->mData == foundList->mData[i]) {
|
|
|
|
gCustomSkyboxPtrList[i] = (Texture*)tex;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 02:29:16 +00:00
|
|
|
|
|
|
|
void *DynOS_Lvl_Override(void *aCmd) {
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _OverrideLevelScripts = DynosOverrideLevelScripts();
|
|
|
|
for (auto& overrideStruct : _OverrideLevelScripts) {
|
2022-04-09 06:01:41 +00:00
|
|
|
if (aCmd == overrideStruct.originalScript || aCmd == overrideStruct.newScript) {
|
|
|
|
aCmd = (void*)overrideStruct.newScript;
|
|
|
|
gLevelScriptModIndex = overrideStruct.gfxData->mModIndex;
|
|
|
|
gLevelScriptActive = (LevelScript*)aCmd;
|
2022-04-09 02:29:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-05-31 23:56:57 +00:00
|
|
|
|
|
|
|
auto& _CustomLevelScripts = DynOS_Lvl_GetArray();
|
|
|
|
for (auto& script : _CustomLevelScripts) {
|
2023-06-05 22:19:19 +00:00
|
|
|
auto& scripts = script.second->mLevelScripts;
|
|
|
|
for (auto& s : scripts) {
|
|
|
|
if (aCmd == s->mData) {
|
|
|
|
gLevelScriptModIndex = script.second->mModIndex;
|
|
|
|
gLevelScriptActive = (LevelScript*)aCmd;
|
|
|
|
}
|
2023-05-31 23:56:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-09 02:29:16 +00:00
|
|
|
return aCmd;
|
|
|
|
}
|
2022-06-06 04:55:31 +00:00
|
|
|
|