From 1033fef95e74e1c6b0b676462d321a627606ec66 Mon Sep 17 00:00:00 2001 From: Ape <155788660+ApeMV@users.noreply.github.com> Date: Thu, 4 Dec 2025 17:39:46 +0100 Subject: [PATCH] Project base --- HNSRevamped.cs | 35 ++++++++++ HNSRevamped.csproj | 25 +++++++ LateTask.cs | 61 +++++++++++++++++ LogHandler.cs | 158 +++++++++++++++++++++++++++++++++++++++++++++ build.cake | 28 ++++++++ nuget.config | 7 ++ 6 files changed, 314 insertions(+) create mode 100644 HNSRevamped.cs create mode 100644 HNSRevamped.csproj create mode 100644 LateTask.cs create mode 100644 LogHandler.cs create mode 100644 build.cake create mode 100644 nuget.config diff --git a/HNSRevamped.cs b/HNSRevamped.cs new file mode 100644 index 0000000..b8e2d22 --- /dev/null +++ b/HNSRevamped.cs @@ -0,0 +1,35 @@ +using BepInEx; +using BepInEx.Configuration; +using BepInEx.Unity.IL2CPP; +using HarmonyLib; + +namespace HNSRevamped; + +[BepInAutoPlugin] +[BepInProcess("Among Us.exe")] +public partial class Main : BasePlugin +{ + public Harmony Harmony { get; } = new(Id); + public static BepInEx.Logging.ManualLogSource Logger; + public static BasePlugin Instance; + + + public override void Load() + { + var handler = HNSRevamped.Logger.Handler("GitVersion"); + Logger = BepInEx.Logging.Logger.CreateLogSource("HNSRevamped"); + HNSRevamped.Logger.Enable(); + Instance = this; + + Harmony.PatchAll(); + } + + [HarmonyPatch(typeof(ModManager), nameof(ModManager.LateUpdate))] + class ModManagerLateUpdatePatch + { + public static void Prefix(ModManager __instance) + { + __instance.ShowModStamp(); + } + } +} diff --git a/HNSRevamped.csproj b/HNSRevamped.csproj new file mode 100644 index 0000000..6a21794 --- /dev/null +++ b/HNSRevamped.csproj @@ -0,0 +1,25 @@ + + + net6.0 + latest + embedded + + 1.0.0 + dev + Mod generated using Reactor Template + + + + + + + + + + + + + + + + diff --git a/LateTask.cs b/LateTask.cs new file mode 100644 index 0000000..06329f8 --- /dev/null +++ b/LateTask.cs @@ -0,0 +1,61 @@ +using System; +using System.Collections.Generic; +using System.Runtime.CompilerServices; + + +namespace HNSRevamped; + +class LateTask +{ + public string name; + public float timer; + public bool shouldLog; + public Action action; + public static List Tasks = []; + public bool Run(float deltaTime) + { + timer -= deltaTime; + if (timer <= 0) + { + action(); + return true; + } + return false; + } + public LateTask(Action action, float time, string name = "No Name Task", bool shoudLog = true, [CallerMemberName] string callerMethodName = "") + { + this.action = action; + this.timer = time; + if (name != "No Name Task") + this.name = name; + else + this.name = callerMethodName + " Task"; + this.shouldLog = shoudLog; + + Tasks.Add(this); + if (name != "") + if (shoudLog) + Logger.Info("\"" + name + "\" is created", "LateTask"); + } + public static void Update(float deltaTime) + { + foreach (var task in Tasks.ToArray()) + { + try + { + if (task.Run(deltaTime)) + { + if (task.name is not "" && task.shouldLog) + Logger.Info($"\"{task.name}\" is finished", "LateTask"); + + Tasks.Remove(task); + } + } + catch (Exception ex) + { + Logger.Error($"{ex.GetType()}: {ex.Message} in \"{task.name}\"\n{ex.StackTrace}", "LateTask.Error", false); + Tasks.Remove(task); + } + } + } +} \ No newline at end of file diff --git a/LogHandler.cs b/LogHandler.cs new file mode 100644 index 0000000..8b04eb1 --- /dev/null +++ b/LogHandler.cs @@ -0,0 +1,158 @@ +using System; +using System.Diagnostics; +using System.IO; +using System.Net.Http; +using System.Runtime.CompilerServices; +using HarmonyLib; +using System.Linq; +using System.Collections.Generic; +using LogLevel = BepInEx.Logging.LogLevel; + +namespace HNSRevamped; + +public interface ILogHandler +{ + public void Info(string text); + public void Warn(string text); + public void Error(string text); + public void Fatal(string text); + public void Msg(string text); + public void Exception(Exception ex); +} + +class LogHandler(string tag) : ILogHandler +{ + public string Tag { get; } = tag; + + public void Info(string text) + => Logger.Info(text, Tag, true); + public void Warn(string text) + => Logger.Warn(text, Tag, true); + public void Error(string text) + => Logger.Error(text, Tag, true); + public void Fatal(string text) + => Logger.Fatal(text, Tag, true); + public void Msg(string text) + => Logger.Msg(text, Tag, true); + public void Exception(Exception ex) + => Logger.Exception(ex, Tag); +} + +class Logger +{ + public static bool IsEnable = true; + public static List DisableList = []; + public static List SendToGameList = []; + private static readonly HashSet NowDetailedErrorLog = []; + + public static bool isDetail = false; + public static bool isAlsoInGame = false; + public static void Enable() => IsEnable = true; + public static void Disable() => IsEnable = false; + public static void Enable(string tag, bool toGame = false) + { + DisableList.Remove(tag); + if (toGame && !SendToGameList.Contains(tag)) SendToGameList.Add(tag); + else SendToGameList.Remove(tag); + } + public static void Disable(string tag) { if (!DisableList.Contains(tag)) DisableList.Add(tag); } + public static void SendInGame(string text) + { + if (!IsEnable) return; + if (DestroyableSingleton._instance) DestroyableSingleton.Instance.Notifier.AddDisconnectMessage(text); + } + private static void SendToFile(string text, LogLevel level = LogLevel.Info, string tag = "", bool escapeCRLF = true, int lineNumber = 0, string fileName = "", bool multiLine = false) + { + if (!IsEnable || DisableList.Contains(tag)) return; + var logger = Main.Logger; + + if (SendToGameList.Contains(tag) || isAlsoInGame) + { + SendInGame($"[{tag}]{text}"); + } + + string log_text; + if (level is LogLevel.Error or LogLevel.Fatal or LogLevel.Warning && !multiLine && !NowDetailedErrorLog.Contains(tag)) + { + string t = DateTime.Now.ToString("HH:mm:ss"); + StackFrame stack = new(2); + string className = stack.GetMethod()?.ReflectedType?.Name; + string memberName = stack.GetMethod()?.Name; + log_text = $"[{t}][{className}.{memberName}({Path.GetFileName(fileName)}:{lineNumber})][{tag}]{text}"; + NowDetailedErrorLog.Add(tag); + _ = new LateTask(() => NowDetailedErrorLog.Remove(tag), 3f); + } + else + { + if (escapeCRLF) text = text.Replace("\r", "\\r").Replace("\n", "\\n"); + string t = DateTime.Now.ToString("HH:mm:ss"); + log_text = $"[{t}][{tag}]{text}"; + } + + switch (level) + { + case LogLevel.Info when !multiLine: + logger.LogInfo(log_text); + break; + case LogLevel.Info: + log_text.Split("\\n").Do(logger.LogInfo); + break; + case LogLevel.Warning when !multiLine: + logger.LogWarning(log_text); + break; + case LogLevel.Warning: + log_text.Split("\\n").Do(logger.LogWarning); + break; + case LogLevel.Error when !multiLine: + logger.LogError(log_text); + break; + case LogLevel.Error: + log_text.Split("\\n").Do(logger.LogError); + break; + case LogLevel.Fatal when !multiLine: + logger.LogFatal(log_text); + break; + case LogLevel.Fatal: + log_text.Split("\\n").Do(logger.LogFatal); + break; + case LogLevel.Message when !multiLine: + logger.LogMessage(log_text); + break; + case LogLevel.Message: + log_text.Split("\\n").Do(logger.LogMessage); + break; + case LogLevel.Debug when !multiLine: + logger.LogFatal(log_text); + break; + case LogLevel.Debug: + log_text.Split("\\n").Do(logger.LogFatal); + break; + default: + logger.LogWarning("Error: Invalid LogLevel"); + logger.LogInfo(log_text); + break; + } + } + public static void Test(object content, string tag = "======= Test =======", bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(content.ToString(), LogLevel.Debug, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Info(string text, string tag, bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(text, LogLevel.Info, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Warn(string text, string tag, bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(text, LogLevel.Warning, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Error(string text, string tag, bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(text, LogLevel.Error, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Fatal(string text, string tag, bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(text, LogLevel.Fatal, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Msg(string text, string tag, bool escapeCRLF = true, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "", bool multiLine = false) => + SendToFile(text, LogLevel.Message, tag, escapeCRLF, lineNumber, fileName, multiLine); + public static void Exception(Exception ex, string tag, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "") => + SendToFile(ex.ToString(), LogLevel.Error, tag, false, lineNumber, fileName); + public static void CurrentMethod([CallerLineNumber] int lineNumber = 0, [CallerFilePath] string fileName = "") + { + StackFrame stack = new(1); + Msg($"\"{stack.GetMethod().ReflectedType.Name}.{stack.GetMethod().Name}\" Called in \"{Path.GetFileName(fileName)}({lineNumber})\"", "Method"); + } + + public static LogHandler Handler(string tag) + => new(tag); +} \ No newline at end of file diff --git a/build.cake b/build.cake new file mode 100644 index 0000000..b3dc40b --- /dev/null +++ b/build.cake @@ -0,0 +1,28 @@ +var target = Argument("target", "Build"); + +var workflow = BuildSystem.GitHubActions.Environment.Workflow; +var buildId = workflow.RunNumber; +var tag = workflow.RefType == GitHubActionsRefType.Tag ? workflow.RefName : null; + +Task("Build") + .Does(() => +{ + var settings = new DotNetBuildSettings + { + Configuration = "Release", + MSBuildSettings = new DotNetMSBuildSettings() + }; + + if (tag != null) + { + settings.MSBuildSettings.Version = tag; + } + else if (buildId != 0) + { + settings.MSBuildSettings.VersionSuffix = "ci." + buildId; + } + + DotNetBuild(".", settings); +}); + +RunTarget(target); diff --git a/nuget.config b/nuget.config new file mode 100644 index 0000000..3ee52a2 --- /dev/null +++ b/nuget.config @@ -0,0 +1,7 @@ + + + + + + +