Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (138 loc) · 5.05 KB
/
Copy pathmain.py
File metadata and controls
184 lines (138 loc) · 5.05 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
# Main code. This should be run.
# This is mostly a wrapper.
# Importing the flags manager.
from winstlib import flags # flags.initflags, flags.raiseflag, flags.getflag
# Initializing flags.
# Set default flags
_flags = ["LoadingError", "FileError", "PhraseError"]
# set full flags list(will be filled)
flagsfull = []
# iterate over all flags and the engine quit one
for flagsl in [_flags, ["EngineWillQuit"]]:
for flag in flagsl:
# add the flags to the full flags list
flagsfull.append(flag)
if flags.initflags(flagsfull):
pass
else:
exit()
# Importing other dependencies once flags are initialized.
from winstlib import fileio # fileio.open
from winstlib import loadtool # loadtool.importmod
from winstlib import phrasetool # phrasetool.phrase_w
from winstlib import putdefaults # putdefaults.add
from winstlib import gamelib # nothing used, just to inject to games
from winstlib import logger # logging :)
import os # just to check if a file exists...
from importlib.metadata import version # Get pip version
# allow quicker use of the stubs from logger with slight overhead.
info = logger.info
warn = logger.warn
error = logger.error
debug = logger.debug
# Make sure pip is safe
pip_version = tuple(map(int, version("pip").split(".")))
info(f"Pip version : {version("pip")}")
if pip_version <= (26, 2, 1):
print("Please update pip before running this.")
flags.raiseflag("EngineWillQuit")
# Avoid bad flags getting through.
def chk_ok():
"""
Checks if the flags are raised and returns True if any is.
Returns:
True if any flag is raised, False otherwise.
"""
# Verify each known flag.
# use only flags other than EngineWillQuit
for flag in _flags:
if flags.getflag(flag):
# Return True(ErrorHappened) if any helper flag error happened.
return True
# Return False otherwise.
return False
# log the start of the loading.
info("Loading DLCbase game... ")
# Load the configuration.
lines = fileio.open(fp="dlc/game.w", mode="r").read().splitlines()
# Phrase configuration.
config = phrasetool.phrase_w(lines)
# Apply defaults for unset keys. This should often say 3 things were skipped.
config = putdefaults.add(cfg=config, data=[["mainfile", ""], ["mainfunction", "main"], ["package", "err.unset.pkg"]])
# Extract package name, main file path and main function to render access easier.
debug("Ectract info")
package = config["package"]
mainfile = config["mainfile"]
mainfunct = config["mainfunction"]
# Verify all information is there.
if package == "":
error("No package name was set.")
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
if mainfile == "":
error("No main file supplied")
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
if mainfunct == "":
error("No main function supplied")
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
# Avoid an useless crash.
if chk_ok():
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
if flags.getflag("EngineWillQuit"):
exit()
# Verify if the main file exists.
if not os.path.exists(mainfile):
error("Main file does not exist.")
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
# Same as before, avoid an useless crash.
if chk_ok():
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
if flags.getflag("EngineWillQuit"):
exit()
# Inform the user of what is running.
info("======")
info(f"== Running package : {package}")
info("======")
# Make sure the code doesn't try to commit suidide.
lines = fileio.open(fp=mainfile, mode="r").read().splitlines()
if "import gamelib" in lines:
warn("Importing gamelib is unsupported. Your code will likely error out.")
warn("This could also be unrelated to the actual code and may just be a mistake.")
# Attempt to load the game.
try:
game = loadtool.importmod(name="mod", path=mainfile)
debug("Read game")
game.gamelib = gamelib
debug("Injected gamelib library")
except Exception as e:
# Handle errors.
print(e)
error("Error loading game")
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
# Once again, avoid a crash.
if chk_ok():
if not flags.getflag("EngineWillQuit"):
flags.raiseflag("EngineWillQuit")
if flags.getflag("EngineWillQuit"):
exit()
# Verify the existance of the requested main function to attempt to run it if exists, when ready.
if hasattr(game, mainfunct):
debug("Running game")
try:
info("===== Game execution start")
getattr(game, mainfunct)()
info("===== Game execution end")
debug("Game closed successfully")
except Exception as e:
# handle errors.
error("Game errored out: ")
error(e)
else:
# Avoid running nonexistant code.
debug("Supplied main function does NOT exist.")