Files
NebulaLauncher/Nebula.Launcher/ViewModels/Pages/ConfigurationViewModel.cs

116 lines
3.9 KiB
C#
Raw Normal View History

2025-05-10 19:19:30 +03:00
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
2025-06-19 21:12:42 +03:00
using System.IO;
using System.IO.Compression;
2025-07-02 21:32:51 +03:00
using System.Threading.Tasks;
2025-06-19 21:12:42 +03:00
using Nebula.Launcher.Services;
2025-12-11 21:47:54 +03:00
using Nebula.Launcher.Utils;
2025-07-02 21:32:51 +03:00
using Nebula.Launcher.ViewModels.Popup;
2025-05-10 19:19:30 +03:00
using Nebula.Launcher.Views.Pages;
2025-06-18 21:36:50 +03:00
using Nebula.Shared;
2025-08-06 21:29:00 +03:00
using Nebula.Shared.Configurations;
2025-05-10 19:19:30 +03:00
using Nebula.Shared.Services;
2025-07-02 21:32:51 +03:00
using Nebula.Shared.ViewHelper;
2025-05-10 19:19:30 +03:00
namespace Nebula.Launcher.ViewModels.Pages;
[ViewModelRegister(typeof(ConfigurationView))]
[ConstructGenerator]
2025-06-18 21:36:50 +03:00
public partial class ConfigurationViewModel : ViewModelBase
2025-05-10 19:19:30 +03:00
{
2025-06-18 21:36:50 +03:00
public ObservableCollection<IConfigControl> ConfigurationVerbose { get; } = new();
2025-05-10 19:19:30 +03:00
[GenerateProperty] private ConfigurationService ConfigurationService { get; } = default!;
2025-06-19 21:12:42 +03:00
[GenerateProperty] private PopupMessageService PopupService { get; } = default!;
[GenerateProperty] private FileService FileService { get; set; } = default!;
2025-07-02 21:32:51 +03:00
[GenerateProperty] private ContentService ContentService { get; set; } = default!;
[GenerateProperty] private CancellationService CancellationService { get; set; } = default!;
[GenerateProperty] private ViewHelperService ViewHelperService { get; set; } = default!;
2025-05-10 19:19:30 +03:00
2025-07-03 12:17:15 +03:00
private readonly List<(object, Type)> _conVarList = new();
2025-05-10 19:19:30 +03:00
2025-06-18 21:36:50 +03:00
public void AddCvarConf<T>(ConVar<T> cvar)
2025-05-10 19:19:30 +03:00
{
2025-06-18 21:36:50 +03:00
ConfigurationVerbose.Add(
2025-07-02 21:32:51 +03:00
ConfigControlHelper.GetConfigControl(cvar.Name, ConfigurationService.GetConfigValue(cvar)!));
2025-07-03 12:17:15 +03:00
_conVarList.Add((cvar, cvar.Type));
2025-05-10 19:19:30 +03:00
}
public void InvokeUpdateConfiguration()
{
2025-06-18 21:36:50 +03:00
for (int i = 0; i < ConfigurationVerbose.Count; i++)
2025-05-10 19:19:30 +03:00
{
2025-06-18 21:36:50 +03:00
var conVarControl = ConfigurationVerbose[i];
if(!conVarControl.Dirty)
continue;
2025-07-03 12:17:15 +03:00
var conVar = _conVarList[i];
2025-06-18 21:36:50 +03:00
var methodInfo = ConfigurationService.GetType().GetMethod("SetConfigValue")!.MakeGenericMethod(conVar.Item2);
methodInfo.Invoke(ConfigurationService, [conVar.Item1, conVarControl.GetValue()]);
2025-05-10 19:19:30 +03:00
}
}
2025-06-19 21:12:42 +03:00
public void ResetConfig()
{
2025-07-03 12:17:15 +03:00
foreach (var conVar in _conVarList)
2025-06-19 21:12:42 +03:00
{
var methodInfo = ConfigurationService.GetType().GetMethod("SetConfigValue")!.MakeGenericMethod(conVar.Item2);
methodInfo.Invoke(ConfigurationService, [conVar.Item1, null]);
}
2025-07-03 12:17:15 +03:00
_conVarList.Clear();
2025-06-19 21:12:42 +03:00
ConfigurationVerbose.Clear();
InitConfiguration();
PopupService.Popup("Configuration has been reset.");
}
public void OpenDataFolder()
{
2025-12-11 21:47:54 +03:00
ExplorerUtils.OpenFolder(FileService.RootPath);
2025-06-19 21:12:42 +03:00
}
public void ExportLogs()
{
var logPath = Path.Join(FileService.RootPath, "log");
var path = Path.Combine(Path.GetTempPath(), "tempThink"+Path.GetRandomFileName());
Directory.CreateDirectory(path);
ZipFile.CreateFromDirectory(logPath, Path.Join(path, DateTime.Now.ToString("yyyy-MM-dd") + ".zip"));
2025-12-11 21:47:54 +03:00
ExplorerUtils.OpenFolder(path);
2025-06-19 21:12:42 +03:00
}
2025-07-02 21:32:51 +03:00
public void RemoveAllContent()
{
Task.Run(() =>
{
using var loader = ViewHelperService.GetViewModel<LoadingContextViewModel>();
loader.LoadingName = "Removing content";
PopupService.Popup(loader);
ContentService.RemoveAllContent(loader.CreateLoadingContext(), CancellationService.Token);
2025-07-02 21:32:51 +03:00
});
}
2025-06-19 21:12:42 +03:00
private void InitConfiguration()
{
2025-06-18 21:36:50 +03:00
AddCvarConf(LauncherConVar.ILSpyUrl);
AddCvarConf(LauncherConVar.Hub);
AddCvarConf(LauncherConVar.AuthServers);
AddCvarConf(CurrentConVar.EngineManifestUrl);
AddCvarConf(CurrentConVar.RobustAssemblyName);
AddCvarConf(CurrentConVar.ManifestDownloadProtocolVersion);
2025-05-10 19:19:30 +03:00
}
2025-06-19 21:12:42 +03:00
protected override void InitialiseInDesignMode()
{
InitConfiguration();
}
2025-05-10 19:19:30 +03:00
protected override void Initialise()
{
2025-06-19 21:12:42 +03:00
InitConfiguration();
2025-05-10 19:19:30 +03:00
}
}