Skip to content

Commit

Permalink
fix: Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Yushu2606 committed Feb 4, 2024
1 parent 6b35fa4 commit 764d708
Show file tree
Hide file tree
Showing 46 changed files with 519 additions and 1,350 deletions.
6 changes: 3 additions & 3 deletions src/Async/AsyncPlayer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
namespace Hosihikari.Minecraft.Extension.Async;

public static class AsyncPlayer { }
// namespace Hosihikari.Minecraft.Extension.Async;
//
// public static class AsyncPlayer;
34 changes: 15 additions & 19 deletions src/Async/Core.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
namespace Hosihikari.Minecraft.Extension.Async;

public class Core
public static class Core
{
/// <summary>
/// Queue in thread pool.
/// Queue in thread pool.
/// </summary>
public static void QueueWorkItem(Action act)
{
ThreadPool.QueueUserWorkItem(
x =>
Task.Run(() =>
{
try
{
act();
}
catch (Exception)
{
try
{
x();
//Console.WriteLineErr(nameof(Async), ex, methodName, file, line);
}
catch (Exception)
catch
{
try
{
//Console.WriteLineErr(nameof(Async), ex, methodName, file, line);
}
catch
{
// ignored
}
// ignored
}
},
act,
true
);
}
});
}
}
}
43 changes: 17 additions & 26 deletions src/Async/RunInTick.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;

namespace Hosihikari.Minecraft.Extension.Async;

public class RunInTickVoid : INotifyCompletion
public sealed class RunInTickVoid : INotifyCompletion
{
public static RunInTickVoid StartAsync(Action func)
{
var asyncOperation = new RunInTickVoid(out var reportResult, out var reportException);
RunInTickVoid asyncOperation = new(out Action reportResult, out Action<Exception> reportException);
LevelTick.RunInTick(() =>
{
try
Expand Down Expand Up @@ -53,7 +52,7 @@ public RunInTickVoid GetAwaiter()
/// </summary>
public void GetResult()
{
if (_exception != null)
if (_exception is not null)
{
ExceptionDispatchInfo.Capture(_exception).Throw();
}
Expand All @@ -68,7 +67,7 @@ public void OnCompleted(Action continuation)
{
if (IsCompleted)
{
Core.QueueWorkItem(() => continuation?.Invoke());
Core.QueueWorkItem(continuation);
}
else
{
Expand All @@ -79,7 +78,7 @@ public void OnCompleted(Action continuation)
private void ReportResult()
{
IsCompleted = true;
if (_continuation != null)
if (_continuation is not null)
{
Core.QueueWorkItem(_continuation);
}
Expand All @@ -88,13 +87,13 @@ private void ReportResult()
private void ReportException(Exception exception)
{
_exception = exception;
if (_exception != null)
if (_exception is not null)
{
//todo log
//Console.WriteLineErr(nameof(RunInTickAsyncVoid), _exception );
}
IsCompleted = true;
if (_continuation != null)
if (_continuation is not null)
{
Core.QueueWorkItem(_continuation);
}
Expand All @@ -121,11 +120,11 @@ public void ContinueWith(Action action)
}
}

public class RunInTick<T> : INotifyCompletion //,IAwaitable<T>, IAwaiter<T>
public sealed class RunInTick<T> : INotifyCompletion //,IAwaitable<T>, IAwaiter<T>
{
public static RunInTick<T> StartAsync(Func<T> func)
{
var asyncOperation = new RunInTick<T>(out var reportResult, out var reportException);
RunInTick<T> asyncOperation = new(out Action<T> reportResult, out Action<Exception> reportException);
LevelTick.RunInTick(() =>
{
try
Expand All @@ -141,8 +140,8 @@ public static RunInTick<T> StartAsync(Func<T> func)
}

private RunInTick(
[NotNull] out Action<T> reportResult,
[NotNull] out Action<Exception> reportException
out Action<T> reportResult,
out Action<Exception> reportException
)
{
reportResult = ReportResult;
Expand Down Expand Up @@ -171,15 +170,7 @@ public RunInTick<T> GetAwaiter()
/// </summary>
public T? Result
{
get
{
if (IsCompleted)
{
return _result;
}

return default;
}
get => IsCompleted ? _result : default;
private set => _result = value;
}

Expand All @@ -199,7 +190,7 @@ public async Task<T> GetResultAsync()
/// </summary>
public T GetResult()
{
if (_exception != null)
if (_exception is not null)
{
// throw the exception that occurred in the asynchronous operation
ExceptionDispatchInfo.Capture(_exception).Throw();
Expand All @@ -219,7 +210,7 @@ public void OnCompleted(Action continuation)
{
// if the task has been completed when the await starts, execute the code after the await directly.
// Note that even if _continuation has a value, you don't need to care, because it will be executed when the report ends.
Core.QueueWorkItem(() => continuation?.Invoke());
Core.QueueWorkItem(continuation);
}
else
{
Expand All @@ -234,7 +225,7 @@ private void ReportResult(T r)
{
Result = r;
IsCompleted = true;
if (_continuation != null)
if (_continuation is not null)
{
//Dispatcher.InvokeAsync(_continuation, _priority);
Core.QueueWorkItem(_continuation);
Expand All @@ -250,7 +241,7 @@ private void ReportException(Exception exception)
// Console.WriteLineErr(nameof(RunInTickAsync<T>), _exception, methodName, file, line);
}
IsCompleted = true;
if (_continuation != null)
if (_continuation is not null)
{
// todo ? Dispatcher.InvokeAsync(_continuation);
// Queue the continuation action on the thread pool.
Expand Down
8 changes: 4 additions & 4 deletions src/EntryPoint.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using System.Runtime.CompilerServices;
using Hosihikari.Minecraft.Extension.PackHelper;
using Hosihikari.Minecraft.Extension.GlobalService;
using System.Runtime.CompilerServices;

namespace Hosihikari.Minecraft.Extension;

internal class EntryPoint
internal static class EntryPoint
{
#pragma warning disable CA2255
[ModuleInitializer]
#pragma warning restore CA2255
internal static void Main() // must be loaded
{
LevelTick.InitHook();
GlobalService.Global.Init();
Global.Init();
PackHelper.PackHelper.Init();
}
}
2 changes: 1 addition & 1 deletion src/Events/EventArgsBase.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Hosihikari.Minecraft.Extension.Events;

public abstract class EventArgsBase : EventArgs { }
public abstract class EventArgsBase : EventArgs;

public abstract class CancelableEventArgsBase : EventArgsBase
{
Expand Down
24 changes: 12 additions & 12 deletions src/Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ namespace Hosihikari.Minecraft.Extension.Events;

public static class Events
{
private static readonly Lazy<ChatEvent> PlayerChatEvent = new(() => new());
public static ChatEvent PlayerChat => PlayerChatEvent.Value;
private static readonly Lazy<ChatEvent> s_playerChatEvent = new(() => new());
public static ChatEvent PlayerChat => s_playerChatEvent.Value;

private static readonly Lazy<InitializedEvent> PlayerInitializedEvent = new(() => new());
public static InitializedEvent PlayerInitialized => PlayerInitializedEvent.Value;
private static readonly Lazy<InitializedEvent> s_playerInitializedEvent = new(() => new());
public static InitializedEvent PlayerInitialized => s_playerInitializedEvent.Value;

private static readonly Lazy<JoinEvent> PlayerJoinEvent = new(() => new());
public static JoinEvent PlayerJoin => PlayerJoinEvent.Value;
private static readonly Lazy<LeftEvent> PlayerLeftEvent = new(() => new());
public static LeftEvent PlayerLeft => PlayerLeftEvent.Value;
private static readonly Lazy<RespawnEvent> PlayerRespawnEvent = new(() => new());
public static RespawnEvent PlayerRespawn => PlayerRespawnEvent.Value;
private static readonly Lazy<DeathEvent> PlayerDeathEvent = new(() => new());
public static DeathEvent PlayerDeath => PlayerDeathEvent.Value;
private static readonly Lazy<JoinEvent> s_playerJoinEvent = new(() => new());
public static JoinEvent PlayerJoin => s_playerJoinEvent.Value;
private static readonly Lazy<LeftEvent> s_playerLeftEvent = new(() => new());
public static LeftEvent PlayerLeft => s_playerLeftEvent.Value;
private static readonly Lazy<RespawnEvent> s_playerRespawnEvent = new(() => new());
public static RespawnEvent PlayerRespawn => s_playerRespawnEvent.Value;
private static readonly Lazy<DeathEvent> s_playerDeathEvent = new(() => new());
public static DeathEvent PlayerDeath => s_playerDeathEvent.Value;
}
11 changes: 6 additions & 5 deletions src/Events/HookEventBase.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Runtime.CompilerServices;
using Hosihikari.Minecraft.Extension.Events.Implements;
using Hosihikari.NativeInterop.Hook.ObjectOriented;
using Microsoft.Extensions.Logging;

namespace Hosihikari.Minecraft.Extension.Events;

public delegate Task AsyncEventHandler<TEventArgs>(object? sender, TEventArgs e)
public delegate Task AsyncEventHandler<in TEventArgs>(object? sender, TEventArgs e)
where TEventArgs : EventArgsBase;

public abstract class HookEventBase<TEventArgs, THookDelegate> : HookBase<THookDelegate>
Expand Down Expand Up @@ -109,7 +110,7 @@ protected virtual void OnEventBefore(TEventArgs e)
}
catch (Exception ex)
{
Log.Logger.Error(_className + "::" + nameof(OnEventBefore), ex);
Log.Logger.LogError("Unhandled Exception in {ModuleName}: {Exception}", _className + "::" + nameof(OnEventBefore), ex);
}
}

Expand All @@ -121,13 +122,13 @@ protected virtual void OnEventAfter(TEventArgs e)
}
catch (Exception ex)
{
Log.Logger.Error(_className + "::" + nameof(OnEventBefore), ex);
Log.Logger.LogError("Unhandled Exception in {ModuleName}: {Exception}", _className + "::" + nameof(OnEventBefore), ex);
}
var task = InternalAsync?.Invoke(this, e);
Task? task = InternalAsync?.Invoke(this, e);
//todo allow user to toggle off in config ?
//output error when async event throw exception
task?.ContinueWith(
t => Log.Logger.Error(_className + "::" + nameof(OnEventAfter) + "Async", t.Exception),
t => Log.Logger.LogError("Unhandled Exception in {ModuleName}: {Exception}", _className + "::" + nameof(OnEventAfter) + "Async", t.Exception),
TaskContinuationOptions.OnlyOnFaulted
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Events/Implements/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Hosihikari.Minecraft.Extension.Events.Implements;

public class Log
public static class Log
{
private static readonly Lazy<ILogger> s_logger = new(() =>
{
Expand Down
30 changes: 17 additions & 13 deletions src/Events/Implements/Player/ChatEvent.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
using Hosihikari.NativeInterop.Unmanaged;
using Microsoft.Extensions.Logging;

namespace Hosihikari.Minecraft.Extension.Events.Implements.Player;

public class ChatEventArgs : CancelableEventArgsBase
public sealed class ChatEventArgs : CancelableEventArgsBase
{
public required ServerPlayer ServerPlayer { get; init; }
public required string Message { get; init; }
internal ChatEventArgs(ServerPlayer serverPlayer, string message)
{
ServerPlayer = serverPlayer;
Message = message;
}
public ServerPlayer ServerPlayer { get; }
public string Message { get; }
}

public class ChatEvent : HookCancelableEventBase<ChatEventArgs, ChatEvent.HookDelegate>
public sealed class ChatEvent()
: HookCancelableEventBase<ChatEventArgs, ChatEvent.HookDelegate>(ServerNetworkHandler.Original.Handle)
{
public ChatEvent()
: base(ServerNetworkHandler.Original.Handle) { }

public unsafe delegate void HookDelegate(
public delegate void HookDelegate(
Pointer<ServerNetworkHandler> networkHandler,
Reference<NetworkIdentifier> networkIdentifier,
Reference<TextPacket> textPacket
);

public override unsafe HookDelegate HookedFunc =>
public override HookDelegate HookedFunc =>
(networkHandlerPtr, networkIdentifierPtr, textPacketPtr) =>
{
try
{
var networkHandler = networkHandlerPtr.Target;
var networkIdentifier = networkIdentifierPtr.Target;
var packet = textPacketPtr.Target;
ServerNetworkHandler networkHandler = networkHandlerPtr.Target;
NetworkIdentifier networkIdentifier = networkIdentifierPtr.Target;
TextPacket packet = textPacketPtr.Target;

throw new NotImplementedException();

Expand All @@ -52,7 +56,7 @@ Reference<TextPacket> textPacket
}
catch (Exception ex)
{
Log.Logger.Error(nameof(ChatEvent), ex);
Log.Logger.LogError("Unhandled Exception in {ModuleName}: {Exception}", nameof(ChatEvent), ex);
}
};
}
Loading

0 comments on commit 764d708

Please sign in to comment.