Skip to content

Commit

Permalink
serialization simplified (#17463)
Browse files Browse the repository at this point in the history
  • Loading branch information
star-e authored Jul 31, 2024
1 parent 50baffe commit e0a82d7
Show file tree
Hide file tree
Showing 5 changed files with 434 additions and 434 deletions.
12 changes: 6 additions & 6 deletions cocos/rendering/custom/archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
*/
/* eslint-disable max-len */
export interface OutputArchive {
writeBool (value: boolean): void;
writeNumber (value: number): void;
writeString (value: string): void;
b (value: boolean): void;
n (value: number): void;
s (value: string): void;
}

export interface InputArchive {
readBool (): boolean;
readNumber (): number;
readString (): string;
b (): boolean;
n (): number;
s (): string;
}
16 changes: 8 additions & 8 deletions cocos/rendering/custom/binary-archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ export class BinaryOutputArchive implements OutputArchive {
this.buffer = new Uint8Array(this.capacity);
this.dataView = new DataView(this.buffer.buffer);
}
writeBool (value: boolean): void {
b (value: boolean): void {
const newSize = this.size + 1;
if (newSize > this.capacity) {
this.reserve(newSize);
}
this.dataView.setUint8(this.size, value ? 1 : 0);
this.size = newSize;
}
writeNumber (value: number): void {
n (value: number): void {
const newSize = this.size + 8;
if (newSize > this.capacity) {
this.reserve(newSize);
}
this.dataView.setFloat64(this.size, value, true);
this.size = newSize;
}
writeString (value: string): void {
this.writeNumber(value.length);
s (value: string): void {
this.n(value.length);
const newSize = this.size + value.length;
if (newSize > this.capacity) {
this.reserve(newSize);
Expand Down Expand Up @@ -78,16 +78,16 @@ export class BinaryInputArchive implements InputArchive {
constructor (data: ArrayBuffer) {
this.dataView = new DataView(data);
}
readBool (): boolean {
b (): boolean {
return this.dataView.getUint8(this.offset++) !== 0;
}
readNumber (): number {
n (): number {
const value = this.dataView.getFloat64(this.offset, true);
this.offset += 8;
return value;
}
readString (): string {
const length = this.readNumber();
s (): string {
const length = this.n();
// we only support ascii string now, so we can use String.fromCharCode
// see https://stackoverflow.com/questions/67057689/typscript-type-uint8array-is-missing-the-following-properties-from-type-numb
// answer on stackoverflow might be wrong.
Expand Down
Loading

0 comments on commit e0a82d7

Please sign in to comment.