aboutsummaryrefslogtreecommitdiff
path: root/NorthstarDLL/squirrel.cpp
diff options
context:
space:
mode:
authorEmma Miler <emma.pi@protonmail.com>2022-11-12 15:39:39 +0100
committerGitHub <noreply@github.com>2022-11-12 14:39:39 +0000
commitbf09852285941e20a04731443d03c693e3f8ba2d (patch)
treea254973af94d819ab18b969e23045a309cbd15f7 /NorthstarDLL/squirrel.cpp
parentab5db7fde23ef6cd87fa8a7e07973179a80e02d4 (diff)
downloadNorthstarLauncher-bf09852285941e20a04731443d03c693e3f8ba2d.tar.gz
NorthstarLauncher-bf09852285941e20a04731443d03c693e3f8ba2d.zip
Logging improvements + Colours (new) (#320)
* log colours :) * commend the freopen stuff (it causes problems) * formatting * add explanatory comment * gaming maybe * move set pattern * ok this works now i think * trailing space :) * Revert "Merge branch 'main' into log-colours" This reverts commit d40b6496b7bd3b3f20f51cc2311b0369aeabe735, reversing changes made to a98bcc22e8961b325806f6a9ca85e64353fd574c. * Revert "Revert "Merge branch 'main' into log-colours"" This reverts commit da792245b1cd7d0ac9e22d5633770142a43ce10c. * fix shit maybe * fix shit but better * initial, still WIP * Add GetContextName_Short * colours for in game console * working, time to improve code * rename var + add a colour or two * whoops * change rpak filesystem to RP NATIVE * add RP NATIVE and NORTHSTAR tags * clang-format off because formatting * formatting 2 * formatting 3: the formattening * bruh * revert unnecessary change * improve comment * Set console title to show profile * pain * Made code not shit * Formatting and remove warning box * Update main.cpp * Formatting * More format changes * Again * Fix small things from code review * Remove empty line * renamed NATIVE_EN -> NATIVE_ENGINE * Update logging.cpp * Update sourceconsole.cpp Co-authored-by: ASpoonPlaysGames <66967891+ASpoonPlaysGames@users.noreply.github.com>
Diffstat (limited to 'NorthstarDLL/squirrel.cpp')
-rw-r--r--NorthstarDLL/squirrel.cpp58
1 files changed, 54 insertions, 4 deletions
diff --git a/NorthstarDLL/squirrel.cpp b/NorthstarDLL/squirrel.cpp
index be0dceeb..c90b2d7f 100644
--- a/NorthstarDLL/squirrel.cpp
+++ b/NorthstarDLL/squirrel.cpp
@@ -8,6 +8,35 @@
AUTOHOOK_INIT()
+std::shared_ptr<ColoredLogger> getSquirrelLoggerByContext(ScriptContext context)
+{
+ switch (context)
+ {
+ case ScriptContext::UI:
+ return NS::log::SCRIPT_UI;
+ case ScriptContext::CLIENT:
+ return NS::log::SCRIPT_CL;
+ case ScriptContext::SERVER:
+ return NS::log::SCRIPT_SV;
+ default:
+ throw std::runtime_error("getSquirrelLoggerByContext called with invalid context");
+ return nullptr;
+ }
+}
+
+namespace NS::log
+{
+ template <ScriptContext context> std::shared_ptr<spdlog::logger> squirrel_logger()
+ {
+ // Switch statements can't be constexpr afaik
+ // clang-format off
+ if constexpr (context == ScriptContext::UI) { return SCRIPT_UI; }
+ if constexpr (context == ScriptContext::CLIENT) { return SCRIPT_CL; }
+ if constexpr (context == ScriptContext::SERVER) { return SCRIPT_SV; }
+ // clang-format on
+ }
+}; // namespace NS::log
+
const char* GetContextName(ScriptContext context)
{
switch (context)
@@ -23,6 +52,21 @@ const char* GetContextName(ScriptContext context)
}
}
+const char* GetContextName_Short(ScriptContext context)
+{
+ switch (context)
+ {
+ case ScriptContext::CLIENT:
+ return "CL";
+ case ScriptContext::SERVER:
+ return "SV";
+ case ScriptContext::UI:
+ return "UI";
+ default:
+ return "??";
+ }
+}
+
eSQReturnType SQReturnTypeFromString(const char* pReturnType)
{
static const std::map<std::string, eSQReturnType> sqReturnTypeNameToString = {
@@ -230,8 +274,7 @@ template <ScriptContext context> SQInteger SQPrintHook(HSquirrelVM* sqvm, const
{
if (buf[charsWritten - 1] == '\n')
buf[charsWritten - 1] = '\0';
-
- spdlog::info("[{} SCRIPT] {}", GetContextName(context), buf);
+ g_pSquirrel<context>->logger->info("{}", buf);
}
va_end(va);
@@ -279,8 +322,10 @@ void __fastcall ScriptCompileErrorHook(HSquirrelVM* sqvm, const char* error, con
bIsFatalError = g_pSquirrel<ScriptContext::UI>->m_bFatalCompilationErrors;
}
- spdlog::error("{} SCRIPT COMPILE ERROR {}", GetContextName(realContext), error);
- spdlog::error("{} line [{}] column [{}]", file, line, column);
+ auto logger = getSquirrelLoggerByContext(realContext);
+
+ logger->error("COMPILE ERROR {}", error);
+ logger->error("{} line [{}] column [{}]", file, line, column);
// use disconnect to display an error message for the compile error, but only if the compilation error was fatal
// todo, we could get this from sqvm itself probably, rather than hooking sq_compiler_create
@@ -518,6 +563,9 @@ ON_DLL_LOAD_RELIESON("client.dll", ClientSquirrel, ConCommand, (CModule module))
&g_pSquirrel<ScriptContext::CLIENT>->RegisterSquirrelFunc);
g_pSquirrel<ScriptContext::UI>->RegisterSquirrelFunc = g_pSquirrel<ScriptContext::CLIENT>->RegisterSquirrelFunc;
+ g_pSquirrel<ScriptContext::CLIENT>->logger = NS::log::SCRIPT_CL;
+ g_pSquirrel<ScriptContext::UI>->logger = NS::log::SCRIPT_UI;
+
// uiscript_reset concommand: don't loop forever if compilation fails
module.Offset(0x3C6E4C).NOP(6);
@@ -582,6 +630,8 @@ ON_DLL_LOAD_RELIESON("server.dll", ServerSquirrel, ConCommand, (CModule module))
g_pSquirrel<ScriptContext::SERVER>->__sq_GetEntityConstant_CBaseEntity = module.Offset(0x418AF0).As<sq_GetEntityConstantType>();
g_pSquirrel<ScriptContext::SERVER>->__sq_getentityfrominstance = module.Offset(0x1E920).As<sq_getentityfrominstanceType>();
+ g_pSquirrel<ScriptContext::SERVER>->logger = NS::log::SCRIPT_SV;
+
MAKEHOOK(
module.Offset(0x1DD10),
&RegisterSquirrelFunctionHook<ScriptContext::SERVER>,