Update the IRC module to work with the modified API.

Document code properly and also various code fixes.
This commit is contained in:
Sandu Liviu Catalin
2016-06-03 21:27:53 +03:00
parent 0c92601362
commit 40ab83743c
7 changed files with 234 additions and 594 deletions
+48 -145
View File
@@ -1,36 +1,21 @@
// --------------------------------------------------------------------------------------------
#include "Module.hpp"
// ------------------------------------------------------------------------------------------------
#include "Common.hpp"
#include "Session.hpp"
// --------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
// --------------------------------------------------------------------------------------------
#include <sqrat.h>
// --------------------------------------------------------------------------------------------
#if defined(WIN32) || defined(_WIN32)
#include <Windows.h>
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <Winsock2.h>
#endif
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// --------------------------------------------------------------------------------------------
PluginFuncs* _Func = nullptr;
PluginCallbacks* _Clbk = nullptr;
PluginInfo* _Info = nullptr;
// --------------------------------------------------------------------------------------------
HSQAPI _SqAPI = nullptr;
HSQEXPORTS _SqMod = nullptr;
HSQUIRRELVM _SqVM = nullptr;
/* ------------------------------------------------------------------------------------------------
* Bind speciffic functions to certain server events.
* Bind specific functions to certain server events.
*/
void BindCallbacks();
@@ -39,22 +24,22 @@ void 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.
/* ------------------------------------------------------------------------------------------------
* Initialize the plug-in by obtaining the API provided by the host plug-in.
*/
void OnSquirrelInitialize()
{
// Attempt to import the plugin API exported by the host plugin
// Attempt to import the plug-in API exported by the host plug-in
_SqMod = sq_api_import(_Func);
// Did we failed to obtain the plugin exports?
if(!_SqMod)
// Did we failed to obtain the plug-in exports?
if (!_SqMod)
{
OutputError("Failed to attach [%s] on host plugin.", SQIRC_NAME);
OutputError("Failed to attach [%s] on host plug-in.", SQIRC_NAME);
}
else
{
@@ -65,12 +50,12 @@ void OnSquirrelInitialize()
}
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* Load the module on the virtual machine provided by the host module.
*/
void OnSquirrelLoad()
{
// Make sure that we have a valid plugin API
// Make sure that we have a valid plug-in API
if (!_SqMod)
{
return; // Unable to proceed!
@@ -90,7 +75,7 @@ void OnSquirrelLoad()
OutputMessage("Registered: %s", SQIRC_NAME);
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* The virtual machine is about to be terminated and script resources should be released.
*/
void OnSquirrelTerminate()
@@ -107,13 +92,13 @@ void OnSquirrelTerminate()
DefaultVM::Set(nullptr);
}
/* --------------------------------------------------------------------------------------------
/* ------------------------------------------------------------------------------------------------
* 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
long vernum = std::strtol(ver, nullptr, 10);
const LongI vernum = std::strtol(ver, nullptr, 10);
// Check against version mismatch
if (vernum == SQMOD_API_VER)
{
@@ -126,8 +111,8 @@ bool CheckAPIVer(CCStr ver)
return false;
}
/* --------------------------------------------------------------------------------------------
* React to command sent by other plugins.
/* ------------------------------------------------------------------------------------------------
* React to command sent by other plug-ins.
*/
static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
{
@@ -150,8 +135,8 @@ static uint8_t OnPluginCommand(uint32_t command_identifier, CCStr message)
return 1;
}
/* --------------------------------------------------------------------------------------------
* The server was initialized and this plugin was loaded successfully.
/* ------------------------------------------------------------------------------------------------
* The server was initialized and this plug-in was loaded successfully.
*/
static uint8_t OnServerInitialise()
{
@@ -164,8 +149,8 @@ static void OnServerShutdown(void)
UnbindCallbacks();
}
/* --------------------------------------------------------------------------------------------
* Update the plugin on each frame.
/* ------------------------------------------------------------------------------------------------
* Update the plug-in on each frame.
*/
static void OnServerFrame(float /*delta*/)
{
@@ -191,7 +176,7 @@ void UnbindCallbacks()
_Clbk->OnPluginCommand = nullptr;
}
// --------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
void RegisterAPI(HSQUIRRELVM vm)
{
Table ircns(vm);
@@ -199,7 +184,7 @@ void RegisterAPI(HSQUIRRELVM vm)
ircns.Bind(_SC("Session"), Class< Session, NoCopy< Session > >(vm, _SC("SqIrcSession"))
// Constructors
.Ctor()
// Core Metamethods
// Core Meta-methods
.Func(_SC("_cmp"), &Session::Cmp)
.SquirrelFunc(_SC("_typename"), &Session::Typename)
.Func(_SC("_tostring"), &Session::ToString)
@@ -276,9 +261,12 @@ void RegisterAPI(HSQUIRRELVM vm)
.SquirrelFunc(_SC("CmdNoticeF"), &Session::CmdNoticeF)
);
ircns.Func(_SC("GetNick"), &GetNick);
ircns.Func(_SC("GetHost"), &GetHost);
ircns.Func(_SC("GetErrStr"), &irc_strerror);
ircns.SquirrelFunc(_SC("GetNick"), &GetNick);
ircns.SquirrelFunc(_SC("GetHost"), &GetHost);
ircns.SquirrelFunc(_SC("StripColorFromMIRC"), &StripColorFromMIRC);
ircns.SquirrelFunc(_SC("ConvertColorFromMIRC"), &ConvertColorFromMIRC);
ircns.SquirrelFunc(_SC("ConvertColorToMIRC"), &ConvertColorToMIRC);
RootTable(vm).Bind(_SC("SqIRC"), ircns);
@@ -477,144 +465,59 @@ void RegisterAPI(HSQUIRRELVM vm)
);
}
// --------------------------------------------------------------------------------------------
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);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;32m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#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);
std::printf("[SQMOD] ");
SetConsoleTextAttribute(hstdout, FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY);
std::vprintf(msg, args);
std::puts("");
SetConsoleTextAttribute(hstdout, csb_before.wAttributes);
#else
std::printf("%c[0;91m[SQMOD]%c[0m", 27, 27);
std::vprintf(msg, args);
std::puts("");
#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)
// ------------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * functions, PluginCallbacks * callbacks, PluginInfo * info)
{
using namespace SqMod;
// Output plugin header
// Output plug-in header
puts("");
OutputMessage("--------------------------------------------------------------------");
OutputMessage("Plugin: %s", SQIRC_NAME);
OutputMessage("Plug-in: %s", SQIRC_NAME);
OutputMessage("Author: %s", SQIRC_AUTHOR);
OutputMessage("Legal: %s", SQIRC_COPYRIGHT);
OutputMessage("--------------------------------------------------------------------");
puts("");
// 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
// Attempt to find the host plug-in ID
const int host_plugin_id = functions->FindPlugin(SQMOD_HOST_NAME);
// See if our plug-in was loaded after the host plug-in
if (host_plugin_id < 0)
{
OutputError("%s could find the host plugin", SQIRC_NAME);
OutputError("%s could find the host plug-in", SQIRC_NAME);
// Don't load!
return SQMOD_FAILURE;
}
// Should never reach this point but just in case
else if (static_cast< Uint32 >(host_plugin_id) > info->pluginId)
{
OutputError("%s loaded after the host plugin", SQIRC_NAME);
OutputError("%s loaded after the host plug-in", SQIRC_NAME);
// Don't load!
return SQMOD_FAILURE;
}
#if defined(_WIN32)
#ifdef SQMOD_OS_WINDOWS
WSADATA wsa_data;
// Initialize the sockets on windows
if (WSAStartup(MAKEWORD(2, 2), &wsa_data) != 0)
{
OutputError("Unable to start because the windows sockets could not be initialized");
OutputError("Unable to initialize the windows sockets");
// Don't load!
return SQMOD_FAILURE;
}
#endif // _WIN32
#endif // SQMOD_OS_WINDOWS
// Store server proxies
_Func = functions;
_Clbk = callbacks;
_Info = info;
// Assign plugin version
// Assign plug-in version
_Info->pluginVersion = SQIRC_VERSION;
_Info->apiMajorVersion = PLUGIN_API_MAJOR;
_Info->apiMinorVersion = PLUGIN_API_MINOR;
// Assign the plugin name
// Assign the plug-in name
std::snprintf(_Info->name, sizeof(_Info->name), "%s", SQIRC_HOST_NAME);
// Bind callbacks
BindCallbacks();
// Notify that the plugin was successfully loaded
// Notify that the plug-in was successfully loaded
OutputMessage("Successfully loaded %s", SQIRC_NAME);
// Dummy spacing
puts("");