ref[TypeScript]: rename get set method

This commit is contained in:
godotg
2024-07-14 21:02:59 +08:00
parent 774be53387
commit 5062143d75
12 changed files with 781 additions and 802 deletions
@@ -35,14 +35,14 @@ public class TsBoolSerializer implements ITsSerializer {
@Override
public void writeObject(StringBuilder builder, String objectStr, int deep, Field field, IFieldRegistration fieldRegistration) {
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("buffer.writeBoolean({});", objectStr)).append(LS);
builder.append(StringUtils.format("buffer.writeBool({});", objectStr)).append(LS);
}
@Override
public String readObject(StringBuilder builder, int deep, Field field, IFieldRegistration fieldRegistration) {
String result = "result" + GenerateProtocolFile.localVariableId++;
GenerateProtocolFile.addTab(builder, deep);
builder.append(StringUtils.format("const {} = buffer.readBoolean(); ", result)).append(LS);
builder.append(StringUtils.format("const {} = buffer.readBool(); ", result)).append(LS);
return result;
}
}
@@ -1,6 +1,7 @@
interface IByteBuffer {
adjustPadding(predictionLength: number, beforeWriteIndex: number): void
compatibleRead(beforeReadIndex: number, length: number): boolean
getBuffer(): ArrayBuffer;
setWriteOffset(writeOffset: number): void
getWriteOffset(): number
setReadOffset(readOffset: number): void
@@ -10,8 +11,8 @@ interface IByteBuffer {
isReadable(): boolean
writeBytes(byteArray: ArrayBuffer): void
toBytes(): ArrayBuffer
writeBoolean(value: boolean): void
readBoolean(): boolean
writeBool(value: boolean): void
readBool(): boolean
writeByte(value: number): void
readByte(): number
writeShort(value: number): void
@@ -31,8 +32,8 @@ interface IByteBuffer {
readString(): string
writePacket(packet: any, protocolId: number): void
readPacket(protocolId: number): any
writeBooleanArray(array: Array<boolean> | null): void
readBooleanArray(): boolean[]
writeBoolArray(array: Array<boolean> | null): void
readBoolArray(): boolean[]
writeByteArray(array: Array<number> | null): void
readByteArray(): number[]
writeShortArray(array: Array<number> | null): void
@@ -50,8 +51,8 @@ interface IByteBuffer {
writePacketArray(array: Array<any> | null, protocolId: number): void
readPacketArray(protocolId: number): any
// ---------------------------------------------list-------------------------------------------
writeBooleanList(list: Array<boolean> | null): void
readBooleanList(): boolean[]
writeBoolList(list: Array<boolean> | null): void
readBoolList(): boolean[]
writeByteList(list: Array<number> | null): void
readByteList(): number[]
writeShortList(list: Array<number> | null): void
@@ -69,8 +70,8 @@ interface IByteBuffer {
writePacketList(list: Array<any> | null, protocolId: number): void
readPacketList(protocolId: number): any[]
// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set: Set<boolean> | null): void
readBooleanSet(): Set<boolean>
writeBoolSet(set: Set<boolean> | null): void
readBoolSet(): Set<boolean>
writeByteSet(set: Set<number> | null): void
readByteSet(): Set<number>
writeShortSet(set: Set<number> | null): void
@@ -84,6 +84,10 @@ class ByteBuffer implements IByteBuffer{
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}
getBuffer(): ArrayBuffer {
return this.buffer;
}
setWriteOffset(writeOffset: number): void {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception:readerIndex:' + this.readOffset +
@@ -142,7 +146,7 @@ class ByteBuffer implements IByteBuffer{
return result;
}
writeBoolean(value: boolean): void {
writeBool(value: boolean): void {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
@@ -155,7 +159,7 @@ class ByteBuffer implements IByteBuffer{
this.writeOffset++;
}
readBoolean(): boolean {
readBool(): boolean {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
@@ -403,23 +407,23 @@ class ByteBuffer implements IByteBuffer{
return protocolRegistration.read(this);
}
writeBooleanArray(array: Array<boolean> | null) {
writeBoolArray(array: Array<boolean> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}
readBooleanArray(): Array<boolean> {
readBoolArray(): Array<boolean> {
const array: boolean[] = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
array.push(this.readBool());
}
}
return array;
@@ -604,12 +608,12 @@ class ByteBuffer implements IByteBuffer{
}
// ---------------------------------------------list-------------------------------------------
writeBooleanList(list: Array<boolean> | null): void {
this.writeBooleanArray(list);
writeBoolList(list: Array<boolean> | null): void {
this.writeBoolArray(list);
}
readBooleanList(): boolean[] {
return this.readBooleanArray();
readBoolList(): boolean[] {
return this.readBoolArray();
}
writeByteList(list: Array<number> | null): void {
@@ -677,19 +681,19 @@ class ByteBuffer implements IByteBuffer{
}
// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set: Set<boolean> | null): void {
writeBoolSet(set: Set<boolean> | null): void {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}
readBooleanSet(): Set<boolean> {
return new Set(this.readBooleanArray());
readBoolSet(): Set<boolean> {
return new Set(this.readBoolArray());
}
writeByteSet(set: Set<number> | null): void {
+7 -8
View File
@@ -17,12 +17,11 @@ function assert(flag: boolean): void {
// 如果是在idea中运行,需要先安装插件:Run Configuration for TypeScript
console.log("Hello world");
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
// const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
const data = fs.readFileSync('D:\\Project\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-no-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-inner-compatible.bytes');
// const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-compatible.bytes');
const data = fs.readFileSync('C:\\github\\zfoo\\protocol\\src\\test\\resources\\compatible\\normal-out-inner-inner-compatible.bytes');
const arrayBytes = new Uint8Array(data.length);
data.copy(arrayBytes, 0, 0, data.length);
@@ -61,8 +60,8 @@ assert(buffer.getCapacity() == 128)
// boolean
const testBoolean = [false, true];
testBoolean.forEach((value) => {
buffer.writeBoolean(value);
assert(buffer.readBoolean() == value);
buffer.writeBool(value);
assert(buffer.readBool() == value);
});
assert(buffer.writeOffset == testBoolean.length);
@@ -1,6 +1,7 @@
interface IByteBuffer {
adjustPadding(predictionLength: number, beforeWriteIndex: number): void
compatibleRead(beforeReadIndex: number, length: number): boolean
getBuffer(): ArrayBuffer;
setWriteOffset(writeOffset: number): void
getWriteOffset(): number
setReadOffset(readOffset: number): void
@@ -10,8 +11,8 @@ interface IByteBuffer {
isReadable(): boolean
writeBytes(byteArray: ArrayBuffer): void
toBytes(): ArrayBuffer
writeBoolean(value: boolean): void
readBoolean(): boolean
writeBool(value: boolean): void
readBool(): boolean
writeByte(value: number): void
readByte(): number
writeShort(value: number): void
@@ -31,8 +32,8 @@ interface IByteBuffer {
readString(): string
writePacket(packet: any, protocolId: number): void
readPacket(protocolId: number): any
writeBooleanArray(array: Array<boolean> | null): void
readBooleanArray(): boolean[]
writeBoolArray(array: Array<boolean> | null): void
readBoolArray(): boolean[]
writeByteArray(array: Array<number> | null): void
readByteArray(): number[]
writeShortArray(array: Array<number> | null): void
@@ -50,8 +51,8 @@ interface IByteBuffer {
writePacketArray(array: Array<any> | null, protocolId: number): void
readPacketArray(protocolId: number): any
// ---------------------------------------------list-------------------------------------------
writeBooleanList(list: Array<boolean> | null): void
readBooleanList(): boolean[]
writeBoolList(list: Array<boolean> | null): void
readBoolList(): boolean[]
writeByteList(list: Array<number> | null): void
readByteList(): number[]
writeShortList(list: Array<number> | null): void
@@ -69,8 +70,8 @@ interface IByteBuffer {
writePacketList(list: Array<any> | null, protocolId: number): void
readPacketList(protocolId: number): any[]
// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set: Set<boolean> | null): void
readBooleanSet(): Set<boolean>
writeBoolSet(set: Set<boolean> | null): void
readBoolSet(): Set<boolean>
writeByteSet(set: Set<number> | null): void
readByteSet(): Set<number>
writeShortSet(set: Set<number> | null): void
@@ -84,6 +84,10 @@ class ByteBuffer implements IByteBuffer{
return length !== -1 && this.getReadOffset() < length + beforeReadIndex;
}
getBuffer(): ArrayBuffer {
return this.buffer;
}
setWriteOffset(writeOffset: number): void {
if (writeOffset > this.buffer.byteLength) {
throw new Error('index out of bounds exception:readerIndex:' + this.readOffset +
@@ -142,7 +146,7 @@ class ByteBuffer implements IByteBuffer{
return result;
}
writeBoolean(value: boolean): void {
writeBool(value: boolean): void {
if (!(value === true || value === false)) {
throw new Error('value must be true of false');
}
@@ -155,7 +159,7 @@ class ByteBuffer implements IByteBuffer{
this.writeOffset++;
}
readBoolean(): boolean {
readBool(): boolean {
const value = this.bufferView.getInt8(this.readOffset);
this.readOffset++;
return (value === 1);
@@ -403,23 +407,23 @@ class ByteBuffer implements IByteBuffer{
return protocolRegistration.read(this);
}
writeBooleanArray(array: Array<boolean> | null) {
writeBoolArray(array: Array<boolean> | null) {
if (array === null) {
this.writeInt(0);
} else {
this.writeInt(array.length);
array.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}
readBooleanArray(): Array<boolean> {
readBoolArray(): Array<boolean> {
const array: boolean[] = [];
const length = this.readInt();
if (length > 0) {
for (let index = 0; index < length; index++) {
array.push(this.readBoolean());
array.push(this.readBool());
}
}
return array;
@@ -604,12 +608,12 @@ class ByteBuffer implements IByteBuffer{
}
// ---------------------------------------------list-------------------------------------------
writeBooleanList(list: Array<boolean> | null): void {
this.writeBooleanArray(list);
writeBoolList(list: Array<boolean> | null): void {
this.writeBoolArray(list);
}
readBooleanList(): boolean[] {
return this.readBooleanArray();
readBoolList(): boolean[] {
return this.readBoolArray();
}
writeByteList(list: Array<number> | null): void {
@@ -677,19 +681,19 @@ class ByteBuffer implements IByteBuffer{
}
// ---------------------------------------------set-------------------------------------------
writeBooleanSet(set: Set<boolean> | null): void {
writeBoolSet(set: Set<boolean> | null): void {
if (set === null) {
this.writeInt(0);
} else {
this.writeInt(set.size);
set.forEach(element => {
this.writeBoolean(element);
this.writeBool(element);
});
}
}
readBooleanSet(): Set<boolean> {
return new Set(this.readBooleanArray());
readBoolSet(): Set<boolean> {
return new Set(this.readBoolArray());
}
writeByteSet(set: Set<number> | null): void {
@@ -12,36 +12,6 @@ type WasmExports = {
let wasm: WasmExports;
try {
// nodejs环境无法使用
// wasm = new WebAssembly.Instance(
// new WebAssembly.Module(
// new Uint8Array([
// 0, 97, 115, 109, 1, 0, 0, 0, 1, 13, 2, 96, 0, 1, 127, 96, 4, 127, 127,
// 127, 127, 1, 127, 3, 7, 6, 0, 1, 1, 1, 1, 1, 6, 6, 1, 127, 1, 65, 0, 11,
// 7, 50, 6, 3, 109, 117, 108, 0, 1, 5, 100, 105, 118, 95, 115, 0, 2, 5,
// 100, 105, 118, 95, 117, 0, 3, 5, 114, 101, 109, 95, 115, 0, 4, 5, 114,
// 101, 109, 95, 117, 0, 5, 8, 103, 101, 116, 95, 104, 105, 103, 104, 0, 0,
// 10, 191, 1, 6, 4, 0, 35, 0, 11, 36, 1, 1, 126, 32, 0, 173, 32, 1, 173,
// 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 126, 34, 4,
// 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0, 173, 32,
// 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134, 132, 127,
// 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126, 32, 0,
// 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66, 32, 134,
// 132, 128, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36, 1, 1, 126,
// 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3, 173, 66,
// 32, 134, 132, 129, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167, 11, 36,
// 1, 1, 126, 32, 0, 173, 32, 1, 173, 66, 32, 134, 132, 32, 2, 173, 32, 3,
// 173, 66, 32, 134, 132, 130, 34, 4, 66, 32, 135, 167, 36, 0, 32, 4, 167,
// 11,
// ])
// ),
// {}
// ).exports as WasmExports;
} catch {
// no wasm support
}
export class Long {
/**
* Signed zero.
@@ -95,10 +95,10 @@ export class ComplexObjectRegistration implements IProtocolRegistration<ComplexO
buffer.writeDouble(packet.ff);
buffer.writeDoubleArray(packet.fff);
buffer.writeDoubleArray(packet.ffff);
buffer.writeBoolean(packet.g);
buffer.writeBoolean(packet.gg);
buffer.writeBooleanArray(packet.ggg);
buffer.writeBooleanArray(packet.gggg);
buffer.writeBool(packet.g);
buffer.writeBool(packet.gg);
buffer.writeBoolArray(packet.ggg);
buffer.writeBoolArray(packet.gggg);
buffer.writeString(packet.jj);
buffer.writeStringArray(packet.jjj);
buffer.writePacket(packet.kk, 102);
@@ -293,13 +293,13 @@ export class ComplexObjectRegistration implements IProtocolRegistration<ComplexO
packet.fff = array22;
const array23 = buffer.readDoubleArray();
packet.ffff = array23;
const result24 = buffer.readBoolean();
const result24 = buffer.readBool();
packet.g = result24;
const result25 = buffer.readBoolean();
const result25 = buffer.readBool();
packet.gg = result25;
const array26 = buffer.readBooleanArray();
const array26 = buffer.readBoolArray();
packet.ggg = array26;
const array27 = buffer.readBooleanArray();
const array27 = buffer.readBoolArray();
packet.gggg = array27;
const result28 = buffer.readString();
packet.jj = result28;
@@ -46,7 +46,7 @@ export class NormalObjectRegistration implements IProtocolRegistration<NormalObj
buffer.writeLong(packet.d);
buffer.writeFloat(packet.e);
buffer.writeDouble(packet.f);
buffer.writeBoolean(packet.g);
buffer.writeBool(packet.g);
buffer.writeString(packet.jj);
buffer.writePacket(packet.kk, 102);
buffer.writeIntList(packet.l);
@@ -83,7 +83,7 @@ export class NormalObjectRegistration implements IProtocolRegistration<NormalObj
packet.e = result5;
const result6 = buffer.readDouble();
packet.f = result6;
const result7 = buffer.readBoolean();
const result7 = buffer.readBool();
packet.g = result7;
const result8 = buffer.readString();
packet.jj = result8;
@@ -19,7 +19,7 @@ export class ObjectBRegistration implements IProtocolRegistration<ObjectB> {
}
const beforeWriteIndex = buffer.getWriteOffset();
buffer.writeInt(4);
buffer.writeBoolean(packet.flag);
buffer.writeBool(packet.flag);
buffer.writeInt(packet.innerCompatibleValue);
buffer.adjustPadding(4, beforeWriteIndex);
}
@@ -31,7 +31,7 @@ export class ObjectBRegistration implements IProtocolRegistration<ObjectB> {
}
const beforeReadIndex = buffer.getReadOffset();
const packet = new ObjectB();
const result0 = buffer.readBoolean();
const result0 = buffer.readBool();
packet.flag = result0;
if (buffer.compatibleRead(beforeReadIndex, length)) {
const result1 = buffer.readInt();
@@ -19,7 +19,7 @@ export class SimpleObjectRegistration implements IProtocolRegistration<SimpleObj
}
buffer.writeInt(-1);
buffer.writeInt(packet.c);
buffer.writeBoolean(packet.g);
buffer.writeBool(packet.g);
}
read(buffer: IByteBuffer): SimpleObject | null {
@@ -31,7 +31,7 @@ export class SimpleObjectRegistration implements IProtocolRegistration<SimpleObj
const packet = new SimpleObject();
const result0 = buffer.readInt();
packet.c = result0;
const result1 = buffer.readBoolean();
const result1 = buffer.readBool();
packet.g = result1;
if (length > 0) {
buffer.setReadOffset(beforeReadIndex + length);
File diff suppressed because it is too large Load Diff