From 18835a588a385e9b2e22385f147a50f8ff62ceb5 Mon Sep 17 00:00:00 2001 From: MysterD Date: Fri, 8 Apr 2022 08:18:06 -0700 Subject: [PATCH] Split dynos_misc stuff into mgr files --- data/dynos.c.h | 2 +- data/dynos.cpp.h | 37 ++++--- data/dynos_c.cpp | 14 +-- data/dynos_gfx_texture.cpp | 2 +- data/dynos_level.cpp | 2 +- data/dynos_mgr_col.cpp | 47 +++++++++ data/dynos_mgr_lvl.cpp | 156 ++++++++++++++++++++++++++++ data/dynos_misc.cpp | 201 +------------------------------------ src/game/moving_texture.c | 2 +- 9 files changed, 237 insertions(+), 226 deletions(-) create mode 100644 data/dynos_mgr_col.cpp create mode 100644 data/dynos_mgr_lvl.cpp diff --git a/data/dynos.c.h b/data/dynos.c.h index cac6ea37..68d57713 100644 --- a/data/dynos.c.h +++ b/data/dynos.c.h @@ -38,7 +38,7 @@ Collision* dynos_collision_get(const char* collisionName); void dynos_add_level(s32 modIndex, const char *modPath, const char* levelName); LevelScript* dynos_level_get(const char* levelName); const char* dynos_level_get_token(u32 index); -struct MovtexQuadCollection *dynos_level_movtexqc_getfromindex(s32 index); +struct MovtexQuadCollection *dynos_level_get_movtexqc(s32 index); void dynos_level_load_background(void *ptr); #endif diff --git a/data/dynos.cpp.h b/data/dynos.cpp.h index 0e9298c1..68f1347b 100644 --- a/data/dynos.cpp.h +++ b/data/dynos.cpp.h @@ -712,14 +712,6 @@ s16 *DynOS_Level_GetWarp(s32 aLevel, s32 aArea, u8 aWarpId); s16 *DynOS_Level_GetWarpEntry(s32 aLevel, s32 aArea); s16 *DynOS_Level_GetWarpDeath(s32 aLevel, s32 aArea); -void DynOS_Lvl_Add(s32 modIndex, const SysPath &aPackFolder, const char *aLevelName); -LevelScript* DynOS_Lvl_Get(const char* levelName); -s32 DynOS_Lvl_GetModIndex(void* levelScript); -const char* DynOS_Lvl_Get_Token(u32 index); -DataNode *DynOS_Lvl_Texture_Get(void *aPtr); -DataNode *DynOS_Lvl_MovtexQuadCollection_GetFromIndex(s32 index); -void DynOS_Lvl_Load_Background(void *aPtr); - // // Warps // @@ -733,14 +725,7 @@ void DynOS_Warp_SetParam(s32 aLevel, s32 aIndex); const char *DynOS_Warp_GetParamName(s32 aLevel, s32 aIndex); // -// Collisions -// - -void DynOS_Col_Add(const SysPath &aPackFolder, const char *aCollisionName); -Collision* DynOS_Col_Get(const char* collisionName); - -// -// Managers +// Builtin // const void* DynOS_Builtin_ScriptPtr_GetFromName(const char* aDataName); @@ -755,6 +740,26 @@ const void* DynOS_Builtin_Func_GetFromName(const char* aDataName); const void* DynOS_Builtin_Func_GetFromIndex(s32 aIndex); s32 DynOS_Builtin_Func_GetIndexFromData(const void* aData); +// +// Lvl Manager +// + +Array> &DynOS_Lvl_GetArray(); +void DynOS_Lvl_Activate(s32 modIndex, const SysPath &aPackFolder, const char *aLevelName); +LevelScript* DynOS_Lvl_GetScript(const char* levelName); +s32 DynOS_Lvl_GetModIndex(void* levelScript); +DataNode *DynOS_Lvl_GetTexture(void *aPtr); +const char* DynOS_Lvl_GetToken(u32 index); +DataNode *DynOS_Lvl_GetMovtexQuadCollection(s32 index); +void DynOS_Lvl_LoadBackground(void *aPtr); + +// +// Col Manager +// + +void DynOS_Col_Activate(const SysPath &aPackFolder, const char *aCollisionName); +Collision* DynOS_Col_Get(const char* collisionName); + // // Bin // diff --git a/data/dynos_c.cpp b/data/dynos_c.cpp index c9eb72d0..d58b48ea 100644 --- a/data/dynos_c.cpp +++ b/data/dynos_c.cpp @@ -91,7 +91,7 @@ const void* dynos_geolayout_get(const char *name) { // -- collisions -- // void dynos_add_collision(const char *modPath, const char* collisionName) { - DynOS_Col_Add(modPath, collisionName); + DynOS_Col_Activate(modPath, collisionName); } Collision* dynos_collision_get(const char* collisionName) { @@ -101,26 +101,26 @@ Collision* dynos_collision_get(const char* collisionName) { // -- levels -- // void dynos_add_level(s32 modIndex, const char *modPath, const char* levelName) { - DynOS_Lvl_Add(modIndex, modPath, levelName); + DynOS_Lvl_Activate(modIndex, modPath, levelName); } LevelScript* dynos_level_get(const char* levelName) { - return DynOS_Lvl_Get(levelName); + return DynOS_Lvl_GetScript(levelName); } const char* dynos_level_get_token(u32 index) { - return DynOS_Lvl_Get_Token(index); + return DynOS_Lvl_GetToken(index); } -struct MovtexQuadCollection *dynos_level_movtexqc_getfromindex(s32 index) { - DataNode *node = DynOS_Lvl_MovtexQuadCollection_GetFromIndex(index); +struct MovtexQuadCollection *dynos_level_get_movtexqc(s32 index) { + DataNode *node = DynOS_Lvl_GetMovtexQuadCollection(index); if (node == NULL) { return NULL; } return node->mData; } void dynos_level_load_background(void *ptr) { - DynOS_Lvl_Load_Background(ptr); + DynOS_Lvl_LoadBackground(ptr); } } \ No newline at end of file diff --git a/data/dynos_gfx_texture.cpp b/data/dynos_gfx_texture.cpp index 0cfa8af4..e533c14f 100644 --- a/data/dynos_gfx_texture.cpp +++ b/data/dynos_gfx_texture.cpp @@ -257,7 +257,7 @@ static DataNode *DynOS_Gfx_RetrieveNode(void *aPtr) { } } } - return DynOS_Lvl_Texture_Get(aPtr); + return DynOS_Lvl_GetTexture(aPtr); } static bool DynOS_Gfx_ImportTexture_Typed(THN **aOutput, void *aPtr, s32 aTile, GRAPI *aGfxRApi, THN **aHashMap, THN *aPool, u32 *aPoolPos, u32 aPoolSize) { diff --git a/data/dynos_level.cpp b/data/dynos_level.cpp index c6e53927..c86c9c8f 100644 --- a/data/dynos_level.cpp +++ b/data/dynos_level.cpp @@ -228,7 +228,7 @@ s32 DynOS_Level_GetCourse(s32 aLevel) { const void *DynOS_Level_GetScript(s32 aLevel) { DynOS_Level_Init(); if (aLevel != LEVEL_WDW) { - LevelScript* script = DynOS_Lvl_Get(""); + LevelScript* script = DynOS_Lvl_GetScript(""); sDynosCurrentLevelNum = aLevel; sDynosLevelWarps[sDynosCurrentLevelNum].Clear(); DynOS_Level_ParseScript(script, DynOS_Level_PreprocessScript); diff --git a/data/dynos_mgr_col.cpp b/data/dynos_mgr_col.cpp new file mode 100644 index 00000000..0ce69fa1 --- /dev/null +++ b/data/dynos_mgr_col.cpp @@ -0,0 +1,47 @@ +#include "dynos.cpp.h" + +static Array*>> sDynosCollisions; + +void DynOS_Col_Activate(const SysPath &aPackFolder, const char *aCollisionName) { + // check for duplicates + for (s32 i = 0; i < sDynosCollisions.Count(); ++i) { + if (!strcmp(sDynosCollisions[i].first, aCollisionName)) { + return; + } + } + + // Allocate name + u16 collisionLen = strlen(aCollisionName); + char* collisionName = (char*)calloc(1, sizeof(char) * (collisionLen + 1)); + strcpy(collisionName, aCollisionName); + + // Load + DataNode* _Node = DynOS_Col_LoadFromBinary(aPackFolder, collisionName); + if (!_Node) { + free(collisionName); + return; + } + + // Add to collisions + sDynosCollisions.Add({ collisionName, _Node }); +} + +Collision* DynOS_Col_Get(const char* collisionName) { + // check levels + auto& levelsArray = DynOS_Lvl_GetArray(); + for (auto& lvl : levelsArray) { + for (auto& col : lvl.second->mCollisions) { + if (col->mName == collisionName) { + return col->mData; + } + } + } + + // check normal actor collisions + for (s32 i = 0; i < sDynosCollisions.Count(); ++i) { + if (!strcmp(sDynosCollisions[i].first, collisionName)) { + return sDynosCollisions[i].second->mData; + } + } + return NULL; +} diff --git a/data/dynos_mgr_lvl.cpp b/data/dynos_mgr_lvl.cpp new file mode 100644 index 00000000..08687599 --- /dev/null +++ b/data/dynos_mgr_lvl.cpp @@ -0,0 +1,156 @@ +#include "dynos.cpp.h" +extern "C" { +#include "engine/level_script.h" +#include "game/skybox.h" +} + +static Array> sDynosCustomLevelScripts; + +Array> &DynOS_Lvl_GetArray() { + return sDynosCustomLevelScripts; +} + +void DynOS_Lvl_Activate(s32 modIndex, const SysPath &aPackFolder, const char *aLevelName) { + // check for duplicates + for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { + if (!strcmp(sDynosCustomLevelScripts[i].first, aLevelName)) { + return; + } + } + + u16 levelLen = strlen(aLevelName); + char* levelName = (char*)calloc(1, sizeof(char) * (levelLen + 1)); + strcpy(levelName, aLevelName); + + GfxData* _Node = DynOS_Lvl_LoadFromBinary(aPackFolder, levelName); + if (!_Node) { + free(levelName); + return; + } + + // remember index + _Node->mModIndex = modIndex; + + // Add to levels + sDynosCustomLevelScripts.Add({ levelName, _Node }); +} + +LevelScript* DynOS_Lvl_GetScript(const char* levelName) { + if (strlen(levelName) == 0) { + static u32 index = 0; // DO NOT COMMIT + s32 levelScriptCount = sDynosCustomLevelScripts.Count(); // DO NOT COMMIT + if (levelScriptCount < 1) { return NULL; } // DO NOT COMMIT + index = (index + 1) % levelScriptCount; // DO NOT COMMIT + auto& scripts = sDynosCustomLevelScripts[index].second->mLevelScripts; // DO NOT COMMIT + Print("Going to level: %s\n", scripts[scripts.Count() - 1]->mName); // DO NOT COMMIT + return scripts[scripts.Count() - 1]->mData; // DO NOT COMMIT + } + + for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { + if (!strcmp(sDynosCustomLevelScripts[i].first, levelName)) { + auto& scripts = sDynosCustomLevelScripts[i].second->mLevelScripts; + return scripts[scripts.Count() - 1]->mData; + } + } + return NULL; +} + +s32 DynOS_Lvl_GetModIndex(void* levelScript) { + for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { + auto& scripts = sDynosCustomLevelScripts[i].second->mLevelScripts; + if (levelScript == scripts[scripts.Count() - 1]->mData) { + return sDynosCustomLevelScripts[i].second->mModIndex; + } + } + return -1; +} + +DataNode *DynOS_Lvl_GetTexture(void *aPtr) { + for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { + auto &mTextures = sDynosCustomLevelScripts[i].second->mTextures; + for (s32 j = 0; j < mTextures.Count(); j++) { + auto &texture = mTextures[j]; + if (texture == aPtr) { + return texture; + } + + } + } + return NULL; +} + +static GfxData* DynOS_Lvl_GetActiveGfx(void) { + for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { + auto& gfxData = sDynosCustomLevelScripts[i].second; + auto& scripts = gfxData->mLevelScripts; + if (gLevelScriptActive == scripts[scripts.Count() - 1]->mData) { + return gfxData; + } + } + 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; + } + + return gfxData->mLuaTokenList[index].begin(); +} + +DataNode *DynOS_Lvl_GetMovtexQuadCollection(s32 index) { + GfxData* gfxData = DynOS_Lvl_GetActiveGfx(); + if (gfxData == NULL) { + return NULL; + } + + auto &mMovtexQCs = gfxData->mMovtexQCs; + + // Sanity check the index we passed. + if (index < 0 || index >= mMovtexQCs.Count()) { + return NULL; + } + + return mMovtexQCs[index]; +} + +void DynOS_Lvl_LoadBackground(void *aPtr) { + // ensure this texture list exists + GfxData* foundGfxData = NULL; + DataNode* foundList = NULL; + for (auto& script : sDynosCustomLevelScripts) { + 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) { + Print("Could not find custom background"); + 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; + } + } + } +} diff --git a/data/dynos_misc.cpp b/data/dynos_misc.cpp index 7fd5be0d..f1557e56 100644 --- a/data/dynos_misc.cpp +++ b/data/dynos_misc.cpp @@ -41,8 +41,6 @@ extern "C" { #include "actors/zcustom0.h" } -static Array> sDynosCustomLevelScripts; - // // String // @@ -391,7 +389,8 @@ const void *DynOS_Geo_GetActorLayoutFromName(const char *aActorName) { if (aActorName == NULL) { return NULL; } // check levels - for (auto& lvl : sDynosCustomLevelScripts) { + auto& levelsArray = DynOS_Lvl_GetArray(); + for (auto& lvl : levelsArray) { for (auto& geo : lvl.second->mGeoLayouts) { if (geo->mName == aActorName) { return geo->mData; @@ -482,199 +481,3 @@ void *DynOS_Geo_GetGraphNode(const void *aGeoLayout, bool aKeepInMemory) { free(_Pool); return NULL; } - - //////////////// - // Collisions // -//////////////// - -static Array*>> sDynosCollisions; - -void DynOS_Col_Add(const SysPath &aPackFolder, const char *aCollisionName) { - // check for duplicates - for (s32 i = 0; i < sDynosCollisions.Count(); ++i) { - if (!strcmp(sDynosCollisions[i].first, aCollisionName)) { - return; - } - } - - u16 collisionLen = strlen(aCollisionName); - char* collisionName = (char*)calloc(1, sizeof(char) * (collisionLen + 1)); - strcpy(collisionName, aCollisionName); - - DataNode* _Node = DynOS_Col_LoadFromBinary(aPackFolder, collisionName); - if (!_Node) { - free(collisionName); - return; - } - - // Add to collisions - sDynosCollisions.Add({ collisionName, _Node }); -} - -Collision* DynOS_Col_Get(const char* collisionName) { - // check levels - for (auto& lvl : sDynosCustomLevelScripts) { - for (auto& col : lvl.second->mCollisions) { - if (col->mName == collisionName) { - return col->mData; - } - } - } - - // check normal actor collisions - for (s32 i = 0; i < sDynosCollisions.Count(); ++i) { - if (!strcmp(sDynosCollisions[i].first, collisionName)) { - return sDynosCollisions[i].second->mData; - } - } - return NULL; -} - - //////////// - // Levels // -//////////// - -void DynOS_Lvl_Add(s32 modIndex, const SysPath &aPackFolder, const char *aLevelName) { - // check for duplicates - for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { - if (!strcmp(sDynosCustomLevelScripts[i].first, aLevelName)) { - return; - } - } - - u16 levelLen = strlen(aLevelName); - char* levelName = (char*)calloc(1, sizeof(char) * (levelLen + 1)); - strcpy(levelName, aLevelName); - - GfxData* _Node = DynOS_Lvl_LoadFromBinary(aPackFolder, levelName); - if (!_Node) { - free(levelName); - return; - } - - // remember index - _Node->mModIndex = modIndex; - - // Add to levels - sDynosCustomLevelScripts.Add({ levelName, _Node }); -} - -LevelScript* DynOS_Lvl_Get(const char* levelName) { - if (strlen(levelName) == 0) { - static u32 index = 0; // DO NOT COMMIT - s32 levelScriptCount = sDynosCustomLevelScripts.Count(); // DO NOT COMMIT - if (levelScriptCount < 1) { return NULL; } // DO NOT COMMIT - index = (index + 1) % levelScriptCount; // DO NOT COMMIT - auto& scripts = sDynosCustomLevelScripts[index].second->mLevelScripts; // DO NOT COMMIT - Print("Going to level: %s\n", scripts[scripts.Count() - 1]->mName); // DO NOT COMMIT - return scripts[scripts.Count() - 1]->mData; // DO NOT COMMIT - } - - for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { - if (!strcmp(sDynosCustomLevelScripts[i].first, levelName)) { - auto& scripts = sDynosCustomLevelScripts[i].second->mLevelScripts; - return scripts[scripts.Count() - 1]->mData; - } - } - return NULL; -} - -s32 DynOS_Lvl_GetModIndex(void* levelScript) { - for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { - auto& scripts = sDynosCustomLevelScripts[i].second->mLevelScripts; - if (levelScript == scripts[scripts.Count() - 1]->mData) { - return sDynosCustomLevelScripts[i].second->mModIndex; - } - } - return -1; -} - -DataNode *DynOS_Lvl_Texture_Get(void *aPtr) { - for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { - auto &mTextures = sDynosCustomLevelScripts[i].second->mTextures; - for (s32 j = 0; j < mTextures.Count(); j++) { - auto &texture = mTextures[j]; - if (texture == aPtr) { - return texture; - } - - } - } - return NULL; -} - -static GfxData* DynOS_Lvl_Get_Active_Gfx(void) { - for (s32 i = 0; i < sDynosCustomLevelScripts.Count(); ++i) { - auto& gfxData = sDynosCustomLevelScripts[i].second; - auto& scripts = gfxData->mLevelScripts; - if (gLevelScriptActive == scripts[scripts.Count() - 1]->mData) { - return gfxData; - } - } - return NULL; -} - -const char* DynOS_Lvl_Get_Token(u32 index) { - GfxData* gfxData = DynOS_Lvl_Get_Active_Gfx(); - 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; - } - - return gfxData->mLuaTokenList[index].begin(); -} - -DataNode *DynOS_Lvl_MovtexQuadCollection_GetFromIndex(s32 index) { - GfxData* gfxData = DynOS_Lvl_Get_Active_Gfx(); - if (gfxData == NULL) { - return NULL; - } - - auto &mMovtexQCs = gfxData->mMovtexQCs; - - // Sanity check the index we passed. - if (index < 0 || index >= mMovtexQCs.Count()) { - return NULL; - } - - return mMovtexQCs[index]; -} - -void DynOS_Lvl_Load_Background(void *aPtr) { - // ensure this texture list exists - GfxData* foundGfxData = NULL; - DataNode* foundList = NULL; - for (auto& script : sDynosCustomLevelScripts) { - 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) { - Print("Could not find custom background"); - 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; - } - } - } -} diff --git a/src/game/moving_texture.c b/src/game/moving_texture.c index 0729e2af..3ba163e1 100644 --- a/src/game/moving_texture.c +++ b/src/game/moving_texture.c @@ -706,7 +706,7 @@ Gfx *geo_movtex_draw_water_regions_ext(s32 callContext, struct GraphNode *node, return NULL; } - quadCollection = dynos_level_movtexqc_getfromindex(asGenerated->parameter); + quadCollection = dynos_level_get_movtexqc(asGenerated->parameter); if (quadCollection == NULL) { return NULL; }