forked from d1str4ught/m2dev-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.py
More file actions
139 lines (114 loc) · 3.26 KB
/
start.py
File metadata and controls
139 lines (114 loc) · 3.26 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
import sys
sys.dont_write_bytecode = True
import os
import time
import json
import subprocess
import traceback
import channels
GAMEDIR = os.getcwd()
PIDS_FILE = os.path.join(GAMEDIR, "pids.json")
def print_green(text):
print("\033[1;32m" + text + "\033[0m")
def print_magenta_prompt():
print("\033[0;35m> ", end="", flush=True)
def start_process(exe):
if os.name == "nt":
exe += ".exe"
return subprocess.Popen([exe], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
def try_start(name, cd, exe, pids, key=None):
try:
os.chdir(cd)
proc = start_process(exe)
entry = {"name": name, "pid": proc.pid}
if key:
pids.setdefault(key, []).append(entry)
else:
pids[name] = entry
return True
except Exception as e:
print(f"> Failed to start {name}: {e}")
traceback.print_exc()
return False
def main():
if len(sys.argv) == 1:
print_green("> How many channels to start?")
print_magenta_prompt()
try:
user_input = int(input())
except ValueError:
print("> Invalid number.")
sys.exit(1)
else:
try:
user_input = int(sys.argv[1])
except ValueError:
print("> Invalid argument.")
sys.exit(1)
pids = {}
try:
print_green("> Starting database...")
try_start(
"db",
os.path.join(GAMEDIR, "channels", "db"),
"./db",
pids
)
time.sleep(1)
print_green("> Starting auth...")
try_start(
"auth",
os.path.join(GAMEDIR, "channels", "auth"),
"./game_auth",
pids
)
time.sleep(1)
# Start Channel cores
# 1. Create a dictionary of channel IDs guaranteed to be integers.
# This ensures that string keys (e.g., '99') are correctly treated as numbers.
int_channel_map = {int(k): v for k, v in channels.CHANNEL_MAP.items()}
# 2. Determine which channels to start based on user input
channels_to_start = set()
for channel_id in int_channel_map.keys():
# Add any channel that is within the user's limit (e.g., if user_input is 2, add 1 and 2)
if user_input and channel_id <= user_input:
channels_to_start.add(channel_id)
# If there is no user input limit, start ALL channels
elif not user_input:
channels_to_start.add(channel_id)
# 3. GUARANTEE Channel 99 is included, regardless of the input limit
if 99 in int_channel_map:
channels_to_start.add(99)
# 4. Sort the list to ensure correct startup order (1, 2, ..., 99)
final_start_order = sorted(list(channels_to_start))
# 5. Iterate through the final, clean list
for channel_id in final_start_order:
cores = int_channel_map[channel_id] # Retrieve cores using the integer ID
print_green(f"> Starting CH{channel_id}:")
for core_id, maps in cores.items():
name = f"channel{channel_id}_core{core_id}"
print_green(f"\t> {name}")
try_start(
name,
os.path.join(GAMEDIR, "channels", f"channel{channel_id}", f"core{core_id}"),
f"./{name}",
pids,
"channel"
)
time.sleep(1)
print()
print_green("> The server is running!")
except Exception as e:
print(f"> Unexpected error: {e}")
traceback.print_exc()
finally:
os.chdir(GAMEDIR)
try:
with open(PIDS_FILE, "w") as f:
json.dump(pids, f, indent=2)
print_green(f"> PID file written to {PIDS_FILE}")
except Exception as e:
print(f"> Failed to write PID file: {e}")
traceback.print_exc()
if __name__ == "__main__":
main()