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

multiple .minecraft locations support (+ flatpak default) #15

Merged
merged 5 commits into from
May 8, 2023
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ packs/

# Launcher files
GPUCache/
user-preferences.json
78 changes: 72 additions & 6 deletions install.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,82 @@
import argparse
from distutils.dir_util import copy_tree
from zipfile import ZipFile
from util import get_user_preference, set_user_preference

def start_launcher(mc_dir):
subprocess.run(['minecraft-launcher', '--workDir', os.path.abspath(mc_dir)])

def get_user_mcdir():
return os.getenv('HOME') + '/.minecraft'
# get the possibles minecraft home folder
possible_homes = (
os.getenv('HOME') + '/.minecraft',
os.getenv('HOME') + '/.var/app/com.mojang.Minecraft/.minecraft/'
)

def main(zipfile, user_mcdir=None, manual=False):
#remove unexistant paths
possible_homes = [h for h in possible_homes if os.path.exists(h)]

# no minecraft path found, ask the user to insert it
if len(possible_homes) == 0:
return input("No minecraft installation detected, please instert the .minecraft folder path (ctrl + c to cancel): ")

# only one possible home has been found, just return it
elif len(possible_homes) == 1:
return possible_homes[0]

# check if more than two paths exists, ask for the user which one should be used for install
elif len(possible_homes) >= 2:
while True:
print("Multiple minecraft installations detected:")
# print each folder with a number
i = 1 # to have more natural numbers, we're starting to 1
for home in possible_homes:
print(i, "- ", home)
i += 1

#ask the user which one to use
home = input("Which minecraft folder should be used: ")

# if the user replied with something else than a number print an error and loop back
if not home.isdigit():
print("Error: the response should be a number!")

# if the option doesn't exists, tell the user
elif int(home)-1 > len(possible_homes):
print("Error: this option doesn't exists!")

# everything seems to be ok, returning the associated path
else:
return possible_homes[int(home)-1]




def main(zipfile, user_mcdir=None, manual=False, automated=False):

# check which minecraft folder to use
if user_mcdir is None:
user_mcdir = get_user_mcdir()
# load the user preferences file
user_mcdir = get_user_preference("minecraft_dir")

# if the user didn't specify a minecraft folder, ask for it
if user_mcdir is None:
user_mcdir = get_user_mcdir()


# check if the user wants to save the path as default if it's different from the one in the preferences
if user_mcdir != get_user_preference("minecraft_dir") and not automated:
#ask the user if he wants to save the path as default
print("Changes detected in the minecraft folder path. \n OLD: %s\n NEW: %s" % (get_user_preference("minecraft_dir"), user_mcdir))
update_preferences = input("would you like to save this new path as default? (Y/n) ")

# if the user wants to save the path as default, save it
if update_preferences.lower().startswith("y") or update_preferences == "":
set_user_preference("minecraft_dir", user_mcdir)
print("Preferences updated! You can change them with the --mcdir option.")
else:
print("Okay, no updates were made.")

# Extract pack
packname = os.path.splitext(zipfile)[0]
packname = os.path.basename(packname)
Expand Down Expand Up @@ -258,7 +323,8 @@ def main(zipfile, user_mcdir=None, manual=False):
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('zipfile')
parser.add_argument('--manual', dest='forge_disable', action='store_true')
parser.add_argument('--mcdir', dest='mcdir')
parser.add_argument('--manual', dest='forge_disable', action='store_true'),
parser.add_argument('--mcdir', dest='mcdir', help="Minecraft directory, overrides stored preferences")
parser.add_argument('--automated', dest='automated', action='store_true', help="Intended for use by other scripts, limit blocking prompts")
args = parser.parse_args(sys.argv[1:])
main(args.zipfile, args.mcdir, args.forge_disable)
main(args.zipfile, user_mcdir=args.mcdir, manual=args.forge_disable, automated=args.automated)
32 changes: 32 additions & 0 deletions util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# utility functions
import requests
import shutil
import json
import os

def status_bar(text, progress, bar_width=0.5, show_percent=True, borders='[]', progress_ch='#', space_ch=' '):
ansi_el = '\x1b[K\r' # escape code to clear the rest of the line plus carriage return
Expand Down Expand Up @@ -57,3 +59,33 @@ def rename_profile(launcher_profiles, orig_name, new_name):
del launcher_profiles['profiles'][orig_name]
launcher_profiles['profiles'][new_name] = orig_profile
launcher_profiles['profiles'][new_name]['name'] = new_name

def __user_preferences_file():
# create the user preferences file if it doesn't exist
if not os.path.isfile('user-preferences.json'):
with open('user-preferences.json', 'w') as f:
json.dump({}, f)

def get_user_preference(key):
__user_preferences_file()

# load the user preferences file
with open('user-preferences.json', 'r') as f:
prefs = json.load(f)

# return the value if it exists, otherwise return None
if key not in prefs:
return None
return prefs[key]

def set_user_preference(key, value):
__user_preferences_file()

# load the user preferences file
with open('user-preferences.json', 'r') as f:
prefs = json.load(f)

# set the value and save the file
prefs[key] = value
with open('user-preferences.json', 'w') as f:
json.dump(prefs, f, indent=4)