-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPython_Link.as
More file actions
391 lines (338 loc) · 10.7 KB
/
Python_Link.as
File metadata and controls
391 lines (338 loc) · 10.7 KB
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
Net::Socket@ sock = null;
Net::Socket@ clientSock = null;
enum MessageType {
SCRunStepSync = 1,
SCCheckpointCountChangedSync = 2,
SCLapCountChangedSync = 3,
SCRequestedFrameSync = 4,
SCOnConnectSync = 5,
CSetSpeed = 6,
CRewindToState = 7,
CRewindToCurrentState = 8,
CGetSimulationState = 9,
CSetInputState = 10,
CGiveUp = 11,
CPreventSimulationFinish = 12,
CShutdown = 13,
CExecuteCommand = 14,
CSetTimeout = 15,
CRaceFinished = 16,
CRequestFrame = 17,
CResetCamera = 18,
CSetOnStepPeriod = 19,
CUnrequestFrame = 20,
CToggleInterface = 21,
CIsInMenus = 22,
CGetInputs = 23,
}
const bool debug = false;
const string HOST = "127.0.0.1";
uint16 PORT;
uint RESPONSE_TIMEOUT = 2000;
int next_frame_requested_H = -1;
int next_frame_requested_W = -1;
int on_step_period = 10;
bool on_connect_queued = false;
auto@ simManager = GetSimulationManager();
void Init_Socket(){
if (@sock is null) {
@sock = Net::Socket();
log("Port set to "+PORT);
sock.Listen(HOST, PORT);
}
}
void close_connection(){
@clientSock = null;
Init_Socket();
next_frame_requested_H = -1;
RESPONSE_TIMEOUT = 2000;
}
void WaitForResponse(MessageType type){
auto now = Time::Now;
while (true) {
auto receivedType = HandleMessage();
if (receivedType == type) {
break;
}
if (receivedType == MessageType::CShutdown) {
break;
}
if (receivedType == -1 && Time::Now - now > RESPONSE_TIMEOUT) {
log("Client disconnected due to timeout (" + RESPONSE_TIMEOUT + "ms)");
close_connection();
break;
}
}
}
int HandleMessage()
{
if (clientSock.Available == 0) {
return -1;
}
int type = clientSock.ReadInt32();
switch(type) {
case MessageType::SCRunStepSync: {
break;
}
case MessageType::SCRequestedFrameSync: {
break;
}
case MessageType::SCCheckpointCountChangedSync: {
break;
}
case MessageType::SCOnConnectSync: {
break;
}
case MessageType::CSetSpeed: {
if(debug){
print("Server: SetSpeed message");
}
const float new_speed = clientSock.ReadFloat();
simManager.SetSpeed(new_speed);
if(debug){
print("Server: Set speed to "+new_speed);
}
break;
}
case MessageType::CGiveUp: {
if(debug){
print("Server: Give up");
}
if (simManager.InRace) {
simManager.GiveUp();
}
break;
}
case MessageType::CPreventSimulationFinish: {
if(debug){
print("Server: prevent simulation finish");
}
if (simManager.InRace) {
simManager.PreventSimulationFinish();
}
break;
}
case MessageType::CRewindToState: {
const int32 stateLength = clientSock.ReadInt32();
const auto stateData = clientSock.ReadBytes(stateLength);
if(debug){
print("Server: rewind message");
}
if (simManager.InRace) {
SimulationState state(stateData);
simManager.RewindToState(state);
}
break;
}
case MessageType::CRewindToCurrentState: {
if (simManager.InRace) {
simManager.RewindToState(simManager.SaveState());
}
break;
}
case MessageType::CGetSimulationState: {
auto@ state = simManager.SaveState();
const auto@ data = state.ToArray();
if(debug){
print("Server: get_simulation_state");
}
clientSock.Write(int(data.Length));
clientSock.Write(data);
break;
}
case MessageType::CSetInputState: {
if(debug){
print("Server: Set input state message");
}
const bool left = clientSock.ReadUint8()>0;
const bool right = clientSock.ReadUint8()>0;
const bool accelerate = clientSock.ReadUint8()>0;
const bool brake = clientSock.ReadUint8()>0;
if(debug){
print("Set input state to "+left+right+accelerate+brake);
}
if (simManager.InRace) {
simManager.SetInputState(InputType::Left, left?1:0);
simManager.SetInputState(InputType::Right, right?1:0);
simManager.SetInputState(InputType::Up, accelerate?1:0);
simManager.SetInputState(InputType::Down, brake?1:0);
}
break;
}
case MessageType::CShutdown: {
log("Client disconnected");
close_connection();
break;
}
case MessageType::CExecuteCommand: {
const int32 bytes_to_read = clientSock.ReadInt32();
const string command = clientSock.ReadString(bytes_to_read);
if(debug){
print("Server: command "+command+" received");
}
ExecuteCommand(command);
break;
}
case MessageType::CSetTimeout: {
const uint new_timeout = clientSock.ReadUint32();
if(debug){
print("Server: set timeout to "+new_timeout);
}
RESPONSE_TIMEOUT = new_timeout;
break;
}
case MessageType::CRaceFinished: {
const int is_race_finished = ((simManager.PlayerInfo.RaceFinished || simManager.TickTime>simManager.RaceTime)?1:0);
if(debug){
print("Server: Answering race_finished with "+is_race_finished);
}
clientSock.Write(is_race_finished);
break;
}
case MessageType::CRequestFrame: {
next_frame_requested_W = clientSock.ReadInt32();
next_frame_requested_H = clientSock.ReadInt32();
if(debug){
print("Client requested next frame in size "+next_frame_requested_H+" "+next_frame_requested_W);
}
break;
}
case MessageType::CResetCamera: {
simManager.ResetCamera();
break;
}
case MessageType::CSetOnStepPeriod: {
on_step_period = clientSock.ReadInt32();
break;
}
case MessageType::CUnrequestFrame: {
next_frame_requested_H = -1;
break;
}
case MessageType::CToggleInterface: {
const bool new_val = clientSock.ReadInt32()>0;
ToggleRaceInterface(new_val);
break;
}
case MessageType::CIsInMenus: {
const int response = GetCurrentGameState()==TM::GameState::Menus? 1 : 0;
clientSock.Write(response);
break;
}
case MessageType::CGetInputs: {
TM::InputEventBuffer@ inputs = simManager.get_InputEvents();
const string input_text = inputs.ToCommandsText();
clientSock.Write(int32(input_text.Length));
clientSock.Write(input_text);
break;
}
default: {
log("Server: got unknown message "+type);
break;
}
}
return type;
}
void OnRunStep(SimulationManager@ simManager){
if (@clientSock is null) {
return;
}
if(debug){
print("Server: OnRunStep " + simManager.RaceTime);
}
if(simManager.RaceTime%on_step_period==0 || simManager.TickTime>simManager.RaceTime){
clientSock.Write(MessageType::SCRunStepSync);
clientSock.Write(simManager.RaceTime);
WaitForResponse(MessageType::SCRunStepSync);
}
}
void OnCheckpointCountChanged(SimulationManager@ simManager, int current, int target){
if (@clientSock is null) {
return;
}
if(debug){
print("Server: OnCheckpointCountChanged");
}
clientSock.Write(MessageType::SCCheckpointCountChangedSync);
clientSock.Write(current);
clientSock.Write(target);
WaitForResponse(MessageType::SCCheckpointCountChangedSync);
}
void OnLapCountChanged(SimulationManager@ simManager, int current, int target){
if (@clientSock is null) {
return;
}
if(debug){
print("Server: OnLapCountChanged");
}
clientSock.Write(MessageType::SCLapCountChangedSync);
clientSock.Write(current);
clientSock.Write(target);
WaitForResponse(MessageType::SCLapCountChangedSync);
}
void OnConnect(){
clientSock.Write(MessageType::SCOnConnectSync);
WaitForResponse(MessageType::SCOnConnectSync);
}
void OnQueueProcessed(int fromTime, int toTime, const string&in commandLine, const array<string>&in args)
{
PORT = uint16(GetVariableDouble("custom_port"));
log("Port donadigo" + GetVariableDouble("custom_port"));
Init_Socket();
}
void Main()
{
RegisterVariable("custom_port", 0);
RegisterCustomCommand("queue_processed", "Internal command", OnQueueProcessed);
CommandList cmdList;
cmdList.Content = "queue_processed";
cmdList.Process();
}
//void Main(){
// RegisterVariable("custom_port", 0);
// PORT = uint16(GetVariableDouble("custom_port"));
// Init_Socket();
//}
void OnGameStateChanged(TM::GameState state){
if(state == TM::GameState::Menus && on_connect_queued){
OnConnect();
on_connect_queued = false;
}
// Helps "activate" the game to receive inputs
if (state == TM::GameState::LocalRace) {
Graphics::FocusGameWindow();
}
}
void Render(){
//Bluescreens if you print every Render()
auto @newSock = sock.Accept(0);
if (@newSock !is null) {
@clientSock = @newSock;
newSock.NoDelay = true;
log("Client connected (IP: " + clientSock.RemoteIP + ")");
if(GetCurrentGameState() != TM::GameState::StartUp){
OnConnect();
}
else{
on_connect_queued = true;
}
}
if(next_frame_requested_H>=0){
const auto@ frame = Graphics::CaptureScreenshot(vec2(next_frame_requested_W,next_frame_requested_H));
clientSock.Write(MessageType::SCRequestedFrameSync);
clientSock.Write(frame);
WaitForResponse(MessageType::SCRequestedFrameSync);
next_frame_requested_H = -1;
if(debug){
print("Got response from client for SCRequestedFrameSync");
}
}
}
PluginInfo@ GetPluginInfo(){
PluginInfo info;
info.Author = "JackOE3";
info.Name = "Python Link";
info.Description = "Reproduce close to original TMI <2 python interface with TMI 2.1 sockets. Original author is Agade. Modified by JackOE3.";
info.Version = "0.1";
return info;
}