Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(logger): improve logger dx #13

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ cd ..
# Copy the mod dll to the mods folder
echo "Copying the mod dll to the mods folder..."
cp ./plugin/bin/Debug/netstandard2.1/$MOD_DLL.* "$GAME_PATH/BepInEx/plugins/"
cp ./plugin/bin/Debug/netstandard2.1/$MOD_DLL.* "../TTIK/libs/"
2 changes: 1 addition & 1 deletion plugin/PiUtils.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<PackageReference Include="BepInEx.Analyzers" Version="1.*" PrivateAssets="all" />
<PackageReference Include="BepInEx.Core" Version="5.*" />
<PackageReference Include="BepInEx.PluginInfoProps" Version="2.*" />
<PackageReference Include="techtonica-libs" Version="0.2.1-c3" />
<PackageReference Include="techtonica-libs" Version="0.2.2-f" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework.TrimEnd(`0123456789`))' == 'net'">
Expand Down
8 changes: 2 additions & 6 deletions plugin/src/PiUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,15 @@ namespace PiUtils;
[BepInPlugin("de.xenira.pi_utils", MyPluginInfo.PLUGIN_NAME, MyPluginInfo.PLUGIN_VERSION)]
public class PiUtils : BaseUnityPlugin
{
public static new ManualLogSource Logger;
private static PluginLogger logger;

public static string gameExePath = Process.GetCurrentProcess().MainModule.FileName;
public static string gamePath = Path.GetDirectoryName(gameExePath);

private void Awake()
{
Logger = base.Logger;
logger = PluginLogger.GetLogger<PiUtils>(Logger);
var logger = PluginLogger.GetLogger<PiUtils>();

logger.LogInfo($"Loading plugin {MyPluginInfo.PLUGIN_GUID} version {MyPluginInfo.PLUGIN_VERSION}...");
License.LogLicense(logger, "Xenira");
License.LogLicense(logger, "Xenira", MyPluginInfo.PLUGIN_GUID, MyPluginInfo.PLUGIN_VERSION);

ModConfig.Init(Config);

Expand Down
2 changes: 1 addition & 1 deletion plugin/src/assets/AssetLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace PiUtils.Assets;

public class AssetLoader
{
private static PluginLogger Logger = PluginLogger.GetLogger<AssetLoader>(PiUtils.Logger);
private static PluginLogger Logger = PluginLogger.GetLogger<AssetLoader>();

private string assetPath = "assets";

Expand Down
48 changes: 47 additions & 1 deletion plugin/src/util/Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,32 @@ public static IEnumerator IntervalFrames(Action callback, int frames, int startI
}
}
}

public static IEnumerator DelayUntilSet<T>(Action callback, T value)
{
yield return new WaitUntil(() => value != null);
callback();
}

public static IEnumerator DelayUntil(IEnumerator condition, Action callback)
{
while (!condition.MoveNext())
{
yield return null;
}
callback();
}

public static IEnumerator DelayUntil(Action callback, Func<bool> condition)
{
yield return new WaitUntil(condition);
callback();
}
}

public class AsyncGameObject : MonoBehaviour
{
private static PluginLogger Logger = PluginLogger.GetLogger<AsyncGameObject>(PiUtils.Logger);
private static PluginLogger Logger = PluginLogger.GetLogger<AsyncGameObject>();
private static AsyncGameObject instance;

private static AsyncGameObject Instance
Expand Down Expand Up @@ -111,6 +132,11 @@ public static Coroutine TimeoutFrames(Action callback, int frames)
return Instance.timeoutFrames(callback, frames);
}

public static Coroutine NextFrame(Action callback)
{
return Instance.timeoutFrames(callback, 1);
}

private Coroutine timeoutFrames(Action callback, int frames)
{
return StartCoroutine(Async.TimeoutFrames(callback, frames));
Expand All @@ -126,6 +152,26 @@ private Coroutine interval(Action callback, float seconds, float startInSeconds,
return StartCoroutine(Async.Interval(callback, seconds, startInSeconds, cnt));
}

public static Coroutine DelayUntilSet<T>(Action callback, T value)
{
return Instance.delayUntilSet(callback, value);
}

private Coroutine delayUntilSet<T>(Action callback, T value)
{
return StartCoroutine(Async.DelayUntilSet(callback, value));
}

public static Coroutine DelayUntil(Action callback, Func<bool> condition)
{
return Instance.delayUntil(callback, condition);
}

private Coroutine delayUntil(Action callback, Func<bool> condition)
{
return StartCoroutine(Async.DelayUntil(callback, condition));
}

public static void Cancel(Coroutine coroutine)
{
Instance.cancel(coroutine);
Expand Down
4 changes: 2 additions & 2 deletions plugin/src/util/License.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace PiUtils.Util;

public class License
{
public static void LogLicense(PluginLogger logger, String author)
public static void LogLicense(PluginLogger logger, String author, String name, String version)
{
logger.LogInfo($"{MyPluginInfo.PLUGIN_NAME} Copyright (C) {DateTime.Now.Year} {author}");
logger.LogInfo($"{name} {'v' + version} Copyright (C) {DateTime.Now.Year} {author}");
logger.LogInfo("This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 3 as published by the Free Software Foundation.");
logger.LogInfo("This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.");
logger.LogInfo("You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.");
Expand Down
42 changes: 28 additions & 14 deletions plugin/src/util/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
using System;
using System.Collections.Generic;
using BepInEx.Logging;
using UnityEngine;

namespace PiUtils.Util;

public class PluginLogger
{
private ManualLogSource logger;
public static Dictionary<string, ManualLogSource> loggers = new Dictionary<string, ManualLogSource>();

private string prefix;
private ManualLogSource logger
{
get
{
if (loggers.ContainsKey(prefix))
{
return loggers[prefix];
}

public PluginLogger(ManualLogSource logger, string prefix)
var logger = BepInEx.Logging.Logger.CreateLogSource(prefix);
loggers.Add(prefix, logger);
return logger;
}
}

public PluginLogger(string prefix)
{
this.logger = logger;
this.prefix = prefix;
}
public PluginLogger(ManualLogSource logger, Type type)

public PluginLogger(Type type) : this(type.FullName)
{
this.logger = logger;
prefix = type.FullName;
}

public static PluginLogger GetLogger<T>(ManualLogSource logger)
public static PluginLogger GetLogger<T>()
{
return new PluginLogger(logger, typeof(T));
return new PluginLogger(typeof(T));
}

public void LogInfo(string message)
{
logger.LogInfo($"[{prefix}] ({Time.frameCount}) {message}");
logger.LogInfo($"<{Time.frameCount}> {message}");
}

public void LogDebug(string message)
{
logger.LogDebug($"[{prefix}] ({Time.frameCount}) {message}");
logger.LogDebug($"<{Time.frameCount}> {message}");
}

public void LogWarning(string message)
{
logger.LogWarning($"[{prefix}] ({Time.frameCount}) {message}");
logger.LogWarning($"<{Time.frameCount}> {message}");
}

public void LogError(string message)
{
logger.LogError($"[{prefix}] ({Time.frameCount}) {message}");
logger.LogError($"<{Time.frameCount}> {message}");
}

internal void LogTrace(string v)
public void LogTrace(string v)
{
if (!ModConfig.TraceLogEnabled())
{
return;
}
logger.LogDebug($"[{prefix}] ({Time.frameCount}) {v}");
logger.LogDebug($"<{Time.frameCount}> {v}");
}
}
2 changes: 1 addition & 1 deletion plugin/src/util/ObjectPosition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace PiUtils.Util;

public class ObjectPosition
{
private static PluginLogger Logger = PluginLogger.GetLogger<ObjectPosition>(PiUtils.Logger);
private static PluginLogger Logger = PluginLogger.GetLogger<ObjectPosition>();

public static Vector3 addLocalPositions(Transform transform, Transform stopAt = null)
{
Expand Down