-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
198 lines (155 loc) · 5.37 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
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
import re
import time
import shutil
import logging
import enum
from pathlib import Path
from argparse import ArgumentParser, Namespace
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s (%(levelname)s) %(message)s",
handlers=[
logging.StreamHandler(),
logging.FileHandler("logs.txt"),
],
)
logger = logging.getLogger(__name__)
DEFAULT_INTERVAL = 60 * 5
DEFAULT_FOLDER_FORMAT = "%d.%m.%Y_%H-%M-%S"
UBISOFT_SAVEGAMES_PATH = Path(
"C:/Program Files (x86)/Ubisoft/Ubisoft Game Launcher/savegames/"
)
class GameType(enum.StrEnum):
"""Game type"""
AUTO = "auto"
STEAM = "steam"
UPLAY = "uplay"
def get_ubisoft_account_folder() -> Path:
"""Get Ubisoft account folder path
:return: Path to the Ubisoft account folder
"""
base_path = UBISOFT_SAVEGAMES_PATH
if not base_path.is_dir():
raise ValueError(f"'{base_path}' is not a directory")
for item in base_path.iterdir():
if not item.is_dir():
continue
# Skip if not a valid Ubisoft account folder (must be a UUID).
if not re.match(
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$",
item.name,
):
continue
return item
raise ValueError(f"No Ubisoft account folder found in '{base_path}'")
def get_game_type(account_folder: Path) -> GameType:
"""Get game type
:param account_folder: Account folder
:return: Game type
"""
if (account_folder / "1771").is_dir():
return GameType.UPLAY
if (account_folder / "3559").is_dir():
return GameType.STEAM
raise ValueError(f"No GRW saves folder found in '{account_folder}'")
def get_wildlands_saves_folder(game_type: GameType) -> Path:
"""Get Ghost Recon Wildlands saves folder path.
:param game_type: Game type. If you have a steam version of a game, use steam, uplay otherwise
:return: Path to the saves folder
"""
account_folder = get_ubisoft_account_folder()
match game_type:
case GameType.AUTO:
saves_path = account_folder / (
"1771" if (account_folder / "1771").is_dir() else "3559"
)
case GameType.STEAM:
saves_path = account_folder / "3559"
case GameType.UPLAY:
saves_path = account_folder / "1771"
case _:
raise ValueError(f"Invalid game type '{game_type}'")
if not saves_path.is_dir():
raise ValueError(f"'{saves_path}' is not a directory")
return saves_path
def parse_args() -> Namespace:
"""Parse command line arguments
:return: Parsed arguments
"""
parser = ArgumentParser(
description="GRW Backup Tool",
usage="python main.py -i <input folder> -o <output folder> -t <steam/uplay>",
)
parser.add_argument(
"-t",
"--type",
choices=list(map(str, GameType)),
default=GameType.AUTO.value,
help="Game type (Defaults to '%(default)s')",
)
parser.add_argument(
"-o",
"--output",
type=Path,
default=Path.cwd() / "saves",
help="Output folder path (Defaults to './saves')",
)
parser.add_argument(
"-i",
"--interval",
type=int,
default=DEFAULT_INTERVAL,
help="Backup interval in seconds (Defaults to %(default)s sec.)",
)
parser.add_argument(
"-f",
"--format",
type=str,
default=DEFAULT_FOLDER_FORMAT,
help="Backup folder format (Defaults to '%(default)s')",
)
return parser.parse_args()
def init_output_folder(output_folder: Path) -> None:
"""Initialize output folder
:param output_folder: Output folder
"""
if not output_folder.is_dir():
logger.debug(f"Creating output folder at '{output_folder}'")
output_folder.mkdir(parents=True)
else:
logger.debug(f"Using existing output folder at '{output_folder}'")
def main():
args = parse_args()
logging.debug(f"Ubisoft savegames folder: '{UBISOFT_SAVEGAMES_PATH}'")
# Resolving paths
account_folder = get_ubisoft_account_folder()
saves_folder = get_wildlands_saves_folder(args.type)
logging.debug(f"Ubisoft account folder: '{account_folder}'")
logging.debug(
f"Wildlands saves folder: '{saves_folder}' ({get_game_type(account_folder).title()})"
)
# Initializing output folder
output_folder: Path = args.output.resolve() # type: ignore
init_output_folder(output_folder)
logger.info("Backup delay: %d sec.", args.interval)
# Starting backup loop
try:
while True:
has_saves = False
destination_folder = output_folder / time.strftime(args.format)
if not destination_folder.is_dir():
logger.debug(f"Creating backup folder '{destination_folder}'")
destination_folder.mkdir()
for file in saves_folder.iterdir():
if file.is_file():
has_saves = True
logger.debug(f"Copying '{file}' to '{destination_folder}'")
shutil.copy(file, destination_folder)
if not has_saves:
logger.debug("No saves found")
destination_folder.rmdir()
time.sleep(args.interval)
except KeyboardInterrupt:
logger.info("Shutting down gracefully")
if __name__ == "__main__":
main()