Search filters protobuf (#516)
* [Wip] search filters protobuf * Filters work * Small tweaks * changelog
This commit is contained in:
1 parent
c6853cd2fc
commit
564afd9685
19 files changed
+895
-10
No files matched your search
@@ -0,0 +1,96 @@
|
||||
namespace Protobuf
|
||||
|
||||
class BinaryReader
|
||||
private buffer
|
||||
private position
|
||||
|
||||
function SetBuffer(buffer as dynamic) as void
|
||||
if buffer = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
if GetInterface(buffer, "ifString") <> invalid
|
||||
b = CreateObject("roByteArray")
|
||||
b.FromBase64String(buffer.DecodeUriComponent())
|
||||
buffer = b
|
||||
end if
|
||||
|
||||
m.buffer = buffer
|
||||
m.position = 0
|
||||
end function
|
||||
|
||||
function ReadVarInt() as dynamic
|
||||
value = 0
|
||||
_shift = 0
|
||||
while true
|
||||
byte = m.ReadByte()
|
||||
if byte = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
if _shift < 32
|
||||
value = value or ((byte and &h7F) << _shift)
|
||||
else
|
||||
value = value + ((byte and &h7F) * (2 ^ _shift))
|
||||
end if
|
||||
_shift += 7
|
||||
|
||||
if (byte and &h80) = 0
|
||||
return value
|
||||
end if
|
||||
end while
|
||||
|
||||
return invalid
|
||||
end function
|
||||
|
||||
function ReadByte() as dynamic
|
||||
if m.position >= m.buffer.Count()
|
||||
return invalid
|
||||
end if
|
||||
byte = m.buffer[m.position]
|
||||
m.position += 1
|
||||
return byte
|
||||
end function
|
||||
|
||||
function ReadInt32() as dynamic
|
||||
if m.position + 4 > m.buffer.Count()
|
||||
return invalid
|
||||
end if
|
||||
value = m.buffer[m.position] or (m.buffer[m.position + 1] << 8) or (m.buffer[m.position + 2] << 16) or (m.buffer[m.position + 3] << 24)
|
||||
m.position += 4
|
||||
return value
|
||||
end function
|
||||
|
||||
function ReadInt64() as dynamic
|
||||
if m.position + 8 > m.buffer.Count()
|
||||
return invalid
|
||||
end if
|
||||
value = m.buffer[m.position] or (m.buffer[m.position + 1] << 8) or (m.buffer[m.position + 2] << 16) or (m.buffer[m.position + 3] << 24) or (m.buffer[m.position + 4] << 32) or (m.buffer[m.position + 5] << 40) or (m.buffer[m.position + 6] << 48) or (m.buffer[m.position + 7] << 56)
|
||||
m.position += 8
|
||||
return value
|
||||
end function
|
||||
|
||||
function ReadBytes() as dynamic
|
||||
length = m.ReadVarInt()
|
||||
if length = invalid
|
||||
return invalid
|
||||
end if
|
||||
endPosition = m.position + length
|
||||
if endPosition > m.buffer.Count()
|
||||
return invalid
|
||||
end if
|
||||
value = m.buffer.Slice(m.position, endPosition)
|
||||
m.position += length
|
||||
return value
|
||||
end function
|
||||
|
||||
function ReadString() as dynamic
|
||||
bytes = m.ReadBytes()
|
||||
if bytes = invalid
|
||||
return invalid
|
||||
end if
|
||||
return bytes.ToAsciiString()
|
||||
end function
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,66 @@
|
||||
namespace Protobuf
|
||||
|
||||
class BinaryWriter
|
||||
private buffer
|
||||
private position
|
||||
|
||||
function SetBuffer(buffer as dynamic) as void
|
||||
if buffer = invalid
|
||||
return
|
||||
end if
|
||||
|
||||
if GetInterface(buffer, "ifString") <> invalid
|
||||
b = CreateObject("roByteArray")
|
||||
b.FromBase64String(buffer.DecodeUriComponent())
|
||||
buffer = b
|
||||
end if
|
||||
|
||||
m.buffer = buffer
|
||||
m.position = 0
|
||||
end function
|
||||
|
||||
function WriteByte(byte as dynamic) as void
|
||||
m.buffer[m.position] = byte
|
||||
m.position += 1
|
||||
end function
|
||||
|
||||
function WriteInt32(value as dynamic) as void
|
||||
m.buffer[m.position] = value and &hFF
|
||||
m.buffer[m.position + 1] = (value >> 8) and &hFF
|
||||
m.buffer[m.position + 2] = (value >> 16) and &hFF
|
||||
m.buffer[m.position + 3] = (value >> 24) and &hFF
|
||||
m.position += 4
|
||||
end function
|
||||
|
||||
function WriteVarInt(value as dynamic) as void
|
||||
while true
|
||||
byte = value and &h7F
|
||||
value >>= 7
|
||||
if value = 0
|
||||
m.WriteByte(byte)
|
||||
return
|
||||
end if
|
||||
m.WriteByte(byte or &h80)
|
||||
end while
|
||||
end function
|
||||
|
||||
function WriteString(value as dynamic) as void
|
||||
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()
|
||||
end function
|
||||
|
||||
function WriteBool(value as dynamic) as void
|
||||
if value = true
|
||||
m.WriteByte(1)
|
||||
else
|
||||
m.WriteByte(0)
|
||||
end if
|
||||
end function
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,54 @@
|
||||
namespace Protobuf
|
||||
|
||||
class Decoder
|
||||
private reader
|
||||
|
||||
function new(reader as object)
|
||||
m.reader = reader
|
||||
end function
|
||||
|
||||
' TODO:P2 decoder is not finished, does not work properly
|
||||
function DecodeMessage(buffer as object) as dynamic
|
||||
m.reader.SetBuffer(buffer)
|
||||
|
||||
message = {}
|
||||
while true
|
||||
field = m.reader.ReadByte()
|
||||
if field = invalid
|
||||
exit while
|
||||
end if
|
||||
|
||||
tag = field >> 3
|
||||
wire_type = field and &h7
|
||||
|
||||
value = invalid
|
||||
if wire_type = 0
|
||||
value = m.reader.ReadVarInt()
|
||||
else if wire_type = 1
|
||||
value = m.reader.ReadInt64()
|
||||
else if wire_type = 2
|
||||
value = m.reader.ReadString()
|
||||
else if wire_type = 5
|
||||
value = m.reader.ReadInt32()
|
||||
end if
|
||||
|
||||
if value = invalid
|
||||
return invalid
|
||||
end if
|
||||
|
||||
tagStr = `${tag}`
|
||||
if message[tagStr] = invalid
|
||||
message[tagStr] = value
|
||||
else if GetInterface(message[tagStr], "ifArray") <> invalid
|
||||
message[tagStr].Push(value)
|
||||
else
|
||||
message[tagStr] = [message[tagStr], value]
|
||||
end if
|
||||
end while
|
||||
|
||||
return message
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,52 @@
|
||||
namespace Protobuf
|
||||
|
||||
class Encoder
|
||||
private writer
|
||||
|
||||
function new(writer as object) as void
|
||||
m.writer = writer
|
||||
end function
|
||||
|
||||
function EncodeString(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 2)
|
||||
m.writer.WriteString(value)
|
||||
end function
|
||||
|
||||
function EncodeInt32(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 0)
|
||||
m.writer.WriteVarInt(value)
|
||||
end function
|
||||
|
||||
function EncodeInt64(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 0)
|
||||
m.writer.WriteInt64(value)
|
||||
end function
|
||||
|
||||
function EncodeFloat(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 5)
|
||||
m.writer.WriteFloat(value)
|
||||
end function
|
||||
|
||||
function EncodeDouble(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 1)
|
||||
m.writer.WriteDouble(value)
|
||||
end function
|
||||
|
||||
function EncodeBool(tag as integer, value as dynamic) as void
|
||||
m.writer.WriteByte((tag << 3) or 0)
|
||||
m.writer.WriteBool(value)
|
||||
end function
|
||||
|
||||
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
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace Tests
|
||||
|
||||
@SGNode("SearchFiltersTestNode")
|
||||
@suite
|
||||
class SearchFiltersTests extends Rooibos.BaseTestSuite
|
||||
|
||||
@describe("SearchFilters")
|
||||
|
||||
@it("Encodes search filters")
|
||||
function _()
|
||||
testCases = m.GetEncodingTestCases()
|
||||
for each testCase in testCases
|
||||
encoded = m.top@.Innertube_GetEncodedSearchFilters(testCase.input)
|
||||
m.assertEqual(encoded, testCase.encoded)
|
||||
end for
|
||||
end function
|
||||
|
||||
function GetEncodingTestCases() as object
|
||||
return [{
|
||||
input: {
|
||||
"sort": "date"
|
||||
}
|
||||
encoded: "CAISAA%3D%3D"
|
||||
}, {
|
||||
input: {
|
||||
"sort": "views"
|
||||
"features": ["3d", "hd", "live"]
|
||||
}
|
||||
encoded: "CAMSBiABOAFAAQ%3D%3D"
|
||||
}, {
|
||||
input: {
|
||||
"type": "playlist"
|
||||
}
|
||||
encoded: "EgIQAw%3D%3D"
|
||||
}]
|
||||
end function
|
||||
|
||||
end class
|
||||
|
||||
end namespace
|
||||
Reference in new issue
Block a user