-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_manager.py
More file actions
222 lines (189 loc) · 7.84 KB
/
file_manager.py
File metadata and controls
222 lines (189 loc) · 7.84 KB
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
# This Python File Handles All File Management for ModDude!
# Path: file_manager.py
# Import Modules
import json
import subprocess
from colorama import Fore, Back, Style
import os
import shutil
import datetime
import PySimpleGUI as pg
from ui_menus import ERROR_UI, exit_app, UI_Setup
import core_bonelab
import core_minecraft
import os
from win32com.client import Dispatch
def install_app():
# Initialize Roaming Storage
if not os.path.exists(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager')):
os.mkdir(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager'))
if not os.path.exists(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager', 'images')):
os.mkdir(os.path.join(os.getenv('APPDATA'),
'Mjolnir Modpack Manager', 'images'))
if os.path.exists(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager', 'images', 'packs')):
# Delete old packs folder
shutil.rmtree(os.path.join(os.getenv('APPDATA'),
'Mjolnir Modpack Manager', 'images', 'packs'))
# Create new packs folder
os.mkdir(os.path.join(os.getenv('APPDATA'),
'Mjolnir Modpack Manager', 'images', 'packs'))
if not os.path.exists(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager', 'GameSettings')):
os.mkdir(os.path.join(os.getenv('APPDATA'),
'Mjolnir Modpack Manager', 'GameSettings'))
# Add Themes Folder and Default Themes
# Create Default Themes (Default, Dark, Light) in Json Format
default_theme = {'name': 'Default', 'dark_color': '#5b0079', 'medium_color': '#813C98', 'light_color': '#995aae','waifu_img': ''}
dark_theme = {'name': 'Dark', 'dark_color': '#2d2d2d', 'medium_color': '#535353', 'light_color': '#797979','waifu_img': ''}
themes = [default_theme, dark_theme]
if not os.path.exists(os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager', 'custom_themes')):
os.mkdir(os.path.join(os.getenv('APPDATA'),
'Mjolnir Modpack Manager', 'custom_themes'))
return os.path.join(os.getenv('APPDATA'), 'Mjolnir Modpack Manager'), themes
def game_settings_initialization(game, BASE_DIR, APPDATA_PATH):
# Store the settings in Roaming
if not os.path.exists(os.path.join(APPDATA_PATH, 'Mjolnir Modpack Manager')):
os.mkdir(os.path.join(APPDATA_PATH, 'Mjolnir Modpack Manager'))
Roaming_Path = os.path.join(APPDATA_PATH, 'Mjolnir Modpack Manager')
if not os.path.exists(f'{Roaming_Path}\\GameSettings'):
os.mkdir(f'{Roaming_Path}\\GameSettings')
path = f'{Roaming_Path}\\GameSettings\\{game}_Settings.json'
if game == 'Minecraft':
core_minecraft.initialize_settings(path, APPDATA_PATH)
elif game == 'Bonelab':
core_bonelab.initialize_settings(path)
# Load settings
with open(path, 'r') as f:
settings = eval(f.read())
return settings
def validate_settings(game_name, settings):
if game_name == 'Minecraft':
valid = core_minecraft.validate_settings(settings)
elif game_name == 'Bonelab':
valid = core_bonelab.validate_settings(settings)
return valid
def path_finder(folder, additional_info=""):
# This code will ask the user for the correct path to an install folder and check
# if it is valid. If it is not valid, it will ask again.
layout = [
[pg.Text(f'Please select the correct path to the {folder}:')]]
if additional_info != "":
layout.append([pg.Text(additional_info)])
layout.append([pg.InputText(), pg.FolderBrowse()])
layout.append([pg.Button('OK'), pg.Button('Exit')])
window = pg.Window('ModDude', layout)
while True:
event, values = window.read()
if event in (None, 'Exit'):
exit_app()
elif event == 'OK':
if os.path.exists(values[0]):
window.close()
return values[0]
else:
ERROR_UI('Error', 'Path not found!')
window.close()
path_finder(folder, additional_info)
def copy_folder(src, dst):
# Copy folder to destination
# Check if destination folder exists
if os.path.exists(dst):
# Remove old folder
delete_old(dst)
try:
shutil.copytree(src, dst)
except OSError as e:
print("Error: %s : %s" % (src, e.strerror))
def check_integrity(src, destination):
# Check if all files are in the destination folder
for file in os.listdir(src):
if not os.path.exists(f'{destination}\\{file}'):
return False
return True
def ask_for_backup(PATH):
# Ask user if they want to backup the folder
layout = [
[pg.Text(f'Would you like to backup the folder {PATH}?')],
[pg.Button('Yes'), pg.Button('No')]
]
window = pg.Window('ModDude', layout)
while True:
event, values = window.read()
if event in (None, 'Exit'):
exit_app()
elif event == 'Yes':
window.close()
backup_old(PATH)
return True
elif event == 'No':
window.close()
return False
def ask_for_delete(PATH):
# Split path into list
path_list = PATH.split('\\')
yes = f'Yes, Delete All of The Files in the {path_list[-1]} Folder'
no = 'No, I\'m just Updating/Adding Mods'
# Ask user if they want to delete the folder
layout = [
[pg.Text(f'Found Files at: {PATH}')],
[pg.Text(f'Would you like to delete the old {path_list[-1]} folder?')],
[pg.Text('This is only recommended if you have backed up the folder and are installing a completely different modpack.')],
[pg.Text(f'DELETING THE FOLDER WILL PERMANENTLY DELETE ALL FILES IN THE FOLDER!')],
[pg.Button(yes), pg.Button(no)]
]
window = pg.Window('ModDude', layout)
while True:
event, values = window.read()
if event in (None, 'Exit'):
exit_app()
elif event == yes:
delete_old(PATH)
window.close()
return True
elif event == no:
window.close()
return False
def backup_old(PATH):
# Backup selected folder to a folder of the same name with a timestamp
# Get current date and time in format: YYYY-MM-DD_HH-MM-SS
now = datetime.datetime.now()
now = now.strftime("%Y-%m-%d_%H-%M-%S")
# Create backup folder
os.mkdir(f'{PATH}_{now}')
# Split path into list
path_list = PATH.split('\\')
# Copy folders to backup folder
done = False
MAX_COPY_BACK_UP = len(os.listdir(PATH))
copied_back_up = 0
layout = [[pg.Text(f'Backing up mods to {path_list[-1]}_{now} folder...')],
[pg.ProgressBar(MAX_COPY_BACK_UP, orientation='h', size=(20, 20), key='progressbar_backed_up')]]
window = pg.Window('ModDude', layout)
progress_bar_backed_up = window['progressbar_backed_up']
while not done:
event, values = window.read(timeout=10)
if event in (None, 'Exit'):
exit_app()
print(f'Backing up mods to {PATH}_{now} folder...')
for folder in os.listdir(PATH):
try:
shutil.copytree(f'{PATH}\\{folder}', f'{PATH}_{now}\\{folder}')
except OSError as e:
print("Error: %s : %s" % (f'{PATH}\\{folder}', e.strerror))
copied_back_up += 1
progress_bar_backed_up.UpdateBar(copied_back_up / MAX_COPY_BACK_UP * 100)
done = True
window.close()
return
def delete_old(PATH):
# Delete old folder
try:
shutil.rmtree(PATH)
except OSError as e:
print("Error: %s : %s" % (PATH, e.strerror))
def delete_temp_files(modpack, BASE_DIR):
# Delete all files in the temp folder
try:
shutil.rmtree(f'{BASE_DIR}\\Downloads\\{modpack.pack_name}')
except OSError as e:
print("Error: %s : %s" %
(f'{BASE_DIR}\\Downloads\\{modpack.pack_name}', e.strerror))