Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a way to set a custom user folder #414

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions custom_user_folder.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[UserFolder]
; custom_user_folder
; Acts as an override to the default Mantella user folder in '../Documents/My Games/Mantella/'
; Use this in case the default path to the user folder of Mantella is inaccessible
custom_user_folder =
50 changes: 38 additions & 12 deletions src/setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import configparser
import logging
import os
import platform
Expand All @@ -17,19 +18,44 @@ def set_cwd_to_exe_dir():
# change the current working directory to the executable's directory
os.chdir(os.path.dirname(sys.executable))

def get_custom_user_folder() -> str:
file_name = "custom_user_folder.ini"
if not os.path.exists(file_name):
return ""

user_folder_config = configparser.ConfigParser()
try:
user_folder_config.read(file_name, encoding='utf-8')

except Exception as e:
logging.error(repr(e))
logging.error(f"Unable to read / open '{file_name}'. If you have recently edited this file, please try reverting to a previous version. This error is normally due to using special characters.")
logging.log(logging.WARNING, "Using default user folder in '../Documents/My Games/Mantella/'.")
return ""

try:
return user_folder_config.get("UserFolder","custom_user_folder")
except Exception as e:
logging.error(f"Could not find option 'custom_user_folder' in section 'UserFolder' in '{file_name}'.")
logging.log(logging.WARNING, "Using default user folder in '../Documents/My Games/Mantella/'.")
return ""

def get_my_games_directory():
documents_path = ""
if platform.system() == "Windows":
reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
documents_path = winreg.QueryValueEx(reg_key, "Personal")[0]
winreg.CloseKey(reg_key)
else:
homepath = os.getenv('HOMEPATH')
if homepath:
documents_path = os.path.realpath(homepath+'/Documents')
documents_path = get_custom_user_folder()
if documents_path == "":
print("ERROR: Could not find 'Documents' folder or equivalent!")
save_dir = Path(os.path.join(documents_path,"My Games","Mantella"))
if platform.system() == "Windows":
reg_key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, r"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders")
documents_path = winreg.QueryValueEx(reg_key, "Personal")[0]
winreg.CloseKey(reg_key)
else:
homepath = os.getenv('HOMEPATH')
if homepath:
documents_path = os.path.realpath(homepath+'/Documents')
if documents_path == "":
print("ERROR: Could not find 'Documents' folder or equivalent!")
save_dir = Path(os.path.join(documents_path,"My Games","Mantella"))
else:
save_dir = Path(documents_path)
save_dir.mkdir(parents=True, exist_ok=True)
return str(save_dir)+'\\'

Expand Down Expand Up @@ -109,7 +135,7 @@ def get_language_info(file_name) -> dict[Hashable, str]:
logging.log(23, f'''Mantella currently running for {config.game}. Mantella mod files located in:
{config.mod_path}''')
if not config.have_all_config_values_loaded_correctly:
logging.error("Cannot start Mantella. Not all settings that are required are set to correct values. Please check the above error messages and correct the corresponding settings!")
logging.error("Cannot start Mantella. Not all settings that are required are set to correct values. This error often occurs when you start Mantella.exe manually without setting up the `Game` tab in the Mantella UI.")

# clean up old instances of exe runtime files
utils.cleanup_mei(config.remove_mei_folders)
Expand Down