Compare commits

...

9 Commits

7 changed files with 202 additions and 44 deletions

View File

@ -8,7 +8,7 @@ endif
DESTDIR:=
PREFIX:=/usr/local
VERSION=0.1.3
VERSION=0.1.5
FLAGS:=-buildmode=pie -modcacherw -mod=readonly -trimpath
LDFLAGS:= -ldflags "-s -w -X main.programVersion=${VERSION}"

View File

@ -1,6 +1,6 @@
# Maintainer: Alexander "Arav" Andreev <me@arav.su>
pkgname=mccl
pkgver=0.1.3
pkgver=0.1.5
pkgrel=1
pkgdesc="Console Minecraft launcher"
arch=('i686' 'x86_64' 'arm' 'armv6h' 'armv7h' 'aarch64')

View File

@ -82,8 +82,8 @@ func (ic *InstallCommand) install_vanilla_client() error {
}
fmt.Printf("Version %s (%s) was chosen.\n", version.Id, version.Type)
fmt.Print("Retrieving a manifest file... ")
manifestPath := path.Join(ic.DestDir, "versions", version.Id, version.Id+".json")
fmt.Print("Retrieving ", path.Join("versions", version.Id, version.Id+".json"), "... ")
data, err = util.LoadOrDownloadFile(manifestPath, version.Url, version.Sha1, "sha1", -1)
if err != nil {
return err
@ -93,21 +93,19 @@ func (ic *InstallCommand) install_vanilla_client() error {
if err != nil {
return err
}
fmt.Println("Done")
if !util.IsFileExist(manifestPath) {
fmt.Print("Writing a manifest file to ", manifestPath, " ... ")
if err := util.WriteFile(manifestPath, data); err != nil {
return err
}
fmt.Println("Done")
} else {
fmt.Println(manifestPath, "does exist. Skipping")
fmt.Println("does exist. Skipping")
}
clientPath := path.Join(ic.DestDir, "versions", manifst.Id, manifst.Id+".jar")
fmt.Print("Retrieving ", path.Join("versions", manifst.Id, manifst.Id+".jar"), "... ")
if !util.IsFileExist(clientPath) {
fmt.Print("Retrieving and writing a client to ", clientPath, " ... ")
data, err = util.LoadOrDownloadFile(clientPath, manifst.Downloads["client"].Url,
manifst.Downloads["client"].Sha1, "sha1", manifst.Downloads["client"].Size)
if err != nil {
@ -118,12 +116,11 @@ func (ic *InstallCommand) install_vanilla_client() error {
}
fmt.Println("Done")
} else {
fmt.Println(clientPath, "does exist. Skipping")
fmt.Println("does exist. Skipping")
}
fmt.Printf("Retrieving an asset index %s.json... ", manifst.AssetIndex.Id)
fmt.Print("Retrieving ", path.Join("assets", "indexes", manifst.AssetIndex.Id+".json"), "... ")
assetIndexPath := path.Join(ic.DestDir, "assets", "indexes", manifst.AssetIndex.Id+".json")
data, err = util.LoadOrDownloadFile(assetIndexPath, manifst.AssetIndex.Url,
manifst.AssetIndex.Sha1, "sha1", manifst.AssetIndex.Size)
if err != nil {
@ -134,16 +131,14 @@ func (ic *InstallCommand) install_vanilla_client() error {
if err != nil {
return err
}
fmt.Println("Done")
if !util.IsFileExist(assetIndexPath) {
fmt.Printf("Writing an asset index %s to %s...", manifst.AssetIndex.Id, assetIndexPath)
if err := util.WriteFile(assetIndexPath, data); err != nil {
return err
}
fmt.Println("Done")
} else {
fmt.Printf("%s does exist. Skip writing\n", assetIndexPath)
fmt.Print("does exist. Skip writing")
}
fmt.Print("Retrieving assets...")
@ -182,8 +177,8 @@ func (ic *InstallCommand) install_vanilla_client() error {
if manifst.Logging.Client.Argument != "" {
logConfPath := path.Join(ic.DestDir, "assets", "log_configs", manifst.Logging.Client.File.Id)
fmt.Print("Retrieving ", path.Join("assets", "log_configs", manifst.Logging.Client.File.Id), "... ")
if !util.IsFileExist(logConfPath) {
fmt.Printf("Retrieving and writing %s...", logConfPath)
data, err = util.LoadOrDownloadFile(logConfPath, manifst.Logging.Client.File.Url,
manifst.Logging.Client.File.Sha1, "sha1", manifst.Logging.Client.File.Size)
if err != nil {

View File

@ -23,7 +23,6 @@ func (lc *ListCommand) Run() error {
if err != nil {
return err
}
fmt.Println("There are following versions installed:")
for _, entry := range entries {
if entry.IsDir() {
fmt.Println(entry.Name())

View File

@ -36,7 +36,6 @@ func (rc *RunCommand) Run() error {
return err
}
if rc.UseProfile {
if util.IsFileExist(path.Join(rc.GameDir, mcclprofile.ProfileFileName)) {
p, err := mcclprofile.Load(rc.GameDir)
if err != nil {
@ -54,7 +53,6 @@ func (rc *RunCommand) Run() error {
return err
}
}
}
return rc.run_client()
}
@ -105,7 +103,13 @@ func (rc *RunCommand) run_client() error {
p["auth_xuid"] = "null"
p["user_type"] = "legacy"
p["version_type"] = manifst.Type
p["natives_directory"] = path.Join("", "versions", manifst.Id, "natives")
nativesId := manifst.Id
if manifst.InheritsFrom != "" {
nativesId = manifst.InheritsFrom
}
p["natives_directory"] = path.Join("", "versions", nativesId, "natives")
p["launcher_name"] = "mccl"
p["launcher_version"] = "0.1.0"
p["classpath"] = manifst.BuildClasspath("", "versions")
@ -157,8 +161,8 @@ func (rc *RunCommand) run_client() error {
}
}()
fmt.Printf("Minecraft version %s is started with username %s and player's UUID %s.\n",
manifst.Id, rc.Username, rc.AuthUuid)
fmt.Printf("Minecraft version %s is started with %s, username %s and player's UUID %s.\n",
manifst.Id, rc.JavaXmx, rc.Username, rc.AuthUuid)
if err := cmd.Run(); err != nil && (err.Error() != "exec: already started") {
return err

View File

@ -0,0 +1,167 @@
package commands
import (
"errors"
"fmt"
"mccl/internal/assets"
"mccl/internal/manifest"
"mccl/pkg/util"
"os"
"path"
)
type UninstallCommand struct {
Id string
GameDir string
}
func NewUninstallCommand(id, gameDir string) *UninstallCommand {
return &UninstallCommand{Id: id, GameDir: gameDir}
}
func (uc *UninstallCommand) Run() error {
if uc.Id == "" {
return errors.New("an empty Minecraft version was provided")
}
if uc.GameDir == "" {
return errors.New("an empty path was provided")
}
return uc.uninstall_client()
}
func (uc *UninstallCommand) uninstall_client() error {
var err error
if uc.GameDir == "." {
execPath, _ := os.Executable()
uc.GameDir = path.Dir(execPath)
}
if _, err := os.Stat(uc.GameDir); err != nil {
return os.ErrNotExist
}
manifests := make(map[string]*manifest.Manifest)
entries, err := os.ReadDir(path.Join(uc.GameDir, "versions"))
if err != nil {
return err
}
for _, entry := range entries {
if entry.IsDir() {
data, err := util.ReadFile(path.Join(uc.GameDir, "versions", entry.Name(), entry.Name()+".json"))
if err != nil {
return err
}
manifests[entry.Name()], err = manifest.New(data)
if err != nil {
return err
}
}
}
for id, manifest := range manifests {
if id == uc.Id {
continue
}
if manifest.InheritsFrom == uc.Id {
return fmt.Errorf("%s inherits %s. Uninstall %s first", id, uc.Id, id)
}
}
assetIndexInUse := manifests[uc.Id].InheritsFrom != ""
logConfigInUse := manifests[uc.Id].InheritsFrom != ""
libsToDelete := make(map[string]string)
for _, lib := range manifests[uc.Id].Libraries {
libsToDelete[lib.Name] = lib.Path()
}
for id, manifest := range manifests {
if id == uc.Id {
continue
}
if !assetIndexInUse && manifests[uc.Id].Assets == manifest.Assets {
assetIndexInUse = true
}
if !logConfigInUse && manifests[uc.Id].Logging.Client.File.Id == manifest.Logging.Client.File.Id {
logConfigInUse = true
}
for _, lib := range manifest.Libraries {
if name, ok := libsToDelete[lib.Name]; ok {
delete(libsToDelete, name)
}
}
}
if !logConfigInUse {
dir := path.Join(uc.GameDir, "assets", "log_configs")
if err := os.Remove(path.Join(dir, manifests[uc.Id].Logging.Client.File.Id)); err != nil {
return fmt.Errorf("failed to remove assets/log_configs/%s: %s", manifests[uc.Id].Logging.Client.File.Id, err)
}
if d, err := os.ReadDir(dir); err == nil && len(d) == 0 {
if err := os.Remove(path.Join(dir, manifests[uc.Id].Logging.Client.File.Id)); err != nil {
return fmt.Errorf("failed to remove assets/log_configs directory: %s", err)
}
}
}
if !assetIndexInUse {
dir := path.Join(uc.GameDir, "assets")
assetFiles := make(map[string]*assets.Assets)
for _, manifest := range manifests {
if _, ok := assetFiles[manifest.Assets]; !ok {
data, err := os.ReadFile(path.Join(dir, "indexes", manifest.Assets+".json"))
if err != nil {
return fmt.Errorf("failed to read an index %s file: %s", manifest.Assets, err)
}
assetFiles[manifest.Assets], err = assets.New(data)
if err != nil {
return fmt.Errorf("failed to load asset index %s: %s", manifest.Assets, err)
}
}
}
toRemove := []string{}
for name, obj := range assetFiles[manifests[uc.Id].Assets].Objects {
inUse := false
for id, idx := range assetFiles {
if id == manifests[uc.Id].Assets {
continue
}
if iobj, ok := idx.Objects[name]; ok && iobj.Hash == obj.Hash {
inUse = true
}
}
if !inUse {
toRemove = append(toRemove, obj.Hash)
}
}
for _, hash := range toRemove {
if err := os.Remove(path.Join(dir, "objects", hash[:2], hash)); err != nil {
return fmt.Errorf("failed to remove an object %s/%s: %s", hash[:2], hash, err)
}
}
if err := os.Remove(path.Join(dir, "indexes", manifests[uc.Id].Assets+".json")); err != nil {
return fmt.Errorf("failed to remove %s: %s", path.Join("assets", "indexes", manifests[uc.Id].Assets+".json"), err)
}
}
for _, path := range libsToDelete {
if err := os.Remove(path); err != nil {
return fmt.Errorf("failed to remove %s: %s", path, err)
}
}
return nil
}

View File

@ -3,7 +3,6 @@ package main
import (
"fmt"
"mccl/cmd/mccl/commands"
mcclprofile "mccl/internal/mccl_profile"
"os"
)
@ -71,7 +70,7 @@ func main() {
}
func version() {
fmt.Fprintf(os.Stderr, "mccl ver. %s\nCopyright (c) 2023 Alexander \"Arav\" Andreev <me@arav.su>\n", programVersion)
fmt.Fprintf(os.Stderr, "mccl ver. %s\nCopyright (c) 2023,2024 Alexander \"Arav\" Andreev <me@arav.su>\n", programVersion)
fmt.Fprintln(os.Stderr, "URL: https://git.arav.su/Arav/mccl")
fmt.Fprintln(os.Stderr, "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.")
}
@ -98,11 +97,7 @@ func usage() {
" -U,--uuid UUID\n"+
" player's account UUID (all zeroes by default)\n"+
" --java-Xmx memory\n"+
" java's -Xmx param, e.g. 4G\n"+
" --profile\n"+
" load/save username, UUID, and -Xmx from/to a file.\n"+
" Once saved, you can ommit -u,-U and --java-Xmx,\n"+
" since they will be stored in", mcclprofile.ProfileFileName)
" java's -Xmx param, e.g. 4G")
}
type arguments struct {
@ -203,8 +198,6 @@ func parseArguments(args []string) (parsed arguments, err error) {
}
parsed.JavaXmx = args[i+1]
i++
case "--profile":
parsed.UseProfile = true
default:
err = fmt.Errorf("an unknown option %s was provided", args[i])
return