2016-02-27 10:57:10 +01:00
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
#include "Module.hpp"
|
|
|
|
#include "Common.hpp"
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cstdarg>
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
#include <sqrat.h>
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
#include <Windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
PluginFuncs* _Func = nullptr;
|
|
|
|
PluginCallbacks* _Clbk = nullptr;
|
|
|
|
PluginInfo* _Info = nullptr;
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
HSQAPI _SqAPI = nullptr;
|
|
|
|
HSQEXPORTS _SqMod = nullptr;
|
|
|
|
HSQUIRRELVM _SqVM = nullptr;
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Bind speciffic functions to certain server events.
|
|
|
|
*/
|
|
|
|
void BindCallbacks();
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Undo changes made with BindCallbacks().
|
|
|
|
*/
|
|
|
|
void UnbindCallbacks();
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Register the module API under the specified virtual machine.
|
|
|
|
*/
|
|
|
|
void RegisterAPI(HSQUIRRELVM vm);
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Initialize the plugin by obtaining the API provided by the host plugin.
|
|
|
|
*/
|
|
|
|
void OnSquirrelInitialize()
|
|
|
|
{
|
|
|
|
// Attempt to import the plugin API exported by the host plugin
|
|
|
|
_SqMod = sq_api_import(_Func);
|
|
|
|
// Did we failed to obtain the plugin exports?
|
|
|
|
if(!_SqMod)
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
OutputError("Failed to attach [%s] on host plugin.", SQSAMPLE_NAME);
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Obtain the Squirrel API
|
|
|
|
_SqAPI = _SqMod->GetSquirrelAPI();
|
|
|
|
// Expand the Squirrel API into global functions
|
|
|
|
sq_api_expand(_SqAPI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Load the module on the virtual machine provided by the host module.
|
|
|
|
*/
|
|
|
|
void OnSquirrelLoad()
|
|
|
|
{
|
|
|
|
// Make sure that we have a valid plugin API
|
|
|
|
if (!_SqMod)
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
|
|
|
return; // Unable to proceed.
|
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Obtain the Squirrel API and VM
|
|
|
|
_SqVM = _SqMod->GetSquirrelVM();
|
|
|
|
// Make sure that a valid virtual machine exists
|
|
|
|
if (!_SqVM)
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
|
|
|
return; // Unable to proceed.
|
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Set this as the default database
|
|
|
|
DefaultVM::Set(_SqVM);
|
|
|
|
// Register the module API
|
|
|
|
RegisterAPI(_SqVM);
|
|
|
|
// Notify about the current status
|
|
|
|
OutputMessage("Registered: %s", SQSAMPLE_NAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* The virtual machine is about to be terminated and script resources should be released.
|
|
|
|
*/
|
|
|
|
void OnSquirrelTerminate()
|
|
|
|
{
|
|
|
|
OutputMessage("Terminating: %s", SQSAMPLE_NAME);
|
|
|
|
// Release the current database (if any)
|
2016-04-14 02:08:35 +02:00
|
|
|
DefaultVM::Set(nullptr);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Release script resources...
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Validate the module API to make sure we don't run into issues.
|
|
|
|
*/
|
|
|
|
bool CheckAPIVer(CCStr ver)
|
|
|
|
{
|
|
|
|
// Obtain the numeric representation of the API version
|
2016-04-14 02:08:35 +02:00
|
|
|
long vernum = std::strtol(ver, nullptr, 10);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Check against version mismatch
|
|
|
|
if (vernum == SQMOD_API_VER)
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
return true;
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
// Log the incident
|
|
|
|
OutputError("API version mismatch on %s", SQSAMPLE_NAME);
|
|
|
|
OutputMessage("=> Requested: %ld Have: %ld", vernum, SQMOD_API_VER);
|
|
|
|
// Invoker should not attempt to communicate through the module API
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* React to command sent by other plugins.
|
|
|
|
*/
|
|
|
|
static int OnInternalCommand(unsigned int type, const char * text)
|
|
|
|
{
|
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case SQMOD_INITIALIZE_CMD:
|
|
|
|
if (CheckAPIVer(text))
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
OnSquirrelInitialize();
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-02-27 10:57:10 +01:00
|
|
|
break;
|
|
|
|
case SQMOD_LOAD_CMD:
|
|
|
|
OnSquirrelLoad();
|
|
|
|
break;
|
|
|
|
case SQMOD_TERMINATE_CMD:
|
|
|
|
OnSquirrelTerminate();
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* The server was initialized and this plugin was loaded successfully.
|
|
|
|
*/
|
|
|
|
static int OnInitServer()
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void OnShutdownServer(void)
|
|
|
|
{
|
|
|
|
// The server may still send callbacks
|
|
|
|
UnbindCallbacks();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void BindCallbacks()
|
|
|
|
{
|
|
|
|
_Clbk->OnInitServer = OnInitServer;
|
|
|
|
_Clbk->OnInternalCommand = OnInternalCommand;
|
|
|
|
_Clbk->OnShutdownServer = OnShutdownServer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void UnbindCallbacks()
|
|
|
|
{
|
2016-04-14 02:08:35 +02:00
|
|
|
_Clbk->OnInitServer = nullptr;
|
|
|
|
_Clbk->OnInternalCommand = nullptr;
|
|
|
|
_Clbk->OnShutdownServer = nullptr;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void RegisterAPI(HSQUIRRELVM vm)
|
|
|
|
{
|
|
|
|
RootTable(vm).Bind(_SC("SampleType"), Class< SampleType >(vm, _SC("SampleType"))
|
|
|
|
.Ctor()
|
|
|
|
.Ctor< int >()
|
|
|
|
.Var(_SC("MyNum"), &SampleType::mMyNum)
|
|
|
|
.Prop(_SC("MyVal"), &SampleType::GetMyVal, &SampleType::SetMyVal)
|
|
|
|
.Func(_SC("SampleMethod"), &SampleType::SampleMethod)
|
|
|
|
);
|
|
|
|
|
|
|
|
RootTable(vm).Func(_SC("SampleFunction"), &SampleFunction);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void OutputMessageImpl(const char * msg, va_list args)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csb_before;
|
|
|
|
GetConsoleScreenBufferInfo( hstdout, &csb_before);
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN);
|
2016-04-14 02:08:35 +02:00
|
|
|
std::printf("[SQMOD] ");
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
|
2016-04-14 02:08:35 +02:00
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
|
|
|
|
#else
|
2016-04-14 02:08:35 +02:00
|
|
|
std::printf("%c[0;32m[SQMOD]%c[0;37m", 27, 27);
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void OutputErrorImpl(const char * msg, va_list args)
|
|
|
|
{
|
|
|
|
#if defined(WIN32) || defined(_WIN32)
|
|
|
|
HANDLE hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
|
|
|
|
|
|
|
|
CONSOLE_SCREEN_BUFFER_INFO csb_before;
|
|
|
|
GetConsoleScreenBufferInfo( hstdout, &csb_before);
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_RED | FOREGROUND_INTENSITY);
|
2016-04-14 02:08:35 +02:00
|
|
|
std::printf("[SQMOD] ");
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
|
2016-04-14 02:08:35 +02:00
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
|
|
|
|
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
|
|
|
|
#else
|
2016-04-14 02:08:35 +02:00
|
|
|
std::printf("%c[0;32m[SQMOD]%c[0;37m", 27, 27);
|
|
|
|
std::vprintf(msg, args);
|
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void OutputDebug(const char * msg, ...)
|
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputMessageImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
#else
|
|
|
|
SQMOD_UNUSED_VAR(msg);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void OutputMessage(const char * msg, ...)
|
|
|
|
{
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputMessageImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
void OutputError(const char * msg, ...)
|
|
|
|
{
|
|
|
|
// Initialize the arguments list
|
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
|
|
|
// Call the output function
|
|
|
|
OutputErrorImpl(msg, args);
|
|
|
|
// Finalize the arguments list
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs* functions, PluginCallbacks* callbacks, PluginInfo* info)
|
|
|
|
{
|
|
|
|
using namespace SqMod;
|
|
|
|
// Output plugin header
|
2016-04-14 02:08:35 +02:00
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
OutputMessage("--------------------------------------------------------------------");
|
|
|
|
OutputMessage("Plugin: %s", SQSAMPLE_NAME);
|
|
|
|
OutputMessage("Author: %s", SQSAMPLE_AUTHOR);
|
|
|
|
OutputMessage("Legal: %s", SQSAMPLE_COPYRIGHT);
|
|
|
|
OutputMessage("--------------------------------------------------------------------");
|
2016-04-14 02:08:35 +02:00
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
// Attempt to find the host plugin ID
|
|
|
|
int host_plugin_id = functions->FindPlugin((char *)(SQMOD_HOST_NAME));
|
|
|
|
// See if our plugin was loaded after the host plugin
|
|
|
|
if (host_plugin_id < 0)
|
|
|
|
{
|
|
|
|
OutputError("%s could find the host plugin", SQSAMPLE_NAME);
|
|
|
|
// Don't load!
|
|
|
|
return SQMOD_FAILURE;
|
|
|
|
}
|
|
|
|
// Should never reach this point but just in case
|
|
|
|
else if (host_plugin_id > (info->nPluginId))
|
|
|
|
{
|
|
|
|
OutputError("%s loaded after the host plugin", SQSAMPLE_NAME);
|
|
|
|
// Don't load!
|
|
|
|
return SQMOD_FAILURE;
|
|
|
|
}
|
|
|
|
// Store server proxies
|
|
|
|
_Func = functions;
|
|
|
|
_Clbk = callbacks;
|
|
|
|
_Info = info;
|
|
|
|
// Assign plugin information
|
|
|
|
_Info->uPluginVer = SQSAMPLE_VERSION;
|
2016-04-14 02:08:35 +02:00
|
|
|
std::strcpy(_Info->szName, SQSAMPLE_HOST_NAME);
|
2016-02-27 10:57:10 +01:00
|
|
|
// Bind callbacks
|
|
|
|
BindCallbacks();
|
|
|
|
// Notify that the plugin was successfully loaded
|
|
|
|
OutputMessage("Successfully loaded %s", SQSAMPLE_NAME);
|
|
|
|
// Dummy spacing
|
2016-04-14 02:08:35 +02:00
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
// Done!
|
|
|
|
return SQMOD_SUCCESS;
|
|
|
|
}
|