Skip to content

Commit 50f5578

Browse files
authored
Adding UniqueID2 for Equipment (#360)
The prior uniqueID for equipment wasn't guaranteed to be unique. This is - guaranteed to be unique - produced in a sortable order - easily marshallable - requires few extra dependencies
1 parent 869a520 commit 50f5578

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ require (
1818
require (
1919
github.com/davecgh/go-spew v1.1.1 // indirect
2020
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
21+
github.com/oklog/ulid/v2 v2.1.0 // indirect
2122
github.com/pmezard/go-difflib v1.0.0 // indirect
2223
github.com/stretchr/objx v0.4.0 // indirect
2324
golang.org/x/image v0.0.0-20220617043117-41969df76e82 // indirect

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ github.com/markus-wa/ice-cipher-go v0.0.0-20220126215401-a6adadccc817 h1:VB4ANo0
2929
github.com/markus-wa/ice-cipher-go v0.0.0-20220126215401-a6adadccc817/go.mod h1:JIsht5Oa9P50VnGJTvH2a6nkOqDFJbUeU1YRZYvdplw=
3030
github.com/markus-wa/quickhull-go/v2 v2.1.0 h1:DA2pzEzH0k5CEnlUsouRqNdD+jzNFb4DBhrX4Hpa5So=
3131
github.com/markus-wa/quickhull-go/v2 v2.1.0/go.mod h1:bOlBUpIzGSMMhHX0f9N8CQs0VZD4nnPeta0OocH7m4o=
32+
github.com/oklog/ulid/v2 v2.1.0 h1:+9lhoxAP56we25tyYETBBY1YLA2SaoLvUFgrP2miPJU=
33+
github.com/oklog/ulid/v2 v2.1.0/go.mod h1:rcEKHmBBKfef9DhnvX7y1HZBYxjXb0cP5ExxNsTT1QQ=
34+
github.com/pborman/getopt v0.0.0-20170112200414-7148bc3a4c30/go.mod h1:85jBQOZwpVEaDAr341tbn15RS4fCAsIst0qp7i8ex1o=
3235
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
3336
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
3437
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

pkg/demoinfocs/common/equipment.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"math/rand"
55
"strings"
66

7+
"github.com/oklog/ulid/v2"
8+
79
st "github.com/markus-wa/demoinfocs-golang/v3/pkg/demoinfocs/sendtables"
810
)
911

@@ -288,7 +290,8 @@ type Equipment struct {
288290
Owner *Player // The player carrying the equipment, not necessarily the buyer.
289291
OriginalString string // E.g. 'models/weapons/w_rif_m4a1_s.mdl'. Used internally to differentiate alternative weapons (M4A4 / M4A1-S etc.).
290292

291-
uniqueID int64
293+
uniqueID int64 // Deprecated, use uniqueID2, see UniqueID() for why
294+
uniqueID2 ulid.ULID
292295
}
293296

294297
// String returns a human readable name for the equipment.
@@ -303,13 +306,23 @@ func (e *Equipment) Class() EquipmentClass {
303306
return e.Type.Class()
304307
}
305308

306-
// UniqueID returns the unique id of the equipment element.
309+
// UniqueID returns a randomly generated unique id of the equipment element.
307310
// The unique id is a random int generated internally by this library and can be used to differentiate
308311
// equipment from each other. This is needed because demo-files reuse entity ids.
312+
// Deprecated: Use UniqueID2 instead. Since UniqueID is randomly generated, duplicate IDs are possible.
313+
// See the birthday problem for why repeatedly generating random 64 bit integers is likely to produce a collision.
309314
func (e *Equipment) UniqueID() int64 {
310315
return e.uniqueID
311316
}
312317

318+
// UniqueID2 returns a unique id of the equipment element that can be sorted efficiently.
319+
// UniqueID2 is a value generated internally by this library and can be used to differentiate
320+
// equipment from each other. This is needed because demo-files reuse entity ids.
321+
// Unlike UniqueID, UniqueID2 is guaranteed to be unique.
322+
func (e *Equipment) UniqueID2() ulid.ULID {
323+
return e.uniqueID2
324+
}
325+
313326
// AmmoInMagazine returns the ammo left in the magazine.
314327
// Returns CWeaponCSBase.m_iClip1 for most weapons and 1 for grenades.
315328
func (e *Equipment) AmmoInMagazine() int {
@@ -375,7 +388,7 @@ func (e *Equipment) AmmoReserve() int {
375388
//
376389
// Intended for internal use only.
377390
func NewEquipment(wep EquipmentType) *Equipment {
378-
return &Equipment{Type: wep, uniqueID: rand.Int63()} //nolint:gosec
391+
return &Equipment{Type: wep, uniqueID: rand.Int63(), uniqueID2: ulid.Make()} //nolint:gosec
379392
}
380393

381394
var equipmentToAlternative = map[EquipmentType]EquipmentType{

0 commit comments

Comments
 (0)