1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Adam Zmuda 38af221b2f
Merge 0fc4be750b into 7bb4940ebd 2025-07-19 08:31:41 +00:00
Peter Nelson 7bb4940ebd
Codechange: Use unique_ptr for all pointers in script instance. (#14339)
Removes manual memory management with new/delete.
2025-07-19 09:29:30 +01:00
kojonek2 0fc4be750b Fix #12900: Join station window will be shown for stations with existing stations in tile gaps 2025-06-19 20:23:18 +02:00
5 changed files with 32 additions and 40 deletions

View File

@ -83,7 +83,7 @@ void AIInstance::Died()
void AIInstance::LoadDummyScript() void AIInstance::LoadDummyScript()
{ {
ScriptAllocatorScope alloc_scope(this->engine); ScriptAllocatorScope alloc_scope(this->engine.get());
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
} }

View File

@ -53,7 +53,7 @@ static ScriptStorage &GetStorage()
/* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr; /* static */ ScriptInstance *ScriptObject::ActiveInstance::active = nullptr;
ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine) ScriptObject::ActiveInstance::ActiveInstance(ScriptInstance &instance) : alc_scope(instance.engine.get())
{ {
this->last_active = ScriptObject::ActiveInstance::active; this->last_active = ScriptObject::ActiveInstance::active;
ScriptObject::ActiveInstance::active = &instance; ScriptObject::ActiveInstance::active = &instance;
@ -230,8 +230,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ bool ScriptObject::CanSuspend() /* static */ bool ScriptObject::CanSuspend()
{ {
Squirrel *squirrel = ScriptObject::GetActiveInstance().engine; Squirrel &squirrel = *ScriptObject::GetActiveInstance().engine;
return GetStorage().allow_do_command && squirrel->CanSuspend(); return GetStorage().allow_do_command && squirrel.CanSuspend();
} }
/* static */ ScriptEventQueue &ScriptObject::GetEventQueue() /* static */ ScriptEventQueue &ScriptObject::GetEventQueue()

View File

@ -50,8 +50,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
ScriptInstance::ScriptInstance(std::string_view api_name) ScriptInstance::ScriptInstance(std::string_view api_name)
{ {
this->storage = new ScriptStorage(); this->storage = std::make_unique<ScriptStorage>();
this->engine = new Squirrel(api_name); this->engine = std::make_unique<Squirrel>(api_name);
this->engine->SetPrintFunction(&PrintFunc); this->engine->SetPrintFunction(&PrintFunc);
} }
@ -59,10 +59,10 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
{ {
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->controller = new ScriptController(company); this->controller = std::make_unique<ScriptController>(company);
/* Register the API functions and classes */ /* Register the API functions and classes */
this->engine->SetGlobalPointer(this->engine); this->engine->SetGlobalPointer(this->engine.get());
this->RegisterAPI(); this->RegisterAPI();
if (this->IsDead()) { if (this->IsDead()) {
/* Failed to register API; a message has already been logged. */ /* Failed to register API; a message has already been logged. */
@ -81,12 +81,11 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
} }
/* Create the main-class */ /* Create the main-class */
this->instance = new SQObject(); this->instance = std::make_unique<SQObject>();
if (!this->engine->CreateClassInstance(instance_name, this->controller, this->instance)) { if (!this->engine->CreateClassInstance(instance_name, this->controller.get(), this->instance.get())) {
/* If CreateClassInstance has returned false instance has not been /* If CreateClassInstance has returned false instance has not been
* registered with squirrel, so avoid trying to Release it by clearing it now */ * registered with squirrel, so avoid trying to Release it by clearing it now */
delete this->instance; this->instance.reset();
this->instance = nullptr;
this->Died(); this->Died();
return; return;
} }
@ -158,11 +157,10 @@ ScriptInstance::~ScriptInstance()
ScriptObject::ActiveInstance active(*this); ScriptObject::ActiveInstance active(*this);
this->in_shutdown = true; this->in_shutdown = true;
if (instance != nullptr) this->engine->ReleaseObject(this->instance); if (instance != nullptr) this->engine->ReleaseObject(this->instance.get());
if (engine != nullptr) delete this->engine;
delete this->storage; /* Engine must be reset explicitly in scope of the active instance. */
delete this->controller; this->engine.reset();
delete this->instance;
} }
void ScriptInstance::Continue() void ScriptInstance::Continue()
@ -179,11 +177,9 @@ void ScriptInstance::Died()
this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache this->last_allocated_memory = this->GetAllocatedMemory(); // Update cache
if (this->instance != nullptr) this->engine->ReleaseObject(this->instance); if (this->instance != nullptr) this->engine->ReleaseObject(this->instance.get());
delete this->instance; this->engine.reset();
delete this->engine; this->instance.reset();
this->instance = nullptr;
this->engine = nullptr;
} }
void ScriptInstance::GameLoop() void ScriptInstance::GameLoop()

View File

@ -256,7 +256,7 @@ public:
void ReleaseSQObject(HSQOBJECT *obj); void ReleaseSQObject(HSQOBJECT *obj);
protected: protected:
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm. std::unique_ptr<class Squirrel> engine; ///< A wrapper around the squirrel vm.
std::string api_version{}; ///< Current API used by this script. std::string api_version{}; ///< Current API used by this script.
/** /**
@ -288,9 +288,9 @@ protected:
virtual void LoadDummyScript() = 0; virtual void LoadDummyScript() = 0;
private: private:
class ScriptController *controller = nullptr; ///< The script main class. std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
class ScriptStorage *storage = nullptr; ///< Some global information for each running script. std::unique_ptr<class ScriptController> controller; ///< The script main class.
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class. std::unique_ptr<SQObject> instance; ///< Squirrel-pointer to the script main class.
bool is_started = false; ///< Is the scripts constructor executed? bool is_started = false; ///< Is the scripts constructor executed?
bool is_dead = false; ///< True if the script has been stopped. bool is_dead = false; ///< True if the script has been stopped.

View File

@ -2222,7 +2222,7 @@ static void AddNearbyStation(TileIndex tile, TileArea *ctx)
* @tparam T the station filter type, for stations to look for * @tparam T the station filter type, for stations to look for
*/ */
template <class T> template <class T>
static const BaseStation *FindStationsNearby(TileArea ta, bool distant_join) static void FindStationsNearby(TileArea ta, bool distant_join)
{ {
TileArea ctx = ta; TileArea ctx = ta;
@ -2230,11 +2230,6 @@ static const BaseStation *FindStationsNearby(TileArea ta, bool distant_join)
_stations_nearby_list.push_back(NEW_STATION); _stations_nearby_list.push_back(NEW_STATION);
_deleted_stations_nearby.clear(); _deleted_stations_nearby.clear();
/* Check the inside, to return, if we sit on another station */
for (TileIndex t : ta) {
if (t < Map::Size() && IsTileType(t, MP_STATION) && T::IsValidID(GetStationIndex(t))) return BaseStation::GetByTile(t);
}
/* Look for deleted stations */ /* Look for deleted stations */
for (const BaseStation *st : BaseStation::Iterate()) { for (const BaseStation *st : BaseStation::Iterate()) {
if (T::IsValidBaseStation(st) && !st->IsInUse() && st->owner == _local_company) { if (T::IsValidBaseStation(st) && !st->IsInUse() && st->owner == _local_company) {
@ -2251,17 +2246,20 @@ static const BaseStation *FindStationsNearby(TileArea ta, bool distant_join)
} }
} }
/* Add stations that are within station tile area. Stations do not have to occupy all tiles */
for (auto t : ta) {
AddNearbyStation<T>(t, &ctx);
}
/* Only search tiles where we have a chance to stay within the station spread. /* Only search tiles where we have a chance to stay within the station spread.
* The complete check needs to be done in the callback as we don't know the * The complete check needs to be done in the callback as we don't know the
* extent of the found station, yet. */ * extent of the found station, yet. */
if (distant_join && std::min(ta.w, ta.h) >= _settings_game.station.station_spread) return nullptr; if (distant_join && std::min(ta.w, ta.h) >= _settings_game.station.station_spread) return;
uint max_dist = distant_join ? _settings_game.station.station_spread - std::min(ta.w, ta.h) : 1; uint max_dist = distant_join ? _settings_game.station.station_spread - std::min(ta.w, ta.h) : 1;
for (auto tile : SpiralTileSequence(TileAddByDir(ctx.tile, DIR_N), max_dist, ta.w, ta.h)) { for (auto tile : SpiralTileSequence(TileAddByDir(ctx.tile, DIR_N), max_dist, ta.w, ta.h)) {
AddNearbyStation<T>(tile, &ctx); AddNearbyStation<T>(tile, &ctx);
} }
return nullptr;
} }
static constexpr NWidgetPart _nested_select_station_widgets[] = { static constexpr NWidgetPart _nested_select_station_widgets[] = {
@ -2421,7 +2419,7 @@ static WindowDesc _select_station_desc(
* @return whether we need to show the station selection window. * @return whether we need to show the station selection window.
*/ */
template <class T> template <class T>
static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc) static bool StationJoinerNeeded(const StationPickerCmdProc &proc)
{ {
/* Only show selection if distant join is enabled in the settings */ /* Only show selection if distant join is enabled in the settings */
if (!_settings_game.station.distant_join_stations) return false; if (!_settings_game.station.distant_join_stations) return false;
@ -2439,9 +2437,7 @@ static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc)
if (!_ctrl_pressed) return false; if (!_ctrl_pressed) return false;
/* Now check if we could build there */ /* Now check if we could build there */
if (!proc(true, StationID::Invalid())) return false; return proc(true, StationID::Invalid());
return FindStationsNearby<T>(ta, false) == nullptr;
} }
/** /**
@ -2453,7 +2449,7 @@ static bool StationJoinerNeeded(TileArea ta, const StationPickerCmdProc &proc)
template <class T> template <class T>
void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc&& proc) void ShowSelectBaseStationIfNeeded(TileArea ta, StationPickerCmdProc&& proc)
{ {
if (StationJoinerNeeded<T>(ta, proc)) { if (StationJoinerNeeded<T>(proc)) {
if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace(); if (!_settings_client.gui.persistent_buildingtools) ResetObjectToPlace();
new SelectStationWindow<T>(_select_station_desc, ta, std::move(proc)); new SelectStationWindow<T>(_select_station_desc, ta, std::move(proc));
} else { } else {