2022-04-02 04:50:42 +02:00
|
|
|
#include "dynos.cpp.h"
|
2022-04-03 00:14:04 +02:00
|
|
|
extern "C" {
|
|
|
|
#include "behavior_table.h"
|
2022-04-03 12:30:47 +02:00
|
|
|
#include "levels/scripts.h"
|
2022-04-03 00:14:04 +02:00
|
|
|
}
|
2022-04-03 12:30:47 +02:00
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
/////////////
|
|
|
|
// Writing //
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
typedef Pair<String, u32> PointerData;
|
|
|
|
static PointerData GetDataFromPointer(const void* aPtr, GfxData* aGfxData) {
|
|
|
|
// Lights
|
|
|
|
for (auto& _Node : aGfxData->mLights) {
|
|
|
|
if (&_Node->mData->l[0] == aPtr) { // Light *, not Lights1 *
|
|
|
|
return { _Node->mName, 1 };
|
|
|
|
}
|
|
|
|
if (&_Node->mData->a == aPtr) { // Ambient *, not Lights1 *
|
|
|
|
return { _Node->mName, 2 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 06:32:55 +02:00
|
|
|
// Light_ts
|
2022-04-05 06:25:40 +02:00
|
|
|
for (auto& _Node : aGfxData->mLightTs) {
|
|
|
|
if (&_Node->mData->col[0] == aPtr) {
|
|
|
|
return { _Node->mName, 1 };
|
|
|
|
}
|
|
|
|
if (&_Node->mData->colc[0] == aPtr) {
|
|
|
|
return { _Node->mName, 2 };
|
|
|
|
}
|
|
|
|
if (&_Node->mData->dir[0] == aPtr) {
|
|
|
|
return { _Node->mName, 3 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 06:32:55 +02:00
|
|
|
// Ambient_ts
|
2022-04-05 06:25:40 +02:00
|
|
|
for (auto& _Node : aGfxData->mAmbientTs) {
|
|
|
|
if (&_Node->mData->col[0] == aPtr) {
|
|
|
|
return { _Node->mName, 1 };
|
|
|
|
}
|
|
|
|
if (&_Node->mData->colc[0] == aPtr) {
|
|
|
|
return { _Node->mName, 2 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Textures
|
|
|
|
for (auto& _Node : aGfxData->mTextures) {
|
|
|
|
if (_Node == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:56:03 +02:00
|
|
|
// Texture Lists
|
|
|
|
for (auto& _Node : aGfxData->mTextureLists) {
|
|
|
|
if (_Node == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Display lists
|
|
|
|
for (auto& _Node : aGfxData->mDisplayLists) {
|
|
|
|
if (_Node == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Geo layouts
|
|
|
|
for (auto& _Node : aGfxData->mGeoLayouts) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collisions
|
|
|
|
for (auto& _Node : aGfxData->mCollisions) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macro objects
|
|
|
|
for (auto& _Node : aGfxData->mMacroObjects) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 01:08:19 +02:00
|
|
|
// Trajectories
|
|
|
|
for (auto& _Node : aGfxData->mTrajectories) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:12:17 +02:00
|
|
|
// Movtexs
|
|
|
|
for (auto& _Node : aGfxData->mMovtexs) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MovtexQCs
|
|
|
|
for (auto& _Node : aGfxData->mMovtexQCs) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 08:42:25 +02:00
|
|
|
// Rooms
|
|
|
|
for (auto& _Node : aGfxData->mRooms) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, 0 };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 00:14:04 +02:00
|
|
|
// Behaviors
|
|
|
|
enum BehaviorId id = get_id_from_vanilla_behavior((const BehaviorScript*) aPtr);
|
|
|
|
if (id >= 0 && id < id_bhv_max_count) {
|
|
|
|
return { get_behavior_name_from_id(id), 0 };
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Actors
|
2022-04-09 03:56:22 +02:00
|
|
|
auto builtinActor = DynOS_Builtin_Actor_GetFromData((const GeoLayout*)aPtr);
|
|
|
|
if (builtinActor != NULL) {
|
|
|
|
return { builtinActor, 0 };
|
2022-04-03 12:30:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Lvl Geos
|
|
|
|
auto builtinGeo = DynOS_Builtin_LvlGeo_GetFromData((const GeoLayout*)aPtr);
|
|
|
|
if (builtinGeo != NULL) {
|
|
|
|
return { builtinGeo, 0 };
|
2022-04-05 06:25:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Lvl Cols
|
|
|
|
auto builtinCol = DynOS_Builtin_LvlCol_GetFromData((const Collision*)aPtr);
|
|
|
|
if (builtinCol != NULL) {
|
|
|
|
return { builtinCol, 0 };
|
2022-04-08 08:06:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Script Pointers
|
|
|
|
auto builtinScriptPtr = DynOS_Builtin_ScriptPtr_GetFromData(aPtr);
|
|
|
|
if (builtinScriptPtr != NULL) {
|
|
|
|
return { builtinScriptPtr, 0 };
|
2022-04-07 06:32:55 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Textures
|
|
|
|
auto builtinTex = DynOS_Builtin_Tex_GetFromData((const Texture*)aPtr);
|
|
|
|
if (builtinTex != NULL) {
|
|
|
|
return { builtinTex, 0 };
|
2022-04-03 12:30:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 08:06:10 +02:00
|
|
|
// the ones below use a saved offset
|
|
|
|
|
|
|
|
s32 _Offset = 0;
|
|
|
|
for (auto& pair : aGfxData->mPointerOffsetList) {
|
|
|
|
if (pair.first == aPtr) {
|
2022-04-12 07:54:36 +02:00
|
|
|
_Offset = (s32)((u8*)pair.first - (u8*)pair.second);
|
2022-04-08 08:06:10 +02:00
|
|
|
aPtr = pair.second;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Level scripts
|
|
|
|
for (auto& _Node : aGfxData->mLevelScripts) {
|
|
|
|
if (_Node->mData == aPtr) {
|
|
|
|
return { _Node->mName, _Offset };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Vertices
|
|
|
|
String _VtxArrayName = "";
|
|
|
|
uintptr_t _VtxArrayStart = 0;
|
|
|
|
for (auto& _Node : aGfxData->mVertices) {
|
|
|
|
if (_Node->mData == aPtr) {
|
2022-04-08 08:06:10 +02:00
|
|
|
return { _Node->mName, _Offset };
|
2022-04-02 04:50:42 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-07 06:32:55 +02:00
|
|
|
|
2022-04-08 08:06:10 +02:00
|
|
|
PrintError("Unable to find pointer!");
|
|
|
|
return { "", 0 };
|
2022-04-02 04:50:42 +02:00
|
|
|
}
|
|
|
|
|
2022-04-05 09:22:09 +02:00
|
|
|
void DynOS_Pointer_Lua_Write(FILE* aFile, u32 index, GfxData* aGfxData) {
|
|
|
|
String& token = aGfxData->mLuaTokenList[index];
|
|
|
|
WriteBytes<u32>(aFile, LUA_VAR_CODE);
|
|
|
|
token.Write(aFile);
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
void DynOS_Pointer_Write(FILE* aFile, const void* aPtr, GfxData* aGfxData) {
|
|
|
|
|
|
|
|
// NULL
|
|
|
|
if (!aPtr) {
|
|
|
|
WriteBytes<u32>(aFile, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-05 09:22:09 +02:00
|
|
|
// Lua variable
|
|
|
|
for (s32 i = 0; i < aGfxData->mLuaPointerList.Count(); i++) {
|
|
|
|
if (aPtr == aGfxData->mLuaPointerList[i]) {
|
|
|
|
u32 index = *((u32*)aPtr);
|
|
|
|
String& token = aGfxData->mLuaTokenList[index];
|
|
|
|
WriteBytes<u32>(aFile, LUA_VAR_CODE);
|
|
|
|
token.Write(aFile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:46:29 +02:00
|
|
|
// Built-in functions
|
2022-04-08 10:52:12 +02:00
|
|
|
s32 _GeoFunctionIndex = DynOS_Builtin_Func_GetIndexFromData(aPtr);
|
2022-04-02 04:50:42 +02:00
|
|
|
if (_GeoFunctionIndex != -1) {
|
|
|
|
WriteBytes<u32>(aFile, FUNCTION_CODE);
|
|
|
|
WriteBytes<s32>(aFile, _GeoFunctionIndex);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pointer
|
|
|
|
PointerData _PtrData = GetDataFromPointer(aPtr, aGfxData);
|
|
|
|
WriteBytes<u32>(aFile, POINTER_CODE);
|
|
|
|
_PtrData.first.Write(aFile);
|
|
|
|
WriteBytes<u32>(aFile, _PtrData.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////
|
|
|
|
// Reading //
|
|
|
|
/////////////
|
|
|
|
|
|
|
|
static void *GetPointerFromData(GfxData *aGfxData, const String &aPtrName, u32 aPtrData) {
|
|
|
|
|
|
|
|
// Lights
|
|
|
|
for (auto& _Node : aGfxData->mLights) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
if (aPtrData == 1) {
|
|
|
|
return (void *) &_Node->mData->l[0];
|
|
|
|
}
|
|
|
|
if (aPtrData == 2) {
|
|
|
|
return (void *) &_Node->mData->a;
|
|
|
|
}
|
|
|
|
sys_fatal("Unknown Light type: %u", aPtrData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 06:32:55 +02:00
|
|
|
// Light_ts
|
2022-04-05 06:25:40 +02:00
|
|
|
for (auto& _Node : aGfxData->mLightTs) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
if (aPtrData == 1) {
|
|
|
|
return (void *) &_Node->mData->col[0];
|
|
|
|
}
|
|
|
|
if (aPtrData == 2) {
|
|
|
|
return (void *) &_Node->mData->colc[0];
|
|
|
|
}
|
|
|
|
if (aPtrData == 3) {
|
|
|
|
return (void *) &_Node->mData->dir[0];
|
|
|
|
}
|
|
|
|
sys_fatal("Unknown Light type: %u", aPtrData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-07 06:32:55 +02:00
|
|
|
// Ambient_ts
|
2022-04-05 06:25:40 +02:00
|
|
|
for (auto& _Node : aGfxData->mAmbientTs) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
if (aPtrData == 1) {
|
|
|
|
return (void *) &_Node->mData->col[0];
|
|
|
|
}
|
|
|
|
if (aPtrData == 2) {
|
|
|
|
return (void *) &_Node->mData->colc[0];
|
|
|
|
}
|
|
|
|
sys_fatal("Unknown Light type: %u", aPtrData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Textures
|
|
|
|
for (auto& _Node : aGfxData->mTextures) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 07:56:03 +02:00
|
|
|
// Texture Lists
|
|
|
|
for (auto& _Node : aGfxData->mTextureLists) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Display lists
|
|
|
|
for (auto &_Node : aGfxData->mDisplayLists) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Geo layouts
|
|
|
|
for (auto &_Node : aGfxData->mGeoLayouts) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vertices
|
|
|
|
for (auto &_Node : aGfxData->mVertices) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) (_Node->mData + aPtrData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collisions
|
|
|
|
for (auto &_Node : aGfxData->mCollisions) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Level scripts
|
|
|
|
for (auto &_Node : aGfxData->mLevelScripts) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
2022-04-08 08:06:10 +02:00
|
|
|
return (void *) (_Node->mData + aPtrData);
|
2022-04-02 04:50:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Macro objects
|
|
|
|
for (auto &_Node : aGfxData->mMacroObjects) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 01:08:19 +02:00
|
|
|
// Trajectories
|
|
|
|
for (auto &_Node : aGfxData->mTrajectories) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 07:12:17 +02:00
|
|
|
// Movtexs
|
|
|
|
for (auto &_Node : aGfxData->mMovtexs) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MovtexQCs
|
|
|
|
for (auto &_Node : aGfxData->mMovtexQCs) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-04 08:42:25 +02:00
|
|
|
// Rooms
|
|
|
|
for (auto &_Node : aGfxData->mRooms) {
|
|
|
|
if (_Node->mName == aPtrName) {
|
|
|
|
return (void *) _Node->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 00:14:04 +02:00
|
|
|
// Behaviors
|
|
|
|
enum BehaviorId id = get_id_from_behavior_name(aPtrName.begin());
|
|
|
|
if (id >= 0 && id < id_bhv_max_count) {
|
|
|
|
return (void*)get_behavior_from_id(id);
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Actors
|
2022-04-09 03:56:22 +02:00
|
|
|
auto builtinActor = DynOS_Builtin_Actor_GetFromName(aPtrName.begin());
|
|
|
|
if (builtinActor != NULL) {
|
|
|
|
return (void*)builtinActor;
|
2022-04-03 12:30:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Lvl Geos
|
|
|
|
auto builtinGeo = DynOS_Builtin_LvlGeo_GetFromName(aPtrName.begin());
|
|
|
|
if (builtinGeo != NULL) {
|
|
|
|
return (void*)builtinGeo;
|
2022-04-05 06:25:40 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Lvl Cols
|
|
|
|
auto builtinCol = DynOS_Builtin_LvlCol_GetFromName(aPtrName.begin());
|
|
|
|
if (builtinCol != NULL) {
|
|
|
|
return (void*)builtinCol;
|
2022-04-08 08:06:10 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Script Pointers
|
|
|
|
auto builtinScriptPtr = DynOS_Builtin_ScriptPtr_GetFromName(aPtrName.begin());
|
|
|
|
if (builtinScriptPtr != NULL) {
|
|
|
|
return (void*)builtinScriptPtr;
|
2022-04-07 06:32:55 +02:00
|
|
|
}
|
|
|
|
|
2022-04-08 10:52:12 +02:00
|
|
|
// Built-in Textures
|
|
|
|
auto builtinTex = DynOS_Builtin_Tex_GetFromName(aPtrName.begin());
|
|
|
|
if (builtinTex != NULL) {
|
|
|
|
return (void*)builtinTex;
|
2022-04-03 12:30:47 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// Error
|
|
|
|
sys_fatal("Pointer not found: %s", aPtrName.begin());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-04-08 10:46:29 +02:00
|
|
|
void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue) {
|
2022-04-02 04:50:42 +02:00
|
|
|
|
2022-04-05 09:22:09 +02:00
|
|
|
// LUAV
|
|
|
|
if (aValue == LUA_VAR_CODE) {
|
|
|
|
String token; token.Read(aFile);
|
2022-04-07 17:21:19 +02:00
|
|
|
for (s32 i = 0; i < aGfxData->mLuaTokenList.Count(); i++) {
|
|
|
|
if (token == aGfxData->mLuaTokenList[i]) {
|
|
|
|
return (void*)(uintptr_t)(i+1);
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 09:22:09 +02:00
|
|
|
u32 index = aGfxData->mLuaTokenList.Count();
|
|
|
|
aGfxData->mLuaTokenList.Add(token);
|
2022-04-07 17:21:19 +02:00
|
|
|
return (void*)(uintptr_t)(index+1);
|
2022-04-05 09:22:09 +02:00
|
|
|
}
|
|
|
|
|
2022-04-02 04:50:42 +02:00
|
|
|
// FUNC
|
|
|
|
if (aValue == FUNCTION_CODE) {
|
2022-04-03 00:14:04 +02:00
|
|
|
s32 _FunctionIndex = ReadBytes<s32>(aFile);
|
2022-04-08 10:52:12 +02:00
|
|
|
return (void*) DynOS_Builtin_Func_GetFromIndex(_FunctionIndex);
|
2022-04-02 04:50:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// PNTR
|
|
|
|
if (aValue == POINTER_CODE) {
|
|
|
|
String _PtrName; _PtrName.Read(aFile);
|
|
|
|
u32 _PtrData = ReadBytes<u32>(aFile);
|
|
|
|
return GetPointerFromData(aGfxData, _PtrName, _PtrData);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a pointer
|
|
|
|
return NULL;
|
|
|
|
}
|