1
0
Fork 0

Compare commits

...

3 Commits

Author SHA1 Message Date
Kuhnovic 34673acac8
Merge b8d966029c 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
Koen Bussemaker b8d966029c Change: Allow building rail over bridges / tunnels and through stations / waypoints. 2025-05-25 14:52:31 +02:00
5 changed files with 47 additions and 36 deletions

View File

@ -83,7 +83,7 @@ void AIInstance::Died()
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");
}

View File

@ -884,11 +884,33 @@ static CommandCost CmdRailTrackHelper(DoCommandFlags flags, TileIndex tile, Tile
CommandCost ret = ValidateAutoDrag(&trackdir, tile, end_tile);
if (ret.Failed()) return ret;
int distance_remaining = static_cast<int>(DistanceManhattan(tile, end_tile)) + 1;
auto advance = [&] {
distance_remaining--;
tile = TileAddByDiagDir(tile, TrackdirToExitdir(trackdir));
trackdir = NextTrackdir(trackdir);
};
bool had_success = false;
CommandCost last_error = CMD_ERROR;
for (;;) {
ret = remove ? Command<CMD_REMOVE_SINGLE_RAIL>::Do(flags, tile, TrackdirToTrack(trackdir)) : Command<CMD_BUILD_SINGLE_RAIL>::Do(flags, tile, railtype, TrackdirToTrack(trackdir), auto_remove_signals);
if (!remove && !fail_on_obstacle && last_error.GetErrorMessage() == STR_ERROR_ALREADY_BUILT) had_success = true;
for (; distance_remaining > 0; advance()) {
if (!remove && !fail_on_obstacle && CheckTileOwnership(tile).Succeeded() && IsCompatibleRail(GetRailType(tile), railtype)) {
/* Skip stations and waypoints */
if (HasStationTileRail(tile) && GetRailStationTrack(tile) == track) continue;
/* Skip bridges and tunnels */
if (IsTileType(tile, MP_TUNNELBRIDGE) && GetTunnelBridgeTransportType(tile) == TRANSPORT_RAIL
&& DiagDirToDiagTrackdir(GetTunnelBridgeDirection(tile)) == trackdir ) {
const uint length = GetTunnelBridgeLength(tile, GetOtherTunnelBridgeEnd(tile));
for (uint i = 0; i <= length; ++i) advance();
continue;
}
}
ret = remove ? Command<CMD_REMOVE_SINGLE_RAIL>::Do(flags, tile, TrackdirToTrack(trackdir))
: Command<CMD_BUILD_SINGLE_RAIL>::Do(flags, tile, railtype, TrackdirToTrack(trackdir), auto_remove_signals);
if (!remove && !fail_on_obstacle && ret.GetErrorMessage() == STR_ERROR_ALREADY_BUILT) had_success = true;
if (ret.Failed()) {
last_error = std::move(ret);
@ -903,13 +925,6 @@ static CommandCost CmdRailTrackHelper(DoCommandFlags flags, TileIndex tile, Tile
had_success = true;
total_cost.AddCost(ret.GetCost());
}
if (tile == end_tile) break;
tile += ToTileIndexDiff(_trackdelta[trackdir]);
/* toggle railbit for the non-diagonal tracks */
if (!IsDiagonalTrackdir(trackdir)) ToggleBit(trackdir, 0);
}
if (had_success) return total_cost;

View File

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

View File

@ -256,7 +256,7 @@ public:
void ReleaseSQObject(HSQOBJECT *obj);
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.
/**
@ -288,9 +288,9 @@ protected:
virtual void LoadDummyScript() = 0;
private:
class ScriptController *controller = nullptr; ///< The script main class.
class ScriptStorage *storage = nullptr; ///< Some global information for each running script.
SQObject *instance = nullptr; ///< Squirrel-pointer to the script main class.
std::unique_ptr<class ScriptStorage> storage; ///< Some global information for each running script.
std::unique_ptr<class ScriptController> controller; ///< 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_dead = false; ///< True if the script has been stopped.