This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dskprt/rewrite
Rewrite
- Loading branch information
Showing
1,866 changed files
with
233,156 additions
and
4,143 deletions.
There are no files selected for viewing
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
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
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
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; | ||
} | ||
} | ||
} |
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
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; | ||
} | ||
} | ||
} |
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
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)(); | ||
} | ||
} | ||
} |
Oops, something went wrong.