XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEWarframe

【WinUI】基于Warframe游戏本体的公开服务器以及安装器

公开
关注 0 Fork 0 Star 0
UTF-8
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Microsoft.UI.Xaml;
using Newtonsoft.Json.Linq;
using XFEExtension.NetCore.WinUIHelper.Utilities.Helper;
using XFEWarframe.Admin.Profiles.CrossVersionProfiles;
using XFEWarframe.Admin.Views;
using XFEWarframe.Desktop.Editing;

namespace XFEWarframe.Admin.ViewModels;

public partial class SettingPageViewModel : AdminViewModelBase
{
    private string? configOrigin;
    [ObservableProperty] public partial string Address { get; set; } = "";
    [ObservableProperty] public partial string Token { get; set; } = "";
    [ObservableProperty] public partial string ServerName { get; set; } = "";
    [ObservableProperty] public partial bool AutoCreateAccount { get; set; }
    [ObservableProperty] public partial bool AllowPlayerRegistration { get; set; }
    [ObservableProperty] public partial bool AllowPlayerCurrencyGrant { get; set; }
    [ObservableProperty] public partial bool AllowPlayerItemGrant { get; set; }
    [ObservableProperty] public partial bool ConfigurationReady { get; set; }
    [ObservableProperty] public partial string ConfigurationStatus { get; set; } = "请先读取服务器设置";
    [ObservableProperty] public partial int ThemeIndex { get; set; } = (int)SystemProfile.Theme;
    [ObservableProperty] public partial ItemLanguage SelectedItemLanguage { get; set; } = ItemLanguage.Find(SystemProfile.ItemLanguage);
    [ObservableProperty] public partial UiLanguage SelectedUiLanguage { get; set; } = UiLanguage.Find(SystemProfile.UiLanguage);
    public IReadOnlyList<UiLanguage> UiLanguages => UiText.Languages;
    partial void OnSelectedUiLanguageChanged(UiLanguage value)
    {
        if (value is null) return;
        SystemProfile.UiLanguage = value.Code;
        SystemProfile.SaveProfile();
    }
    public IReadOnlyList<ItemLanguage> ItemLanguages => ItemLanguage.All;
    partial void OnSelectedItemLanguageChanged(ItemLanguage value)
    {
        if (value is not null) SystemProfile.ItemLanguage = value.Code;
    }
    public string LocalRoot => Service.LocalRoot;

    protected override async Task LoadAsync(object? parameter)
    {
        Address = State.Address;
        Token = State.Token;
        if (State.Connected) await ReadConfigurationCoreAsync();
    }
    partial void OnThemeIndexChanged(int value)
    {
        if (value is < 0 or > 2) return;
        SystemProfile.Theme = (ElementTheme)value;
        AppThemeHelper.Theme = SystemProfile.Theme;
        AppThemeHelper.ChangeTheme(SystemProfile.Theme);
    }
    [RelayCommand] private Task ConnectAsync() => RunAsync(async () =>
    {
        State.Address = Address;
        State.Token = Token;
        configOrigin = null;
        ConfigurationReady = false;
        ConfigurationStatus = "请先读取服务器设置";
        await Service.ConnectAsync();
        Notify("服务器连接成功");
        Navigation.NavigateTo<MainPage>();
    });
    [RelayCommand] private Task ReadLocalAsync() => RunAsync(() =>
    {
        Service.ReadLocalConfiguration();
        Address = State.Address;
        Token = State.Token;
        configOrigin = null;
        ConfigurationReady = false;
        ConfigurationStatus = "请先读取服务器设置";
        return Task.CompletedTask;
    });
    [RelayCommand] private Task ReadConfigurationAsync() => RunAsync(ReadConfigurationCoreAsync);
    private async Task ReadConfigurationCoreAsync()
    {
        var config = await Service.GetAsync("config");
        ServerName = config.Value<string>("serverName") ?? "XFEWarframe";
        AutoCreateAccount = config.Value<bool>("autoCreateAccount");
        AllowPlayerRegistration = config.Value<bool>("allowPlayerRegistration");
        AllowPlayerCurrencyGrant = config.Value<bool>("allowPlayerCurrencyGrant");
        AllowPlayerItemGrant = config.Value<bool>("allowPlayerItemGrant");
        configOrigin = State.Address;
        ConfigurationReady = true;
        ConfigurationStatus = "已读取,可以保存修改";
    }
    [RelayCommand] private Task SaveConfigurationAsync() => RunAsync(async () =>
    {
        if (configOrigin != State.Address || configOrigin is null) throw new InvalidOperationException("请先读取当前服务器的设置。");
        await Service.PostAsync("config", new { serverName = ServerName, autoCreateAccount = AutoCreateAccount, allowPlayerRegistration = AllowPlayerRegistration, allowPlayerCurrencyGrant = AllowPlayerCurrencyGrant, allowPlayerItemGrant = AllowPlayerItemGrant });
        Notify("服务器设置已保存");
    });
}