VaultSmpInstaller/ViewModels/ProfileWindow1ViewModel.cs

126 lines
4.9 KiB
C#

using System;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.Json.Nodes;
using Avalonia.Media;
using ReactiveUI;
namespace VaultSmpInstaller.ViewModels;
public class ProfileWindow1ViewModel : ViewModelBase
{
public Interaction<ProfileWindow2ViewModel, ProfileWindow2ViewModel.InstanceInfo?> ShowProfileSelectionDialog { get; }
public ReactiveCommand<Unit, ProfileWindow2ViewModel.InstanceInfo?> UseCurseforgeCommand { get; }
public ReactiveCommand<Unit, ProfileWindow2ViewModel.InstanceInfo?> UsePrismCommand { get; }
public ProfileWindow1ViewModel()
{
var curseforgeDir = CurseforgeInstanceDir;
var prismDir = PrismInstanceDir;
ShowProfileSelectionDialog = new Interaction<ProfileWindow2ViewModel, ProfileWindow2ViewModel.InstanceInfo?>();
UseCurseforgeCommand = ReactiveCommand.CreateFromTask(async () =>
{
var profileWindowModel = new ProfileWindow2ViewModel(ProfileWindow2ViewModel.InstanceType.Curseforge, curseforgeDir);
return await ShowProfileSelectionDialog.Handle(profileWindowModel);
});
UsePrismCommand = ReactiveCommand.CreateFromTask(async () =>
{
var profileWindowModel = new ProfileWindow2ViewModel(ProfileWindow2ViewModel.InstanceType.Prism, prismDir);
return await ShowProfileSelectionDialog.Handle(profileWindowModel);
});
}
public static Brush Background => SolidColorBrush.Parse("#282A36");
public static Brush SecondaryBackground => SolidColorBrush.Parse("#44475A");
public static Brush ButtonBackground => SolidColorBrush.Parse("#6272A4");
public static Brush TextColor => SolidColorBrush.Parse("#F8F8F2");
private string? _curseforgeInstanceDir = null;
public bool IsCurseforgeInstalled { get; set; } = false;
public string CurseforgeButtonText => IsCurseforgeInstalled ? "Curseforge" : "Curseforge Not Detected";
public string? CurseforgeInstanceDir
{
get
{
if (_curseforgeInstanceDir == null && TryGetCurseforgeMinecraftRoot(out _curseforgeInstanceDir))
{
IsCurseforgeInstalled = true;
this.RaisePropertyChanged(nameof(IsCurseforgeInstalled));
this.RaisePropertyChanged(nameof(CurseforgeButtonText));
this.RaisePropertyChanged();
}
return _curseforgeInstanceDir;
}
}
public bool IsPrismInstalled { get; set; } = false;
private string? _prismInstanceDir = null;
public string PrismButtonText => IsPrismInstalled ? "Prism Launcher" : "Prism Launcher Not Detected";
public string? PrismInstanceDir
{
get
{
if (_prismInstanceDir == null && TryGetPrismMinecraftRoot(out _prismInstanceDir))
{
IsPrismInstalled = true;
this.RaisePropertyChanged(nameof(IsPrismInstalled));
this.RaisePropertyChanged(nameof(PrismButtonText));
this.RaisePropertyChanged();
}
return _prismInstanceDir;
}
}
public bool TryGetCurseforgeMinecraftRoot(out string? minecraftRoot)
{
minecraftRoot = null;
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!File.Exists(Path.Combine(appData, "Curseforge", "storage.json"))) return false;
var curseforgeConfig = JsonNode.Parse(File.ReadAllText(Path.Combine(appData, "Curseforge", "storage.json")))!.AsObject();
if (!curseforgeConfig.TryGetPropertyValue("minecraft-settings", out var minecraftSettingsNode)) return false;
if (!minecraftSettingsNode!.AsValue().TryGetValue(out string? minecraftSettingsString)) return false;
var minecraftSettings = JsonNode.Parse(minecraftSettingsString);
if (!minecraftSettings!.AsObject().TryGetPropertyValue("minecraftRoot", out var minecraftRootNode)) return false;
if (!minecraftRootNode!.AsValue().TryGetValue(out minecraftRoot)) return false;
minecraftRoot = Path.Combine(minecraftRoot, "Instances");
return true;
}
public bool TryGetPrismMinecraftRoot(out string? minecraftRoot)
{
minecraftRoot = null;
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if (!File.Exists(Path.Combine(appData, "PrismLauncher", "prismlauncher.cfg"))) return false;
foreach(String line in File.ReadLines(Path.Combine(appData, "PrismLauncher", "prismlauncher.cfg")))
{
if (line.StartsWith("InstanceDir"))
{
string path = line.Split('=', 2)[1];
if (Path.IsPathFullyQualified(path))
minecraftRoot = path;
else
minecraftRoot = Path.Combine(appData, "PrismLauncher", path);
return true;
}
}
return false;
}
}