scripthandler.cpp

来自「这是整套横扫千军3D版游戏的源码」· C++ 代码 · 共 124 行

CPP
124
字号
// ScriptHandler.cpp: implementation of the CScriptHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "StdAfx.h"
#include "ScriptHandler.h"
#include "Game/Game.h"
#include "FileSystem/FileHandler.h"
#include "LoadScript.h"
#include "CommanderScript.h"
#include "CommanderScript2.h"
#include "AirScript.h"
#include "GlobalAITestScript.h"
#include "SpawnScript.h"
#include "EmptyScript.h"
#include "TestScript.h"
#include "Platform/SharedLib.h"
#ifndef NO_LUA
#include "System/Platform/errorhandler.h"
#endif
#include "mmgr.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CScriptHandler::CScriptHandler() : chosenScript(0), callback(0)
{
}

/** Load all scripts. */
void CScriptHandler::LoadScripts() {

	ENTER_SYNCED;
	loaded_scripts.push_back( SAFE_NEW CCommanderScript() );
	loaded_scripts.push_back( SAFE_NEW CCommanderScript2() );
	loaded_scripts.push_back( SAFE_NEW CAirScript() );
	loaded_scripts.push_back( SAFE_NEW CEmptyScript() );
	loaded_scripts.push_back( SAFE_NEW CSpawnScript(false) );
	loaded_scripts.push_back( SAFE_NEW CSpawnScript(true) );
	loaded_scripts.push_back( SAFE_NEW CTestScript() );

	const char *path = "AI/Bot-libs/";
	std::vector<std::string> f = CFileHandler::FindFiles(path, std::string("*.") + SharedLib::GetLibExtension());

	for(std::vector<std::string>::iterator fi = f.begin(), e = f.end(); fi != e; ++fi)
		loaded_scripts.push_back(SAFE_NEW CGlobalAITestScript(*fi));

	f = CFileHandler::FindFiles("Saves/", "*.ssf");
	for(std::vector<std::string>::iterator fi = f.begin(), e = f.end(); fi != e; ++fi) {
		loaded_scripts.push_back(SAFE_NEW CLoadScript(*fi));
	}
	ENTER_UNSYNCED;
}

void CScriptHandler::StartLua()
{
#ifndef NO_LUA
	std::vector<string> files = CFileHandler::FindFiles("startscripts/", "*.lua");
	for (std::vector<string>::iterator i = files.begin(); i != files.end(); ++i) {
		CLuaBinder* lua = SAFE_NEW CLuaBinder();
		if (!lua->LoadScript(*i))
			handleerror(NULL, lua->lastError.c_str(), "Lua", MBF_OK|MBF_EXCL);
		lua_binders.push_back(lua);
	}
#endif
}

/** @Return a reference to the only CScriptHandler instance */
CScriptHandler& CScriptHandler::Instance()
{
	static bool created = false;
	static CScriptHandler instance;
	if( !created ) {
		created = true;
		instance.LoadScripts();
	}
	return instance;
}

CScriptHandler::~CScriptHandler()
{
	while(!loaded_scripts.empty()) {
		delete loaded_scripts.back();
		loaded_scripts.pop_back();
	}

#ifndef NO_LUA
	while (!lua_binders.empty()) {
		delete lua_binders.back();
		lua_binders.pop_back();
	}
#endif
}

/** Called by the CScript constructors to add themselves to the CScriptHandler. */
void CScriptHandler::AddScript(string name, CScript *s)
{
	scripts[name] = s;
}

/** Called by the CglList generated by CScriptHandler::GenList on selection of a script. */
void CScriptHandler::SelectScript(std::string s)
{
	CScriptHandler::Instance().chosenName = s;
	if (CScriptHandler::Instance().scripts.find(s) == CScriptHandler::Instance().scripts.end()) {
		throw std::runtime_error("script not found: " + s);
	}
	CScriptHandler::Instance().chosenScript = CScriptHandler::Instance().scripts[s];
	CScriptHandler::Instance().chosenScript->ScriptSelected();
	if (CScriptHandler::Instance().callback)
		CScriptHandler::Instance().callback(s);
}

/** Generate a CglList with all available scripts. */
CglList* CScriptHandler::GenList(ListSelectCallback callb)
{
	CglList* list = SAFE_NEW CglList("Select script", SelectScript, 1);
	for (std::map<std::string,CScript*>::const_iterator it = scripts.begin(); it != scripts.end(); ++it)
		list->AddItem(it->first.c_str(), it->first.c_str());
	callback = callb;
	return list;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?