-
Notifications
You must be signed in to change notification settings - Fork 4
/
synctest.go
252 lines (228 loc) · 6.69 KB
/
synctest.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package ggpo
import (
"github.com/assemblaj/ggpo/internal/util"
"os"
"github.com/assemblaj/ggpo/internal/buffer"
"github.com/assemblaj/ggpo/internal/input"
"github.com/assemblaj/ggpo/internal/polling"
"github.com/assemblaj/ggpo/internal/protocol"
"github.com/assemblaj/ggpo/transport"
)
type SyncTest struct {
session Session
sync Sync
numPlayers int
checkDistance int
lastVerified int
rollingBack bool
running bool
logFp os.File
game string
currentInput input.GameInput
lastInput input.GameInput
savedFrames buffer.RingBuffer[savedInfo]
strict bool
leniantRevert bool
}
type savedInfo struct {
frame int
checksum int
buf []byte
cbuf int
input input.GameInput
}
func NewSyncTest(cb Session,
numPlayers int,
frames int, inputSize int, strict bool) SyncTest {
s := SyncTest{
session: cb,
numPlayers: numPlayers,
checkDistance: frames,
savedFrames: buffer.NewRingBuffer[savedInfo](32)}
s.currentInput.Erase()
s.currentInput.Bits = make([]byte, input.GameInputMaxBytes*input.GameInputMaxPlayers)
s.currentInput.Size = inputSize
var config SyncConfig
config.session = s.session
config.numPredictionFrames = MaxPredictionFrames
config.inputSize = inputSize
s.sync = NewSync(nil, &config)
s.strict = strict
return s
}
func (s *SyncTest) Idle(timeout int, timeFunc ...polling.FuncTimeType) error {
if !s.running {
var info Event
info.Code = EventCodeRunning
s.session.OnEvent(&info)
s.running = true
}
return nil
}
func (s *SyncTest) AddPlayer(player *Player, handle *PlayerHandle) error {
if player.PlayerNum < 1 || player.PlayerNum > s.numPlayers {
return Error{Code: ErrorCodePlayerOutOfRange, Name: "ErrorCodePlayerOutOfRange"}
}
*handle = (PlayerHandle(player.PlayerNum - 1))
return nil
}
func (s *SyncTest) AddLocalInput(player PlayerHandle, values []byte, size int) error {
if !s.running {
return Error{Code: ErrorCodeNotSynchronized, Name: "ErrorCodeNotSynchronized"}
}
//index := int(player)
//for i := 0; i < size; i++ {
// s.currentInput.Bits[index*size+i] |= values[i]
//}
//copy(s.currentInput.Bits, values)
index := int(player)
start := index * size
end := start + size
copy(s.currentInput.Bits[start:end], values)
return nil
}
func (s *SyncTest) SyncInput(discconectFlags *int) ([][]byte, error) {
if s.rollingBack {
var info savedInfo
var err error
info, err = s.savedFrames.Front()
if err != nil {
panic(err)
}
s.lastInput = *info.input.Clone()
} else {
if s.sync.FrameCount() == 0 {
s.sync.SaveCurrentFrame()
}
s.lastInput = *s.currentInput.Clone()
}
//var values = make([]byte, len(s.lastInput.Bits))
//copy(values, s.lastInput.Bits)
values := make([][]byte, s.numPlayers)
offset := 0
counter := 0
for offset < len(s.lastInput.Bits) && counter < s.numPlayers {
values[counter] = s.lastInput.Bits[offset : s.lastInput.Size+offset]
offset += s.lastInput.Size
counter++
}
if *discconectFlags > 0 {
*discconectFlags = 0
}
return values, nil
}
func (s *SyncTest) AdvanceFrame(checksum uint32) error {
s.sync.AdvanceFrame()
s.currentInput.Erase()
util.Log.Printf("End of frame(%d)...\n", s.sync.FrameCount())
if s.rollingBack {
return nil
}
frame := s.sync.FrameCount()
// Hold onto the current frame in our queue of saved states. We'll need
// the checksum later to verify that our replay of the same frame got the
// same results
var err error
var info savedInfo
info.frame = frame
info.input = s.lastInput
info.checksum = s.sync.GetLastSavedFrame().checksum
err = s.savedFrames.Push(info)
if err != nil {
panic(err)
}
if frame-s.lastVerified == s.checkDistance {
// We've gone far enough ahead and should now now start replaying frames
// Load the last verified frame and set the rollback flag to true.
err = s.sync.LoadFrame(s.lastVerified)
if err != nil {
panic(err)
}
s.rollingBack = true
for !s.savedFrames.Empty() {
s.session.AdvanceFrame(0)
// Verify that the checksum of this frame is the same as the one in our
// list
info, err = s.savedFrames.Front()
if err != nil {
panic(err)
}
err = s.savedFrames.Pop()
if err != nil {
panic(err)
}
if info.frame != s.sync.FrameCount() {
util.Log.Printf("Frame number %d does not match saved frame number %d", info.frame, frame)
if s.strict {
panic("RaiseSyncError")
} else {
util.Log.Println("RaiseSyncError: Continuing as normal.")
//s.revert()
//break
}
}
checksum := s.sync.GetLastSavedFrame().checksum
if info.checksum != checksum {
s.LogGameStates(info)
util.Log.Printf("Checksum for frame %d does not match saved (%d != %d)", frame, checksum, info.checksum)
if s.strict {
panic("RaiseSyncError")
} else {
util.Log.Println("RaiseSyncError: Continuing as normal..")
//s.revert()
//break
}
lastVerifiedStateID, _ := s.sync.FindSavedFrameIndex(s.lastVerified)
s.session.OnEvent(&Event{
Code: EventCodeSyncTestDesync,
CurrentState: s.sync.savedState.head,
LastVerified: lastVerifiedStateID,
})
}
util.Log.Printf("Checksum %08d for frame %d matches.\n", checksum, info.frame)
}
s.lastVerified = frame
s.rollingBack = false
}
return nil
}
func (s *SyncTest) revert() {
err := s.sync.LoadFrame(s.lastVerified)
if err != nil {
panic(err)
}
s.leniantRevert = true
for !s.savedFrames.Empty() {
err = s.savedFrames.Pop()
if err != nil {
panic(err)
}
}
}
func (s *SyncTest) LogGameStates(info savedInfo) {
//s.session.LogGameState("saved:", info.buf, len(info.buf))
//s.session.LogGameState("loaded:", s.sync.GetLastSavedFrame().buf, s.sync.GetLastSavedFrame().cbuf)
}
// We must 'impliment' these for this to be a true Session
func (s *SyncTest) DisconnectPlayer(handle PlayerHandle) error {
return Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) GetNetworkStats(handle PlayerHandle) (protocol.NetworkStats, error) {
return protocol.NetworkStats{}, Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) SetFrameDelay(player PlayerHandle, delay int) error {
return Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) SetDisconnectTimeout(timeout int) error {
return Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) SetDisconnectNotifyStart(timeout int) error {
return Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) Close() error {
return Error{Code: ErrorCodeInvalidRequest, Name: "ErrorCodeInvalidRequest"}
}
func (s *SyncTest) Start() {}
func (s *SyncTest) InitializeConnection(c ...transport.Connection) error {
return nil
}