You've already forked Nitro_Render_V3
mirror of
https://github.com/duckietm/Nitro_Render_V3.git
synced 2026-06-19 23:16:20 +00:00
Move to Renderer V2
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
export class Byte
|
||||
{
|
||||
private _value: number;
|
||||
|
||||
constructor(value: number)
|
||||
{
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
public get value(): number
|
||||
{
|
||||
return this._value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
export class Short
|
||||
{
|
||||
private _value: number;
|
||||
|
||||
constructor(value: number)
|
||||
{
|
||||
this._value = value;
|
||||
}
|
||||
|
||||
public get value(): number
|
||||
{
|
||||
return this._value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
import { IBinaryReader, IMessageDataWrapper } from '@nitrots/api';
|
||||
|
||||
export class EvaWireDataWrapper implements IMessageDataWrapper
|
||||
{
|
||||
private _header: number;
|
||||
private _buffer: IBinaryReader;
|
||||
|
||||
constructor(header: number, buffer: IBinaryReader)
|
||||
{
|
||||
this._header = header;
|
||||
this._buffer = buffer;
|
||||
}
|
||||
|
||||
public readBytes(length: number): IBinaryReader
|
||||
{
|
||||
if(!this._buffer) return null;
|
||||
|
||||
return this._buffer.readBytes(length);
|
||||
}
|
||||
|
||||
public readByte(): number
|
||||
{
|
||||
if(!this._buffer) return -1;
|
||||
|
||||
return this._buffer.readByte();
|
||||
}
|
||||
|
||||
public readBoolean(): boolean
|
||||
{
|
||||
return (this.readByte() === 1);
|
||||
}
|
||||
|
||||
public readShort(): number
|
||||
{
|
||||
if(!this._buffer) return -1;
|
||||
|
||||
return this._buffer.readShort();
|
||||
}
|
||||
|
||||
public readInt(): number
|
||||
{
|
||||
if(!this._buffer) return -1;
|
||||
|
||||
return this._buffer.readInt();
|
||||
}
|
||||
|
||||
public readFloat(): number
|
||||
{
|
||||
if(!this._buffer) return -1;
|
||||
|
||||
return this._buffer.readFloat();
|
||||
}
|
||||
|
||||
public readDouble(): number
|
||||
{
|
||||
if(!this._buffer) return -1;
|
||||
|
||||
return this._buffer.readDouble();
|
||||
}
|
||||
|
||||
public readString(): string
|
||||
{
|
||||
const length = this.readShort();
|
||||
const buffer = this._buffer.readBytes(length);
|
||||
|
||||
return buffer.toString('utf8');
|
||||
}
|
||||
|
||||
public get header(): number
|
||||
{
|
||||
return this._header;
|
||||
}
|
||||
|
||||
public get bytesAvailable(): boolean
|
||||
{
|
||||
return (this._buffer && (this._buffer.remaining() > 0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
import { IBinaryWriter, ICodec, IConnection, IMessageDataWrapper } from '@nitrots/api';
|
||||
import { BinaryReader, BinaryWriter } from '@nitrots/utils';
|
||||
import { Byte } from '../Byte';
|
||||
import { Short } from '../Short';
|
||||
import { EvaWireDataWrapper } from './EvaWireDataWrapper';
|
||||
|
||||
export class EvaWireFormat implements ICodec
|
||||
{
|
||||
public encode(header: number, messages: any[]): IBinaryWriter
|
||||
{
|
||||
const writer = new BinaryWriter();
|
||||
|
||||
writer.writeShort(header);
|
||||
|
||||
for(const value of messages)
|
||||
{
|
||||
let type: string = typeof value;
|
||||
|
||||
if(type === 'object')
|
||||
{
|
||||
if(value === null) type = 'null';
|
||||
else if(value instanceof Byte) type = 'byte';
|
||||
else if(value instanceof Short) type = 'short';
|
||||
else if(value instanceof ArrayBuffer) type = 'arraybuffer';
|
||||
}
|
||||
|
||||
switch(type)
|
||||
{
|
||||
case 'undefined':
|
||||
case 'null':
|
||||
writer.writeShort(0);
|
||||
break;
|
||||
case 'byte':
|
||||
writer.writeByte(value.value);
|
||||
break;
|
||||
case 'short':
|
||||
writer.writeShort(value.value);
|
||||
break;
|
||||
case 'number':
|
||||
writer.writeInt(value);
|
||||
break;
|
||||
case 'boolean':
|
||||
writer.writeByte(value ? 1 : 0);
|
||||
break;
|
||||
case 'string':
|
||||
if(!value) writer.writeShort(0);
|
||||
else
|
||||
{
|
||||
writer.writeString(value, true);
|
||||
}
|
||||
break;
|
||||
case 'arraybuffer':
|
||||
writer.writeBytes(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
const buffer = writer.getBuffer();
|
||||
|
||||
if(!buffer) return null;
|
||||
|
||||
return new BinaryWriter().writeInt(buffer.byteLength).writeBytes(buffer);
|
||||
}
|
||||
|
||||
public decode(connection: IConnection): IMessageDataWrapper[]
|
||||
{
|
||||
if(!connection || !connection.dataBuffer || !connection.dataBuffer.byteLength) return null;
|
||||
|
||||
const wrappers: IMessageDataWrapper[] = [];
|
||||
|
||||
while(connection.dataBuffer.byteLength)
|
||||
{
|
||||
if(connection.dataBuffer.byteLength < 4) break;
|
||||
|
||||
const container = new BinaryReader(connection.dataBuffer);
|
||||
const length = container.readInt();
|
||||
|
||||
if(length > (connection.dataBuffer.byteLength - 4)) break;
|
||||
|
||||
const extracted = container.readBytes(length);
|
||||
|
||||
wrappers.push(new EvaWireDataWrapper(extracted.readShort(), extracted));
|
||||
|
||||
connection.dataBuffer = connection.dataBuffer.slice(length + 4);
|
||||
}
|
||||
|
||||
return wrappers;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from './EvaWireDataWrapper';
|
||||
export * from './EvaWireFormat';
|
||||
@@ -0,0 +1,3 @@
|
||||
export * from './Byte';
|
||||
export * from './evawire';
|
||||
export * from './Short';
|
||||
Reference in New Issue
Block a user