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

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
TianMengLucky committed Aug 20, 2024
1 parent 4b7ced9 commit c5677b7
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Next.Api/Attributes/IsUnmanagedAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// ReSharper disable CheckNamespace

namespace System.Runtime.CompilerServices;

/// <summary>
/// This attribute is required by the compiler for the "unmanaged" generic type constraint.
/// It should be generated automatically, but for whatever reason its not.
/// </summary>
[Obsolete("Do not use directly.", true)]
[AttributeUsage(AttributeTargets.All)]
internal sealed class IsUnmanagedAttribute : Attribute
{
}
10 changes: 10 additions & 0 deletions Next.Api/Bases/LanguageLoaderBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Next.Api.Interfaces;

namespace Next.Api.Bases;

public abstract class LanguageLoaderBase(string[] filter)
{
public string[] Filter { get; protected set; } = filter;

public abstract void Load(ILanguageManager _manager, Stream stream, string FileName);
}
12 changes: 12 additions & 0 deletions Next.Api/Interfaces/ILanguageManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Next.Api.Bases;

namespace Next.Api.Interfaces;

public interface ILanguageManager
{
public void AddLoader(LanguageLoaderBase _loader);

public LanguageLoaderBase? GetLoader(string extensionName);

public void AddToMap(SupportedLangs lang, string key, string value, string loaderName);
}
30 changes: 30 additions & 0 deletions Next.Api/Logs/LocalWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using BepInEx;
using BepInEx.Core;

namespace Next.Api.Logs;

public class LocalWriter
{
public LocalWriter(NextLog log)
{
if (!Directory.Exists(LogDir))
Directory.CreateDirectory(LogDir);

LogFileWriter = new StreamWriter(File.Open(Path.Combine(LogDir, log.LogSource.SourceName), FileMode.OpenOrCreate, FileAccess.Write))
{
AutoFlush = true,
};

LogFileWriter.WriteLine("NextLog Listener Start");
LogFileWriter.WriteLine($"CurrentTime: {DateTime.Now:g}");
}

public static readonly string LogDir = Path.Combine(Paths.GameRootPath, "NextLogs");
private readonly StreamWriter LogFileWriter;

public LocalWriter Write(string str)
{
LogFileWriter.WriteLine(str);
return this;
}
}
11 changes: 11 additions & 0 deletions Next.Api/Logs/LogConfigInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;

namespace Next.Api.Logs;

public class LogConfigInfo(Assembly assembly, NextLog log)
{
public readonly Assembly Assembly = assembly;
public string LogName { get; set; } = string.Empty;
public NextLog _Log { get; set; } = log;
public readonly List<Assembly> UseAssembly = [assembly];
}
95 changes: 95 additions & 0 deletions Next.Api/Logs/LogHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System.Reflection;
using BepInEx.Logging;


namespace Next.Api.Logs;

public static class LogHelper
{
/*
各消息作用:
发生了致命错误,无法从中恢复 : A fatal error has occurred, which cannot be recovered from
Fatal
发生错误,但可以从中恢复 : An error has occured, but can be recovered from
Error
已发出警告,但并不一定意味着发生了错误 : A warning has been produced, but does not necessarily mean that something wrong has happened
Warning
应向用户显示的重要消息 : An important message that should be displayed to the user
Message
重要性较低的消息 : A message of low importance
Info
可能只有开发人员感兴趣的消息 : A message that would likely only interest a developer
Debug,
*/


/// <summary>
/// 一般信息
/// </summary>
/// <param name="Message"></param>
public static void Info(string Message)
{
Log(Message, LogLevel.Info, Assembly.GetCallingAssembly());
}

/// <summary>
/// 报错
/// </summary>
/// <param name="Message"></param>
public static void Error(string Message)
{
Log(Message, LogLevel.Error, Assembly.GetCallingAssembly());
}

/// <summary>
/// 测试
/// </summary>
/// <param name="Message"></param>
public static void Debug(string Message)
{
Log(Message, LogLevel.Debug, Assembly.GetCallingAssembly());
}

public static void Fatal(string Message)
{
Log(Message, LogLevel.Fatal, Assembly.GetCallingAssembly());
}

/// <summary>
/// 警告
/// </summary>
/// <param name="Message"></param>
public static void Warn(string Message)
{
Log(Message, LogLevel.Warning, Assembly.GetCallingAssembly());
}


public static void Message(string Message)
{
Log(Message, LogLevel.Message, Assembly.GetCallingAssembly());
}

public static void Exception(Exception exception)
{
Error(exception.ToString());
}

public static void Log(object @object, LogLevel errorLevel = LogLevel.None, Assembly? assembly = null)
{
var _assembly = assembly ?? Assembly.GetCallingAssembly();
var log = NextLog.GetUseLog(_assembly);
log.WriteToFile(@object, errorLevel);
}

public static void LogObject(object @object)
{
Log(@object, LogLevel.Error, Assembly.GetCallingAssembly());
}
}
110 changes: 110 additions & 0 deletions Next.Api/Logs/NextLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System.Reflection;
using System.Text;
using BepInEx;
using BepInEx.Logging;

namespace Next.Api.Logs;

public class NextLog(Assembly _assembly, ManualLogSource logSource)
{
public Assembly Assembly = _assembly;
public ManualLogSource LogSource = logSource;
public bool UseLocalWrite { get; set; } = false;
private LocalWriter? _writer;
public LocalWriter? Writer
{
get
{
if (UseLocalWrite)
{
return _writer ??= new LocalWriter(this);
}

return null;
}
}

public static List<LogConfigInfo> LogConfigInfos = [];
public static NextLog? MainLog { get; private set; }

public static void RemoveLog(Assembly? assembly = null)
{
var _assembly = assembly ?? Assembly.GetCallingAssembly();
foreach (var info in LogConfigInfos.Where(n => n.UseAssembly.Contains(_assembly)))
info.UseAssembly.Remove(_assembly);
}

public static NextLog? SetLog(Assembly assembly)
{
if (LogConfigInfos.All(n => n.Assembly != assembly))
return null;

var info = LogConfigInfos.First(n => n.Assembly == assembly);
info.UseAssembly.Add(assembly);

return info._Log;
}

public static NextLog CreateMainLog(ManualLogSource logSource)
{
if (ConsoleManager.ConsoleEnabled)
System.Console.OutputEncoding = Encoding.UTF8;

var assembly = Assembly.GetCallingAssembly();
MainLog = new NextLog(assembly, logSource);
RemoveLog(assembly);

LogConfigInfos.Add(new LogConfigInfo(assembly, MainLog)
{
LogName = logSource.SourceName
});
return MainLog;
}

public static NextLog GetUseLog(Assembly? assembly = null)
{
var _assembly = assembly ?? Assembly.GetCallingAssembly();
var log = LogConfigInfos.FirstOrDefault(n => n.UseAssembly.Contains(_assembly))?._Log;
if (log != null)
return log;

if (MainLog == null) return CreateMainLog(_assembly.GetLogSource());

LogConfigInfos.First(n => n._Log == MainLog).UseAssembly.Add(_assembly);
return MainLog;
}

public NextLog WriteToFile(object @object, LogLevel errorLevel = LogLevel.None)
{
var Message = @object as string;
switch (errorLevel)
{
case LogLevel.Message:
LogSource.LogMessage(Message);
break;
case LogLevel.Error:
LogSource.LogError(Message);
break;
case LogLevel.Warning:
LogSource.LogWarning(Message);
break;
case LogLevel.Fatal:
LogSource.LogFatal(Message);
break;
case LogLevel.Info:
LogSource.LogInfo(Message);
break;
case LogLevel.Debug:
LogSource.LogDebug(Message);
break;
case LogLevel.None:
case LogLevel.All:
default:
LogSource.LogInfo(Message);
goto Writer;
}
Writer:
Writer?.Write($"[FastLog, Level: {errorLevel}] {Message}");
return this;
}
}
25 changes: 25 additions & 0 deletions Next.Api/Utils/BepInExUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Reflection;
using BepInEx;
using BepInEx.Configuration;
using BepInEx.Logging;
using BepInEx.Unity.IL2CPP;

namespace Next.Api.Utils;

public static class BepInExUtils
{
public static PluginInfo GetPlugin(this Assembly assembly)
{
return IL2CPPChainloader.Instance.Plugins.FirstOrDefault(n => n.Value.Location == assembly.Location).Value;
}

public static ConfigFile GetConfig(this Assembly assembly) => assembly.GetBaseInstance().Config;

public static ManualLogSource GetLogSource(this Assembly assembly) => assembly.GetBaseInstance().Log;

public static BasePlugin GetBaseInstance(this Assembly assembly) => (BasePlugin)assembly.GetPlugin().Instance;

public static T? GetInstance<T>(this PluginInfo info) where T : BasePlugin => info.Instance as T;

public static BepInPlugin GetMetadata(this Assembly assembly) => assembly.GetPlugin().Metadata;
}

0 comments on commit c5677b7

Please sign in to comment.