Skip to content

Commit

Permalink
lot of little thing
Browse files Browse the repository at this point in the history
add `directory_remove`
rename `add` to `directory_add`
rename `remove` to `uninstall`
command `version` and `loader` show the current value if no ID provided

fix `list` command with invalide path dir
fix not overwrite a already exiting ".mcsmp.json" when change a dir/path
fix error when deleting a non-existent file
  • Loading branch information
un-pogaz committed Jun 17, 2023
1 parent 1d6be95 commit 8f08e8a
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 48 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Then, install the python script in the folder of your choice.
After installing mcsmp, you have to define a .minecraft folder to manage.\
To add this, use this command:
```bat
mcsmp add <DIRECTORY_NAME> <PATH TO THE .MINECRAFT FOLDER>
mcsmp directory_add <DIRECTORY_NAME> <PATH TO THE .MINECRAFT FOLDER>
```

Once directory are defined, you have to set the Minecraft version assosiated to it, as well as the mod loader used.
Expand Down Expand Up @@ -67,7 +67,7 @@ mcsmp disable <DIRECTORY_NAME> <PROJECT>

And removing a project:
```bat
mcsmp remove <DIRECTORY_NAME> <PROJECT>
mcsmp uninstall <DIRECTORY_NAME> <PROJECT>
```

<br>
Expand Down Expand Up @@ -102,7 +102,7 @@ mcsmp clear-cache [FILE ...]

examples:
```bat
mcsmp add fabric-1.18.2 C:\Users\ME\AppData\Roaming\.minecraft
mcsmp directory_add fabric-1.18.2 C:\Users\ME\AppData\Roaming\.minecraft
mcsmp check fabric-1.18.2 sodium
mcsmp install fabric-1.18.2 sodium
mcsmp install fabric-1.18.2 sodium-extra
Expand All @@ -116,14 +116,14 @@ mcsmp info sodium

<br>

To use mcsmp with datapacks, you must also specify the world you targeting by adding its folder name at the end of the command. Commands that can support the "world last argument" for datapack are: `list`, `check`, `install`, `enable`, `disable`, `remove`, `update`\
To use mcsmp with datapacks, you must also specify the world you targeting by adding its folder name at the end of the command. Commands that can support the "world last argument" for datapack are: `list`, `check`, `install`, `enable`, `disable`, `uninstall`, `update`\
examples, with the world "New Start":
```bat
mcsmp check fabric-1.18.2 rpgtitles "New Start"
mcsmp install fabric-1.18.2 rpgtitles "New Start"
mcsmp install fabric-1.18.2 gm4-bat-grenades "New Start"
mcsmp disable fabric-1.18.2 gm4-bat-grenades "New Start"
mcsmp remove fabric-1.18.2 gm4-bat-grenades "New Start"
mcsmp uninstall fabric-1.18.2 gm4-bat-grenades "New Start"
mcsmp list fabric-1.18.2 "New Start"
mcsmp update fabric-1.18.2 "New Start"
```
Expand Down
104 changes: 61 additions & 43 deletions mcsmp.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ def root(data=None):
data = sort_dict(data)
return sort_dict(_json('mcsmp.json', data))

def _mcsmp(path):
def mcsmp_path(path):
return os.path.join(path, '.mcsmp.json')
def mcsmp(dir, data=None):
def mcsmp(dir, data=None, exit_if_error=True):
path = root().get(dir, None)

if not path:
print(f'The directory "{dir}" his not defined')
exit()
return exit() if exit_if_error else None

if not os.path.exists(path):
print(f'The path "{path}" of the directory "{dir}" doesn\'t exist')
exit()
return exit() if exit_if_error else None

edited = False
data_path = _mcsmp(path)
data_path = mcsmp_path(path)
if data is not None:
edited = True
else:
Expand Down Expand Up @@ -197,7 +197,7 @@ def hash_file(path):



def dir_add(dir, path):
def directory_add(dir, path):
path = os.path.abspath(path).replace('\\', '/')
if not os.path.exists(path):
print(f'The path "{path}" doesn\'t exist')
Expand All @@ -218,8 +218,9 @@ def dir_add(dir, path):
root(r)

if path_old and path_old != path:
_json(_mcsmp(path), _json(_mcsmp(path_old)))
os.remove(_mcsmp(path_old))
if not os.path.exists(path):
_json(mcsmp_path(path), _json(mcsmp_path(path_old)))
safe_del(mcsmp_path(path_old))

data = mcsmp(dir)

Expand All @@ -231,18 +232,35 @@ def dir_add(dir, path):
elif not data['loader']:
print(f"Don't forget to set a 'loader'")

def directory_remove(dir):
r = root()
if dir in r:
del r[dir]
root(r)
print(f"The directory {dir} has been removed")
else:
print(f"No directory {dir} defined to remove")


def dir_version(dir, version):
def dir_version(dir, version=None):
data = mcsmp(dir)
data['game_version'] = version
print(f'Directorie "{dir}" set to the version {version}')
mcsmp(dir, data)
if version:
data['game_version'] = version
print(f'Directorie "{dir}" set to the version: {version}')
mcsmp(dir, data)
else:
version = data['game_version']
print(f'Directorie "{dir}" is set to the version: {version}')

def dir_loader(dir, loader):
def dir_loader(dir, loader=None):
data = mcsmp(dir)
data['loader'] = loader.lower()
print(f'Directorie "{dir}" set to the loader {loader}')
mcsmp(dir, data)
if loader:
data['loader'] = loader.lower()
print(f'Directorie "{dir}" set to the loader: {loader}')
mcsmp(dir, data)
else:
loader = data['loader']
print(f'Directorie "{dir}" is set to the loader: {loader}')


def test_version(dir, data, _exit=True):
Expand Down Expand Up @@ -295,7 +313,7 @@ def get_print_filename(enabled, present):
return ('' if enabled else (' [disabled]' if present else ' !!not present!!'))


def project_list(dir, world=None):
def project_list(dir=None, world=None):

def print_basic(name, data):
path = data['path']
Expand All @@ -309,10 +327,15 @@ def print_basic(name, data):
print(f'No directorys has defined')
return
for name in r:
print_basic(name, mcsmp(name))
data = mcsmp(name, exit_if_error=False)
if data:
print_basic(name, data)

if dir is not None:
data = mcsmp(dir)
data = mcsmp(dir, exit_if_error=False)
if not data:
return

print_basic(dir, data)

if world:
Expand Down Expand Up @@ -553,10 +576,7 @@ def install_project_file(dir, data, urlslug, world=None):
return None

if filename_old and filename_old != filename:
try:
os.remove(path_filename_old)
except:
pass
safe_del(path_filename_old)

if world:
if world not in data[project_type]:
Expand Down Expand Up @@ -646,7 +666,7 @@ def is_installed(dependencie):
return False


def project_remove(dir, urlslug, world=None):
def project_uninstall(dir, urlslug, world=None):
urlslug = urlslug.lower()
data = mcsmp(dir)
test_version(dir, data)
Expand All @@ -656,10 +676,7 @@ def project_remove(dir, urlslug, world=None):
if world in data[type] and urlslug in data[type][world]:
path_filename = os.path.join(data['path'], 'saves', world, pt.folder, data[type][world][urlslug])
path_enable(data, type, urlslug, True, world)
try:
os.remove(path_filename)
except:
pass
safe_del(path_filename)

del data[type][world][urlslug]
mcsmp(dir, data)
Expand All @@ -673,10 +690,7 @@ def project_remove(dir, urlslug, world=None):
if urlslug in data[type]:
path_filename = os.path.join(data['path'], pt.folder, data[type][urlslug])
path_enable(data, type, urlslug, True)
try:
os.remove(path_filename)
except:
pass
safe_del(path_filename)

del data[type][urlslug]
mcsmp(dir, data)
Expand Down Expand Up @@ -804,20 +818,21 @@ def usage():
print(" list [DIR] - show all installed projects in specified directory (mods, resourcepacks and datapacks)")
print(" - if no DIR specified, show all defined directory")
print()
print(" add <DIR> <PATH> - add a directory, the target path must the root .minecraft folder")
print(" version <DIR> <ID> - set Minecraft version of a directory")
print(" loader <DIR> <ID> - define the loader of the directory")
print(" directory_add <DIR> <PATH> - add a directory, the target path must the root /.minecraft/ folder")
print(" directory_remove <DIR> - remove a directory")
print(" version <DIR> [ID] - set Minecraft version of a directory, else show the current if no ID")
print(" loader <DIR> [ID] - define the loader of the directory, else show the current if no ID")
print()
print(" check <DIR> <PROJECT> - check if the project is installed")
print(" install <DIR> <PROJECT> - install/update a project")
print(" enable <DIR> <PROJECT> - enable a project")
print(" disable <DIR> <PROJECT> - disable a project")
print(" remove <DIR> <PROJECT> - remove a project")
print(" uninstall <DIR> <PROJECT> - uninstall a project")
print(" update <DIR> - update all projects in a directory")
print()
print(" info <PROJECT> - show info about a project")
print(" api URL [-- PARAMS ...]] - print a API request")
print(" clear-cache [FILE ...]] - clear the cache, or specific cache files")
print(" clear-cache [FILE ...]] - clear the cache, or specific cache files")
print()
print("DIR is the target directory to manage")
print("PROJECT is the slug-name of the wanted project")
Expand All @@ -840,12 +855,15 @@ def main():
elif cmd == 'list':
project_list(get_arg_n(2, False), get_arg_n(3, False))

elif cmd == 'add':
dir_add(get_arg_n(2), get_arg_n(3))
elif cmd == 'directory_add':
directory_add(get_arg_n(2), get_arg_n(3))
elif cmd == 'directory_remove':
directory_remove(get_arg_n(2))

elif cmd == 'version':
dir_version(get_arg_n(2), get_arg_n(3))
dir_version(get_arg_n(2), get_arg_n(3, False))
elif cmd == 'loader':
dir_loader(get_arg_n(2), get_arg_n(3))
dir_loader(get_arg_n(2), get_arg_n(3, False))

elif cmd == 'check':
project_check(get_arg_n(2), get_arg_n(3), get_arg_n(4, False))
Expand All @@ -855,8 +873,8 @@ def main():
project_enable(get_arg_n(2), get_arg_n(3), True, get_arg_n(4, False))
elif cmd == 'disable':
project_enable(get_arg_n(2), get_arg_n(3), False, get_arg_n(4, False))
elif cmd == 'remove':
project_remove(get_arg_n(2), get_arg_n(3), get_arg_n(4, False))
elif cmd == 'uninstall':
project_uninstall(get_arg_n(2), get_arg_n(3), get_arg_n(4, False))
elif cmd == 'update':
project_update(get_arg_n(2), get_arg_n(3, False))

Expand Down

0 comments on commit 8f08e8a

Please sign in to comment.