From 3e6d0261291a1c91bf4f57c06fd9d180c175befc Mon Sep 17 00:00:00 2001 From: truebrain Date: Tue, 29 Nov 2011 23:26:52 +0000 Subject: (svn r23366) -Codechange: move most of the Dummy code to script/, unifying it --- projects/openttd_vs100.vcxproj | 2 +- projects/openttd_vs100.vcxproj.filters | 6 +- projects/openttd_vs80.vcproj | 8 +-- projects/openttd_vs90.vcproj | 8 +-- source.list | 2 +- src/ai/ai_info_dummy.cpp | 112 --------------------------------- src/ai/ai_instance.cpp | 6 ++ src/ai/ai_instance.hpp | 1 + src/ai/ai_scanner.cpp | 4 +- src/script/script_info_dummy.cpp | 111 ++++++++++++++++++++++++++++++++ src/script/script_instance.cpp | 4 +- src/script/script_instance.hpp | 5 ++ src/script/squirrel.hpp | 15 ++--- 13 files changed, 145 insertions(+), 139 deletions(-) delete mode 100644 src/ai/ai_info_dummy.cpp create mode 100644 src/script/script_info_dummy.cpp diff --git a/projects/openttd_vs100.vcxproj b/projects/openttd_vs100.vcxproj index 9b727ce3f..602a52cc3 100644 --- a/projects/openttd_vs100.vcxproj +++ b/projects/openttd_vs100.vcxproj @@ -793,6 +793,7 @@ + @@ -853,7 +854,6 @@ - diff --git a/projects/openttd_vs100.vcxproj.filters b/projects/openttd_vs100.vcxproj.filters index ee7f53783..af9f32424 100644 --- a/projects/openttd_vs100.vcxproj.filters +++ b/projects/openttd_vs100.vcxproj.filters @@ -1602,6 +1602,9 @@ Script + + Script + Script @@ -1782,9 +1785,6 @@ AI Core - - AI Core - AI Core diff --git a/projects/openttd_vs80.vcproj b/projects/openttd_vs80.vcproj index 3bb30382d..448454617 100644 --- a/projects/openttd_vs80.vcproj +++ b/projects/openttd_vs80.vcproj @@ -2478,6 +2478,10 @@ RelativePath=".\..\src\script\script_info.hpp" > + + @@ -2730,10 +2734,6 @@ RelativePath=".\..\src\ai\ai_info.hpp" > - - diff --git a/projects/openttd_vs90.vcproj b/projects/openttd_vs90.vcproj index 62be33640..eb997fb37 100644 --- a/projects/openttd_vs90.vcproj +++ b/projects/openttd_vs90.vcproj @@ -2475,6 +2475,10 @@ RelativePath=".\..\src\script\script_info.hpp" > + + @@ -2727,10 +2731,6 @@ RelativePath=".\..\src\ai\ai_info.hpp" > - - diff --git a/source.list b/source.list index 1f15b3ec7..70e87feed 100644 --- a/source.list +++ b/source.list @@ -558,6 +558,7 @@ script/script_config.hpp script/script_fatalerror.hpp script/script_info.cpp script/script_info.hpp +script/script_info_dummy.cpp script/script_instance.cpp script/script_instance.hpp script/script_scanner.cpp @@ -624,7 +625,6 @@ ai/ai_gui.cpp ai/ai_gui.hpp ai/ai_info.cpp ai/ai_info.hpp -ai/ai_info_dummy.cpp ai/ai_instance.cpp ai/ai_instance.hpp ai/ai_scanner.cpp diff --git a/src/ai/ai_info_dummy.cpp b/src/ai/ai_info_dummy.cpp deleted file mode 100644 index f706110f7..000000000 --- a/src/ai/ai_info_dummy.cpp +++ /dev/null @@ -1,112 +0,0 @@ -/* $Id$ */ - -/* - * This file is part of OpenTTD. - * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. - * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. - * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . - */ - -/** @file ai_info_dummy.cpp Implementation of a dummy AI. */ - -#include -#include "../stdafx.h" - -#include "../string_func.h" -#include "../strings_func.h" -#include "table/strings.h" - -/* The reason this exists in C++, is that a user can trash his ai/ dir, - * leaving no AIs available. The complexity to solve this is insane, and - * therefor the alternative is used, and make sure there is always an AI - * available, no matter what the situation is. By defining it in C++, there - * is simply now way a user can delete it, and therefor safe to use. It has - * to be noted that this AI is complete invisible for the user, and impossible - * to select manual. It is a fail-over in case no AIs are available. - */ - -/** info.nut for the dummy AI. */ -const SQChar _dummy_script_info[] = _SC(" \n\ -class DummyAI extends AIInfo { \n\ - function GetAuthor() { return \"OpenTTD NoAI Developers Team\"; } \n\ - function GetName() { return \"DummyAI\"; } \n\ - function GetShortName() { return \"DUMM\"; } \n\ - function GetDescription() { return \"A Dummy AI that is loaded when your ai/ dir is empty\"; }\n\ - function GetVersion() { return 1; } \n\ - function GetDate() { return \"2008-07-26\"; } \n\ - function CreateInstance() { return \"DummyAI\"; } \n\ -} \n\ - \n\ -RegisterDummyAI(DummyAI()); \n\ -"); - -/** Run the dummy info.nut. */ -void AI_CreateAIInfoDummy(HSQUIRRELVM vm) -{ - sq_pushroottable(vm); - - /* Load and run the script */ - if (SQ_SUCCEEDED(sq_compilebuffer(vm, _dummy_script_info, scstrlen(_dummy_script_info), _SC("dummy"), SQTrue))) { - sq_push(vm, -2); - if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { - sq_pop(vm, 1); - return; - } - } - NOT_REACHED(); -} - -/** Run the dummy AI and let it generate an error message. */ -void AI_CreateAIDummy(HSQUIRRELVM vm) -{ - /* We want to translate the error message. - * We do this in three steps: - * 1) We get the error message - */ - char error_message[1024]; - GetString(error_message, STR_ERROR_AI_NO_AI_FOUND, lastof(error_message)); - - /* Make escapes for all quotes and slashes. */ - char safe_error_message[1024]; - char *q = safe_error_message; - for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) { - if (*p == '"' || *p == '\\') *q++ = '\\'; - *q = *p; - } - *q = '\0'; - - /* 2) We construct the AI's code. This is done by merging a header, body and footer */ - char dummy_script[4096]; - char *dp = dummy_script; - dp = strecpy(dp, "class DummyAI extends AIController {\n function Start()\n {\n", lastof(dummy_script)); - - /* As special trick we need to split the error message on newlines and - * emit each newline as a separate error printing string. */ - char *newline; - char *p = safe_error_message; - do { - newline = strchr(p, '\n'); - if (newline != NULL) *newline = '\0'; - - dp += seprintf(dp, lastof(dummy_script), " AILog.Error(\"%s\");\n", p); - p = newline + 1; - } while (newline != NULL); - - dp = strecpy(dp, " }\n}\n", lastof(dummy_script)); - - /* 3) We translate the error message in the character format that Squirrel wants. - * We can use the fact that the wchar string printing also uses %s to print - * old style char strings, which is what was generated during the script generation. */ - const SQChar *sq_dummy_script = OTTD2SQ(dummy_script); - - /* And finally we load and run the script */ - sq_pushroottable(vm); - if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) { - sq_push(vm, -2); - if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { - sq_pop(vm, 1); - return; - } - } - NOT_REACHED(); -} diff --git a/src/ai/ai_instance.cpp b/src/ai/ai_instance.cpp index 11459f649..2df8c2e44 100644 --- a/src/ai/ai_instance.cpp +++ b/src/ai/ai_instance.cpp @@ -229,6 +229,12 @@ void AIInstance::Died() } } +void AIInstance::LoadDummyScript() +{ + extern void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type); + Script_CreateDummy(this->engine->GetVM(), STR_ERROR_AI_NO_AI_FOUND, "AI"); +} + /** * DoCommand callback function for all commands executed by AIs. * @param result The result of the command. diff --git a/src/ai/ai_instance.hpp b/src/ai/ai_instance.hpp index f0b66997e..886b716ae 100644 --- a/src/ai/ai_instance.hpp +++ b/src/ai/ai_instance.hpp @@ -32,6 +32,7 @@ private: /* virtual */ void RegisterAPI(); /* virtual */ void Died(); /* virtual */ CommandCallback *GetDoCommandCallback(); + /* virtual */ void LoadDummyScript(); /** * Load squirrel scripts to emulate an older API. diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp index 3a296f00e..25d38ccdb 100644 --- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -34,8 +34,8 @@ void AIScannerInfo::Initialize(const char *name) /* Create the dummy AI */ free(this->main_script); this->main_script = strdup("%_dummy"); - extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm); - AI_CreateAIInfoDummy(this->engine->GetVM()); + extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir); + Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai"); } void AIScannerInfo::SetDummyAI(class AIInfo *info) diff --git a/src/script/script_info_dummy.cpp b/src/script/script_info_dummy.cpp new file mode 100644 index 000000000..4620ce8f7 --- /dev/null +++ b/src/script/script_info_dummy.cpp @@ -0,0 +1,111 @@ +/* $Id$ */ + +/* + * This file is part of OpenTTD. + * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2. + * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see . + */ + +/** @file script_info_dummy.cpp Implementation of a dummy Script. */ + +#include +#include "../stdafx.h" + +#include "../string_func.h" +#include "../strings_func.h" +#include "table/strings.h" + +/* The reason this exists in C++, is that a user can trash his ai/ or game/ dir, + * leaving no Scripts available. The complexity to solve this is insane, and + * therefor the alternative is used, and make sure there is always a Script + * available, no matter what the situation is. By defining it in C++, there + * is simply no way a user can delete it, and therefor safe to use. It has + * to be noted that this Script is complete invisible for the user, and impossible + * to select manual. It is a fail-over in case no Scripts are available. + */ + +/** Run the dummy info.nut. */ +void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir) +{ + char dummy_script[4096]; + char *dp = dummy_script; + dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sInfo {\n", type, type); + dp += seprintf(dp, lastof(dummy_script), "function GetAuthor() { return \"OpenTTD Developers Team\"; }\n"); + dp += seprintf(dp, lastof(dummy_script), "function GetName() { return \"Dummy%s\"; }\n", type); + dp += seprintf(dp, lastof(dummy_script), "function GetShortName() { return \"DUMM\"; }\n"); + dp += seprintf(dp, lastof(dummy_script), "function GetDescription() { return \"A Dummy %s that is loaded when your %s/ dir is empty\"; }\n", type, dir); + dp += seprintf(dp, lastof(dummy_script), "function GetVersion() { return 1; }\n"); + dp += seprintf(dp, lastof(dummy_script), "function GetDate() { return \"2008-07-26\"; }\n"); + dp += seprintf(dp, lastof(dummy_script), "function CreateInstance() { return \"Dummy%s\"; }\n", type); + dp += seprintf(dp, lastof(dummy_script), "} RegisterDummy%s(Dummy%s());\n", type, type); + + const SQChar *sq_dummy_script = OTTD2SQ(dummy_script); + + sq_pushroottable(vm); + + /* Load and run the script */ + if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) { + sq_push(vm, -2); + if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { + sq_pop(vm, 1); + return; + } + } + NOT_REACHED(); +} + +/** Run the dummy AI and let it generate an error message. */ +void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type) +{ + /* We want to translate the error message. + * We do this in three steps: + * 1) We get the error message + */ + char error_message[1024]; + GetString(error_message, string, lastof(error_message)); + + /* Make escapes for all quotes and slashes. */ + char safe_error_message[1024]; + char *q = safe_error_message; + for (const char *p = error_message; *p != '\0' && q < lastof(safe_error_message) - 2; p++, q++) { + if (*p == '"' || *p == '\\') *q++ = '\\'; + *q = *p; + } + *q = '\0'; + + /* 2) We construct the AI's code. This is done by merging a header, body and footer */ + char dummy_script[4096]; + char *dp = dummy_script; + dp += seprintf(dp, lastof(dummy_script), "class Dummy%s extends %sController {\n function Start()\n {\n", type, type); + + /* As special trick we need to split the error message on newlines and + * emit each newline as a separate error printing string. */ + char *newline; + char *p = safe_error_message; + do { + newline = strchr(p, '\n'); + if (newline != NULL) *newline = '\0'; + + dp += seprintf(dp, lastof(dummy_script), " %sLog.Error(\"%s\");\n", type, p); + p = newline + 1; + } while (newline != NULL); + + dp = strecpy(dp, " }\n}\n", lastof(dummy_script)); + + /* 3) We translate the error message in the character format that Squirrel wants. + * We can use the fact that the wchar string printing also uses %s to print + * old style char strings, which is what was generated during the script generation. */ + const SQChar *sq_dummy_script = OTTD2SQ(dummy_script); + + /* And finally we load and run the script */ + sq_pushroottable(vm); + if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) { + sq_push(vm, -2); + if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { + sq_pop(vm, 1); + return; + } + } + NOT_REACHED(); +} diff --git a/src/script/script_instance.cpp b/src/script/script_instance.cpp index beebe2fd7..97fdfd68f 100644 --- a/src/script/script_instance.cpp +++ b/src/script/script_instance.cpp @@ -78,8 +78,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na ScriptObject::SetAllowDoCommand(false); /* Load and execute the script for this script */ if (strcmp(main_script, "%_dummy") == 0) { - extern void AI_CreateAIDummy(HSQUIRRELVM vm); - AI_CreateAIDummy(this->engine->GetVM()); + this->LoadDummyScript(); } else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) { if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to load script. AI is not started."); this->Died(); @@ -103,6 +102,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na void ScriptInstance::RegisterAPI() { + extern void squirrel_register_std(Squirrel *engine); squirrel_register_std(this->engine); } diff --git a/src/script/script_instance.hpp b/src/script/script_instance.hpp index c5ff730c7..b84a36496 100644 --- a/src/script/script_instance.hpp +++ b/src/script/script_instance.hpp @@ -162,6 +162,11 @@ protected: */ virtual CommandCallback *GetDoCommandCallback() = 0; + /** + * Load the dummy script. + */ + virtual void LoadDummyScript() = 0; + private: class ScriptController *controller; ///< The script main class. class ScriptStorage *storage; ///< Some global information for each running script. diff --git a/src/script/squirrel.hpp b/src/script/squirrel.hpp index 6b9c3ab70..dbccd3c08 100644 --- a/src/script/squirrel.hpp +++ b/src/script/squirrel.hpp @@ -35,11 +35,6 @@ private: */ static SQInteger _RunError(HSQUIRRELVM vm); - /** - * Get the squirrel VM. Try to avoid using this. - */ - HSQUIRRELVM GetVM() { return this->vm; } - /** * Get the API name. */ @@ -67,14 +62,14 @@ protected: static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); public: - friend class AIScannerInfo; - friend class ScriptInstance; - friend class ScriptController; - friend void squirrel_register_std(Squirrel *engine); - Squirrel(const char *APIName); ~Squirrel(); + /** + * Get the squirrel VM. Try to avoid using this. + */ + HSQUIRRELVM GetVM() { return this->vm; } + /** * Load a script. * @param script The full script-name to load. -- cgit v1.2.3-54-g00ecf