- ServerDashbord Hinzugefügt
-Ftp Seite überarbeitet mit path auswahl und zurück plus weiter button. Fehlt noch logik zum speichern vom path in global variable (: um sie zu laden evtl local zwischen speichern damit nicht jedes mal eine anfrage zum server kommt. -Server Dashboard beispiel mod config hinzugefügt um ftp string zu lesen und schreiben. Plugin unterstützung fehlt hier noch plus plugin muss erstmal geschrieben werden das er es kann :P
This commit is contained in:
152
Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs
Normal file
152
Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs
Normal file
@@ -0,0 +1,152 @@
|
||||
using Avalonia.Controls;
|
||||
using ReactiveUI;
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reactive;
|
||||
using System.Reactive.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Valheim_Dashboard.Models;
|
||||
|
||||
namespace Valheim_Dashboard.ViewModels;
|
||||
|
||||
public class ServerDashboardViewModel : ViewModelBase
|
||||
{
|
||||
// BepInEx Status
|
||||
private string _bepInExStatus;
|
||||
public string BepInExStatus
|
||||
{
|
||||
get => _bepInExStatus;
|
||||
set => this.RaiseAndSetIfChanged(ref _bepInExStatus, value);
|
||||
}
|
||||
|
||||
private bool _showInstallButton;
|
||||
public bool ShowInstallButton
|
||||
{
|
||||
get => _showInstallButton;
|
||||
set => this.RaiseAndSetIfChanged(ref _showInstallButton, value);
|
||||
}
|
||||
|
||||
private bool _showUpdateButton;
|
||||
public bool ShowUpdateButton
|
||||
{
|
||||
get => _showUpdateButton;
|
||||
set => this.RaiseAndSetIfChanged(ref _showUpdateButton, value);
|
||||
}
|
||||
|
||||
public ObservableCollection<ModInfo> Mods { get; } = new();
|
||||
|
||||
private ModInfo _selectedMod;
|
||||
public ModInfo SelectedMod
|
||||
{
|
||||
get => _selectedMod;
|
||||
set
|
||||
{
|
||||
this.RaiseAndSetIfChanged(ref _selectedMod, value);
|
||||
_ = LoadSelectedModConfigFromFtp(); // async fire-and-forget
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LoadSelectedModConfigFromFtp()
|
||||
{
|
||||
if (SelectedMod == null)
|
||||
{
|
||||
SelectedModConfig = string.Empty;
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
SelectedModConfig = await FtpViewModel._ftpClient.DownloadTextAsync(SelectedMod.ConfigPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
SelectedModConfig = $"Fehler beim Laden: {ex.Message}";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private string _selectedModConfig;
|
||||
public string SelectedModConfig
|
||||
{
|
||||
get => _selectedModConfig;
|
||||
set => this.RaiseAndSetIfChanged(ref _selectedModConfig, value);
|
||||
}
|
||||
private Window _mainWindow;
|
||||
public void SetMainWindow(Window mainWindow)
|
||||
{
|
||||
_mainWindow = mainWindow;
|
||||
}
|
||||
|
||||
|
||||
// Reactive Commands
|
||||
public ReactiveCommand<Unit, Unit> InstallBepInExCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> UpdateBepInExCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> UploadModCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> InstallFromStoreCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> SaveConfigCommand { get; }
|
||||
public ReactiveCommand<Unit, Unit> RemoveModCommand { get; }
|
||||
|
||||
public ServerDashboardViewModel()
|
||||
{
|
||||
InstallBepInExCommand = ReactiveCommand.Create(InstallBepInEx);
|
||||
UpdateBepInExCommand = ReactiveCommand.Create(UpdateBepInEx);
|
||||
UploadModCommand = ReactiveCommand.Create(UploadMod);
|
||||
InstallFromStoreCommand = ReactiveCommand.Create(InstallFromStore);
|
||||
SaveConfigCommand = ReactiveCommand.Create(SaveConfig);
|
||||
RemoveModCommand = ReactiveCommand.Create(RemoveMod);
|
||||
|
||||
// Demo
|
||||
BepInExStatus = "Nicht installiert";
|
||||
ShowInstallButton = true;
|
||||
ShowUpdateButton = false;
|
||||
|
||||
Mods.Add(new ModInfo { Name = "ValheimPlus", Version = "1.0.0", ConfigPath = "config/valheim_plus.cfg" });
|
||||
Mods.Add(new ModInfo { Name = "BetterUI", Version = "2.3.4", ConfigPath = "config/better_ui.cfg" });
|
||||
}
|
||||
|
||||
private void InstallBepInEx()
|
||||
{
|
||||
BepInExStatus = "Installiert (vX.Y.Z)";
|
||||
ShowInstallButton = false;
|
||||
ShowUpdateButton = true;
|
||||
}
|
||||
|
||||
private void UpdateBepInEx()
|
||||
{
|
||||
BepInExStatus = "Aktuell (vX.Y.Z)";
|
||||
ShowUpdateButton = false;
|
||||
}
|
||||
|
||||
public async Task<string?> OpenFileAsync(Window parent)
|
||||
{
|
||||
var dialog = new OpenFileDialog
|
||||
{
|
||||
AllowMultiple = false
|
||||
};
|
||||
dialog.Filters.Add(new FileDialogFilter { Name = "Config Files", Extensions = { "cfg" } });
|
||||
dialog.Filters.Add(new FileDialogFilter { Name = "All Files", Extensions = { "*" } });
|
||||
|
||||
var result = await dialog.ShowAsync(parent);
|
||||
return result?.FirstOrDefault(); // Gibt den ausgew<65>hlten Pfad zur<75>ck oder null
|
||||
}
|
||||
|
||||
private async void UploadMod() {
|
||||
if (_mainWindow == null)
|
||||
return; // Window muss gesetzt sein
|
||||
|
||||
var path = await OpenFileAsync(_mainWindow);
|
||||
FtpViewModel._ftpClient.UploadFileAsync("", "");
|
||||
}
|
||||
private void InstallFromStore() { /* TODO */ }
|
||||
private void SaveConfig() {
|
||||
Debug.WriteLine(SelectedMod.ConfigPath);
|
||||
FtpViewModel._ftpClient.UploadTextAsync(SelectedModConfig, SelectedMod.ConfigPath);
|
||||
}
|
||||
private void RemoveMod()
|
||||
{
|
||||
if (SelectedMod != null)
|
||||
Mods.Remove(SelectedMod);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user