2022-04-20 04:06:18 +00:00
|
|
|
#include <map>
|
2022-05-14 21:28:25 +00:00
|
|
|
#include <algorithm>
|
2022-04-09 01:56:22 +00:00
|
|
|
#include "dynos.cpp.h"
|
|
|
|
|
2022-04-20 04:06:18 +00:00
|
|
|
extern "C" {
|
|
|
|
#include "object_fields.h"
|
|
|
|
#include "game/level_update.h"
|
|
|
|
#include "game/object_list_processor.h"
|
|
|
|
#include "pc/configfile.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static maps/arrays
|
|
|
|
static std::map<const void*, ActorGfx>& DynosValidActors() {
|
|
|
|
static std::map<const void*, ActorGfx> sDynosValidActors;
|
|
|
|
return sDynosValidActors;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Array<Pair<const char*, void *>>& DynosCustomActors() {
|
|
|
|
static Array<Pair<const char*, void *>> sDynosCustomActors;
|
|
|
|
return sDynosCustomActors;
|
|
|
|
}
|
2022-04-09 01:56:22 +00:00
|
|
|
|
|
|
|
// TODO: the cleanup/refactor didn't really go as planned.
|
|
|
|
// clean up the actor management code more
|
|
|
|
|
2022-04-16 20:05:10 +00:00
|
|
|
void DynOS_Actor_AddCustom(const SysPath &aFilename, const char *aActorName) {
|
2023-11-09 04:14:14 +00:00
|
|
|
const void* georef = DynOS_Builtin_Actor_GetFromName(aActorName);
|
2022-04-20 04:06:18 +00:00
|
|
|
|
2022-04-09 01:56:22 +00:00
|
|
|
u16 actorLen = strlen(aActorName);
|
|
|
|
char* actorName = (char*)calloc(1, sizeof(char) * (actorLen + 1));
|
|
|
|
strcpy(actorName, aActorName);
|
|
|
|
|
2022-04-20 04:06:18 +00:00
|
|
|
GfxData *_GfxData = DynOS_Actor_LoadFromBinary(aFilename, actorName, aFilename, false);
|
2022-04-09 01:56:22 +00:00
|
|
|
if (!_GfxData) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintError(" ERROR: Couldn't load Actor Binary \"%s\" from \"%s\"", actorName, aFilename.c_str());
|
2022-04-09 01:56:22 +00:00
|
|
|
free(actorName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* geoLayout = (*(_GfxData->mGeoLayouts.end() - 1))->mData;
|
|
|
|
if (!geoLayout) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintError(" ERROR: Couldn't load geo layout for \"%s\"", actorName);
|
2022-04-09 01:56:22 +00:00
|
|
|
free(actorName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alloc and init the actors gfx list
|
2023-05-16 03:45:04 +00:00
|
|
|
u32 id = 0;
|
2022-05-11 04:20:19 +00:00
|
|
|
ActorGfx actorGfx = { };
|
2022-04-23 22:54:39 +00:00
|
|
|
actorGfx.mGfxData = _GfxData;
|
2022-05-09 14:27:47 +00:00
|
|
|
actorGfx.mPackIndex = MOD_PACK_INDEX;
|
2023-05-19 04:29:57 +00:00
|
|
|
actorGfx.mGraphNode = (GraphNode *) DynOS_Model_LoadGeo(&id, MODEL_POOL_SESSION, geoLayout, true);
|
2023-05-07 22:48:19 +00:00
|
|
|
if (!actorGfx.mGraphNode) {
|
2023-05-19 11:20:08 +00:00
|
|
|
PrintError(" ERROR: Couldn't load graph node for \"%s\"", actorName);
|
2023-05-07 22:48:19 +00:00
|
|
|
free(actorName);
|
|
|
|
return;
|
|
|
|
}
|
2022-05-09 16:28:20 +00:00
|
|
|
actorGfx.mGraphNode->georef = georef;
|
|
|
|
|
|
|
|
// Add to custom actors
|
|
|
|
if (georef == NULL) {
|
2023-05-15 08:15:20 +00:00
|
|
|
DynosCustomActors().Add({ strdup(actorName), geoLayout });
|
2022-05-09 16:28:20 +00:00
|
|
|
georef = geoLayout;
|
|
|
|
}
|
2022-04-20 04:06:18 +00:00
|
|
|
|
|
|
|
// Add to list
|
|
|
|
DynOS_Actor_Valid(georef, actorGfx);
|
2023-05-15 08:15:20 +00:00
|
|
|
free(actorName);
|
2022-04-09 01:56:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const void *DynOS_Actor_GetLayoutFromName(const char *aActorName) {
|
|
|
|
if (aActorName == NULL) { return NULL; }
|
|
|
|
|
|
|
|
// check levels
|
|
|
|
auto& levelsArray = DynOS_Lvl_GetArray();
|
|
|
|
for (auto& lvl : levelsArray) {
|
|
|
|
for (auto& geo : lvl.second->mGeoLayouts) {
|
|
|
|
if (geo->mName == aActorName) {
|
|
|
|
return geo->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-23 01:54:11 +00:00
|
|
|
// check custom actors
|
|
|
|
for (auto& pair : DynosCustomActors()) {
|
|
|
|
if (!strcmp(aActorName, pair.first)) {
|
|
|
|
return pair.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 04:06:18 +00:00
|
|
|
// check loaded actors
|
|
|
|
for (auto& pair : DynosValidActors()) {
|
|
|
|
for (auto& geo : pair.second.mGfxData->mGeoLayouts) {
|
|
|
|
if (!strcmp(aActorName, geo->mName.begin())) {
|
|
|
|
return geo->mData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check built in actors
|
|
|
|
for (s32 i = 0; i < DynOS_Builtin_Actor_GetCount(); ++i) {
|
|
|
|
auto name = DynOS_Builtin_Actor_GetNameFromIndex(i);
|
|
|
|
if (!strcmp(aActorName, name)) {
|
|
|
|
return DynOS_Builtin_Actor_GetFromIndex(i);
|
2022-04-09 01:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2022-05-14 21:28:25 +00:00
|
|
|
ActorGfx* DynOS_Actor_GetActorGfx(const GraphNode* aGraphNode) {
|
|
|
|
if (aGraphNode == NULL) { return NULL; }
|
2022-04-20 04:06:18 +00:00
|
|
|
auto& _ValidActors = DynosValidActors();
|
2022-05-14 21:28:25 +00:00
|
|
|
|
|
|
|
// If georef is not NULL, check georef
|
|
|
|
if (aGraphNode->georef != NULL) {
|
|
|
|
if (_ValidActors.count(aGraphNode->georef) != 0) {
|
|
|
|
return &_ValidActors[aGraphNode->georef];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check graph node
|
2022-05-29 04:09:27 +00:00
|
|
|
for (const auto& _Actor : _ValidActors) {
|
|
|
|
if (_Actor.second.mGraphNode == aGraphNode) {
|
|
|
|
return (ActorGfx*)&_Actor.second;
|
|
|
|
}
|
2022-05-14 21:28:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// No actor found
|
|
|
|
return NULL;
|
2022-04-20 04:06:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Actor_Valid(const void* aGeoref, ActorGfx& aActorGfx) {
|
|
|
|
if (aGeoref == NULL) { return; }
|
|
|
|
auto& _ValidActors = DynosValidActors();
|
|
|
|
_ValidActors[aGeoref] = aActorGfx;
|
|
|
|
DynOS_Tex_Valid(aActorGfx.mGfxData);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Actor_Invalid(const void* aGeoref, s32 aPackIndex) {
|
|
|
|
if (aGeoref == NULL) { return; }
|
|
|
|
auto& _ValidActors = DynosValidActors();
|
|
|
|
if (_ValidActors.count(aGeoref) == 0) { return; }
|
|
|
|
if (_ValidActors[aGeoref].mPackIndex != aPackIndex) { return; }
|
|
|
|
|
|
|
|
DynOS_Tex_Invalid(_ValidActors[aGeoref].mGfxData);
|
|
|
|
_ValidActors.erase(aGeoref);
|
2022-04-09 01:56:22 +00:00
|
|
|
}
|
|
|
|
|
2022-04-20 04:06:18 +00:00
|
|
|
void DynOS_Actor_Override(void** aSharedChild) {
|
|
|
|
if ((aSharedChild == NULL) || (*aSharedChild == NULL)) { return; }
|
|
|
|
|
|
|
|
const void* georef = (*(GraphNode**)aSharedChild)->georef;
|
|
|
|
if (georef == NULL) { return; }
|
|
|
|
|
|
|
|
auto& _ValidActors = DynosValidActors();
|
|
|
|
if (_ValidActors.count(georef) == 0) { return; }
|
|
|
|
|
|
|
|
*aSharedChild = (void*)_ValidActors[georef].mGraphNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DynOS_Actor_Override_All(void) {
|
|
|
|
if (!gObjectLists) { return; }
|
|
|
|
// Loop through all object lists
|
|
|
|
for (s32 list : { OBJ_LIST_PLAYER, OBJ_LIST_DESTRUCTIVE, OBJ_LIST_GENACTOR, OBJ_LIST_PUSHABLE, OBJ_LIST_LEVEL, OBJ_LIST_DEFAULT, OBJ_LIST_SURFACE, OBJ_LIST_POLELIKE, OBJ_LIST_UNIMPORTANT }) {
|
|
|
|
struct Object *_Head = (struct Object *) &gObjectLists[list];
|
|
|
|
for (struct Object *_Object = (struct Object *) _Head->header.next; _Object != _Head; _Object = (struct Object *) _Object->header.next) {
|
|
|
|
if (_Object->header.gfx.sharedChild != NULL && _Object->header.gfx.sharedChild->georef != NULL) {
|
2022-04-23 10:05:16 +00:00
|
|
|
GraphNode* georef = (GraphNode*)_Object->header.gfx.sharedChild->georef;
|
2023-05-16 03:45:04 +00:00
|
|
|
u32 id = 0;
|
2023-05-19 04:29:57 +00:00
|
|
|
_Object->header.gfx.sharedChild = DynOS_Model_LoadGeo(&id, MODEL_POOL_PERMANENT, georef, true);
|
2022-04-20 04:06:18 +00:00
|
|
|
}
|
|
|
|
DynOS_Actor_Override((void**)&_Object->header.gfx.sharedChild);
|
|
|
|
}
|
|
|
|
}
|
2022-04-09 01:56:22 +00:00
|
|
|
}
|
2022-05-09 14:27:47 +00:00
|
|
|
|
|
|
|
void DynOS_Actor_ModShutdown() {
|
|
|
|
auto& _DynosCustomActors = DynosCustomActors();
|
|
|
|
while (_DynosCustomActors.Count() > 0) {
|
|
|
|
auto& pair = _DynosCustomActors[0];
|
|
|
|
DynOS_Actor_Invalid(pair.second, MOD_PACK_INDEX);
|
|
|
|
free((void*)pair.first);
|
|
|
|
_DynosCustomActors.Remove(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& _ValidActors = DynosValidActors();
|
|
|
|
for (auto it = _ValidActors.cbegin(); it != _ValidActors.cend();) {
|
|
|
|
auto& actorGfx = it->second;
|
|
|
|
if (actorGfx.mPackIndex == MOD_PACK_INDEX) {
|
|
|
|
DynOS_Gfx_Free(actorGfx.mGfxData);
|
|
|
|
_ValidActors.erase(it++);
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DynOS_Actor_Override_All();
|
|
|
|
}
|