UPDATE: Add ldiff
This commit is contained in:
167
internal/diff-service/hdiffz.go
Normal file
167
internal/diff-service/hdiffz.go
Normal file
@@ -0,0 +1,167 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/json"
|
||||
"firefly-launcher/pkg/constant"
|
||||
"firefly-launcher/pkg/hpatchz"
|
||||
"firefly-launcher/pkg/models"
|
||||
"firefly-launcher/pkg/sevenzip"
|
||||
"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, "manifest"); err == nil && ok {
|
||||
return true, "manifest", ""
|
||||
}
|
||||
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "hdifffiles.txt"); err == nil && ok {
|
||||
return true, "hdifffiles.txt", ""
|
||||
}
|
||||
if ok, err := sevenzip.IsFileIn7z(patchPath, "hdiffmap.json"); err == nil && ok {
|
||||
return true, "hdiffmap.json", ""
|
||||
}
|
||||
return false, "", "not found hdifffiles.txt or hdiffmap.json"
|
||||
}
|
||||
|
||||
func (h *DiffService) VersionValidate(gamePath, patchPath string) (bool, string) {
|
||||
oldVersionData, err := models.ParseBinaryVersion(filepath.Join(gamePath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes"))
|
||||
if 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.ExtractAFileFromZip(patchPath, "StarRail_Data\\StreamingAssets\\BinaryVersion.bytes", constant.TempUrl); err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
binPath := filepath.Join(constant.TempUrl, "BinaryVersion.bytes")
|
||||
newVersionData, err := models.ParseBinaryVersion(binPath)
|
||||
if err != nil {
|
||||
return false, err.Error()
|
||||
}
|
||||
defer os.Remove(binPath)
|
||||
|
||||
v := newVersionData.Subtract(*oldVersionData)
|
||||
if v != 0 && v != 1 {
|
||||
return false, fmt.Sprintf("the diff version %s not valid with game version %s", newVersionData, oldVersionData)
|
||||
}
|
||||
|
||||
return true, "validated"
|
||||
}
|
||||
|
||||
func (h *DiffService) HDiffPatchData(gamePath string) (bool, string) {
|
||||
hdiffMapPath := filepath.Join(gamePath, "hdiffmap.json")
|
||||
hdiffFilesPath := filepath.Join(gamePath, "hdifffiles.txt")
|
||||
|
||||
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(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"))
|
||||
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 {
|
||||
return false, "no delete files exist"
|
||||
}
|
||||
|
||||
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, ""
|
||||
}
|
||||
172
internal/diff-service/ldifff.go
Normal file
172
internal/diff-service/ldifff.go
Normal file
@@ -0,0 +1,172 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"firefly-launcher/pkg/firefly"
|
||||
"firefly-launcher/pkg/firefly/pb"
|
||||
"firefly-launcher/pkg/hpatchz"
|
||||
"firefly-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
|
||||
}
|
||||
100
internal/diff-service/utils.go
Normal file
100
internal/diff-service/utils.go
Normal file
@@ -0,0 +1,100 @@
|
||||
package diffService
|
||||
|
||||
import (
|
||||
"firefly-launcher/pkg/constant"
|
||||
"firefly-launcher/pkg/verifier"
|
||||
"firefly-launcher/pkg/sevenzip"
|
||||
"github.com/wailsapp/wails/v3/pkg/application"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"io"
|
||||
)
|
||||
|
||||
func (h *DiffService) DataExtract(gamePath, patchPath string, isSkipVerify bool) (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()
|
||||
}
|
||||
|
||||
if !isSkipVerify {
|
||||
validator, err := verifier.NewVerifier(gamePath, constant.TempUrl)
|
||||
if err != nil {
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
return false, err.Error()
|
||||
}
|
||||
|
||||
if err := validator.VerifyAll(); err != nil {
|
||||
os.RemoveAll(constant.TempUrl)
|
||||
return false, err.Error()
|
||||
}
|
||||
}
|
||||
return true, "validated"
|
||||
}
|
||||
|
||||
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"
|
||||
}
|
||||
Reference in New Issue
Block a user