From e20fb7df90bc41cc52264f8fb60eb3edd19c8569 Mon Sep 17 00:00:00 2001 From: Brahim Hadriche Date: Tue, 24 Dec 2024 11:04:59 -0500 Subject: [PATCH] small tweak to proto stuff --- .../Innertube/protos/params.proto.gen.bs | 18 +++++++++++++----- .../src/source/Protobuf/BinaryWriter.bs | 18 ++++++++++++++---- playlet-lib/src/source/Protobuf/Encoder.bs | 7 +------ tools/bs-plugins/proto-gen-plugin.ts | 10 +++++++--- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs index 6a716551..c02fec65 100644 --- a/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs +++ b/playlet-lib/src/components/Services/Innertube/protos/params.proto.gen.bs @@ -43,7 +43,7 @@ namespace Protobuf - function encodeFilters(message as dynamic) as string + function encodeFilters(message as dynamic, asByteArray = false as boolean) as dynamic buffer = CreateObject("roByteArray") writer = new Protobuf.BinaryWriter() writer.SetBuffer(buffer) @@ -104,10 +104,14 @@ namespace Protobuf value = message["features_vr180"] encoder.EncodeBool(26, value) end if - return buffer.ToBase64String().EncodeUriComponent() + if asByteArray + return buffer + else + return buffer.ToBase64String().EncodeUriComponent() + end if end function - function encodeSearchFilter(message as dynamic) as string + function encodeSearchFilter(message as dynamic, asByteArray = false as boolean) as dynamic buffer = CreateObject("roByteArray") writer = new Protobuf.BinaryWriter() writer.SetBuffer(buffer) @@ -118,9 +122,13 @@ namespace Protobuf end if if message.DoesExist("filters") value = message["filters"] - encoder.EncodeMessage(2, Protobuf.Generated.misc.encodeFilters(value)) + encoder.EncodeMessage(2, Protobuf.Generated.misc.encodeFilters(value, true)) + end if + if asByteArray + return buffer + else + return buffer.ToBase64String().EncodeUriComponent() end if - return buffer.ToBase64String().EncodeUriComponent() end function diff --git a/playlet-lib/src/source/Protobuf/BinaryWriter.bs b/playlet-lib/src/source/Protobuf/BinaryWriter.bs index 4b611706..133ce32e 100644 --- a/playlet-lib/src/source/Protobuf/BinaryWriter.bs +++ b/playlet-lib/src/source/Protobuf/BinaryWriter.bs @@ -48,10 +48,12 @@ namespace Protobuf strBuffer = CreateObject("roByteArray") strBuffer.FromAsciiString(value) m.WriteVarInt(strBuffer.Count()) - for i = 0 to strBuffer.Count() - 1 - m.buffer[m.position + i] = strBuffer[i] - end for - m.position += strBuffer.Count() + m.WriteByteArrayRaw(strBuffer, 0, strBuffer.Count()) + end function + + function WriteProtoByteArray(value as dynamic) as void + m.WriteVarInt(value.Count()) + m.WriteByteArrayRaw(value, 0, value.Count()) end function function WriteBool(value as dynamic) as void @@ -61,6 +63,14 @@ namespace Protobuf m.WriteByte(0) end if end function + + function WriteByteArrayRaw(buffer as dynamic, index as dynamic, size as dynamic) as void + position = m.position + for i = 0 to size - 1 + m.buffer[position + i] = buffer[index + i] + end for + m.position += size + end function end class end namespace diff --git a/playlet-lib/src/source/Protobuf/Encoder.bs b/playlet-lib/src/source/Protobuf/Encoder.bs index a79967b6..18d0b95f 100644 --- a/playlet-lib/src/source/Protobuf/Encoder.bs +++ b/playlet-lib/src/source/Protobuf/Encoder.bs @@ -39,12 +39,7 @@ namespace Protobuf function EncodeMessage(tag as integer, value as dynamic) as void m.writer.WriteByte((tag << 3) or 2) - buffer = CreateObject("roByteArray") - buffer.FromBase64String(value.DecodeUriComponent()) - m.writer.WriteVarInt(buffer.Count()) - for i = 0 to buffer.Count() - 1 - m.writer.WriteByte(buffer[i]) - end for + m.writer.WriteProtoByteArray(value) end function end class diff --git a/tools/bs-plugins/proto-gen-plugin.ts b/tools/bs-plugins/proto-gen-plugin.ts index 246d9dff..e13b5a33 100644 --- a/tools/bs-plugins/proto-gen-plugin.ts +++ b/tools/bs-plugins/proto-gen-plugin.ts @@ -154,7 +154,7 @@ end function`; } if (innerTypes.includes(field.type)) { - fieldEncode += `encoder.EncodeMessage(${field.tag}, ${fullNameSpace}.encode${field.type}(value))\n`; + fieldEncode += `encoder.EncodeMessage(${field.tag}, ${fullNameSpace}.encode${field.type}(value, true))\n`; } else if (enums.includes(field.type)) { fieldEncode += `encoder.EncodeInt32(${field.tag}, value)\n`; @@ -169,13 +169,17 @@ end function`; return fieldEncode + 'end if'; }).join('\n'); - return `function encode${message.name}(message as dynamic) as string + return `function encode${message.name}(message as dynamic, asByteArray = false as boolean) as dynamic buffer = CreateObject("roByteArray") writer = new Protobuf.BinaryWriter() writer.SetBuffer(buffer) encoder = new Protobuf.Encoder(writer) ${encodeFields} -return buffer.ToBase64String().EncodeUriComponent() +if asByteArray + return buffer +else + return buffer.ToBase64String().EncodeUriComponent() +end if end function`; }