77 lines
2.3 KiB
Plaintext
77 lines
2.3 KiB
Plaintext
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())
|
|
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
|
|
if value = true
|
|
m.WriteByte(1)
|
|
else
|
|
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
|