Skip to content

Commit 5ac85be

Browse files
committed
Clean up NewParser(), add ParserConfig (#16)
1 parent a06d41a commit 5ac85be

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

demoinfocs_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ func TestDemoInfoCs(t *testing.T) {
3636
}
3737
defer f.Close()
3838

39-
p := dem.NewParser(f)
39+
p := dem.NewParserWithConfig(f, dem.ParserConfig{
40+
MsgQueueBufferSize: 1000,
41+
})
4042

4143
fmt.Println("Parsing header")
4244
h, err := p.ParseHeader()

parser.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -126,22 +126,36 @@ func (p *Parser) setError(err error) {
126126
}
127127
}
128128

129-
// TODO: Change the New* methods (names + parameters)
130-
131-
// NewParser creates a new Parser on the basis of an io.Reader
132-
// - like os.File or bytes.Reader - that reads demo data.
129+
// NewParser creates a new Parser with the default configuration.
130+
// The demostream io.Reader (e.g. os.File or bytes.Reader) must provide demo data in the '.DEM' format.
131+
// See also: NewCustomParser() & DefaultParserConfig
133132
func NewParser(demostream io.Reader) *Parser {
134-
return NewParserWithBufferSize(demostream, -1)
133+
return NewParserWithConfig(demostream, DefaultParserConfig)
134+
}
135+
136+
// ParserConfig contains the configuration for creating a new Parser.
137+
type ParserConfig struct {
138+
// MsgQueueBufferSize defines the size of the internal net-message queue.
139+
// For large demos, fast i/o and slow CPUs higher numbers are suggested and vice versa.
140+
// The buffer size can easily be in the hundred-thousands to low millions for the best performance.
141+
// A negative value will make the Parser automatically decide the buffer size during ParseHeader()
142+
// based on the number of ticks in the demo (nubmer of ticks = buffer size);
143+
// this is the default behavior for DefaultParserConfig.
144+
// Zero enforces sequential parsing.
145+
MsgQueueBufferSize int
135146
}
136147

137-
// NewParserWithBufferSize returns a new Parser with a custom msgQueue buffer size.
138-
// For large demos, fast i/o and slow CPUs higher numbers are suggested and vice versa.
139-
// The buffer size can easily be in the hundred-thousands to low millions for the best performance.
140-
// A negative value will make the Parser automatically decide the buffer size during ParseHeader()
141-
// based on the number of ticks in the demo (nubmer of ticks = buffer size).
142-
// See also: NewParser()
143-
func NewParserWithBufferSize(demostream io.Reader, msgQueueBufferSize int) *Parser {
148+
// DefaultParserConfig is the default Parser configuration used by NewParser().
149+
// You may set this variable to a custom configuration to be used by NewParser().
150+
var DefaultParserConfig = ParserConfig{
151+
MsgQueueBufferSize: -1,
152+
}
153+
154+
// NewParserWithConfig returns a new Parser with a custom configuration.
155+
// See also: NewParser() & ParserConfig
156+
func NewParserWithConfig(demostream io.Reader, config ParserConfig) *Parser {
144157
var p Parser
158+
145159
// Init parser
146160
p.bitReader = bit.NewLargeBitReader(demostream)
147161
p.instanceBaselines = make(map[int][]byte)
@@ -164,9 +178,10 @@ func NewParserWithBufferSize(demostream io.Reader, msgQueueBufferSize int) *Pars
164178
p.msgDispatcher.RegisterHandler(p.handleFrameParsed)
165179
p.msgDispatcher.RegisterHandler(p.gameState.handleIngameTickNumber)
166180

167-
if msgQueueBufferSize >= 0 {
168-
p.initMsgQueue(msgQueueBufferSize)
181+
if config.MsgQueueBufferSize >= 0 {
182+
p.initMsgQueue(config.MsgQueueBufferSize)
169183
}
184+
170185
return &p
171186
}
172187

0 commit comments

Comments
 (0)