1
0
Fork 0

Compare commits

...

4 Commits

Author SHA1 Message Date
Peter Nelson ae1e934325
Merge b96da4d7a0 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
Peter Nelson b96da4d7a0
Change: Automatically push/pop colours when formatting a sub-string.
Reverts 226a44bf86.

This universally prevents the sub-string from changing colours in the outer string.
2025-04-15 19:31:09 +01:00
Peter Nelson ffed3c7f1c
Change: Don't replace stripped control codes with '?' for scripts. 2025-04-15 19:31:08 +01:00
6 changed files with 36 additions and 35 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

@ -1231,7 +1231,7 @@ STR_CONFIG_SETTING_EXPAND_ALL :Expand all
STR_CONFIG_SETTING_COLLAPSE_ALL :Collapse all STR_CONFIG_SETTING_COLLAPSE_ALL :Collapse all
STR_CONFIG_SETTING_RESET_ALL :Reset all values STR_CONFIG_SETTING_RESET_ALL :Reset all values
STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT :(no explanation available) STR_CONFIG_SETTING_NO_EXPLANATION_AVAILABLE_HELPTEXT :(no explanation available)
STR_CONFIG_SETTING_VALUE :{PUSH_COLOUR}{ORANGE}{STRING1}{POP_COLOUR} STR_CONFIG_SETTING_VALUE :{ORANGE}{STRING1}
STR_CONFIG_SETTING_DEFAULT_VALUE :{LTBLUE}Default value: {ORANGE}{STRING1} STR_CONFIG_SETTING_DEFAULT_VALUE :{LTBLUE}Default value: {ORANGE}{STRING1}
STR_CONFIG_SETTING_TYPE :{LTBLUE}Setting type: {ORANGE}{STRING} STR_CONFIG_SETTING_TYPE :{LTBLUE}Setting type: {ORANGE}{STRING}
STR_CONFIG_SETTING_TYPE_CLIENT :Client setting (not stored in saves; affects all games) STR_CONFIG_SETTING_TYPE_CLIENT :Client setting (not stored in saves; affects all games)
@ -3624,17 +3624,17 @@ STR_NEWGRF_LIST_COMPATIBLE :{YELLOW}Found c
STR_NEWGRF_LIST_MISSING :{RED}Missing files STR_NEWGRF_LIST_MISSING :{RED}Missing files
# NewGRF 'it's broken' warnings # NewGRF 'it's broken' warnings
STR_NEWGRF_BROKEN :{WHITE}Behaviour of NewGRF '{PUSH_COLOUR}{0:RAW_STRING}{POP_COLOUR}' is likely to cause desyncs and/or crashes STR_NEWGRF_BROKEN :{WHITE}Behaviour of NewGRF '{0:RAW_STRING}' is likely to cause desyncs and/or crashes
STR_NEWGRF_BROKEN_POWERED_WAGON :{WHITE}It changed powered-wagon state for '{PUSH_COLOUR}{1:ENGINE}{POP_COLOUR}' when not inside a depot STR_NEWGRF_BROKEN_POWERED_WAGON :{WHITE}It changed powered-wagon state for '{1:ENGINE}' when not inside a depot
STR_NEWGRF_BROKEN_VEHICLE_LENGTH :{WHITE}It changed vehicle length for '{PUSH_COLOUR}{1:ENGINE}{POP_COLOUR}' when not inside a depot STR_NEWGRF_BROKEN_VEHICLE_LENGTH :{WHITE}It changed vehicle length for '{1:ENGINE}' when not inside a depot
STR_NEWGRF_BROKEN_CAPACITY :{WHITE}It changed vehicle capacity for '{PUSH_COLOUR}{1:ENGINE}{POP_COLOUR}' when not inside a depot or refitting STR_NEWGRF_BROKEN_CAPACITY :{WHITE}It changed vehicle capacity for '{1:ENGINE}' when not inside a depot or refitting
STR_BROKEN_VEHICLE_LENGTH :{WHITE}Train '{VEHICLE}' belonging to '{COMPANY}' has invalid length. It is probably caused by problems with NewGRFs. Game may desync or crash STR_BROKEN_VEHICLE_LENGTH :{WHITE}Train '{VEHICLE}' belonging to '{COMPANY}' has invalid length. It is probably caused by problems with NewGRFs. Game may desync or crash
STR_NEWGRF_BUGGY :{WHITE}NewGRF '{PUSH_COLOUR}{0:RAW_STRING}{POP_COLOUR}' provides incorrect information STR_NEWGRF_BUGGY :{WHITE}NewGRF '{0:RAW_STRING}' provides incorrect information
STR_NEWGRF_BUGGY_ARTICULATED_CARGO :{WHITE}Cargo/refit information for '{PUSH_COLOUR}{1:ENGINE}{POP_COLOUR}' differs from purchase list after construction. This might cause autorenew/-replace to fail refitting correctly STR_NEWGRF_BUGGY_ARTICULATED_CARGO :{WHITE}Cargo/refit information for '{1:ENGINE}' differs from purchase list after construction. This might cause autorenew/-replace to fail refitting correctly
STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK :{WHITE}'{PUSH_COLOUR}{1:STRING}{POP_COLOUR}' caused an endless loop in the production callback STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK :{WHITE}'{1:STRING}' caused an endless loop in the production callback
STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT :{WHITE}Callback {1:HEX} returned unknown/invalid result {2:HEX} STR_NEWGRF_BUGGY_UNKNOWN_CALLBACK_RESULT :{WHITE}Callback {1:HEX} returned unknown/invalid result {2:HEX}
STR_NEWGRF_BUGGY_INVALID_CARGO_PRODUCTION_CALLBACK :{WHITE}'{PUSH_COLOUR}{1:STRING}{POP_COLOUR}' returned invalid cargo type in the production callback at {2:HEX} STR_NEWGRF_BUGGY_INVALID_CARGO_PRODUCTION_CALLBACK :{WHITE}'{1:STRING}' returned invalid cargo type in the production callback at {2:HEX}
# 'User removed essential NewGRFs'-placeholders for stuff without specs # 'User removed essential NewGRFs'-placeholders for stuff without specs
STR_NEWGRF_INVALID_CARGO :<invalid cargo> STR_NEWGRF_INVALID_CARGO :<invalid cargo>

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

@ -1112,6 +1112,9 @@ static void FormatString(StringBuilder &builder, std::string_view str_arg, Strin
std::stack<StrStackItem, std::vector<StrStackItem>> str_stack; std::stack<StrStackItem, std::vector<StrStackItem>> str_stack;
str_stack.emplace(str_arg, orig_first_param_offset, orig_case_index); str_stack.emplace(str_arg, orig_first_param_offset, orig_case_index);
bool appending = builder.AnyBytesWritten();
if (appending) builder.PutUtf8(SCC_PUSH_COLOUR);
for (;;) { for (;;) {
try { try {
while (!str_stack.empty() && !str_stack.top().consumer.AnyBytesLeft()) { while (!str_stack.empty() && !str_stack.top().consumer.AnyBytesLeft()) {
@ -1839,6 +1842,8 @@ static void FormatString(StringBuilder &builder, std::string_view str_arg, Strin
builder += "(invalid parameter)"; builder += "(invalid parameter)";
} }
} }
if (appending) builder.PutUtf8(SCC_POP_COLOUR);
} }