mirror of https://github.com/OpenTTD/OpenTTD
(svn r24468) -Add [FS#5219]: API compatibility scripts for Goal Scripts (Hirundo)
parent
5192155253
commit
54aa43c81b
|
@ -81,7 +81,6 @@
|
|||
|
||||
#include "../company_base.h"
|
||||
#include "../company_func.h"
|
||||
#include "../fileio_func.h"
|
||||
|
||||
AIInstance::AIInstance() :
|
||||
ScriptInstance("AI")
|
||||
|
@ -194,29 +193,7 @@ void AIInstance::RegisterAPI()
|
|||
SQAIWaypointList_Register(this->engine);
|
||||
SQAIWaypointList_Vehicle_Register(this->engine);
|
||||
|
||||
if (!this->LoadCompatibilityScripts(this->versionAPI)) this->Died();
|
||||
}
|
||||
|
||||
bool AIInstance::LoadCompatibilityScripts(const char *api_version)
|
||||
{
|
||||
char script_name[32];
|
||||
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
||||
char buf[MAX_PATH];
|
||||
Searchpath sp;
|
||||
FOR_ALL_SEARCHPATHS(sp) {
|
||||
FioAppendDirectory(buf, MAX_PATH, sp, AI_DIR);
|
||||
ttd_strlcat(buf, script_name, MAX_PATH);
|
||||
if (!FileExists(buf)) continue;
|
||||
|
||||
if (this->engine->LoadScript(buf)) return true;
|
||||
|
||||
ScriptLog::Error("Failed to load API compatibility script");
|
||||
DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
ScriptLog::Warning("API compatibility script not found");
|
||||
return true;
|
||||
if (!this->LoadCompatibilityScripts(this->versionAPI, AI_DIR)) this->Died();
|
||||
}
|
||||
|
||||
void AIInstance::Died()
|
||||
|
|
|
@ -29,17 +29,10 @@ public:
|
|||
/* virtual */ ScriptInfo *FindLibrary(const char *library, int version);
|
||||
|
||||
private:
|
||||
const char *versionAPI; ///< Current API used by this script.
|
||||
|
||||
/* virtual */ void RegisterAPI();
|
||||
/* virtual */ void Died();
|
||||
/* virtual */ CommandCallback *GetDoCommandCallback();
|
||||
/* virtual */ void LoadDummyScript();
|
||||
|
||||
/**
|
||||
* Load squirrel scripts to emulate an older API.
|
||||
*/
|
||||
bool LoadCompatibilityScripts(const char *api_version);
|
||||
};
|
||||
|
||||
#endif /* AI_INSTANCE_HPP */
|
||||
|
|
|
@ -89,6 +89,8 @@ GameInstance::GameInstance() :
|
|||
|
||||
void GameInstance::Initialize(GameInfo *info)
|
||||
{
|
||||
this->versionAPI = info->GetAPIVersion();
|
||||
|
||||
/* Register the GameController */
|
||||
SQGSController_Register(this->engine);
|
||||
|
||||
|
@ -192,6 +194,8 @@ void GameInstance::RegisterAPI()
|
|||
SQGSWindow_Register(this->engine);
|
||||
|
||||
RegisterGameTranslation(this->engine);
|
||||
|
||||
if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
|
||||
}
|
||||
|
||||
int GameInstance::GetSetting(const char *name)
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
|
||||
#include "../company_base.h"
|
||||
#include "../company_func.h"
|
||||
#include "../fileio_func.h"
|
||||
|
||||
ScriptStorage::~ScriptStorage()
|
||||
{
|
||||
|
@ -104,6 +105,28 @@ void ScriptInstance::RegisterAPI()
|
|||
squirrel_register_std(this->engine);
|
||||
}
|
||||
|
||||
bool ScriptInstance::LoadCompatibilityScripts(const char *api_version, Subdirectory dir)
|
||||
{
|
||||
char script_name[32];
|
||||
seprintf(script_name, lastof(script_name), "compat_%s.nut", api_version);
|
||||
char buf[MAX_PATH];
|
||||
Searchpath sp;
|
||||
FOR_ALL_SEARCHPATHS(sp) {
|
||||
FioAppendDirectory(buf, MAX_PATH, sp, dir);
|
||||
ttd_strlcat(buf, script_name, MAX_PATH);
|
||||
if (!FileExists(buf)) continue;
|
||||
|
||||
if (this->engine->LoadScript(buf)) return true;
|
||||
|
||||
ScriptLog::Error("Failed to load API compatibility script");
|
||||
DEBUG(script, 0, "Error compiling / running API compatibility script: %s", buf);
|
||||
return false;
|
||||
}
|
||||
|
||||
ScriptLog::Warning("API compatibility script not found");
|
||||
return true;
|
||||
}
|
||||
|
||||
ScriptInstance::~ScriptInstance()
|
||||
{
|
||||
ScriptObject::ActiveInstance active(this);
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
|
||||
#include "../command_type.h"
|
||||
#include "../company_type.h"
|
||||
#include "../fileio_type.h"
|
||||
|
||||
static const uint SQUIRREL_MAX_DEPTH = 25; ///< The maximum recursive depth for items stored in the savegame.
|
||||
|
||||
|
@ -176,12 +177,21 @@ public:
|
|||
|
||||
protected:
|
||||
class Squirrel *engine; ///< A wrapper around the squirrel vm.
|
||||
const char *versionAPI; ///< Current API used by this script.
|
||||
|
||||
/**
|
||||
* Register all API functions to the VM.
|
||||
*/
|
||||
virtual void RegisterAPI();
|
||||
|
||||
/**
|
||||
* Load squirrel scripts to emulate an older API.
|
||||
* @param api_version: API version to load scripts for
|
||||
* @param dir Subdirectory to find the scripts in
|
||||
* @return true iff script loading should proceed
|
||||
*/
|
||||
bool LoadCompatibilityScripts(const char *api_version, Subdirectory dir);
|
||||
|
||||
/**
|
||||
* Tell the script it died.
|
||||
*/
|
||||
|
|
Loading…
Reference in New Issue