Skip to content
This repository has been archived by the owner on Feb 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #6 from DaniruKun/ux-improvements
Browse files Browse the repository at this point in the history
Add custom target save file selection
  • Loading branch information
DaniruKun authored Nov 12, 2022
2 parents 488c051 + 5ab6570 commit 62cc746
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 45 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 9 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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' .
Expand All @@ -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
Expand Down
104 changes: 65 additions & 39 deletions gui.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"io"
"net/url"

Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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
}
24 changes: 24 additions & 0 deletions holocure/holocure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 62cc746

Please sign in to comment.