-
Notifications
You must be signed in to change notification settings - Fork 0
/
VMM.py
303 lines (271 loc) · 12.6 KB
/
VMM.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
# -*- coding:Utf-8 -*-
import sys
import subprocess
from tkinter import *
from tkinter.ttk import *
import os.path
import stat
from shutil import copyfile
def launch_victoria2():
subprocess.run('steam steam://rungameid/42960 &', shell=True, check=True, executable='/bin/sh')
class InterfaceV2MM(object):
"""
Creation and representation of the mod manager interface.
"""
def __init__(self):
self.root = Tk()
self.root.title('Victoria 2 Mod Manager (linux)')
self.manager_data_directory = os.path.expanduser("~") + '/.v2mm/'
self.log_frame = Text(self.root, height=10, width=100)
# The interface is made up of three main frames. One containing two sub-frames, one for logs and one for the
# leave button.
# MAIN FRAME
main_frame = Frame(self.root)
main_frame.grid(row=0, column=0)
# The main frame is composed ow two sub-frames. The left one for actions and buttons dans the right one for
# data shown in a table.
# LEFT SUB-FRAME
self.left_sub_frame = Notebook(main_frame)
# The left sub_frame is made up of two tabs. One for installation and one for usage.
tab_installation = Frame(self.left_sub_frame)
self.left_sub_frame.add(tab_installation, text='Installation')
# STEAM LAUNCHER
steam_launch_label = Label(tab_installation, text="1° First, launch steam.")
steam_launch_label.grid(column=0, row=2, sticky='W')
steam_launcher_button = Button(
tab_installation,
text="Launch Steam",
command=self.launch_steam,
width=15
)
steam_launcher_button.grid(column=0, row=3, pady=(0, 0), sticky='W')
# ADD LAUNCH OPTION
file_label = Label(
tab_installation,
text="2° Now add the text below as a launch option (you can remove it later).")
file_label.grid(column=0, row=4, sticky='W')
launch_option = Entry(tab_installation, width=10)
launch_option.grid(row=5, sticky='ew')
launch_option.insert(0, 'PROTON_DUMP_DEBUG_COMMANDS=1 %command%')
# CHECK PROTON VERSION
proton_version_label = Label(
tab_installation,
text="3° At the same place, check that you force\nthe SteamPlay Compatibility and choose the version"
" « PROTON 4.11-13 ».")
proton_version_label.grid(column=0, row=6, sticky='W')
# LAUNCH VICTORIA 2 ONCE
launch_victoria = Label(tab_installation, text="4° Launch the game once.")
launch_victoria.grid(column=0, row=7, sticky='W')
steam_launcher_button = Button(tab_installation, text="Launch Victoria 2", command=launch_victoria2)
steam_launcher_button.grid(column=0, row=8, pady=(0, 0), sticky='W')
# COLLECT YOUR DATA
grab_data = Label(tab_installation, text="5° Collect your game data")
grab_data.grid(column=0, row=9, sticky='W')
grab_data_button = Button(tab_installation, text="Save your data", command=self.grab_game_data)
grab_data_button.grid(column=0, row=10, pady=(0, 0), sticky='W')
# SWAP YOUR EXECUTABLES
steam_launch_label = Label(
tab_installation,
text="6° Then, modify the executable we want to use."
)
steam_launch_label.grid(column=0, row=11, sticky='W')
steam_launcher_button = Button(
tab_installation,
text="Swap the game executable",
command=self.swap_executable)
steam_launcher_button.grid(column=0, row=12, pady=(0, 0), sticky='W')
self.tab_usage = Frame(self.left_sub_frame)
self.left_sub_frame.add(self.tab_usage, text='Usage')
# GAME FRAME
# LIST OF MODS FOUNT
mod_list_label = Label(self.tab_usage, text="Mods fount in your steam files.")
mod_list_label.grid(column=0, row=1, sticky='W')
self.mod_list = Listbox(self.tab_usage)
self.mod_list.grid(column=0, row=2, sticky='W')
for mod in self.get_list_of_mods(self.manager_data_directory + 'run'):
self.mod_list.insert(END, mod)
# LAUNCH MOD BUTTON
Button(self.tab_usage, text='Launch selected mod', command=self.launch_game_with_selected_mod).grid(column=1, row=2)
self.left_sub_frame.grid(row=1, column=0)
# RIGHT SUB-FRAME
right_sub_frame = Frame(main_frame)
right_sub_frame.grid(row=1, column=1)
data = Treeview(right_sub_frame, columns=2, show=["headings"])
data['columns'] = ('Data', 'Value')
data.heading('#1', text='Data')
data.heading('#2', text='Value')
data.column("Data", minwidth=150)
data.column("Value", minwidth=800)
data.insert('', '1', values=('V2MM directory', self.manager_data_directory))
data.insert(
'',
'1',
values=(
'Game directory',
self.extract_game_directory_from_proton_runfile(self.manager_data_directory + 'run')
)
)
data.grid(column=1, row=4)
# LOGS FRAME
self.log_frame = Text(self.root, height=10, width=100)
self.log_frame.grid(row=2, column=0)
self.log_frame.insert(END, "")
# END BUTTON FRAME
end_button_frame = Frame(self.root)
end_button_frame.grid(row=3, column=0)
Button(end_button_frame, text='Leave', command=self.root.quit).grid(row=0, column=0)
if self.is_game_already_installed():
self.left_sub_frame.select(self.tab_usage)
def is_game_already_installed(self):
"""
How do we check if the mod manager has already been installed ? Well, we just check if some files are already
present in the manager directory.
:return:
"""
if os.path.exists(self.manager_data_directory):
if os.path.exists(self.manager_data_directory + 'mod_launcher'):
self.add_new_logs('Manager directory (' + self.manager_data_directory + ') already exists.')
return True
self.add_new_logs('Unable to find the default manager directory (' + self.manager_data_directory + ').')
return False
@staticmethod
def launch_steam():
"""
Just launches steam in a sub process.
TODO : Find a way to get the command output and display it in the logs.
:return:
"""
subprocess.run('steam &',
shell=True,
check=True,
executable='/bin/sh'
)
def swap_executable(self):
"""
Te game usually uses victoria2.exe while we want it to use v2game.exe.
The solution is to :
- rename victoria2.exe to _victoria2.exe
- copy v2game.exe to victoria2.exe
:return:
"""
game_folder = self.extract_game_directory_from_proton_runfile(self.manager_data_directory + 'run')
try:
os.rename(game_folder + '/victoria2.exe', game_folder + '/_victoria2.exe')
copyfile(game_folder + '/v2game.exe', game_folder + '/victoria2.exe')
self.left_sub_frame.select(self.tab_usage)
except Exception as e:
self.add_new_logs(e.__str__())
def add_new_logs(self, new_logs=''):
"""
Adding new logs means geting the current displayed logs and place the new logs on top of them (at the
beginning).
"""
old_logs = self.log_frame.get("1.0", END)
self.log_frame.delete("1.0", END)
final_logs = new_logs.__str__() + "\n" + old_logs
self.log_frame.insert(END, final_logs)
def launch_game_with_selected_mod(self):
"""
Launches the game with the appropriate mod. Made with the following steps :
1° create a temporary file (erase any existing file with the same name)
2° copy the content of our run file in the new one with a modification
3° execute this new file within a sub-shell
:return:
"""
# Getting the original content.
try:
source_file_opener = open(self.manager_data_directory + 'run', 'r')
executable_content = source_file_opener.readlines()
# Modifying the executable to inject our mod.
new_content = ''
for line in executable_content:
if line.startswith('DEF_CMD'):
new_content += line[0:-2] + ' "-mod=mod/' + self.mod_list.get(ANCHOR) + '")' + "\n"
else:
new_content += line
source_file_opener.close()
# 1° Creating the new file.
try:
destination_file = self.manager_data_directory + 'mod_launcher'
destination_file_opener = open(destination_file, 'w')
# 2° Copy the modified content.
destination_file_opener.write(new_content)
destination_file_opener.close()
# Remember to make the file executable
st = os.stat(destination_file)
os.chmod(destination_file, st.st_mode | stat.S_IEXEC)
# 3° Execute in a sub-shell
subprocess.run(destination_file + ' &', shell=True, check=True, executable='/bin/sh')
except Exception as e:
self.add_new_logs(e.__str__())
except Exception as e:
self.add_new_logs(e.__str__())
def get_list_of_mods(self, runfile_location):
mod_list = ['---']
# We need to extract the game directory which is within the file. It is the line starting by « cd " ».
game_directory = self.extract_game_directory_from_proton_runfile(runfile_location)
if not game_directory: # If failure to load mods, send back empty list.
self.add_new_logs('No mods fount.')
return mod_list
for item in os.listdir(game_directory + "/mod/"):
if os.path.isfile(game_directory + "/mod/" + item): # Is it a file ?
# And is its extension « .mod » ?
if item.endswith('.mod'):
self.add_new_logs('Mod in directory : ' + item)
mod_list.append(item)
return mod_list
def extract_game_directory_from_proton_runfile(self, runfile_location):
"""
Extracts the game main directory using the data from the run file generated by PROTON.
:param runfile_location: The location of the run file generated by PROTON.
:return:
"""
try:
file_opener = open(runfile_location, 'r')
executable_content = file_opener.readlines()
for line in executable_content:
if line.startswith('cd "'):
return line[4:-2]
except FileNotFoundError:
self.add_new_logs('File not found : ' + runfile_location)
return False
return False
def grab_game_data(self):
"""
Collects Proton's debug log and saves them somewhere. Then, updates the mod list.
:return:
"""
# The files are - most of the time - stored in a temporary folder.
# The common location is /tmp/proton_UNIX-USERNAME .
# One log file is enough for the mod manager : /tmp/proton_UNIX-USERNAME/run
# Get the file location
home_directory = os.path.expanduser("~")
username = os.path.basename(home_directory)
# So, the file we want is...
path_to_run_exe = '/tmp/proton_' + username + '/run'
# We want to copy it somewhere. ~/v2mm/run might be a good place.
try:
file_source = open(path_to_run_exe, 'r')
if not os.path.exists(self.manager_data_directory): # Creates the directory if it does not exist.
os.mkdir(home_directory + '/.v2mm/')
self.add_new_logs('Creation of a new directory for the manager : ' + home_directory + '/.v2mm/')
file_destination = open(home_directory + '/.v2mm/run', 'w')
file_destination.write(file_source.read())
file_source.close()
file_destination.close()
except Exception as e:
self.add_new_logs(e.__str__())
# Update the mod listbox once the data are loaded.
self.mod_list.delete(0, END)
for mod in self.get_list_of_mods(self.manager_data_directory + 'run'):
self.mod_list.insert(END, mod)
if __name__ == '__main__':
if len(sys.argv) == 1:
# Just launch the mainloop.
myInterface = InterfaceV2MM()
myInterface.root.mainloop()
exit()
else:
print('Hello, you somehow mislaunched the mod manager. Use this command instead :')
print('python3 VMM.py')
exit()