Files
LuaCsForBarotraumaEP/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Services/LoggerService.cs
MapleWheels 6880e5e9ee [Milestone] AssemblyLoader completed.
Details:
- Assembly Mgmt Service for loading now a separate interface, not intended for normal use.
- Assembly Loader work; implemented custom dictionary key and table.
- Assembly loading work.
- EventService completed.
- Moved assembly extensions to ModUtils.cs
- Work to event service.
NetworkService work
- Added ImpromptuInterfaces package.
- Networking Service work to support NetVars
- Event Service
- Added assemblies references package for script compilation. Updated Roslyn version for compatibility.
- Package Loading work.
Swap Harmony to HarmonyX
- More refactor conversion to FluentResults.
- Updated StylesService to return Results.
- Refactor of PackageService partially complete.
- Made IService.Reset() required to return a Result.
- Moved plugin/assembly related code to their own folder (same namespace).
- Updated interfaces to reflect the use of Result<T>.
- Partial refactor, incomplete.
- Added 'FluentResults' so we can stop using cursed Exception-based flow control in loading code.
- Added 'OneOf' nuget package: https://github.com/mcintyre321/OneOf
for the implementation of the Optional<T> pattern and complex discrete return types instead of cursed enums (see current AssemblyManager.cs).
- Reapplied old branch changes.
2026-02-07 20:10:26 -05:00

155 lines
5.0 KiB
C#

using System;
using Barotrauma.Networking;
using Microsoft.Xna.Framework;
using MoonSharp.Interpreter;
namespace Barotrauma.LuaCs.Services;
public partial class LoggerService : ILoggerService
{
public bool HideUserNames = true;
#if SERVER
private const string LogPrefix = "SV";
private const int NetMaxLength = 1024; // character limit of vanilla Barotrauma's chat system.
private const int NetMaxMessages = 60;
// This is used so it's possible to call logging functions inside the serverLog
// hook without creating an infinite loop
private bool _lockLog = false;
#else
private const string LogPrefix = "CL";
#endif
public void HandleException(Exception exception, string prefix = null)
{
string errorString = "";
switch (exception)
{
case NetRuntimeException netRuntimeException:
if (netRuntimeException.DecoratedMessage == null)
{
errorString = $"{prefix ?? ""}{netRuntimeException.ToString()}";
}
else
{
// FIXME: netRuntimeException.ToString() doesn't print the InnerException's stack trace...
errorString = $"{prefix ?? ""}{netRuntimeException.DecoratedMessage}: {netRuntimeException}";
}
break;
case InterpreterException interpreterException:
if (interpreterException.DecoratedMessage == null)
{
errorString = $"{prefix ?? ""}{interpreterException.ToString()}";
}
else
{
errorString = $"{prefix ?? ""}{interpreterException.DecoratedMessage}";
}
break;
default:
string s = exception.StackTrace != null ? exception.ToString() : $"{exception}\n{Environment.StackTrace}";
errorString = $"{prefix ?? ""}{s}";
break;
}
LogError(prefix + Environment.UserName + " " + errorString);
}
public void LogError(string message)
{
if (HideUserNames && !Environment.UserName.IsNullOrEmpty())
{
message = message.Replace(Environment.UserName, "USERNAME");
}
Log($"{message}", Color.Red, ServerLog.MessageType.Error);
}
public void LogWarning(string message)
{
throw new NotImplementedException();
}
public void LogMessage(string message, Color? serverColor = null, Color? clientColor = null)
{
serverColor ??= Color.MediumPurple;
clientColor ??= Color.Purple;
#if SERVER
Log(message, serverColor);
#else
Log(message, clientColor);
#endif
}
public void Log(string message, Color? color = null, ServerLog.MessageType messageType = ServerLog.MessageType.ServerMessage)
{
DebugConsole.NewMessage(message, color);
#if SERVER
void BroadcastMessage(string m)
{
foreach (var client in GameMain.Server.ConnectedClients)
{
ChatMessage consoleMessage = ChatMessage.Create("", m, ChatMessageType.Console, null, textColor: color);
GameMain.Server.SendDirectChatMessage(consoleMessage, client);
if (!GameMain.Server.ServerSettings.SaveServerLogs || !client.HasPermission(ClientPermissions.ServerLog))
{
continue;
}
ChatMessage logMessage = ChatMessage.Create(messageType.ToString(), "[LuaCs] " + m, ChatMessageType.ServerLog, null);
GameMain.Server.SendDirectChatMessage(logMessage, client);
}
}
if (GameMain.Server != null)
{
if (GameMain.Server.ServerSettings.SaveServerLogs)
{
string logMessage = "[LuaCs] " + message;
GameMain.Server.ServerSettings.ServerLog.WriteLine(logMessage, messageType, false);
if (!_lockLog)
{
_lockLog = true;
GameMain.LuaCs?.Hook?.Call("serverLog", logMessage, messageType);
_lockLog = false;
}
}
for (int i = 0; i < message.Length; i += NetMaxLength)
{
string subStr = message.Substring(i, Math.Min(1024, message.Length - i));
BroadcastMessage(subStr);
}
}
#endif
}
public void LogResults(FluentResults.Result result)
{
throw new NotImplementedException();
}
public void LogDebug(string message, Color? color = null)
{
throw new NotImplementedException();
}
public void LogDebugWarning(string message)
{
throw new NotImplementedException();
}
public void LogDebugError(string message)
{
throw new NotImplementedException();
}
public void Dispose() { }
public FluentResults.Result Reset() => FluentResults.Result.Ok();
}