diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2024-01-28 17:25:03 +0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-01-28 17:25:03 +0300 |
| commit | 344b5888f5de2707d9f78a27ce48f3731cecc034 (patch) | |
| tree | d079aa28eea9cf746ff86bb620995aea5aeee91b /backend/parser/parser.go | |
| parent | fix: time (#137) (diff) | |
| download | lphub-344b5888f5de2707d9f78a27ce48f3731cecc034.tar.gz lphub-344b5888f5de2707d9f78a27ce48f3731cecc034.tar.bz2 lphub-344b5888f5de2707d9f78a27ce48f3731cecc034.zip | |
feat: check steam ids when uploading demo (#148)
Diffstat (limited to 'backend/parser/parser.go')
| -rw-r--r-- | backend/parser/parser.go | 94 |
1 files changed, 85 insertions, 9 deletions
diff --git a/backend/parser/parser.go b/backend/parser/parser.go index db84256..1a80d4a 100644 --- a/backend/parser/parser.go +++ b/backend/parser/parser.go | |||
| @@ -9,10 +9,10 @@ import ( | |||
| 9 | ) | 9 | ) |
| 10 | 10 | ||
| 11 | // Don't try to understand it, feel it. | 11 | // Don't try to understand it, feel it. |
| 12 | func ProcessDemo(filePath string) (portalCount int, tickCount int, err error) { | 12 | func ProcessDemo(filePath string) (portalCount int, tickCount int, hostSteamID string, partnerSteamID string, err error) { |
| 13 | file, err := os.Open(filePath) | 13 | file, err := os.Open(filePath) |
| 14 | if err != nil { | 14 | if err != nil { |
| 15 | return 0, 0, err | 15 | return 0, 0, "", "", err |
| 16 | } | 16 | } |
| 17 | reader := bitreader.NewReader(file, true) | 17 | reader := bitreader.NewReader(file, true) |
| 18 | demoFileStamp := reader.TryReadString() | 18 | demoFileStamp := reader.TryReadString() |
| @@ -20,13 +20,13 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, err error) { | |||
| 20 | networkProtocol := reader.TryReadSInt32() | 20 | networkProtocol := reader.TryReadSInt32() |
| 21 | reader.SkipBytes(1056) | 21 | reader.SkipBytes(1056) |
| 22 | if demoFileStamp != "HL2DEMO" { | 22 | if demoFileStamp != "HL2DEMO" { |
| 23 | return 0, 0, errors.New("invalid demo file stamp") | 23 | return 0, 0, "", "", errors.New("invalid demo file stamp") |
| 24 | } | 24 | } |
| 25 | if demoProtocol != 4 { | 25 | if demoProtocol != 4 { |
| 26 | return 0, 0, errors.New("this parser only supports demos from new engine") | 26 | return 0, 0, "", "", errors.New("this parser only supports demos from new engine") |
| 27 | } | 27 | } |
| 28 | if networkProtocol != 2001 { | 28 | if networkProtocol != 2001 { |
| 29 | return 0, 0, errors.New("this parser only supports demos from portal 2") | 29 | return 0, 0, "", "", errors.New("this parser only supports demos from portal 2") |
| 30 | } | 30 | } |
| 31 | for { | 31 | for { |
| 32 | packetType := reader.TryReadUInt8() | 32 | packetType := reader.TryReadUInt8() |
| @@ -217,21 +217,97 @@ func ProcessDemo(filePath string) (portalCount int, tickCount int, err error) { | |||
| 217 | case 33: | 217 | case 33: |
| 218 | packetReader.SkipBits(packetReader.TryReadBits(32)) | 218 | packetReader.SkipBits(packetReader.TryReadBits(32)) |
| 219 | default: | 219 | default: |
| 220 | panic("unknown msg type") | 220 | return 0, 0, "", "", errors.New("unknown msg type") |
| 221 | } | 221 | } |
| 222 | } | 222 | } |
| 223 | case 3, 7: | 223 | case 3, 7: |
| 224 | case 4, 6, 9: | 224 | case 4, 6: |
| 225 | reader.SkipBytes(uint64(reader.TryReadSInt32())) | 225 | reader.SkipBytes(uint64(reader.TryReadSInt32())) |
| 226 | case 5, 8: | 226 | case 5, 8: |
| 227 | reader.SkipBits(32) | 227 | reader.SkipBits(32) |
| 228 | reader.SkipBytes(uint64(reader.TryReadSInt32())) | 228 | reader.SkipBytes(uint64(reader.TryReadSInt32())) |
| 229 | case 9: | ||
| 230 | type StringTableClass struct { | ||
| 231 | Name string | ||
| 232 | Data string | ||
| 233 | } | ||
| 234 | type StringTableEntry struct { | ||
| 235 | Name string | ||
| 236 | EntryData any | ||
| 237 | } | ||
| 238 | type StringTable struct { | ||
| 239 | Name string | ||
| 240 | TableEntries []StringTableEntry | ||
| 241 | Classes []StringTableClass | ||
| 242 | } | ||
| 243 | size := reader.TryReadSInt32() | ||
| 244 | stringTableReader := bitreader.NewReaderFromBytes(reader.TryReadBytesToSlice(uint64(size)), true) | ||
| 245 | tableCount := stringTableReader.TryReadBits(8) | ||
| 246 | guidCount := 0 | ||
| 247 | for i := 0; i < int(tableCount); i++ { | ||
| 248 | tableName := stringTableReader.TryReadString() | ||
| 249 | entryCount := stringTableReader.TryReadBits(16) | ||
| 250 | for i := 0; i < int(entryCount); i++ { | ||
| 251 | stringTableReader.TryReadString() | ||
| 252 | if stringTableReader.TryReadBool() { | ||
| 253 | byteLen, err := stringTableReader.ReadBits(16) | ||
| 254 | if err != nil { | ||
| 255 | return 0, 0, "", "", errors.New("error on reading entry length") | ||
| 256 | } | ||
| 257 | stringTableEntryReader := bitreader.NewReaderFromBytes(stringTableReader.TryReadBytesToSlice(byteLen), true) | ||
| 258 | if tableName == "userinfo" { | ||
| 259 | const SignedGuidLen int32 = 32 | ||
| 260 | const MaxPlayerNameLength int32 = 32 | ||
| 261 | userInfo := struct { | ||
| 262 | SteamID uint64 | ||
| 263 | Name string | ||
| 264 | UserID int32 | ||
| 265 | GUID string | ||
| 266 | FriendsID uint32 | ||
| 267 | FriendsName string | ||
| 268 | FakePlayer bool | ||
| 269 | IsHltv bool | ||
| 270 | CustomFiles []uint32 | ||
| 271 | FilesDownloaded uint8 | ||
| 272 | }{ | ||
| 273 | SteamID: stringTableEntryReader.TryReadUInt64(), | ||
| 274 | Name: stringTableEntryReader.TryReadStringLength(uint64(MaxPlayerNameLength)), | ||
| 275 | UserID: stringTableEntryReader.TryReadSInt32(), | ||
| 276 | GUID: stringTableEntryReader.TryReadStringLength(uint64(SignedGuidLen) + 1), | ||
| 277 | } | ||
| 278 | stringTableEntryReader.SkipBytes(3) | ||
| 279 | userInfo.FriendsID = stringTableEntryReader.TryReadUInt32() | ||
| 280 | userInfo.FriendsName = stringTableEntryReader.TryReadStringLength(uint64(MaxPlayerNameLength)) | ||
| 281 | userInfo.FakePlayer = stringTableEntryReader.TryReadUInt8() != 0 | ||
| 282 | userInfo.IsHltv = stringTableEntryReader.TryReadUInt8() != 0 | ||
| 283 | stringTableEntryReader.SkipBytes(2) | ||
| 284 | userInfo.CustomFiles = []uint32{stringTableEntryReader.TryReadUInt32(), stringTableEntryReader.TryReadUInt32(), stringTableEntryReader.TryReadUInt32(), stringTableEntryReader.TryReadUInt32()} | ||
| 285 | userInfo.FilesDownloaded = stringTableEntryReader.TryReadUInt8() | ||
| 286 | stringTableEntryReader.SkipBytes(3) | ||
| 287 | if guidCount == 0 { | ||
| 288 | hostSteamID = userInfo.GUID | ||
| 289 | } else if guidCount == 1 { | ||
| 290 | partnerSteamID = userInfo.GUID | ||
| 291 | } | ||
| 292 | } | ||
| 293 | } | ||
| 294 | } | ||
| 295 | if stringTableReader.TryReadBool() { | ||
| 296 | classCount := stringTableReader.TryReadBits(16) | ||
| 297 | for i := 0; i < int(classCount); i++ { | ||
| 298 | stringTableReader.TryReadString() | ||
| 299 | if stringTableReader.TryReadBool() { | ||
| 300 | stringTableReader.TryReadStringLength(uint64(stringTableReader.TryReadUInt16())) | ||
| 301 | } | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 229 | default: | 305 | default: |
| 230 | return 0, 0, errors.New("invalid packet type") | 306 | return 0, 0, "", "", errors.New("invalid packet type") |
| 231 | } | 307 | } |
| 232 | if packetType == 7 { | 308 | if packetType == 7 { |
| 233 | break | 309 | break |
| 234 | } | 310 | } |
| 235 | } | 311 | } |
| 236 | return portalCount, tickCount, nil | 312 | return portalCount, tickCount, hostSteamID, partnerSteamID, nil |
| 237 | } | 313 | } |