-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·219 lines (203 loc) · 8.67 KB
/
main.py
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
#==============================================================================
# crispyController FreePIE main
#
# This initializes all games and switches processing based on the
# currently active game/exe.
#
# Requirements:
# - ./Lib/pylib symlinked from $PF86/FreePIE/pylib/crispy (see ./INSTALL)
#
# NB: FreePIE executes this whole file repeatedly every threadExecutionInterval.
# DO NOT put initialization stuff outside of the "if starting:" block!
# (Actually, it sleep(threadExecutionInterval) after each execution, so
# the execution frequency is actually slower than threadExecutionInterval).
#
#==============================================================================
# DEBUGGING: Uncomment this section to allow debugging via native python3
Debug = True
import sys
if sys.implementation.name != 'ironpython':
print("Loading simulator plugins")
from crispy.sim_plugins import *
starting = True
else:
from System import Console # .NET for printf
#==============================================================================
#================= START OF starting: BLOCK =================================
if starting:
import sys
import time
#================================================
class PIEGlobals:
"""Encapsulate all globals for use in modules.
FreePIE has a bunch of special globals which are accessible
only in this main file and don't appear in globals().
For use in modules, we store and pass them in the G object.
"""
#-------------------------
# Standard FreePIE plugins
Key = Key
diagnostics = diagnostics
filters = filters
joystick = joystick
keyboard = keyboard
midi = midi
mouse = mouse
system = system
trackIR = trackIR
vJoy = vJoy
window = window
xbox360 = xbox360
#-------------------------
# Additional custom plugins
#xoutput = xoutput
#-------------------------
# Timing
if sys.implementation.name == 'ironpython':
def clock(self):
return time.clock()
else:
def clock(self):
return time.perf_counter()
#-------------------------
# Output
prt = diagnostics.debug
def printf(self, fmt, *args):
self.prt(fmt % args) # NB: includes newline
# XXX: test the following; only for python 3+
# XXX: need to rewrite all printf calls to add \n
#if sys.implementation.name == 'ironpython':
# def printf(self, fmt, *args):
# """C-style printf"""
# Console.Write(fmt % args)
#else:
# def printf(self, fmt, *args):
# """C-style printf"""
# # NB: can't even write print(..., end='') in python 2.7
# # .. and can't 'from __future__' in FreePIE (it has
# # already injected script code before it reads this file).
# sys.stdout.write(fmt % args)
#-------------------------
# Debugging
dbg_facilities = []
def dbgon(self, facility):
"""Turn on the given debugging facility."""
if facility not in self.dbg_facilities:
self.dbg_facilities.append(facility)
def dbgoff(self, facility):
"""Turn off the given debugging facility."""
if facility in self.dbg_facilities:
self.dbg_facilities.remove(facility)
def dbg(self, facility, fmt, *args):
"""Print this debug message if facility is turned on"""
if not self.Debug: return # fast rejection
if facility in self.dbg_facilities:
self.printf('>> '+fmt, *args)
_printr_recursed = None
def _printr_objs(self, obj, _indent):
""" Internal recursion checker and object printer for printr()"""
if _indent > 40:
self.printf("ABORTING: RECURSION TOO DEEP")
return
if isinstance(obj, list) or isinstance(obj, set):
for i in obj: self._printr_objs(i, _indent)
elif isinstance(obj, dict):
for k in obj: self._printr_objs(obj[k], _indent)
elif '__dict__' in dir(obj): # object
self.printr(obj, _indent)
else: # scalar
pass
def printr(self, obj, _indent=0):
"""Recursively dump/print obj."""
if _indent == 0: # first run
self._printr_recursed = [self] # exclude PIEGlobals
if '__dict__' in dir(obj): # object
if obj not in self._printr_recursed:
self._printr_recursed.append(obj)
self.printf(' '*_indent + str(obj))
for k,v in obj.__dict__.items():
self.printf(' '*(_indent+2) + k + ": " + str(v))
self._printr_objs(v, _indent+4)
else:
self.printf(' '*_indent + str(obj))
self._printr_objs(v, _indent+2)
if _indent == 0:
self._printr_recursed = None
#================================================
def TimingTester(G):
"""Self-contained function to produce timing results."""
if not hasattr(TimingTester, "G"):
# initialize our static variables, print header
TimingTester.G = G
TimingTester.startT = G.clock()
TimingTester.currT = TimingTester.startT
TimingTester.freePIEdeltaT = 0
G.printf( "Measuring actual ExecutionInterval deltaT...")
G.printf( "%20s %10s (%10s)",
"ExecutionIntervals", "actualTime", "deltaT")
return
# accumulate timing stats
TimingTester.prevT = TimingTester.currT
TimingTester.currT = G.clock()
deltaT = TimingTester.currT - TimingTester.prevT
totT = TimingTester.currT - TimingTester.startT
TimingTester.freePIEdeltaT += G.system.threadExecutionInterval
# produce output every 1000ms:
if totT >= 1.000:
G.printf( "%20d %10.4f (%10.4f)",
TimingTester.freePIEdeltaT, totT, deltaT)
#float(totT.total_seconds()),
#float(deltaT.total_seconds()))
TimingTester.freePIEdeltaT = 0
TimingTester.startT = G.clock()
TimingTester.currT = TimingTester.startT
#================================================
# Create the PIEGlobals object
if not 'Debug' in globals(): Debug = False # make sure Debug exists
G = PIEGlobals() # the PIEGlobals object
G.Debug = Debug
#================================================
# Turn on any Debugging facilities
G.dbgon('SW') # GameSwitcher
#G.dbgon('MJSX') # MouseJoy Steer X
#G.dbgon('MJSY') # MouseJoy Steer X
#G.dbgon('MJLX') # MouseJoy Look Y
#G.dbgon('MJLY') # MouseJoy Look Y
#G.dbgon('MJLXS') # MouseJoy Look X Smoothing
#G.dbgon('MJLYS') # MouseJoy Look Y Smoothing
#G.dbgon('MJSXK') # MouseJoy Steer X Key presses
#G.dbgon('MJSYK') # MouseJoy Steer Y Key presses
#G.dbgon('SM') # Smoothing
G.dbgon('ED') # EDTracker
#================================================
# Loop interval (in milliseconds) -- default is 1 ms
system.setThreadTiming(TimingTypes.HighresSystemTimer)
system.threadExecutionInterval = 2
#================================================
# Import Games and initialize the Game Switcher
from crispy.GameSwitcher import GameSwitcher
# GAMES
import Games.EliteDangerous
import Games.EuroTrucks2
import Games.AmerTrucks
import Games.Kerbal
# import Games.NoMansSky
import Games.JoystickTest
switcher = GameSwitcher(G)
G.printf("Init completed; running...")
#===========================================
# Simulator loop
if Debug and sys.implementation.name != 'ironpython':
starting = False
while True:
switcher.Run()
#time.sleep(G.system.threadExecutionInterval)
time.sleep(0.2) # 5Hz for debugging
#================= END OF starting: BLOCK ===================================
#===========================================
# freepie: uncomment to run just a few iterations
#count = 0
#if count > 10: sys.exit()
#count += 1
#TimingTester(G) # uncomment to produce timing output
switcher.Run()