-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud_game_saving.py
141 lines (123 loc) · 4.08 KB
/
cloud_game_saving.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
"""
Start play game
"""
import sys
import RepositoryModule
def get_config_options():
"""
get config options list
"""
result = []
for class_name in dir(sys.modules['RepositoryModule']):
if class_name[0].isupper() and class_name != 'Repository':
result.append(class_name)
return result
def creat_config(config_name):
"""
Creat a new config file
"""
options = get_config_options()
print("Select one Repository Type : ")
for index, config_type in enumerate(options):
print('ID {} = {}'.format(index, config_type))
choice = int(input("Type ID? : "))
config = getattr(sys.modules['RepositoryModule'], options[choice]).create_config(config_name)
#config = LocalRepository.create_config(config_name)
return config
def pause():
"""
just simulate os.system("pause")
"""
input("Press any key to continue")
def clear_file_name(config_name):
"""
remove illegal char
"""
error_char = ['<', '>', ':', '\"', '/', '\\', '|', '?', '*']
for ecr in error_char:
config_name = config_name.replace(ecr, '_')
return config_name
def run_game(launch_target, is_steam_game=False):
"""
start gane
"""
import subprocess
import platform
if is_steam_game:
#no for mac
subprocess.call(["start", launch_target], shell=True)
else:
commad_list = []
use_shell = False
if platform.system() == 'Windows':
commad_list = ["start", "", "/B", "/WAIT"]
use_shell = True
for parm in sys.argv[1:]:
commad_list.append(parm)
subprocess.call(commad_list, shell=use_shell)
def main():
"""
enter point
"""
import os
import datetime
#change dir for save config
script_path = os.path.abspath(os.path.dirname(__file__))
os.chdir(script_path)
if len(sys.argv) < 2:
print("No launch target")
os.system("pause")
return
is_steam_game = False
launch_target = ' '.join(sys.argv[1:])
config_name = launch_target
wait_second = 30
monitor_process_name = ''
token = sys.argv[1].split('/')
if token[0] == 'steam:':
config_name = token[len(token)-1]
is_steam_game = True
config_name = clear_file_name(config_name)
config_file_path = os.path.join("config", "{}.conf".format(config_name))
config = {}
if not os.path.exists(config_file_path):
from json import dump
config = creat_config(config_name)
config['launch_target'] = launch_target
config['wait_second'] = 30
with open(config_file_path, 'w') as json_file:
dump(config, json_file)
else:
from json import load
with open(config_file_path, 'r') as json_file:
config = load(json_file)
if 'wait_second' in config:
wait_second = config['wait_second']
if 'process_name' in config:
monitor_process_name = config['process_name']
repository_controller = getattr(sys.modules['RepositoryModule']
, config['RepositoryType'])(config)
repository_controller.before_launch_game()
run_game(launch_target, is_steam_game)
if is_steam_game or monitor_process_name:
from helper import process_helper
from time import sleep
print("wait game launch")
sleep(wait_second)
pid = -1
if is_steam_game:
from helper import steam_helper
pid = steam_helper.find_current_steam_game_pid()
else:
pid = process_helper.find_launch_game_pid(monitor_process_name)
if pid == -1:
print("[error] pid not found !")
return
else:
process_helper.wait_game_terminate(pid)
repository_controller.exit_game()
commit_message = "{0:%Y-%m-%d %H:%M:%S}".format(datetime.datetime.now())
repository_controller.backup_save(commit_message)
pause()
if __name__ == "__main__":
main()