mirror of https://github.com/OpenTTD/OpenTTD
Compare commits
3 Commits
43118e8e25
...
14de9078b4
Author | SHA1 | Date |
---|---|---|
|
14de9078b4 | |
|
7bb4940ebd | |
|
3c56c0fe2e |
|
@ -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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -325,7 +325,7 @@ struct GSConfigWindow : public Window {
|
||||||
}
|
}
|
||||||
} else if (!bool_item && !config_item.complete_labels) {
|
} else if (!bool_item && !config_item.complete_labels) {
|
||||||
/* Display a query box so users can enter a custom value. */
|
/* Display a query box so users can enter a custom value. */
|
||||||
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {});
|
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault);
|
||||||
}
|
}
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
break;
|
break;
|
||||||
|
@ -347,10 +347,17 @@ struct GSConfigWindow : public Window {
|
||||||
|
|
||||||
void OnQueryTextFinished(std::optional<std::string> str) override
|
void OnQueryTextFinished(std::optional<std::string> str) override
|
||||||
{
|
{
|
||||||
|
/* The user pressed cancel */
|
||||||
if (!str.has_value()) return;
|
if (!str.has_value()) return;
|
||||||
auto value = ParseInteger<int32_t>(*str, 10, true);
|
|
||||||
if (!value.has_value()) return;
|
if (!str->empty()) {
|
||||||
this->SetValue(*value);
|
auto value = ParseInteger<int32_t>(*str, 10, true);
|
||||||
|
if (!value.has_value()) return;
|
||||||
|
|
||||||
|
this->SetValue(*value);
|
||||||
|
} else {
|
||||||
|
this->SetDefaultValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDropdownSelect(WidgetID widget, int index, int) override
|
void OnDropdownSelect(WidgetID widget, int index, int) override
|
||||||
|
@ -419,6 +426,14 @@ private:
|
||||||
this->gs_config->SetSetting(config_item.name, value);
|
this->gs_config->SetSetting(config_item.name, value);
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetDefaultValue()
|
||||||
|
{
|
||||||
|
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
|
||||||
|
if (_game_mode == GM_NORMAL && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
|
||||||
|
this->gs_config->ResetSetting(config_item.name);
|
||||||
|
this->SetDirty();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Open the GS config window. */
|
/** Open the GS config window. */
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -106,6 +106,16 @@ void ScriptConfig::ResetSettings()
|
||||||
this->settings.clear();
|
this->settings.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ScriptConfig::ResetSetting(std::string_view name)
|
||||||
|
{
|
||||||
|
if (this->info == nullptr) return;
|
||||||
|
|
||||||
|
const ScriptConfigItem *config_item = this->info->GetConfigItem(name);
|
||||||
|
if (config_item == nullptr) return;
|
||||||
|
|
||||||
|
this->settings.erase(std::string{name});
|
||||||
|
}
|
||||||
|
|
||||||
void ScriptConfig::ResetEditableSettings(bool yet_to_start)
|
void ScriptConfig::ResetEditableSettings(bool yet_to_start)
|
||||||
{
|
{
|
||||||
if (this->info == nullptr) return ResetSettings();
|
if (this->info == nullptr) return ResetSettings();
|
||||||
|
|
|
@ -129,6 +129,11 @@ public:
|
||||||
*/
|
*/
|
||||||
void ResetSettings();
|
void ResetSettings();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reset a setting to its default value.
|
||||||
|
*/
|
||||||
|
void ResetSetting(std::string_view name);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reset only editable and visible settings to their default value.
|
* Reset only editable and visible settings to their default value.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -470,7 +470,7 @@ struct ScriptSettingsWindow : public Window {
|
||||||
}
|
}
|
||||||
} else if (!bool_item && !config_item.complete_labels) {
|
} else if (!bool_item && !config_item.complete_labels) {
|
||||||
/* Display a query box so users can enter a custom value. */
|
/* Display a query box so users can enter a custom value. */
|
||||||
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, {});
|
ShowQueryString(GetString(STR_JUST_INT, old_val), STR_CONFIG_SETTING_QUERY_CAPTION, INT32_DIGITS_WITH_SIGN_AND_TERMINATION, this, CS_NUMERAL_SIGNED, QueryStringFlag::EnableDefault);
|
||||||
}
|
}
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
break;
|
break;
|
||||||
|
@ -485,10 +485,17 @@ struct ScriptSettingsWindow : public Window {
|
||||||
|
|
||||||
void OnQueryTextFinished(std::optional<std::string> str) override
|
void OnQueryTextFinished(std::optional<std::string> str) override
|
||||||
{
|
{
|
||||||
|
/* The user pressed cancel */
|
||||||
if (!str.has_value()) return;
|
if (!str.has_value()) return;
|
||||||
auto value = ParseInteger<int32_t>(*str, 10, true);
|
|
||||||
if (!value.has_value()) return;
|
if (!str->empty()) {
|
||||||
this->SetValue(*value);
|
auto value = ParseInteger<int32_t>(*str, 10, true);
|
||||||
|
if (!value.has_value()) return;
|
||||||
|
|
||||||
|
this->SetValue(*value);
|
||||||
|
} else {
|
||||||
|
this->SetDefaultValue();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnDropdownSelect(WidgetID widget, int index, int) override
|
void OnDropdownSelect(WidgetID widget, int index, int) override
|
||||||
|
@ -552,6 +559,14 @@ private:
|
||||||
this->script_config->SetSetting(config_item.name, value);
|
this->script_config->SetSetting(config_item.name, value);
|
||||||
this->SetDirty();
|
this->SetDirty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SetDefaultValue()
|
||||||
|
{
|
||||||
|
const ScriptConfigItem &config_item = *this->visible_settings[this->clicked_row];
|
||||||
|
if (_game_mode == GM_NORMAL && ((this->slot == OWNER_DEITY) || Company::IsValidID(this->slot)) && !config_item.flags.Test(ScriptConfigFlag::InGame)) return;
|
||||||
|
this->script_config->ResetSetting(config_item.name);
|
||||||
|
this->SetDirty();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Widgets for the Script settings window. */
|
/** Widgets for the Script settings window. */
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue