VaultSmpInstaller/ViewModels/ProfileWindow1ViewModel.cs
CanadianBacon 4663bbca87 1.0.1
Refactor, make safer, add some logging
2024-02-21 07:34:48 +01:00

143 lines
5.8 KiB
C#

using System;
using System.IO;
using System.Reactive;
using System.Reactive.Linq;
using System.Text.Json.Nodes;
using Avalonia.Media;
using Microsoft.Win32;
using ReactiveUI;
namespace VaultSmpInstaller.ViewModels;
public class ProfileWindow1ViewModel : ReactiveObject
{
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 () =>
{
if (curseforgeDir == null) return null;
var profileWindowModel = new ProfileWindow2ViewModel(ProfileWindow2ViewModel.InstanceType.Curseforge, curseforgeDir);
return await ShowProfileSelectionDialog.Handle(profileWindowModel);
});
UsePrismCommand = ReactiveCommand.CreateFromTask(async () =>
{
if (prismDir == null) return null;
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;
public bool IsCurseforgeInstalled { get; set; }
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; }
private string? _prismInstanceDir;
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 IsOverwolfInstalled { get; } = Registry.GetValue(@"HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Overwolf", "UninstallString", null) != null;
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 == null)
{
File.AppendAllText(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "smpinstaller.log"), "Failed to read curseforge config!\n");
return false;
}
if (!curseforgeConfig.TryGetPropertyValue("minecraft-settings", out var minecraftSettingsNode))
{
minecraftRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "curseforge", "minecraft", "Instances");
return true;
}
if (!minecraftSettingsNode!.AsValue().TryGetValue(out string? minecraftSettingsString)) return false;
var minecraftSettings = JsonNode.Parse(minecraftSettingsString);
if (!minecraftSettings!.AsObject().TryGetPropertyValue("minecraftRoot", out var minecraftRootNode)) {
minecraftRoot = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "curseforge", "minecraft", "Instances");
return true;
}
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;
}
}