Skip to content

Instantly share code, notes, and snippets.

View thenameless314159's full-sized avatar

Nameless thenameless314159

View GitHub Profile
#if NETCOREAPP3_1
using System;
using System.Linq;
using System.Text;
using System.Buffers;
using NUnit.Framework;
using System.Reflection;
using System.Buffers.Binary;
using static FastExpressionCompiler.LightExpression.Expression;
// ReSharper disable InconsistentNaming
#if NETCOREAPP3_1
using System;
using System.Linq;
using System.Text;
using System.Buffers;
using NUnit.Framework;
using System.Reflection;
using System.Buffers.Binary;
using static FastExpressionCompiler.LightExpression.Expression;
// ReSharper disable InconsistentNaming
#if NETCOREAPP3_1
using System;
using System.Linq;
using System.Text;
using System.Buffers;
using NUnit.Framework;
using System.Reflection;
using System.Buffers.Binary;
using static FastExpressionCompiler.LightExpression.Expression;
// ReSharper disable InconsistentNaming
public ref struct SpanWriter
{
private readonly Span<byte> _buffer;
private int _position;
public int Remaining => Count - Position;
public int Count { get; }
public int Position
{
public class DofusFrameDecoder : HeaderFrameDecoder<DofusMetadata>
{
protected override bool TryParseHeader(ref SequenceReader<byte> input, out DofusMetadata frame, out int length)
{
frame = default;
length = 0;
if (!input.TryReadBigEndian(out short header)) return false;
if (!input.TryReadBigEndian(out int instanceId)) return false;
var messageId = header >> 2;
@thenameless314159
thenameless314159 / With.cs
Created May 31, 2019 20:33
Implementation of With from F# in C#
/// <summary>
/// Provide a thread-safe way to handle immutable objects.
/// </summary>
/// <typeparam name="T"></typeparam>
public static class With<T>
{
static With() => Cache.Register();
/// <summary>
/// Create a new instance of <see cref="T"/> with the specified property to replace with the provided <see cref="value"/>.
public class CustomVarTests
{
private static readonly ISizing Sizing = new SizingBuilder()
.Register(new CustomVarInt16SizeStorage())
.Register(new CustomVarUInt16SizeStorage())
.Register(new CustomVarInt32SizeStorage())
.Register(new CustomVarUInt32SizeStorage())
.Register(new CustomVarInt64SizeStorage())
.Register(new CustomVarUInt64SizeStorage()).Build();
@thenameless314159
thenameless314159 / CustomVarInt16BinaryStorage.cs
Created May 17, 2019 13:15
Astron.Binary storage implementation for the CustomVar<short> provided on the wiki summary of Astron.
public class CustomVarInt16BinaryStorage : IBinaryStorage<CustomVar<short>>
{
public Func<IReader, CustomVar<short>> ReadValue => reader =>
{
var result = 0;
for (var offset = 0; offset < 16; offset += 7)
{
var readByte = reader.ReadValue<sbyte>();
var hasNext = (readByte & 128) == 128;
@thenameless314159
thenameless314159 / ConsoleOutputStrategy.cs
Created May 10, 2019 09:59
Astron: LoggingStrategy imlemention
/// <summary>
/// Use this strategy to output logs to your console.
/// </summary>
public class ConsoleOutputStrategy : LoggingStrategy
{
private readonly Dictionary<LogLevel, ConsoleColor> _colorWrapper = new Dictionary<LogLevel, ConsoleColor>()
{
{LogLevel.Trace, ConsoleColor.DarkGreen },
{LogLevel.Debug, ConsoleColor.Green },
{LogLevel.Info, ConsoleColor.Blue },
@thenameless314159
thenameless314159 / DynamicMethodBuilder.cs
Created July 8, 2017 17:33
simple dynamic method builder (basic constructor)
public delegate TObj InitializerDlg();
public static Delegate GetDynamicDlg()
{
var ctor = typeof(TObj).GetTypeInfo()
.DeclaredConstructors
.First(
c => c.GetParameters().Length < 1);
var method = new DynamicMethod(