-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
455 lines (366 loc) · 18.8 KB
/
index.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
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
import os
import subprocess
import tkinter as tk
from tkinter import filedialog
from PIL import Image, ImageTk
from pathlib import Path
import shutil
import datetime
bg_color = '#2C3E50'
text_color = '#ECF0F1'
button_color = '#34495E'
button_hover_color = '#4F4F4F'
header_color = '#1ABC9C'
def get_server_dir():
# Find the Steam installation directory from the registry
steam_dir = None
try:
import winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Valve\Steam") as key:
steam_dir = winreg.QueryValueEx(key, "InstallPath")[0]
except Exception:
pass
if steam_dir is None:
steam_dir = input("Unable to find Steam installation directory. Please enter the path manually: ")
# Find the ARMA 3 Dedicated Server folder
server_dir = os.path.join(steam_dir, "steamapps", "common", "Arma 3 Server")
if not os.path.exists(server_dir):
print("Unable to find ARMA 3 Dedicated Server directory. Please enter the path manually.")
server_dir = input("ARMA 3 Dedicated Server path: ")
return server_dir
def get_workshop_dir():
# Find the Steam installation directory from the registry
steam_dir = None
try:
import winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SOFTWARE\WOW6432Node\Valve\Steam") as key:
steam_dir = winreg.QueryValueEx(key, "InstallPath")[0]
except Exception:
pass
if steam_dir is None:
steam_dir = input("Unable to find Steam installation directory. Please enter the path manually: ")
# Find the ARMA 3 installation folder
arma_dir = os.path.join(steam_dir, "steamapps", "common", "Arma 3")
if not os.path.exists(arma_dir):
print("Unable to find ARMA 3 installation directory. Please enter the path manually.")
arma_dir = input("ARMA 3 installation path: ")
# Find the !Workshop folder inside the ARMA 3 installation folder
workshop_dir = os.path.join(arma_dir, "!Workshop")
if not os.path.exists(workshop_dir):
print("Unable to find ARMA 3 Workshop directory. Please enter the path manually.")
workshop_dir = input("ARMA 3 Workshop path: ")
return workshop_dir
def update_mod_keys(keys_dir, mod_dirs, server_params):
# clear all .bikey files from the keys folder
print('Clearing old .bikey files from the keys folder...')
for file in Path(keys_dir).glob('*.bikey'):
if file.name != 'a3.bikey':
file.unlink()
print('Updating mod keys...' + mod_dirs[0])
mod_last_modified = {}
for mods_dir in mod_dirs:
for mod_dir in Path(mods_dir).rglob('*'):
if mod_dir.is_dir():
mod_dir_sanitized = ''.join(c for c in str(mod_dir) if c.isalnum() or c in '._-\\/ ')
if mod_dir_sanitized:
if mod_dir_sanitized not in mod_last_modified:
mod_last_modified[mod_dir_sanitized] = datetime.datetime.fromtimestamp(0)
for file in mod_dir.glob('**/*.bikey'):
mod_time = os.path.getmtime(str(file))
mod_time_dt = datetime.datetime.fromtimestamp(mod_time)
if mod_time_dt > mod_last_modified[mod_dir_sanitized]:
mod_last_modified[mod_dir_sanitized] = mod_time_dt
key_file = Path(keys_dir) / file.name
shutil.copy(str(file), str(key_file))
print(f'Copied file: {file} to {key_file}')
print('Mod keys updated.')
def start_server(keys_dir, mod_dirs, server_params):
# Search for the server executable in the server directory
for file in os.listdir(server_dir):
if file.startswith('arma3server_x64'):
server_exe = os.path.join(server_dir, file)
break
else:
print('Error: arma3server_x64 executable not found in specified server directory')
return
# Search for mod folders in the mod directories
mods = []
for mod_dir in mod_dirs.split(';'):
for mod_folder in os.listdir(mod_dir):
if os.path.isdir(os.path.join(mod_dir, mod_folder)):
mods.append(os.path.join(mod_dir, mod_folder))
# Build the command line arguments
args = [server_exe] + server_params + ['-mod=' + ';'.join(mods)]
# Start the server process
subprocess.Popen(args, creationflags=subprocess.CREATE_NEW_CONSOLE)
def stop_server():
subprocess.run(['taskkill', '/f', '/im', 'arma3server_x64.exe'])
print('Server stopped.')
# GUIDE
with open('params_help_guide.txt', 'r') as file:
PARAMS_HELP_GUIDE = file.read()
# Set up the default server directory and mod directories
if not os.path.exists('profiles'):
os.makedirs('profiles')
# Function to create the main menu
def edit_profile_and_destroy_main(profile_name, main_window):
# Destroy the main menu window
main_window.destroy()
create_profile_edit(profile_name)
# Function to create the main menu
def return_to_menu(profile_window):
# Destroy the main menu window
profile_window.destroy()
create_main_menu()
def create_main_menu():
main_window = tk.Tk()
# Create the main window
icon_image = Image.open('./app_icon.png')
tk_icon = ImageTk.PhotoImage(icon_image)
main_window.wm_iconphoto(True, tk_icon)
main_window.title('ARMA 3 Server Control')
main_window.geometry('600x400')
main_window.configure(bg=bg_color)
# Create the header label
header_label = tk.Label(main_window, text='Server Profile', font=('Arial', 18), fg=text_color, bg=bg_color)
header_label.pack(pady=10)
# Create the profile selection dropdown menu
profile_options = ['Server 1', 'Server 2', 'Server 3'] # Replace with actual profiles
selected_profile = tk.StringVar()
selected_profile.set(profile_options[0])
profile_menu = tk.OptionMenu(main_window, selected_profile, *profile_options)
profile_menu.configure(font=('Arial', 12), bg=button_color, fg=text_color, activebackground=button_hover_color, activeforeground=text_color, highlightthickness=0)
profile_menu.pack(pady=10)
# Create the profile edit button
edit_button = tk.Button(main_window, text='Edit Profile', font=('Arial', 12), bg=button_color, fg=text_color, activebackground=button_hover_color, activeforeground=text_color, highlightthickness=0, command=lambda: edit_profile_and_destroy_main(selected_profile.get(), main_window))
edit_button.pack(pady=10)
main_window.mainloop()
def edit_profile_and_destroy_main(profile_name, main_window):
main_window.destroy()
create_profile_edit(profile_name)
def create_profile_edit(profile_name):
load_profile(profile_name)
# Create the profile edit window
profile_window = tk.Tk()
profile_window.title(f'{profile_name} Editor')
profile_window.geometry('800x720')
# Create the header frame
header_frame = tk.Frame(profile_window, bg=header_color)
header_frame.pack(fill=tk.X)
# Create the header label
header_label = tk.Label(header_frame, text=f'{profile_name}', font=('Helvetica', 20),
bg=header_color, fg=text_color, padx=10)
header_label.pack(side=tk.LEFT)
# Create the server directory frame
server_dir_frame = tk.Frame(profile_window)
server_dir_frame.pack(pady=20)
# Create the server directory label
server_dir_label = tk.Label(server_dir_frame, text='Server Directory:', font=('Helvetica', 14))
server_dir_label.pack(side=tk.LEFT)
# Create the server directory input field
server_dir_input = tk.Entry(server_dir_frame, width=40)
server_dir_input.pack(side=tk.LEFT, padx=10)
server_dir_input.insert(tk.END, server_dir)
# Create the server directory browse button
server_dir_button = tk.Button(server_dir_frame, text='Browse', font=('Helvetica', 14),
command=lambda: browse_for_server_dir(server_dir_input))
server_dir_button.pack(side=tk.LEFT)
# Create a button to open the server directory
server_dir_open_button = tk.Button(server_dir_frame, text='Open server dir', font=('Helvetica', 14),
command=lambda: os.startfile(server_dir_input.get().split(";")[0]))
server_dir_open_button.pack(side=tk.LEFT)
# Create the mod directory frame
mod_dirs_frame = tk.Frame(profile_window)
mod_dirs_frame.pack(pady=20)
# Create the mod directory label
mod_dirs_label = tk.Label(mod_dirs_frame, text='Mod Directories:', font=('Helvetica', 14))
mod_dirs_label.pack(side=tk.LEFT)
# Create the mod directory input field
mod_dirs_input = tk.Entry(mod_dirs_frame, width=40)
mod_dirs_input.pack(side=tk.LEFT, padx=10)
mod_dirs_input.insert(tk.END, mod_dirs)
mod_dirs_button = tk.Button(mod_dirs_frame, text='Browse', font=('Helvetica', 14),
command=lambda: browse_for_mod_dirs(mod_dirs_input))
mod_dirs_button.pack(side=tk.LEFT)
# Create a button to open the first mod directory in the list
open_mod_dir_button = tk.Button(mod_dirs_frame, text='Open mod dir', font=('Helvetica', 14),
command=lambda: os.startfile(mod_dirs_input.get().split(";")[0]))
open_mod_dir_button.pack(side=tk.LEFT)
# Create the server parameters frame
params_frame = tk.Frame(profile_window)
params_frame.pack(side=tk.TOP, pady=10)
# Create the server parameters label
params_label = tk.Label(params_frame, text='Server Parameters:', font=('Helvetica', 14))
params_label.pack(side=tk.TOP)
# Create the server parameters text field
params_text = tk.Text(params_frame, width=50, height=5, font=('Helvetica', 12))
params_text.pack(side=tk.TOP, padx=10)
# Set the default server parameters in the text field
params_text.insert(tk.END, server_params)
# Create the select parameter frame
select_param_frame = tk.Frame(params_frame)
select_param_frame.pack(side=tk.TOP)
# Create the default parameter text
default_param = tk.StringVar(profile_window, 'Insert a parameter...')
# Create the server parameters options menu
params_options = ['-profiles', '-config', '-serverMod', '-world', '-name', '-cfg', '-autoinit',
'-nologs', '-nosplash', '-noFilePatching', '-noSound', '-enableHT', '-hugePages', '-malloc',
'-maxMem=3072', '-maxVRAM=2047', '-noPause', '-noPauseMP', '-exThreads=7', '-filePatching', '-port=2302',
'-password=', '-loadMissionToMemory',
'-ip=127.0.0.1', '-rankings', '-serverLog', '-netlog',
'-h', '-help', '-?', '-showScriptErrors', '-par', '-limitFPS=50', '-checkSignatures']
params_menu = tk.OptionMenu(select_param_frame, default_param, *params_options, command=lambda option: insert_server_param(option, params_text))
params_menu.configure(font=('Arial', 12), bg='#4F4F4F', fg='#FFFFFF', activebackground='#808080', activeforeground='#FFFFFF', highlightthickness=0)
params_menu.pack(pady=5)
# Create the server parameters help button
params_help_button = tk.Button(params_frame, text='Parameter Reference', font=('Helvetica', 12),
command=lambda: show_params_help_guide())
params_help_button.pack(side=tk.TOP, padx=10)
# Function to show the parameters help guide
def show_params_help_guide():
# Create a new window for the guide
guide_window = tk.Toplevel(profile_window)
guide_window.title('Server Parameters Help Guide')
guide_window.geometry('600x400')
# Create a scrollable text box for the guide
guide_text = tk.Text(guide_window, font=('Arial', 12), wrap=tk.WORD, padx=10, pady=10)
guide_text.pack(fill=tk.BOTH, expand=True)
# Insert the help guide text into the text box
guide_text.insert(tk.END, PARAMS_HELP_GUIDE)
guide_text.configure(state=tk.DISABLED)
# Create a scrollbar for the text box
scrollbar = tk.Scrollbar(guide_text)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
guide_text.config(yscrollcommand=scrollbar.set)
scrollbar.config(command=guide_text.yview)
# Create the button frame
button_frame = tk.Frame(profile_window)
button_frame.pack(pady=20)
# Create the footer frame
footer_frame = tk.Frame(profile_window)
footer_frame.pack(pady=20)
# Create the start button
start_button = tk.Button(button_frame, text='Start Server', font=('Helvetica', 14),
command=lambda: start_server(server_dir_input.get(), mod_dirs_input.get(), server_params))
start_button.pack(side=tk.LEFT, padx=10)
# Create the stop button
stop_button = tk.Button(button_frame, text='Stop Server', font=('Helvetica', 14), command=stop_server)
stop_button.pack(side=tk.LEFT, padx=10)
# Create the update mod keys button
update_button = tk.Button(button_frame, text='Update Mod Keys', font=('Helvetica', 14),
command=lambda: update_mod_keys(keys_dir, mod_dirs_input.get().split(";"), server_params))
update_button.pack(side=tk.LEFT, padx=10)
# Create the save button
save_button = tk.Button(footer_frame, text='Save Profile', font=('Helvetica', 14),
command=lambda: save_profile(profile_name, server_dir_input, mod_dirs_input, params_text))
save_button.pack(side=tk.LEFT, padx=10)
# Create the back button
menu_button = tk.Button(footer_frame, text='Select Profile', font=('Helvetica', 14),
command=lambda: return_to_menu(profile_window))
menu_button.pack(side=tk.LEFT, padx=10)
# Create the auto-setup button
auto_setup_button = tk.Button(profile_window, text='Auto-Setup', font=('Arial', 12), bg='#4F4F4F', fg='#FFFFFF', activebackground='#808080', activeforeground='#FFFFFF', highlightthickness=0, command=lambda: auto_setup())
auto_setup_button.pack(pady=10)
def auto_setup():
# Get the ARMA 3 Dedicated Server directory
server_dir = get_server_dir()
workshop_dir = get_workshop_dir()
# Set the default mod directories
mod_dirs = [workshop_dir]
# Set the default server parameters
server_params = "-name=MyServer -port=2302 -password=12345"
# Update the input fields in the profile editor window
server_dir_input.delete(0, tk.END)
server_dir_input.insert(0, server_dir)
mod_dirs_input.delete(0, tk.END)
mod_dirs_input.insert(0, ';'.join(mod_dirs))
params_text.delete('1.0', tk.END)
params_text.insert(tk.END, server_params)
#Stylize
profile_window.config(bg=bg_color)
server_dir_frame.config(bg=bg_color)
mod_dirs_frame.config(bg=bg_color)
params_frame.config(bg=bg_color)
select_param_frame.config(bg=bg_color)
button_frame.config(bg=bg_color)
footer_frame.config(bg=bg_color)
#labels
server_dir_label.config(fg=text_color,bg=bg_color)
mod_dirs_label.config(fg=text_color,bg=bg_color)
params_label.config(fg=text_color,bg=bg_color)
#buttons
server_dir_button.config(bg=button_color, fg=text_color, activebackground=button_hover_color, activeforeground=text_color, highlightthickness=0)
mod_dirs_button.config(bg=button_color, fg=text_color, activebackground=button_hover_color, activeforeground=text_color, highlightthickness=0)
params_help_button.config(bg=button_color, fg=text_color, activebackground=button_hover_color, activeforeground=text_color, highlightthickness=0)
profile_window.mainloop()
def insert_server_param(param, params_text):
params_text.insert(tk.END, f' {param}')
# Function to browse for server directory
def browse_for_server_dir(server_dir_input):
# Open the file dialog and get the selected directory
selected_dir = filedialog.askdirectory()
# Set the selected directory in the input field
server_dir_input.delete(0, tk.END)
server_dir_input.insert(0, selected_dir)
# Function to browse for mod directories
def browse_for_mod_dirs(mod_dirs_input):
# Open the file dialog and get the selected directories
# selected_dirs = filedialog.askdirectory()
dirselect = filedialog.Directory()
selected_dirs = []
while True:
d = dirselect.show()
if not d: break
selected_dirs.append(d)
# Set the selected directories in the input field
mod_dirs_input.delete(0, tk.END)
mod_dirs_input.insert(0, ';'.join(selected_dirs))
def save_profile(selected_profile, server_dir_input, mod_dirs_input, params_text):
# Create the profile file path
profile_path = f'profiles/{selected_profile}.txt'
if os.path.exists(profile_path):
os.remove(profile_path)
# Write the server directory, keys directory, mod directories, and server parameters to the profile file
with open(profile_path, 'w') as f:
f.write(f'server_dir={server_dir_input.get()}\n')
f.write(f'keys_dir={os.path.join(server_dir_input.get(), "keys")}\n')
f.write(f'mod_dirs={mod_dirs_input.get()}\n')
f.write(f'server_params={params_text.get("1.0", tk.END).strip()}\n')
print(f'Saved profile: {selected_profile}')
def load_profile(selected_profile):
# Create the profile file path
profile_path = os.path.join('profiles', f'{selected_profile}.txt')
# Default values for server_dir, keys_dir, mod_dirs, and server_params
default_server_dir = 'path/to/default_server_dir'
default_keys_dir = 'path/to/default_keys_dir'
default_mod_dirs = 'path/to/default_mod_dirs'
default_server_params = 'default_server_params'
if os.path.exists(profile_path):
# Read the server directory, keys directory, mod directories, and server parameters from the profile file
with open(profile_path, 'r') as f:
for line in f:
if line.startswith('server_dir='):
global server_dir
server_dir = line.strip().split('=')[1]
elif line.startswith('keys_dir='):
global keys_dir
keys_dir = line.strip().split('=')[1]
elif line.startswith('mod_dirs='):
global mod_dirs
mod_dirs = line.strip().split('=')[1]
elif line.startswith('server_params='):
global server_params
server_params = line.replace('server_params=', '').strip().split(' ')
else:
# Create a new profile with default values
with open(profile_path, 'w') as f:
f.write(f'server_dir={default_server_dir}\n')
f.write(f'keys_dir={default_keys_dir}\n')
f.write(f'mod_dirs={default_mod_dirs}\n')
f.write(f'server_params={default_server_params}\n')
# Set the global variables to the default values
server_dir = default_server_dir
keys_dir = default_keys_dir
mod_dirs = default_mod_dirs
server_params = default_server_params.split(' ')
create_main_menu()