Browse Source

Cleaned up and added a few more types to ETFEncoder

pull/22/head
RogueException 9 years ago
parent
commit
0c4af8d042
4 changed files with 95 additions and 32 deletions
  1. +9
    -0
      src/Discord.Net.Net45/Discord.Net.csproj
  2. +9
    -0
      src/Discord.Net/ETF/ETFDecoder.cs
  3. +45
    -32
      src/Discord.Net/ETF/ETFEncoder.cs
  4. +32
    -0
      src/Discord.Net/ETF/ETFType.cs

+ 9
- 0
src/Discord.Net.Net45/Discord.Net.csproj View File

@@ -421,6 +421,15 @@
<Compile Include="..\Discord.Net\Enums\UserStatus.cs">
<Link>Enums\UserStatus.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\ETF\ETFDecoder.cs">
<Link>ETF\ETFDecoder.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\ETF\ETFEncoder.cs">
<Link>ETF\ETFEncoder.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\ETF\ETFType.cs">
<Link>ETF\ETFType.cs</Link>
</Compile>
<Compile Include="..\Discord.Net\Events\ChannelEventArgs.cs">
<Link>Events\ChannelEventArgs.cs</Link>
</Compile>


+ 9
- 0
src/Discord.Net/ETF/ETFDecoder.cs View File

@@ -0,0 +1,9 @@
using System;
using System.IO;

namespace Discord.ETF
{
public class ETFDecoder
{
}
}

src/Discord.Net/API/Client/ETFEncoder.cs → src/Discord.Net/ETF/ETFEncoder.cs View File

@@ -2,23 +2,24 @@
using System.Collections.Generic;
using System.IO;

namespace Discord.API.Client
namespace Discord.ETF
{
//TODO: Floats, Atoms, Tuples, Lists, Dictionaries

public class ETFEncoder
public unsafe class ETFEncoder
{
private const byte SMALL_INTEGER_EXT = 97;
private const byte INTEGER_EXT = 98;
private const byte SMALL_BIG_EXT = 110;
private const byte SMALL_ATOM_EXT = 115;
private readonly static byte[] _nilBytes = new byte[] { (byte)ETFType.SMALL_ATOM_EXT, 3, (byte)'n', (byte)'i', (byte)'l' };
private readonly static byte[] _falseBytes = new byte[] { (byte)ETFType.SMALL_ATOM_EXT, 5, (byte)'f', (byte)'a', (byte)'l', (byte)'s', (byte)'e' };
private readonly static byte[] _trueBytes = new byte[] { (byte)ETFType.SMALL_ATOM_EXT, 4, (byte)'t', (byte)'r', (byte)'u', (byte)'e' };
private byte[] _writeBuffer;

private readonly static byte[] nilBytes = new byte[] { SMALL_ATOM_EXT, 3, (byte)'n', (byte)'i', (byte)'l' };
private readonly static byte[] falseBytes = new byte[] { SMALL_ATOM_EXT, 5, (byte)'f', (byte)'a', (byte)'l', (byte)'s', (byte)'e' };
private readonly static byte[] trueBytes = new byte[] { SMALL_ATOM_EXT, 4, (byte)'t', (byte)'r', (byte)'u', (byte)'e' };
public ETFEncoder()
{
_writeBuffer = new byte[11];
}

private void WriteNil(BinaryWriter writer) => Append(writer, nilBytes);
public void Write(BinaryWriter writer, bool value) => Append(writer, value ? trueBytes : falseBytes);
private void WriteNil(BinaryWriter writer) => Append(writer, _nilBytes);
public void Write(BinaryWriter writer, bool value) => Append(writer, value ? _trueBytes : _falseBytes);

public void Write(BinaryWriter writer, byte value) => Write(writer, (ulong)value);
public void Write(BinaryWriter writer, sbyte value) => Write(writer, (long)value);
@@ -29,22 +30,19 @@ namespace Discord.API.Client
public void Write(BinaryWriter writer, ulong value)
{
if (value <= byte.MaxValue)
Append(writer, new byte[] { SMALL_INTEGER_EXT, (byte)value });
Append(writer, new byte[] { (byte)ETFType.SMALL_INTEGER_EXT, (byte)value });
else if (value <= int.MaxValue)
{
Append(writer, new byte[]
{
INTEGER_EXT,
Append(writer, (byte)ETFType.INTEGER_EXT,
(byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF)
});
(byte)(value & 0xFF));
}
else
{
var buffer = new byte[3 + 8];
buffer[0] = SMALL_BIG_EXT;
buffer[0] = (byte)ETFType.SMALL_BIG_EXT;
//buffer[1] = 0; //Always positive

byte bytes = 0;
@@ -62,22 +60,22 @@ namespace Discord.API.Client
public void Write(BinaryWriter writer, long value)
{
if (value >= byte.MinValue && value <= byte.MaxValue)
Append(writer, new byte[] { SMALL_INTEGER_EXT, (byte)value });
{
Append(writer, (byte)ETFType.SMALL_INTEGER_EXT,
(byte)value);
}
else if (value >= int.MinValue && value <= int.MaxValue)
{
Append(writer, new byte[]
{
INTEGER_EXT,
Append(writer, (byte)ETFType.INTEGER_EXT,
(byte)((value >> 24) & 0xFF),
(byte)((value >> 16) & 0xFF),
(byte)((value >> 8) & 0xFF),
(byte)(value & 0xFF)
});
(byte)(value & 0xFF));
}
else
{
var buffer = new byte[3 + 8];
buffer[0] = SMALL_BIG_EXT;
buffer[0] = (byte)ETFType.SMALL_BIG_EXT;
if (value < 0)
{
buffer[2] = 1; //Is negative
@@ -97,22 +95,37 @@ namespace Discord.API.Client
}
}
//public void Write(BinaryWriter writer, double value) => Write(writer, (float)value);
public void Write(BinaryWriter writer, float value)
public void Write(BinaryWriter writer, float value) => Write(writer, (double)value);
public void Write(BinaryWriter writer, double value)
{
throw new NotImplementedException();
ulong value2 = *(ulong*)&value;
Append(writer, (byte)ETFType.NEW_FLOAT_EXT,
(byte)((value2 >> 56) & 0xFF),
(byte)((value2 >> 48) & 0xFF),
(byte)((value2 >> 40) & 0xFF),
(byte)((value2 >> 32) & 0xFF),
(byte)((value2 >> 24) & 0xFF),
(byte)((value2 >> 16) & 0xFF),
(byte)((value2 >> 8) & 0xFF),
(byte)(value2 & 0xFF));
}
public void Write(BinaryWriter writer, byte[] value)
{
throw new NotImplementedException();
int count = value.Length;
Append(writer, (byte)ETFType.BINARY_EXT,
(byte)((count >> 24) & 0xFF),
(byte)((count >> 16) & 0xFF),
(byte)((count >> 8) & 0xFF),
(byte)(count & 0xFF));
Append(writer, value);
}
public void Write(BinaryWriter writer, string value)
{
throw new NotImplementedException();
}

public void Write<T>(BinaryWriter writer, T value)
/*public void Write<T>(BinaryWriter writer, T value)
where T : ISerializable
{
throw new NotImplementedException();
@@ -121,9 +134,9 @@ namespace Discord.API.Client
where T : ISerializable
{
throw new NotImplementedException();
}
}*/

private void Append(BinaryWriter writer, byte[] data) => Append(writer, data, data.Length);
private void Append(BinaryWriter writer, params byte[] data) => Append(writer, data, data.Length);
private void Append(BinaryWriter writer, byte[] data, int length)
{
throw new NotImplementedException();

+ 32
- 0
src/Discord.Net/ETF/ETFType.cs View File

@@ -0,0 +1,32 @@
namespace Discord.ETF
{
public enum ETFType : byte
{
NEW_FLOAT_EXT = 70,
BIT_BINARY_EXT = 77,
ATOM_CACHE_REF = 82,
SMALL_INTEGER_EXT = 97,
INTEGER_EXT = 98,
FLOAT_EXT = 99,
ATOM_EXT = 100,
REFERENCE_EXT = 101,
PORT_EXT = 102,
PID_EXT = 103,
SMALL_TUPLE_EXT = 104,
LARGE_TUPLE_EXT = 105,
NIL_EXT = 106,
STRING_EXT = 107,
LIST_EXT = 108,
BINARY_EXT = 109,
SMALL_BIG_EXT = 110,
LARGE_BIG_EXT = 111,
NEW_FUN_EXT = 112,
EXPORT_EXT = 113,
NEW_REFERENCE_EXT = 114,
SMALL_ATOM_EXT = 115,
MAP_EXT = 116,
FUN_EXT = 117,
ATOM_UTF8_EXT = 118,
SMALL_ATOM_UTF8_EXT = 119
}
}

Loading…
Cancel
Save