Exemplo n.º 1
0
        private Task ExecuteAsync(bool shouldCreateDirectory)
        {
            var ct = CancellationToken;

            return(Task.Run(() =>
            {
                Progress.Value = 0;
                int count = 0;
                var outputDirectoryPath = Path.GetDirectoryName(FilePaths.First())
                                          + "\\AigisCutter_"
                                          + DateTime.Now.ToString("yyyyMMdd_HHmmss");;

                if (shouldCreateDirectory)
                {
                    if (Directory.Exists(outputDirectoryPath))
                    {
                        throw new Exception();
                    }

                    try
                    {
                        Directory.CreateDirectory(outputDirectoryPath);
                    }
                    catch (Exception)
                    {
                        OnError?.Invoke($"出力ディレクトリの作成に失敗しました\r\n{outputDirectoryPath}");
                    }
                }

                errorLog = new List <string>();
                Progress.Value = 10;

                try
                {
                    foreach (var path in FilePaths)
                    {
                        ct.ThrowIfCancellationRequested();
                        Progress.Value = 10 + 90 * count++ / FilePaths.Count;

                        var outputPath = shouldCreateDirectory ? outputDirectoryPath : Path.GetDirectoryName(path);
                        outputPath += "\\"
                                      + Path.GetFileNameWithoutExtension(path)
                                      + ".png";

                        try
                        {
                            if (IsPcSq.Value)
                            {
                                GameCutter.CutPcCount(outputPath, path, Width.Value, Height.Value, Delta.Value);
                            }
                            else if (IsPcEdge.Value)
                            {
                                GameCutter.CutPcSq(outputPath, path, Width.Value, Height.Value);
                            }
                            else if (IsIos.Value)
                            {
                                GameCutter.CutIosPoint(outputPath, path, HomebarHeight.Value);
                            }
                            else if (IsIosAndroid.Value)
                            {
                                GameCutter.CutIosAndroid(outputPath, path, Delta.Value);
                            }
                            else
                            {
                                throw new InvalidOperationException();
                            }
                        }
                        catch (FileNotFoundException)
                        {
                            errorLog.Add($"{path}が見つかりませんでした");
                        }
                        catch (Exception ex)
                        {
                            errorLog.Add($"{path} {ex.Message.Replace("\r\n", "  ")}");
                        }
                    }
                    Progress.Value = 100;
                }
                catch (OperationCanceledException)
                {
                }

                if (errorLog.Count > 0)
                {
                    try
                    {
                        var logPath = outputDirectoryPath + "\\error.txt";
                        using (var sw = new StreamWriter(logPath, false, Encoding.GetEncoding("Shift_JIS")))
                        {
                            foreach (var e in errorLog)
                            {
                                sw.WriteLine(e);
                            }
                        }
                        OnError?.Invoke($"{errorLog.Count}枚の画像でエラーが発生しました\r\n{logPath}を確認してください");
                    }
                    catch (Exception)
                    {
                        OnError?.Invoke("予期せぬエラーが発生しました");
                    }
                }
            }, ct));
        }