1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00
SqMod/source/Main.cpp

558 lines
17 KiB
C++
Raw Normal View History

// --------------------------------------------------------------------------------------------
2015-09-30 02:56:11 +02:00
#include "Logger.hpp"
#include "Core.hpp"
#include "Command.hpp"
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
#include <cstdio>
#if defined(_WIN32)
#include <winsock2.h>
#endif // _WIN32
2016-02-23 16:48:30 +01:00
// ------------------------------------------------------------------------------------------------
namespace SqMod {
extern void InitExports();
} // Namespace:: SqMod
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
using namespace SqMod;
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
void BindCallbacks();
void UnbindCallbacks();
2015-09-30 02:56:11 +02:00
2016-02-23 16:48:30 +01:00
// ------------------------------------------------------------------------------------------------
void DestroyComponents()
{
// Destroy command component
if (_Cmd)
{
delete _Cmd;
_Cmd = NULL;
}
// Destroy core component
if (_Core)
{
delete _Core;
_Core = NULL;
}
// Destroy logger component
if (_Log)
{
delete _Log;
_Log = NULL;
}
}
2015-09-30 02:56:11 +02:00
// ------------------------------------------------------------------------------------------------
SQMOD_API_EXPORT unsigned int VcmpPluginInit(PluginFuncs * funcs, PluginCallbacks * calls, PluginInfo * info)
{
#if defined(_WIN32)
WSADATA wsaData;
#endif // _WIN32
_Log = Logger::Get();
// Verify that core components are working
2015-09-30 02:56:11 +02:00
if (!_Log)
{
puts("[SQMOD] Unable to start because the logging class could not be instantiated");
return SQMOD_FAILURE;
}
2016-02-23 16:48:30 +01:00
_Core = Core::Get();
if (!_Core)
2015-09-30 02:56:11 +02:00
{
2016-02-23 16:48:30 +01:00
DestroyComponents();
2015-09-30 02:56:11 +02:00
puts("[SQMOD] Unable to start because the central core class could not be instantiated");
return SQMOD_FAILURE;
}
2016-02-23 16:48:30 +01:00
_Cmd = CmdManager::Get();
if (!_Cmd)
{
DestroyComponents();
puts("[SQMOD] Unable to start because the command class could not be instantiated");
return SQMOD_FAILURE;
}
#if defined(_WIN32)
// Initialize the sockets on windows
2016-02-23 16:48:30 +01:00
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
{
2016-02-23 16:48:30 +01:00
DestroyComponents();
puts("[SQMOD] Unable to start because the windows sockets could not be initialized");
return SQMOD_FAILURE;
}
#endif // _WIN32
2015-09-30 02:56:11 +02:00
_Func = funcs;
_Clbk = calls;
_Info = info;
2015-09-30 02:56:11 +02:00
_Info->uPluginVer = SQMOD_VERSION;
2016-02-23 16:48:30 +01:00
strcpy(_Info->szName, SQMOD_HOST_NAME);
2015-09-30 02:56:11 +02:00
if (!_Core->Init())
{
LogFtl("The plugin failed to initialize");
_Core->Terminate();
2016-02-23 16:48:30 +01:00
DestroyComponents();
2015-09-30 02:56:11 +02:00
return SQMOD_FAILURE;
}
else if (_Clbk)
{
BindCallbacks();
}
else
{
2016-02-23 16:48:30 +01:00
_Core->Terminate();
DestroyComponents();
LogFtl("Unable to start because the server callbacks are missing");
2016-02-23 16:48:30 +01:00
return SQMOD_FAILURE;
2015-09-30 02:56:11 +02:00
}
2016-02-23 16:48:30 +01:00
// Attempt to initialize the plugin exports
InitExports();
// Initialization was successful
2015-09-30 02:56:11 +02:00
return SQMOD_SUCCESS;
}
// --------------------------------------------------------------------------------------------
static int VC_InitServer(void)
2015-09-30 02:56:11 +02:00
{
2016-02-23 16:48:30 +01:00
if (!_Core)
return SQMOD_FAILURE;
_Core->SetState(1);
2015-09-30 02:56:11 +02:00
if (_Core->Load())
_Core->EmitServerStartup();
2015-09-30 02:56:11 +02:00
else
LogFtl("Unable to load the plugin resources properly");
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static void VC_ShutdownServer(void)
2015-09-30 02:56:11 +02:00
{
2016-02-23 16:48:30 +01:00
if (!_Core)
return;
_Core->EmitServerShutdown();
// Deallocate and release everything obtained at startup
2015-09-30 02:56:11 +02:00
_Core->Terminate();
// The server still triggers callbacks and we deallocated everything!
UnbindCallbacks();
2016-02-23 16:48:30 +01:00
// Destroy components
DestroyComponents();
2015-09-30 02:56:11 +02:00
}
static void VC_Frame(float delta)
2015-09-30 02:56:11 +02:00
{
_Core->EmitServerFrame(delta);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerConnect(int player)
2015-09-30 02:56:11 +02:00
{
_Core->ConnectPlayer(player, SQMOD_CREATE_AUTOMATIC, NullObject());
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerDisconnect(int player, int reason)
2015-09-30 02:56:11 +02:00
{
_Core->DisconnectPlayer(player, reason, NullObject());
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerBeginTyping(int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerStartTyping(player);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerEndTyping(int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerStopTyping(player);
2015-09-30 02:56:11 +02:00
}
static int VC_PlayerRequestClass(int player, int offset)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerRequestClass(player, offset);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static int VC_PlayerRequestSpawn(int player)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerRequestSpawn(player);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static void VC_PlayerSpawn(int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerSpawn(player);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerDeath(int player, int killer, int reason, int body_part)
2015-09-30 02:56:11 +02:00
{
if (_Func->IsPlayerConnected(killer))
_Core->EmitPlayerKilled(player, killer, reason, body_part);
2015-09-30 02:56:11 +02:00
else
_Core->EmitPlayerWasted(player, reason);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerUpdate(int player, int type)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerUpdate(player, type);
2015-09-30 02:56:11 +02:00
}
static int VC_PlayerRequestEnter(int player, int vehicle, int slot)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerEmbarking(player, vehicle, slot);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static void VC_PlayerEnterVehicle(int player, int vehicle, int slot)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerEmbarked(player, vehicle, slot);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerExitVehicle(int player, int vehicle)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerDisembark(player, vehicle);
2015-09-30 02:56:11 +02:00
}
static int VC_PickupClaimPicked(int pickup, int player)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPickupClaimed(player, pickup);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static void VC_PickupPickedUp(int pickup, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPickupCollected(player, pickup);
2015-09-30 02:56:11 +02:00
}
static void VC_PickupRespawn(int pickup)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPickupRespawn(pickup);
2015-09-30 02:56:11 +02:00
}
static void VC_VehicleUpdate(int vehicle, int type)
2015-09-30 02:56:11 +02:00
{
_Core->EmitVehicleUpdate(vehicle, type);
2015-09-30 02:56:11 +02:00
}
static void VC_VehicleExplode(int vehicle)
2015-09-30 02:56:11 +02:00
{
_Core->EmitVehicleExplode(vehicle);
2015-09-30 02:56:11 +02:00
}
static void VC_VehicleRespawn(int vehicle)
2015-09-30 02:56:11 +02:00
{
_Core->EmitVehicleRespawn(vehicle);
2015-09-30 02:56:11 +02:00
}
static void VC_ObjectShot(int object, int player, int weapon)
2015-09-30 02:56:11 +02:00
{
_Core->EmitObjectShot(player, object, weapon);
2015-09-30 02:56:11 +02:00
}
static void VC_ObjectBump(int object, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitObjectBump(player, object);
2015-09-30 02:56:11 +02:00
}
static int VC_PublicMessage(int player, const char * text)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerChat(player, text);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static int VC_CommandMessage(int player, const char * text)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerCommand(player, text);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static int VC_PrivateMessage(int player, int target, const char * text)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitPlayerMessage(player, target, text);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static int VC_InternalCommand(unsigned int type, const char * text)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitInternalCommand(type, text);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static int VC_LoginAttempt(char * name, const char * passwd, const char * address)
2015-09-30 02:56:11 +02:00
{
_Core->SetState(SQMOD_SUCCESS);
_Core->EmitLoginAttempt(name, passwd, address);
2015-09-30 02:56:11 +02:00
return _Core->GetState();
}
static void VC_EntityPool(int type, int id, unsigned int deleted)
2015-09-30 02:56:11 +02:00
{
_Core->EmitEntityPool(type, id, static_cast<bool>(deleted));
2015-09-30 02:56:11 +02:00
}
static void VC_KeyBindDown(int player, int bind)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerKeyPress(player, bind);
2015-09-30 02:56:11 +02:00
}
static void VC_KeyBindUp(int player, int bind)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerKeyRelease(player, bind);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerAway(int player, unsigned int status)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerAway(player, static_cast<bool>(status));
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerSpectate(int player, int target)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerSpectate(player, target);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerCrashReport(int player, const char * report)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerCrashreport(player, report);
2015-09-30 02:56:11 +02:00
}
static void VC_ServerPerformanceReport(int count, const char ** description, unsigned long long * millis)
2015-09-30 02:56:11 +02:00
{
// Ignored for now...
SQMOD_UNUSED_VAR(count);
SQMOD_UNUSED_VAR(description);
SQMOD_UNUSED_VAR(millis);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerName(int player, const char * previous, const char * current)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerRename(player, previous, current);
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerState(int player, int previous, int current)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerState(player, previous, current);
2015-09-30 02:56:11 +02:00
switch (current)
{
case SQMOD_PLAYER_STATE_NONE:
_Core->EmitStateNone(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_NORMAL:
_Core->EmitStateNormal(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_SHOOTING:
_Core->EmitStateShooting(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_DRIVER:
_Core->EmitStateDriver(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_PASSENGER:
_Core->EmitStatePassenger(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_ENTERING_AS_DRIVER:
_Core->EmitStateEnterDriver(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_ENTERING_AS_PASSENGER:
_Core->EmitStateEnterPassenger(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_EXITING_VEHICLE:
_Core->EmitStateExitVehicle(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_STATE_UNSPAWNED:
_Core->EmitStateUnspawned(player, previous);
2015-09-30 02:56:11 +02:00
break;
}
}
static void VC_PlayerAction(int player, int previous, int current)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerAction(player, previous, current);
2015-09-30 02:56:11 +02:00
switch (current)
{
case SQMOD_PLAYER_ACTION_NONE:
_Core->EmitActionNone(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_NORMAL:
_Core->EmitActionNormal(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_AIMING:
_Core->EmitActionAiming(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_SHOOTING:
_Core->EmitActionShooting(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_JUMPING:
_Core->EmitActionJumping(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_LYING_ON_GROUND:
_Core->EmitActionLieDown(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_GETTING_UP:
_Core->EmitActionGettingUp(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_JUMPING_FROM_VEHICLE:
_Core->EmitActionJumpVehicle(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_DRIVING:
_Core->EmitActionDriving(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_DYING:
_Core->EmitActionDying(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_WASTED:
_Core->EmitActionWasted(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_ENTERING_VEHICLE:
_Core->EmitActionEmbarking(player, previous);
2015-09-30 02:56:11 +02:00
break;
case SQMOD_PLAYER_ACTION_EXITING_VEHICLE:
_Core->EmitActionDisembarking(player, previous);
2015-09-30 02:56:11 +02:00
break;
}
}
static void VC_PlayerOnFire(int player, unsigned int state)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerBurning(player, static_cast<bool>(state));
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerCrouch(int player, unsigned int state)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerCrouching(player, static_cast<bool>(state));
2015-09-30 02:56:11 +02:00
}
static void VC_PlayerGameKeys(int player, int previous, int current)
2015-09-30 02:56:11 +02:00
{
_Core->EmitPlayerGameKeys(player, previous, current);
2015-09-30 02:56:11 +02:00
}
static void VC_OnCheckpointEntered(int checkpoint, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitCheckpointEntered(player, checkpoint);
2015-09-30 02:56:11 +02:00
}
static void VC_OnCheckpointExited(int checkpoint, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitCheckpointExited(player, checkpoint);
2015-09-30 02:56:11 +02:00
}
static void VC_OnSphereEntered(int sphere, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitForcefieldEntered(player, sphere);
2015-09-30 02:56:11 +02:00
}
static void VC_OnSphereExited(int sphere, int player)
2015-09-30 02:56:11 +02:00
{
_Core->EmitForcefieldExited(player, sphere);
2015-09-30 02:56:11 +02:00
}
// ------------------------------------------------------------------------------------------------
void BindCallbacks()
2015-09-30 02:56:11 +02:00
{
_Clbk->OnInitServer = VC_InitServer;
_Clbk->OnShutdownServer = VC_ShutdownServer;
_Clbk->OnFrame = VC_Frame;
_Clbk->OnPlayerConnect = VC_PlayerConnect;
_Clbk->OnPlayerDisconnect = VC_PlayerDisconnect;
_Clbk->OnPlayerBeginTyping = VC_PlayerBeginTyping;
_Clbk->OnPlayerEndTyping = VC_PlayerEndTyping;
_Clbk->OnPlayerRequestClass = VC_PlayerRequestClass;
_Clbk->OnPlayerRequestSpawn = VC_PlayerRequestSpawn;
_Clbk->OnPlayerSpawn = VC_PlayerSpawn;
_Clbk->OnPlayerDeath = VC_PlayerDeath;
_Clbk->OnPlayerUpdate = VC_PlayerUpdate;
_Clbk->OnPlayerRequestEnter = VC_PlayerRequestEnter;
_Clbk->OnPlayerEnterVehicle = VC_PlayerEnterVehicle;
_Clbk->OnPlayerExitVehicle = VC_PlayerExitVehicle;
_Clbk->OnPickupClaimPicked = VC_PickupClaimPicked;
_Clbk->OnPickupPickedUp = VC_PickupPickedUp;
_Clbk->OnPickupRespawn = VC_PickupRespawn;
_Clbk->OnVehicleUpdate = VC_VehicleUpdate;
_Clbk->OnVehicleExplode = VC_VehicleExplode;
_Clbk->OnVehicleRespawn = VC_VehicleRespawn;
_Clbk->OnObjectShot = VC_ObjectShot;
_Clbk->OnObjectBump = VC_ObjectBump;
_Clbk->OnPublicMessage = VC_PublicMessage;
_Clbk->OnCommandMessage = VC_CommandMessage;
_Clbk->OnPrivateMessage = VC_PrivateMessage;
_Clbk->OnInternalCommand = VC_InternalCommand;
_Clbk->OnLoginAttempt = VC_LoginAttempt;
_Clbk->OnEntityPoolChange = VC_EntityPool;
_Clbk->OnKeyBindDown = VC_KeyBindDown;
_Clbk->OnKeyBindUp = VC_KeyBindUp;
_Clbk->OnPlayerAwayChange = VC_PlayerAway;
_Clbk->OnPlayerSpectate = VC_PlayerSpectate;
_Clbk->OnPlayerCrashReport = VC_PlayerCrashReport;
_Clbk->OnServerPerformanceReport = VC_ServerPerformanceReport;
_Clbk->OnPlayerNameChange = VC_PlayerName;
_Clbk->OnPlayerStateChange = VC_PlayerState;
_Clbk->OnPlayerActionChange = VC_PlayerAction;
_Clbk->OnPlayerOnFireChange = VC_PlayerOnFire;
_Clbk->OnPlayerCrouchChange = VC_PlayerCrouch;
_Clbk->OnPlayerGameKeysChange = VC_PlayerGameKeys;
_Clbk->OnCheckpointEntered = VC_OnCheckpointEntered;
_Clbk->OnCheckpointExited = VC_OnCheckpointExited;
_Clbk->OnSphereEntered = VC_OnSphereEntered;
_Clbk->OnSphereExited = VC_OnSphereExited;
}
// ------------------------------------------------------------------------------------------------
void UnbindCallbacks()
{
_Clbk->OnInitServer = NULL;
_Clbk->OnShutdownServer = NULL;
_Clbk->OnFrame = NULL;
_Clbk->OnPlayerConnect = NULL;
_Clbk->OnPlayerDisconnect = NULL;
_Clbk->OnPlayerBeginTyping = NULL;
_Clbk->OnPlayerEndTyping = NULL;
_Clbk->OnPlayerRequestClass = NULL;
_Clbk->OnPlayerRequestSpawn = NULL;
_Clbk->OnPlayerSpawn = NULL;
_Clbk->OnPlayerDeath = NULL;
_Clbk->OnPlayerUpdate = NULL;
_Clbk->OnPlayerRequestEnter = NULL;
_Clbk->OnPlayerEnterVehicle = NULL;
_Clbk->OnPlayerExitVehicle = NULL;
_Clbk->OnPickupClaimPicked = NULL;
_Clbk->OnPickupPickedUp = NULL;
_Clbk->OnPickupRespawn = NULL;
_Clbk->OnVehicleUpdate = NULL;
_Clbk->OnVehicleExplode = NULL;
_Clbk->OnVehicleRespawn = NULL;
_Clbk->OnObjectShot = NULL;
_Clbk->OnObjectBump = NULL;
_Clbk->OnPublicMessage = NULL;
_Clbk->OnCommandMessage = NULL;
_Clbk->OnPrivateMessage = NULL;
_Clbk->OnInternalCommand = NULL;
_Clbk->OnLoginAttempt = NULL;
_Clbk->OnEntityPoolChange = NULL;
_Clbk->OnKeyBindDown = NULL;
_Clbk->OnKeyBindUp = NULL;
_Clbk->OnPlayerAwayChange = NULL;
_Clbk->OnPlayerSpectate = NULL;
_Clbk->OnPlayerCrashReport = NULL;
_Clbk->OnServerPerformanceReport = NULL;
_Clbk->OnPlayerNameChange = NULL;
_Clbk->OnPlayerStateChange = NULL;
_Clbk->OnPlayerActionChange = NULL;
_Clbk->OnPlayerOnFireChange = NULL;
_Clbk->OnPlayerCrouchChange = NULL;
_Clbk->OnPlayerGameKeysChange = NULL;
_Clbk->OnCheckpointEntered = NULL;
_Clbk->OnCheckpointExited = NULL;
_Clbk->OnSphereEntered = NULL;
_Clbk->OnSphereExited = NULL;
}