Skip to content

Commit

Permalink
Add custom remote names
Browse files Browse the repository at this point in the history
  • Loading branch information
mircearoata committed Jun 8, 2024
1 parent 9ee11fd commit 0d8672d
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
18 changes: 16 additions & 2 deletions backend/ficsitcli/metadata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package ficsitcli

import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"log/slog"
Expand All @@ -13,6 +15,7 @@ import (

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/settings"
)

func (f *ficsitCLI) initLocalInstallationsMetadata() error {
Expand Down Expand Up @@ -161,18 +164,29 @@ func (f *ficsitCLI) getRemoteServerMetadata(installation *cli.Installation) (*co

branch := common.BranchEarlyAccess // TODO: Do we have a way to detect this for remote installs?

remoteName := settings.Settings.RemoteNames[remoteKey(installation.Path)]

if remoteName == "" {
remoteName = f.GetNextRemoteLauncherName()
}

return &common.Installation{
Path: installation.Path,
Type: installType,
Location: common.LocationTypeRemote,
Branch: branch,
Version: gameVersion,
Launcher: f.getNextRemoteLauncherName(),
Launcher: remoteName,
SavedPath: path.Join(installation.BasePath(), "FactoryGame", "Saved"),
}, nil
}

func (f *ficsitCLI) getNextRemoteLauncherName() string {
func remoteKey(path string) string {
hash := sha256.Sum256([]byte(path))
return hex.EncodeToString(hash[:])
}

func (f *ficsitCLI) GetNextRemoteLauncherName() string {
existingNumbers := make(map[int]bool)
for _, install := range f.GetRemoteInstallations() {
metadata, ok := f.installationMetadata.Load(install)
Expand Down
17 changes: 16 additions & 1 deletion backend/ficsitcli/remote_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"

"github.com/satisfactorymodding/SatisfactoryModManager/backend/installfinders/common"
"github.com/satisfactorymodding/SatisfactoryModManager/backend/settings"
)

func (f *ficsitCLI) GetRemoteInstallations() []string {
Expand All @@ -22,7 +23,7 @@ func (f *ficsitCLI) GetRemoteInstallations() []string {
return paths
}

func (f *ficsitCLI) AddRemoteServer(path string) error {
func (f *ficsitCLI) AddRemoteServer(path string, name string) error {
if f.ficsitCli.Installations.GetInstallation(path) != nil {
return fmt.Errorf("installation already exists")
}
Expand All @@ -38,11 +39,20 @@ func (f *ficsitCLI) AddRemoteServer(path string) error {
l.Error("failed to save installations", slog.Any("error", err))
}

if name != "" {
settings.Settings.RemoteNames[remoteKey(installation.Path)] = name
}

meta, err := f.getRemoteServerMetadata(installation)
if err != nil {
if name != "" {
delete(settings.Settings.RemoteNames, remoteKey(installation.Path))
}
return fmt.Errorf("failed to get remote server metadata: %w", err)
}

_ = settings.SaveSettings()

f.installationMetadata.Store(path, installationMetadata{
State: InstallStateValid,
Info: meta,
Expand Down Expand Up @@ -73,6 +83,11 @@ func (f *ficsitCLI) RemoveRemoteServer(path string) error {
slog.Error("failed to save installations", slog.Any("error", err))
}
f.installationMetadata.Delete(path)

delete(settings.Settings.RemoteNames, remoteKey(path))
_ = settings.SaveSettings()

f.EmitGlobals()

return nil
}
4 changes: 4 additions & 0 deletions backend/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type settings struct {
FavoriteMods []string `json:"favoriteMods,omitempty"`
ModFilters SavedModFilters `json:"modFilters,omitempty"`

RemoteNames map[string]string `json:"remoteNames,omitempty"`

QueueAutoStart bool `json:"queueAutoStart"`
IgnoredUpdates map[string][]string `json:"ignoredUpdates,omitempty"`
UpdateCheckMode UpdateCheckMode `json:"updateCheckMode,omitempty"`
Expand Down Expand Up @@ -80,6 +82,8 @@ var Settings = &settings{
Filter: "Compatible",
},

RemoteNames: map[string]string{},

QueueAutoStart: true,
IgnoredUpdates: map[string][]string{},
UpdateCheckMode: UpdateOnLaunch,
Expand Down
22 changes: 19 additions & 3 deletions frontend/src/lib/components/modals/ServerManager.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import Tooltip from '$lib/components/Tooltip.svelte';
import { type PopupSettings, popup } from '$lib/skeletonExtensions';
import { installsMetadata, remoteServers } from '$lib/store/ficsitCLIStore';
import { AddRemoteServer, FetchRemoteServerMetadata, RemoveRemoteServer } from '$wailsjs/go/ficsitcli/ficsitCLI';
import { AddRemoteServer, FetchRemoteServerMetadata, GetNextRemoteLauncherName, RemoveRemoteServer } from '$wailsjs/go/ficsitcli/ficsitCLI';
import { ficsitcli } from '$wailsjs/go/models';
export let parent: { onClose: () => void };
Expand Down Expand Up @@ -48,6 +48,9 @@
let newServerPath = '';
let err = '';
let defaultRemoteName = '';
let remoteName = '';
let advancedMode = false;
let addInProgress = false;
Expand Down Expand Up @@ -106,7 +109,7 @@
try {
err = '';
addInProgress = true;
await AddRemoteServer(fullInstallPath);
await AddRemoteServer(fullInstallPath, remoteName);
newServerUsername = '';
newServerPassword = '';
newServerHost = '';
Expand Down Expand Up @@ -143,6 +146,13 @@
return `remote-server-warning-${install}`;
}
$: {
remoteServers;
(async () => {
defaultRemoteName = await GetNextRemoteLauncherName();
})();
}
$: installWarningPopups = $remoteServers.map((i) => [i, {
event: 'hover',
target: installWarningPopupId(i),
Expand All @@ -164,6 +174,7 @@
<tbody>
{#each $remoteServers as remoteServer}
<tr>
<td class="break-all">{$installsMetadata[remoteServer].info?.launcher}</td>
<td class="break-all">{remoteServer}</td>
<td>
{#if $installsMetadata[remoteServer]?.state === ficsitcli.InstallState.VALID}
Expand Down Expand Up @@ -304,8 +315,13 @@
/>
</div>
{/if}
<input
class="input px-4 h-full col-start-4 row-start-1"
placeholder={$t('server-manager.name-placeholder', 'Name (default: {default})', { default: defaultRemoteName })}
type="text"
bind:value={remoteName}/>
<button
class="btn h-full text-sm bg-primary-600 text-secondary-900 col-start-2 sm:col-start-4 row-start-1"
class="btn h-full text-sm bg-primary-600 text-secondary-900 col-start-2 sm:col-start-4 row-start-2"
disabled={addInProgress || !isValid}
on:click={() => addNewRemoteServer()}>
<span>
Expand Down

0 comments on commit 0d8672d

Please sign in to comment.