This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class NetMessagesAccessor | |
{ | |
static Type netMessages = Assembly.GetAssembly(typeof(Provider)).GetType("SDG.Unturned.NetMessages"); | |
public static Delegate ConvertDelegate(Delegate del) => | |
Delegate.CreateDelegate( | |
netMessages.GetNestedType(del.GetType().Name), | |
del.Target, | |
del.Method | |
); | |
public delegate void ClientWriteHandler(NetPakWriter writer); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FieldDelegator<T> : FieldDelegator where T : Delegate | |
{ | |
public FieldDelegator(FieldInfo field, object owner = null) : base(field, owner) { } | |
public static void Do(FieldInfo fld, object owner = null, T del = null, bool combine = true) => | |
FieldDelegator.Do(fld, owner, del, combine); | |
public void Do(T del, bool combine = true) => Do(Instance, Owner, del, combine); | |
} | |
public class FieldDelegator | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// FileStream that not consuming too much memory to read/write. | |
/// </summary> | |
public class LazyFileStream : | |
FileStream, IDisposable | |
{ | |
public LazyFileStream(string path, FileMode mode = FileMode.OpenOrCreate, FileAccess access = FileAccess.ReadWrite, FileShare share = FileShare.ReadWrite, int buffer = 4096, FileOptions options = FileOptions.SequentialScan | FileOptions.Asynchronous) : base(path, mode, access, share, buffer, options) | |
{ } | |
public void Write(byte[] data, int offset = 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public struct TimeSince | |
{ | |
public TimeSince(DateTime? from = default) | |
{ | |
From = from.HasValue ? from.Value : DateTime.Now; | |
} | |
readonly DateTime From; | |
public static implicit operator float(TimeSince @this) => (float)((TimeSpan)@this).TotalSeconds; | |
public static implicit operator TimeSince(float @this) => new TimeSince(DateTime.Now.AddSeconds(@this)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static T To<T>(this IConvertible convertible) | |
where T : IConvertible | |
{ | |
if (convertible is null) | |
return default; | |
var convertMethod = typeof(IConvertible).GetMethod($"To{typeof(T).Name}"); | |
if (convertMethod is null) | |
throw new ArgumentException($"{convertible.GetType().Name} can't be converted to {typeof(T).Name}"); | |
var value = convertMethod.Invoke(convertible, new object[] { null }); | |
return (T)value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
public static class TaskExtensions | |
{ | |
public delegate void ExceptionCallback(Exception exception); | |
public delegate void CompleteCallback<in T>(T result); | |
public delegate void CompleteCallback(); | |
public static Task<T> WithResult<T>(this Task task, Func<Task, T> func) => |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Creates new <see cref="LinkedList{T}"/> with reversed nodes. | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
public static LinkedList<T> Reverse<T>(this LinkedList<T> originalList) | |
{ | |
var reversedList = new LinkedList<T>(); | |
var last = originalList.Last; | |
reversedList.AddFirst(last.Value); | |
while ((last = last.Previous) is not null) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// <see cref="List{T}"/> with functionality of <see cref="Queue{T}"/> that allows to enqueue elements in order using <see cref="Comparison{T}"/>. | |
/// </summary> | |
/// <typeparam name="T">Type of elements.</typeparam> | |
public class OrderedQueue<T> : List<T> | |
{ | |
public OrderedQueue(Comparison<T> comparison = null) | |
{ | |
OrderComparison = comparison ?? ((a, b) => a.Equals(b) ? 0 : -1); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ConditionalWeakDictionary<TKey, TValue> : IDictionary<TKey, TValue> | |
where TKey : class | |
where TValue : class | |
{ | |
private readonly ConditionalWeakTable<TKey, TValue> table = new(); | |
public TValue this[TKey key] | |
{ | |
get => Get(key); | |
set => Set(key, value); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Collections.Generic; | |
/// <summary> | |
/// Enum extensions. | |
/// </summary> | |
public static class EnumExtensions | |
{ | |
/// <inheritdoc cref="EnumExtensions{TEnum}.Values"/> |
OlderNewer