1
0
Fork 0

(svn r23358) -Codechange: move AI_FatalError to Script_FatalError (and to its own file)

release/1.2
truebrain 2011-11-29 23:21:13 +00:00
parent 99cb93ef6f
commit bbd9facb44
12 changed files with 65 additions and 32 deletions

View File

@ -788,6 +788,7 @@
<ClInclude Include="..\src\table\water_land.h" /> <ClInclude Include="..\src\table\water_land.h" />
<ClCompile Include="..\src\3rdparty\md5\md5.cpp" /> <ClCompile Include="..\src\3rdparty\md5\md5.cpp" />
<ClInclude Include="..\src\3rdparty\md5\md5.h" /> <ClInclude Include="..\src\3rdparty\md5\md5.h" />
<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_scanner.cpp" /> <ClCompile Include="..\src\script\script_scanner.cpp" />

View File

@ -1587,6 +1587,9 @@
<ClInclude Include="..\src\3rdparty\md5\md5.h"> <ClInclude Include="..\src\3rdparty\md5\md5.h">
<Filter>MD5</Filter> <Filter>MD5</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="..\src\script\script_fatalerror.hpp">
<Filter>Script</Filter>
</ClInclude>
<ClCompile Include="..\src\script\script_info.cpp"> <ClCompile Include="..\src\script\script_info.cpp">
<Filter>Script</Filter> <Filter>Script</Filter>
</ClCompile> </ClCompile>

View File

@ -2458,6 +2458,10 @@
<Filter <Filter
Name="Script" Name="Script"
> >
<File
RelativePath=".\..\src\script\script_fatalerror.hpp"
>
</File>
<File <File
RelativePath=".\..\src\script\script_info.cpp" RelativePath=".\..\src\script\script_info.cpp"
> >

View File

@ -2455,6 +2455,10 @@
<Filter <Filter
Name="Script" Name="Script"
> >
<File
RelativePath=".\..\src\script\script_fatalerror.hpp"
>
</File>
<File <File
RelativePath=".\..\src\script\script_info.cpp" RelativePath=".\..\src\script\script_info.cpp"
> >

View File

@ -553,6 +553,7 @@ table/water_land.h
#if AI #if AI
# Script # Script
script/script_fatalerror.hpp
script/script_info.cpp script/script_info.cpp
script/script_info.hpp script/script_info.hpp
script/script_scanner.cpp script/script_scanner.cpp

View File

@ -18,6 +18,7 @@
#include "ai_config.hpp" #include "ai_config.hpp"
#include "ai_gui.hpp" #include "ai_gui.hpp"
#include "../script/script_fatalerror.hpp"
#include "../script/script_storage.hpp" #include "../script/script_storage.hpp"
#include "ai_instance.hpp" #include "ai_instance.hpp"
@ -154,7 +155,7 @@ void AIInstance::Initialize(AIInfo *info)
return; return;
} }
ScriptObject::SetAllowDoCommand(true); ScriptObject::SetAllowDoCommand(true);
} catch (AI_FatalError e) { } catch (Script_FatalError e) {
this->is_dead = true; this->is_dead = true;
this->engine->ThrowError(e.GetErrorMessage()); this->engine->ThrowError(e.GetErrorMessage());
this->engine->ResumeError(); this->engine->ResumeError();
@ -375,7 +376,7 @@ void AIInstance::GameLoop()
} catch (AI_VMSuspend e) { } catch (AI_VMSuspend e) {
this->suspend = e.GetSuspendTime(); this->suspend = e.GetSuspendTime();
this->callback = e.GetSuspendCallback(); this->callback = e.GetSuspendCallback();
} catch (AI_FatalError e) { } catch (Script_FatalError e) {
this->is_dead = true; this->is_dead = true;
this->engine->ThrowError(e.GetErrorMessage()); this->engine->ThrowError(e.GetErrorMessage());
this->engine->ResumeError(); this->engine->ResumeError();
@ -396,7 +397,7 @@ void AIInstance::GameLoop()
} catch (AI_VMSuspend e) { } catch (AI_VMSuspend e) {
this->suspend = e.GetSuspendTime(); this->suspend = e.GetSuspendTime();
this->callback = e.GetSuspendCallback(); this->callback = e.GetSuspendCallback();
} catch (AI_FatalError e) { } catch (Script_FatalError e) {
this->is_dead = true; this->is_dead = true;
this->engine->ThrowError(e.GetErrorMessage()); this->engine->ThrowError(e.GetErrorMessage());
this->engine->ResumeError(); this->engine->ResumeError();
@ -640,9 +641,9 @@ void AIInstance::Save()
this->engine->CrashOccurred(); this->engine->CrashOccurred();
return; return;
} }
} catch (AI_FatalError e) { } catch (Script_FatalError e) {
/* If we don't mark the AI as dead here cleaning up the squirrel /* If we don't mark the AI as dead here cleaning up the squirrel
* stack could throw AI_FatalError again. */ * stack could throw Script_FatalError again. */
this->is_dead = true; this->is_dead = true;
this->engine->ThrowError(e.GetErrorMessage()); this->engine->ThrowError(e.GetErrorMessage());
this->engine->ResumeError(); this->engine->ResumeError();

View File

@ -51,29 +51,6 @@ private:
AISuspendCallbackProc *callback; ///< Callback function to call when the AI can run again. AISuspendCallbackProc *callback; ///< Callback function to call when the AI can run again.
}; };
/**
* A throw-class that is given when the AI made a fatal error.
*/
class AI_FatalError {
public:
/**
* Creates a "fatal error" exception.
* @param msg The message describing the cause of the fatal error.
*/
AI_FatalError(const char *msg) :
msg(msg)
{}
/**
* The error message associated with the fatal error.
* @return The error message.
*/
const char *GetErrorMessage() { return msg; }
private:
const char *msg; ///< The error message.
};
/** Runtime information about an AI like a pointer to the squirrel vm and the current state. */ /** Runtime information about an AI like a pointer to the squirrel vm and the current state. */
class AIInstance { class AIInstance {
public: public:

View File

@ -20,6 +20,7 @@
#include "../../ai/ai_instance.hpp" #include "../../ai/ai_instance.hpp"
#include "../../ai/ai_config.hpp" #include "../../ai/ai_config.hpp"
#include "../../ai/ai.hpp" #include "../../ai/ai.hpp"
#include "../script_fatalerror.hpp"
#include "script_log.hpp" #include "script_log.hpp"
/* static */ void ScriptController::SetCommandDelay(int ticks) /* static */ void ScriptController::SetCommandDelay(int ticks)
@ -31,7 +32,7 @@
/* static */ void ScriptController::Sleep(int ticks) /* static */ void ScriptController::Sleep(int ticks)
{ {
if (!ScriptObject::CanSuspend()) { if (!ScriptObject::CanSuspend()) {
throw AI_FatalError("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator."); throw Script_FatalError("You are not allowed to call Sleep in your constructor, Save(), Load(), and any valuator.");
} }
if (ticks <= 0) { if (ticks <= 0) {

View File

@ -14,6 +14,7 @@
#include "../../company_base.h" #include "../../company_base.h"
#include "../../company_func.h" #include "../../company_func.h"
#include "../../ai/ai_instance.hpp" #include "../../ai/ai_instance.hpp"
#include "../script_fatalerror.hpp"
bool ScriptExecMode::ModeProc() bool ScriptExecMode::ModeProc()
{ {
@ -34,7 +35,7 @@ ScriptExecMode::~ScriptExecMode()
if (this->GetDoCommandModeInstance() != this) { if (this->GetDoCommandModeInstance() != this) {
/* Ignore this error if the AI already died. */ /* Ignore this error if the AI already died. */
if (!ScriptObject::GetActiveInstance()->IsDead()) { if (!ScriptObject::GetActiveInstance()->IsDead()) {
throw AI_FatalError("ScriptExecMode object was removed while it was not the latest AI*Mode object created."); throw Script_FatalError("ScriptExecMode object was removed while it was not the latest AI*Mode object created.");
} }
} }
this->SetDoCommandMode(this->last_mode, this->last_instance); this->SetDoCommandMode(this->last_mode, this->last_instance);

View File

@ -17,6 +17,7 @@
#include "../script_storage.hpp" #include "../script_storage.hpp"
#include "../../ai/ai_instance.hpp" #include "../../ai/ai_instance.hpp"
#include "../script_fatalerror.hpp"
#include "script_error.hpp" #include "script_error.hpp"
/** /**
@ -226,7 +227,7 @@ ScriptObject::ActiveInstance::~ActiveInstance()
/* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback) /* static */ bool ScriptObject::DoCommand(TileIndex tile, uint32 p1, uint32 p2, uint cmd, const char *text, AISuspendCallbackProc *callback)
{ {
if (!ScriptObject::CanSuspend()) { if (!ScriptObject::CanSuspend()) {
throw AI_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator."); throw Script_FatalError("You are not allowed to execute any DoCommand (even indirect) in your constructor, Save(), Load(), and any valuator.");
} }
/* Set the default callback to return a true/false result of the DoCommand */ /* Set the default callback to return a true/false result of the DoCommand */

View File

@ -14,6 +14,7 @@
#include "../../company_base.h" #include "../../company_base.h"
#include "../../company_func.h" #include "../../company_func.h"
#include "../../ai/ai_instance.hpp" #include "../../ai/ai_instance.hpp"
#include "../script_fatalerror.hpp"
bool ScriptTestMode::ModeProc() bool ScriptTestMode::ModeProc()
{ {
@ -34,7 +35,7 @@ ScriptTestMode::~ScriptTestMode()
if (this->GetDoCommandModeInstance() != this) { if (this->GetDoCommandModeInstance() != this) {
/* Ignore this error if the AI already died. */ /* Ignore this error if the AI already died. */
if (!ScriptObject::GetActiveInstance()->IsDead()) { if (!ScriptObject::GetActiveInstance()->IsDead()) {
throw AI_FatalError("AITestmode object was removed while it was not the latest AI*Mode object created."); throw Script_FatalError("AITestmode object was removed while it was not the latest AI*Mode object created.");
} }
} }
this->SetDoCommandMode(this->last_mode, this->last_instance); this->SetDoCommandMode(this->last_mode, this->last_instance);

View File

@ -0,0 +1,38 @@
/* $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 <http://www.gnu.org/licenses/>.
*/
/** @file script_fatalerror.hpp The definition of Script_FatalError. */
#ifndef SCRIPT_FATALERROR_HPP
#define SCRIPT_FATALERROR_HPP
/**
* A throw-class that is given when the script made a fatal error.
*/
class Script_FatalError {
public:
/**
* Creates a "fatal error" exception.
* @param msg The message describing the cause of the fatal error.
*/
Script_FatalError(const char *msg) :
msg(msg)
{}
/**
* The error message associated with the fatal error.
* @return The error message.
*/
const char *GetErrorMessage() { return msg; }
private:
const char *msg; ///< The error message.
};
#endif /* SCRIPT_FATALERROR_HPP */