This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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); |
NewerOlder