Skip to content
Open
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
2 changes: 1 addition & 1 deletion Code/ArgumentSystem/BaseArguments/Argument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public abstract class Argument(string name)
/// </summary>
public string? Description { get; init; } = null;

public record Default(object? Value, string? StringRep);
public record Default(object? Value, string? StringRep = null);

/// <summary>
/// Sets the default value for this argument, allowing it to be skipped by the user.
Expand Down
9 changes: 5 additions & 4 deletions Code/Helpers/FrameworkExtensions/CallVoteHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using MEC;
using SER.Code.MethodSystem.Structures;

namespace SER.Code.Helpers.FrameworkExtensions;

public sealed class CallvoteBridge : FrameworkBridge
{
public static event Action? OnDetected;
protected override string Name => "Callvote";

public void Load()
public override IDependOnFramework.Type FrameworkType { get; } = IDependOnFramework.Type.Callvote;

public CallvoteBridge()
{
Await(OnDetected).RunCoroutine();

}
}
8 changes: 5 additions & 3 deletions Code/Helpers/FrameworkExtensions/ExiledHelper.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using MEC;
using SER.Code.MethodSystem.Structures;

namespace SER.Code.Helpers.FrameworkExtensions;

public sealed class ExiledBridge : FrameworkBridge
{
protected override string Name => "Exiled Loader";
public static event Action? OnDetected;
public override bool ShouldRegister { get; } = false;
public override IDependOnFramework.Type FrameworkType { get; } = IDependOnFramework.Type.Exiled;

public void Load()
public ExiledBridge()
{
Await(OnDetected).RunCoroutine();

}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
using LabApi.Features.Console;
using LabApi.Loader;
using MEC;
using PluginSCPSL.Library.Utility;
using SER.Code.MethodSystem;
using SER.Code.MethodSystem.Structures;

namespace SER.Code.Helpers.FrameworkExtensions;

public abstract class FrameworkBridge
public abstract class FrameworkBridge : Registerable<FrameworkBridge>
{
protected abstract string Name { get; }
public abstract IDependOnFramework.Type FrameworkType { get; }
public event Action? OnDetected;

public override bool IsDebug { get; } = true;

protected override void OnRegistered()
{
OnDetected += () => MethodIndex.LoadMethodsOfFramework(FrameworkType);
Await(OnDetected).RunCoroutine();
base.OnRegistered();
}

protected IEnumerator<float> Await(Action? onDetected)
{
Expand Down
104 changes: 104 additions & 0 deletions Code/Helpers/FrameworkExtensions/Registerable/Registerable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Reflection;
using SER.Code.Helpers;
using Log = Exiled.API.Features.Log;

namespace PluginSCPSL.Library.Utility;

public class Registerable<TSelf> where TSelf : Registerable<TSelf>
{
public static RegisterableList<TSelf> Registered { get; } = new(OnAdd, OnRemove, () => {});

public virtual bool IsDebug => false;
public virtual bool ShouldRegister => true;
private Assembly _assembly = null;

private static void OnAdd(TSelf item)
{
item._assembly ??= typeof(TSelf).Assembly;
item.OnRegistered();
}

private static void OnRemove(TSelf item)
{
item.OnUnregistered();
}

protected virtual void OnRegistered() { }
protected virtual void OnUnregistered() { }

public static void RegisterAll(Assembly assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
foreach (var type in assembly.GetTypes())
{
if (type.IsAbstract) continue;
if (typeof(TSelf) == type)
{
Log.Info($"Skipping {type.Name}, because it's the original instance.");
continue;
}
if (!type.IsSubclassOf(typeof(TSelf))) continue;
if (type.GetConstructor([]) == null)
{
Log.Error($"Type {type.Name} could not be registered, please add default constructor");
continue;
}
TSelf instance;
try
{
instance = (TSelf)Activator.CreateInstance(type);
}
catch (Exception e)
{
Log.Error($"Instance registration error. Type: {type}\nMessage: {e}");
continue;
}
instance._assembly = assembly;

if (instance.ShouldRegister && instance.ShouldAddInstance())
{
if (instance.IsDebug)
{
Log.Info($"Adding {type.Name} as {typeof(TSelf).Name}");
}
Registered.Add(instance);
}
}
}

public virtual TSelf CloneMyself()
{
if (this.GetType().GetConstructor([]) == null)
{
Log.Error($"{this.GetType().Name} does not have default constructor");
return null;
}
return (TSelf)Activator.CreateInstance(this.GetType());
}

public virtual bool ShouldAddInstance()
{
return true;
}

public static void Register(TSelf instance)
{
Registered.Add(instance);
}

public static void Unregister(TSelf instance)
{
Registered.Remove(instance);
}

public static void UnregisterAll(Assembly assembly = null)
{
assembly ??= Assembly.GetCallingAssembly();
foreach (var i in Registered)
{
i.OnUnregistered();
}
Registered.RemoveAll(r => r._assembly == assembly);
}
}
52 changes: 52 additions & 0 deletions Code/Helpers/FrameworkExtensions/Registerable/RegisterableList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#nullable enable
namespace SER.Code.Helpers;

public class RegisterableList<T> : List<T>
{
public Action<T> OnAdd { get; set; }
public Action<T> OnRemove { get; set; }
public Action OnClear { get; set; }

public RegisterableList()
{

}

public RegisterableList(Action<T> onAdd, Action<T> onRemove, Action onClear)
{
OnAdd = onAdd;
OnRemove = onRemove;
OnClear = onClear;
}

public new void Add(T item)
{
OnAdd?.Invoke(item);
base.Add(item);
}

public new bool Remove(T item)
{
OnRemove?.Invoke(item);
return base.Remove(item);
}

public new void Clear()
{
OnClear?.Invoke();
foreach (var i in this)
{
OnRemove?.Invoke(i);
}
base.Clear();
}

public new void RemoveAll(Predicate<T> match)
{
foreach (var item in this)
{
if(match(item)) OnRemove?.Invoke(item);
}
base.RemoveAll(match);
}
}
15 changes: 15 additions & 0 deletions Code/Helpers/FrameworkExtensions/RueBridge.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MEC;
using SER.Code.MethodSystem.Structures;

namespace SER.Code.Helpers.FrameworkExtensions;

public sealed class RueBridge : FrameworkBridge
{
protected override string Name { get; } = "RueI";
public override IDependOnFramework.Type FrameworkType { get; } = IDependOnFramework.Type.Ruei;

public RueBridge()
{

}
}
9 changes: 5 additions & 4 deletions Code/Helpers/FrameworkExtensions/UcrBridge.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
using MEC;
using SER.Code.MethodSystem.Structures;

namespace SER.Code.Helpers.FrameworkExtensions;

public sealed class UcrBridge : FrameworkBridge
{
public static event Action? OnDetected;
protected override string Name => "UncomplicatedCustomRoles";

public void Load()
public override IDependOnFramework.Type FrameworkType { get; } = IDependOnFramework.Type.Ucr;

public UcrBridge()
{
Await(OnDetected).RunCoroutine();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public abstract class ReferenceReturningMethod : ReturningMethod<ReferenceValue>

public abstract class ReferenceReturningMethod<T> : ReferenceReturningMethod
{
public override Type ReturnType => typeof(T);
public sealed override Type ReturnType => typeof(T);

protected new T ReturnValue
{
Expand Down
9 changes: 4 additions & 5 deletions Code/MethodSystem/MethodIndex.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Reflection;
using LabApi.Features.Console;
using SER.Code.Extensions;
using SER.Code.Helpers;
using SER.Code.Helpers.FrameworkExtensions;
using SER.Code.Helpers.ResultSystem;
using SER.Code.MethodSystem.BaseMethods;
Expand All @@ -21,10 +22,8 @@ internal static void Initialize()
NameToMethodIndex.Clear();

AddAllDefinedMethodsInAssembly();

ExiledBridge.OnDetected += () => LoadMethodsOfFramework(IDependOnFramework.Type.Exiled);
CallvoteBridge.OnDetected += () => LoadMethodsOfFramework(IDependOnFramework.Type.Callvote);
UcrBridge.OnDetected += () => LoadMethodsOfFramework(IDependOnFramework.Type.Ucr);

FrameworkBridge.RegisterAll();
}

/// <summary>
Expand Down Expand Up @@ -103,7 +102,7 @@ public static TryGet<Method> TryGetMethod(string name)
return $"There is no method with name '{name}'. Did you mean '{closestMethod ?? "<error>"}'?";
}

private static void LoadMethodsOfFramework(IDependOnFramework.Type framework)
public static void LoadMethodsOfFramework(IDependOnFramework.Type framework)
{
foreach (var method in FrameworkDependentMethods.TryGetValue(framework, out var methods) ? methods : [])
{
Expand Down
Loading