From 7afbf78bb060aa690c1847dbb7654ee089fdc7c6 Mon Sep 17 00:00:00 2001 From: tbS Date: Sat, 23 Aug 2025 16:13:08 +0200 Subject: [PATCH] =?UTF-8?q?-=20ServerDashbord=20Hinzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -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 --- Valheim Dashboard/FtpClient.cs | 29 +++- Valheim Dashboard/Models/ModInfo.cs | 15 ++ Valheim Dashboard/Valheim Dashboard.csproj | 4 +- .../ViewModels/MainWindowViewModel.cs | 7 + .../ViewModels/ServerDashboardViewModel.cs | 152 ++++++++++++++++++ .../ViewModels/ViewModelLocator.cs | 2 + Valheim Dashboard/Views/FTPView.axaml | 129 ++++++++------- Valheim Dashboard/Views/FTPView.axaml.cs | 14 ++ Valheim Dashboard/Views/MainWindow.axaml.cs | 12 ++ Valheim Dashboard/Views/ServerDashboard.axaml | 69 ++++++++ .../Views/ServerDashboard.axaml.cs | 14 ++ 11 files changed, 391 insertions(+), 56 deletions(-) create mode 100644 Valheim Dashboard/Models/ModInfo.cs create mode 100644 Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs create mode 100644 Valheim Dashboard/Views/ServerDashboard.axaml create mode 100644 Valheim Dashboard/Views/ServerDashboard.axaml.cs diff --git a/Valheim Dashboard/FtpClient.cs b/Valheim Dashboard/FtpClient.cs index c30542b..bdf227e 100644 --- a/Valheim Dashboard/FtpClient.cs +++ b/Valheim Dashboard/FtpClient.cs @@ -1,10 +1,11 @@ +using ReactiveUI; using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Reactive; +using System.Text; using System.Threading.Tasks; -using ReactiveUI; using Valheim_Dashboard.ViewModels; namespace Valheim_Dashboard @@ -123,6 +124,32 @@ namespace Valheim_Dashboard await fileStream.CopyToAsync(requestStream); } + public async Task UploadTextAsync(string text, string remoteFilePath) + { + var request = CreateFtpWebRequest(remoteFilePath, WebRequestMethods.Ftp.UploadFile); + + // Konvertiere den String in ein Byte-Array (UTF-8) + byte[] bytes = Encoding.UTF8.GetBytes(text); + request.ContentLength = bytes.Length; + + using var requestStream = await request.GetRequestStreamAsync(); + await requestStream.WriteAsync(bytes, 0, bytes.Length); + } + + public async Task DownloadTextAsync(string remoteFilePath) + { + var request = CreateFtpWebRequest(remoteFilePath, WebRequestMethods.Ftp.DownloadFile); + + using var response = (FtpWebResponse)await request.GetResponseAsync(); + using var responseStream = response.GetResponseStream(); + using var reader = new StreamReader(responseStream!, Encoding.UTF8); + + string content = await reader.ReadToEndAsync(); + return content; + } + + + /// /// Löscht eine Datei vom FTP-Server (Async). /// diff --git a/Valheim Dashboard/Models/ModInfo.cs b/Valheim Dashboard/Models/ModInfo.cs new file mode 100644 index 0000000..9f5f12c --- /dev/null +++ b/Valheim Dashboard/Models/ModInfo.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Valheim_Dashboard.Models; + +public class ModInfo +{ + public string Name { get; set; } + public string Version { get; set; } + public string ConfigPath { get; set; } +} + diff --git a/Valheim Dashboard/Valheim Dashboard.csproj b/Valheim Dashboard/Valheim Dashboard.csproj index 5971f8f..77c4e6d 100644 --- a/Valheim Dashboard/Valheim Dashboard.csproj +++ b/Valheim Dashboard/Valheim Dashboard.csproj @@ -9,7 +9,6 @@ - @@ -39,5 +38,8 @@ True Resources.resx + + ServerDashboard.axaml + diff --git a/Valheim Dashboard/ViewModels/MainWindowViewModel.cs b/Valheim Dashboard/ViewModels/MainWindowViewModel.cs index b3a9fdc..db4556b 100644 --- a/Valheim Dashboard/ViewModels/MainWindowViewModel.cs +++ b/Valheim Dashboard/ViewModels/MainWindowViewModel.cs @@ -32,6 +32,8 @@ namespace Valheim_Dashboard.ViewModels { _currentViewModel = _locator.ErsterStartViewModel; } + + } @@ -61,6 +63,11 @@ namespace Valheim_Dashboard.ViewModels CurrentViewModel = _locator.ServerSettingsViewModel; } + public void SwitchToServerDashboard() + { + CurrentViewModel = _locator.ServerDashboardViewModel; + } + public void SwitchToDashboard() { CurrentViewModel = _locator.DashboardViewModel; diff --git a/Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs b/Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs new file mode 100644 index 0000000..922f726 --- /dev/null +++ b/Valheim Dashboard/ViewModels/ServerDashboardViewModel.cs @@ -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 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 InstallBepInExCommand { get; } + public ReactiveCommand UpdateBepInExCommand { get; } + public ReactiveCommand UploadModCommand { get; } + public ReactiveCommand InstallFromStoreCommand { get; } + public ReactiveCommand SaveConfigCommand { get; } + public ReactiveCommand 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 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ählten Pfad zurü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); + } +} diff --git a/Valheim Dashboard/ViewModels/ViewModelLocator.cs b/Valheim Dashboard/ViewModels/ViewModelLocator.cs index 87757dc..60eb252 100644 --- a/Valheim Dashboard/ViewModels/ViewModelLocator.cs +++ b/Valheim Dashboard/ViewModels/ViewModelLocator.cs @@ -8,6 +8,8 @@ namespace Valheim_Dashboard public ErsterStartViewModel ErsterStartViewModel => new ErsterStartViewModel(); public LizenzViewModel LizenzViewModel => new LizenzViewModel(); // Hinzugefügt public ServerSettingsViewModel ServerSettingsViewModel => new ServerSettingsViewModel(); // Hinzugefügt + + public ServerDashboardViewModel ServerDashboardViewModel => new ServerDashboardViewModel(); // Hinzugefügt public DashboardViewModel DashboardViewModel => new DashboardViewModel(); // Hinzugefügt public FtpViewModel FtpViewModel => new FtpViewModel(); // Hinzugefügt diff --git a/Valheim Dashboard/Views/FTPView.axaml b/Valheim Dashboard/Views/FTPView.axaml index 8b53f95..a9829ab 100644 --- a/Valheim Dashboard/Views/FTPView.axaml +++ b/Valheim Dashboard/Views/FTPView.axaml @@ -2,62 +2,83 @@ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - xmlns:local="clr-namespace:Valheim_Dashboard" xmlns:vm="clr-namespace:Valheim_Dashboard.ViewModels" xmlns:views="clr-namespace:Valheim_Dashboard.Views" mc:Ignorable="d" - x:Name="root" - x:Class="Valheim_Dashboard.Views.FtpView" - x:DataType="vm:FtpViewModel" - > - - - - - - - - - + x:Class="Valheim_Dashboard.Views.FtpView" + x:DataType="vm:FtpViewModel"> - - - - - - + + + + + + + + + + + + + + +