From 083228a10dffcaa77b1d0035c29013c6802befd4 Mon Sep 17 00:00:00 2001 From: "lapayo94@gmail.com" Date: Sun, 8 Jul 2012 21:01:08 +0000 Subject: Squirrel Plugins I worked a little bit on the squirrel Bindings They work now on linux and windows :) (OSX is untested, but should work also) but they are very limited at the moment. (Only made OnChat working) I also fixed some small bugs. git-svn-id: http://mc-server.googlecode.com/svn/trunk@648 0a769ca7-a7f5-676a-18bf-c427514a06d6 --- source/squirrelbindings/SquirrelBindings.cpp | 52 +++++++++++++++++++++ source/squirrelbindings/SquirrelBindings.h | 13 ++++++ source/squirrelbindings/SquirrelFunctions.cpp | 65 +++++++++++++++++++++++++++ source/squirrelbindings/SquirrelFunctions.h | 6 +++ source/squirrelbindings/SquirrelObject.h | 24 ++++++++++ source/squirrelbindings/cSquirrelBaseClass.h | 29 ++++++++++++ 6 files changed, 189 insertions(+) create mode 100644 source/squirrelbindings/SquirrelBindings.cpp create mode 100644 source/squirrelbindings/SquirrelBindings.h create mode 100644 source/squirrelbindings/SquirrelFunctions.cpp create mode 100644 source/squirrelbindings/SquirrelFunctions.h create mode 100644 source/squirrelbindings/SquirrelObject.h create mode 100644 source/squirrelbindings/cSquirrelBaseClass.h (limited to 'source/squirrelbindings') diff --git a/source/squirrelbindings/SquirrelBindings.cpp b/source/squirrelbindings/SquirrelBindings.cpp new file mode 100644 index 000000000..27a82c306 --- /dev/null +++ b/source/squirrelbindings/SquirrelBindings.cpp @@ -0,0 +1,52 @@ +#include "Globals.h" +#include "SquirrelBindings.h" +#include "SquirrelFunctions.h" + +#include "cSquirrelBaseClass.h" + +#include "../cPlayer.h" + +using namespace Sqrat; + + + +void BindSquirrel(HSQUIRRELVM vm) +{ + RootTable() + .Bind("Plugin", Class() + .Func("AddHook", &cSquirrelBaseClass::AddHook) + ); + RootTable().Bind("cPlayer", Class() + .Func("GetName", &cPlayer::GetName) + ); + + + RootTable().Func("print", &sqPrint); + + + ConstTable().Enum("Hook", Enumeration() + .Const("Tick", cPluginManager::HOOK_TICK) + .Const("Chat", cPluginManager::HOOK_CHAT) + .Const("CollectItem", cPluginManager::HOOK_COLLECT_ITEM) + .Const("BlockDig", cPluginManager::HOOK_BLOCK_DIG) + .Const("BlockPlace", cPluginManager::HOOK_BLOCK_PLACE) + .Const("Disconnect", cPluginManager::HOOK_DISCONNECT) + .Const("Handshake", cPluginManager::HOOK_HANDSHAKE) + .Const("Login", cPluginManager::HOOK_LOGIN) + .Const("PlayerSpawn", cPluginManager::HOOK_PLAYER_SPAWN) + .Const("PlayerJoin", cPluginManager::HOOK_PLAYER_JOIN) + .Const("PlayerMove", cPluginManager::HOOK_PLAYER_MOVE) + .Const("TakeDamage", cPluginManager::HOOK_TAKE_DAMAGE) + .Const("Killed", cPluginManager::HOOK_KILLED) + .Const("ChunkGenerated", cPluginManager::HOOK_CHUNK_GENERATED) + .Const("ChunkGenerating", cPluginManager::HOOK_CHUNK_GENERATING) + .Const("BlockToDrops", cPluginManager::HOOK_BLOCK_TO_DROPS) + .Const("PreCrafting", cPluginManager::HOOK_PRE_CRAFTING) + .Const("CraftingNoRecipe", cPluginManager::HOOK_CRAFTING_NO_RECIPE) + .Const("PostCrafting", cPluginManager::HOOK_POST_CRAFTING) + .Const("BlockToPickup", cPluginManager::HOOK_BLOCK_TO_PICKUP) + .Const("WeatherChanged", cPluginManager::HOOK_WEATHER_CHANGED) + .Const("UpdatingSign", cPluginManager::HOOK_UPDATING_SIGN) + .Const("UpdatedSign", cPluginManager::HOOK_UPDATED_SIGN)); + +} \ No newline at end of file diff --git a/source/squirrelbindings/SquirrelBindings.h b/source/squirrelbindings/SquirrelBindings.h new file mode 100644 index 000000000..1b71f5e86 --- /dev/null +++ b/source/squirrelbindings/SquirrelBindings.h @@ -0,0 +1,13 @@ +#pragma once + + +#define USE_SQUIRREL 1 + +#if USE_SQUIRREL + +#include +#include + +void BindSquirrel(HSQUIRRELVM vm); + +#endif diff --git a/source/squirrelbindings/SquirrelFunctions.cpp b/source/squirrelbindings/SquirrelFunctions.cpp new file mode 100644 index 000000000..88871369d --- /dev/null +++ b/source/squirrelbindings/SquirrelFunctions.cpp @@ -0,0 +1,65 @@ + +#include "Globals.h" +#include "SquirrelFunctions.h" +#include "SquirrelBindings.h" + +static HSQUIRRELVM squirrelvm = NULL; + +SQInteger runtimeErrorHandler(HSQUIRRELVM a_VM) +{ + const SQChar *sErr = 0; + if(sq_gettop(a_VM) >= 1) + { + if(SQ_SUCCEEDED(sq_getstring(a_VM, 2, &sErr))) + { + LOGERROR("Squirrel Error: %s", sErr); + } + else + { + LOGERROR("Squirrel Error: Unknown Error"); + } + } + return 0; +} + +void compilerErrorHandler(HSQUIRRELVM v, + const SQChar* a_Desc, + const SQChar* a_Source, + SQInteger a_Line, + SQInteger a_Column) +{ + + LOGERROR("Squirrel Error: %s (%d:%d) %s", a_Source, a_Line, a_Column, a_Desc); +} + +HSQUIRRELVM OpenSquirrelVM() +{ + if(!squirrelvm) + { + squirrelvm = sq_open(1024); + Sqrat::DefaultVM::Set(squirrelvm); + + sq_newclosure(squirrelvm, runtimeErrorHandler, 0); + sq_seterrorhandler(squirrelvm); + + sq_setcompilererrorhandler(squirrelvm, compilerErrorHandler); + + BindSquirrel(squirrelvm); + } + return squirrelvm; +} + +void CloseSquirrelVM() +{ + if(squirrelvm) + { + sq_close(squirrelvm); + squirrelvm = NULL; + } +} + + +void sqPrint(SQChar * text) +{ + LOGINFO("%s", text); +} \ No newline at end of file diff --git a/source/squirrelbindings/SquirrelFunctions.h b/source/squirrelbindings/SquirrelFunctions.h new file mode 100644 index 000000000..0d08a726c --- /dev/null +++ b/source/squirrelbindings/SquirrelFunctions.h @@ -0,0 +1,6 @@ +#pragma once +#include +HSQUIRRELVM OpenSquirrelVM(); +void CloseSquirrelVM(); + +void sqPrint(SQChar * text); \ No newline at end of file diff --git a/source/squirrelbindings/SquirrelObject.h b/source/squirrelbindings/SquirrelObject.h new file mode 100644 index 000000000..1ac6fa105 --- /dev/null +++ b/source/squirrelbindings/SquirrelObject.h @@ -0,0 +1,24 @@ +#pragma once +#include + +class SquirrelObject +{ +public: + SquirrelObject(Sqrat::Object a_Obj) + { + m_SquirrelObject = a_Obj; + } + + Sqrat::Function GetFunction(const char *methodName) + { + if(m_SquirrelObject.IsNull()) + return Sqrat::Function(); + + Sqrat::Function method(m_SquirrelObject, methodName); + return method; + } + +protected: + Sqrat::Object m_SquirrelObject; + +}; \ No newline at end of file diff --git a/source/squirrelbindings/cSquirrelBaseClass.h b/source/squirrelbindings/cSquirrelBaseClass.h new file mode 100644 index 000000000..e06301555 --- /dev/null +++ b/source/squirrelbindings/cSquirrelBaseClass.h @@ -0,0 +1,29 @@ +#pragma once +#include "SquirrelBindings.h" +#include "../cPlugin_Squirrel.h" +#include "../cPluginManager.h" +#include "cRoot.h" + +//The baseclass for squirrel plugins +class cSquirrelBaseClass +{ +public: + cSquirrelBaseClass() + : m_Instance(0) + { + } + + void setInstance(cPlugin_Squirrel *a_Instance) + { + m_Instance = a_Instance; + } + + void AddHook(short a_Hook) + { + if(m_Instance) + cRoot::Get()->GetPluginManager()->AddHook(m_Instance, (cPluginManager::PluginHook) a_Hook); + } + +protected: + cPlugin_Squirrel *m_Instance; +}; \ No newline at end of file -- cgit v1.2.3