Skip to content
This repository has been archived by the owner on Oct 27, 2024. It is now read-only.

Moved some things around #11

Merged
merged 9 commits into from
Nov 21, 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
20 changes: 20 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Install",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"args": [
"install",
"-y"
]
}
]
}
Empty file added src/common/__init__.py
Empty file.
91 changes: 91 additions & 0 deletions src/common/maven_coords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# MCM-Manager: Minecraft Modpack Manager
# Copyright (C) 2023 Tygo Everts
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from os import path


class maven_parse:
def __init__(self, arg: str) -> None:
"""
Parse java maven coords to a folder and a file.

Example:
```txt
de.oceanlabs.mcp:mcp_config:1.20.1-20230612.114412@zip
- folder: de/oceanlabs/mcp/mcp_config/1.20.1-20230612.114412/
- file: mcp_config-1.20.1-20230612.114412.zip
```

You can get a file path using `.maven_to_file()`
or an url using `.maven_to_url`.

To directly access the parsed result,
use the variable `.parsed`
"""

self.parsed = self._parse(arg)

def _parse(self, arg: str) -> tuple[str, str]:
# Split the file extension
if '@' in arg:
arg, ext = arg.split('@', 1)
else:
ext = 'jar'

# Until the third colon, replace
# ':' with path.sep. Until the
# first, also replace '.' with it
colons = 0
folder = ''
for char in arg:
if colons == 3:
break
if char == ':':
colons += 1
char = path.sep
elif char == '.' and colons == 0:
char = path.sep
folder += char

if not folder[-1] == '/':
folder += '/'

# Select everything from the first
# colon and replace ':' with '-'
file = ''
for char in arg[arg.find(':')+1:]:
if char == ':':
char = '-'
file += char

# Add the file extension
file += '.' + ext

return folder, file

def to_file(self, *paths: str) -> str:
"""
Create a file path from the maven coord. This calls
a `path.join()` with all paths plus `self.parsed`
"""
return path.join(*paths, *self.parsed)

def to_url(self, base: str) -> str:
"""Create a url from the maven coord"""
if not base[-1] == '/':
base += '/'

return ''.join((base, *self.parsed))
4 changes: 2 additions & 2 deletions src/install/_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
Generic, NotRequired, Optional
)

# --- install/filesize.py --- #
# --- filesize.py --- #
SizeSystem = list[tuple[int, str | tuple[str, str]]]


# --- install --- #
# --- common --- #
Client = Literal['client']
Server = Literal['server']
Side = Literal['client', 'server']
Expand Down
45 changes: 7 additions & 38 deletions src/install/modloaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
from .urls import forge as forge_urls, fabric as fabric_urls
from .loadingbar import loadingbar

from ..common.maven_coords import maven_parse

from ._types import (
Client, Server, Side, Modloader,
MinecraftJson, VersionJson,
Expand Down Expand Up @@ -169,54 +171,21 @@ def replace_arg_vars(self, arg: str, data: dict[str, str]) -> str:
# in the arg string with **data
arg = arg.format(**data)

# Extract when paths ends with '.lzma'
if path.normpath(arg).endswith('.lzma'):
# Extract when a path ends with '.lzma'
if arg.endswith('.lzma'):
with ZipFile(self.installer, 'r') as archive:
archive.extract(arg[1:], self.temp_dir)
return path.join(self.temp_dir, path.normpath(arg[1:]))

# Return arg if it isn't "[something]"
# Return arg when it isn't "[something]"
if arg[0] != '[' and arg[-1] != ']':
return arg

# Remove the brackets
arg = arg.replace('[', '').replace(']', '')

# Split the file extension
if '@' in arg:
arg, file_extension = arg.split('@', 1)
else:
file_extension = 'jar'

# Create the file and folder path
folder = (
# Until the first colon, replace
# ':' and '.' with path.sep
arg[:arg.find(':')]
.replace(':', '.')
.replace('.', path.sep) +

# Then, do the same
# until the third colon
(arg[arg.find(':'):]
if arg.count(':') != 3
else arg[arg.find(':'):
arg.rfind(':')]
).replace(':', path.sep)
)

file = (
# Select everything from
# the first colon and
# replace ':' with '-'
arg[arg.find(':')+1:]
.replace(':', '-') +

# Add the file extension
'.' + file_extension)
arg = arg[1:-1]

# Return the full path
return path.join(self.launcher_dir, 'libraries', folder, file)
return maven_parse(arg).to_file(self.launcher_dir, 'libraries')

def download_jar_files(self) -> None:
"""Download the jar files"""
Expand Down
16 changes: 16 additions & 0 deletions src/runner.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
# MCM-Manager: Minecraft Modpack Manager
# Copyright (C) 2023 Tygo Everts
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from argparse import ArgumentParser
from dataclasses import dataclass, fields
from os import path, makedirs
Expand Down
50 changes: 0 additions & 50 deletions tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,50 +0,0 @@
# MCM-Manager: Minecraft Modpack Manager
# Copyright (C) 2023 Tygo Everts
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from os import path, mkdir
from json import dump
from .vars import install_dir, launcher_dir

# Create the install dir
if not path.isdir(install_dir):
mkdir(install_dir)

# Create the launcher dir and example-manifest.json
if not path.isdir(launcher_dir):
mkdir(launcher_dir)

launcher_profiles = {
"profiles": {},
"settings": {
"crashAssistance": True,
"enableAdvanced": False,
"enableAnalytics": True,
"enableHistorical": False,
"enableReleases": True,
"enableSnapshots": False,
"keepLauncherOpen": False,
"profileSorting": "ByLastPlayed",
"showGameLog": False,
"showMenu": False,
"soundOn": False
},
"version": 3
}

with open(path.join(launcher_dir, 'launcher_profiles.json'), 'w') as fp:
dump(launcher_profiles, fp, indent=4)

del path, mkdir, dump, install_dir, launcher_dir, launcher_profiles, fp
17 changes: 17 additions & 0 deletions tests/assets/launcher_profiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"profiles": {},
"settings": {
"crashAssistance": true,
"enableAdvanced": false,
"enableAnalytics": true,
"enableHistorical": false,
"enableReleases": true,
"enableSnapshots": false,
"keepLauncherOpen": false,
"profileSorting": "ByLastPlayed",
"showGameLog": false,
"showMenu": false,
"soundOn": false
},
"version": 3
}
Empty file added tests/common/__init__.py
Empty file.
71 changes: 71 additions & 0 deletions tests/common/test_maven_coords.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# MCM-Manager: Minecraft Modpack Manager
# Copyright (C) 2023 Tygo Everts
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

import unittest

from src.common.maven_coords import maven_parse


class MavenParse(unittest.TestCase):
def test_maven_coords(self):
cases: list[tuple[str, tuple[str, str], str, str]] = [
( # With three parts
'net.fabricmc:fabric-loader:0.14.22', (
'net/fabricmc/fabric-loader/0.14.22/',
'fabric-loader-0.14.22.jar'
),
'libraries/net/fabricmc/fabric-loader/0.14.22/'
'fabric-loader-0.14.22.jar',
'https://example.com/net/fabricmc/fabric-loader'
'/0.14.22/fabric-loader-0.14.22.jar'
),
( # With four parts
'net.minecraft:client:1.20.1-20230612.114412:slim', (
'net/minecraft/client/1.20.1-20230612.114412/',
'client-1.20.1-20230612.114412-slim.jar'
),
'libraries/net/minecraft/client/1.20.1-20230612.114412/'
'client-1.20.1-20230612.114412-slim.jar',
'https://example.com/net/minecraft/client/'
'1.20.1-20230612.114412/client-1.20.1-20230612.114412-slim.jar'
),
( # With three parts and a file extension
'de.oceanlabs.mcp:mcp_config:1.20.1-20230612.114412@zip', (
'de/oceanlabs/mcp/mcp_config/1.20.1-20230612.114412/',
'mcp_config-1.20.1-20230612.114412.zip'
),
'libraries/de/oceanlabs/mcp/mcp_config/1.20.1-20230612.114412/'
'mcp_config-1.20.1-20230612.114412.zip',
'https://example.com/de/oceanlabs/mcp/mcp_config/'
'1.20.1-20230612.114412/mcp_config-1.20.1-20230612.114412.zip'
),
( # With four parts and a file extension
'de.oceanlabs.mcp:mcp_config:1.20.1-20230612.114412:mappings@txt', (
'de/oceanlabs/mcp/mcp_config/1.20.1-20230612.114412/',
'mcp_config-1.20.1-20230612.114412-mappings.txt'
),
'libraries/de/oceanlabs/mcp/mcp_config/1.20.1-20230612.114412/'
'mcp_config-1.20.1-20230612.114412-mappings.txt',
'https://example.com/de/oceanlabs/mcp/mcp_config/'
'1.20.1-20230612.114412/mcp_config-1.20.1-20230612.114412-mappings.txt'
)
]

for i in cases:
maven = maven_parse(i[0])
self.assertEqual(maven.parsed, i[1])
self.assertEqual(maven.to_file('libraries'), i[2])
self.assertEqual(maven.to_url('https://example.com'), i[3])
27 changes: 27 additions & 0 deletions tests/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# MCM-Manager: Minecraft Modpack Manager
# Copyright (C) 2023 Tygo Everts
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from os import path

CURDIR = path.dirname(path.realpath(__file__))

TMPDIR = path.join(CURDIR, 'temp')
LAUNDIR = path.join(TMPDIR, '.minecraft')
INSTDIR = path.join(TMPDIR, 'gamedir')

ASSETDIR = path.join(CURDIR, 'assets')
MANIFEST = path.join(ASSETDIR, 'manifest.json')
LAUNPROF = path.join(ASSETDIR, 'launcher_profiles.json')
Loading