Skip to content

Commit

Permalink
feat: added user files, version comparison & motd updating
Browse files Browse the repository at this point in the history
  • Loading branch information
SeanLatimer committed Mar 10, 2021
1 parent 40ca973 commit 1bc2e8d
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 18 deletions.
23 changes: 23 additions & 0 deletions cpVersion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
// cPVersion, err := UnmarshalCPVersion(bytes)
// bytes, err = cPVersion.Marshal()

package main

import "encoding/json"

func UnmarshalCPVersion(data []byte) (CPVersion, error) {
var r CPVersion
err := json.Unmarshal(data, &r)
return r, err
}

func (r *CPVersion) Marshal() ([]byte, error) {
return json.Marshal(r)
}

type CPVersion struct {
Version string `json:"version"`
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.16

require (
github.com/klauspost/compress v1.11.12
github.com/magiconair/properties v1.8.4 // indirect
github.com/otiai10/copy v1.5.0 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/jwalterweatherman v1.1.0
github.com/stretchr/testify v1.4.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/compress v1.11.12 h1:famVnQVu7QwryBN4jNseQdUKES71ZAOnB6UQQJPZvqk=
github.com/klauspost/compress v1.11.12/go.mod h1:aoV0uJVorq1K+umq18yTdKaF57EivdYsUV+/s2qKfXs=
github.com/magiconair/properties v1.8.4 h1:8KGKTcQQGm0Kv7vEbKFErAoAOFyyacLStRtQSeYtvkY=
github.com/magiconair/properties v1.8.4/go.mod h1:y3VJvCyxH9uVvJTWEGAELF3aiYNyPKd5NZ3oSwXrF60=
github.com/otiai10/copy v1.5.0 h1:SoXDGnlTUZoqB/wSuj/Y5L6T5i6iN4YRAcMCd+JnLNU=
github.com/otiai10/copy v1.5.0/go.mod h1:XWfuS3CrI0R6IE0FbgHsEazaXO8G0LpMp9o8tos0x4E=
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/curr v1.0.0/go.mod h1:LskTG5wDwr8Rs+nNQ+1LlxRjAtTZZjtJW4rMXl6j4vs=
github.com/otiai10/mint v1.3.0/go.mod h1:F5AjcsTsWUqX+Na9fpHb52P8pcRX2CI6A3ctIT91xUo=
github.com/otiai10/mint v1.3.2/go.mod h1:/yxELlJQ0ufhjUwhshSj+wFjZ78CnZ48/1wtmBH1OTc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
20 changes: 14 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ func main() {
Usage: "Directory to install the pack in",
EnvVars: []string{"CP_DIR"},
},
&cli.BoolFlag{
Name: "motd",
Value: false,
Usage: "Enables updating the server properties to reflect the pack version",
EnvVars: []string{"CP_MOTD"},
},
},
Name: "cursepack",
Action: run,
Expand Down Expand Up @@ -74,17 +80,19 @@ func run(ctx *cli.Context) error {
}
if strings.HasSuffix(pack, ".zip") {
return handleZipPack(PackInstallOptions{
Pack: pack,
Path: ctx.String("dir"),
Server: ctx.Bool("server"),
Pack: pack,
Path: ctx.String("dir"),
Server: ctx.Bool("server"),
ServerMotd: ctx.Bool("motd"),
})
}
// IMPLEMENT
return errors.New("Pack IDs are not currently supported")
}

type PackInstallOptions struct {
Pack string
Server bool
Path string
Pack string
Server bool
Path string
ServerMotd bool
}
88 changes: 88 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package main

import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"

"github.com/magiconair/properties"
jww "github.com/spf13/jwalterweatherman"
)

Expand All @@ -22,6 +25,14 @@ func fileExists(filename string) bool {
return !info.IsDir()
}

func folderExists(folder string) bool {
info, err := os.Stat(folder)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}

func installForgeServer(forgeVersion string, path string) error {
jww.INFO.Println("Downloading Forge Server")
installerJar := "forge-" + forgeVersion + "-installer.jar"
Expand Down Expand Up @@ -74,3 +85,80 @@ func installForgeServer(forgeVersion string, path string) error {

return os.Rename(src, dest)
}

func precreateUserDir(path string) error {
err := os.MkdirAll(filepath.Join(path, "mods"), 0700)
if err != nil {
return err
}
err = os.MkdirAll(filepath.Join(path, "config"), 0700)
if err != nil {
return err
}
file, err := os.Create(filepath.Join(path, "README.txt"))
if err != nil {
return err
}
defer file.Close()

_, err = file.WriteString("The contents of this folder should mirror the structure of the pack\n")
if err != nil {
return err
}

_, err = file.WriteString("Any user provided files will be copied from here into the pack\n")
if err != nil {
return err
}

return file.Sync()
}

func writeVersionFile(version string, path string) error {
ver := CPVersion{
Version: version,
}
bytes, err := ver.Marshal()
if err != nil {
return err
}

return ioutil.WriteFile(path, bytes, 0700)
}

// compareVersion will return true if versions match, false if otherwise
func compareVersion(version string, path string) (bool, error) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return false, err
}
ver, err := UnmarshalCPVersion(bytes)
if err != nil {
return false, err
}
return ver.Version == version, nil
}

func updateServerPropsVersion(version string, path string) error {
if fileExists(path) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return err
}

props, err := properties.Load(bytes, properties.UTF8)
props.SetValue("motd", fmt.Sprintf("Version %s", version))

file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()

_, err = props.Write(file, properties.UTF8)
if err != nil {
return err
}
}
return nil
}
65 changes: 53 additions & 12 deletions zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"

"github.com/klauspost/compress/zip"
"github.com/otiai10/copy"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -42,34 +43,74 @@ func handleZipPack(opts PackInstallOptions) error {
return err
}

// TODO: Possibly change this later to not blow away the whole mods folder
modsDest := filepath.Join(packPath, "mods")
err = os.RemoveAll(modsDest)
versionFilePath := filepath.Join(packPath, "cpversion.json")
versionMatch, err := compareVersion(manifest.Version, versionFilePath)
if err != nil {
return err
}
if versionMatch {
jww.INFO.Println("Manifest version matches the currently installed pack version, skipping install.")
jww.INFO.Println("Delete cpversion.json to force an install.")
} else {
modsDest := filepath.Join(packPath, "mods")
err = os.RemoveAll(modsDest)
if err != nil {
return err
}

err = downloadPackMods(manifest, modsDest)
if err != nil {
return err
err = downloadPackMods(manifest, modsDest)
if err != nil {
return err
}

err = extractZipOverrides(zipPath, manifest.Overrides, packPath)
if err != nil {
return err
}
err := writeVersionFile(manifest.Version, versionFilePath)
if err != nil {
return err
}
}

err = extractZipOverrides(zipPath, manifest.Overrides, packPath)
if err != nil {
return err
userFilesPath := filepath.Join(packPath, "user_files")
if !folderExists(userFilesPath) {
jww.INFO.Println("User files not found, precreating...")
err := precreateUserDir(userFilesPath)
if err != nil {
return err
}
} else {
jww.INFO.Println("User files found, copying any that exist...")
cpOpt := copy.Options{
Skip: func(src string) (bool, error) {
return filepath.Base(src) == "README.txt", nil
},
}
err := copy.Copy(userFilesPath, packPath, cpOpt)
if err != nil {
return err
}
}

if opts.Server {
if opts.Server && !versionMatch {
mcVersion := manifest.Minecraft.Version
modLoader := manifest.Minecraft.ModLoaders[0].ID
forgeVersion := mcVersion + "-" + modLoader[6:]
err := installForgeServer(forgeVersion, packPath)
if err != nil {
return err
}

if opts.ServerMotd {
err = updateServerPropsVersion(manifest.Version, filepath.Join(packPath, "server.properties"))
if err != nil {
return err
}
}
}

return err
return nil
}

// https://addons-ecs.forgesvc.net/api/v2/addon/231275/file/3222705
Expand All @@ -94,7 +135,7 @@ func downloadPackMods(manifest ZipManifest, dest string) error {
return err
}

jww.INFO.Printf("Downloading %s", zipAddon.FileName)
jww.DEBUG.Printf("Downloading %s", zipAddon.FileName)
modPath := filepath.Join(dest, path.Base(zipAddon.DownloadURL))
modFile, err := os.Create(modPath)
if err != nil {
Expand Down

0 comments on commit 1bc2e8d

Please sign in to comment.