Implement updatings

This commit is contained in:
2025-01-11 19:10:22 +01:00
parent e36f46bd8c
commit c691125b10

117
main.go
View File

@@ -1,6 +1,8 @@
package main
import (
"archive/zip"
"bytes"
"embed"
"fmt"
"io"
@@ -10,6 +12,7 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
@@ -69,6 +72,95 @@ func (a *Addon) GetRemoteRelease() (body []byte, err error) {
}
return io.ReadAll(response.Body)
}
func (a *Addon) HasBreakingChanges(lhsToc, rhsToc string) bool {
lhsLines := strings.Split(lhsToc, "\n")
rhsLines := strings.Split(rhsToc, "\n")
for i, lhsLine := range lhsLines {
rhsLine := rhsLines[i]
lhsLine = strings.TrimSpace(lhsLine)
rhsLine = strings.TrimSpace(rhsLine)
// We don't care about ## lines
if strings.HasPrefix(lhsLine, "#") && strings.HasPrefix(rhsLine, "#") {
continue
}
// Nor do we care about empty lines
if lhsLine == "" && rhsLine == "" {
continue
}
log.Printf("%s %s", lhsLine, rhsLine)
if i >= len(rhsLines) || rhsLine != lhsLine {
return true
}
}
return false
}
func UpdateFile(localPath string, data []byte) (err error) {
err = os.MkdirAll(filepath.Dir(localPath), 0755)
if err != nil {
return fmt.Errorf("error creating directory: %w", err)
}
fileHandle, err := os.OpenFile(localPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
defer fileHandle.Close()
_, err = fileHandle.Write(data)
if err != nil {
return fmt.Errorf("error writing file: %w", err)
}
return nil
}
func (a *Addon) Update(body []byte) (err error) {
log.Printf("Updating %s", a.Name)
zipReader, err := zip.NewReader(bytes.NewReader(body), int64(len(body)))
if err != nil {
return fmt.Errorf("error creating zip reader: %w", err)
}
log.Printf("Found %d files", len(zipReader.File))
hasBreakingChanges := false
for _, file := range zipReader.File {
if file.FileInfo().IsDir() {
continue
}
log.Printf("Found file %s", file.Name)
fileHandle, err := file.Open()
if err != nil {
return fmt.Errorf("error opening file: %w", err)
}
fileData, err := io.ReadAll(fileHandle)
if err != nil {
return fmt.Errorf("error reading file: %w", err)
}
if strings.HasSuffix(file.Name, ".toc") {
localToc, err := os.ReadFile(a.GetLocalTocPath())
if err != nil {
if os.IsNotExist(err) {
localToc = []byte{}
} else {
return fmt.Errorf("error reading local toc: %w", err)
}
}
if a.HasBreakingChanges(string(localToc), string(fileData)) {
log.Printf("Has breaking changes")
hasBreakingChanges = true
}
}
localPath := filepath.Join(gamePath, "Interface", "AddOns", file.Name)
log.Printf("Updating file %s", localPath)
err = UpdateFile(localPath, fileData)
if err != nil {
return fmt.Errorf("error updating file: %w", err)
}
}
if hasBreakingChanges {
Warning.Printf("Has breaking changes")
}
return nil
}
func (a *Addon) GetLocalTocPath() string {
return filepath.Join(gamePath, "Interface", "AddOns", a.Name, a.Name+".toc")
}
@@ -115,20 +207,25 @@ func (a *Addon) IsUpToDate() bool {
func main() {
gamePath = filepath.Join("C:\\", "Games", "WoWRuski")
for _, addon := range addons {
log.Printf("%#v", addon.IsUpToDate())
log.Printf("%#v", addon.GetRemoteReleaseURL())
body, err := addon.GetRemoteRelease()
if err != nil {
log.Printf("error getting remote release: %s", err)
continue
}
log.Printf("%#v", string(body))
// for _, addon := range addons {
// log.Printf("%#v", addon.IsUpToDate())
// log.Printf("%#v", addon.GetRemoteReleaseURL())
// }
file, err := os.ReadFile("Heimdall.zip")
if err != nil {
log.Printf("error reading file: %s", err)
return
}
err = addons[0].Update(file)
if err != nil {
log.Printf("error updating addon: %s", err)
return
}
log.Printf("Addon updated")
return
app := NewApp()
err := wails.Run(&options.App{
err = wails.Run(&options.App{
Title: "wails-template",
Width: 1024,
Height: 768,