2016-06-03 20:31:58 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 10:57:10 +01:00
|
|
|
#include "Common.hpp"
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-14 02:08:35 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-27 10:57:10 +01:00
|
|
|
namespace SqMod {
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-07-17 02:24:07 +02:00
|
|
|
* Register the module API under the obtained virtual machine.
|
2016-02-27 10:57:10 +01:00
|
|
|
*/
|
2016-07-19 20:13:56 +02:00
|
|
|
static bool RegisterAPI(HSQUIRRELVM vm)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
// Make sure there's a valid virtual machine before proceeding
|
2016-07-19 20:13:56 +02:00
|
|
|
if (!vm)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
OutputError("%s: Cannot register API without a valid virtual machine", SQSAMPLE_NAME);
|
|
|
|
// Registration failed
|
|
|
|
return false;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
2016-07-17 02:24:07 +02:00
|
|
|
|
2016-07-19 20:13:56 +02:00
|
|
|
RootTable(vm).Bind(_SC("SampleType"),
|
|
|
|
Class< SampleType >(vm, _SC("SampleType"))
|
2016-07-17 02:24:07 +02:00
|
|
|
.Ctor()
|
|
|
|
.Ctor< int >()
|
|
|
|
.Var(_SC("MyNum"), &SampleType::mMyNum)
|
|
|
|
.Prop(_SC("MyVal"), &SampleType::GetMyVal, &SampleType::SetMyVal)
|
|
|
|
.Func(_SC("SampleMethod"), &SampleType::SampleMethod)
|
|
|
|
);
|
|
|
|
|
2016-07-19 20:13:56 +02:00
|
|
|
RootTable(vm).Func(_SC("SampleFunction"), &SampleFunction);
|
2016-07-17 02:24:07 +02:00
|
|
|
|
|
|
|
// Registration was successful
|
|
|
|
return true;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-27 10:57:10 +01:00
|
|
|
* Load the module on the virtual machine provided by the host module.
|
|
|
|
*/
|
2016-07-17 02:24:07 +02:00
|
|
|
static bool OnSquirrelLoad()
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
// Make sure that we have a valid module API
|
|
|
|
if (!SqMod_GetSquirrelVM)
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
OutputError("%s: Cannot obtain the Squirrel virtual machine without the module API", SQSAMPLE_NAME);
|
|
|
|
// Unable to proceed!
|
|
|
|
return false;
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-07-17 02:24:07 +02:00
|
|
|
// Obtain the Squirrel virtual machine from the host plug-in
|
|
|
|
DefaultVM::Set(SqMod_GetSquirrelVM());
|
2016-02-27 10:57:10 +01:00
|
|
|
// Make sure that a valid virtual machine exists
|
2016-07-17 02:24:07 +02:00
|
|
|
if (!DefaultVM::Get())
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
OutputError("%s: Squirrel virtual machine obtained from the host plug-in is invalid", SQSAMPLE_NAME);
|
|
|
|
// Unable to proceed!
|
|
|
|
return false;
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-07-03 02:27:18 +02:00
|
|
|
// Prevent common null objects from using dead virtual machines
|
2017-06-16 01:22:48 +02:00
|
|
|
NullObject().Release();
|
|
|
|
NullTable().Release();
|
|
|
|
NullArray().Release();
|
|
|
|
NullLightObj().Release();
|
|
|
|
NullFunction().ReleaseGently();
|
2016-02-27 10:57:10 +01:00
|
|
|
// Register the module API
|
2016-07-19 20:13:56 +02:00
|
|
|
if (RegisterAPI(DefaultVM::Get()))
|
2016-07-17 02:24:07 +02:00
|
|
|
{
|
|
|
|
OutputMessage("Registered: %s", SQSAMPLE_NAME);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// At this point, the module was successfully loaded
|
|
|
|
return true;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-02-27 10:57:10 +01:00
|
|
|
* The virtual machine is about to be terminated and script resources should be released.
|
|
|
|
*/
|
2016-07-17 02:24:07 +02:00
|
|
|
static void OnSquirrelTerminate()
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
OutputMessage("Terminating: %s", SQSAMPLE_NAME);
|
2016-06-03 20:31:58 +02:00
|
|
|
// Release null objects just in case
|
|
|
|
NullObject().Release();
|
|
|
|
NullTable().Release();
|
|
|
|
NullArray().Release();
|
|
|
|
NullFunction().ReleaseGently();
|
2016-06-26 15:18:23 +02:00
|
|
|
// Release script resources...
|
|
|
|
}
|
|
|
|
|
2016-09-02 13:11:39 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* The virtual machined is about to be closed. Last chance to release anything manually.
|
|
|
|
*/
|
|
|
|
static void OnSquirrelClosing()
|
|
|
|
{
|
|
|
|
// Nothing to release manually...
|
|
|
|
}
|
|
|
|
|
2016-06-26 15:18:23 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* The virtual machined was closed and all memory associated with it was released.
|
|
|
|
*/
|
2016-07-17 02:24:07 +02:00
|
|
|
static void OnSquirrelReleased()
|
2016-06-26 15:18:23 +02:00
|
|
|
{
|
2016-05-22 21:34:27 +02:00
|
|
|
// Release the current virtual machine, if any
|
2016-04-14 02:08:35 +02:00
|
|
|
DefaultVM::Set(nullptr);
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* React to command sent by other plug-ins.
|
2016-02-27 10:57:10 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
switch(command_identifier)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
case SQMOD_INITIALIZE_CMD:
|
2016-07-17 02:24:07 +02:00
|
|
|
{
|
|
|
|
if (CheckModuleAPIVer(message, SQSAMPLE_NAME))
|
2016-04-14 02:08:35 +02:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ImportModuleAPI(_Func, SQSAMPLE_NAME);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
OutputError("%s", e.what());
|
|
|
|
// Failed to initialize
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-14 02:08:35 +02:00
|
|
|
}
|
2016-07-17 02:24:07 +02:00
|
|
|
} break;
|
2016-02-27 10:57:10 +01:00
|
|
|
case SQMOD_LOAD_CMD:
|
2016-07-17 02:24:07 +02:00
|
|
|
{
|
|
|
|
return OnSquirrelLoad();
|
|
|
|
} break;
|
2016-02-27 10:57:10 +01:00
|
|
|
case SQMOD_TERMINATE_CMD:
|
2016-07-17 02:24:07 +02:00
|
|
|
{
|
2016-02-27 10:57:10 +01:00
|
|
|
OnSquirrelTerminate();
|
2016-07-17 02:24:07 +02:00
|
|
|
} break;
|
2016-09-02 13:11:39 +02:00
|
|
|
case SQMOD_CLOSING_CMD:
|
|
|
|
{
|
|
|
|
OnSquirrelClosing();
|
|
|
|
} break;
|
2016-06-26 15:18:23 +02:00
|
|
|
case SQMOD_RELEASED_CMD:
|
2016-07-17 02:24:07 +02:00
|
|
|
{
|
2016-06-26 15:18:23 +02:00
|
|
|
OnSquirrelReleased();
|
2016-07-17 02:24:07 +02:00
|
|
|
} break;
|
2016-02-27 10:57:10 +01:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* The server was initialized and this plug-in was loaded successfully.
|
2016-02-27 10:57:10 +01:00
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static uint8_t OnServerInitialise()
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
2016-07-17 02:24:07 +02:00
|
|
|
return 1; // Initialization was successful
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
2016-07-17 02:24:07 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* The server is about to shutdown gracefully.
|
|
|
|
*/
|
2016-05-22 05:20:38 +02:00
|
|
|
static void OnServerShutdown(void)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
// The server may still send callbacks
|
2016-05-22 05:20:38 +02:00
|
|
|
_Clbk->OnServerInitialise = nullptr;
|
|
|
|
_Clbk->OnServerShutdown = nullptr;
|
|
|
|
_Clbk->OnPluginCommand = nullptr;
|
2016-02-27 10:57:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|
|
|
|
|
2016-06-03 20:31:58 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCallbacks * callbacks, PluginInfo * info)
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
using namespace SqMod;
|
2016-06-03 20:31:58 +02:00
|
|
|
// Output plug-in header
|
2016-04-14 02:08:35 +02:00
|
|
|
std::puts("");
|
2016-02-27 10:57:10 +01:00
|
|
|
OutputMessage("--------------------------------------------------------------------");
|
2016-06-03 20:31:58 +02:00
|
|
|
OutputMessage("Plug-in: %s", SQSAMPLE_NAME);
|
2016-02-27 10:57:10 +01:00
|
|
|
OutputMessage("Author: %s", SQSAMPLE_AUTHOR);
|
|
|
|
OutputMessage("Legal: %s", SQSAMPLE_COPYRIGHT);
|
|
|
|
OutputMessage("--------------------------------------------------------------------");
|
2016-04-14 02:08:35 +02:00
|
|
|
std::puts("");
|
2016-07-17 02:24:07 +02:00
|
|
|
// Make sure that the module was loaded after the host plug-in
|
|
|
|
if (!CheckModuleOrder(functions, info->pluginId, SQSAMPLE_NAME))
|
2016-02-27 10:57:10 +01:00
|
|
|
{
|
|
|
|
return SQMOD_FAILURE;
|
|
|
|
}
|
|
|
|
// Store server proxies
|
|
|
|
_Func = functions;
|
|
|
|
_Clbk = callbacks;
|
|
|
|
_Info = info;
|
2016-06-03 20:31:58 +02:00
|
|
|
// Assign plug-in version
|
2016-05-22 05:20:38 +02:00
|
|
|
_Info->pluginVersion = SQSAMPLE_VERSION;
|
|
|
|
_Info->apiMajorVersion = PLUGIN_API_MAJOR;
|
|
|
|
_Info->apiMinorVersion = PLUGIN_API_MINOR;
|
2016-06-03 20:31:58 +02:00
|
|
|
// Assign the plug-in name
|
2016-05-22 05:20:38 +02:00
|
|
|
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQSAMPLE_HOST_NAME);
|
2016-07-16 20:51:01 +02:00
|
|
|
// Bind to the server callbacks
|
|
|
|
_Clbk->OnServerInitialise = OnServerInitialise;
|
|
|
|
_Clbk->OnServerShutdown = OnServerShutdown;
|
|
|
|
_Clbk->OnPluginCommand = OnPluginCommand;
|
2016-06-03 20:31:58 +02:00
|
|
|
// Notify that the plug-in was successfully loaded
|
2016-02-27 10:57:10 +01:00
|
|
|
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;
|
|
|
|
}
|