0
0
mirror of https://github.com/sp-tarkov/patcher.git synced 2025-02-13 08:10:47 -05:00
patcher/Patcher/PatchClient/ViewModels/PatcherViewModel.cs

93 lines
2.8 KiB
C#

using Avalonia;
using PatchClient.Models;
using PatcherUtils;
using ReactiveUI;
using Splat;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace PatchClient.ViewModels
{
public class PatcherViewModel : ViewModelBase
{
private bool initLineItemProgress = true;
public ObservableCollection<LineItemProgress> LineItems { get; set; } = new ObservableCollection<LineItemProgress>();
private string _ProgressMessage;
public string ProgressMessage
{
get => _ProgressMessage;
set => this.RaiseAndSetIfChanged(ref _ProgressMessage, value);
}
private int _PatchPercent;
public int PatchPercent
{
get => _PatchPercent;
set => this.RaiseAndSetIfChanged(ref _PatchPercent, value);
}
private string _PatchMessage;
public string PatchMessage
{
get => _PatchMessage;
set => this.RaiseAndSetIfChanged(ref _PatchMessage, value);
}
private ViewNavigator navigator => Locator.Current.GetService<ViewNavigator>();
public PatcherViewModel()
{
RunPatcher();
}
private void RunPatcher()
{
Task.Run(() =>
{
//Slight delay to avoid some weird race condition in avalonia core, seems to be a bug, but also maybe I'm just stupid, idk -waffle
//Error without delay: An item with the same key has already been added. Key: [1, Avalonia.Controls.Generators.ItemContainerInfo]
System.Threading.Thread.Sleep(1000);
PatchHelper patcher = new PatchHelper(Environment.CurrentDirectory, null, LazyOperations.PatchFolder);
patcher.ProgressChanged += patcher_ProgressChanged;
string message = patcher.ApplyPatches();
navigator.SelectedViewModel = new MessageViewModel(message).WithDelay(400);
});
}
private void patcher_ProgressChanged(object Sender, int Progress, int Total, int Percent, string Message = "", params LineItem[] AdditionalLineItems)
{
foreach (LineItem item in AdditionalLineItems)
{
if (initLineItemProgress)
{
if (item.ItemValue <= 0) continue;
LineItems.Add(new LineItemProgress(item));
}
LineItems.FirstOrDefault(x => x.Info == item.ItemText).UpdateProgress(item.ItemValue);
}
initLineItemProgress = false;
PatchPercent = Percent;
if (!string.IsNullOrWhiteSpace(Message))
{
PatchMessage = Message;
}
ProgressMessage = $"Patching: {Progress} / {Total} - {Percent}%";
}
}
}