Skip to content

Commit

Permalink
Use ActorPtr (RefCountingObjectPtr<Actor>) everywhere.
Browse files Browse the repository at this point in the history
This introduces `RefCountingObject` to the codebase. Using it makes it possible to pass object between C++ and AngelScript without worry. See https://github.com/only-a-ptr/RefCountingObject-AngelScript

In the case of Actor, the refcounting was dummy and the game would certainly crash if a script kept pointer to an Actor which got removed.

Signifficant code changes:
- utils/memory/RefCountingObject* - new files.
- ForwardDeclarations.h - added ActorPtr
- Application.h - updated comments regarding message queue arguments. Instead of sending weak Actor* pointers, a strong ActorPtr* is sent.
- main.cpp - updated message handling to cope with new pointers.
- SimData.h - new entry in `enum class ActorState`: `DISPOSED` - means the actor was removed from simulation and it's component objects were deleted, but it still lives as an empty shell to satisfy existing pointers.
- ActorManager.cpp - DeleteActorInternal() - changed to only dispose, but not delete the actor - the deleting will be done by RefCountingObjectPtr.
- ScriptEngine.h/cpp - added function `setEventsEnabled()` which stops the TRIGGER_EVENT() macros from working, as a hack for fast dirty shutdown.
  • Loading branch information
ohlidalp committed Oct 12, 2022
1 parent aed38fd commit e3835d9
Show file tree
Hide file tree
Showing 110 changed files with 1,013 additions and 415 deletions.
2 changes: 1 addition & 1 deletion source/main/AppContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void AppContext::windowResized(Ogre::RenderWindow* rw)
App::GetOverlayWrapper()->windowResized();
if (App::sim_state->getEnum<AppState>() == RoR::AppState::SIMULATION)
{
for (Actor* actor: App::GetGameContext()->GetActorManager()->GetActors())
for (ActorPtr& actor: App::GetGameContext()->GetActorManager()->GetActors())
{
actor->ar_dashboard->windowResized();
}
Expand Down
8 changes: 4 additions & 4 deletions source/main/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ enum MsgType
MSG_SIM_UNLOAD_TERRN_REQUESTED,
MSG_SIM_SPAWN_ACTOR_REQUESTED, //!< Payload = RoR::ActorSpawnRequest* (owner)
MSG_SIM_MODIFY_ACTOR_REQUESTED, //!< Payload = RoR::ActorModifyRequest* (owner)
MSG_SIM_DELETE_ACTOR_REQUESTED, //!< Payload = RoR::Actor* (weak)
MSG_SIM_SEAT_PLAYER_REQUESTED, //!< Payload = RoR::Actor* (weak) | nullptr
MSG_SIM_DELETE_ACTOR_REQUESTED, //!< Payload = RoR::ActorPtr* (owner)
MSG_SIM_SEAT_PLAYER_REQUESTED, //!< Payload = RoR::ActorPtr (owner) | nullptr
MSG_SIM_TELEPORT_PLAYER_REQUESTED, //!< Payload = Ogre::Vector3* (owner)
MSG_SIM_HIDE_NET_ACTOR_REQUESTED, //!< Payload = Actor* (weak)
MSG_SIM_UNHIDE_NET_ACTOR_REQUESTED, //!< Payload = Actor* (weak)
MSG_SIM_HIDE_NET_ACTOR_REQUESTED, //!< Payload = ActorPtr* (owner)
MSG_SIM_UNHIDE_NET_ACTOR_REQUESTED, //!< Payload = ActorPtr* (owner)
// GUI
MSG_GUI_OPEN_MENU_REQUESTED,
MSG_GUI_CLOSE_MENU_REQUESTED,
Expand Down
3 changes: 3 additions & 0 deletions source/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ set(SOURCE_FILES
terrain/Terrain.{h,cpp}
terrain/TerrainObjectManager.{h,cpp}
threadpool/ThreadPool.h
utils/memory/RefCountingObject.h
utils/memory/RefCountingObjectPtr.h
utils/ConfigFile.{h,cpp}
utils/ErrorUtils.{h,cpp}
utils/ForceFeedback.{h,cpp}
Expand Down Expand Up @@ -344,6 +346,7 @@ target_include_directories(${BINNAME} PRIVATE
terrain
terrain/map
threadpool
utils/memory
utils
)

Expand Down
4 changes: 4 additions & 0 deletions source/main/ForwardDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
/// @author Petr Ohlidal
/// @date 12/2013

#include "RefCountingObjectPtr.h"

#pragma once

Expand Down Expand Up @@ -141,6 +142,9 @@ namespace RoR
struct client_t;
struct authorinfo_t;

/// AngelScript-friendly shared pointers
typedef RefCountingObjectPtr<Actor> ActorPtr;

namespace GUI
{
class ConsoleView;
Expand Down
66 changes: 33 additions & 33 deletions source/main/GameContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ void GameContext::UnloadTerrain()
// --------------------------------
// Actors (physics and netcode)

Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
ActorPtr GameContext::SpawnActor(ActorSpawnRequest& rq)
{
if (rq.asr_origin == ActorSpawnRequest::Origin::USER)
{
Expand Down Expand Up @@ -206,7 +206,7 @@ Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
}
#endif //SOCKETW

Actor* fresh_actor = m_actor_manager.CreateNewActor(rq, def);
ActorPtr fresh_actor = m_actor_manager.CreateNewActor(rq, def);

// lock slide nodes after spawning the actor?
if (def->slide_nodes_connect_instantly)
Expand All @@ -219,7 +219,7 @@ Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
m_last_spawned_actor = fresh_actor;
if (fresh_actor->ar_driveable != NOT_DRIVEABLE)
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)fresh_actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(fresh_actor))));
}
if (rq.asr_spawnbox == nullptr)
{
Expand All @@ -233,13 +233,13 @@ Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
fresh_actor->ar_num_nodes > 0 &&
App::diag_preset_veh_enter->getBool())
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)fresh_actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(fresh_actor))));
}
if (fresh_actor->ar_driveable != NOT_DRIVEABLE &&
fresh_actor->ar_num_nodes > 0 &&
App::cli_preset_veh_enter->getBool())
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)fresh_actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(fresh_actor))));
}
}
else if (rq.asr_origin == ActorSpawnRequest::Origin::TERRN_DEF)
Expand Down Expand Up @@ -281,7 +281,7 @@ Actor* GameContext::SpawnActor(ActorSpawnRequest& rq)
if (fresh_actor->ar_driveable != NOT_DRIVEABLE &&
rq.asr_origin != ActorSpawnRequest::Origin::NETWORK)
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)fresh_actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(fresh_actor))));
}
}

Expand Down Expand Up @@ -340,7 +340,7 @@ void GameContext::ModifyActor(ActorModifyRequest& rq)
}
}

void GameContext::DeleteActor(Actor* actor)
void GameContext::DeleteActor(ActorPtr actor)
{
if (actor == m_player_actor)
{
Expand Down Expand Up @@ -386,9 +386,9 @@ void GameContext::DeleteActor(Actor* actor)
m_actor_manager.DeleteActorInternal(actor);
}

void GameContext::ChangePlayerActor(Actor* actor)
void GameContext::ChangePlayerActor(ActorPtr actor)
{
Actor* prev_player_actor = m_player_actor;
ActorPtr prev_player_actor = m_player_actor;
m_player_actor = actor;

// hide any old dashes
Expand Down Expand Up @@ -487,12 +487,12 @@ void GameContext::ChangePlayerActor(Actor* actor)
m_actor_manager.UpdateSleepingState(m_player_actor, 0.f);
}

Actor* GameContext::FetchPrevVehicleOnList()
ActorPtr GameContext::FetchPrevVehicleOnList()
{
return m_actor_manager.FetchPreviousVehicleOnList(m_player_actor, m_prev_player_actor);
}

Actor* GameContext::FetchNextVehicleOnList()
ActorPtr GameContext::FetchNextVehicleOnList()
{
return m_actor_manager.FetchNextVehicleOnList(m_player_actor, m_prev_player_actor);
}
Expand All @@ -502,7 +502,7 @@ void GameContext::UpdateActors()
m_actor_manager.UpdateActors(m_player_actor);
}

Actor* GameContext::FindActorByCollisionBox(std::string const & ev_src_instance_name, std::string const & box_name)
ActorPtr GameContext::FindActorByCollisionBox(std::string const & ev_src_instance_name, std::string const & box_name)
{
return m_actor_manager.FindActorInsideBox(App::GetSimTerrain()->GetCollisions(),
ev_src_instance_name, box_name);
Expand Down Expand Up @@ -563,7 +563,7 @@ void GameContext::ShowLoaderGUI(int type, const Ogre::String& instance, const Og
if (!(App::mp_state->getEnum<MpState>() == MpState::CONNECTED))
{
collision_box_t* spawnbox = App::GetSimTerrain()->GetCollisions()->getBox(instance, box);
for (auto actor : this->GetActorManager()->GetActors())
for (ActorPtr actor : this->GetActorManager()->GetActors())
{
for (int i = 0; i < actor->ar_num_nodes; i++)
{
Expand Down Expand Up @@ -749,7 +749,7 @@ void GameContext::TeleportPlayer(float x, float z)

float src_agl = std::numeric_limits<float>::max();
float dst_agl = std::numeric_limits<float>::max();
for (auto actor : actors)
for (ActorPtr actor : actors)
{
for (int i = 0; i < actor->ar_num_nodes; i++)
{
Expand All @@ -762,7 +762,7 @@ void GameContext::TeleportPlayer(float x, float z)

translation += Ogre::Vector3::UNIT_Y * (std::max(0.0f, src_agl) - dst_agl);

for (auto actor : actors)
for (ActorPtr actor : actors)
{
actor->resetPosition(actor->ar_nodes[0].AbsPosition + translation, false);
}
Expand Down Expand Up @@ -892,8 +892,8 @@ void GameContext::UpdateSimInputEvents(float dt)
{
// find the nearest vehicle
float mindist = 1000.0;
Actor* nearest_actor = nullptr;
for (auto actor : this->GetActorManager()->GetActors())
ActorPtr nearest_actor = nullptr;
for (ActorPtr& actor : this->GetActorManager()->GetActors())
{
if (!actor->ar_driveable)
continue;
Expand All @@ -916,7 +916,7 @@ void GameContext::UpdateSimInputEvents(float dt)

if (mindist < 20.0)
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)nearest_actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(nearest_actor))));
}
}
else // We're in a vehicle -> If moving slowly enough, get out
Expand All @@ -933,20 +933,20 @@ void GameContext::UpdateSimInputEvents(float dt)
// enter next truck
if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_ENTER_NEXT_TRUCK, 0.25f))
{
Actor* actor = this->FetchNextVehicleOnList();
ActorPtr actor = this->FetchNextVehicleOnList();
if (actor != this->GetPlayerActor())
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(actor))));
}
}

// enter previous truck
if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_ENTER_PREVIOUS_TRUCK, 0.25f))
{
Actor* actor = this->FetchPrevVehicleOnList();
ActorPtr actor = this->FetchPrevVehicleOnList();
if (actor != this->GetPlayerActor())
{
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)actor));
this->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(actor))));
}
}

Expand All @@ -967,9 +967,9 @@ void GameContext::UpdateSimInputEvents(float dt)
{
// Find nearest actor
const Ogre::Vector3 position = App::GetGameContext()->GetPlayerCharacter()->getPosition();
Actor* nearest_actor = nullptr;
ActorPtr nearest_actor = nullptr;
float min_squared_distance = std::numeric_limits<float>::max();
for (auto actor : App::GetGameContext()->GetActorManager()->GetActors())
for (ActorPtr& actor : App::GetGameContext()->GetActorManager()->GetActors())
{
float squared_distance = position.squaredDistance(actor->ar_nodes[0].AbsPosition);
if (squared_distance < min_squared_distance)
Expand Down Expand Up @@ -1106,7 +1106,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
// remove current truck
if (App::GetInputEngine()->getEventBoolValue(EV_COMMON_REMOVE_CURRENT_TRUCK))
{
App::GetGameContext()->PushMessage(Message(MSG_SIM_DELETE_ACTOR_REQUESTED, (void*)m_player_actor));
App::GetGameContext()->PushMessage(Message(MSG_SIM_DELETE_ACTOR_REQUESTED, static_cast<void*>(new ActorPtr(m_player_actor))));
}

// blinkers
Expand All @@ -1128,7 +1128,7 @@ void GameContext::UpdateCommonInputEvents(float dt)

if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_TRUCK_REMOVE))
{
App::GetGameContext()->PushMessage(Message(MSG_SIM_DELETE_ACTOR_REQUESTED, (void*)m_player_actor));
App::GetGameContext()->PushMessage(Message(MSG_SIM_DELETE_ACTOR_REQUESTED, static_cast<void*>(new ActorPtr(m_player_actor))));
}

if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_ROPELOCK))
Expand Down Expand Up @@ -1161,7 +1161,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_TOGGLE_DEBUG_VIEW))
{
m_player_actor->GetGfxActor()->ToggleDebugView();
for (auto actor : m_player_actor->getAllLinkedActors())
for (ActorPtr actor : m_player_actor->getAllLinkedActors())
{
actor->GetGfxActor()->SetDebugView(m_player_actor->GetGfxActor()->GetDebugView());
}
Expand All @@ -1170,7 +1170,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
if (App::GetInputEngine()->getEventBoolValueBounce(EV_COMMON_CYCLE_DEBUG_VIEWS))
{
m_player_actor->GetGfxActor()->CycleDebugViews();
for (auto actor : m_player_actor->getAllLinkedActors())
for (ActorPtr actor : m_player_actor->getAllLinkedActors())
{
actor->GetGfxActor()->SetDebugView(m_player_actor->GetGfxActor()->GetDebugView());
}
Expand All @@ -1190,8 +1190,8 @@ void GameContext::UpdateCommonInputEvents(float dt)
App::mp_state->getEnum<MpState>() != MpState::CONNECTED &&
m_player_actor->ar_driveable != AIRPLANE)
{
Actor* rescuer = nullptr;
for (auto actor : App::GetGameContext()->GetActorManager()->GetActors())
ActorPtr rescuer = nullptr;
for (ActorPtr& actor : App::GetGameContext()->GetActorManager()->GetActors())
{
if (actor->ar_rescuer_flag)
{
Expand All @@ -1204,7 +1204,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
}
else
{
App::GetGameContext()->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, (void*)rescuer));
App::GetGameContext()->PushMessage(Message(MSG_SIM_SEAT_PLAYER_REQUESTED, static_cast<void*>(new ActorPtr(rescuer))));
}
}

Expand Down Expand Up @@ -1239,7 +1239,7 @@ void GameContext::UpdateCommonInputEvents(float dt)
// toggle physics
if (RoR::App::GetInputEngine()->getEventBoolValueBounce(EV_TRUCK_TOGGLE_PHYSICS))
{
for (auto actor : App::GetGameContext()->GetPlayerActor()->getAllLinkedActors())
for (ActorPtr actor : App::GetGameContext()->GetPlayerActor()->getAllLinkedActors())
{
actor->ar_physics_paused = !App::GetGameContext()->GetPlayerActor()->ar_physics_paused;
}
Expand Down Expand Up @@ -1686,7 +1686,7 @@ void GameContext::UpdateTruckInputEvents(float dt)
}

m_player_actor->UpdatePropAnimInputEvents();
for (Actor* linked_actor : m_player_actor->getAllLinkedActors())
for (ActorPtr linked_actor : m_player_actor->getAllLinkedActors())
{
linked_actor->UpdatePropAnimInputEvents();
}
Expand Down
26 changes: 13 additions & 13 deletions source/main/GameContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,22 +114,22 @@ class GameContext
/// @name Actors
/// @{

Actor* SpawnActor(ActorSpawnRequest& rq);
ActorPtr SpawnActor(ActorSpawnRequest& rq);
void ModifyActor(ActorModifyRequest& rq);
void DeleteActor(Actor* actor);
void DeleteActor(ActorPtr actor);
void UpdateActors();
ActorManager* GetActorManager() { return &m_actor_manager; }
Actor* FetchPrevVehicleOnList();
Actor* FetchNextVehicleOnList();
Actor* FindActorByCollisionBox(std::string const & ev_src_instance_name, std::string const & box_name);
ActorPtr FetchPrevVehicleOnList();
ActorPtr FetchNextVehicleOnList();
ActorPtr FindActorByCollisionBox(std::string const & ev_src_instance_name, std::string const & box_name);
void RespawnLastActor();
void SpawnPreselectedActor(std::string const& preset_vehicle, std::string const& preset_veh_config); //!< needs `Character` to exist

Actor* GetPlayerActor() { return m_player_actor; }
Actor* GetPrevPlayerActor() { return m_prev_player_actor; }
Actor* GetLastSpawnedActor() { return m_last_spawned_actor; } //!< Last actor spawned by user and still alive.
void SetPrevPlayerActor(Actor* actor) { m_prev_player_actor = actor; }
void ChangePlayerActor(Actor* actor);
ActorPtr GetPlayerActor() { return m_player_actor; }
ActorPtr GetPrevPlayerActor() { return m_prev_player_actor; }
ActorPtr GetLastSpawnedActor() { return m_last_spawned_actor; } //!< Last actor spawned by user and still alive.
void SetPrevPlayerActor(ActorPtr actor) { m_prev_player_actor = actor; }
void ChangePlayerActor(ActorPtr actor);

void ShowLoaderGUI(int type, const Ogre::String& instance, const Ogre::String& box);
void OnLoaderGuiCancel(); //!< GUI callback
Expand Down Expand Up @@ -181,9 +181,9 @@ class GameContext

// Actors (physics and netcode)
ActorManager m_actor_manager;
Actor* m_player_actor = nullptr; //!< Actor (vehicle or machine) mounted and controlled by player
Actor* m_prev_player_actor = nullptr; //!< Previous actor (vehicle or machine) mounted and controlled by player
Actor* m_last_spawned_actor = nullptr; //!< Last actor spawned by user and still alive.
ActorPtr m_player_actor = nullptr; //!< Actor (vehicle or machine) mounted and controlled by player
ActorPtr m_prev_player_actor = nullptr; //!< Previous actor (vehicle or machine) mounted and controlled by player
ActorPtr m_last_spawned_actor = nullptr; //!< Last actor spawned by user and still alive.

CacheEntry* m_last_cache_selection = nullptr; //!< Vehicle/load
CacheEntry* m_last_skin_selection = nullptr;
Expand Down
Loading

0 comments on commit e3835d9

Please sign in to comment.