diff options
author | yexo <yexo@openttd.org> | 2009-02-13 01:44:56 +0000 |
---|---|---|
committer | yexo <yexo@openttd.org> | 2009-02-13 01:44:56 +0000 |
commit | 312e57df5fb73f1415840bc4e53aa1d997b6b4c3 (patch) | |
tree | 4ac59ee567e71a4ae0857930ec2f1912b0ffd4ca /src | |
parent | 8eecd774ffb983b75e3be2001e82be117a8b019b (diff) | |
download | openttd-312e57df5fb73f1415840bc4e53aa1d997b6b4c3.tar.xz |
(svn r15464) -Codechange [NoAI]: Call all info.nut functions exactly once and only during initialization.
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/ai_info.cpp | 40 | ||||
-rw-r--r-- | src/ai/ai_info.hpp | 12 | ||||
-rw-r--r-- | src/ai/ai_scanner.cpp | 2 |
3 files changed, 32 insertions, 22 deletions
diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index fd58f8b45..9cb73ef04 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -48,31 +48,27 @@ AILibrary::~AILibrary() const char *AIFileInfo::GetAuthor() { - if (this->author == NULL) this->author = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetAuthor"); return this->author; } const char *AIFileInfo::GetName() { - if (this->name == NULL) this->name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetName"); return this->name; } const char *AIFileInfo::GetShortName() { - if (this->short_name == NULL) this->short_name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetShortName"); return this->short_name; } const char *AIFileInfo::GetDescription() { - if (this->description == NULL) this->description = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDescription"); return this->description; } int AIFileInfo::GetVersion() { - return this->engine->CallIntegerMethod(*this->SQ_instance, "GetVersion"); + return this->version; } void AIFileInfo::GetSettings() @@ -82,24 +78,14 @@ void AIFileInfo::GetSettings() const char *AIFileInfo::GetDate() { - if (this->date == NULL) this->date = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetDate"); return this->date; } const char *AIFileInfo::GetInstanceName() { - if (this->instance_name == NULL) this->instance_name = this->engine->CallStringMethodStrdup(*this->SQ_instance, "CreateInstance"); return this->instance_name; } -bool AIFileInfo::CanLoadFromVersion(int version) -{ - if (version == -1) return true; - if (!this->engine->MethodExists(*this->SQ_instance, "MinVersionToLoad")) return (version == this->GetVersion()); - - return version >= this->engine->CallIntegerMethod(*this->SQ_instance, "MinVersionToLoad") && version <= this->GetVersion(); -} - const char *AIFileInfo::GetMainScript() { return this->main_script; @@ -144,6 +130,15 @@ bool AIFileInfo::CheckMethod(const char *name) info->main_script = strdup(info->base->GetMainScript()); + /* Cache the data the info file gives us. */ + info->author = info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetAuthor"); + info->name = info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetName"); + info->short_name = info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetShortName"); + info->description = info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDescription"); + info->date = info->engine->CallStringMethodStrdup(*info->SQ_instance, "GetDate"); + info->version = info->engine->CallIntegerMethod(*info->SQ_instance, "GetVersion"); + info->instance_name = info->engine->CallStringMethodStrdup(*info->SQ_instance, "CreateInstance"); + return 0; } @@ -166,6 +161,11 @@ bool AIFileInfo::CheckMethod(const char *name) if (info->engine->MethodExists(*info->SQ_instance, "GetSettings")) { info->GetSettings(); } + if (info->engine->MethodExists(*info->SQ_instance, "MinVersionToLoad")) { + info->min_loadable_version = info->engine->CallIntegerMethod(*info->SQ_instance, "MinVersionToLoad"); + } else { + info->min_loadable_version = info->GetVersion(); + } /* Remove the link to the real instance, else it might get deleted by RegisterAI() */ sq_setinstanceup(vm, 2, NULL); @@ -207,6 +207,12 @@ AIInfo::~AIInfo() this->config_list.clear(); } +bool AIInfo::CanLoadFromVersion(int version) +{ + if (version == -1) return true; + return version >= this->min_loadable_version && version <= this->GetVersion(); +} + SQInteger AIInfo::AddSetting(HSQUIRRELVM vm) { AIConfigItem config; @@ -401,6 +407,9 @@ int AIInfo::GetSettingDefaultValue(const char *name) return res; } + /* Cache the category */ + library->category = library->engine->CallStringMethodStrdup(*library->SQ_instance, "GetCategory"); + /* Register the Library to the base system */ library->base->RegisterLibrary(library); @@ -409,7 +418,6 @@ int AIInfo::GetSettingDefaultValue(const char *name) const char *AILibrary::GetCategory() { - if (this->category == NULL) this->category = this->engine->CallStringMethodStrdup(*this->SQ_instance, "GetCategory"); return this->category; } diff --git a/src/ai/ai_info.hpp b/src/ai/ai_info.hpp index b7ca5ad45..03442a824 100644 --- a/src/ai/ai_info.hpp +++ b/src/ai/ai_info.hpp @@ -85,11 +85,6 @@ public: const char *GetInstanceName(); /** - * Check if we can start this AI. - */ - bool CanLoadFromVersion(int version); - - /** * Get the filename of the main.nut script. */ const char *GetMainScript(); @@ -115,6 +110,7 @@ private: const char *description; const char *date; const char *instance_name; + int version; }; class AIInfo : public AIFileInfo { @@ -140,6 +136,11 @@ public: const AIConfigItem *GetConfigItem(const char *name); /** + * Check if we can start this AI. + */ + bool CanLoadFromVersion(int version); + + /** * Set a setting. */ SQInteger AddSetting(HSQUIRRELVM vm); @@ -156,6 +157,7 @@ public: private: AIConfigItemList config_list; + int min_loadable_version; }; class AILibrary : public AIFileInfo { diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp index 734058774..db5acd90c 100644 --- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -404,7 +404,7 @@ AIInfo *AIScanner::FindInfo(const char *nameParam, int versionParam) snprintf(ai_name_compare, sizeof(ai_name_compare), "%s", (*it).second->GetInstanceName()); strtolower(ai_name_compare); - if (strcasecmp(ai_name, ai_name_compare) == 0 && (*it).second->CanLoadFromVersion(versionParam) && (*it).second->GetVersion() > version) { + if (strcasecmp(ai_name, ai_name_compare) == 0 && (*it).second->CanLoadFromVersion(versionParam)) { version = (*it).second->GetVersion(); info = (*it).second; } |