Skip to content

Commit 8acc3fc

Browse files
committed
feat: decode int64 prop
1 parent 90b1034 commit 8acc3fc

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

internal/bitread/bitread.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
smallBuffer = 512
1717
largeBuffer = 1024 * 128
1818
maxVarInt32Bytes = 5
19+
maxVarintBytes = 10
1920
)
2021

2122
// BitReader wraps github.com/markus-wa/gobitread.BitReader and provides additional functionality specific to CS:GO demos.
@@ -67,12 +68,33 @@ func (r *BitReader) ReadVarInt32() uint32 {
6768
return res
6869
}
6970

71+
// ReadVarInt64 reads a variable size unsigned int (max 64-bit).
72+
func (r *BitReader) ReadVarInt64() uint64 {
73+
var (
74+
res uint64
75+
b uint64 = 0x80
76+
)
77+
78+
for count := uint(0); b&0x80 != 0 && count != maxVarintBytes; count++ {
79+
b = uint64(r.ReadSingleByte())
80+
res |= (b & 0x7f) << (7 * count)
81+
}
82+
83+
return res
84+
}
85+
7086
// ReadSignedVarInt32 reads a variable size signed int (max 32-bit).
7187
func (r *BitReader) ReadSignedVarInt32() int32 {
7288
res := r.ReadVarInt32()
7389
return int32((res >> 1) ^ -(res & 1))
7490
}
7591

92+
// ReadSignedVarInt64 reads a variable size signed int (max 64-bit).
93+
func (r *BitReader) ReadSignedVarInt64() int64 {
94+
res := r.ReadVarInt64()
95+
return int64((res >> 1) ^ -(res & 1))
96+
}
97+
7698
// ReadUBitInt reads some kind of variable size uint.
7799
// Honestly, not quite sure how it works.
78100
func (r *BitReader) ReadUBitInt() uint {

pkg/demoinfocs/sendtables/propdecoder.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var propDecoder propertyDecoder
9191
type PropertyValue struct {
9292
VectorVal r3.Vector
9393
IntVal int
94+
Int64Val int64
9495
ArrayVal []PropertyValue
9596
StringVal string
9697
FloatVal float32
@@ -123,6 +124,9 @@ func (propertyDecoder) decodeProp(prop *property, reader *bit.BitReader) {
123124
case propTypeString:
124125
prop.value.StringVal = propDecoder.decodeString(reader)
125126

127+
case propTypeInt64:
128+
prop.value.Int64Val = propDecoder.decodeInt64(prop.entry.prop, reader)
129+
126130
default:
127131
panic(fmt.Sprintf("Unknown prop type %d", prop.entry.prop.rawType))
128132
}
@@ -144,6 +148,36 @@ func (propertyDecoder) decodeInt(prop *sendTableProperty, reader *bit.BitReader)
144148
return reader.ReadSignedInt(prop.numberOfBits)
145149
}
146150

151+
func (propertyDecoder) decodeInt64(prop *sendTableProperty, reader *bit.BitReader) int64 {
152+
if prop.flags.hasFlagSet(propFlagVarInt) {
153+
if prop.flags.hasFlagSet(propFlagUnsigned) {
154+
return int64(reader.ReadVarInt64())
155+
}
156+
157+
return reader.ReadSignedVarInt64()
158+
} else {
159+
var high uint = 0
160+
var low uint = 0
161+
var isNegative bool = false
162+
if prop.flags.hasFlagSet(propFlagUnsigned) {
163+
low = reader.ReadInt(32)
164+
high = reader.ReadInt(prop.numberOfBits - 32)
165+
} else {
166+
isNegative = reader.ReadBit()
167+
low = reader.ReadInt(32)
168+
high = reader.ReadInt(prop.numberOfBits - 32 - 1)
169+
}
170+
171+
var result int64 = (int64(high) << 32) | int64(low)
172+
173+
if isNegative {
174+
result = -result
175+
}
176+
177+
return result
178+
}
179+
}
180+
147181
func (propertyDecoder) decodeFloat(prop *sendTableProperty, reader *bit.BitReader) float32 {
148182
if prop.flags&specialFloatFlags != 0 {
149183
return propDecoder.decodeSpecialFloat(prop, reader)

0 commit comments

Comments
 (0)