Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotswap server url #37

Merged
merged 4 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/golang-collections/collections v0.0.0-20130729185459-604e922904d3
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0
github.com/rs/zerolog v1.31.0
github.com/seternate/go-lanty v0.1.0-beta.0.20240517150350-28bf44a66830
github.com/seternate/go-lanty v0.1.0-beta.0.20240607200813-712d640af65d
golang.design/x/clipboard v0.7.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ github.com/seternate/go-lanty v0.1.0-beta.0.20240505140903-1ecd9b6d4b5d h1:Fy0j7
github.com/seternate/go-lanty v0.1.0-beta.0.20240505140903-1ecd9b6d4b5d/go.mod h1:1mxIN82SScfhtjQYzjUSFSz3ArrgOIsHBSjHl54BaMc=
github.com/seternate/go-lanty v0.1.0-beta.0.20240517150350-28bf44a66830 h1:ba5WOpu0ckeJivCbMrRHRg09vLA8pPSqI9WxbTzGGbU=
github.com/seternate/go-lanty v0.1.0-beta.0.20240517150350-28bf44a66830/go.mod h1:1mxIN82SScfhtjQYzjUSFSz3ArrgOIsHBSjHl54BaMc=
github.com/seternate/go-lanty v0.1.0-beta.0.20240607200813-712d640af65d h1:GHEWKLXg3IZs28cD9q7B8L9z1UnLCjS81uNK7g25Rnc=
github.com/seternate/go-lanty v0.1.0-beta.0.20240607200813-712d640af65d/go.mod h1:1mxIN82SScfhtjQYzjUSFSz3ArrgOIsHBSjHl54BaMc=
github.com/shurcooL/go v0.0.0-20200502201357-93f07166e636/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
Expand Down
15 changes: 3 additions & 12 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"context"
"net/url"
"sync"
"time"

Expand Down Expand Up @@ -38,17 +37,9 @@ func NewController(ctx context.Context) *Controller {
log.Fatal().Err(err).Send()
}

u, err := url.Parse(settings.ServerURL)
if len(u.Host) == 0 {
u, err = url.Parse("http://" + settings.ServerURL)
}
if err != nil {
log.Fatal().Err(err).Str("url", settings.ServerURL).Msg("failed to parse server URL")
}

client := api.NewClient(u, timeout)
client, err := api.NewClient(settings.ServerURL, timeout)
if err != nil {
log.Fatal().Err(err).Msg("failed to create API client")
log.Error().Err(err).Msg("error creating API client")
}
log.Debug().Msg("created API client")

Expand Down Expand Up @@ -86,7 +77,7 @@ func (controller *Controller) WithStatusController() *Controller {
}

func (controller *Controller) WithGameController() *Controller {
controller.Game = NewGameController(controller, 10*time.Second)
controller.Game = NewGameController(controller, 2*time.Second)
return controller
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func (controller *Download) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *Download) notifySubcriber() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/downloadcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (controller *DownloadController) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *DownloadController) notifySubcriber() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/gamecontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (controller *GameController) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *GameController) notifySubcriber() {
Expand Down
20 changes: 19 additions & 1 deletion pkg/controller/settingscontroller.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package controller

import (
"errors"
"os"
"slices"
"sync"
"time"
Expand All @@ -27,24 +29,40 @@ func NewSettingsController(parent *Controller, settings *setting.Settings) (cont
}

func (controller *SettingsController) SetServerURL(serverurl string) {
err := controller.parent.client.SetBaseURL(serverurl)
if err != nil {
controller.parent.Status.Error("Invalid server URL: "+err.Error(), 3*time.Second)
}
controller.mutex.Lock()
controller.settings.ServerURL = serverurl
controller.mutex.Unlock()
controller.notifySubcriber()
controller.Save()
}

func (controller *SettingsController) SetGameDirectory(gamedirectory string) {
info, err := os.Stat(gamedirectory)
if errors.Is(err, os.ErrNotExist) {
controller.parent.Status.Error("Invalid gamedirectory path: does not exist", 3*time.Second)
return
}
if !info.IsDir() {
controller.parent.Status.Error("Invalid gamedirectory path: not a folder", 3*time.Second)
return
}
controller.mutex.Lock()
controller.settings.GameDirectory = gamedirectory
controller.mutex.Unlock()
controller.notifySubcriber()
controller.Save()
}

func (controller *SettingsController) SetUsername(username string) {
controller.mutex.Lock()
controller.settings.Username = username
controller.mutex.Unlock()
controller.notifySubcriber()
controller.Save()
}

func (controller *SettingsController) Settings() setting.Settings {
Expand All @@ -71,7 +89,7 @@ func (controller *SettingsController) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *SettingsController) notifySubcriber() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/statuscontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (controller *StatusController) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *StatusController) notifySubcriber() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/usercontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (controller *UserController) Unsubscribe(subscriber chan struct{}) {
defer controller.mutex.Unlock()
controller.mutex.Lock()
index := slices.Index(controller.subscriber, subscriber)
slices.Delete(controller.subscriber, index, index+1)
_ = slices.Delete(controller.subscriber, index, index+1)
}

func (controller *UserController) notifySubcriber() {
Expand Down
29 changes: 4 additions & 25 deletions pkg/widget/settingbrowser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package widget

import (
"errors"
"os"
"regexp"
"time"

Expand Down Expand Up @@ -44,41 +43,23 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (

settingsbrowser.serverurl.SetText(controller.Settings.Settings().ServerURL)
settingsbrowser.serverurl.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.serverurl.Validate() == nil {
if !b {
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
controller.Settings.Save()
}
}
settingsbrowser.serverurl.OnSubmitted = func(s string) {
if settingsbrowser.serverurl.Validate() == nil {
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
controller.Settings.Save()
}
controller.Settings.SetServerURL(settingsbrowser.serverurl.Text)
}
settingsbrowser.form.AppendItem(NewFormItem("Server URL", settingsbrowser.serverurl))

settingsbrowser.gamedirectory.SetText(controller.Settings.Settings().GameDirectory)
settingsbrowser.gamedirectory.Validator = func(path string) error {
info, err := os.Stat(path)
if errors.Is(err, os.ErrNotExist) {
return errors.New("folder does not exist")
}
if !info.IsDir() {
return errors.New("path is not a folder")
}
return nil
}
settingsbrowser.gamedirectory.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.gamedirectory.Validate() == nil {
if !b {
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
controller.Settings.Save()
}
}
settingsbrowser.gamedirectory.OnSubmitted = func(s string) {
if settingsbrowser.gamedirectory.Validate() == nil {
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
controller.Settings.Save()
}
controller.Settings.SetGameDirectory(settingsbrowser.gamedirectory.Text)
}
gamedirectoryexplorer := widget.NewButtonWithIcon("", theme.FolderOpenIcon(), settingsbrowser.gamedirectoryExplorerCallback)
gamedirectory := container.NewBorder(nil, nil, nil, gamedirectoryexplorer, settingsbrowser.gamedirectory)
Expand All @@ -95,13 +76,11 @@ func NewSettingsBrowser(controller *controller.Controller, window fyne.Window) (
settingsbrowser.username.OnFocusChanged = func(b bool) {
if !b && settingsbrowser.username.Validate() == nil {
controller.Settings.SetUsername(settingsbrowser.username.Text)
controller.Settings.Save()
}
}
settingsbrowser.username.OnSubmitted = func(s string) {
if settingsbrowser.username.Validate() == nil {
controller.Settings.SetUsername(settingsbrowser.username.Text)
controller.Settings.Save()
}
}
settingsbrowser.form.AppendItem(NewFormItem("Username", settingsbrowser.username))
Expand Down
Loading