Changeset - r19539:8ff9f94e70e3
[Not reviewed]
master
0 5 0
yexo - 12 years ago 2012-08-13 19:22:26
yexo@openttd.org
(svn r24468) -Add [FS#5219]: API compatibility scripts for Goal Scripts (Hirundo)
5 files changed with 38 insertions and 31 deletions:
0 comments (0 inline, 0 general)
src/ai/ai_instance.cpp
Show inline comments
 
@@ -78,13 +78,12 @@
 
#include "../script/api/ai/ai_vehiclelist.hpp.sq"
 
#include "../script/api/ai/ai_waypoint.hpp.sq"
 
#include "../script/api/ai/ai_waypointlist.hpp.sq"
 

	
 
#include "../company_base.h"
 
#include "../company_func.h"
 
#include "../fileio_func.h"
 

	
 
AIInstance::AIInstance() :
 
	ScriptInstance("AI")
 
{}
 

	
 
void AIInstance::Initialize(AIInfo *info)
 
@@ -191,35 +190,13 @@ void AIInstance::RegisterAPI()
 
	SQAIVehicleList_SharedOrders_Register(this->engine);
 
	SQAIVehicleList_Station_Register(this->engine);
 
	SQAIWaypoint_Register(this->engine);
 
	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()
 
{
 
	ScriptInstance::Died();
 

	
src/ai/ai_instance.hpp
Show inline comments
 
@@ -26,20 +26,13 @@ public:
 
	void Initialize(class AIInfo *info);
 

	
 
	/* virtual */ int GetSetting(const char *name);
 
	/* 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 */
src/game/game_instance.cpp
Show inline comments
 
@@ -86,12 +86,14 @@
 
GameInstance::GameInstance() :
 
	ScriptInstance("GS")
 
{}
 

	
 
void GameInstance::Initialize(GameInfo *info)
 
{
 
	this->versionAPI = info->GetAPIVersion();
 

	
 
	/* Register the GameController */
 
	SQGSController_Register(this->engine);
 

	
 
	ScriptInstance::Initialize(info->GetMainScript(), info->GetInstanceName(), OWNER_DEITY);
 
}
 

	
 
@@ -189,12 +191,14 @@ void GameInstance::RegisterAPI()
 
	SQGSWaypoint_Register(this->engine);
 
	SQGSWaypointList_Register(this->engine);
 
	SQGSWaypointList_Vehicle_Register(this->engine);
 
	SQGSWindow_Register(this->engine);
 

	
 
	RegisterGameTranslation(this->engine);
 

	
 
	if (!this->LoadCompatibilityScripts(this->versionAPI, GAME_DIR)) this->Died();
 
}
 

	
 
int GameInstance::GetSetting(const char *name)
 
{
 
	return GameConfig::GetConfig()->GetSetting(name);
 
}
src/script/script_instance.cpp
Show inline comments
 
@@ -24,12 +24,13 @@
 
#include "api/script_error.hpp"
 
#include "api/script_event.hpp"
 
#include "api/script_log.hpp"
 

	
 
#include "../company_base.h"
 
#include "../company_func.h"
 
#include "../fileio_func.h"
 

	
 
ScriptStorage::~ScriptStorage()
 
{
 
	/* Free our pointers */
 
	if (event_data != NULL) ScriptEventController::FreeEventPointer();
 
	if (log_data != NULL) ScriptLog::FreeLogPointer();
 
@@ -101,12 +102,34 @@ void ScriptInstance::Initialize(const ch
 
void ScriptInstance::RegisterAPI()
 
{
 
	extern void squirrel_register_std(Squirrel *engine);
 
	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);
 

	
 
	if (instance != NULL) this->engine->ReleaseObject(this->instance);
 
	if (engine != NULL) delete this->engine;
src/script/script_instance.hpp
Show inline comments
 
@@ -14,12 +14,13 @@
 

	
 
#include <squirrel.h>
 
#include "script_suspend.hpp"
 

	
 
#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.
 

	
 
/** Runtime information about a script like a pointer to the squirrel vm and the current state. */
 
class ScriptInstance {
 
public:
 
@@ -173,19 +174,28 @@ public:
 
	 *  script executed a DoCommand, or executed this.Sleep().
 
	 */
 
	bool IsSleeping() { return this->suspend != 0; }
 

	
 
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.
 
	 */
 
	virtual void Died();
 

	
 
	/**
 
	 * Get the callback handling DoCommands in case of networking.
0 comments (0 inline, 0 general)