/
main.py
151 lines (116 loc) · 4.49 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
import argparse
import os
import sys
import json
from time import sleep
from utils import manage_requirements
VERSION = '1.2.0' # TODO find a better way than this, grabbing git tags perhaps
parser = argparse.ArgumentParser()
arg_texts = {
'd': 'skip dependency check on start',
'p': 'skip platform check for fxmodes',
's': 'skip version check for fxmodes',
'f': 'displays frame times per thread. Disables available commands while active',
't': 'single threaded mode. Slow, but useful for testing/debugging',
}
parser.add_argument('-d', '--no-deps', help=arg_texts.get('d'), action='store_true')
parser.add_argument('-p', '--no-platform-check', help=arg_texts.get('p'), action='store_true')
parser.add_argument('-s', '--no-version-check', help=arg_texts.get('s'), action='store_true')
parser.add_argument('-f', '--display-frametimes', help=arg_texts.get('f'), action='store_true')
parser.add_argument('-t', '--single-threaded', help=arg_texts.get('t'), action='store_true')
args = parser.parse_args()
no_dependency_check = args.no_deps
flags = {
'no_version_check': args.no_version_check,
'no_platform_check': args.no_platform_check,
}
if not no_dependency_check:
if sys.prefix == sys.base_prefix:
print('Looks like you aren\'t running ImmersiveFX inside a virtual environment')
print('This will likely cause issues or prevent it from running altogether')
print()
else:
manage_requirements()
try:
with open('config.json') as file:
config = json.load(file)
except FileNotFoundError:
print('config file not found. you need to place config.json into the main.py directory.')
print('There is a config.json.example to use as starting point.')
exit()
script_path, *_ = sys.argv
base_dir = os.path.dirname(script_path)
fxmode_path = os.path.join(base_dir, 'fxmodes')
available_fxmodes = {}
for fxmode in os.listdir(fxmode_path):
try:
modes = getattr(__import__(f'fxmodes.{fxmode}', globals(), locals(), [fxmode]), 'modes')
for mode in modes:
available_fxmodes = {
**available_fxmodes,
mode.name: mode,
}
except (ModuleNotFoundError, AttributeError):
continue
if not available_fxmodes:
print('No valid FXModes found, are the dependencies available (e.g. venv activated)? exiting...')
exit(1)
valid_fxmodes = list(available_fxmodes)
selected_fxmode = available_fxmodes.get(config.get('fxmode'))
while not selected_fxmode:
print('No FXMode was found inside your config. No big deal, you can pick it now:\n')
for index, mode in enumerate(valid_fxmodes):
print(f'{index}: {mode}')
choice = input()
try:
selected_fxmode = available_fxmodes.get(valid_fxmodes[int(choice)])
except (IndexError, ValueError):
print(f'Invalid choice! It must be a number bigger than 0 and smaller than {len(valid_fxmodes)}, try again!')
fxmode = selected_fxmode(
core_version=VERSION,
config=config,
launch_arguments=args,
)
print('')
print('ImmersiveFX Core version %s' % VERSION)
print('---------------------------------------------------')
try:
while True:
# the main thread must stay alive and should do as little as possible from this point on.
# heavy lifting is done by the data thread and sending by device threads
if args.display_frametimes:
command = ''
sleep(1)
else:
command = input()
if command == 'reload':
print('reloading...')
fxmode.stop()
try:
with open('config.json') as file:
config = json.load(file)
except FileNotFoundError:
print('config file not found. you need to place config.json into the main.py directory.')
print('There is a config.json.example to use as starting point.')
exit()
fxmode = selected_fxmode(
core_version=VERSION,
config=config,
launch_arguments=args,
)
print('reload done.')
if command == 'start':
print('starting threads...')
fxmode.start_threads()
print('started threads.')
if command == 'stop':
print('stoppting threads...')
fxmode.stop()
print('stopped threads.')
if command == 'exit':
print('exiting...')
fxmode.kill()
break
except KeyboardInterrupt:
fxmode.kill()
exit()