Newer
Older
Qwilight / Qwilight / System / FlintSystem.cs
@Taehui Taehui on 20 Aug 2 KB 2024-08-20 오후 5:48
using CommandLine;
using Qwilight.Compute;
using Qwilight.Utilities;
using Qwilight.ViewModel;
using System.IO;
using System.IO.Pipes;
using System.Text;

namespace Qwilight
{
    public sealed class FlintSystem
    {
        public static readonly FlintSystem Instance = new();

        static readonly string FaultEntryPath = Path.Combine(QwilightComponent.FaultEntryPath, nameof(FlintSystem));

        public void HandleSystem()
        {
            while (true)
            {
                using var ss = new NamedPipeServerStream("Qwilight", PipeDirection.In);
                try
                {
                    ss.WaitForConnection();
                    using var sr = new StreamReader(ss, Encoding.UTF8);
                    Parser.Default.ParseArguments<Params.FlintParams>(sr.ReadLine()?.Split(" ", 3) ?? Array.Empty<string>()).WithParsed(o =>
                    {
                        var mainViewModel = ViewModels.Instance.MainValue;
                        var defaultComputer = mainViewModel.Computer;
                        if (o.Stop)
                        {
                            if (defaultComputer != null)
                            {
                                defaultComputer.SetPause = true;
                            }
                        }
                        else if (o.Handle)
                        {
                            var m = o.Meter;
                            var noteFile = defaultComputer?.NoteFile;
                            var noteFilePath = o.FileName.Replace("\"", string.Empty);
                            if (defaultComputer?.IsHandling == true && noteFile?.NoteFilePath == noteFilePath && noteFile.IsValid())
                            {
                                defaultComputer.LevyingMeter = m;
                                defaultComputer.SetUndoValue = DefaultCompute.SetUndo.Just;
                            }
                            else
                            {
                                mainViewModel.FlintNoteFile(noteFilePath, -1, m);
                            }
                        }
                    });
                }
                catch (Exception e)
                {
                    Utility.SaveFaultFile(FaultEntryPath, e);
                }
            }
        }
    }
}