feat: initial commit for G-Launcher based on Firefly with Horoyoi enhancements
This commit is contained in:
commit
a99470a69f
155 files changed
+19061
No files matched your search
@@ -0,0 +1,264 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"g-launcher/pkg/constant"
|
||||
"g-launcher/pkg/hpatchz"
|
||||
"g-launcher/pkg/models"
|
||||
"g-launcher/pkg/sevenzip"
|
||||
"g-launcher/pkg/verifier"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
type DiffService struct{}
|
||||
|
||||
func (h *DiffService) CheckTypeHDiff(patchPath string) (bool, string, string) {
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "hdifffiles.txt"); err == nil && ok {
|
||||
return true, "hdifffiles.txt", ""
|
||||
}
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "hdifffiles.json"); err == nil && ok {
|
||||
return true, "hdifffiles.json", ""
|
||||
}
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "hdiffmap.json"); err == nil && ok {
|
||||
return true, "hdiffmap.json", ""
|
||||
}
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "manifest"); err == nil && ok {
|
||||
return true, "manifest", ""
|
||||
}
|
||||
|
||||
return false, "", "not found hdifffiles.txt or hdiffmap.json"
|
||||
}
|
||||
|
||||
func (h *DiffService) VersionValidate(gamePath, patchPath string) (bool, string) {
|
||||
oldBinPath := filepath.Join(gamePath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes")
|
||||
if _, err := os.Stat(oldBinPath); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
if _, err := os.Stat(patchPath); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
if _, err := os.Stat(constant.TempUrl); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(constant.TempUrl, os.ModePerm); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
okFull, errFull := sevenzip.IsFileIn7z(patchPath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes")
|
||||
okDiff, errDiff := sevenzip.IsFileIn7z(patchPath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes.hdiff")
|
||||
|
||||
if (errFull != nil && errDiff != nil) || (!okFull && !okDiff) {
|
||||
return false, "BinaryVersion file not found in patch"
|
||||
}
|
||||
|
||||
var tempBinFile string
|
||||
|
||||
if okFull {
|
||||
if err := sevenzip.ExtractAFileFromZip(patchPath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes", constant.TempUrl); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
tempBinFile = filepath.Join(constant.TempUrl, "BinaryVersion.bytes")
|
||||
} else {
|
||||
if err := sevenzip.ExtractAFileFromZip(patchPath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes.hdiff", constant.TempUrl); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
patchBinFile := filepath.Join(constant.TempUrl, "BinaryVersion.bytes.hdiff")
|
||||
sourceBinFile := oldBinPath
|
||||
tempBinFile = filepath.Join(constant.TempUrl, "BinaryVersion.bytes")
|
||||
|
||||
if err := hpatchz.ApplyPatch(sourceBinFile, patchBinFile, tempBinFile); err != nil {
|
||||
os.Remove(patchBinFile)
|
||||
return false, err.Error()
|
||||
}
|
||||
os.Remove(patchBinFile)
|
||||
}
|
||||
|
||||
okFullPkg, err1 := sevenzip.IsFileIn7z(patchPath, "pkg_version")
|
||||
okDiffPkg, err2 := sevenzip.IsFileIn7z(patchPath, "pkg_version.hdiff")
|
||||
if err1 != nil && err2 != nil {
|
||||
return false, err1.Error()
|
||||
}
|
||||
if okFullPkg {
|
||||
if err := sevenzip.ExtractAFileFromZip(patchPath, "pkg_version", constant.TempUrl); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
}
|
||||
if okDiffPkg {
|
||||
if err := sevenzip.ExtractAFileFromZip(patchPath, "pkg_version.hdiff", constant.TempUrl); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
patchPkgFile := filepath.Join(constant.TempUrl, "pkg_version.hdiff")
|
||||
sourcePkgFile := filepath.Join(gamePath, "pkg_version")
|
||||
tempPkgFile := filepath.Join(constant.TempUrl, "pkg_version")
|
||||
if err := hpatchz.ApplyPatch(sourcePkgFile, patchPkgFile, tempPkgFile); err != nil {
|
||||
os.Remove(patchPkgFile)
|
||||
os.Remove(tempPkgFile)
|
||||
return false, err.Error()
|
||||
}
|
||||
os.Remove(patchPkgFile)
|
||||
}
|
||||
|
||||
tempPkgFile := filepath.Join(constant.TempUrl, "pkg_version")
|
||||
pkgDataList, err := models.LoadPkgVersion(tempPkgFile)
|
||||
if err != nil {
|
||||
os.Remove(tempPkgFile)
|
||||
return false, err.Error()
|
||||
}
|
||||
os.Remove(tempPkgFile)
|
||||
|
||||
// MD5 check BinaryVersion
|
||||
flags := false
|
||||
for _, pkgData := range pkgDataList {
|
||||
if filepath.ToSlash(pkgData.RemoteFile) == "StarRail_Data/StreamingAssets/BinaryVersion.bytes" {
|
||||
flags = true
|
||||
md5, err := verifier.FileMD5(tempBinFile)
|
||||
if err != nil {
|
||||
os.Remove(tempBinFile)
|
||||
return false, err.Error()
|
||||
}
|
||||
if md5 != pkgData.MD5 {
|
||||
os.Remove(tempBinFile)
|
||||
return false, fmt.Sprintf("md5 mismatch for %s: expected %s, got %s",
|
||||
tempBinFile, pkgData.MD5, md5)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !flags {
|
||||
os.Remove(tempBinFile)
|
||||
return false, "BinaryVersion file not found in patch"
|
||||
}
|
||||
_, err = models.ParseBinaryVersion(tempBinFile)
|
||||
if err != nil {
|
||||
os.Remove(tempBinFile)
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
os.Remove(tempBinFile)
|
||||
return true, "validated"
|
||||
}
|
||||
|
||||
func (h *DiffService) HDiffPatchData(gamePath string) (bool, string) {
|
||||
hdiffMapPath := filepath.Join(gamePath, "hdiffmap.json")
|
||||
hdiffFilesPath := filepath.Join(gamePath, "hdifffiles.txt")
|
||||
hdifffilesJsonPath := filepath.Join(gamePath, "hdifffiles.json")
|
||||
|
||||
var jsonData struct {
|
||||
DiffMap []*models.HDiffData `json:"diff_map"`
|
||||
}
|
||||
|
||||
if _, err := os.Stat(hdiffMapPath); err == nil {
|
||||
data, err := os.ReadFile(hdiffMapPath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
var jsonDataDiffMap struct {
|
||||
DiffMap []*models.DiffMapType `json:"diff_map"`
|
||||
}
|
||||
if err := json.Unmarshal(data, &jsonDataDiffMap); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
for _, entry := range jsonDataDiffMap.DiffMap {
|
||||
jsonData.DiffMap = append(jsonData.DiffMap, entry.ToHDiffData())
|
||||
}
|
||||
} else if _, err := os.Stat(hdifffilesJsonPath); err == nil {
|
||||
data, err := os.ReadFile(hdifffilesJsonPath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
var hdiffJson []*models.HDiffData
|
||||
if err := json.Unmarshal(data, &hdiffJson); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
jsonData.DiffMap = append(jsonData.DiffMap, hdiffJson...)
|
||||
} else if _, err := os.Stat(hdiffFilesPath); err == nil {
|
||||
files, err := models.LoadHDiffFiles(hdiffFilesPath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
for _, entry := range files {
|
||||
jsonData.DiffMap = append(jsonData.DiffMap, entry.ToHDiffData())
|
||||
}
|
||||
} else {
|
||||
return false, "no hdiff entries map exist"
|
||||
}
|
||||
|
||||
application.Get().Event.Emit("diff:stage", map[string]string{"stage": "Patching HDiff"})
|
||||
|
||||
for i, entry := range jsonData.DiffMap {
|
||||
application.Get().Event.Emit(
|
||||
"diff:progress", map[string]int{
|
||||
"progress": i,
|
||||
"maxProgress": len(jsonData.DiffMap),
|
||||
})
|
||||
|
||||
sourceFile := filepath.Join(gamePath, entry.SourceFileName)
|
||||
patchFile := filepath.Join(gamePath, entry.PatchFileName)
|
||||
targetFile := filepath.Join(gamePath, entry.TargetFileName)
|
||||
|
||||
if _, err := os.Stat(patchFile); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
if entry.SourceFileName == "" {
|
||||
hpatchz.ApplyPatchEmpty(patchFile, targetFile)
|
||||
os.Remove(patchFile)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := os.Stat(sourceFile); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
hpatchz.ApplyPatch(sourceFile, patchFile, targetFile)
|
||||
if entry.SourceFileName != entry.TargetFileName {
|
||||
os.Remove(sourceFile)
|
||||
}
|
||||
os.Remove(patchFile)
|
||||
}
|
||||
|
||||
os.Remove(filepath.Join(gamePath, "hdiffmap.json"))
|
||||
os.Remove(filepath.Join(gamePath, "hdifffiles.txt"))
|
||||
os.Remove(filepath.Join(gamePath, "hdifffiles.json"))
|
||||
return true, "patching completed"
|
||||
}
|
||||
|
||||
func (h *DiffService) DeleteFiles(gamePath string) (bool, string) {
|
||||
var deleteFiles []string
|
||||
|
||||
file, err := os.Open(filepath.Join(gamePath, "deletefiles.txt"))
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := strings.TrimSpace(scanner.Text())
|
||||
if line != "" {
|
||||
deleteFiles = append(deleteFiles, line)
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
file.Close()
|
||||
return false, "no delete files exist"
|
||||
}
|
||||
|
||||
file.Close()
|
||||
|
||||
for i, file := range deleteFiles {
|
||||
os.Remove(filepath.Join(gamePath, file))
|
||||
application.Get().Event.Emit("diff:progress", map[string]int{"progress": i, "maxProgress": len(deleteFiles)})
|
||||
}
|
||||
os.Remove(filepath.Join(gamePath, "deletefiles.txt"))
|
||||
return true, ""
|
||||
}
|
||||
@@ -0,0 +1,172 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"g-launcher/pkg/firefly"
|
||||
"g-launcher/pkg/firefly/pb"
|
||||
"g-launcher/pkg/hpatchz"
|
||||
"g-launcher/pkg/models"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
func (h *DiffService) LDiffPatchData(gamePath string) (bool, string) {
|
||||
entries, err := os.ReadDir(gamePath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
ldiffPath := filepath.Join(gamePath, "ldiff")
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
if !entry.Type().IsRegular() {
|
||||
continue
|
||||
}
|
||||
|
||||
name := entry.Name()
|
||||
if strings.HasPrefix(name, "manifest") {
|
||||
manifestName := entry.Name()
|
||||
manifestPath := filepath.Join(gamePath, manifestName)
|
||||
|
||||
manifest, err := firefly.LoadManifestProto(manifestPath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
ldiffEntries, err := os.ReadDir(ldiffPath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
application.Get().Event.Emit("diff:stage", map[string]string{"stage": "Processing LDiff"})
|
||||
for i, ldiffEntry := range ldiffEntries {
|
||||
assetName := ldiffEntry.Name()
|
||||
var matchingAssets []struct {
|
||||
AssetName string
|
||||
AssetSize int64
|
||||
Asset *pb.AssetManifest
|
||||
}
|
||||
|
||||
application.Get().Event.Emit(
|
||||
"diff:progress", map[string]int{
|
||||
"progress": i,
|
||||
"maxProgress": len(ldiffEntries),
|
||||
})
|
||||
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
|
||||
for _, assetGroup := range manifest.Assets {
|
||||
assetGroup := assetGroup
|
||||
wg.Go(func() {
|
||||
if data := assetGroup.AssetData; data != nil {
|
||||
for _, asset := range data.Assets {
|
||||
if asset.ChunkFileName == assetName {
|
||||
mu.Lock()
|
||||
matchingAssets = append(matchingAssets, struct {
|
||||
AssetName string
|
||||
AssetSize int64
|
||||
Asset *pb.AssetManifest
|
||||
}{assetGroup.AssetName, assetGroup.AssetSize, asset})
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
for _, ma := range matchingAssets {
|
||||
err := firefly.LDiffFile(ma.Asset, ma.AssetName, ma.AssetSize, ldiffPath, gamePath)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
diffMapNames := make([]string, len(ldiffEntries))
|
||||
for i, e := range ldiffEntries {
|
||||
diffMapNames[i] = e.Name()
|
||||
}
|
||||
|
||||
diffMapList, err := MakeDiffMap(manifest, diffMapNames)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
application.Get().Event.Emit("diff:stage", map[string]string{"stage": "Patching HDiff"})
|
||||
for i, entry := range diffMapList {
|
||||
application.Get().Event.Emit(
|
||||
"diff:progress", map[string]int{
|
||||
"progress": i,
|
||||
"maxProgress": len(diffMapList),
|
||||
})
|
||||
sourceFile := filepath.Join(gamePath, entry.SourceFileName)
|
||||
patchFile := filepath.Join(gamePath, entry.PatchFileName)
|
||||
targetFile := filepath.Join(gamePath, entry.TargetFileName)
|
||||
|
||||
if _, err := os.Stat(patchFile); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
if entry.SourceFileName == "" {
|
||||
hpatchz.ApplyPatchEmpty(patchFile, targetFile)
|
||||
os.Remove(patchFile)
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := os.Stat(sourceFile); os.IsNotExist(err) {
|
||||
continue
|
||||
}
|
||||
|
||||
hpatchz.ApplyPatch(sourceFile, patchFile, targetFile)
|
||||
if entry.SourceFileName != entry.TargetFileName {
|
||||
os.Remove(sourceFile)
|
||||
}
|
||||
os.Remove(patchFile)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
os.RemoveAll(ldiffPath)
|
||||
return true, "patching completed"
|
||||
}
|
||||
|
||||
func MakeDiffMap(manifest *pb.ManifestProto, chunkNames []string) ([]*models.HDiffData, error) {
|
||||
var hdiffFiles []*models.HDiffData
|
||||
|
||||
for _, asset := range manifest.Assets {
|
||||
assetName := asset.AssetName
|
||||
assetSize := asset.AssetSize
|
||||
|
||||
if asset.AssetData != nil {
|
||||
for _, chunk := range asset.AssetData.Assets {
|
||||
matched := false
|
||||
for _, name := range chunkNames {
|
||||
if name == chunk.ChunkFileName {
|
||||
matched = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !matched {
|
||||
continue
|
||||
}
|
||||
|
||||
if chunk.OriginalFileSize != 0 || chunk.HdiffFileSize != assetSize {
|
||||
hdiffFiles = append(hdiffFiles, &models.HDiffData{
|
||||
SourceFileName: chunk.OriginalFilePath,
|
||||
TargetFileName: assetName,
|
||||
PatchFileName: fmt.Sprintf("%s.hdiff", assetName),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return hdiffFiles, nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"g-launcher/pkg/constant"
|
||||
"g-launcher/pkg/sevenzip"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
func (h *DiffService) DataExtract(gamePath, patchPath string) (bool, string) {
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
if _, err := os.Stat(gamePath); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
if _, err := os.Stat(patchPath); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
if _, err := os.Stat(constant.TempUrl); os.IsNotExist(err) {
|
||||
if err := os.MkdirAll(constant.TempUrl, os.ModePerm); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
}
|
||||
|
||||
if err := sevenzip.ExtractAllFilesFromZip(patchPath, constant.TempUrl); err != nil {
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
return false, err.Error()
|
||||
}
|
||||
return true, "extract completed"
|
||||
}
|
||||
|
||||
func (h *DiffService) CutData(gamePath string) (bool, string) {
|
||||
if _, err := os.Stat(constant.TempUrl); os.IsNotExist(err) {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
err := filepath.Walk(constant.TempUrl, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
relPath, err := filepath.Rel(constant.TempUrl, path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
destPath := filepath.Join(gamePath, relPath)
|
||||
application.Get().Event.Emit("diff:message", map[string]string{"message": destPath})
|
||||
if info.IsDir() {
|
||||
return os.MkdirAll(destPath, os.ModePerm)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(destPath), os.ModePerm); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := os.Rename(path, destPath); err != nil {
|
||||
srcFile, err := os.Open(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer srcFile.Close()
|
||||
|
||||
dstFile, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dstFile.Close()
|
||||
|
||||
if _, err := io.Copy(dstFile, srcFile); err != nil {
|
||||
return err
|
||||
}
|
||||
os.Remove(path)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
return false, err.Error()
|
||||
}
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
return true, "cut completed"
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
)
|
||||
|
||||
const ziftExecutablePath = "bin/zift.exe"
|
||||
|
||||
var ziftPercentPattern = regexp.MustCompile(`(?i)(\d+(?:\.\d+)?)\s*%`)
|
||||
var ziftANSIPattern = regexp.MustCompile(`\x1b\[[0-?]*[ -/]*[@-~]`)
|
||||
|
||||
func (h *DiffService) ApplyDiffZiff(gamePath, patchPath string) (bool, string) {
|
||||
launcherPath, err := os.Executable()
|
||||
if err != nil {
|
||||
return false, fmt.Sprintf("locate launcher directory: %v", err)
|
||||
}
|
||||
launcherDir := filepath.Dir(launcherPath)
|
||||
ziftPath := filepath.Join(launcherDir, filepath.FromSlash(ziftExecutablePath))
|
||||
if _, err := os.Stat(ziftPath); err != nil {
|
||||
if cwdPath, cwdErr := filepath.Abs(filepath.FromSlash(ziftExecutablePath)); cwdErr == nil {
|
||||
if _, statErr := os.Stat(cwdPath); statErr == nil {
|
||||
ziftPath = cwdPath
|
||||
}
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(ziftPath); err != nil {
|
||||
return false, fmt.Sprintf("Zift executable not found at %s: %v", ziftPath, err)
|
||||
}
|
||||
if info, err := os.Stat(patchPath); err != nil || !info.Mode().IsRegular() {
|
||||
if err != nil {
|
||||
return false, fmt.Sprintf("patch file is unavailable: %v", err)
|
||||
}
|
||||
return false, "selected patch is not a file"
|
||||
}
|
||||
cmd := exec.Command(ziftPath, "-a", "-y", patchPath, gamePath)
|
||||
cmd.Dir = launcherDir
|
||||
outputReader, outputWriter := io.Pipe()
|
||||
cmd.Stdout = outputWriter
|
||||
cmd.Stderr = outputWriter
|
||||
application.Get().Event.Emit("diff:ziff-progress", map[string]interface{}{
|
||||
"message": "Starting Zift…",
|
||||
"percent": -1.0,
|
||||
})
|
||||
outputDone := make(chan string, 1)
|
||||
go func() { outputDone <- collectZiftOutput(outputReader) }()
|
||||
if err := cmd.Start(); err != nil {
|
||||
_ = outputWriter.Close()
|
||||
message := <-outputDone
|
||||
if message != "" {
|
||||
return false, fmt.Sprintf("run Zift: %v\n%s", err, message)
|
||||
}
|
||||
return false, fmt.Sprintf("run Zift: %v", err)
|
||||
}
|
||||
err = cmd.Wait()
|
||||
_ = outputWriter.Close()
|
||||
message := strings.TrimSpace(<-outputDone)
|
||||
if err != nil {
|
||||
if message == "" {
|
||||
return false, fmt.Sprintf("run Zift: %v", err)
|
||||
}
|
||||
return false, fmt.Sprintf("Zift failed: %v\n%s", err, message)
|
||||
}
|
||||
if message == "" {
|
||||
message = "Ziff patch applied successfully"
|
||||
}
|
||||
return true, message
|
||||
}
|
||||
|
||||
func collectZiftOutput(reader io.Reader) string {
|
||||
buffered := bufio.NewReader(reader)
|
||||
var output strings.Builder
|
||||
var line strings.Builder
|
||||
emitLine := func() {
|
||||
text := strings.TrimSpace(ziftANSIPattern.ReplaceAllString(line.String(), ""))
|
||||
line.Reset()
|
||||
if text == "" {
|
||||
return
|
||||
}
|
||||
output.WriteString(text)
|
||||
output.WriteByte('\n')
|
||||
progress := -1.0
|
||||
if match := ziftPercentPattern.FindStringSubmatch(text); len(match) > 1 {
|
||||
if value, err := strconv.ParseFloat(match[1], 64); err == nil && value >= 0 && value <= 100 {
|
||||
progress = value
|
||||
}
|
||||
}
|
||||
application.Get().Event.Emit("diff:ziff-progress", map[string]interface{}{
|
||||
"message": text,
|
||||
"percent": progress,
|
||||
})
|
||||
}
|
||||
|
||||
for {
|
||||
value, err := buffered.ReadByte()
|
||||
if err != nil {
|
||||
if line.Len() > 0 {
|
||||
emitLine()
|
||||
}
|
||||
break
|
||||
}
|
||||
if value == '\r' || value == '\n' {
|
||||
emitLine()
|
||||
continue
|
||||
}
|
||||
line.WriteByte(value)
|
||||
}
|
||||
return output.String()
|
||||
}
|
||||
Reference in new issue
Block a user