1
0
Fork 0

(svn r23366) -Codechange: move most of the Dummy code to script/, unifying it

release/1.2
truebrain 2011-11-29 23:26:52 +00:00
parent 4505edbd47
commit 3e6d026129
12 changed files with 61 additions and 55 deletions

View File

@ -793,6 +793,7 @@
<ClInclude Include="..\src\script\script_fatalerror.hpp" /> <ClInclude Include="..\src\script\script_fatalerror.hpp" />
<ClCompile Include="..\src\script\script_info.cpp" /> <ClCompile Include="..\src\script\script_info.cpp" />
<ClInclude Include="..\src\script\script_info.hpp" /> <ClInclude Include="..\src\script\script_info.hpp" />
<ClCompile Include="..\src\script\script_info_dummy.cpp" />
<ClCompile Include="..\src\script\script_instance.cpp" /> <ClCompile Include="..\src\script\script_instance.cpp" />
<ClInclude Include="..\src\script\script_instance.hpp" /> <ClInclude Include="..\src\script\script_instance.hpp" />
<ClCompile Include="..\src\script\script_scanner.cpp" /> <ClCompile Include="..\src\script\script_scanner.cpp" />
@ -853,7 +854,6 @@
<ClInclude Include="..\src\ai\ai_gui.hpp" /> <ClInclude Include="..\src\ai\ai_gui.hpp" />
<ClCompile Include="..\src\ai\ai_info.cpp" /> <ClCompile Include="..\src\ai\ai_info.cpp" />
<ClInclude Include="..\src\ai\ai_info.hpp" /> <ClInclude Include="..\src\ai\ai_info.hpp" />
<ClCompile Include="..\src\ai\ai_info_dummy.cpp" />
<ClCompile Include="..\src\ai\ai_instance.cpp" /> <ClCompile Include="..\src\ai\ai_instance.cpp" />
<ClInclude Include="..\src\ai\ai_instance.hpp" /> <ClInclude Include="..\src\ai\ai_instance.hpp" />
<ClCompile Include="..\src\ai\ai_scanner.cpp" /> <ClCompile Include="..\src\ai\ai_scanner.cpp" />

View File

@ -1602,6 +1602,9 @@
<ClInclude Include="..\src\script\script_info.hpp"> <ClInclude Include="..\src\script\script_info.hpp">
<Filter>Script</Filter> <Filter>Script</Filter>
</ClInclude> </ClInclude>
<ClCompile Include="..\src\script\script_info_dummy.cpp">
<Filter>Script</Filter>
</ClCompile>
<ClCompile Include="..\src\script\script_instance.cpp"> <ClCompile Include="..\src\script\script_instance.cpp">
<Filter>Script</Filter> <Filter>Script</Filter>
</ClCompile> </ClCompile>
@ -1782,9 +1785,6 @@
<ClInclude Include="..\src\ai\ai_info.hpp"> <ClInclude Include="..\src\ai\ai_info.hpp">
<Filter>AI Core</Filter> <Filter>AI Core</Filter>
</ClInclude> </ClInclude>
<ClCompile Include="..\src\ai\ai_info_dummy.cpp">
<Filter>AI Core</Filter>
</ClCompile>
<ClCompile Include="..\src\ai\ai_instance.cpp"> <ClCompile Include="..\src\ai\ai_instance.cpp">
<Filter>AI Core</Filter> <Filter>AI Core</Filter>
</ClCompile> </ClCompile>

View File

@ -2478,6 +2478,10 @@
RelativePath=".\..\src\script\script_info.hpp" RelativePath=".\..\src\script\script_info.hpp"
> >
</File> </File>
<File
RelativePath=".\..\src\script\script_info_dummy.cpp"
>
</File>
<File <File
RelativePath=".\..\src\script\script_instance.cpp" RelativePath=".\..\src\script\script_instance.cpp"
> >
@ -2730,10 +2734,6 @@
RelativePath=".\..\src\ai\ai_info.hpp" RelativePath=".\..\src\ai\ai_info.hpp"
> >
</File> </File>
<File
RelativePath=".\..\src\ai\ai_info_dummy.cpp"
>
</File>
<File <File
RelativePath=".\..\src\ai\ai_instance.cpp" RelativePath=".\..\src\ai\ai_instance.cpp"
> >

View File

@ -2475,6 +2475,10 @@
RelativePath=".\..\src\script\script_info.hpp" RelativePath=".\..\src\script\script_info.hpp"
> >
</File> </File>
<File
RelativePath=".\..\src\script\script_info_dummy.cpp"
>
</File>
<File <File
RelativePath=".\..\src\script\script_instance.cpp" RelativePath=".\..\src\script\script_instance.cpp"
> >
@ -2727,10 +2731,6 @@
RelativePath=".\..\src\ai\ai_info.hpp" RelativePath=".\..\src\ai\ai_info.hpp"
> >
</File> </File>
<File
RelativePath=".\..\src\ai\ai_info_dummy.cpp"
>
</File>
<File <File
RelativePath=".\..\src\ai\ai_instance.cpp" RelativePath=".\..\src\ai\ai_instance.cpp"
> >

View File

@ -558,6 +558,7 @@ script/script_config.hpp
script/script_fatalerror.hpp script/script_fatalerror.hpp
script/script_info.cpp script/script_info.cpp
script/script_info.hpp script/script_info.hpp
script/script_info_dummy.cpp
script/script_instance.cpp script/script_instance.cpp
script/script_instance.hpp script/script_instance.hpp
script/script_scanner.cpp script/script_scanner.cpp
@ -624,7 +625,6 @@ ai/ai_gui.cpp
ai/ai_gui.hpp ai/ai_gui.hpp
ai/ai_info.cpp ai/ai_info.cpp
ai/ai_info.hpp ai/ai_info.hpp
ai/ai_info_dummy.cpp
ai/ai_instance.cpp ai/ai_instance.cpp
ai/ai_instance.hpp ai/ai_instance.hpp
ai/ai_scanner.cpp ai/ai_scanner.cpp

View File

@ -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. * DoCommand callback function for all commands executed by AIs.
* @param result The result of the command. * @param result The result of the command.

View File

@ -32,6 +32,7 @@ private:
/* virtual */ void RegisterAPI(); /* virtual */ void RegisterAPI();
/* virtual */ void Died(); /* virtual */ void Died();
/* virtual */ CommandCallback *GetDoCommandCallback(); /* virtual */ CommandCallback *GetDoCommandCallback();
/* virtual */ void LoadDummyScript();
/** /**
* Load squirrel scripts to emulate an older API. * Load squirrel scripts to emulate an older API.

View File

@ -34,8 +34,8 @@ void AIScannerInfo::Initialize(const char *name)
/* Create the dummy AI */ /* Create the dummy AI */
free(this->main_script); free(this->main_script);
this->main_script = strdup("%_dummy"); this->main_script = strdup("%_dummy");
extern void AI_CreateAIInfoDummy(HSQUIRRELVM vm); extern void Script_CreateDummyInfo(HSQUIRRELVM vm, const char *type, const char *dir);
AI_CreateAIInfoDummy(this->engine->GetVM()); Script_CreateDummyInfo(this->engine->GetVM(), "AI", "ai");
} }
void AIScannerInfo::SetDummyAI(class AIInfo *info) void AIScannerInfo::SetDummyAI(class AIInfo *info)

View File

@ -7,7 +7,7 @@
* 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 <http://www.gnu.org/licenses/>. * 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 <http://www.gnu.org/licenses/>.
*/ */
/** @file ai_info_dummy.cpp Implementation of a dummy AI. */ /** @file script_info_dummy.cpp Implementation of a dummy Script. */
#include <squirrel.h> #include <squirrel.h>
#include "../stdafx.h" #include "../stdafx.h"
@ -16,37 +16,36 @@
#include "../strings_func.h" #include "../strings_func.h"
#include "table/strings.h" #include "table/strings.h"
/* The reason this exists in C++, is that a user can trash his ai/ dir, /* The reason this exists in C++, is that a user can trash his ai/ or game/ dir,
* leaving no AIs available. The complexity to solve this is insane, and * leaving no Scripts available. The complexity to solve this is insane, and
* therefor the alternative is used, and make sure there is always an AI * 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 * 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 * is simply no 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 be noted that this Script is complete invisible for the user, and impossible
* to select manual. It is a fail-over in case no AIs are available. * to select manual. It is a fail-over in case no Scripts 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. */ /** Run the dummy info.nut. */
void AI_CreateAIInfoDummy(HSQUIRRELVM vm) 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); sq_pushroottable(vm);
/* Load and run the script */ /* Load and run the script */
if (SQ_SUCCEEDED(sq_compilebuffer(vm, _dummy_script_info, scstrlen(_dummy_script_info), _SC("dummy"), SQTrue))) { if (SQ_SUCCEEDED(sq_compilebuffer(vm, sq_dummy_script, scstrlen(sq_dummy_script), _SC("dummy"), SQTrue))) {
sq_push(vm, -2); sq_push(vm, -2);
if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) { if (SQ_SUCCEEDED(sq_call(vm, 1, SQFalse, SQTrue))) {
sq_pop(vm, 1); sq_pop(vm, 1);
@ -57,14 +56,14 @@ void AI_CreateAIInfoDummy(HSQUIRRELVM vm)
} }
/** Run the dummy AI and let it generate an error message. */ /** Run the dummy AI and let it generate an error message. */
void AI_CreateAIDummy(HSQUIRRELVM vm) void Script_CreateDummy(HSQUIRRELVM vm, StringID string, const char *type)
{ {
/* We want to translate the error message. /* We want to translate the error message.
* We do this in three steps: * We do this in three steps:
* 1) We get the error message * 1) We get the error message
*/ */
char error_message[1024]; char error_message[1024];
GetString(error_message, STR_ERROR_AI_NO_AI_FOUND, lastof(error_message)); GetString(error_message, string, lastof(error_message));
/* Make escapes for all quotes and slashes. */ /* Make escapes for all quotes and slashes. */
char safe_error_message[1024]; char safe_error_message[1024];
@ -78,7 +77,7 @@ void AI_CreateAIDummy(HSQUIRRELVM vm)
/* 2) We construct the AI's code. This is done by merging a header, body and footer */ /* 2) We construct the AI's code. This is done by merging a header, body and footer */
char dummy_script[4096]; char dummy_script[4096];
char *dp = dummy_script; char *dp = dummy_script;
dp = strecpy(dp, "class DummyAI extends AIController {\n function Start()\n {\n", lastof(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 /* As special trick we need to split the error message on newlines and
* emit each newline as a separate error printing string. */ * emit each newline as a separate error printing string. */
@ -88,7 +87,7 @@ void AI_CreateAIDummy(HSQUIRRELVM vm)
newline = strchr(p, '\n'); newline = strchr(p, '\n');
if (newline != NULL) *newline = '\0'; if (newline != NULL) *newline = '\0';
dp += seprintf(dp, lastof(dummy_script), " AILog.Error(\"%s\");\n", p); dp += seprintf(dp, lastof(dummy_script), " %sLog.Error(\"%s\");\n", type, p);
p = newline + 1; p = newline + 1;
} while (newline != NULL); } while (newline != NULL);

View File

@ -78,8 +78,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na
ScriptObject::SetAllowDoCommand(false); ScriptObject::SetAllowDoCommand(false);
/* Load and execute the script for this script */ /* Load and execute the script for this script */
if (strcmp(main_script, "%_dummy") == 0) { if (strcmp(main_script, "%_dummy") == 0) {
extern void AI_CreateAIDummy(HSQUIRRELVM vm); this->LoadDummyScript();
AI_CreateAIDummy(this->engine->GetVM());
} else if (!this->engine->LoadScript(main_script) || this->engine->IsSuspended()) { } 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."); if (this->engine->IsSuspended()) ScriptLog::Error("This script took too long to load script. AI is not started.");
this->Died(); this->Died();
@ -103,6 +102,7 @@ void ScriptInstance::Initialize(const char *main_script, const char *instance_na
void ScriptInstance::RegisterAPI() void ScriptInstance::RegisterAPI()
{ {
extern void squirrel_register_std(Squirrel *engine);
squirrel_register_std(this->engine); squirrel_register_std(this->engine);
} }

View File

@ -162,6 +162,11 @@ protected:
*/ */
virtual CommandCallback *GetDoCommandCallback() = 0; virtual CommandCallback *GetDoCommandCallback() = 0;
/**
* Load the dummy script.
*/
virtual void LoadDummyScript() = 0;
private: private:
class ScriptController *controller; ///< The script main class. class ScriptController *controller; ///< The script main class.
class ScriptStorage *storage; ///< Some global information for each running script. class ScriptStorage *storage; ///< Some global information for each running script.

View File

@ -35,11 +35,6 @@ private:
*/ */
static SQInteger _RunError(HSQUIRRELVM vm); static SQInteger _RunError(HSQUIRRELVM vm);
/**
* Get the squirrel VM. Try to avoid using this.
*/
HSQUIRRELVM GetVM() { return this->vm; }
/** /**
* Get the API name. * Get the API name.
*/ */
@ -67,14 +62,14 @@ protected:
static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...); static void ErrorPrintFunc(HSQUIRRELVM vm, const SQChar *s, ...);
public: public:
friend class AIScannerInfo;
friend class ScriptInstance;
friend class ScriptController;
friend void squirrel_register_std(Squirrel *engine);
Squirrel(const char *APIName); Squirrel(const char *APIName);
~Squirrel(); ~Squirrel();
/**
* Get the squirrel VM. Try to avoid using this.
*/
HSQUIRRELVM GetVM() { return this->vm; }
/** /**
* Load a script. * Load a script.
* @param script The full script-name to load. * @param script The full script-name to load.