1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Documented the code in Core class and made a few minor modifications.

This commit is contained in:
Sandu Liviu Catalin 2015-10-29 22:06:31 +02:00
parent 491ead2b12
commit 0ccf4678a8

View File

@ -54,13 +54,14 @@ Core::Core() noexcept
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Core::~Core() Core::~Core()
{ {
// Tell the plugin to terminate
this->Terminate(); this->Terminate();
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Core::_Finalizer(Core * ptr) noexcept void Core::_Finalizer(Core * ptr) noexcept
{ {
if (ptr) delete ptr; delete ptr; /* Assuming 'delete' checks for NULL */
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
@ -78,7 +79,7 @@ Core::Pointer Core::Inst() noexcept
bool Core::Init() noexcept bool Core::Init() noexcept
{ {
LogMsg("%s", CenterStr("INITIALIZING", '*')); LogMsg("%s", CenterStr("INITIALIZING", '*'));
// Attempt to initialize the plugin resources
if (!this->Configure() || !this->CreateVM() || !this->LoadScripts()) if (!this->Configure() || !this->CreateVM() || !this->LoadScripts())
{ {
return false; return false;
@ -92,7 +93,7 @@ bool Core::Init() noexcept
bool Core::Load() noexcept bool Core::Load() noexcept
{ {
LogMsg("%s", CenterStr("LOADING", '*')); LogMsg("%s", CenterStr("LOADING", '*'));
// Attemot to execute the loaded scripts
if (!this->Execute()) if (!this->Execute())
{ {
return false; return false;
@ -106,6 +107,7 @@ bool Core::Load() noexcept
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Core::Deinit() noexcept void Core::Deinit() noexcept
{ {
// Release the VM created during the initialization process
this->DestroyVM(); this->DestroyVM();
} }
@ -133,13 +135,15 @@ SQInteger Core::GetState() const noexcept
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
string Core::GetOption(const string & name) const noexcept string Core::GetOption(const String & name) const noexcept
{ {
// Attempt to find the specified option
OptionPool::const_iterator elem = m_Options.find(name); OptionPool::const_iterator elem = m_Options.find(name);
return (elem == m_Options.cend()) ? string() : elem->second; // Return the value associated with the found option
return (elem == m_Options.cend()) ? String() : elem->second;
} }
void Core::SetOption(const string & name, const string & value) noexcept void Core::SetOption(const String & name, const String & value) noexcept
{ {
m_Options[name] = value; m_Options[name] = value;
} }
@ -147,28 +151,41 @@ void Core::SetOption(const string & name, const string & value) noexcept
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
Core::Buffer Core::PullBuffer(unsigned sz) noexcept Core::Buffer Core::PullBuffer(unsigned sz) noexcept
{ {
// The container that will manage the buffer
Buffer buf; Buffer buf;
// See if there's any buffers available in the pool
if (m_BufferPool.empty()) if (m_BufferPool.empty())
{ {
// Create a new buffer if one wasn't available
buf.resize(sz); buf.resize(sz);
} }
// Just fetch one from the pool
else else
{ {
// Fetch the buffer
buf = std::move(m_BufferPool.back()); buf = std::move(m_BufferPool.back());
// Remove it from the pool
m_BufferPool.pop(); m_BufferPool.pop();
} }
// Give the obtained buffer
return std::move(buf); return std::move(buf);
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Core::PushBuffer(Buffer && buf) noexcept void Core::PushBuffer(Buffer && buf) noexcept
{ {
m_BufferPool.push(std::move(buf)); // Make sure we don't store empty buffers
if (!buf.empty())
{
// Return the specified buffer back to the pool
m_BufferPool.push(std::move(buf));
}
} }
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void Core::MakeBuffer(unsigned num, unsigned sz) noexcept void Core::MakeBuffer(unsigned num, unsigned sz) noexcept
{ {
// Create the specified number of buffers
while (num--) while (num--)
{ {
m_BufferPool.emplace(sz); m_BufferPool.emplace(sz);
@ -204,16 +221,18 @@ void Core::DisconnectPlayer(SQInt32 id, SQInt32 header, SqObj & payload) noexcep
bool Core::Configure() noexcept bool Core::Configure() noexcept
{ {
LogDbg("Attempting to instantiate the configuration file"); LogDbg("Attempting to instantiate the configuration file");
// See if the configurations instance was previously created
if (g_Config) if (g_Config)
{ {
// Release the loaded configurations
g_Config->Reset(); g_Config->Reset();
} }
// Create the configuration instance
else else
{ {
g_Config.reset(new CSimpleIniA(true, true, true)); g_Config.reset(new CSimpleIniA(true, true, true));
} }
// See if a configuration instance couuld be created
if (!g_Config) if (!g_Config)
{ {
LogFtl("Unable to instantiate the configuration class"); LogFtl("Unable to instantiate the configuration class");
@ -222,9 +241,9 @@ bool Core::Configure() noexcept
} }
LogDbg("Attempting to load the configuration file."); LogDbg("Attempting to load the configuration file.");
// Attempt to load the configurations from disk
SI_Error ini_ret = g_Config->LoadFile(_SC("./sqmod.ini")); SI_Error ini_ret = g_Config->LoadFile(_SC("./sqmod.ini"));
// See if the configurations could be loaded
if (ini_ret < 0) if (ini_ret < 0)
{ {
switch (ini_ret) switch (ini_ret)
@ -238,7 +257,7 @@ bool Core::Configure() noexcept
} }
LogDbg("Applying the specified logging filters"); LogDbg("Applying the specified logging filters");
// Apply the specified logging filters before anything else
if (!SToB(g_Config->GetValue("ConsoleLog", "Debug", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_DBG); if (!SToB(g_Config->GetValue("ConsoleLog", "Debug", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_DBG);
if (!SToB(g_Config->GetValue("ConsoleLog", "Message", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_MSG); if (!SToB(g_Config->GetValue("ConsoleLog", "Message", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_MSG);
if (!SToB(g_Config->GetValue("ConsoleLog", "Success", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_SCS); if (!SToB(g_Config->GetValue("ConsoleLog", "Success", "true"))) _Log->DisableConsoleLevel(Logger::LEVEL_SCS);
@ -1647,45 +1666,58 @@ void Core::OnLogMessage(SQInt32 type, const SQChar * message) noexcept
void Core::OnPlayerUpdate(SQInt32 player, SQInt32 type) noexcept void Core::OnPlayerUpdate(SQInt32 player, SQInt32 type) noexcept
{ {
Vector3 pos; Vector3 pos;
_Func->GetPlayerPos(player, &pos.x, &pos.y, &pos.z); // Is this player instance tracked for the first time
if (m_PlayerTrack[player].Fresh) if (m_PlayerTrack[player].Fresh)
{ {
// Obtain the current position of this instance
_Func->GetPlayerPos(player, &pos.x, &pos.y, &pos.z);
// Initialize the tracked values for the first time
m_PlayerTrack[player].Position = pos; m_PlayerTrack[player].Position = pos;
m_PlayerTrack[player].Health = _Func->GetPlayerHealth(player); m_PlayerTrack[player].Health = _Func->GetPlayerHealth(player);
m_PlayerTrack[player].Armour = _Func->GetPlayerArmour(player); m_PlayerTrack[player].Armour = _Func->GetPlayerArmour(player);
m_PlayerTrack[player].Weapon = _Func->GetPlayerWeapon(player); m_PlayerTrack[player].Weapon = _Func->GetPlayerWeapon(player);
m_PlayerTrack[player].Fresh = false; m_PlayerTrack[player].Fresh = false;
// No need to check a freshly tracked instance
return; return;
} }
// Obtain the current position of this instance
_Func->GetPlayerPos(player, &pos.x, &pos.y, &pos.z);
// Did the position change since the last tracked value?
if (pos != m_PlayerTrack[player].Position) if (pos != m_PlayerTrack[player].Position)
{ {
// Triger the speciffic event
PlayerMove.Emit(player, m_PlayerTrack[player].Position, pos); PlayerMove.Emit(player, m_PlayerTrack[player].Position, pos);
// Update the tracked value
m_PlayerTrack[player].Position = pos; m_PlayerTrack[player].Position = pos;
} }
// Obtain the current health of this instance
SQFloat health = _Func->GetPlayerHealth(player); SQFloat health = _Func->GetPlayerHealth(player);
// Did the health change since the last tracked value?
if (!EpsEq(health, m_PlayerTrack[player].Health)) if (!EpsEq(health, m_PlayerTrack[player].Health))
{ {
// Triger the speciffic event
PlayerHealth.Emit(player, m_PlayerTrack[player].Health, health); PlayerHealth.Emit(player, m_PlayerTrack[player].Health, health);
// Update the tracked value
m_PlayerTrack[player].Health = health; m_PlayerTrack[player].Health = health;
} }
// Obtain the current armour of this instance
SQFloat armour = _Func->GetPlayerArmour(player); SQFloat armour = _Func->GetPlayerArmour(player);
// Did the armour change since the last tracked value?
if (!EpsEq(armour, m_PlayerTrack[player].Armour)) if (!EpsEq(armour, m_PlayerTrack[player].Armour))
{ {
// Triger the speciffic event
PlayerArmour.Emit(player, m_PlayerTrack[player].Armour, armour); PlayerArmour.Emit(player, m_PlayerTrack[player].Armour, armour);
// Update the tracked value
m_PlayerTrack[player].Armour = armour; m_PlayerTrack[player].Armour = armour;
} }
// Obtain the current weapon of this instance
SQInteger wep = _Func->GetPlayerWeapon(player); SQInteger wep = _Func->GetPlayerWeapon(player);
// Did the weapon change since the last tracked value?
if (wep != m_PlayerTrack[player].Weapon) if (wep != m_PlayerTrack[player].Weapon)
{ {
// Triger the speciffic event
PlayerWeapon.Emit(player, m_PlayerTrack[player].Weapon, wep); PlayerWeapon.Emit(player, m_PlayerTrack[player].Weapon, wep);
// Update the tracked value
m_PlayerTrack[player].Weapon = wep; m_PlayerTrack[player].Weapon = wep;
} }
} }
@ -1693,35 +1725,47 @@ void Core::OnPlayerUpdate(SQInt32 player, SQInt32 type) noexcept
void Core::OnVehicleUpdate(SQInt32 vehicle, SQInt32 type) noexcept void Core::OnVehicleUpdate(SQInt32 vehicle, SQInt32 type) noexcept
{ {
Vector3 pos; Vector3 pos;
_Func->GetVehiclePos(vehicle, &pos.x, &pos.y, &pos.z); // Is this vehicle instance tracked for the first time
if (m_VehicleTrack[vehicle].Fresh) if (m_VehicleTrack[vehicle].Fresh)
{ {
// Obtain the current position of this instance
_Func->GetVehiclePos(vehicle, &pos.x, &pos.y, &pos.z);
// Initialize the tracked values for the first time
m_VehicleTrack[vehicle].Position = pos; m_VehicleTrack[vehicle].Position = pos;
m_VehicleTrack[vehicle].Health = _Func->GetVehicleHealth(vehicle); m_VehicleTrack[vehicle].Health = _Func->GetVehicleHealth(vehicle);
m_VehicleTrack[vehicle].Fresh = false; m_VehicleTrack[vehicle].Fresh = false;
// No need to check a freshly tracked instance
return; return;
} }
// Obtain the current position of this instance
_Func->GetVehiclePos(vehicle, &pos.x, &pos.y, &pos.z);
// Did the position change since the last tracked value?
if (pos != m_VehicleTrack[vehicle].Position) if (pos != m_VehicleTrack[vehicle].Position)
{ {
// Triger the speciffic event
VehicleMove.Emit(vehicle, m_VehicleTrack[vehicle].Position, pos); VehicleMove.Emit(vehicle, m_VehicleTrack[vehicle].Position, pos);
// Update the tracked value
m_VehicleTrack[vehicle].Position = pos; m_VehicleTrack[vehicle].Position = pos;
} }
// Obtain the current health of this instance
SQFloat health = _Func->GetVehicleHealth(vehicle); SQFloat health = _Func->GetVehicleHealth(vehicle);
// Did the health change since the last tracked value?
if (!EpsEq(health, m_VehicleTrack[vehicle].Health)) if (!EpsEq(health, m_VehicleTrack[vehicle].Health))
{ {
// Triger the speciffic event
VehicleHealth.Emit(vehicle, m_VehicleTrack[vehicle].Health, health); VehicleHealth.Emit(vehicle, m_VehicleTrack[vehicle].Health, health);
// Update the tracked value
m_VehicleTrack[vehicle].Health = health; m_VehicleTrack[vehicle].Health = health;
} }
} }
void Core::OnEntityPool(SQInt32 type, SQInt32 id, bool deleted) noexcept void Core::OnEntityPool(SQInt32 type, SQInt32 id, bool deleted) noexcept
{ {
// Script object to play the role of a dummy payload
static SqObj payload; static SqObj payload;
// Make sure that the payload is null
payload.Release(); payload.Release();
// See what type of change happened in the entity pool
switch (type) switch (type)
{ {
case SQMOD_ENTITY_POOL_VEHICLE: case SQMOD_ENTITY_POOL_VEHICLE: