Skip to content

Commit

Permalink
Fixed array parsing and added getBlob
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmit799 committed Nov 2, 2022
1 parent cf9e6e4 commit d2e38ca
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
8 changes: 5 additions & 3 deletions gens/ts/function_parsing_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,14 @@ def _gen_buffer(self):
elem_array.getElementType().asClassParameter()
)

params += "ReadHelper.readArrayStatic(di, () => {\n"
params += (
"ReadHelper.readArrayStatic(di, (arrayData) => {\n"
)

if elem_param_simple:
params += f"\t\t\treturn di.get{get_formatted_subatomic_type(elem_param_simple.getType())}();\n"
params += f"\t\t\treturn arrayData.get{get_formatted_subatomic_type(elem_param_simple.getType())}();\n"
else:
params += f"\t\t\treturn StructParsing.get{elem_param_class.getClass().getName()}(di);\n"
params += f"\t\t\treturn StructParsing.get{elem_param_class.getClass().getName()}(arrayData);\n"

params += "\t\t}), "

Expand Down
2 changes: 1 addition & 1 deletion gens/ts/static/net/Datagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default class Datagram {
}
}

public addBlob(arg: string) {
public addBlob(arg: ArrayBuffer) {
// TODO: Add this.
}

Expand Down
9 changes: 6 additions & 3 deletions gens/ts/static/net/DatagramIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ export default class DatagramIterator {
return str.join("");
}

public getBlob(): string {
// TODO: Add this.
return "";
public getBlob(): ArrayBuffer {
const len = this.getUint16();
const data = this.buffer.slice(this.bufferIndex, this.bufferIndex + len);
this.bufferIndex += len;

return data;
}

public getUint32Array(): number[] {
Expand Down
9 changes: 5 additions & 4 deletions gens/ts/static/net/ReadHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import DatagramIterator from "./DatagramIterator";

export default class ReadHelper {
public static readArrayStatic(di: DatagramIterator, callback: () => any): any[] {
const arrayLength = di.getUint16();
public static readArrayStatic(di: DatagramIterator, callback: (arrayData: DatagramIterator) => any): any[] {
const data = di.getBlob();
const arrayData = new DatagramIterator(data);

const array = [];
for (let i = 0; i < arrayLength; i++) {
array.push(callback());
while (arrayData.getRemainingSize()) {
array.push(callback(arrayData));
}

return array;
Expand Down
21 changes: 17 additions & 4 deletions gens/ts/struct_parsing_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,26 @@ def _gen_buffer(self):
)
continue

dc_param_class = dc_parameter.asClassParameter()
dc_param_simple = dc_parameter.asSimpleParameter()
dc_param_array = dc_parameter.asArrayParameter()

if dc_param_simple:
if dc_param_class:
# This is (always?) a struct arg.
elem_dc_class = dc_param_class.getClass()
if not elem_dc_class.isStruct():
self.notify.warning(
f"Got non-struct class as field param: {name} - {elem_dc_class.getName()}"
)
continue

static_out += f"\t\tobj.{field.getName()} = StructParsing.get{elem_dc_class.getName()}(di);\n"

elif dc_param_simple:
static_out += f"\t\tobj.{field.getName()} = di.get{get_formatted_subatomic_type(dc_param_simple.getType())}();\n"

elif dc_param_array:
static_out += f"\t\tobj.{field.getName()} = ReadHelper.readArrayStatic(di, () => {{\n"
static_out += f"\t\tobj.{field.getName()} = ReadHelper.readArrayStatic(di, (arrayData) => {{\n"

# TODO: Can we have multi-dimensional arrays?
elem_param_simple = (
Expand All @@ -64,9 +77,9 @@ def _gen_buffer(self):
)

if elem_param_simple:
static_out += f"\t\t\treturn di.get{get_formatted_subatomic_type(elem_param_simple.getType())}();\n"
static_out += f"\t\t\treturn arrayData.get{get_formatted_subatomic_type(elem_param_simple.getType())}();\n"
elif elem_param_class:
static_out += f"\t\t\treturn StructParsing.get{elem_param_class.getClass().getName()}(di);\n"
static_out += f"\t\t\treturn StructParsing.get{elem_param_class.getClass().getName()}(arrayData);\n"

static_out += "\t\t});\n"

Expand Down
5 changes: 4 additions & 1 deletion gens/ts/util_ts.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ def get_ts_type_for_subatomic_type(subatomic_type):
elif subatomic_type in (STUint64, STInt64):
return "bigint"

elif subatomic_type in (STChar, STString, STBlob, STBlob32):
elif subatomic_type in (STChar, STString):
return "string"

elif subatomic_type in (STBlob, STBlob32):
return "ArrayBuffer"

elif subatomic_type in (
STUint8array,
STInt8array,
Expand Down

0 comments on commit d2e38ca

Please sign in to comment.