~welt/murse

a8763f6a651f07d9a076d54c487be992bacde339 — welt 3 months ago 80dfaa0
Ignore gameinfo.txt during upgrade/repair if it exists, add option to disable this behaviour.
4 files changed, 59 insertions(+), 15 deletions(-)

M main.go
M upgrade.go
M util.go
M verify.go
M main.go => main.go +5 -2
@@ 13,12 13,14 @@ func main() {
	var dry bool
	var http2 bool
	var repair bool
	var owGameInfo bool

	upgrade := flaggy.NewSubcommand("upgrade")
	upgrade.Description = DESC_UPGRADE
	upgrade.String(&url, "u", "url", DESC_REPO)
	upgrade.Int(&threads, "c", "threads", "Number of threads to use for downloading, minimum two.")
	upgrade.Bool(&http2, "2", "http2", DESC_HTTP2)
	upgrade.Bool(&owGameInfo, "G", "overwrite-gameinfo", "When upgrading in a directory named open_fortress, overwrite any existing gameinfo.txt file.")
	upgrade.AddPositionalValue(&dir, "directory", 1, true, DESC_FILES)
	flaggy.AttachSubcommand(upgrade, 1)



@@ 26,8 28,9 @@ func main() {
	verify.Description = "Verifies an installation's files."
	verify.AddPositionalValue(&dir, "directory", 1, true, "Directory of game files.")
	verify.String(&url, "u", "url", DESC_REPO)
	verify.Bool(&repair, "r", "repair", "Repair the files where problems are found automatically.")
	verify.Bool(&http2, "2", "http2", DESC_HTTP2)
	verify.Bool(&repair, "r", "repair", "Repair the files where problems are found automatically.")
	verify.Bool(&owGameInfo, "G", "overwrite-gameinfo", "When repairing in a directory named open_fortress, overwrite any existing gameinfo.txt file.")
	flaggy.AttachSubcommand(verify, 1)

	var tvsdir string


@@ 47,7 50,7 @@ func main() {
			flaggy.ShowHelp("Must have at least two threads.")
			os.Exit(1)
		}
		os.Exit(upgradeMain(dir, url, threads, http2))
		os.Exit(upgradeMain(dir, url, threads, http2, owGameInfo))
	}

	if verify.Used {

M upgrade.go => upgrade.go +17 -3
@@ 10,7 10,7 @@ import (
	"strings"
)

func upgradeMain(dir string, url string, threads int, http2 bool) int {
func upgradeMain(dir string, url string, threads int, http2 bool, overwGameInfo bool) int {
	if !strings.HasSuffix(url, "/") {
		url = url + "/"
	}


@@ 98,9 98,25 @@ func upgradeMain(dir string, url string, threads int, http2 bool) int {
		return true
	}

	isOF := (filepath.Dir(dir) == "open_fortress")

	for _, v := range writes {
		change := v
		path := filepath.FromSlash(change.Path)
		fpath := filepath.Join(dir, path)
		f := func() error {
			if !overwGameInfo && isOF && change.Path == "gameinfo.txt" {
				exists, err := isExists(fpath)
				if err != nil {
					return err
				}

				if exists {
					fmt.Println("IGNORE", change.Path)
					return nil
				}
			}

			fmt.Println("WRITE", change.Path)
			object, err := client.GetObject(change.Object)
			if err != nil {


@@ 109,8 125,6 @@ func upgradeMain(dir string, url string, threads int, http2 bool) int {

			defer object.Close()

			path := filepath.FromSlash(change.Path)
			fpath := filepath.Join(dir, path)
			err = os.RemoveAll(fpath)
			if err != nil {
				return err

M util.go => util.go +14 -0
@@ 12,3 12,17 @@ var sfnode, _ = snowflake.NewNode(0)
func errPrintln(a ...any) {
	fmt.Fprintln(os.Stderr, a...)
}

func isExists(dir string) (bool, error) {
	_, err := os.Stat(dir)
	if err != nil {
		if os.IsNotExist(err) {
			return false, nil
		}

		return false, err
	}

	return true, nil

}

M verify.go => verify.go +23 -10
@@ 107,20 107,33 @@ func verifyMain(dir string, url string, repair bool, http2 bool) int {
		}
	}

	for _, v := range writes {
		path := filepath.Join(dir, filepath.FromSlash(v.Path))
		var fix func() error
	isOF := (filepath.Dir(dir) == "open_fortress")

	for _, change := range writes {
		fpath := filepath.Join(dir, filepath.FromSlash(change.Path))
		var fix func() error
		if repair {
			fix = func() error {
				object, err := client.GetObject(v.Object)
				if isOF && change.Path == "gameinfo.txt" {
					exists, err := isExists(fpath)
					if err != nil {
						return err
					}

					if exists {
						fmt.Println("IGNORE", change.Path)
						return nil
					}
				}

				object, err := client.GetObject(change.Object)
				if err != nil {
					return err
				}

				defer object.Close()

				file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
				file, err := os.OpenFile(fpath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0777)
				if err != nil {
					return err
				}


@@ 129,19 142,19 @@ func verifyMain(dir string, url string, repair bool, http2 bool) int {

				_, err = io.Copy(file, object)
				if err == nil {
					fmt.Println("FIXWRITE", v.Path)
					fmt.Println("FIXWRITE", change.Path)
				}
				return err
			}
		}

		file, err := os.Open(path)
		file, err := os.Open(fpath)
		if err != nil {
			if !os.IsNotExist(err) {
				errPrintln(err)
				return 1
			} else {
				fmt.Println("MISSING", v.Path)
				fmt.Println("MISSING", change.Path)
				if repair {
					err := fix()
					if err != nil {


@@ 163,8 176,8 @@ func verifyMain(dir string, url string, repair bool, http2 bool) int {
			file.Close()

			md5 := hex.EncodeToString(hash.Sum(nil))
			if v.MD5 != md5 {
				fmt.Println("INCORRECT", v.Path, "EXPECTED", v.MD5, "GOT", md5)
			if change.MD5 != md5 {
				fmt.Println("INCORRECT", change.Path, "EXPECTED", change.MD5, "GOT", md5)
				if repair {
					err := fix()
					if err != nil {