Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Redid logging to make it similar to Java's log4j.
Browse files Browse the repository at this point in the history
To access the original VentLogger behaviour utilize methods from StaticLogger.
  • Loading branch information
ImaMapleTree committed Aug 2, 2023
1 parent 1459ad0 commit bf0e5b7
Show file tree
Hide file tree
Showing 100 changed files with 2,549 additions and 410 deletions.
1 change: 1 addition & 0 deletions FodyWeavers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<IncludeAssemblies>
YamlDotNet
Jetbrains.Annotations
Pastel
</IncludeAssemblies>
</Costura>
</Weavers>
11 changes: 7 additions & 4 deletions VentFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Title>VentFramework</Title>
<Description>A modding framework for the popular social deduction game Among Us </Description>
<Version>1.2.3.412</Version>
<Version>1.2.4.2</Version>
<RootNamespace>VentLib</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
Expand All @@ -12,6 +12,7 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<Authors>ImaMapleTree</Authors>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
Expand All @@ -20,14 +21,16 @@
<PrivateAssets>all</PrivateAssets>
</PackageReference>

<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.668" />
<PackageReference Include="BepInEx.Unity.IL2CPP" Version="6.0.0-be.671" PrivateAssets="all" />

<PackageReference Include="JetBrains.Annotations" Version="2023.2.0-eap2">
<PackageReference Include="JetBrains.Annotations" Version="2023.2.0-eap3">
<Aliases>JetbrainsAnnotations</Aliases>
</PackageReference>

<PackageReference Include="Pastel" Version="4.1.0" />

<PackageReference Include="Samboy063.Cpp2IL.Core" Version="2022.1.0-development.866" />
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2023.6.13" PrivateAssets="all" />
<PackageReference Include="AmongUs.GameLibs.Steam" Version="2023.7.11" PrivateAssets="all" />

<PackageReference Include="BepInEx.IL2CPP.MSBuild" Version="2.0.1" PrivateAssets="all" />

Expand Down
18 changes: 12 additions & 6 deletions Vents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
Expand All @@ -9,6 +8,7 @@
using VentLib.Commands;
using VentLib.Localization;
using VentLib.Logging;
using VentLib.Logging.Default;
using VentLib.Networking.Interfaces;
using VentLib.Networking.RPC;
using VentLib.Networking.RPC.Attributes;
Expand Down Expand Up @@ -40,12 +40,17 @@ public class Vents

private static bool _initialized;

static Vents()
{
BepInExLogListener.BindToUnity();
}

[SuppressMessage("ReSharper", "InconsistentNaming")]
public static ModRPC? FindRPC(uint callId, MethodInfo? targetMethod = null)
{
if (!RpcBindings.TryGetValue(callId, out List<ModRPC>? RPCs))
{
VentLogger.Warn($"Attempted to find unregistered RPC: {callId}", "VentLib");
NoDepLogger.Warn($"Attempted to find unregistered RPC: {callId}", "VentLib");
return null;
}

Expand All @@ -60,7 +65,7 @@ public class Vents
throw new NullReferenceException($"No matching method with name {methodName} in class {declaringClass}");
if (!RpcBindings.TryGetValue(callId, out List<ModRPC>? RPCs))
{
VentLogger.Warn($"Attempted to find unregistered RPC: {callId}", "VentLib");
NoDepLogger.Warn($"Attempted to find unregistered RPC: {callId}", "VentLib");
return null;
}

Expand All @@ -71,7 +76,7 @@ public class Vents

public static bool Register(Assembly assembly, bool localize = true)
{
VentLogger.Info($"Registering {assembly.GetName().Name}");
NoDepLogger.Info($"Registering {assembly.GetName().Name}");
if (RegisteredAssemblies.ContainsKey(assembly)) return false;
RegisteredAssemblies.Add(assembly, VentControlFlag.AllowedReceiver | VentControlFlag.AllowedSender);
AssemblyNames.TryAdd(assembly, assembly.GetName().Name!);
Expand All @@ -91,7 +96,7 @@ public static bool Register(Assembly assembly, bool localize = true)
.SelectMany(t => t.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
.Where(m => m.GetCustomAttribute<ModRPCAttribute>() != null).ToList();

VentLogger.Info($"Registering {methods.Count} methods from {assembly.GetName().Name}", "VentLib");
NoDepLogger.Info($"Registering {methods.Count} methods from {assembly.GetName().Name}", "VentLib");
foreach (var method in methods)
{
ModRPCAttribute attribute = method.GetCustomAttribute<ModRPCAttribute>()!;
Expand All @@ -114,8 +119,9 @@ public static bool Register(Assembly assembly, bool localize = true)
public static void Initialize()
{
if (_initialized) return;
MainThreadAnchor.IsMainThread();

VentLogger.High($"Initializing VentFramework {Assembly.GetExecutingAssembly().GetName().Version!.ToString(4)} by Tealeaf");
NoDepLogger.High($"Initializing VentFramework {Assembly.GetExecutingAssembly().GetName().Version!.ToString(4)} by Tealeaf");

var _ = Async.AUCWrapper;
RootAssemby = Assembly.GetCallingAssembly();
Expand Down
4 changes: 2 additions & 2 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ The following parameters are supported by ModRPC:
// Sends / receives a message
[ModRPC((uint)MyRPCs.PublicSendMessage)]
public void AnyoneSendMessage(string message) {
VentLogger.Info($"Message Received: {message}");
log.Info($"Message Received: {message}");
}
// Allows only host to send a message, and allows for only non-hosts to receive the message
[ModRPC((uint)MyRPCs.HostSendMsg, senders: RpcActors.Host, receivers: RpcActors.NonHost]
public void HostMessage(string message) {
VentLogger.Info($"I am not the host and I received this: \"{message}\" message.);
log.Info($"I am not the host and I received this: \"{message}\" message.);
}
Interfaces
Expand Down
1 change: 0 additions & 1 deletion src/Commands/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using VentLib.Logging;
using VentLib.Options.Processors;
using VentLib.Utilities.Extensions;

Expand Down
5 changes: 3 additions & 2 deletions src/Lobbies/LobbyChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace VentLib.Lobbies;

public class LobbyChecker
{
private static StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LobbyChecker));
/*private static string LobbyEndpoint = "http://localhost:25565/lobbies";
private static string LobbyUpdateEndpoint = "http://localhost:25565/update-lobby";*/
private const string LobbyEndpoint = "http://18.219.112.36:8080/lobbies";
Expand Down Expand Up @@ -59,7 +60,7 @@ internal static void GETModdedLobbies()

private static void WaitForResponse(SyncTaskWaiter<HttpResponseMessage> response, int times)
{
if (times > 20) VentLogger.Fatal("Failed to get modded lobbies");
if (times > 20) log.Fatal("Failed to get modded lobbies");
else if (!response.Finished)
Async.Schedule(() => WaitForResponse(response, times + 1), 1f);
else HandleResponse(response.Response);
Expand All @@ -72,7 +73,7 @@ internal static void HandleResponse(Task<HttpResponseMessage>? response)
StreamReader reader = new(response.Result.Content.ReadAsStream());
string result = reader.ReadToEnd();
reader.Close();
VentLogger.Log(LogLevel.Fatal, $"Response from lobby server: {result}", "ModdedLobbyCheck");
log.Log(LogLevel.Fatal, $"Response from lobby server: {result}", "ModdedLobbyCheck");
_moddedLobbies = JsonSerializer.Deserialize<Dictionary<int, ModdedLobby>>(result)!;
}

Expand Down
8 changes: 5 additions & 3 deletions src/Lobbies/Patches/LobbyListingsPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace VentLib.Lobbies.Patches;
[HarmonyPatch(typeof(FindAGameManager), nameof(FindAGameManager.HandleList))]
internal class LobbyListingsPatch
{
private static StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LobbyListingsPatch));
internal static List<(int, MatchMakerGameButton?)> ModdedGames = new();

internal static void Prefix(FindAGameManager __instance)
Expand All @@ -24,7 +25,7 @@ internal static void Postfix(FindAGameManager __instance, [HarmonyArgument(0)] I
ModdedGames.Clear();
availableGames.ToArray().Do(game =>
{
VentLogger.Debug($"Game: {game.HostName} | {game.GameId} | {game.IPString}");
log.Debug($"Game: {game.HostName} | {game.GameId} | {game.IPString}");
var button = __instance.buttonPool.activeChildren
.ToArray()
.ToList()
Expand All @@ -39,6 +40,7 @@ internal static void Postfix(FindAGameManager __instance, [HarmonyArgument(0)] I
[HarmonyPatch(typeof(FindAGameManager), nameof(FindAGameManager.Start))]
internal class LobbyListingUIPatch
{
private static StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LobbyListingUIPatch));
private static bool _option = true;

internal static void Postfix(FindAGameManager __instance)
Expand Down Expand Up @@ -83,11 +85,11 @@ private static void CustomFinderMenu(FindAGameManager manager)

foreach (var kv in textComponents)
{
VentLogger.Fatal($"Name: {kv.Key} | Value: {kv.Value.TypeName()}");
log.Fatal($"Name: {kv.Key} | Value: {kv.Value.TypeName()}");
}

var c = textComponents["FilterTags"].GetComponentsInChildren<Component>().Select(c => (c.name, c.TypeName())).StrJoin();
VentLogger.Fatal($"C: {c}");
log.Fatal($"C: {c}");
// Moving map to left side
textComponents["MapPicker"].FindChild("Title_TMP").localPosition -= new Vector3(4.7f, 0);
mapButton.transform.localPosition -= new Vector3(6.5f, 0.48f);
Expand Down
15 changes: 7 additions & 8 deletions src/Lobbies/Patches/LobbyPublicPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@
using AmongUs.Data;
using HarmonyLib;
using VentLib.Logging;
using VentLib.Networking;
using VentLib.Networking.Handshake.Patches;
using VentLib.Utilities;

namespace VentLib.Lobbies.Patches;

[HarmonyPatch(typeof(GameStartManager), nameof(GameStartManager.MakePublic))]
internal class LobbyPublicPatch
public class LobbyPublicPatch
{
private static DateTime? lastUpdate;
private static StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LobbyPublicPatch));
private static DateTime? _lastUpdate;
public static void Prefix(GameStartManager __instance)
{
if (lastUpdate != null && DateTime.Now.Subtract(lastUpdate.Value).TotalSeconds < 1.5f) return;
lastUpdate = DateTime.Now;
if (_lastUpdate != null && DateTime.Now.Subtract(_lastUpdate.Value).TotalSeconds < 1.5f) return;
_lastUpdate = DateTime.Now;
if (!AmongUsClient.Instance.AmHost) return;
VentLogger.Info($"Lobby Created: {AmongUsClient.Instance.GameId}", "ModdedLobbyCheck");
log.Info($"Lobby Created: {AmongUsClient.Instance.GameId}", "ModdedLobbyCheck");
//if (!NetworkRules.AllowRoomDiscovery) return;
VentLogger.Info("Posting Room to Public", "RoomDiscovery");
log.Info("Posting Room to Public", "RoomDiscovery");
LobbyChecker.POSTModdedLobby(AmongUsClient.Instance.GameId, DataManager.Player.customization.name);
}

Expand Down
7 changes: 4 additions & 3 deletions src/Lobbies/Patches/LobbyStatusPatches.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ namespace VentLib.Lobbies.Patches;

internal class LobbyStatusPatches
{
private static readonly StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LobbyStatusPatches));
[QuickPrefix(typeof(AmongUsClient), nameof(AmongUsClient.StartGame))]
private static void UpdateStatusInGame(AmongUsClient __instance)
{
if (!__instance.AmHost) return;
if (!NetworkRules.AllowRoomDiscovery) return;
VentLogger.Info($"Updating Lobby Status: {LobbyStatus.InGame}", "LobbyStatus");
log.Info($"Updating Lobby Status: {LobbyStatus.InGame}", "LobbyStatus");
LobbyChecker.UpdateModdedLobby(__instance.GameId, LobbyStatus.InGame);
}

Expand All @@ -21,7 +22,7 @@ private static void UpdateStatusClosed(InnerNetClient __instance, DisconnectReas
{
if (reason is DisconnectReasons.NewConnection || !__instance.AmHost) return;
if (!NetworkRules.AllowRoomDiscovery) return;
VentLogger.Info($"Updating Lobby Status: {LobbyStatus.Closed}", "LobbyStatus");
log.Info($"Updating Lobby Status: {LobbyStatus.Closed}", "LobbyStatus");
LobbyChecker.UpdateModdedLobby(__instance.GameId, LobbyStatus.Closed);
}

Expand All @@ -30,7 +31,7 @@ private static void UpdateStatusStart(LobbyBehaviour __instance)
{
if (!AmongUsClient.Instance.AmHost) return;
if (!NetworkRules.AllowRoomDiscovery) return;
VentLogger.Info($"Updating Lobby Status: {LobbyStatus.Open}", "LobbyStatus");
log.Info($"Updating Lobby Status: {LobbyStatus.Open}", "LobbyStatus");
LobbyChecker.UpdateModdedLobby(AmongUsClient.Instance.GameId, LobbyStatus.Open);
}
}
2 changes: 2 additions & 0 deletions src/Localization/Attributes/LocalizedAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public void Inject(Localizer localizer, LocalizedAttribute[] parentAttributes, F
else qualifier = qualifier + "." + fieldAttribute.Qualifier;

string? defaultValue = containingField.GetValue(null) as string;
#pragma warning disable CS0618 // Type or member is obsolete
string translation = localizer.Translate(qualifier, defaultValue ?? $"<{qualifier}>", false, fieldAttribute.ForceOverride ? TranslationCreationOption.ForceSave : TranslationCreationOption.CreateIfNull);
#pragma warning restore CS0618 // Type or member is obsolete
containingField.SetValue(null, translation);
}
}
3 changes: 2 additions & 1 deletion src/Localization/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace VentLib.Localization;

public class Language
{
private static readonly StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(Language));
public string Name { get; set; }= null!;
public List<string> Authors { get; set; } = null!;
internal QualifiedDictionary Translations { get; set; } = null!;
Expand All @@ -25,7 +26,7 @@ public class Language
internal void Dump(ISerializer serializer)
{
if (File == null) return;
VentLogger.Trace($"Saving Translation File: {File.Name}");
log.Trace($"Saving Translation File: {File.Name}");

FileStream stream = File.Open(FileMode.Create);
string yamlString = serializer.Serialize(this);
Expand Down
11 changes: 6 additions & 5 deletions src/Localization/Localizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace VentLib.Localization;

public class Localizer
{
private static readonly StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(Localizer));
internal static readonly Dictionary<Assembly, Localizer> Localizers = new();

public Dictionary<string, Language> Languages { get; }
Expand Down Expand Up @@ -91,10 +92,10 @@ public string SmartTranslate(string qualifier, string defaultValue = "<{}>", boo
{
callingType ??= AccessTools.GetOutsideCaller().DeclaringType;

if (callingType == null) return Translate(qualifier, defaultValue!, useCache, translationCreationOption);
if (callingType == null) return Translate(qualifier, defaultValue, useCache, translationCreationOption);
string? baseQualifier = LocalizedAttribute.ClassQualifiers.GetValueOrDefault(callingType);
if (baseQualifier == null) return Translate(qualifier, defaultValue!, useCache, translationCreationOption);
return Translate(baseQualifier + "." + qualifier, defaultValue!, useCache, translationCreationOption);
if (baseQualifier == null) return Translate(qualifier, defaultValue, useCache, translationCreationOption);
return Translate(baseQualifier + "." + qualifier, defaultValue, useCache, translationCreationOption);
}

public string Translate(string qualifier, string defaultValue = "<{}>", bool useCache = true, TranslationCreationOption translationCreationOption = TranslationCreationOption.SaveIfNull)
Expand Down Expand Up @@ -175,7 +176,7 @@ private List<Language> LoadLanguages(DirectoryInfo directory)
{
return directory.EnumerateFiles("lang_*").SelectWhere(f =>
{
VentLogger.Info($"Loading Language File: {f.Name}");
log.Info($"Loading Language File: {f.Name}");
try
{
StreamReader reader = new(f.Open(FileMode.OpenOrCreate), Encoding.UTF8);
Expand All @@ -186,7 +187,7 @@ private List<Language> LoadLanguages(DirectoryInfo directory)
language.Localizer = this;
return language;
} catch (Exception e) {
VentLogger.Exception(e, $"Unable to load Language File \"{f.Name}\": ");
log.Exception($"Unable to load Language File \"{f.Name}\": ", e);
return null;
}
}).ToList();
Expand Down
3 changes: 2 additions & 1 deletion src/Localization/Patches/LanguageSetPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ namespace VentLib.Localization.Patches;
[HarmonyPatch(typeof(TranslationController), nameof(TranslationController.SetLanguage))]
internal class LanguageSetPatch
{
private static readonly StandardLogger log = LoggerFactory.GetLogger<StandardLogger>(typeof(LanguageSetPatch));
internal static string CurrentLanguage = DataManager.Settings.Language.CurrentLanguage.ToString();

[SuppressMessage("ReSharper", "UnusedMember.Local")]
private static void Postfix([HarmonyArgument(0)] TranslatedImageSet lang)
{
VentLogger.Info($"Loaded Language: {lang.languageID}");
log.Info($"Loaded Language: {lang.languageID}");
CurrentLanguage = lang.languageID.ToString();
Localizer.Localizers.Values.ForEach(l => l.CurrentLanguage = CurrentLanguage);
}
Expand Down
Loading

0 comments on commit bf0e5b7

Please sign in to comment.