Update progress bar and more embed
This commit is contained in:
@@ -133,6 +133,5 @@ func DiffFolders(oldPath, newPath string) (*DiffResult, error) {
|
||||
close(jobs)
|
||||
wg.Wait()
|
||||
bar.Finish()
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
@@ -36,6 +36,5 @@ func CopyNewFiles(newPath string, result *DiffResult) error {
|
||||
bar.Add(1)
|
||||
}
|
||||
bar.Finish()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ module hdiff-any-game
|
||||
go 1.25.0
|
||||
|
||||
require (
|
||||
github.com/amenzhinsky/go-memexec v0.7.1 // indirect
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
|
||||
github.com/rivo/uniseg v0.4.7 // indirect
|
||||
github.com/schollz/progressbar/v3 v3.18.0 // indirect
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
github.com/amenzhinsky/go-memexec v0.7.1 h1:DVm4cXzklaNWZoTJgZUi/dlXtelhC7QBtX4luKjl1qk=
|
||||
github.com/amenzhinsky/go-memexec v0.7.1/go.mod h1:ApTO9/i2bcii7kvIXi74gum+/zYDzkiOXtuBZoYOKVE=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db h1:62I3jR2EmQ4l5rM/4FEfDWcRD+abF5XlKShorW5LRoQ=
|
||||
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db/go.mod h1:l0dey0ia/Uv7NcFFVbCLtqEBQbrT4OCwCSKTEv6enCw=
|
||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||
|
||||
Binary file not shown.
@@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func runHdiffz(oldPath, newPath, outDiff string) error {
|
||||
args := []string{"-s-64", "-SD", "-c-zstd-21-24", "-d", oldPath, newPath, outDiff}
|
||||
hdiffzPath, err := filepath.Abs(filepath.Join("bin", "hdiffz.exe"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
cmd := exec.Command(hdiffzPath, args...)
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
|
||||
return cmd.Run()
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
@@ -10,48 +10,12 @@ import (
|
||||
)
|
||||
|
||||
//go:embed bin/hdiffz.exe
|
||||
var hdiffz []byte
|
||||
|
||||
//go:embed bin/7za.exe
|
||||
var embeddedFiles embed.FS
|
||||
|
||||
func ensureBinaries() (map[string]string, error) {
|
||||
binDir := "bin"
|
||||
if _, err := os.Stat(binDir); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(binDir, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
files := []string{"hdiffz.exe", "7za.exe"}
|
||||
paths := make(map[string]string)
|
||||
|
||||
for _, f := range files {
|
||||
destPath := filepath.Join(binDir, f)
|
||||
paths[f] = destPath
|
||||
|
||||
if _, err := os.Stat(destPath); os.IsNotExist(err) {
|
||||
data, err := embeddedFiles.ReadFile("bin/" + f)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := os.WriteFile(destPath, data, 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths, nil
|
||||
}
|
||||
var sevenZip []byte
|
||||
|
||||
func main() {
|
||||
paths, err := ensureBinaries()
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
fmt.Println("Binary ready at:", path)
|
||||
}
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
fmt.Print("Enter OLD game path: ")
|
||||
@@ -82,31 +46,48 @@ func main() {
|
||||
hdiffName += ".zip"
|
||||
}
|
||||
|
||||
fmt.Println("Diffing folders...")
|
||||
result, err := DiffFolders(oldPath, newPath)
|
||||
if err != nil {
|
||||
fmt.Println("Error:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Diffing folders done.")
|
||||
|
||||
hdiffFolderPath := filepath.Join(".", "hdiff")
|
||||
os.MkdirAll(hdiffFolderPath, 0755)
|
||||
|
||||
fmt.Println("Copying new files...")
|
||||
if err := CopyNewFiles(newPath, result); err != nil {
|
||||
fmt.Println("Error writing diff:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Copying new files done.")
|
||||
|
||||
fmt.Println("Making hdiff files...")
|
||||
if err := MakeHdiffFile(oldPath, newPath, result.Changed); err != nil {
|
||||
fmt.Println("Error writing diff:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Making hdiff files done.")
|
||||
|
||||
fmt.Println("Zipping hdiff files...")
|
||||
if err := ZipWith7za(hdiffFolderPath, hdiffName); err != nil {
|
||||
fmt.Println("Error writing diff:", err)
|
||||
return
|
||||
}
|
||||
fmt.Println("Zipping hdiff files done.")
|
||||
|
||||
if err := RemoveFolderWithProgress(hdiffFolderPath); err != nil {
|
||||
fmt.Println("Removing hdiff temp files...")
|
||||
if err := RemoveFolderWithProgress(hdiffFolderPath, "🗑️ Removing hdiff temp files"); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "error removing temp dir:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println()
|
||||
fmt.Println("Removing hdiff temp files done.")
|
||||
|
||||
fmt.Println("Done")
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func MakeHdiffFile(oldPath string, newPath string, changedFiles []string) error
|
||||
}
|
||||
|
||||
bar := progressbar.NewOptions(len(changedFiles),
|
||||
progressbar.OptionSetDescription("Creating HDIFF files"),
|
||||
progressbar.OptionSetDescription("📦 Creating HDiff files"),
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionSetWidth(30),
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
@@ -54,7 +54,7 @@ func MakeHdiffFile(oldPath string, newPath string, changedFiles []string) error
|
||||
fmt.Fprintf(os.Stderr, "failed to create dir: %v\n", err)
|
||||
continue
|
||||
}
|
||||
runHdiffz(oldFile, newFile, hdiffPath)
|
||||
Hdiffz(oldFile, newFile, hdiffPath)
|
||||
bar.Add(1)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -4,11 +4,13 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"time"
|
||||
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/amenzhinsky/go-memexec"
|
||||
"github.com/schollz/progressbar/v3"
|
||||
)
|
||||
|
||||
@@ -97,11 +99,6 @@ func ZipWith7za(src, dest string) error {
|
||||
return fmt.Errorf("source folder is empty: %s", src)
|
||||
}
|
||||
|
||||
sevenZipPath, err := filepath.Abs(filepath.Join("bin", "7za.exe"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
destAbs, err := filepath.Abs(filepath.Join(".", dest))
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -112,50 +109,75 @@ func ZipWith7za(src, dest string) error {
|
||||
args = append(args, f.Name())
|
||||
}
|
||||
|
||||
cmd := exec.Command(sevenZipPath, args...)
|
||||
exe, err := memexec.New(sevenZip)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer exe.Close()
|
||||
cmd := exe.Command(args...)
|
||||
cmd.Dir = src
|
||||
cmd.Stdout = os.Stdout
|
||||
cmd.Stderr = os.Stderr
|
||||
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
func RemoveFolderWithProgress(folder string) error {
|
||||
func RemoveFolderWithProgress(folder string, title string) error {
|
||||
var total int
|
||||
filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
|
||||
if err == nil {
|
||||
if err == nil && !info.IsDir() {
|
||||
total++
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
bar := progressbar.NewOptions(total,
|
||||
progressbar.OptionSetDescription("Removing temp files"),
|
||||
progressbar.OptionSetDescription(title),
|
||||
progressbar.OptionShowCount(),
|
||||
progressbar.OptionSetWidth(30),
|
||||
progressbar.OptionSetPredictTime(true),
|
||||
)
|
||||
|
||||
err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
|
||||
_ = filepath.Walk(folder, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
return nil
|
||||
}
|
||||
if !info.IsDir() {
|
||||
if err := os.Remove(path); err != nil {
|
||||
return err
|
||||
if info.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
var rmErr error
|
||||
for i := 0; i < 10; i++ {
|
||||
rmErr = os.Remove(path)
|
||||
if rmErr == nil {
|
||||
break
|
||||
}
|
||||
bar.Add(1)
|
||||
time.Sleep(300 * time.Millisecond)
|
||||
}
|
||||
if rmErr != nil {
|
||||
fmt.Printf("⚠️ Could not remove %s: %v\n", path, rmErr)
|
||||
}
|
||||
|
||||
bar.Add(1)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err := os.RemoveAll(folder); err != nil {
|
||||
return fmt.Errorf("failed to remove folder %s: %w", folder, err)
|
||||
}
|
||||
bar.Finish()
|
||||
return nil
|
||||
}
|
||||
|
||||
func Hdiffz(oldPath, newPath, outDiff string) error {
|
||||
args := []string{"-s-64", "-SD", "-c-zstd-21-24", "-d", oldPath, newPath, outDiff}
|
||||
exe, err := memexec.New(hdiffz)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.RemoveAll(folder); err != nil {
|
||||
return err
|
||||
}
|
||||
defer exe.Close()
|
||||
cmd := exe.Command(args...)
|
||||
cmd.Stdout = nil
|
||||
cmd.Stderr = nil
|
||||
|
||||
bar.Finish()
|
||||
fmt.Println("\nTemp folder removed")
|
||||
return nil
|
||||
return cmd.Run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user