Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Merge pull request #12 from dskprt/rewrite
Browse files Browse the repository at this point in the history
Rewrite
  • Loading branch information
dskprt authored Aug 28, 2021
2 parents 44ce376 + 3d970aa commit 69a89e8
Show file tree
Hide file tree
Showing 1,866 changed files with 233,156 additions and 4,143 deletions.
1 change: 0 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ insert_final_newline = false
# Organize usings
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = true
file_header_template = unset

# this. and Me. preferences
dotnet_style_qualification_for_event = false:suggestion
Expand Down
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# jetbrains rider
.idea

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
Expand Down Expand Up @@ -359,4 +362,4 @@ MigrationBackup/
.ionide/

# Fody - auto-generated XML schema
FodyWeavers.xsd
FodyWeavers.xsd
25 changes: 25 additions & 0 deletions nylium.Core/Block/BlockAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using nylium.Utilities;

namespace nylium.Core.Block {

class BlockAttribute : Attribute {

public Identifier Id { get; }
public int ProtocolId { get; }

public ushort MinimumState { get; }
public ushort MaximumState { get; }
public ushort DefaultState { get; }

public BlockAttribute(string id, int protocolId,
ushort minState, ushort maxState, ushort defaultState) {

Id = new Identifier(id);
ProtocolId = protocolId;
MinimumState = minState;
MaximumState = maxState;
DefaultState = defaultState;
}
}
}
65 changes: 65 additions & 0 deletions nylium.Core/Block/BlockBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using IntervalTree;
using Ionic.Zlib;
using Jil;
using nylium.Utilities;
using nylium.Utilities.Collections;

namespace nylium.Core.Block {

public abstract class BlockBase {

public const int BITS_PER_BLOCK = 15;

// TODO is it worth caching this?
private BlockAttribute Attribute {
get {
return GetType().GetCustomAttribute<BlockAttribute>(false);
}
}

public Identifier Id { get { return Attribute.Id; } }
public int ProtocolId { get { return Attribute.ProtocolId; } }

public ushort MinimumState { get { return Attribute.MinimumState; } }
public ushort MaximumState { get { return Attribute.MaximumState; } }
public ushort DefaultState { get { return Attribute.DefaultState; } }

public abstract ushort State { get; set; }

public BlockBase() { }
public BlockBase(ushort state) { }

private static BlockAttribute GetAttribute(Type block) {
return block.GetCustomAttribute<BlockAttribute>(false);
}

public static Identifier GetId(Type block) {
return GetAttribute(block).Id;
}

public static int GetProtocolId(Type block) {
return GetAttribute(block).ProtocolId;
}

public static ushort GetMinimumState(Type block) {
return GetAttribute(block).MinimumState;
}

public static ushort GetMaximumState(Type block) {
return GetAttribute(block).MaximumState;
}

public static ushort GetDefaultState(Type block) {
return GetAttribute(block).DefaultState;
}
}
}
73 changes: 73 additions & 0 deletions nylium.Core/Block/BlockRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using nylium.Utilities.Collections;
using IntervalTree;
using nylium.Logging;
using nylium.Utilities;

namespace nylium.Core.Block {

public class BlockRepository {

private static readonly MultiKeyDictionary<Func<BlockBase>> blocks = new();
private static readonly IntervalTree<ushort, Func<ushort, BlockBase>> stateBlocks = new();

public static void Initialize() {
Stopwatch stopwatch = new();
stopwatch.Start();

Type[] ctorParams = { typeof(ushort) };

Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.Namespace == "nylium.Core.Block.Blocks")
.ToList()
.ForEach(t => {
ConstructorInfo constructor = t.GetConstructor(ctorParams);
ConstructorInfo defaultConstructor = t.GetConstructor(Type.EmptyTypes);
if(constructor == null || defaultConstructor == null) {
Logger.Debug($"Type [{t.FullName}] is probably not a block, ignoring");
return;
}
ParameterExpression parameter = Expression.Parameter(typeof(ushort));
Func<ushort, BlockBase> ctor = Expression.Lambda<Func<ushort, BlockBase>>(
Expression.New(constructor, parameter), parameter).Compile();
Func<BlockBase> defaultCtor = Expression.Lambda<Func<BlockBase>>(
Expression.New(defaultConstructor)).Compile();
BlockAttribute attribute = t.GetCustomAttribute<BlockAttribute>(false);
if(attribute == null) {
Logger.Warning($"Type [{t.FullName}] has no BlockAttribute attribute.");
return;
}
stateBlocks.Add(attribute.MinimumState, attribute.MaximumState, ctor);
blocks.Add(attribute.Id, defaultCtor);
blocks.Add(attribute.ProtocolId, defaultCtor);
blocks.FinishAdd(defaultCtor);
});

stopwatch.Stop();
Logger.Debug("Initialized block repository in " + Math.Round(stopwatch.Elapsed.TotalMilliseconds, 2) + "ms");
}

public static BlockBase Create(ushort state) {
return stateBlocks.Query(state).First()(state);
}

public static BlockBase Create(Identifier id) {
return blocks.Get(id)();
}

public static BlockBase Create(int protocolId) {
return blocks.Get(protocolId)();
}
}
}
Loading

0 comments on commit 69a89e8

Please sign in to comment.