From 1dce132e2ef6856ccb187f1d3e4ca2d91fc4ee53 Mon Sep 17 00:00:00 2001 From: Daniils Petrovs Date: Thu, 10 Nov 2022 00:47:03 +0100 Subject: [PATCH 1/2] Add custom target save file selection --- FyneApp.toml | 4 +- Makefile | 12 +++-- gui.go | 104 +++++++++++++++++++++++++++---------------- holocure/holocure.go | 24 ++++++++++ main.go | 2 +- 5 files changed, 101 insertions(+), 45 deletions(-) diff --git a/FyneApp.toml b/FyneApp.toml index 250684e..26f3e82 100644 --- a/FyneApp.toml +++ b/FyneApp.toml @@ -4,5 +4,5 @@ Website = "https://danpetrov.xyz/tasukeru" Icon = "Tasukeru.png" Name = "Tasukeru" ID = "com.tasukeru.app" - Version = "1.1.0" - Build = 17 + Version = "1.1.1" + Build = 24 diff --git a/Makefile b/Makefile index b626290..4662d05 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,8 @@ BINARY_NAME := tasukeru .PHONY: all build compile-cli compile-windows compile-mac clean run format +.DEFAULT: build + build: @echo "Building tasukeru build for the current platform" go build -o bin/${BINARY_NAME} -ldflags '-s -w' . @@ -15,11 +17,15 @@ compile-windows: FyneApp.toml @echo "Building native Windows cross compiled build" fyne-cross windows -ldflags '-s -w' -compile-mac: FyneApp.toml +compile-mac: @echo "Building native Mac app" - fyne-cross darwin -ldflags '-s -w' + fyne package -os darwin -icon Tasukeru.png --release + +compile-linux: FyneApp.toml + @echo "Building native Linux app" + fyne-cross linux -ldflags '-s -w' -all: compile-cli compile-windows compile-mac +all: compile-cli compile-windows compile-mac compile-linux clean: go clean diff --git a/gui.go b/gui.go index 187d7b4..da56af9 100644 --- a/gui.go +++ b/gui.go @@ -1,6 +1,7 @@ package main import ( + "errors" "io" "net/url" @@ -14,30 +15,38 @@ import ( "github.com/DaniruKun/tasukeru/holocure" ) +var ( + srcDec, targetDec []byte + sourceFilePath, targetFilePath string + fileFilter = storage.NewExtensionFileFilter([]string{".dat"}) + fileBrowserSize = fyne.NewSize(1000, 800) +) + // Starts a blocking event loop of the GUI. func RunGUI() { a := app.New() w := a.NewWindow("Tasukeru") w.Resize(fyne.NewSize(800, 600)) - var srcDec []byte - - targetSaveFilePath := holocure.SaveFilePath() - confirmButton := widget.NewButtonWithIcon("Import", theme.ConfirmIcon(), func() { - targetDec := mergeFiles(srcDec, targetSaveFilePath) - err := holocure.WriteSaveFile(targetSaveFilePath, targetDec) + if srcDec == nil || targetDec == nil { + dialog.ShowError(errors.New("missing data"), w) + return + } + targetDec = holocure.MergeSaveDataDecoded(srcDec, targetDec) + err := holocure.WriteSaveFile(targetFilePath, targetDec) if err != nil { dialog.ShowError(err, w) return } - dialog.NewInformation("Result", "Save imported successfully!", w).Show() + dialog.ShowInformation("Result", "Save imported successfully!", w) }) + confirmButton.Hide() - openFileButtonLabel := widget.NewLabel("Select save file to import") - openFileButtonLabel.Alignment = fyne.TextAlignCenter - openFileButton := widget.NewButton("Open file", func() { + openSourceFileButtonLabel := widget.NewLabel("Select save file to import") + openSourceFileButtonLabel.Alignment = fyne.TextAlignCenter + openSourceFileButton := widget.NewButtonWithIcon("Browse source file", theme.FileIcon(), func() { fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) { if err != nil { dialog.ShowError(err, w) @@ -47,20 +56,59 @@ func RunGUI() { return } + u, _ := url.ParseRequestURI(reader.URI().String()) + sourceFilePath = u.Path + srcDec, err = holocure.Decode(loadFile(reader)) if err != nil { dialog.ShowError(err, w) return } + openSourceFileButtonLabel.SetText("Selected source file: " + sourceFilePath) + confirmButton.Show() }, w) - fd.SetFilter(storage.NewExtensionFileFilter([]string{".dat"})) - fd.Resize(fyne.NewSize(1000, 800)) + fd.SetFilter(fileFilter) + fd.Resize(fileBrowserSize) + fd.Show() + }) + + openTargetFileButtonLabel := widget.NewLabel("Select target file to merge into") + openTargetFileButtonLabel.Alignment = fyne.TextAlignCenter + openTargetFileButton := widget.NewButtonWithIcon("Browse target file", theme.FileIcon(), func() { + fd := dialog.NewFileOpen(func(reader fyne.URIReadCloser, err error) { + if err != nil { + dialog.ShowError(err, w) + return + } + if reader == nil { + return + } + + u, _ := url.ParseRequestURI(reader.URI().String()) + targetFilePath = u.Path + + targetDec, err = holocure.Decode(loadFile(reader)) + if err != nil { + dialog.ShowError(err, w) + return + } + + openTargetFileButtonLabel.SetText("Selected target file: " + targetFilePath) + }, w) + + fd.SetFilter(fileFilter) + fd.Resize(fileBrowserSize) fd.Show() }) - openFileButton.Icon = theme.FileIcon() + + if holocure.DefaultSaveFileExists() { + targetFilePath = holocure.SaveFilePath() + targetDec, _ = holocure.DecodeSaveFile(targetFilePath) + openTargetFileButtonLabel.SetText("Override default save file: " + targetFilePath) + } aboutUrl, err := url.Parse(HomePage) check(err) @@ -69,8 +117,10 @@ func RunGUI() { versionLabel := widget.NewLabelWithStyle("Version "+Version, fyne.TextAlignTrailing, fyne.TextStyle{Monospace: true}) box := container.NewVBox( - openFileButtonLabel, - openFileButton, + openSourceFileButtonLabel, + openSourceFileButton, + openTargetFileButtonLabel, + openTargetFileButton, confirmButton, aboutHyperLink, versionLabel, @@ -88,27 +138,3 @@ func loadFile(f fyne.URIReadCloser) []byte { } return data } - -func mergeFiles(srcDec []byte, targetSaveFilePath string) []byte { - var start, end int - - start, end = holocure.FindSaveBlockStartEnd(&srcDec) - srcSaveBlock := srcDec[start : end+1] - - targetDec, err := holocure.DecodeSaveFile(targetSaveFilePath) - check(err) - - start, _ = holocure.FindSaveBlockStartEnd(&targetDec) - - for i, char := range srcSaveBlock { - targetOffset := start + i - - if targetOffset >= len(targetDec) { - targetDec = append(targetDec, char) - } else { - targetDec[targetOffset] = char - } - } - - return targetDec -} diff --git a/holocure/holocure.go b/holocure/holocure.go index 8f225ee..02a0662 100644 --- a/holocure/holocure.go +++ b/holocure/holocure.go @@ -16,6 +16,12 @@ func SaveFilePath() string { return filepath.Join(dir, "HoloCure", defaultSaveFileName) } +// Returns truthy if the default save file does not exist yet +func DefaultSaveFileExists() bool { + _, err := os.Stat(SaveFilePath()) + return err == nil +} + // It seems that the start offset will always be the same // across machines, but safer to find save block dynamically func FindSaveBlockStartEnd(data *[]byte) (start, end int) { @@ -75,6 +81,24 @@ func MergeSaves(sourceSaveFilePath, targetSaveFilePath string) []byte { return targetDec } +func MergeSaveDataDecoded(srcData, targetData []byte) []byte { + start, end := FindSaveBlockStartEnd(&srcData) + srcSaveBlock := srcData[start : end+1] + + start, _ = FindSaveBlockStartEnd(&targetData) + + for i, char := range srcSaveBlock { + targetOffset := start + i + + if targetOffset >= len(targetData) { + targetData = append(targetData, char) + } else { + targetData[targetOffset] = char + } + } + return targetData +} + func check(e error) { if e != nil { panic(e) diff --git a/main.go b/main.go index 0dcae0c..ed6ccf6 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,7 @@ import ( "github.com/Songmu/prompter" ) -const Version = "1.1.0" +const Version = "1.1.1" const HomePage = "https://danpetrov.xyz/tasukeru" func check(e error) { From 5ab6570f63ce0e60b04180a882272b09235aed51 Mon Sep 17 00:00:00 2001 From: Daniils Petrovs Date: Sat, 12 Nov 2022 17:13:59 +0100 Subject: [PATCH 2/2] Update CI --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5fdf08c..e4079c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,6 +13,10 @@ jobs: steps: - uses: actions/checkout@v3 + - name: Get dependencies + run: sudo apt-get update && sudo apt-get install gcc libgl1-mesa-dev libegl1-mesa-dev libgles2-mesa-dev libx11-dev xorg-dev libwayland-dev libxkbcommon-dev bc + if: ${{ runner.os == 'Linux' }} + - name: Set up Go uses: actions/setup-go@v3 with: