LandSandBoat / server

:sailboat: LandSandBoat - a server emulator for Final Fantasy XI
https://landsandboat.github.io/server/
GNU General Public License v3.0
289 stars 577 forks source link

🔨 `EntityId` tracking structure #6167

Open zach2good opened 2 weeks ago

zach2good commented 2 weeks ago

I affirm:

Describe the feature

A recurring theme of the server is the caching/looking up of pointers to invalid/gone entities. As much as I like to harp on about the abuse of DEs, invalid pointers to Pets and Trusts are still possible, so it is a legitimate problem. We need a mechanism to safely hold references to entities that might no longer exist when you try to look them up.

struct EntityId
{
    u64 hash; // Incremental is enough here. A process will never be up long enough for this to overflow. If it does we've won the stability game.
    u16 zoneId;
    u16 index;
};

TODO: A global (per process) tracker of entities.

TODO: Lookup mechanism.

TODO: On entity destruction, look up your own lookup entry and invalidate it.

zach2good commented 2 weeks ago

I'm not sure if it's even possible to stop people from taking references/pointers to objects. We wouldn't even want std::optional. We'd need:

auto entityToken = PEntity->getToken();

// later

if (auto PEntity = entityToken.resolve())
{
    // Successfully looked-up
}

// later

if (!entityToken.valid())
{
    // Log
    return;
}
zach2good commented 2 weeks ago

Does this mean we'll move all entities out of Zone->ZoneEntities? Unknown.

zach2good commented 2 weeks ago

Creation/lookup/removal of these tokens needs to be constant time. It needs to proliferate into Lua

zach2good commented 2 weeks ago

Partially related to: https://github.com/LandSandBoat/server/issues/5065