-
Notifications
You must be signed in to change notification settings - Fork 0
/
arma3-automate.py
306 lines (225 loc) · 9.61 KB
/
arma3-automate.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
#!/usr/bin/python3
import os
import os.path
import re
from datetime import datetime
from urllib import request
from pathlib import Path
import json
import jsonschema
import argparse
import shutil
from colorama import Fore, Style
from enum import Enum
from typing import Any
from functools import wraps
import glob
class LogLevel(Enum):
DEBUG = 1
INFO = 2
WARNING = 3
ERROR = 4
def __str__(self) -> str:
return str(self.value)
class Log:
_logLevel = LogLevel.INFO
@staticmethod
def requiredLogLevel(level: LogLevel) -> Any:
def logLevelDecorator(func: Any):
@wraps(func)
def wrapper(*args: Any, **kwargs: Any) -> Any:
if Log._logLevel.value <= level.value:
return func(*args, **kwargs)
return wrapper
return logLevelDecorator
@staticmethod
def _log(msg: str, color: str | None = None, prefix: str = "") -> None:
print(f"{color}{prefix}{msg}{Style.RESET_ALL}")
@staticmethod
def setLogLevel(level: LogLevel):
Log._logLevel = level
@staticmethod
@requiredLogLevel(LogLevel.DEBUG)
def debug(msg: str) -> None:
Log._log(msg, color=Fore.CYAN, prefix="DEBUG: ")
@staticmethod
@requiredLogLevel(LogLevel.INFO)
def info(msg: str) -> None:
Log._log(msg, color=Fore.BLUE, prefix="INFO: ")
@staticmethod
@requiredLogLevel(LogLevel.ERROR)
def success(msg: str) -> None:
Log._log(msg, color=Fore.GREEN, prefix="SUCCESS: ")
@staticmethod
@requiredLogLevel(LogLevel.WARNING)
def warning(msg: str) -> None:
Log._log(msg, color=Fore.YELLOW, prefix="WARNING: ")
@staticmethod
@requiredLogLevel(LogLevel.ERROR)
def error(msg: str) -> None:
Log._log(msg, color=Fore.RED, prefix="ERROR: ")
class Config:
_CONFIG_SCHEMA = {
"type": "object",
"properties": {
"steam_cmd": {"type": "string"},
"server_directory": {"type": "string"},
"mod_directory": {"type": "string"},
"steam_user": {"type": "string"},
"mods": {"type": "object"},
"do_game_update": {"type": "boolean"},
"arma3_workshop_id": {"type": "string"},
},
"required": ["steam_cmd", "server_directory", "mod_directory", "mods", "arma3_workshop_id"]
}
def __init__(self, path: str = ".", filename: str = "config.json"):
try:
with open(Path(path) / filename) as jsonFile:
configJson = json.load(jsonFile)
jsonschema.validate(configJson, Config._CONFIG_SCHEMA)
self._configJson = configJson
except FileNotFoundError:
Log.error(
f"File {path}/{filename} does not exist or is not accessible.")
exit()
except json.decoder.JSONDecodeError as error:
Log.error(f"Malformed json file. {error}")
exit()
except jsonschema.ValidationError as error: # type: ignore
# unknownMemberType: ignore
Log.error(f"Malformed json file. {error.message}")
exit()
self._extractConfig()
def _extractConfig(self) -> None:
self.STEAM_CMD = str(self._configJson["steam_cmd"])
self.SERVER_DIR = Path(self._configJson["server_directory"])
self.MODS_DIR = Path(self._configJson["mod_directory"])
self.MODS: list[tuple[str, str]] = list(
self._configJson["mods"].items())
self.STEAM_USER = str(
self._configJson.get("steam_user") if self._configJson.get("steam_user") is not None else None)
self.ARMA_3_WORKSHOP_ID = str(self._configJson["arma3_workshop_id"])
self.WORKSHOP_DIR = self.SERVER_DIR / \
f"steamapps/workshop/content/{self.ARMA_3_WORKSHOP_ID}"
class SteamCmdQuery:
_baseQuery = ""
_parameters: list[str] = []
def __init__(self, exe: str, forceInstallDir: Path, username: str | None = None, autoQuit: bool = True, runAsSudo: bool = True):
self._baseQuery = exe
self._autoQuit = autoQuit
self._runAsSudo = runAsSudo
self.addParameter(f"+force_install_dir {forceInstallDir}")
if username != None:
self.addParameter(f"+login {username}")
def addParameter(self, parameter: str) -> None:
self._parameters.append(parameter)
def _getQueryString(self) -> str:
parameterString = " ".join(self._parameters)
return f"{'sudo ' if self._runAsSudo else ''}{self._baseQuery} {parameterString}"
def run(self):
if self._autoQuit:
self.addParameter("+quit")
queryString = self._getQueryString()
Log.debug(queryString)
os.system(queryString)
def addModDownloadsToQueryParameters(steamCmdQuery: SteamCmdQuery, config: Config) -> SteamCmdQuery:
A3_WORKSHOP_DIR = Path(config.SERVER_DIR) / \
f"steamapps/workshop/content/{config.ARMA_3_WORKSHOP_ID}"
def doesModNeedDownload(modId: str, path: Path) -> bool:
UPDATE_PATTERN = re.compile(
r"workshopAnnouncement.*?<p id=\"(\d+)\">", re.DOTALL)
WORKSHOP_CHANGELOG_URL = "https://steamcommunity.com/sharedfiles/filedetails/changelog"
if os.path.isdir(path) and os.path.isdir(path / str(modId)):
response = request.urlopen(
f"{WORKSHOP_CHANGELOG_URL}/{modId}").read()
response = response.decode("utf-8")
match = UPDATE_PATTERN.search(response)
if match:
updated_at = datetime.fromtimestamp(int(match.group(1)))
created_at = datetime.fromtimestamp(os.path.getctime(path))
return updated_at >= created_at
return True
for modName, modId in config.MODS:
if doesModNeedDownload(modId, A3_WORKSHOP_DIR):
steamCmdQuery.addParameter(
f"+workshop_download_item {config.ARMA_3_WORKSHOP_ID} {modId}")
Log.debug(
f"Added \"{modName}\" ({modId}) to the List of mods to download.")
else:
Log.info(
f"No download or update required for \"{modName}\" ({modId})... SKIPPING")
continue
return steamCmdQuery
def addGameUpdateToQueryParameters(steamCmdQuery: SteamCmdQuery) -> SteamCmdQuery:
ARMA_3_SERVER_ID = "233780"
steamCmdQuery.addParameter(f"+app_update {ARMA_3_SERVER_ID} validate")
return steamCmdQuery
def assertAllModsAreDownloaded(config: Config) -> None:
def isModDownloaded(modId: str, config: Config) -> bool:
path = Path(config.WORKSHOP_DIR) / modId
return (os.path.isdir(path) is True)
abortScript = False
for modName, modId in config.MODS:
if not isModDownloaded(modId, config):
Log.error(
f"Mod \"{modName}\" ({modId}) could not be downloaded! Please check steam-cmd error above and retry later.")
abortScript = True
if abortScript:
Log.error("Unrecoverable error. ABORTING!!!")
exit()
def createModSymlinks(mods: list[tuple[str, str]], config: Config) -> None:
for modName, modId in mods:
link_path = f"{config.MODS_DIR}/{modName}"
real_path = f"{config.WORKSHOP_DIR}/{modId}"
if os.path.isdir(real_path):
if not os.path.islink(link_path):
os.symlink(real_path, link_path)
Log.debug("Creating symlink '{link_path}'.")
else:
Log.debug("Symlink '{link_path}' already present.")
else:
Log.error(
f"Mod '{modName}' was expected in {real_path} but is not present. Are there any download errors?")
exit()
def toLowercase(config: Config) -> None:
os.system(r"(cd {} && find . -depth -exec rename -v 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;)".format(config.ARMA_3_WORKSHOP_ID))
def download_mods(config: Config) -> None:
steamCmdQuery = SteamCmdQuery(
config.STEAM_CMD, config.SERVER_DIR, config.STEAM_USER)
steamCmdQuery = addModDownloadsToQueryParameters(steamCmdQuery, config)
Log.info("Starting Steam-CMD for automatic download/update.")
steamCmdQuery.run()
Log.info("Downloading Complete")
assertAllModsAreDownloaded(config)
toLowercase(config)
Log.info("Creating Mod directories (symbolic links)")
createModSymlinks(config.MODS, config)
Log.success("All Mods successfully downloaded!")
def clean(config: Config) -> None:
if os.path.isdir(config.WORKSHOP_DIR):
Log.debug(f"Deleting '{config.WORKSHOP_DIR}'")
shutil.rmtree(config.WORKSHOP_DIR)
if os.path.isdir(config.MODS_DIR):
Log.debug(f"Deleting '{config.MODS_DIR}/*'")
modFolders = glob.glob(f"{config.MODS_DIR}/*'")
for f in modFolders:
os.unlink(f)
Log.info("Auxiliary files deleted.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description='Arma 3 mod server mod downloaded. See https://github.com/Dieschdel/arma3-automate for more info')
parser.add_argument(
'--clean', help='deletes all downloaded mods and auxiliary files', action="store_true")
parser.add_argument(
"--no-download-mods", action="store_true", help="stops the download of mods but still processes the other flags (debug)")
parser.add_argument(
"--log-level", choices=[level.name for level in list(LogLevel)], help="Sets Log-Level to the specified option")
args = parser.parse_args()
if args.log_level:
Log.setLogLevel(LogLevel[args.log_level])
config = Config(filename="config.json")
Log.info("Config loaded.")
if args.clean:
clean(config)
if not args.no_download_mods:
download_mods(config)