Newer
Older
Qwilight / Igniter / ViewModels / MainViewModel.cs
@Taehui Taehui on 21 Aug 5 KB v1.16.40
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Messaging;
using Igniter.MSG;
using Igniter.Utilities;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;

namespace Igniter.ViewModel
{
    public sealed partial class MainViewModel : ObservableObject
    {
        double _value = double.NaN;
        string _text = "Loading…";
        bool _isVisible = true;

        public double Value
        {
            get => _value;

            set => SetProperty(ref _value, value, nameof(Value));
        }

        public string Text
        {
            get => _text;

            set => SetProperty(ref _text, value, nameof(Text));
        }

        public bool IsVisible
        {
            get => _isVisible;

            set => SetProperty(ref _isVisible, value, nameof(IsVisible));
        }

        public async Task OnLoaded()
        {
            switch (StrongReferenceMessenger.Default.Send(new ViewAllowWindow
            {
                Text = LanguageSystem.Instance.Levy,
                Input = MessageBoxButton.OKCancel,
                Drawing = MessageBoxImage.Information
            }).Response)
            {
                case MessageBoxResult.OK:
                    while (true)
                    {
                        try
                        {
                            await Task.Run(() =>
                            {
                                var rootEntryPath = Environment.GetCommandLineArgs()[1];

                                var filePaths = new List<string>();
                                SetFilePaths(rootEntryPath);

                                void SetFilePaths(string targetEntryPath)
                                {
                                    foreach (var entryPath in Utility.GetEntry(targetEntryPath))
                                    {
                                        SetFilePaths(entryPath);
                                    }
                                    foreach (var filePath in Utility.GetFiles(targetEntryPath))
                                    {
                                        filePaths.Add(filePath);
                                    }
                                }

                                var filePathsCount = filePaths.Count;
                                for (var i = 0; i < filePathsCount; ++i)
                                {
                                    var filePath = filePaths[i];
                                    var logicalFilePath = filePath.Replace(rootEntryPath + Path.DirectorySeparatorChar, string.Empty);
                                    var targetFilePath = Path.Combine(Path.GetDirectoryName(IgniterComponent.QwilightFilePath), logicalFilePath);

                                    Value = 100.0 * i / filePathsCount;
                                    Text = logicalFilePath;

                                    if (new FileInfo(filePath).Length > 0)
                                    {
                                        Utility.CopyFile(filePath, targetFilePath);
                                    }
                                    else
                                    {
                                        Utility.WipeFile(targetFilePath);
                                    }
                                }
                            });

                            IsVisible = false;
                            StrongReferenceMessenger.Default.Send(new ViewAllowWindow
                            {
                                Text = LanguageSystem.Instance.Ignited,
                                Input = MessageBoxButton.OK,
                                Drawing = MessageBoxImage.Information
                            });
                            switch (StrongReferenceMessenger.Default.Send(new ViewAllowWindow
                            {
                                Text = LanguageSystem.Instance.ExeQwilight,
                                Input = MessageBoxButton.YesNo,
                                Drawing = MessageBoxImage.Question
                            }).Response)
                            {
                                case MessageBoxResult.Yes:
                                    Process.Start(IgniterComponent.QwilightFilePath);
                                    break;
                            }
                            Environment.Exit(0);
                        }
                        catch (Exception e)
                        {
                            IsVisible = false;
                            StrongReferenceMessenger.Default.Send(new ViewAllowWindow
                            {
                                Text = e.Message,
                                Input = MessageBoxButton.OK,
                                Drawing = MessageBoxImage.Error
                            });
                            switch (StrongReferenceMessenger.Default.Send(new ViewAllowWindow
                            {
                                Text = LanguageSystem.Instance.IgnitingFault,
                                Input = MessageBoxButton.YesNo,
                                Drawing = MessageBoxImage.Warning
                            }).Response)
                            {
                                case MessageBoxResult.Yes:
                                    IsVisible = true;
                                    break;
                                case MessageBoxResult.No:
                                    Environment.Exit(1);
                                    break;
                            }

                            if (IsVisible)
                            {
                                continue;
                            }
                        }
                    }
                case MessageBoxResult.Cancel:
                    Environment.Exit(1);
                    break;
            }
        }
    }
}