This commit is contained in:
zznty
2023-11-20 19:36:35 +07:00
parent 3e09ef63fb
commit e1ca099dd9
14 changed files with 45 additions and 15 deletions

View File

@@ -0,0 +1,78 @@
using System.Reflection;
using java.lang;
using java.util.concurrent;
using java.util.function;
using net.luckperms.api.@event;
namespace LuckPerms.Torch.Utils.Extensions;
public static class DelegateExtensions
{
private static readonly Func<Consumer, Consumer, Consumer> AndThenDefault =
(Func<Consumer, Consumer, Consumer>)typeof(Consumer).GetMethod("<default>andThen",
BindingFlags.Static | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func<Consumer, Consumer, Consumer>));
private static readonly Func<Predicate, Predicate, Predicate> AndDefault =
(Func<Predicate, Predicate, Predicate>)typeof(Predicate).GetMethod("<default>and",
BindingFlags.Static | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func<Predicate, Predicate, Predicate>));
private static readonly Func<Predicate, Predicate> NegateDefault =
(Func<Predicate, Predicate>)typeof(Predicate).GetMethod("<default>negate",
BindingFlags.Static | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func<Predicate, Predicate>));
private static readonly Func<Predicate, Predicate, Predicate> OrDefault =
(Func<Predicate, Predicate, Predicate>)typeof(Predicate).GetMethod("<default>or",
BindingFlags.Static | BindingFlags.NonPublic)!.CreateDelegate(typeof(Func<Predicate, Predicate, Predicate>));
public static Runnable ToRunnable(this Action action) => new DelegateRunnable(action);
public static Consumer ToConsumer<T>(this Action<T> action) => new DelegateConsumer<T>(action);
public static Supplier ToSupplier<T>(this Func<T> func) where T : notnull => new DelegateSupplier<T>(func);
public static Predicate ToPredicate<T>(this Predicate<T> predicate) => new DelegatePredicate<T>(predicate);
public static Callable ToCallable<T>(this Func<T> func) => new DelegateSupplier<T>(func);
private sealed class DelegatePredicate<T>(Predicate<T> predicate) : Predicate
{
public bool test(object t) => predicate((T)t);
public Predicate and(Predicate other) => AndDefault(this, other);
public Predicate negate() => NegateDefault(this);
public Predicate or(Predicate other) => OrDefault(this, other);
}
private sealed class DelegateSupplier<T>(Func<T> func) : Supplier, Callable where T : notnull
{
public object get() => func();
public object call() => func();
}
private sealed class DelegateConsumer<T>(Action<T> action) : Consumer
{
public void accept(object t)
{
action((T)t);
}
public Consumer andThen(Consumer after) => AndThenDefault(this, after);
}
// ReSharper disable once InconsistentNaming
// lets make it an overload for convenience
public static void execute(this Executor executor, Action action) => executor.execute(action.ToRunnable());
public static EventSubscription subscribe<TEvent>(this EventBus bus, object plugin, Action<TEvent> action)
where TEvent : class, LuckPermsEvent => bus.subscribe(plugin, typeof(TEvent), action.ToConsumer());
public static EventSubscription subscribe<TEvent>(this EventBus bus, Action<TEvent> action)
where TEvent : class, LuckPermsEvent => bus.subscribe(typeof(TEvent), action.ToConsumer());
private sealed class DelegateRunnable(Action action) : Runnable
{
public void run() => action();
}
}

View File

@@ -0,0 +1,18 @@
using java.util;
namespace LuckPerms.Torch.Utils.Extensions;
public static class EnumerableExtensions
{
public static Collection ToCollection<T>(this IEnumerable<T> enumerable)
{
var collection = enumerable is IReadOnlyCollection<T> readOnlyCollection ? new ArrayList(readOnlyCollection.Count) : new ArrayList(((IReadOnlyCollection<T>)(enumerable = enumerable.ToArray())).Count);
foreach (var t in enumerable)
{
collection.add(t);
}
return collection;
}
}

View File

@@ -0,0 +1,57 @@
using System.Collections;
using java.util;
using java.util.stream;
namespace LuckPerms.Torch.Utils.Extensions;
public static class IteratorExtensions
{
public static StreamEnumerable<T> AsEnumerable<T>(this BaseStream stream) => new(stream);
public static IteratorEnumerator<object> GetEnumerator(this BaseStream stream) => new(stream.iterator());
public struct StreamEnumerable<T>(BaseStream stream) : IEnumerable<T>
{
public IteratorEnumerator<T> GetEnumerator() => new(stream.iterator());
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
public static IteratorEnumerator<object> GetEnumerator(this Iterator iterator) => new(iterator);
public static IteratorEnumerable<T> AsEnumerable<T>(this Iterator iterator) => new(iterator);
public struct IteratorEnumerator<T>(Iterator iterator) : IEnumerator<T>
{
public bool MoveNext()
{
if (!iterator.hasNext()) return false;
Current = (T)iterator.next();
return true;
}
public void Reset()
{
throw new NotSupportedException();
}
object? IEnumerator.Current => Current;
public T Current { get; private set; }
public void Dispose()
{
}
}
public struct IteratorEnumerable<T>(Iterator iterator) : IEnumerable<T>
{
public IteratorEnumerator<T> GetEnumerator() => new(iterator);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
}

View File

@@ -0,0 +1,92 @@
using java.io;
using JavaIOException = java.io.IOException;
using SystemIOException = System.IO.IOException;
namespace LuckPerms.Torch.Utils.Extensions;
public static class StreamExtensions
{
public static InputStream GetInputStream(this Stream stream)
{
if (!stream.CanRead)
throw new ArgumentException("Stream should be readable", nameof(stream));
return new WrapperInputStream(stream);
}
private sealed class WrapperInputStream(Stream stream) : InputStream
{
public override int read()
{
try
{
return stream.ReadByte();
}
catch (SystemIOException e)
{
throw new JavaIOException(e.Message, e);
}
}
public override int read(byte[] b, int off, int len)
{
try
{
return stream.Read(b, off, len);
}
catch (SystemIOException e)
{
throw new JavaIOException(e.Message, e);
}
}
public override long skip(long n)
{
if (!stream.CanSeek) return base.skip(n);
try
{
return stream.Seek(n, SeekOrigin.Current);
}
catch (SystemIOException e)
{
throw new JavaIOException(e.Message, e);
}
}
public override int available()
{
try
{
return (int)(stream.Length - stream.Position);
}
catch (SystemIOException e)
{
throw new JavaIOException(e.Message, e);
}
}
public override void reset()
{
if (!stream.CanSeek)
{
base.reset();
return;
}
try
{
stream.Seek(0, SeekOrigin.Begin);
}
catch (SystemIOException e)
{
throw new JavaIOException(e.Message, e);
}
}
public override void close()
{
stream.Dispose();
}
}
}

View File

@@ -0,0 +1,10 @@
using java.util;
namespace LuckPerms.Torch.Utils.Extensions;
public static class UuidExtensions
{
public static ulong GetSteamId(this UUID uuid) => (ulong)uuid.getLeastSignificantBits();
public static UUID GetUuid(this ulong steamId) => new(0, (long)steamId);
}