-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
removing some debug code from unity added a UI framework project but no code yet added and implemented a network framework (currently only encoders done) added tests
- Loading branch information
Showing
58 changed files
with
2,811 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
using System.Text; | ||
using Progression.IO.Encoders.Special; | ||
using Progression.Util; | ||
|
||
namespace Progression.IO.Encoders.Base | ||
{ | ||
public abstract class ArrayEncoderBase<TBase, TRoot>:Derived1EncoderBase<TBase[], TBase, TRoot> | ||
{ | ||
protected ArrayEncoderBase(IEncoder<TBase, TRoot> baseEncoder, IEncoder<TRoot, TRoot> rootEncoder) : base(baseEncoder, rootEncoder, "Array", new Guid("F9251789-FEAA-4184-A9D4-9090ED902E92")) {} | ||
|
||
|
||
public abstract void EncodeHeadless(TBase[] objs, PacketData data); | ||
public abstract TBase[] DecodeHeadless(PacketData data, int arrayLength); | ||
|
||
public override TBase[] Decode(PacketData data) | ||
{ | ||
int arrayLength = Types.Int32.Decode(data); | ||
return DecodeHeadless(data, arrayLength); | ||
} | ||
|
||
public override void Encode(TBase[] objs, PacketData data) | ||
{ | ||
Types.Int32.Encode(objs.Length, data); | ||
EncodeHeadless(objs, data); | ||
} | ||
|
||
public override int Estimate(TBase[] objs) | ||
{ | ||
if (BaseEncoder.FixedSize) return objs.Length * BaseEncoder.ByteSize + 4; | ||
|
||
int result = 4; | ||
foreach (var obj in objs) { | ||
result += BaseEncoder.Estimate(obj); | ||
} | ||
return result; | ||
} | ||
|
||
|
||
} | ||
} |
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,35 @@ | ||
using System; | ||
using Progression.IO.Encoders.Special; | ||
using Progression.Util; | ||
|
||
namespace Progression.IO.Encoders.Base { | ||
public abstract class Derived1EncoderBase<TType, TBase, TRoot>:DerivedEncoderBase<TType, TRoot> | ||
{ | ||
public Derived1EncoderBase(IEncoder<TBase, TRoot> baseEncoder, IEncoder<TRoot, TRoot> rootEncoder, string derivationTypeName, Guid guidNamespace ) | ||
{ | ||
BaseEncoder = baseEncoder; | ||
RootEncoder = rootEncoder; | ||
var name = $"{derivationTypeName}<{BaseEncoder.Name}>"; | ||
Guid = GuidUtil.Create(guidNamespace, name); | ||
Name = name; | ||
} | ||
public IEncoder<TBase, TRoot> BaseEncoder { get; } | ||
public IEncoder<TRoot, TRoot> RootEncoder { get; } | ||
|
||
public override string Name { get; } | ||
public override Guid Guid { get; } | ||
|
||
|
||
protected override NullableEncoderBase<TType, TRoot> CreateNullable() | ||
{ | ||
return new NullableEncoder<TType, TRoot>(this, RootEncoder); | ||
} | ||
|
||
protected override ArrayEncoderBase<TType, TRoot> CreateArray() | ||
{ | ||
return new ArrayEncoder<TType, TRoot>(this, RootEncoder); | ||
} | ||
|
||
public override bool FixedSize => false; | ||
} | ||
} |
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,35 @@ | ||
using System; | ||
using Progression.IO.Encoders.Special; | ||
using Progression.Util; | ||
|
||
namespace Progression.IO.Encoders.Base { | ||
public abstract class Derived2EncoderBase<TType, TBase1, TRoot1, TBase2, TRoot2>:DerivedEncoderBase<TType, TType> | ||
{ | ||
public Derived2EncoderBase(IEncoder<TBase1, TRoot1> base1Encoder, IEncoder<TBase2, TRoot2> base2Encoder, string derivationTypeName, Guid guidNamespace ) | ||
{ | ||
Base1Encoder = base1Encoder; | ||
Base2Encoder = base2Encoder; | ||
var name = $"{derivationTypeName}<{Base1Encoder.Name}, {Base2Encoder.Name}>"; | ||
Guid = GuidUtil.Create(guidNamespace, name); | ||
Name = name; | ||
} | ||
public IEncoder<TBase1, TRoot1> Base1Encoder { get; } | ||
public IEncoder<TBase2, TRoot2> Base2Encoder { get; } | ||
|
||
public override string Name { get; } | ||
public override Guid Guid { get; } | ||
|
||
|
||
protected override NullableEncoderBase<TType, TType> CreateNullable() | ||
{ | ||
return new NullableEncoder<TType, TType>(this, this); | ||
} | ||
|
||
protected override ArrayEncoderBase<TType, TType> CreateArray() | ||
{ | ||
return new ArrayEncoder<TType, TType>(this, this); | ||
} | ||
|
||
public override bool FixedSize => false; | ||
} | ||
} |
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,7 @@ | ||
namespace Progression.IO.Encoders.Base | ||
{ | ||
public abstract class DerivedEncoderBase<TType, TRoot>:EncoderBase<TType, TRoot> | ||
{ | ||
public override bool IsBaseEncoder => false; | ||
} | ||
} |
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,69 @@ | ||
using System; | ||
using System.Linq; | ||
using Progression.IO.Encoders.Special; | ||
|
||
namespace Progression.IO.Encoders.Base | ||
{ | ||
public abstract class EncoderBase<TType, TRoot> : IEncoder<TType, TRoot> | ||
{ | ||
protected EncoderBase() | ||
{ | ||
// ReSharper disable once VirtualMemberCallInConstructor | ||
if (IsBaseEncoder) { | ||
if (!Types._registeredBaseTypes.Contains(this)) { | ||
Types._registeredBaseTypes.Add(this); | ||
} | ||
} | ||
} | ||
public abstract void Encode(TType obj, PacketData data); | ||
void IEncoder.Encode(object obj, PacketData data) => Encode((TType) obj, data); | ||
|
||
object IEncoder.Decode(PacketData data) => Decode(data); | ||
|
||
int IEncoder.Estimate(object obj) => Estimate((TType) obj); | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return obj != null & obj is IEncoder & ((IEncoder) obj)?.EncodingType == EncodingType; | ||
} | ||
|
||
public abstract TType Decode(PacketData data); | ||
public abstract int Estimate(TType obj); | ||
public abstract string Name { get; } | ||
public abstract Guid Guid { get; } | ||
public abstract bool FixedSize { get; } | ||
|
||
public virtual int ByteSize => -1; | ||
|
||
public virtual bool IsBaseEncoder => typeof(TType) == typeof(TRoot); | ||
|
||
IEncoder IEncoder.Nullable => NullableObject; | ||
protected virtual IEncoder NullableObject => Nullable; | ||
|
||
IEncoder IEncoder.Array => Array; | ||
public Type EncodingType => typeof(TRoot); | ||
|
||
|
||
private NullableEncoderBase<TType, TRoot> _nullable; | ||
public NullableEncoderBase<TType, TRoot> Nullable => _nullable = _nullable ?? CreateNullable(); | ||
|
||
protected abstract NullableEncoderBase<TType, TRoot> CreateNullable(); | ||
|
||
private ArrayEncoderBase<TType, TRoot> _array; | ||
public ArrayEncoderBase<TType, TRoot> Array => _array = _array ?? CreateArray(); | ||
|
||
protected abstract ArrayEncoderBase<TType, TRoot> CreateArray(); | ||
} | ||
|
||
public abstract class EncoderBase<T> : EncoderBase<T, T> { | ||
|
||
protected override ArrayEncoderBase<T, T> CreateArray() | ||
{ | ||
return new ArrayEncoder<T, T>(this, this); | ||
} | ||
protected override NullableEncoderBase<T, T> CreateNullable() | ||
{ | ||
return new NullableEncoder<T, T>(this, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
|
||
namespace Progression.IO.Encoders.Base { | ||
public interface IEncoder<TType> : IEncoder { | ||
void Encode(TType obj, PacketData data); | ||
new TType Decode(PacketData data); | ||
int Estimate(TType obj); | ||
} | ||
|
||
public interface IEncoder<TType, TRoot> : IEncoder<TType> { | ||
new NullableEncoderBase<TType, TRoot> Nullable { get; } | ||
new ArrayEncoderBase<TType, TRoot> Array { get; } | ||
} | ||
|
||
public interface IEncoder { | ||
void Encode(object obj, PacketData data); | ||
object Decode(PacketData data); | ||
int Estimate(object obj); | ||
string Name { get; } | ||
Guid Guid { get; } | ||
bool FixedSize { get; } | ||
int ByteSize { get; } | ||
IEncoder Nullable { get; } | ||
IEncoder Array { get; } | ||
Type EncodingType { get; } | ||
} | ||
} |
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,27 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Progression.IO.Encoders.Special; | ||
|
||
namespace Progression.IO.Encoders.Base { | ||
public abstract class NullableEncoderBase<TType, TRoot> : Derived1EncoderBase<TType, TType, TRoot> | ||
{ | ||
protected NullableEncoderBase(IEncoder<TType, TRoot> baseEncoder, IEncoder<TRoot, TRoot> rootEncoder) : base(baseEncoder, rootEncoder, "Nullable", new Guid("F0691A4D-F47D-4FF6-901F-C8832A434ABE")) { } | ||
|
||
protected override NullableEncoderBase<TType, TRoot> CreateNullable() | ||
{ | ||
return this; | ||
} | ||
|
||
protected override ArrayEncoderBase<TType, TRoot> CreateArray() | ||
{ | ||
return new NullableArrayEncoder<TType, TRoot>(BaseEncoder, RootEncoder); | ||
} | ||
|
||
public override int Estimate(TType obj) | ||
{ | ||
return EqualityComparer<TType>.Default.Equals(obj, default(TType))?1:BaseEncoder.Estimate(obj) + 1; | ||
} | ||
|
||
public override bool FixedSize => false; | ||
} | ||
} |
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,128 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using Progression.IO.Encoders.Special; | ||
|
||
namespace Progression.IO.Encoders.Base | ||
{ | ||
public abstract class StructEncoderBase<T> : EncoderBase<T> where T : struct | ||
{ | ||
public StructEncoderBase() | ||
{ | ||
_wrapper = new NullableStructWrapper(this); | ||
} | ||
|
||
public override bool FixedSize => true; | ||
|
||
public abstract override int ByteSize { get; } | ||
|
||
public sealed override int Estimate(T obj) | ||
{ | ||
return ByteSize; | ||
} | ||
|
||
private readonly NullableStructWrapper _wrapper; | ||
|
||
protected override IEncoder NullableObject => Nullable; | ||
private NullableEncoderBase<T?, T> _nullable; | ||
public new NullableEncoderBase<T?, T> Nullable => _nullable = _nullable ?? CreateNullable(); | ||
|
||
protected new NullableEncoderBase<T?, T> CreateNullable() | ||
{ | ||
return new NullableEncoder<T?, T>(_wrapper, this); | ||
} | ||
|
||
private class NullableStructWrapper : IEncoder<T?, T> | ||
{ | ||
private readonly StructEncoderBase<T> _encoderImplementation; | ||
public NullableStructWrapper(StructEncoderBase<T> encoderImplementation) | ||
{ | ||
_encoderImplementation = encoderImplementation; | ||
} | ||
|
||
public void Encode(T? obj, PacketData data) | ||
{ | ||
|
||
_encoderImplementation.Encode(obj.Value, data); | ||
} | ||
|
||
public void Encode(object obj, PacketData data) | ||
{ | ||
((IEncoder) _encoderImplementation).Encode(obj, data); | ||
} | ||
|
||
object IEncoder.Decode(PacketData data) | ||
{ | ||
return ((IEncoder) _encoderImplementation).Decode(data); | ||
} | ||
|
||
public int Estimate(object obj) | ||
{ | ||
return ((IEncoder) _encoderImplementation).Estimate(obj); | ||
} | ||
|
||
public T? Decode(PacketData data) | ||
{ | ||
return _encoderImplementation.Decode(data); | ||
} | ||
|
||
public int Estimate(T? obj) | ||
{ | ||
return _encoderImplementation.Estimate(obj.Value); | ||
} | ||
|
||
public string Name => _encoderImplementation.Name; | ||
|
||
public Guid Guid => _encoderImplementation.Guid; | ||
|
||
public bool FixedSize => _encoderImplementation.FixedSize; | ||
|
||
public int ByteSize => _encoderImplementation.ByteSize; | ||
IEncoder IEncoder.Nullable => ((IEncoder) _encoderImplementation).Nullable; | ||
|
||
IEncoder IEncoder.Array => ((IEncoder) _encoderImplementation).Array; | ||
public Type EncodingType => _encoderImplementation.EncodingType; | ||
|
||
public NullableEncoderBase<T?, T> Nullable => _encoderImplementation.Nullable; | ||
|
||
public ArrayEncoderBase<T?, T> Array => Nullable.Array; | ||
|
||
public static implicit operator StructEncoderBase<T>(NullableStructWrapper value) | ||
{ | ||
return value._encoderImplementation; | ||
} | ||
|
||
|
||
public static implicit operator NullableStructWrapper(StructEncoderBase<T> value) | ||
{ | ||
return value._wrapper; | ||
} | ||
|
||
public static bool operator ==(NullableStructWrapper obj1, StructEncoderBase<T> obj2) | ||
{ | ||
if (ReferenceEquals(obj1, null)) { | ||
return false; | ||
} | ||
if (ReferenceEquals(obj2, null)) { | ||
return false; | ||
} | ||
|
||
return ReferenceEquals(obj1._encoderImplementation, obj2); | ||
} | ||
|
||
public static bool operator !=(NullableStructWrapper obj1, StructEncoderBase<T> obj2) | ||
{ | ||
return !(obj1 == obj2); | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return _encoderImplementation.GetHashCode(); | ||
} | ||
|
||
public override bool Equals(object obj) | ||
{ | ||
return ReferenceEquals(this, obj) | (obj is StructEncoderBase<T> & (StructEncoderBase<T> ) obj == this); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.