2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-09-30 02:56:11 +02:00
|
|
|
#include "Core.hpp"
|
|
|
|
#include "Logger.hpp"
|
2018-07-29 11:25:44 +02:00
|
|
|
#include "Misc/Signal.hpp"
|
|
|
|
#include "Misc/Areas.hpp"
|
|
|
|
#include "Base/Buffer.hpp"
|
|
|
|
#include "Library/Utils/Buffer.hpp"
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
#include "Entity/Blip.hpp"
|
|
|
|
#include "Entity/Checkpoint.hpp"
|
|
|
|
#include "Entity/Keybind.hpp"
|
|
|
|
#include "Entity/Object.hpp"
|
|
|
|
#include "Entity/Pickup.hpp"
|
|
|
|
#include "Entity/Player.hpp"
|
|
|
|
#include "Entity/Vehicle.hpp"
|
2015-09-30 02:56:11 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2018-07-29 11:25:44 +02:00
|
|
|
#include <SqMod.h>
|
2015-09-30 02:56:11 +02:00
|
|
|
#include <sqstdio.h>
|
|
|
|
#include <sqstdblob.h>
|
|
|
|
#include <sqstdmath.h>
|
|
|
|
#include <sqstdsystem.h>
|
|
|
|
#include <sqstdstring.h>
|
2016-02-20 23:25:00 +01:00
|
|
|
#include <SimpleIni.h>
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdarg>
|
2015-09-30 02:56:11 +02:00
|
|
|
#include <exception>
|
2016-02-20 23:25:00 +01:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <algorithm>
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2018-07-29 11:25:44 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
#ifdef GetObject
|
|
|
|
#undef GetObject
|
|
|
|
#endif // ef you MS
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
extern bool RegisterAPI(HSQUIRRELVM vm);
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-02-23 04:23:56 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-11-16 23:23:59 +01:00
|
|
|
extern void InitializeTasks();
|
2016-05-22 05:20:38 +02:00
|
|
|
extern void InitializeRoutines();
|
2017-06-19 03:09:35 +02:00
|
|
|
extern void TerminateAreas();
|
2016-11-16 23:23:59 +01:00
|
|
|
extern void TerminateTasks();
|
2016-05-22 05:20:38 +02:00
|
|
|
extern void TerminateRoutines();
|
2016-07-03 02:22:00 +02:00
|
|
|
extern void TerminateCommands();
|
2016-10-30 17:26:37 +01:00
|
|
|
extern void TerminateSignals();
|
2016-02-23 04:23:56 +01:00
|
|
|
|
2016-06-18 19:31:35 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
extern Buffer GetRealFilePath(CSStr path);
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Loader used to process a section from the configuration file and look for scripts to load.
|
|
|
|
*/
|
|
|
|
class ScriptLoader
|
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
CSimpleIniA & m_Config; // The processed configuration.
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
2020-03-22 09:31:43 +01:00
|
|
|
explicit ScriptLoader(CSimpleIniA & conf)
|
2016-06-18 19:31:35 +02:00
|
|
|
: m_Config(conf)
|
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Function call operator.
|
|
|
|
*/
|
|
|
|
bool operator () (CCStr key, CCStr val) const
|
|
|
|
{
|
|
|
|
// Validate the specified key
|
|
|
|
if (!key || *key == '\0')
|
|
|
|
{
|
|
|
|
return true; // Move to the next element!
|
|
|
|
}
|
|
|
|
// Identify the load option
|
|
|
|
if (std::strcmp(key, "Section") == 0)
|
|
|
|
{
|
|
|
|
return m_Config.ProcAllValues(val, ScriptLoader(m_Config));
|
|
|
|
}
|
|
|
|
else if (std::strcmp(key, "Compile") == 0)
|
|
|
|
{
|
|
|
|
return Core::Get().LoadScript(val, true);
|
|
|
|
}
|
|
|
|
else if (std::strcmp(key, "Execute") == 0)
|
|
|
|
{
|
|
|
|
return Core::Get().LoadScript(val, false);
|
|
|
|
}
|
|
|
|
// Move to the next element!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-16 14:45:36 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Implements RAII to make sure that entity containers area cleaned up at all costs.
|
|
|
|
*/
|
2016-07-16 18:39:51 +02:00
|
|
|
class ContainerCleaner
|
2016-07-16 14:45:36 +02:00
|
|
|
{
|
|
|
|
// --------------------------------------------------------------------------------------------
|
2016-07-16 18:39:51 +02:00
|
|
|
EntityType m_Type; // The type of entity container to clear.
|
2016-07-16 14:45:36 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
2016-07-16 18:39:51 +02:00
|
|
|
template < typename T > ContainerCleaner(T & container, EntityType type, bool destroy)
|
2016-07-16 14:45:36 +02:00
|
|
|
: m_Type(type)
|
|
|
|
{
|
|
|
|
for (auto & ent : container)
|
|
|
|
{
|
2017-02-21 20:24:59 +01:00
|
|
|
ent.Destroy(destroy, SQMOD_DESTROY_CLEANUP, NullLightObj());
|
2016-07-16 14:45:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~ContainerCleaner()
|
|
|
|
{
|
|
|
|
Core::Get().ClearContainer(m_Type);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
Core Core::s_Inst;
|
2016-03-24 04:28:55 +01:00
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-03-22 09:31:43 +01:00
|
|
|
Core::Core() noexcept
|
2016-02-20 23:25:00 +01:00
|
|
|
: m_State(0)
|
2016-03-24 05:43:02 +01:00
|
|
|
, m_VM(nullptr)
|
2016-02-20 23:25:00 +01:00
|
|
|
, m_Scripts()
|
2016-08-19 18:46:26 +02:00
|
|
|
, m_PendingScripts()
|
2016-02-20 23:25:00 +01:00
|
|
|
, m_Options()
|
|
|
|
, m_Blips()
|
|
|
|
, m_Checkpoints()
|
|
|
|
, m_Keybinds()
|
|
|
|
, m_Objects()
|
|
|
|
, m_Pickups()
|
|
|
|
, m_Players()
|
|
|
|
, m_Vehicles()
|
2017-02-21 20:24:59 +01:00
|
|
|
, m_Events()
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_CircularLocks(0)
|
2016-05-24 18:26:43 +02:00
|
|
|
, m_ReloadHeader(0)
|
|
|
|
, m_ReloadPayload()
|
2016-05-22 05:20:38 +02:00
|
|
|
, m_IncomingNameBuffer(nullptr)
|
|
|
|
, m_IncomingNameCapacity(0)
|
2017-06-19 03:09:35 +02:00
|
|
|
, m_AreasEnabled(false)
|
2016-06-17 02:33:58 +02:00
|
|
|
, m_Debugging(false)
|
2016-06-18 19:31:35 +02:00
|
|
|
, m_Executed(false)
|
2016-07-16 18:39:51 +02:00
|
|
|
, m_Shutdown(false)
|
2016-08-25 02:08:44 +02:00
|
|
|
, m_LockPreLoadSignal(false)
|
|
|
|
, m_LockPostLoadSignal(false)
|
|
|
|
, m_LockUnloadSignal(false)
|
2016-08-27 11:15:51 +02:00
|
|
|
, m_EmptyInit(false)
|
2016-08-25 11:22:39 +02:00
|
|
|
, m_Verbosity(1)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
/* ... */
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
Core::~Core()
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
if (m_VM)
|
|
|
|
{
|
2016-07-16 16:52:55 +02:00
|
|
|
Terminate(true);
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
bool Core::Initialize()
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the plug-in was not already initialized
|
|
|
|
if (m_VM != nullptr)
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
OutputError("Plug-in was already initialized");
|
2016-02-20 23:25:00 +01:00
|
|
|
return true;
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
CSimpleIniA conf(false, true, true);
|
|
|
|
// Attempt to load the configurations from disk
|
|
|
|
SI_Error ini_ret = conf.LoadFile("sqmod.ini");
|
|
|
|
// See if the configurations could be loaded
|
|
|
|
if (ini_ret < 0)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
switch (ini_ret)
|
|
|
|
{
|
|
|
|
case SI_FAIL:
|
2016-07-16 18:39:51 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
OutputError("Failed to load the configuration file. Probably invalid");
|
2016-07-16 18:39:51 +02:00
|
|
|
} break;
|
2016-02-20 23:25:00 +01:00
|
|
|
case SI_NOMEM:
|
2016-07-16 18:39:51 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
OutputError("Run out of memory while loading the configuration file");
|
2016-07-16 18:39:51 +02:00
|
|
|
} break;
|
2016-02-20 23:25:00 +01:00
|
|
|
case SI_FILE:
|
2016-07-16 18:39:51 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
OutputError("Failed to load the configuration file. %s", std::strerror(errno));
|
2016-07-16 18:39:51 +02:00
|
|
|
} break;
|
|
|
|
default: OutputError("Failed to load the configuration file for some unforeseen reason");
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
// Failed to load the configuration file
|
|
|
|
return false;
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
2015-10-02 00:34:28 +02:00
|
|
|
|
2019-06-16 01:51:16 +02:00
|
|
|
// See if debugging options should be enabled
|
|
|
|
m_Debugging = conf.GetBoolValue("Squirrel", "Debugging", m_Debugging);
|
2016-08-27 11:15:51 +02:00
|
|
|
// Configure the empty initialization
|
|
|
|
m_EmptyInit = conf.GetBoolValue("Squirrel", "EmptyInit", false);
|
2016-08-25 11:22:39 +02:00
|
|
|
// Configure the verbosity level
|
|
|
|
m_Verbosity = conf.GetLongValue("Log", "VerbosityLevel", 1);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Initialize the log filename
|
|
|
|
Logger::Get().SetLogFilename(conf.GetValue("Log", "Filename", nullptr));
|
|
|
|
// Configure the logging timestamps
|
|
|
|
Logger::Get().ToggleConsoleTime(conf.GetBoolValue("Log", "ConsoleTimestamp", false));
|
|
|
|
Logger::Get().ToggleLogFileTime(conf.GetBoolValue("Log", "LogFileTimestamp", true));
|
|
|
|
// Apply the specified logging filters only after initialization was completed
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_DBG, conf.GetBoolValue("Log", "ConsoleDebug", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_USR, conf.GetBoolValue("Log", "ConsoleUser", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_SCS, conf.GetBoolValue("Log", "ConsoleSuccess", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_INF, conf.GetBoolValue("Log", "ConsoleInfo", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_WRN, conf.GetBoolValue("Log", "ConsoleWarning", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_ERR, conf.GetBoolValue("Log", "ConsoleError", true));
|
|
|
|
Logger::Get().ToggleConsoleLevel(LOGL_FTL, conf.GetBoolValue("Log", "ConsoleFatal", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_DBG, conf.GetBoolValue("Log", "LogFileDebug", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_USR, conf.GetBoolValue("Log", "LogFileUser", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_SCS, conf.GetBoolValue("Log", "LogFileSuccess", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_INF, conf.GetBoolValue("Log", "LogFileInfo", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_WRN, conf.GetBoolValue("Log", "LogFileWarning", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_ERR, conf.GetBoolValue("Log", "LogFileError", true));
|
|
|
|
Logger::Get().ToggleLogFileLevel(LOGL_FTL, conf.GetBoolValue("Log", "LogFileFatal", true));
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Resizing the entity containers");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Make sure the entity containers have the proper size
|
|
|
|
m_Blips.resize(SQMOD_BLIP_POOL);
|
|
|
|
m_Checkpoints.resize(SQMOD_CHECKPOINT_POOL);
|
|
|
|
m_Keybinds.resize(SQMOD_KEYBIND_POOL);
|
|
|
|
m_Objects.resize(SQMOD_OBJECT_POOL);
|
|
|
|
m_Pickups.resize(SQMOD_PICKUP_POOL);
|
|
|
|
m_Players.resize(SQMOD_PLAYER_POOL);
|
|
|
|
m_Vehicles.resize(SQMOD_VEHICLE_POOL);
|
|
|
|
|
|
|
|
// Attempt to read the virtual machine stack size
|
|
|
|
const LongI stack_size = conf.GetLongValue("Squirrel", "StackSize", SQMOD_STACK_SIZE);
|
|
|
|
// Make sure that the retrieved number is within range
|
|
|
|
if (!stack_size)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
LogWrn("Invalid virtual machine stack size: %lu", stack_size);
|
|
|
|
// Stop the initialization process
|
|
|
|
return false;
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Creating a virtual machine (%ld stack size)", stack_size);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Attempt to create the script virtual machine
|
|
|
|
m_VM = sq_open(ConvTo< SQInteger >::From(stack_size));
|
2016-03-10 04:57:13 +01:00
|
|
|
// See if the virtual machine could be created
|
2016-02-20 23:25:00 +01:00
|
|
|
if (cLogFtl(!m_VM, "Unable to create the virtual machine"))
|
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return false; // Unable to load the plug-in properly!
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// Set this as the default VM
|
2016-02-20 23:25:00 +01:00
|
|
|
DefaultVM::Set(m_VM);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Configure error handling
|
|
|
|
ErrorHandling::Enable(conf.GetBoolValue("Squirrel", "ErrorHandling", true));
|
2016-02-20 23:25:00 +01:00
|
|
|
|
2016-07-03 02:27:18 +02:00
|
|
|
// Prevent common null objects from using dead virtual machines
|
|
|
|
NullArray() = Array();
|
|
|
|
NullTable() = Table();
|
|
|
|
NullObject() = Object();
|
2017-02-21 20:24:59 +01:00
|
|
|
NullLightObj() = LightObj();
|
2016-07-03 02:27:18 +02:00
|
|
|
NullFunction() = Function();
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Registering the standard libraries");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Push the root table on the stack
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_pushroottable(m_VM);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Register the standard library on the pushed table
|
2016-02-20 23:25:00 +01:00
|
|
|
sqstd_register_iolib(m_VM);
|
|
|
|
sqstd_register_bloblib(m_VM);
|
|
|
|
sqstd_register_mathlib(m_VM);
|
|
|
|
sqstd_register_systemlib(m_VM);
|
|
|
|
sqstd_register_stringlib(m_VM);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Pop the root table from the stack
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_pop(m_VM, 1);
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Setting the script output function");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Tell the VM to use these functions to output user on error messages
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_setprintfunc(m_VM, PrintFunc, ErrorFunc);
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Setting the script error handlers");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Tell the VM to trigger this function on compile time errors
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_setcompilererrorhandler(m_VM, CompilerErrorHandler);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Push the runtime error handler on the stack and create a closure
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_newclosure(m_VM, RuntimeErrorHandler, 0);
|
2016-03-10 04:57:13 +01:00
|
|
|
// Tell the VM to trigger this function on runtime errors
|
2016-02-20 23:25:00 +01:00
|
|
|
sq_seterrorhandler(m_VM);
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Registering the plug-in API");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Attempt to register the plug-in API
|
2016-02-20 23:25:00 +01:00
|
|
|
if (cLogFtl(!RegisterAPI(m_VM), "Unable to register the plug-in API"))
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
return false; // Can't execute scripts without a valid API!
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
|
2017-02-21 20:24:59 +01:00
|
|
|
// Initialize the module global events
|
|
|
|
InitEvents();
|
|
|
|
|
|
|
|
// Initialize load stage signals
|
|
|
|
InitSignalPair(mOnPreLoad, NullLightObj(), nullptr);
|
|
|
|
InitSignalPair(mOnPostLoad, NullLightObj(), nullptr);
|
|
|
|
InitSignalPair(mOnUnload, NullLightObj(), nullptr);
|
|
|
|
|
2016-06-18 19:31:35 +02:00
|
|
|
CSimpleIniA::TNamesDepend scripts;
|
|
|
|
// Attempt to retrieve the list of keys to make sure there's actually something to process
|
|
|
|
if (conf.GetAllKeys("Scripts", scripts) && scripts.size() > 0)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-06-18 19:31:35 +02:00
|
|
|
// Attempt to load the specified scripts
|
|
|
|
if (!conf.ProcAllValues("Scripts", ScriptLoader(conf)))
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-06-18 19:31:35 +02:00
|
|
|
LogErr("Unable to load the specified scripts");
|
|
|
|
// Either no script was found or failed to load
|
|
|
|
return false;
|
2016-06-17 00:40:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// See if any script could be queued for loading
|
2016-08-27 11:15:51 +02:00
|
|
|
if (m_PendingScripts.empty() && !m_EmptyInit)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
LogErr("No scripts loaded. No reason to load the plug-in");
|
2016-02-20 23:25:00 +01:00
|
|
|
// No point in loading the plug-in
|
2015-09-30 02:56:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Reading the options from the general section");
|
2016-03-10 04:57:13 +01:00
|
|
|
// Read options only after loading was successful
|
2016-02-20 23:25:00 +01:00
|
|
|
CSimpleIniA::TNamesDepend options;
|
2016-03-10 04:57:13 +01:00
|
|
|
// Are there any options to read?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (conf.GetAllKeys("Options", options) || options.size() > 0)
|
|
|
|
{
|
2019-08-17 14:52:04 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Found (%" PRINT_SZ_FMT ") options in the configuration file",
|
|
|
|
static_cast< SQUnsignedInteger >(options.size()));
|
2016-02-20 23:25:00 +01:00
|
|
|
// Process all the specified keys under the [Options] section
|
2016-03-10 04:57:13 +01:00
|
|
|
for (const auto & option : options)
|
2016-02-20 23:25:00 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
CSimpleIniA::TNamesDepend values;
|
|
|
|
// Get the values of all keys with the same name
|
|
|
|
if (!conf.GetAllValues("Options", option.pItem, values))
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
continue;
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
// Sort the keys in their original order
|
2016-03-10 04:57:13 +01:00
|
|
|
values.sort(CSimpleIniA::Entry::LoadOrder());
|
|
|
|
// Save each option option and overwrite existing value
|
|
|
|
for (const auto & value : values)
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
m_Options[option.pItem] = value.pItem;
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
|
|
|
}
|
2015-09-30 02:56:11 +02:00
|
|
|
|
2016-11-16 23:23:59 +01:00
|
|
|
// Initialize routines and tasks
|
2016-05-22 05:20:38 +02:00
|
|
|
InitializeRoutines();
|
2016-11-16 23:23:59 +01:00
|
|
|
InitializeTasks();
|
2016-02-20 23:25:00 +01:00
|
|
|
|
|
|
|
// Initialization successful
|
2015-09-30 02:56:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-22 05:20:38 +02:00
|
|
|
bool Core::Execute()
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
// Are there any scripts to execute?
|
2016-08-27 11:15:51 +02:00
|
|
|
if (m_PendingScripts.empty())
|
2016-05-22 05:20:38 +02:00
|
|
|
{
|
2016-08-27 11:15:51 +02:00
|
|
|
// Are we allowed to continue without any scripts?
|
|
|
|
if (m_EmptyInit)
|
|
|
|
{
|
|
|
|
LogWrn("No scripts to execute. Empty initialization was forced");
|
|
|
|
// Allow empty initialization since it was requested
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
LogWrn("No scripts to execute. Plug-in has no purpose");
|
|
|
|
// No reason to execute the plug-in
|
|
|
|
return false;
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
2016-03-10 04:57:13 +01:00
|
|
|
|
2016-08-25 02:08:44 +02:00
|
|
|
// Unlock signal containers
|
|
|
|
m_LockPreLoadSignal = false;
|
|
|
|
m_LockPostLoadSignal = false;
|
|
|
|
m_LockUnloadSignal = false;
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to register their API");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Tell modules to do their monkey business
|
2016-07-17 02:56:52 +02:00
|
|
|
_Func->SendPluginCommand(SQMOD_LOAD_CMD, "");
|
2016-02-27 10:57:10 +01:00
|
|
|
|
2016-08-19 18:46:26 +02:00
|
|
|
// Load pending scripts while we're in the bounds of the allowed recursiveness
|
|
|
|
for (unsigned levels = 0; (m_PendingScripts.empty() == false) && (levels < 8); ++levels)
|
2016-06-18 12:09:08 +02:00
|
|
|
{
|
2016-08-19 18:46:26 +02:00
|
|
|
// Remember the last script from the pool
|
|
|
|
const Scripts::size_type last = m_Scripts.size();
|
|
|
|
// Push pending scripts to the back of the list
|
|
|
|
std::move(m_PendingScripts.begin(), m_PendingScripts.end(), std::back_inserter(m_Scripts));
|
|
|
|
// Clear all pending scripts, if any
|
|
|
|
m_PendingScripts.clear();
|
|
|
|
// Process all pending scripts
|
|
|
|
if (DoScripts(m_Scripts.begin() + last, m_Scripts.end()) == false)
|
2016-06-18 19:31:35 +02:00
|
|
|
{
|
2016-08-19 18:46:26 +02:00
|
|
|
return false; // One of the scripts failed to execute
|
2016-03-10 04:57:13 +01:00
|
|
|
}
|
2016-09-11 19:48:20 +02:00
|
|
|
|
2019-08-17 14:43:50 +02:00
|
|
|
cLogDbg(m_Verbosity >= 2, "Completed execution of stage (%u) scripts. Pending scripts %" PRINT_SZ_FMT,
|
2019-08-17 14:52:04 +02:00
|
|
|
levels, static_cast< SQUnsignedInteger >(m_PendingScripts.size()));
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2020-03-21 12:54:38 +01:00
|
|
|
// Force enable null entities if not already enabled by script
|
|
|
|
EnableNullEntities();
|
2016-03-24 07:44:01 +01:00
|
|
|
|
2016-08-25 02:08:44 +02:00
|
|
|
m_LockPreLoadSignal = true;
|
2017-02-21 20:24:59 +01:00
|
|
|
// Trigger callbacks that must initialize stuff before the loaded event is triggered
|
|
|
|
(*mOnPreLoad.first)();
|
|
|
|
// Clear the callbacks
|
|
|
|
ResetSignalPair(mOnPreLoad);
|
2016-08-25 02:08:44 +02:00
|
|
|
|
2016-06-26 15:30:19 +02:00
|
|
|
// Notify the script callback that the scripts were loaded
|
|
|
|
EmitScriptLoaded();
|
|
|
|
|
2016-08-25 02:08:44 +02:00
|
|
|
m_LockPostLoadSignal = true;
|
2017-02-21 20:24:59 +01:00
|
|
|
// Trigger callbacks that must initialize stuff after the loaded event is triggered
|
|
|
|
(*mOnPostLoad.first)();
|
|
|
|
// Clear the callbacks
|
|
|
|
ResetSignalPair(mOnPostLoad);
|
2016-08-25 02:08:44 +02:00
|
|
|
|
2016-03-24 07:44:01 +01:00
|
|
|
// Import already existing entities
|
|
|
|
ImportPlayers();
|
|
|
|
ImportBlips();
|
|
|
|
ImportCheckpoints();
|
|
|
|
ImportKeybinds();
|
|
|
|
ImportObjects();
|
|
|
|
ImportPickups();
|
|
|
|
ImportVehicles();
|
2016-05-22 05:20:38 +02:00
|
|
|
// Successfully executed
|
2016-06-18 19:31:35 +02:00
|
|
|
return (m_Executed = true);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-07-16 16:52:55 +02:00
|
|
|
void Core::Terminate(bool shutdown)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-07-16 18:39:51 +02:00
|
|
|
m_Shutdown = shutdown;
|
|
|
|
// Is there a virtual machine present?
|
2016-05-22 05:20:38 +02:00
|
|
|
if (m_VM)
|
|
|
|
{
|
2016-08-25 02:08:44 +02:00
|
|
|
m_LockUnloadSignal = true;
|
2017-02-21 20:24:59 +01:00
|
|
|
// Trigger callbacks that must de-initialize stuff before the scripts are unloaded
|
|
|
|
(*mOnUnload.first)();
|
|
|
|
// Clear the callbacks
|
|
|
|
ResetSignalPair(mOnUnload);
|
2016-08-25 02:08:44 +02:00
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to release their resources");
|
2016-05-22 05:20:38 +02:00
|
|
|
// Tell modules to do their monkey business
|
2016-07-17 02:56:52 +02:00
|
|
|
_Func->SendPluginCommand(SQMOD_TERMINATE_CMD, "");
|
2016-05-22 05:20:38 +02:00
|
|
|
}
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Clearing the entity containers");
|
2016-02-20 23:25:00 +01:00
|
|
|
// Release all entity resources by clearing the containers
|
2016-07-16 18:39:51 +02:00
|
|
|
const ContainerCleaner cc_players(m_Players, ENT_PLAYER, !shutdown);
|
|
|
|
const ContainerCleaner cc_vehicles(m_Vehicles, ENT_VEHICLE, !shutdown);
|
|
|
|
const ContainerCleaner cc_objects(m_Objects, ENT_OBJECT, !shutdown);
|
|
|
|
const ContainerCleaner cc_pickups(m_Pickups, ENT_PICKUP, !shutdown);
|
|
|
|
const ContainerCleaner cc_checkpoints(m_Checkpoints, ENT_CHECKPOINT, !shutdown);
|
|
|
|
const ContainerCleaner cc_blips(m_Blips, ENT_BLIP, !shutdown);
|
|
|
|
const ContainerCleaner cc_keybinds(m_Keybinds, ENT_KEYBIND, !shutdown);
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Terminating routines an commands");
|
2016-11-16 23:23:59 +01:00
|
|
|
// Release all resources from routines and tasks
|
2016-05-22 05:20:38 +02:00
|
|
|
TerminateRoutines();
|
2016-11-16 23:23:59 +01:00
|
|
|
TerminateTasks();
|
2016-07-03 02:22:00 +02:00
|
|
|
// Release all resources from command managers
|
|
|
|
TerminateCommands();
|
2016-10-30 17:26:37 +01:00
|
|
|
// Release all resources from signals
|
|
|
|
TerminateSignals();
|
2017-06-19 03:09:35 +02:00
|
|
|
// Release all managed areas
|
|
|
|
TerminateAreas();
|
2016-03-24 08:32:44 +01:00
|
|
|
// In case there's a payload for reload
|
2016-05-24 18:26:43 +02:00
|
|
|
m_ReloadPayload.Release();
|
2016-05-22 05:20:38 +02:00
|
|
|
// Release null objects in case any reference to valid objects is stored in them
|
|
|
|
NullArray().Release();
|
|
|
|
NullTable().Release();
|
|
|
|
NullObject().Release();
|
2017-02-21 20:24:59 +01:00
|
|
|
NullLightObj().Release();
|
2016-05-22 05:20:38 +02:00
|
|
|
NullFunction().ReleaseGently();
|
2016-08-07 00:54:33 +02:00
|
|
|
// Release null entity instances
|
|
|
|
m_NullBlip.Release();
|
|
|
|
m_NullCheckpoint.Release();
|
|
|
|
m_NullKeybind.Release();
|
|
|
|
m_NullObject.Release();
|
|
|
|
m_NullPickup.Release();
|
|
|
|
m_NullPlayer.Release();
|
|
|
|
m_NullVehicle.Release();
|
2016-02-20 23:25:00 +01:00
|
|
|
// Is there a VM to close?
|
|
|
|
if (m_VM)
|
|
|
|
{
|
2016-09-04 00:27:00 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Releasing any final resources and all loaded scripts");
|
2016-02-20 23:25:00 +01:00
|
|
|
// Release all script callbacks
|
2017-02-21 20:24:59 +01:00
|
|
|
DropEvents();
|
2016-02-20 23:25:00 +01:00
|
|
|
// Release the script instances
|
|
|
|
m_Scripts.clear();
|
2016-08-19 18:46:26 +02:00
|
|
|
m_PendingScripts.clear(); // Just in case
|
2016-06-18 19:31:35 +02:00
|
|
|
// Specify that no scripts are left executed
|
|
|
|
m_Executed = false;
|
2016-05-22 05:20:38 +02:00
|
|
|
// Assertions during close may cause double delete/close!
|
2016-02-20 23:25:00 +01:00
|
|
|
HSQUIRRELVM sq_vm = m_VM;
|
2016-03-24 05:43:02 +01:00
|
|
|
m_VM = nullptr;
|
2016-09-02 13:11:39 +02:00
|
|
|
|
|
|
|
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins the virtual machine is closing");
|
|
|
|
// Tell modules to do their monkey business
|
|
|
|
_Func->SendPluginCommand(SQMOD_CLOSING_CMD, "");
|
2019-05-01 17:00:24 +02:00
|
|
|
// Release any callbacks from the logger
|
|
|
|
Logger::Get().Release();
|
2016-02-20 23:25:00 +01:00
|
|
|
// Attempt to close the VM
|
|
|
|
sq_close(sq_vm);
|
2016-06-26 15:18:23 +02:00
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(m_Verbosity >= 1, "Signaling outside plug-ins to release the virtual machine");
|
2016-06-26 15:18:23 +02:00
|
|
|
// Tell modules to do their monkey business
|
2016-07-17 02:56:52 +02:00
|
|
|
_Func->SendPluginCommand(SQMOD_RELEASED_CMD, "");
|
2016-02-20 23:25:00 +01:00
|
|
|
}
|
2016-07-16 14:45:36 +02:00
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
OutputMessage("Squirrel plug-in was successfully terminated");
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-05-24 05:51:40 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-05-24 18:26:43 +02:00
|
|
|
bool Core::Reload()
|
2016-05-24 05:51:40 +02:00
|
|
|
{
|
|
|
|
// Are we already reloading?
|
|
|
|
if (m_CircularLocks & CCL_RELOAD_SCRIPTS)
|
|
|
|
{
|
|
|
|
return false; // Already reloading!
|
|
|
|
}
|
2016-06-03 20:26:19 +02:00
|
|
|
// Prevent circular reloads when we send plug-in commands
|
2016-05-24 05:51:40 +02:00
|
|
|
const BitGuardU32 bg(m_CircularLocks, static_cast< Uint32 >(CCL_RELOAD_SCRIPTS));
|
|
|
|
// Allow reloading by default
|
|
|
|
Core::Get().SetState(1);
|
|
|
|
// Emit the reload event
|
2016-05-24 18:26:43 +02:00
|
|
|
Core::Get().EmitScriptReload(m_ReloadHeader, m_ReloadPayload);
|
2016-05-24 05:51:40 +02:00
|
|
|
// Are we allowed to reload?
|
|
|
|
if (!Core::Get().GetState())
|
|
|
|
{
|
|
|
|
return false; // Request denied!
|
|
|
|
}
|
|
|
|
// Terminate the current VM and release resources
|
2016-07-16 16:52:55 +02:00
|
|
|
Terminate(false);
|
2016-05-24 18:26:43 +02:00
|
|
|
// Reset the reload header after termination
|
|
|
|
m_ReloadHeader = -1;
|
|
|
|
// Attempt to initialize the central core and load resources
|
|
|
|
return (Initialize() && Execute());
|
2016-05-24 05:51:40 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 12:54:38 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Core::EnableNullEntities()
|
|
|
|
{
|
|
|
|
// Create the null entity instances
|
|
|
|
if (m_NullBlip.IsNull()) m_NullBlip = LightObj(new CBlip(-1));
|
|
|
|
if (m_NullCheckpoint.IsNull()) m_NullBlip = LightObj(new CCheckpoint(-1));
|
|
|
|
if (m_NullKeybind.IsNull()) m_NullBlip = LightObj(new CKeybind(-1));
|
|
|
|
if (m_NullObject.IsNull()) m_NullBlip = LightObj(new CObject(-1));
|
|
|
|
if (m_NullPickup.IsNull()) m_NullBlip = LightObj(new CPickup(-1));
|
|
|
|
if (m_NullPlayer.IsNull()) m_NullBlip = LightObj(new CPlayer(-1));
|
|
|
|
if (m_NullVehicle.IsNull()) m_NullBlip = LightObj(new CVehicle(-1));
|
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-11 19:04:26 +01:00
|
|
|
CSStr Core::GetOption(CSStr name) const
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
Options::const_iterator elem = m_Options.find(name);
|
2016-03-11 19:04:26 +01:00
|
|
|
return (elem == m_Options.end()) ? _SC("") : elem->second.c_str();
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 19:04:26 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
CSStr Core::GetOption(CSStr name, CSStr value) const
|
|
|
|
{
|
|
|
|
Options::const_iterator elem = m_Options.find(name);
|
|
|
|
return (elem == m_Options.end()) ? value : elem->second.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Core::SetOption(CSStr name, CSStr value)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-02-20 23:25:00 +01:00
|
|
|
m_Options[name] = value;
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2019-06-16 01:12:51 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Core::Scripts::iterator Core::FindScript(const CSStr src)
|
|
|
|
{
|
|
|
|
// Iterate over loaded scripts
|
|
|
|
for (Scripts::iterator itr = m_Scripts.begin(); itr != m_Scripts.end(); ++itr)
|
|
|
|
{
|
|
|
|
// Is this script the source we're looking for?
|
|
|
|
if (itr->mPath.compare(0, String::npos, src) == 0)
|
|
|
|
{
|
|
|
|
return itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not found!
|
|
|
|
return m_Scripts.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
Core::Scripts::iterator Core::FindPendingScript(const CSStr src)
|
|
|
|
{
|
|
|
|
// Iterate over loaded scripts
|
|
|
|
for (Scripts::iterator itr = m_PendingScripts.begin(); itr != m_PendingScripts.end(); ++itr)
|
|
|
|
{
|
|
|
|
// Is this script the source we're looking for?
|
|
|
|
if (itr->mPath.compare(0, String::npos, src) == 0)
|
|
|
|
{
|
|
|
|
return itr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Not found!
|
|
|
|
return m_PendingScripts.end();
|
|
|
|
}
|
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-18 19:31:35 +02:00
|
|
|
bool Core::LoadScript(CSStr filepath, bool delay)
|
2016-03-10 04:57:13 +01:00
|
|
|
{
|
|
|
|
// Is the specified path empty?
|
2016-05-22 05:20:38 +02:00
|
|
|
if (!filepath || *filepath == '\0')
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-06-18 19:31:35 +02:00
|
|
|
LogErr("Cannot load script with empty or invalid path");
|
|
|
|
// Failed to load
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-19 18:46:26 +02:00
|
|
|
|
2016-06-18 19:31:35 +02:00
|
|
|
Buffer bpath;
|
|
|
|
// Attempt to get the real file path
|
|
|
|
try
|
|
|
|
{
|
|
|
|
bpath = GetRealFilePath(filepath);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
2016-07-09 13:18:09 +02:00
|
|
|
LogErr("Unable to load script: %s", e.what());
|
2016-06-18 19:31:35 +02:00
|
|
|
// Failed to load
|
|
|
|
return false;
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-06-18 19:31:35 +02:00
|
|
|
catch (const std::exception & e)
|
|
|
|
{
|
|
|
|
LogErr("Unable to load script: %s", e.what());
|
|
|
|
// Failed to load
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-19 18:46:26 +02:00
|
|
|
|
2016-06-18 19:31:35 +02:00
|
|
|
// Make the path into a string
|
|
|
|
String path(bpath.Data(), bpath.Position());
|
2016-08-19 18:46:26 +02:00
|
|
|
|
2016-03-10 04:57:13 +01:00
|
|
|
// See if it wasn't already loaded
|
2016-06-17 02:28:37 +02:00
|
|
|
if (std::find_if(m_Scripts.cbegin(), m_Scripts.cend(), [&path](Scripts::const_reference s) {
|
|
|
|
return (s.mPath == path);
|
|
|
|
}) != m_Scripts.end())
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-03-10 04:57:13 +01:00
|
|
|
LogWrn("Script was specified before: %s", path.c_str());
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-08-19 18:46:26 +02:00
|
|
|
// Also check the pending scripts container
|
|
|
|
else if (std::find_if(m_PendingScripts.cbegin(), m_PendingScripts.cend(), [&path](Scripts::const_reference s) {
|
|
|
|
return (s.mPath == path);
|
|
|
|
}) != m_PendingScripts.end())
|
|
|
|
{
|
|
|
|
LogWrn("Script was specified before: %s", path.c_str());
|
|
|
|
}
|
2016-06-18 19:31:35 +02:00
|
|
|
// Were the scripts already executed? Then there's no need to queue them
|
|
|
|
else if (m_Executed)
|
|
|
|
{
|
|
|
|
// Create a new script container and insert it into the script pool
|
|
|
|
m_Scripts.emplace_back(m_VM, std::move(path), delay, m_Debugging);
|
|
|
|
|
|
|
|
// Attempt to load and compile the script file
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Scripts.back().mExec.CompileFile(m_Scripts.back().mPath);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
LogFtl("Unable to compile: %s", m_Scripts.back().mPath.c_str());
|
2016-08-19 18:46:26 +02:00
|
|
|
// Remove the script container since it's invalid
|
|
|
|
m_Scripts.pop_back();
|
|
|
|
// Failed to compile properly
|
2016-06-18 19:31:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// At this point the script should be completely loaded
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogDbg(m_Verbosity >= 3, "Compiled script: %s", m_Scripts.back().mPath.c_str());
|
2016-06-18 19:31:35 +02:00
|
|
|
|
|
|
|
// Attempt to execute the compiled script code
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_Scripts.back().mExec.Run();
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
LogFtl("Unable to execute: %s", m_Scripts.back().mPath.c_str());
|
2016-08-19 18:46:26 +02:00
|
|
|
// Remove the script container since it's invalid
|
|
|
|
m_Scripts.pop_back();
|
2016-06-18 19:31:35 +02:00
|
|
|
// Failed to execute properly
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// At this point the script should be completely loaded
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogScs(m_Verbosity >= 2, "Executed script: %s", m_Scripts.back().mPath.c_str());
|
2016-06-18 19:31:35 +02:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// We don't compile the scripts yet. We just store their path and prepare the objects
|
2016-03-10 04:57:13 +01:00
|
|
|
else
|
2016-03-26 17:16:01 +01:00
|
|
|
{
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogDbg(m_Verbosity >= 2, "Pending %s script: %s", (delay ? "deferred" : "immediate"), path.c_str());
|
|
|
|
|
2016-08-19 18:46:26 +02:00
|
|
|
// Create a new script container and insert it into the pending script pool
|
|
|
|
m_PendingScripts.emplace_back(m_VM, std::move(path), delay, m_Debugging);
|
2016-03-26 17:16:01 +01:00
|
|
|
}
|
2016-08-19 18:46:26 +02:00
|
|
|
|
|
|
|
// At this point the script exists in one of the pools
|
2016-03-10 04:57:13 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-22 05:20:38 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void Core::SetIncomingName(CSStr name)
|
|
|
|
{
|
|
|
|
// Is there an incoming connection buffer that we can write to?
|
|
|
|
if (!m_IncomingNameBuffer)
|
|
|
|
{
|
|
|
|
STHROWF("No incoming player name buffer available");
|
|
|
|
}
|
|
|
|
// Is the specified name valid?
|
|
|
|
else if (!name || *name == '\0')
|
|
|
|
{
|
|
|
|
STHROWF("Invalid player name for incoming connection");
|
|
|
|
}
|
|
|
|
// Calculate the length of the specified name
|
|
|
|
const size_t len = std::strlen(name);
|
|
|
|
// Is the length of the name out of bounds?
|
|
|
|
if (len > m_IncomingNameCapacity)
|
|
|
|
{
|
|
|
|
STHROWF("The specified name exceeds the designated buffer");
|
|
|
|
}
|
|
|
|
// Does the name satisfy the minimum length required?
|
|
|
|
else if (len < 2)
|
|
|
|
{
|
|
|
|
STHROWF("The specified name needs to be at least 2 characters: %zu", len);
|
|
|
|
}
|
|
|
|
// Copy the specified name to the assigned buffer
|
|
|
|
std::strncpy(m_IncomingNameBuffer, name, m_IncomingNameCapacity);
|
|
|
|
// Make sure that the name inside the buffer is null terminated
|
|
|
|
m_IncomingNameBuffer[len] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-06-16 01:51:16 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
String Core::FetchCodeLine(CSStr src, SQInteger line, bool trim)
|
|
|
|
{
|
|
|
|
// Find the script we're looking for
|
|
|
|
Scripts::iterator script = FindScript(src);
|
|
|
|
// Do we have a valid script and line?
|
2019-08-17 14:43:50 +02:00
|
|
|
if ((script == m_Scripts.end()) || !(script->mInfo) || (static_cast< SQInteger >(script->mLine.size()) < line))
|
2019-06-16 01:51:16 +02:00
|
|
|
{
|
|
|
|
return String{}; // No such script!
|
|
|
|
}
|
|
|
|
// Fetch the line of code
|
|
|
|
return script->FetchLine(line, trim);
|
|
|
|
}
|
|
|
|
|
2016-08-19 18:46:26 +02:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Core::DoScripts(Scripts::iterator itr, Scripts::iterator end)
|
|
|
|
{
|
|
|
|
Scripts::iterator itr_state = itr;
|
|
|
|
|
2016-08-25 11:22:39 +02:00
|
|
|
cLogDbg(Get().m_Verbosity >= 1, "Attempting to compile the specified scripts");
|
2016-08-19 18:46:26 +02:00
|
|
|
// Compile scripts first so that the constants can take effect
|
|
|
|
for (; itr != end; ++itr)
|
|
|
|
{
|
|
|
|
// Is this script already compiled?
|
|
|
|
if (!(*itr).mExec.IsNull())
|
|
|
|
{
|
|
|
|
continue; // Already compiled!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to load and compile the script file
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*itr).mExec.CompileFile((*itr).mPath);
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
LogFtl("Unable to compile: %s", (*itr).mPath.c_str());
|
|
|
|
// Failed to execute properly
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogDbg(Get().m_Verbosity >= 3, "Compiled script: %s", (*itr).mPath.c_str());
|
2016-08-19 18:46:26 +02:00
|
|
|
|
|
|
|
// Should we delay the execution of this script?
|
|
|
|
if ((*itr).mDelay)
|
|
|
|
{
|
|
|
|
continue; // Execute later!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to execute the compiled script code
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*itr).mExec.Run();
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
LogFtl("Unable to execute: %s", (*itr).mPath.c_str());
|
|
|
|
// Failed to execute properly
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogScs(Get().m_Verbosity >= 2, "Executed script: %s", (*itr).mPath.c_str());
|
2016-08-19 18:46:26 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogDbg(Get().m_Verbosity >= 1, "Attempting to execute the delayed scripts");
|
2016-08-19 18:46:26 +02:00
|
|
|
// Execute scripts only after compilation successful
|
|
|
|
for (itr = itr_state; itr != end; ++itr)
|
|
|
|
{
|
|
|
|
// Was this script delayed from execution?
|
|
|
|
if (!(*itr).mDelay)
|
|
|
|
{
|
|
|
|
continue; // Already executed!
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to execute the compiled script code
|
|
|
|
try
|
|
|
|
{
|
|
|
|
(*itr).mExec.Run();
|
|
|
|
}
|
|
|
|
catch (const Sqrat::Exception & e)
|
|
|
|
{
|
|
|
|
LogFtl("Unable to execute: %s", (*itr).mPath.c_str());
|
|
|
|
// Failed to execute properly
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-09-11 19:48:20 +02:00
|
|
|
cLogScs(Get().m_Verbosity >= 2, "Executed script: %s", (*itr).mPath.c_str());
|
2016-08-19 18:46:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// At this point the scripts were loaded and executed successfully
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-03-22 03:27:11 +01:00
|
|
|
void Core::PrintFunc(HSQUIRRELVM /*vm*/, CSStr msg, ...)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Initialize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Forward the message to the logger
|
|
|
|
Logger::Get().Send(LOGL_USR, false, msg, args);
|
|
|
|
// Finalize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_end(args);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void Core::ErrorFunc(HSQUIRRELVM /*vm*/, CSStr msg, ...)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Initialize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_list args;
|
|
|
|
va_start(args, msg);
|
2016-05-22 05:20:38 +02:00
|
|
|
// Tell the logger to display debugging information
|
|
|
|
Logger::Get().Debug(msg, args);
|
|
|
|
// Finalize the variable argument list
|
2016-02-20 23:25:00 +01:00
|
|
|
va_end(args);
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
2016-02-20 23:25:00 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQInteger Core::RuntimeErrorHandler(HSQUIRRELVM vm)
|
2015-09-30 02:56:11 +02:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
// Was there a value thrown?
|
2016-02-20 23:25:00 +01:00
|
|
|
if (sq_gettop(vm) < 1)
|
2016-03-22 03:27:11 +01:00
|
|
|
{
|
2016-05-22 05:20:38 +02:00
|
|
|
return SQ_OK; // No error to display!
|
2016-03-22 03:27:11 +01:00
|
|
|
}
|
2019-05-01 15:04:29 +02:00
|
|
|
// Attempt to generate the string value
|
|
|
|
StackStrF val(vm, 2);
|
|
|
|
// Have we failed to retrieve the string?
|
|
|
|
if (SQ_FAILED(val.Proc(false)))
|
2016-03-22 03:27:11 +01:00
|
|
|
{
|
2019-05-01 15:04:29 +02:00
|
|
|
Logger::Get().Debug(_SC("Unknown runtime error has occurred"));
|
2016-03-22 03:27:11 +01:00
|
|
|
}
|
2016-02-20 23:25:00 +01:00
|
|
|
else
|
2016-03-22 03:27:11 +01:00
|
|
|
{
|
2019-05-01 15:04:29 +02:00
|
|
|
Logger::Get().Debug(_SC("%s"), val.mPtr);
|
2016-03-22 03:27:11 +01:00
|
|
|
}
|
2016-05-22 05:20:38 +02:00
|
|
|
// We handled the error
|
2016-02-20 23:25:00 +01:00
|
|
|
return SQ_OK;
|
2015-09-30 02:56:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-02-20 23:25:00 +01:00
|
|
|
void Core::CompilerErrorHandler(HSQUIRRELVM /*vm*/, CSStr desc, CSStr src, SQInteger line, SQInteger column)
|
2015-11-07 11:17:39 +01:00
|
|
|
{
|
2019-06-16 01:12:51 +02:00
|
|
|
// Should we include code in output? (we count lines from 0, squirrel counts from 1)
|
|
|
|
if ((line <= 0) || !Core::Get().IsDebugging() || !Core::Get().CompilerErrorHandlerEx(desc, src, --line, column)) {
|
|
|
|
LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_INT_FMT "\n=>Column: %" PRINT_INT_FMT "\n]", desc, src, line, column);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
bool Core::CompilerErrorHandlerEx(CSStr desc, CSStr src, SQInteger line, SQInteger column)
|
|
|
|
{
|
2019-06-16 01:51:16 +02:00
|
|
|
// Grab the associated line of code
|
|
|
|
String code = FetchCodeLine(src, line, true);
|
2019-06-16 02:22:53 +02:00
|
|
|
// Contains an arrow pointing at the exact column
|
|
|
|
String point;
|
|
|
|
// Do we have a valid column?
|
|
|
|
if (column >= 0)
|
|
|
|
{
|
|
|
|
// The `=>Code: ` is included as white space
|
|
|
|
point.resize(8, ' ');
|
|
|
|
// The line towards the arrow
|
|
|
|
point.resize(column+7, '-');
|
|
|
|
// Append the actual arrow
|
|
|
|
point.push_back('^');
|
|
|
|
// Include the new line here
|
|
|
|
point.push_back('\n');
|
|
|
|
}
|
2019-06-16 01:51:16 +02:00
|
|
|
// Valid line of code?
|
|
|
|
if (!code.empty())
|
2019-06-16 01:12:51 +02:00
|
|
|
{
|
2019-06-16 01:51:16 +02:00
|
|
|
// Display the error message with the code included
|
2019-06-16 02:22:53 +02:00
|
|
|
LogFtl("Message: %s\n[\n=>Location: %s\n=>Line: %" PRINT_SZ_FMT "\n=>Column: %" PRINT_INT_FMT "\n=>Code: %s\n%s]",
|
|
|
|
desc, src, ++line, column, code.c_str(), point.c_str());
|
2019-06-16 01:51:16 +02:00
|
|
|
// We displayed the information
|
|
|
|
return true;
|
2019-06-16 01:12:51 +02:00
|
|
|
}
|
2019-06-16 01:51:16 +02:00
|
|
|
// No code to show. Fall back to normal message
|
2019-06-16 01:12:51 +02:00
|
|
|
return true;
|
2015-11-07 11:17:39 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 02:56:11 +02:00
|
|
|
} // Namespace:: SqMod
|
2018-07-29 11:25:44 +02:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Include remaining functionality.
|
|
|
|
*/
|
|
|
|
#include "Core/Inst.inc"
|
|
|
|
#include "Core/Entity.inc"
|
|
|
|
#include "Core/Events.inc"
|
|
|
|
#include "Core/Utils.inc"
|
|
|
|
#include "Core/Funcs.inc"
|