1
0
Fork 0

Compare commits

...

6 Commits

Author SHA1 Message Date
Peter Nelson a48c4c7372
Merge a43418a2cc into baced00e9f 2025-07-13 04:47:25 +00:00
translators baced00e9f Update: Translations from eints
spanish (mexican): 1 change by absay
english (us): 1 change by 2TallTyler
galician: 10 changes by pvillaverde
dutch: 1 change by Afoklala
portuguese: 10 changes by jcteotonio
2025-07-13 04:45:12 +00:00
Peter Nelson a43418a2cc
Codechange: Use unique_ptr for all pointers in script instance.
Removes manual memory management with new/delete.
2025-06-15 21:37:13 +01:00
Peter Nelson 6052249915
Codechange: Get/pass ScriptStorage by reference instead of pointer. 2025-06-15 21:37:12 +01:00
Peter Nelson a1b855669e
Codechange: Get/pass engine by reference instead of pointer. 2025-06-15 21:37:11 +01:00
Peter Nelson 49bc9d03cb
Codechange: Get/pass script controller by reference instead of pointer. 2025-06-15 21:37:11 +01:00
14 changed files with 134 additions and 115 deletions

View File

@ -43,7 +43,7 @@ void AIInstance::Initialize(AIInfo *info)
this->api_version = info->GetAPIVersion();
/* Register the AIController (including the "import" command) */
SQAIController_Register(*this->engine);
SQAIController_Register(this->GetEngine());
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), _current_company);
}
@ -53,7 +53,7 @@ void AIInstance::RegisterAPI()
ScriptInstance::RegisterAPI();
/* Register all classes */
SQAI_RegisterAll(*this->engine);
SQAI_RegisterAll(this->GetEngine());
if (!this->LoadCompatibilityScripts(AI_DIR, AIInfo::ApiVersions)) this->Died();
}
@ -83,8 +83,8 @@ void AIInstance::Died()
void AIInstance::LoadDummyScript()
{
ScriptAllocatorScope alloc_scope(this->engine);
Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
ScriptAllocatorScope alloc_scope(&this->GetEngine());
Script_CreateDummy(this->GetEngine().GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI");
}
int AIInstance::GetSetting(const std::string &name)

View File

@ -38,7 +38,7 @@ void GameInstance::Initialize(GameInfo *info)
this->api_version = info->GetAPIVersion();
/* Register the GameController */
SQGSController_Register(*this->engine);
SQGSController_Register(this->GetEngine());
ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
}
@ -48,11 +48,11 @@ void GameInstance::RegisterAPI()
ScriptInstance::RegisterAPI();
/* Register all classes */
SQGS_RegisterAll(*this->engine);
SQGS_RegisterAll(this->GetEngine());
if (!this->LoadCompatibilityScripts(GAME_DIR, GameInfo::ApiVersions)) this->Died();
if (this->IsAlive()) RegisterGameTranslation(*this->engine);
if (this->IsAlive()) RegisterGameTranslation(this->GetEngine());
}
int GameInstance::GetSetting(const std::string &name)

View File

@ -267,6 +267,7 @@ STR_UNITS_YEARS :{NUM}{NBSP}jaar
STR_UNITS_PERIODS :{NUM}{NBSP}period{P "" en}
STR_LIST_SEPARATOR :,{SPACE}
STR_TRUNCATION_ELLIPSIS :...
# Common window strings
STR_LIST_FILTER_TITLE :{BLACK}Filter:

View File

@ -267,6 +267,7 @@ STR_UNITS_YEARS :{NUM}{NBSP}year
STR_UNITS_PERIODS :{NUM}{NBSP}period{P "" s}
STR_LIST_SEPARATOR :,{SPACE}
STR_TRUNCATION_ELLIPSIS :...
# Common window strings
STR_LIST_FILTER_TITLE :{BLACK}Filter:

View File

@ -268,6 +268,7 @@ STR_UNITS_YEARS :{NUM}{NBSP}ano{
STR_UNITS_PERIODS :{NUM}{NBSP}período{P "" s}
STR_LIST_SEPARATOR :,{SPACE}
STR_TRUNCATION_ELLIPSIS :...
# Common window strings
STR_LIST_FILTER_TITLE :{BLACK}Filtrar:
@ -1302,6 +1303,9 @@ STR_CONFIG_SETTING_INTEREST_RATE_HELPTEXT :A taxa de inter
STR_CONFIG_SETTING_RUNNING_COSTS :Custos de explotación: {STRING}
STR_CONFIG_SETTING_RUNNING_COSTS_HELPTEXT :Establece o nivel de mantemento e custo de operación de vehículos e infraestrutura
###length 3
STR_CONFIG_SETTING_RUNNING_COSTS_LOW :Baixo
STR_CONFIG_SETTING_RUNNING_COSTS_MEDIUM :Medio
STR_CONFIG_SETTING_RUNNING_COSTS_HIGH :Alto
STR_CONFIG_SETTING_CONSTRUCTION_SPEED :Velocidade de construción: {STRING}
STR_CONFIG_SETTING_CONSTRUCTION_SPEED_HELPTEXT :Limita a cantidade de accións construtivas das IAs
@ -1324,6 +1328,9 @@ STR_CONFIG_SETTING_SUBSIDY_DURATION_DISABLED :Sen subvención
STR_CONFIG_SETTING_CONSTRUCTION_COSTS :Custos de construción: {STRING}
STR_CONFIG_SETTING_CONSTRUCTION_COSTS_HELPTEXT :Fixa o nivel de custos de construción e compra
###length 3
STR_CONFIG_SETTING_CONSTRUCTION_COSTS_LOW :Baixo
STR_CONFIG_SETTING_CONSTRUCTION_COSTS_MEDIUM :Medio
STR_CONFIG_SETTING_CONSTRUCTION_COSTS_HIGH :Alto
STR_CONFIG_SETTING_RECESSIONS :Recesións económicas: {STRING}
STR_CONFIG_SETTING_RECESSIONS_HELPTEXT :Se está activado, a economía pode entrar en recesión periódicamente. Durante unha recesión tódalas producións son significativamente máis baixas (volvendo ao nivel anterior cando a recesión remata)
@ -2079,9 +2086,9 @@ STR_CONFIG_SETTING_DISTRIBUTION_ARMOURED_HELPTEXT :A clase de merc
STR_CONFIG_SETTING_DISTRIBUTION_DEFAULT :Xeito de distribución para outros tipos de mercadoría: {STRING}
STR_CONFIG_SETTING_DISTRIBUTION_DEFAULT_HELPTEXT :"Asimétrico" significa que calquera cantidade de mercadorías pode ser enviada en calquera dirección. "Manual" significa que non haberá distribución automática para estas mercadorías
###length 3
STR_CONFIG_SETTING_DISTRIBUTION_MANUAL :manual
STR_CONFIG_SETTING_DISTRIBUTION_ASYMMETRIC :asimétrica
STR_CONFIG_SETTING_DISTRIBUTION_SYMMETRIC :simétrica
STR_CONFIG_SETTING_DISTRIBUTION_MANUAL :Manual
STR_CONFIG_SETTING_DISTRIBUTION_ASYMMETRIC :Asimétrica
STR_CONFIG_SETTING_DISTRIBUTION_SYMMETRIC :Simétrica
STR_CONFIG_SETTING_LINKGRAPH_ACCURACY :Precisión da distribución: {STRING}
STR_CONFIG_SETTING_LINKGRAPH_ACCURACY_HELPTEXT :Canto máis alto sexa o valor, máis tempo de CPU levará o cálculo de distribución. Se leva demasiado tempo podes experimentar retraso. Se sen embargo o fixas nun valor baixo, a distribución será imprecisa, e pode que a carga non sexa enviada aos destinos que ti queres

View File

@ -739,7 +739,7 @@ STR_PLAYLIST_TOOLTIP_CLICK_TO_ADD_TRACK :{BLACK}Clique n
STR_PLAYLIST_TOOLTIP_CLICK_TO_REMOVE_TRACK :{BLACK}Clique numa faixa para a remover da lista (apenas Personalização 1 ou Personalização 2)
# Highscore window
STR_HIGHSCORE_TOP_COMPANIES :{BIG_FONT}{BLACK}Melhores empresas
STR_HIGHSCORE_TOP_COMPANIES :{BIG_FONT}{BLACK}As melhores empresas
STR_HIGHSCORE_POSITION :{BIG_FONT}{BLACK}{COMMA}.
STR_HIGHSCORE_PERFORMANCE_TITLE_BUSINESSMAN :Negociante
STR_HIGHSCORE_PERFORMANCE_TITLE_ENTREPRENEUR :Empresário
@ -749,7 +749,7 @@ STR_HIGHSCORE_PERFORMANCE_TITLE_MAGNATE :Magnata
STR_HIGHSCORE_PERFORMANCE_TITLE_MOGUL :Grande magnata
STR_HIGHSCORE_PERFORMANCE_TITLE_TYCOON_OF_THE_CENTURY :Magnata do século
STR_HIGHSCORE_NAME :{PRESIDENT_NAME}, {COMPANY}
STR_HIGHSCORE_STATS :{BIG_FONT}'{STRING}' ({COMMA})
STR_HIGHSCORE_STATS :{BIG_FONT}"{STRING}" ({COMMA})
STR_HIGHSCORE_COMPANY_ACHIEVES_STATUS :{BIG_FONT}{BLACK}{COMPANY} conquista o estatuto de '{STRING}'!
STR_HIGHSCORE_PRESIDENT_OF_COMPANY_ACHIEVES_STATUS :{BIG_FONT}{WHITE}{PRESIDENT_NAME} de {COMPANY} conquista o estatuto de '{STRING}'!
@ -3117,8 +3117,8 @@ STR_INDUSTRY_CARGOES_SELECT_INDUSTRY_TOOLTIP :{BLACK}Selecion
# Land area window
STR_LAND_AREA_INFORMATION_CAPTION :{WHITE}Informações do Terreno
STR_LAND_AREA_INFORMATION_LOCATION_TOOLTIP :{BLACK}Centrar visualização na localização do mosaico. Ctrl+Clique para abrir uma nova janela de visualização nesse mosaico.
STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A :{BLACK}Custo para limpar: {LTBLUE}N/D
STR_LAND_AREA_INFORMATION_COST_TO_CLEAR :{BLACK}Custo para limpar: {RED}{CURRENCY_LONG}
STR_LAND_AREA_INFORMATION_COST_TO_CLEAR_N_A :{BLACK}Custo de desobstrução: {LTBLUE}N/D
STR_LAND_AREA_INFORMATION_COST_TO_CLEAR :{BLACK}Custo de desobstrução: {RED}{CURRENCY_LONG}
STR_LAND_AREA_INFORMATION_REVENUE_WHEN_CLEARED :{BLACK}Receitas apuradas: {LTBLUE}{CURRENCY_LONG}
STR_LAND_AREA_INFORMATION_OWNER_N_A :N/D
STR_LAND_AREA_INFORMATION_OWNER :{BLACK}Proprietário: {LTBLUE}{STRING}
@ -3126,7 +3126,7 @@ STR_LAND_AREA_INFORMATION_ROAD_OWNER :{BLACK}Dono da
STR_LAND_AREA_INFORMATION_TRAM_OWNER :{BLACK}Dono da linha de eléctrico: {LTBLUE}{STRING}
STR_LAND_AREA_INFORMATION_RAIL_OWNER :{BLACK}Dono da linha férrea: {LTBLUE}{STRING}
STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY :{BLACK}Autoridade local: {LTBLUE}{STRING}
STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE :Nenhum
STR_LAND_AREA_INFORMATION_LOCAL_AUTHORITY_NONE :Nenhuma
STR_LAND_AREA_INFORMATION_LANDINFO_COORDS :{BLACK}Coordenadas: {LTBLUE}{NUM} x {NUM} x {NUM}
STR_LAND_AREA_INFORMATION_LANDINFO_INDEX :{BLACK}Índice do mosaico: {LTBLUE}{NUM} ({HEX})
STR_LAND_AREA_INFORMATION_BUILD_DATE :{BLACK}Construído/renovado: {LTBLUE}{DATE_LONG}
@ -3685,9 +3685,9 @@ STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED_WINTER :{BLACK}No inver
STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED_GENERAL :{ORANGE}{STRING}{GREEN} fo{G 0 i i i ram ram} entregue{G 0 "" "" "" s s}
STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_REQUIRED :{ORANGE}{CARGO_TINY} / {CARGO_LONG}{RED} (ainda necessário)
STR_TOWN_VIEW_CARGO_FOR_TOWNGROWTH_DELIVERED :{ORANGE}{CARGO_TINY} / {CARGO_LONG}{GREEN} (entregue)
STR_TOWN_VIEW_TOWN_GROWS_EVERY :{BLACK}Localidade cresce a cada {ORANGE}{UNITS_DAYS_OR_SECONDS}
STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED :{BLACK}Localidade cresce a cada {ORANGE}{UNITS_DAYS_OR_SECONDS} (financiada)
STR_TOWN_VIEW_TOWN_GROW_STOPPED :{BLACK}Localidade {RED}não{BLACK} está a crescer
STR_TOWN_VIEW_TOWN_GROWS_EVERY :{BLACK}A localidade cresce a cada {ORANGE}{UNITS_DAYS_OR_SECONDS}
STR_TOWN_VIEW_TOWN_GROWS_EVERY_FUNDED :{BLACK}A localidade cresce a cada {ORANGE}{UNITS_DAYS_OR_SECONDS} (financiada)
STR_TOWN_VIEW_TOWN_GROW_STOPPED :{BLACK}A localidade {RED}não{BLACK} está a crescer
STR_TOWN_VIEW_NOISE_IN_TOWN :{BLACK}Limite de ruído na localidade: {ORANGE}{COMMA}{BLACK} máx: {ORANGE}{COMMA}
STR_TOWN_VIEW_CENTER_TOOLTIP :{BLACK}Centrar visualização na localização da localidade. Ctrl+Clique para abrir um novo visualizador na localização da localidade
STR_TOWN_VIEW_LOCAL_AUTHORITY_BUTTON :{BLACK}Autoridade Local
@ -3700,7 +3700,7 @@ STR_TOWN_VIEW_EXPAND_BUILDINGS_BUTTON :{BLACK}Expandir
STR_TOWN_VIEW_EXPAND_BUILDINGS_TOOLTIP :{BLACK}Aumentar os edifícios da localidade
STR_TOWN_VIEW_EXPAND_ROADS_BUTTON :{BLACK}Expandir estradas
STR_TOWN_VIEW_EXPAND_ROADS_TOOLTIP :{BLACK}Aumentar as estradas da localidade
STR_TOWN_VIEW_DELETE_BUTTON :{BLACK}Apagar
STR_TOWN_VIEW_DELETE_BUTTON :{BLACK}Remover
STR_TOWN_VIEW_DELETE_TOOLTIP :{BLACK}Remover completamente esta localidade
STR_TOWN_VIEW_RENAME_TOWN_BUTTON :Renomear Localidade
@ -3943,7 +3943,7 @@ STR_FINANCES_INFRASTRUCTURE_BUTTON :{BLACK}Infraest
STR_COMPANY_VIEW_CAPTION :{WHITE}{COMPANY} {BLACK}{COMPANY_NUM}
STR_COMPANY_VIEW_PRESIDENT_MANAGER_TITLE :{WHITE}{PRESIDENT_NAME}{}{GOLD}(Presidente)
STR_COMPANY_VIEW_INAUGURATED_TITLE :{GOLD}Inaugurado: {WHITE}{NUM}
STR_COMPANY_VIEW_INAUGURATED_TITLE :{GOLD}Inaugurada em: {WHITE}{NUM}
STR_COMPANY_VIEW_INAUGURATED_TITLE_WALLCLOCK :{GOLD}Inaugurada: {WHITE}{NUM} (período {NUM})
STR_COMPANY_VIEW_COLOUR_SCHEME_TITLE :{GOLD}Cores:
STR_COMPANY_VIEW_VEHICLES_TITLE :{GOLD}Veículos:

View File

@ -268,6 +268,7 @@ STR_UNITS_YEARS :{NUM}{NBSP}año
STR_UNITS_PERIODS :{NUM}{NBSP}período{P "" s}
STR_LIST_SEPARATOR :,{SPACE}
STR_TRUNCATION_ELLIPSIS :...
# Common window strings
STR_LIST_FILTER_TITLE :{BLACK}Filtrar:

View File

@ -76,7 +76,7 @@ ScriptController::ScriptController(::CompanyID company) :
/* static */ uint ScriptController::GetTick()
{
return ScriptObject::GetActiveInstance().GetController()->ticks;
return ScriptObject::GetActiveInstance().GetController().ticks;
}
/* static */ int ScriptController::GetOpsTillSuspend()
@ -96,9 +96,9 @@ ScriptController::ScriptController(::CompanyID company) :
/* static */ HSQOBJECT ScriptController::Import(const std::string &library, const std::string &class_name, int version)
{
ScriptController *controller = ScriptObject::GetActiveInstance().GetController();
Squirrel *engine = ScriptObject::GetActiveInstance().engine;
HSQUIRRELVM vm = engine->GetVM();
ScriptController &controller = ScriptObject::GetActiveInstance().GetController();
Squirrel &engine = *ScriptObject::GetActiveInstance().engine;
HSQUIRRELVM vm = engine.GetVM();
ScriptInfo *lib = ScriptObject::GetActiveInstance().FindLibrary(library, version);
if (lib == nullptr) {
@ -114,11 +114,11 @@ ScriptController::ScriptController(::CompanyID company) :
std::string fake_class;
LoadedLibraryList::iterator it = controller->loaded_library.find(library_name);
if (it != controller->loaded_library.end()) {
LoadedLibraryList::iterator it = controller.loaded_library.find(library_name);
if (it != controller.loaded_library.end()) {
fake_class = (*it).second;
} else {
int next_number = ++controller->loaded_library_count;
int next_number = ++controller.loaded_library_count;
/* Create a new fake internal name */
fake_class = fmt::format("_internalNA{}", next_number);
@ -128,14 +128,14 @@ ScriptController::ScriptController(::CompanyID company) :
sq_pushstring(vm, fake_class);
sq_newclass(vm, SQFalse);
/* Load the library */
if (!engine->LoadScript(vm, lib->GetMainScript(), false)) {
if (!engine.LoadScript(vm, lib->GetMainScript(), false)) {
throw sq_throwerror(vm, fmt::format("there was a compile error when importing '{}' version {}", library, version));
}
/* Create the fake class */
sq_newslot(vm, -3, SQFalse);
sq_pop(vm, 1);
controller->loaded_library[library_name] = fake_class;
controller.loaded_library[library_name] = fake_class;
}
/* Find the real class inside the fake class (like 'sets.Vector') */

View File

@ -45,7 +45,7 @@ void SimpleCountedObject::Release()
* Get the storage associated with the current ScriptInstance.
* @return The storage.
*/
static ScriptStorage *GetStorage()
static ScriptStorage &GetStorage()
{
return ScriptObject::GetActiveInstance().GetStorage();
}
@ -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;
@ -65,7 +65,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
}
ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
: AutoRestoreBackup(GetStorage()->allow_do_command, false)
: AutoRestoreBackup(GetStorage().allow_do_command, false)
{}
/* static */ ScriptInstance &ScriptObject::GetActiveInstance()
@ -78,181 +78,181 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
/* static */ void ScriptObject::SetDoCommandDelay(uint ticks)
{
assert(ticks > 0);
GetStorage()->delay = ticks;
GetStorage().delay = ticks;
}
/* static */ uint ScriptObject::GetDoCommandDelay()
{
return GetStorage()->delay;
return GetStorage().delay;
}
/* static */ void ScriptObject::SetDoCommandMode(ScriptModeProc *proc, ScriptObject *instance)
{
GetStorage()->mode = proc;
GetStorage()->mode_instance = instance;
GetStorage().mode = proc;
GetStorage().mode_instance = instance;
}
/* static */ ScriptModeProc *ScriptObject::GetDoCommandMode()
{
return GetStorage()->mode;
return GetStorage().mode;
}
/* static */ ScriptObject *ScriptObject::GetDoCommandModeInstance()
{
return GetStorage()->mode_instance;
return GetStorage().mode_instance;
}
/* static */ void ScriptObject::SetDoCommandAsyncMode(ScriptAsyncModeProc *proc, ScriptObject *instance)
{
GetStorage()->async_mode = proc;
GetStorage()->async_mode_instance = instance;
GetStorage().async_mode = proc;
GetStorage().async_mode_instance = instance;
}
/* static */ ScriptAsyncModeProc *ScriptObject::GetDoCommandAsyncMode()
{
return GetStorage()->async_mode;
return GetStorage().async_mode;
}
/* static */ ScriptObject *ScriptObject::GetDoCommandAsyncModeInstance()
{
return GetStorage()->async_mode_instance;
return GetStorage().async_mode_instance;
}
/* static */ void ScriptObject::SetLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "SetLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
s->last_data = data;
s->last_cmd = cmd;
ScriptStorage &s = GetStorage();
Debug(script, 6, "SetLastCommand company={:02d} cmd={} data={}", s.root_company, cmd, FormatArrayAsHex(data));
s.last_data = data;
s.last_cmd = cmd;
}
/* static */ bool ScriptObject::CheckLastCommand(const CommandDataBuffer &data, Commands cmd)
{
ScriptStorage *s = GetStorage();
Debug(script, 6, "CheckLastCommand company={:02d} cmd={} data={}", s->root_company, cmd, FormatArrayAsHex(data));
if (s->last_cmd != cmd) return false;
if (s->last_data != data) return false;
ScriptStorage &s = GetStorage();
Debug(script, 6, "CheckLastCommand company={:02d} cmd={} data={}", s.root_company, cmd, FormatArrayAsHex(data));
if (s.last_cmd != cmd) return false;
if (s.last_data != data) return false;
return true;
}
/* static */ void ScriptObject::SetDoCommandCosts(Money value)
{
GetStorage()->costs = CommandCost(INVALID_EXPENSES, value); // Expense type is never read.
GetStorage().costs = CommandCost(INVALID_EXPENSES, value); // Expense type is never read.
}
/* static */ void ScriptObject::IncreaseDoCommandCosts(Money value)
{
GetStorage()->costs.AddCost(value);
GetStorage().costs.AddCost(value);
}
/* static */ Money ScriptObject::GetDoCommandCosts()
{
return GetStorage()->costs.GetCost();
return GetStorage().costs.GetCost();
}
/* static */ void ScriptObject::SetLastError(ScriptErrorType last_error)
{
GetStorage()->last_error = last_error;
GetStorage().last_error = last_error;
}
/* static */ ScriptErrorType ScriptObject::GetLastError()
{
return GetStorage()->last_error;
return GetStorage().last_error;
}
/* static */ void ScriptObject::SetLastCost(Money last_cost)
{
GetStorage()->last_cost = last_cost;
GetStorage().last_cost = last_cost;
}
/* static */ Money ScriptObject::GetLastCost()
{
return GetStorage()->last_cost;
return GetStorage().last_cost;
}
/* static */ void ScriptObject::SetRoadType(RoadType road_type)
{
GetStorage()->road_type = road_type;
GetStorage().road_type = road_type;
}
/* static */ RoadType ScriptObject::GetRoadType()
{
return GetStorage()->road_type;
return GetStorage().road_type;
}
/* static */ void ScriptObject::SetRailType(RailType rail_type)
{
GetStorage()->rail_type = rail_type;
GetStorage().rail_type = rail_type;
}
/* static */ RailType ScriptObject::GetRailType()
{
return GetStorage()->rail_type;
return GetStorage().rail_type;
}
/* static */ void ScriptObject::SetLastCommandRes(bool res)
{
GetStorage()->last_command_res = res;
GetStorage().last_command_res = res;
}
/* static */ bool ScriptObject::GetLastCommandRes()
{
return GetStorage()->last_command_res;
return GetStorage().last_command_res;
}
/* static */ void ScriptObject::SetLastCommandResData(CommandDataBuffer data)
{
GetStorage()->last_cmd_ret = std::move(data);
GetStorage().last_cmd_ret = std::move(data);
}
/* static */ const CommandDataBuffer &ScriptObject::GetLastCommandResData()
{
return GetStorage()->last_cmd_ret;
return GetStorage().last_cmd_ret;
}
/* static */ void ScriptObject::SetCompany(::CompanyID company)
{
if (GetStorage()->root_company == INVALID_OWNER) GetStorage()->root_company = company;
GetStorage()->company = company;
if (GetStorage().root_company == INVALID_OWNER) GetStorage().root_company = company;
GetStorage().company = company;
_current_company = company;
}
/* static */ ::CompanyID ScriptObject::GetCompany()
{
return GetStorage()->company;
return GetStorage().company;
}
/* static */ ::CompanyID ScriptObject::GetRootCompany()
{
return GetStorage()->root_company;
return GetStorage().root_company;
}
/* 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()
{
return GetStorage()->event_queue;
return GetStorage().event_queue;
}
/* static */ ScriptLogTypes::LogData &ScriptObject::GetLogData()
{
return GetStorage()->log_data;
return GetStorage().log_data;
}
/* static */ void ScriptObject::SetCallbackVariable(int index, int value)
{
if (static_cast<size_t>(index) >= GetStorage()->callback_value.size()) GetStorage()->callback_value.resize(index + 1);
GetStorage()->callback_value[index] = value;
if (static_cast<size_t>(index) >= GetStorage().callback_value.size()) GetStorage().callback_value.resize(index + 1);
GetStorage().callback_value[index] = value;
}
/* static */ int ScriptObject::GetCallbackVariable(int index)
{
return GetStorage()->callback_value[index];
return GetStorage().callback_value[index];
}
/* static */ CommandCallbackData *ScriptObject::GetDoCommandCallback()
@ -310,8 +310,8 @@ ScriptObject::DisableDoCommandScope::DisableDoCommandScope()
IncreaseDoCommandCosts(res.GetCost());
if (!_generating_world) {
/* Charge a nominal fee for asynchronously executed commands */
Squirrel *engine = ScriptObject::GetActiveInstance().engine;
Squirrel::DecreaseOps(engine->GetVM(), 100);
Squirrel &engine = *ScriptObject::GetActiveInstance().engine;
Squirrel::DecreaseOps(engine.GetVM(), 100);
}
if (callback != nullptr) {
/* Insert return value into to stack and throw a control code that

View File

@ -47,9 +47,8 @@ static void PrintFunc(bool error_msg, std::string_view message)
}
ScriptInstance::ScriptInstance(std::string_view api_name)
: storage(std::make_unique<ScriptStorage>()), engine(std::make_unique<Squirrel>(api_name))
{
this->storage = new ScriptStorage();
this->engine = new Squirrel(api_name);
this->engine->SetPrintFunction(&PrintFunc);
}
@ -57,10 +56,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. */
@ -79,12 +78,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;
}
@ -98,7 +96,7 @@ void ScriptInstance::Initialize(const std::string &main_script, const std::strin
void ScriptInstance::RegisterAPI()
{
squirrel_register_std(this->engine);
squirrel_register_std(*this->engine);
}
bool ScriptInstance::LoadCompatibilityScript(std::string_view api_version, Subdirectory dir)
@ -144,11 +142,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 as object destruction requires the instance to exist. */
this->engine.reset();
}
void ScriptInstance::Continue()
@ -165,11 +162,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()
@ -316,9 +311,10 @@ void ScriptInstance::CollectGarbage()
}
ScriptStorage *ScriptInstance::GetStorage()
ScriptStorage &ScriptInstance::GetStorage()
{
return this->storage;
assert(this->storage != nullptr);
return *this->storage;
}
ScriptLogTypes::LogData &ScriptInstance::GetLogData()

View File

@ -91,7 +91,7 @@ public:
/**
* Get the storage of this script.
*/
class ScriptStorage *GetStorage();
class ScriptStorage &GetStorage();
/**
* Get the log pointer of this script.
@ -146,7 +146,11 @@ public:
/**
* Get the controller attached to the instance.
*/
class ScriptController *GetController() { return controller; }
class ScriptController &GetController()
{
assert(this->controller != nullptr);
return *this->controller;
}
/**
* Return the "this script died" value
@ -252,7 +256,6 @@ public:
void ReleaseSQObject(HSQOBJECT *obj);
protected:
class Squirrel *engine = nullptr; ///< A wrapper around the squirrel vm.
std::string api_version{}; ///< Current API used by this script.
/**
@ -283,10 +286,20 @@ protected:
*/
virtual void LoadDummyScript() = 0;
/**
* Get the storage of this script.
*/
inline class Squirrel &GetEngine()
{
assert(this->engine != nullptr);
return *this->engine;
}
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 Squirrel> engine; ///< A wrapper around the squirrel vm.
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.

View File

@ -536,7 +536,7 @@ void Squirrel::Initialize()
sq_setforeignptr(this->vm, this);
sq_pushroottable(this->vm);
squirrel_register_global_std(this);
squirrel_register_global_std(*this);
/* Set consts table as delegate of root table, so consts/enums defined via require() are accessible */
sq_pushconsttable(this->vm);

View File

@ -82,20 +82,20 @@ SQInteger SquirrelStd::notifyallexceptions(HSQUIRRELVM vm)
return SQ_ERROR;
}
void squirrel_register_global_std(Squirrel *engine)
void squirrel_register_global_std(Squirrel &engine)
{
/* We don't use squirrel_helper here, as we want to register to the global
* scope and not to a class. */
engine->AddMethod("require", &SquirrelStd::require, ".s");
engine->AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, ".b");
engine.AddMethod("require", &SquirrelStd::require, ".s");
engine.AddMethod("notifyallexceptions", &SquirrelStd::notifyallexceptions, ".b");
}
void squirrel_register_std(Squirrel *engine)
void squirrel_register_std(Squirrel &engine)
{
/* We don't use squirrel_helper here, as we want to register to the global
* scope and not to a class. */
engine->AddMethod("min", &SquirrelStd::min, ".ii");
engine->AddMethod("max", &SquirrelStd::max, ".ii");
engine.AddMethod("min", &SquirrelStd::min, ".ii");
engine.AddMethod("max", &SquirrelStd::max, ".ii");
sqstd_register_mathlib(engine->GetVM());
sqstd_register_mathlib(engine.GetVM());
}

View File

@ -52,12 +52,12 @@ public:
/**
* Register all standard functions we want to give to a script.
*/
void squirrel_register_std(Squirrel *engine);
void squirrel_register_std(Squirrel &engine);
/**
* Register all standard functions that are available on first startup.
* @note this set is very limited, and is only meant to load other scripts and things like that.
*/
void squirrel_register_global_std(Squirrel *engine);
void squirrel_register_global_std(Squirrel &engine);
#endif /* SQUIRREL_STD_HPP */