Function pointer cleanup

This commit is contained in:
MysterD 2022-04-08 01:46:29 -07:00
parent 11e07d832e
commit 5baf650dd2
10 changed files with 131 additions and 158 deletions

View File

@ -695,9 +695,6 @@ s32 DynOS_Geo_GetActorCount();
const char *DynOS_Geo_GetActorName(s32 aIndex);
const void *DynOS_Geo_GetActorLayout(s32 aIndex);
s32 DynOS_Geo_GetActorIndex(const void *aGeoLayout);
void *DynOS_Geo_GetFunctionPointerFromName(const String &aName);
void *DynOS_Geo_GetFunctionPointerFromIndex(s32 aIndex);
s32 DynOS_Geo_GetFunctionIndex(const void *aPtr);
void *DynOS_Geo_GetGraphNode(const void *aGeoLayout, bool aKeepInMemory);
//
@ -754,6 +751,9 @@ const Collision* DynOS_Mgr_VanillaLvlCol_GetFromName(const char* aDataName);
const char* DynOS_Mgr_VanillaLvlCol_GetFromData(const Collision* aData);
const Texture* DynOS_Mgr_VanillaTex_GetFromName(const char* aDataName);
const char* DynOS_Mgr_VanillaTex_GetFromData(const Texture* aData);
const void* DynOS_Mgr_VanillaFunc_GetFromName(const char* aDataName);
const void* DynOS_Mgr_VanillaFunc_GetFromIndex(s32 aIndex);
s32 DynOS_Mgr_VanillaFunc_GetIndexFromData(const void* aData);
//
// Bin
@ -837,7 +837,7 @@ void DynOS_Vtx_Load(FILE *aFile, GfxData *aGfxData);
void DynOS_Pointer_Lua_Write(FILE* aFile, u32 index, GfxData* aGfxData);
void DynOS_Pointer_Write(FILE* aFile, const void* aPtr, GfxData* aGfxData);
void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue, bool isLvl);
void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue);
void DynOS_GfxDynCmd_Load(FILE *aFile, GfxData *aGfxData);
@ -847,8 +847,6 @@ void DynOS_Actor_GeneratePack(const SysPath &aPackFolder);
DataNode<LevelScript>* DynOS_Lvl_Parse(GfxData* aGfxData, DataNode<LevelScript>* aNode, bool aDisplayPercent);
GfxData *DynOS_Lvl_LoadFromBinary(const SysPath &aPackFolder, const char *aLevelName);
void DynOS_Lvl_GeneratePack(const SysPath &aPackFolder);
s32 DynOS_Lvl_GetFunctionIndex(const void *aPtr);
void *DynOS_Lvl_GetFunctionPointerFromIndex(s32 aIndex);
s64 DynOS_Lvl_ParseLevelScriptConstants(const String& _Arg, bool* found);
#endif

View File

@ -27,10 +27,10 @@ static s64 ParseGeoSymbolArg(GfxData* aGfxData, DataNode<GeoLayout>* aNode, u64&
return integerValue;
}
// Geo functions
void *_GeoFunctionPtr = DynOS_Geo_GetFunctionPointerFromName(_Arg);
if (_GeoFunctionPtr != NULL) {
return (s64) _GeoFunctionPtr;
// Built-in functions
const void *_FunctionPtr = DynOS_Mgr_VanillaFunc_GetFromName(_Arg.begin());
if (_FunctionPtr != NULL) {
return (s64) _FunctionPtr;
}
// Layer constants
@ -440,7 +440,7 @@ void DynOS_Geo_Load(FILE *aFile, GfxData *aGfxData) {
_Node->mData = New<GeoLayout>(_Node->mSize);
for (u32 i = 0; i != _Node->mSize; ++i) {
u32 _Value = ReadBytes<u32>(aFile);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value, false);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value);
if (_Ptr) {
_Node->mData[i] = (uintptr_t) _Ptr;
} else {

View File

@ -973,7 +973,7 @@ void DynOS_Gfx_Load(FILE *aFile, GfxData *aGfxData) {
for (u32 i = 0; i != _Node->mSize; ++i) {
u32 _WordsW0 = ReadBytes<u32>(aFile);
u32 _WordsW1 = ReadBytes<u32>(aFile);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _WordsW1, false);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _WordsW1);
if (_Ptr) {
_Node->mData[i].words.w0 = (uintptr_t) _WordsW0;
_Node->mData[i].words.w1 = (uintptr_t) _Ptr;

View File

@ -21,42 +21,6 @@ static void ClearLvlDataNodes(DataNodes<T> &aDataNodes) {
}
}
//
// Lvl Functions
//
static const Array<Pair<const char *, void *>> &__LvlFunctions() {
#define define_lvl_function(name) { #name, (void *) name }
static const Array<Pair<const char *, void *>> sLvlFunctions = {
define_lvl_function(lvl_init_or_update)
};
#undef define_lvl_function
return sLvlFunctions;
}
#define sLvlFunctions __LvlFunctions()
void *DynOS_Lvl_GetFunctionPointerFromName(const String &aName) {
for (const auto &_LvlFunction : sLvlFunctions) {
if (aName == _LvlFunction.first) {
return _LvlFunction.second;
}
};
return NULL;
}
s32 DynOS_Lvl_GetFunctionIndex(const void *aPtr) {
for (const auto &_LvlFunction : sLvlFunctions) {
if (_LvlFunction.second == aPtr) {
return (s32) (&_LvlFunction - sLvlFunctions.begin());
}
}
return -1;
}
void *DynOS_Lvl_GetFunctionPointerFromIndex(s32 aIndex) {
return sLvlFunctions[aIndex].second;
}
/////////////
// Parsing //
/////////////
@ -1421,10 +1385,10 @@ static LevelScript ParseLevelScriptSymbolArgInternal(GfxData* aGfxData, DataNode
_Arg = _Arg.SubString(0, _Plus);
}
// Lvl functions
void *_LvlFunctionPtr = DynOS_Lvl_GetFunctionPointerFromName(_Arg);
if (_LvlFunctionPtr != NULL) {
return (LevelScript) _LvlFunctionPtr;
// Built-in functions
const void *_FunctionPtr = DynOS_Mgr_VanillaFunc_GetFromName(_Arg.begin());
if (_FunctionPtr != NULL) {
return (s64) _FunctionPtr;
}
bool constantFound = false;
@ -1999,7 +1963,7 @@ static DataNode<LevelScript>* DynOS_Lvl_Load(FILE *aFile, GfxData *aGfxData) {
// Read it
for (u32 i = 0; i != _Node->mSize; ++i) {
u32 _Value = ReadBytes<u32>(aFile);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value, true);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value);
if (_Ptr) {
_Node->mData[i] = (uintptr_t) _Ptr;
} else {

View File

@ -84,7 +84,7 @@ DataNode<MovtexQC>* DynOS_MovtexQC_Load(FILE *aFile, GfxData *aGfxData) {
for (u32 i = 0; i != _Node->mSize; ++i) {
_Node->mData[i].id = ReadBytes<s16>(aFile);
u32 _Value = ReadBytes<u32>(aFile);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value, false);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value);
_Node->mData[i].quadArraySegmented = (Movtex*)_Ptr;
}

View File

@ -208,22 +208,14 @@ void DynOS_Pointer_Write(FILE* aFile, const void* aPtr, GfxData* aGfxData) {
}
}
// Geo function
s32 _GeoFunctionIndex = DynOS_Geo_GetFunctionIndex(aPtr);
// Built-in functions
s32 _GeoFunctionIndex = DynOS_Mgr_VanillaFunc_GetIndexFromData(aPtr);
if (_GeoFunctionIndex != -1) {
WriteBytes<u32>(aFile, FUNCTION_CODE);
WriteBytes<s32>(aFile, _GeoFunctionIndex);
return;
}
// Lvl function
s32 _LvlFunctionIndex = DynOS_Lvl_GetFunctionIndex(aPtr);
if (_LvlFunctionIndex != -1) {
WriteBytes<u32>(aFile, FUNCTION_CODE);
WriteBytes<s32>(aFile, _LvlFunctionIndex);
return;
}
// Pointer
PointerData _PtrData = GetDataFromPointer(aPtr, aGfxData);
WriteBytes<u32>(aFile, POINTER_CODE);
@ -407,7 +399,7 @@ static void *GetPointerFromData(GfxData *aGfxData, const String &aPtrName, u32 a
return NULL;
}
void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue, bool isLvl) {
void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue) {
// LUAV
if (aValue == LUA_VAR_CODE) {
@ -425,9 +417,7 @@ void *DynOS_Pointer_Load(FILE *aFile, GfxData *aGfxData, u32 aValue, bool isLvl)
// FUNC
if (aValue == FUNCTION_CODE) {
s32 _FunctionIndex = ReadBytes<s32>(aFile);
return isLvl
? DynOS_Lvl_GetFunctionPointerFromIndex(_FunctionIndex)
: DynOS_Geo_GetFunctionPointerFromIndex(_FunctionIndex);
return (void*) DynOS_Mgr_VanillaFunc_GetFromIndex(_FunctionIndex);
}
// PNTR

View File

@ -76,7 +76,7 @@ DataNode<TexData*>* DynOS_TexList_Load(FILE *aFile, GfxData *aGfxData) {
_Node->mData = New<TexData*>(_Node->mSize);
for (u32 i = 0; i != _Node->mSize; ++i) {
u32 _Value = ReadBytes<u32>(aFile);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value, false);
void *_Ptr = DynOS_Pointer_Load(aFile, aGfxData, _Value);
if (_Ptr == NULL) {
PrintError("Could not read texture in texlist");
} else {

View File

@ -2,6 +2,22 @@
extern "C" {
#include "behavior_table.h"
#include "levels/scripts.h"
#include "object_fields.h"
#include "engine/level_script.h"
#include "game/object_helpers.h"
#include "game/segment2.h"
#include "game/level_geo.h"
#include "game/level_update.h"
#include "game/moving_texture.h"
#include "game/paintings.h"
#include "game/geo_misc.h"
#include "game/mario_misc.h"
#include "game/mario_actions_cutscene.h"
#include "game/screen_transition.h"
#include "game/object_list_processor.h"
#include "game/behavior_actions.h"
#include "game/rendering_graph_node.h"
#include "game/skybox.h"
#include "levels/bbh/header.h"
#include "levels/bitdw/header.h"
@ -39,7 +55,6 @@ extern "C" {
#include "textures.h"
}
#define MGR_FIND_DATA(_DataTable, _Cast) \
size_t _count = sizeof(_DataTable) / (2 * sizeof(_DataTable[0])); \
for (u32 _i = 0; _i < _count; _i++) { \
@ -1038,3 +1053,96 @@ const Texture* DynOS_Mgr_VanillaTex_GetFromName(const char* aDataName) {
const char* DynOS_Mgr_VanillaTex_GetFromData(const Texture* aData) {
MGR_FIND_NAME(sDynosVanillaTexs);
}
////////////////////
// Functions Ptrs //
////////////////////
static void *geo_rotate_3d_coin(s32 callContext, void *node, UNUSED void *c) {
if (callContext == GEO_CONTEXT_RENDER) {
struct Object *obj = (struct Object *) gCurGraphNodeObject;
struct GraphNodeRotation *rotNode = (struct GraphNodeRotation *) ((struct GraphNode *) node)->next;
rotNode->rotation[0] = 0;
rotNode->rotation[1] = obj->oAnimState;
rotNode->rotation[2] = 0;
obj->oAnimState += 0x0800;
}
return NULL;
}
#define define_vanilla_func(tex) (const void*) #tex, (const void*) tex
static const void* sDynosVanillaFuncs[] = {
define_vanilla_func(geo_mirror_mario_set_alpha),
define_vanilla_func(geo_switch_mario_stand_run),
define_vanilla_func(geo_switch_mario_eyes),
define_vanilla_func(geo_mario_tilt_torso),
define_vanilla_func(geo_mario_head_rotation),
define_vanilla_func(geo_switch_mario_hand),
define_vanilla_func(geo_mario_hand_foot_scaler),
define_vanilla_func(geo_switch_mario_cap_effect),
define_vanilla_func(geo_switch_mario_cap_on_off),
define_vanilla_func(geo_mario_rotate_wing_cap_wings),
define_vanilla_func(geo_switch_mario_hand_grab_pos),
define_vanilla_func(geo_render_mirror_mario),
define_vanilla_func(geo_mirror_mario_backface_culling),
define_vanilla_func(geo_update_projectile_pos_from_parent),
define_vanilla_func(geo_update_layer_transparency),
define_vanilla_func(geo_switch_anim_state),
define_vanilla_func(geo_switch_area),
define_vanilla_func(geo_camera_main),
define_vanilla_func(geo_camera_fov),
define_vanilla_func(geo_envfx_main),
define_vanilla_func(geo_skybox_main),
define_vanilla_func(geo_wdw_set_initial_water_level),
define_vanilla_func(geo_movtex_pause_control),
define_vanilla_func(geo_movtex_draw_water_regions),
define_vanilla_func(geo_movtex_draw_nocolor),
define_vanilla_func(geo_movtex_draw_colored),
define_vanilla_func(geo_movtex_draw_colored_no_update),
define_vanilla_func(geo_movtex_draw_colored_2_no_update),
define_vanilla_func(geo_movtex_update_horizontal),
define_vanilla_func(geo_movtex_draw_colored_no_update),
define_vanilla_func(geo_painting_draw),
define_vanilla_func(geo_painting_update),
define_vanilla_func(geo_exec_inside_castle_light),
define_vanilla_func(geo_exec_flying_carpet_timer_update),
define_vanilla_func(geo_exec_flying_carpet_create),
define_vanilla_func(geo_exec_cake_end_screen),
define_vanilla_func(geo_cannon_circle_base),
define_vanilla_func(geo_move_mario_part_from_parent),
define_vanilla_func(geo_bits_bowser_coloring),
define_vanilla_func(geo_update_body_rot_from_parent),
define_vanilla_func(geo_switch_bowser_eyes),
define_vanilla_func(geo_switch_tuxie_mother_eyes),
define_vanilla_func(geo_update_held_mario_pos),
define_vanilla_func(geo_snufit_move_mask),
define_vanilla_func(geo_snufit_scale_body),
define_vanilla_func(geo_scale_bowser_key),
(const void *) "geo_rotate_coin", (const void *) geo_rotate_3d_coin,
define_vanilla_func(geo_offset_klepto_held_object),
define_vanilla_func(geo_switch_peach_eyes),
// coop-specific
define_vanilla_func(geo_mario_set_player_colors),
define_vanilla_func(geo_movtex_draw_water_regions_ext),
define_vanilla_func(lvl_init_or_update),
};
const void* DynOS_Mgr_VanillaFunc_GetFromName(const char* aDataName) {
MGR_FIND_DATA(sDynosVanillaFuncs, (const void*));
}
const void* DynOS_Mgr_VanillaFunc_GetFromIndex(s32 aIndex) {
size_t count = sizeof(sDynosVanillaFuncs) / (2 * sizeof(sDynosVanillaFuncs[0]));
if (aIndex < 0 || aIndex >= count) { return NULL; }
return (const void*)sDynosVanillaFuncs[aIndex * 2 + 1];
}
s32 DynOS_Mgr_VanillaFunc_GetIndexFromData(const void* aData) {
size_t count = sizeof(sDynosVanillaFuncs) / (2 * sizeof(sDynosVanillaFuncs[0]));
for (s32 i = 0; i < count; i++) {
if ((const void*)sDynosVanillaFuncs[i * 2 + 1] == aData) {
return i;
}
}
return -1;
}

View File

@ -439,92 +439,9 @@ bool DynOS_Geo_IsCustomActor(s32 aIndex) {
}
//
// Geo Functions
// Geo
//
static const Array<Pair<const char *, void *>> &__GeoFunctions() {
#define define_geo_function(name) { #name, (void *) name }
static const Array<Pair<const char *, void *>> sGeoFunctions = {
define_geo_function(geo_mirror_mario_set_alpha),
define_geo_function(geo_switch_mario_stand_run),
define_geo_function(geo_switch_mario_eyes),
define_geo_function(geo_mario_tilt_torso),
define_geo_function(geo_mario_head_rotation),
define_geo_function(geo_switch_mario_hand),
define_geo_function(geo_mario_hand_foot_scaler),
define_geo_function(geo_switch_mario_cap_effect),
define_geo_function(geo_switch_mario_cap_on_off),
define_geo_function(geo_mario_rotate_wing_cap_wings),
define_geo_function(geo_switch_mario_hand_grab_pos),
define_geo_function(geo_render_mirror_mario),
define_geo_function(geo_mirror_mario_backface_culling),
define_geo_function(geo_update_projectile_pos_from_parent),
define_geo_function(geo_update_layer_transparency),
define_geo_function(geo_switch_anim_state),
define_geo_function(geo_switch_area),
define_geo_function(geo_camera_main),
define_geo_function(geo_camera_fov),
define_geo_function(geo_envfx_main),
define_geo_function(geo_skybox_main),
define_geo_function(geo_wdw_set_initial_water_level),
define_geo_function(geo_movtex_pause_control),
define_geo_function(geo_movtex_draw_water_regions),
define_geo_function(geo_movtex_draw_nocolor),
define_geo_function(geo_movtex_draw_colored),
define_geo_function(geo_movtex_draw_colored_no_update),
define_geo_function(geo_movtex_draw_colored_2_no_update),
define_geo_function(geo_movtex_update_horizontal),
define_geo_function(geo_movtex_draw_colored_no_update),
define_geo_function(geo_painting_draw),
define_geo_function(geo_painting_update),
define_geo_function(geo_exec_inside_castle_light),
define_geo_function(geo_exec_flying_carpet_timer_update),
define_geo_function(geo_exec_flying_carpet_create),
define_geo_function(geo_exec_cake_end_screen),
define_geo_function(geo_cannon_circle_base),
define_geo_function(geo_move_mario_part_from_parent),
define_geo_function(geo_bits_bowser_coloring),
define_geo_function(geo_update_body_rot_from_parent),
define_geo_function(geo_switch_bowser_eyes),
define_geo_function(geo_switch_tuxie_mother_eyes),
define_geo_function(geo_update_held_mario_pos),
define_geo_function(geo_snufit_move_mask),
define_geo_function(geo_snufit_scale_body),
define_geo_function(geo_scale_bowser_key),
{ "geo_rotate_coin", (void *) geo_rotate_3d_coin },
define_geo_function(geo_offset_klepto_held_object),
define_geo_function(geo_switch_peach_eyes),
// coop-specific
define_geo_function(geo_mario_set_player_colors),
define_geo_function(geo_movtex_draw_water_regions_ext),
};
#undef define_geo_function
return sGeoFunctions;
}
#define sGeoFunctions __GeoFunctions()
void *DynOS_Geo_GetFunctionPointerFromName(const String &aName) {
for (const auto &_GeoFunction : sGeoFunctions) {
if (aName == _GeoFunction.first) {
return _GeoFunction.second;
}
};
return NULL;
}
void *DynOS_Geo_GetFunctionPointerFromIndex(s32 aIndex) {
return sGeoFunctions[aIndex].second;
}
s32 DynOS_Geo_GetFunctionIndex(const void *aPtr) {
for (const auto &_GeoFunction : sGeoFunctions) {
if (_GeoFunction.second == aPtr) {
return (s32) (&_GeoFunction - sGeoFunctions.begin());
}
}
return -1;
}
static void _RelocateGraphNodePointers(struct GraphNode *aHead, u64 aOffset) {
struct GraphNode *_Node = aHead;
do {

View File

@ -340,11 +340,7 @@ void bhv_big_bully_with_minions_init(void) {
void big_bully_spawn_star(void) {
if (obj_lava_death() == 1) {
spawn_mist_particles();
<<<<<<< HEAD
f32* starPos = gStarPositions.BigBullyStarPos;
=======
f32* starPos = gStarPositions.BigBullyTrioStarPos;
>>>>>>> 5eb6122af28ea24721350109b0e0bf2affa0650a
spawn_default_star(starPos[0], starPos[1], starPos[2]);
}
}