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

Commit

Permalink
add position, angle, array, slot and bytearray data type; release v0.…
Browse files Browse the repository at this point in the history
…0.1a
  • Loading branch information
dskprt committed Mar 3, 2021
1 parent 8ae34c9 commit bdaecae
Show file tree
Hide file tree
Showing 32 changed files with 335 additions and 118 deletions.
18 changes: 18 additions & 0 deletions nylium.Core/Inventory/InventorySlot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using fNbt;

namespace nylium.Core.Inventory {

public class InventorySlot {

public int ItemId { get; }
public sbyte Count { get; }
public NbtFile NBT { get; }

public InventorySlot(int itemId, sbyte count, NbtFile nbt) {
ItemId = itemId;
Count = count;
NBT = nbt;
}
}
}
13 changes: 13 additions & 0 deletions nylium.Core/nylium.Core.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RuntimeIdentifiers>win-x64;osx-x64;linux-x64;linux-musl-x64;linux-arm;linux-arm64</RuntimeIdentifiers>
<LangVersion>9.0</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CoreFNBT" Version="1.0.9" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions nylium.Networking/DataTypes/Angle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.IO;

namespace nylium.Networking.DataTypes {

public class Angle : DataType<double> {

public Angle() : base(0) { }
public Angle(double value) : base(value) { }

public override int Read(Stream stream) {
byte[] read = new byte[1];
int bytesRead = stream.Read(read, 0, 1);

Value = (360 / 256) * read[0];
return bytesRead;
}

public override void Write(Stream stream) {
stream.Write(new byte[1] { (byte) Math.Round((256 / 360) * Value, MidpointRounding.AwayFromZero) }); ;
}
}
}
48 changes: 48 additions & 0 deletions nylium.Networking/DataTypes/Array.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Reflection;

namespace nylium.Networking.DataTypes {

public class Array<I, T> : DataType<T[]> where T : DataType<I> {

private static readonly Func<T> defaultCtor;

public int Count { get; }

static Array() {
ConstructorInfo constructor = typeof(T).GetConstructor(new Type[0]);
defaultCtor = Expression.Lambda<Func<T>>(Expression.New(constructor)).Compile();
}

public Array(int count) : base(new T[count]) {
Count = count;
}

public Array(T[] value) : base(value) {
Count = value.Length;
}

public override int Read(Stream stream) {
int bytesRead = 0;
T[] arr = new T[Count];

for(int i = 0; i < Count; i++) {
T t = defaultCtor();
bytesRead += t.Read(stream);

arr[i] = t;
}

return bytesRead;
}

public override void Write(Stream stream) {
for(int i = 0; i < Count; i++) {
Value[Count].Write(stream);
}
}
}
}
7 changes: 3 additions & 4 deletions nylium.Networking/DataTypes/Boolean.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ public class Boolean : DataType<bool> {
public Boolean() : base(false) { }
public Boolean(bool value) : base(value) { }

public override void Read(Stream stream, out int bytesRead) {
bytesRead = 0;
public override int Read(Stream stream) {
bool result;
byte[] read = new byte[1];

stream.Read(read, 0, 1);
bytesRead++;
int bytesRead = stream.Read(read, 0, 1);

if(read[0] == 0x00) result = false;
else if(read[0] == 0x01) result = true;
else throw new ArgumentException("Invalid value for Boolean data type");

Value = result;
return bytesRead;
}

public override void Write(Stream stream) {
Expand Down
7 changes: 3 additions & 4 deletions nylium.Networking/DataTypes/Byte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ public class Byte : DataType<sbyte> {
public Byte() : base(0) { }
public Byte(sbyte value) : base(value) { }

public override void Read(Stream stream, out int bytesRead) {
bytesRead = 0;
public override int Read(Stream stream) {
byte[] read = new byte[1];

stream.Read(read, 0, 1);
bytesRead++;
int bytesRead = stream.Read(read, 0, 1);

Value = (sbyte) read[0];
return bytesRead;
}

public override void Write(Stream stream) {
Expand Down
24 changes: 24 additions & 0 deletions nylium.Networking/DataTypes/ByteArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System;
using System.IO;

namespace nylium.Networking.DataTypes {

public class ByteArray : DataType<sbyte[]> {

public int Count { get; }

public ByteArray(int count) : base(new sbyte[count]) {
Count = count;
}

public ByteArray(sbyte[] value) : base(value) { }

public override int Read(Stream stream) {
return stream.Read((byte[]) (Array) Value, 0, Count);
}

public override void Write(Stream stream) {
stream.Write((byte[]) (Array) Value);
}
}
}
6 changes: 4 additions & 2 deletions nylium.Networking/DataTypes/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ public class Chat : DataType<dynamic> {

public Chat() : base(null) { }

public override void Read(Stream stream, out int bytesRead) {
public override int Read(Stream stream) {
String @string = new String();
@string.Read(stream, out bytesRead);
int bytesRead = @string.Read(stream);

Value = JSON.DeserializeDynamic(@string.Value);
return bytesRead;
}

public override void Write(Stream stream) {
Expand All @@ -21,6 +22,7 @@ public override void Write(Stream stream) {
}

// awful hack because you can't use dynamic variables in base constructor or something
// TODO find a better way to do this
public void SetValue(dynamic value) {
Value = value;
}
Expand Down
2 changes: 1 addition & 1 deletion nylium.Networking/DataTypes/DataType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public DataType(T value) {
Value = value;
}

public abstract void Read(Stream stream, out int bytesRead);
public abstract int Read(Stream stream);
public abstract void Write(Stream stream);
}
}
9 changes: 4 additions & 5 deletions nylium.Networking/DataTypes/Double.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ public class Double : DataType<double> {
public Double() : base(0) { }
public Double(double value) : base(value) { }

public override void Read(Stream stream, out int bytesRead) {
bytesRead = 0;
public override int Read(Stream stream) {
byte[] read = new byte[8];

stream.Read(read, 0, 8);
bytesRead += 8;
int bytesRead = stream.Read(read, 0, 8);

Value = read.ReadBigEndianD();

return bytesRead;
}

public override void Write(Stream stream) {
Expand Down
Loading

0 comments on commit bdaecae

Please sign in to comment.