示例#1
0
        private void Timer_Tick(object sender, EventArgs e)
        {
            if (Window == null)
            {
                LoadWindows();
                return;
            }

            int size = GetWindowTextLength(Window.hwnd);

            if (size++ > 0 && IsWindowVisible(Window.hwnd))
            {
                StringBuilder sb = new StringBuilder(size);
                GetWindowText(Window.hwnd, sb, size);
                var currentTime = Regex.Match(sb.ToString(), "([0-9]+\\s*:\\s*[0-9]+\\s*:\\s*[0-9]+)", RegexOptions.IgnoreCase).Value;
                if (string.IsNullOrEmpty(currentTime))
                {
                    currentTime = Regex.Match(sb.ToString(), "(Time\\s*\\[\\s*[0-9]\\s*\\]\\s*:\\s*[0-9]+)", RegexOptions.IgnoreCase).Value;
                }
                if (!string.IsNullOrEmpty(currentTime))
                {
                    GameTime = currentTime.Replace("Time", "");
                    var tempDeaths = Regex.Match(sb.ToString(), DeathsRegex, RegexOptions.IgnoreCase).Value;
                    var temp       = Regex.Match(tempDeaths, "\\[[0-9]\\]").Value;
                    Deaths = !string.IsNullOrEmpty(temp) ? tempDeaths.Replace(temp, "") : tempDeaths;
                    Deaths = Deaths.ToUpper()
                             .Replace(" ", "")
                             .Replace(":", "")
                             .Replace("DEATHS", "")
                             .Replace("DEATH", "");
                    string tempGameName = sb.ToString().Replace(currentTime, "").Replace(tempDeaths, "");

                    gameName = Regex.Replace(tempGameName, "[^A-Za-zА-Яа-я0-9 _.]", "_") + ".txt";
                    if (!File.Exists(folder + gameName))
                    {
                        var fs = File.Create(folder + gameName);
                        fs.Close();
                        Stats = new Stats {
                            Values = new List <StatsValue>(), Parallels = new ObservableCollection <Parallels>()
                        };
                        var json = Serializer.Serialize(Stats);
                        File.WriteAllText(folder + gameName, json);
                    }
                    if (Stats == null)
                    {
                        var json = File.ReadAllText(folder + gameName);
                        Stats = Serializer.Deserialize <Stats>(json);
                    }

                    var currentDeaths = GetDeaths();
                    foreach (var parallel in Stats.Parallels) // для ситуаций, когда текущее время меньше чем записаное в файл
                    {
                        if (parallel.OffTime > new ComplexTime {
                            Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                        })
                        {
                            parallel.OffTime = new ComplexTime {
                                Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                            };
                            parallel.ElapsedTime = GTC.CalculateSum(GTC.CalculateDifference(parallel.BackTime, parallel.OffTime), parallel.ElapsedTime);
                        }
                        if (parallel.BackTime > new ComplexTime {
                            Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                        })
                        {
                            parallel.BackTime = new ComplexTime {
                                Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                            };
                        }

                        if (parallel.OffDeaths > currentDeaths)
                        {
                            parallel.OffDeaths     = currentDeaths.Value;
                            parallel.ElapsedDeaths = parallel.OffDeaths - parallel.BackDeaths + parallel.ElapsedDeaths;
                        }
                        if (parallel.BackDeaths > currentDeaths)
                        {
                            parallel.BackDeaths = currentDeaths.Value;
                        }
                        ParallelsVisibility = Visibility.Visible;
                    }
                    foreach (var statsValue in Stats.Values)
                    {
                        if (statsValue.Time > new ComplexTime {
                            Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                        })
                        {
                            statsValue.Time = new ComplexTime {
                                Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                            };
                        }
                        if (statsValue.Deaths.HasValue && statsValue.Deaths.Value > currentDeaths)
                        {
                            statsValue.Deaths = currentDeaths.Value;
                        }
                    }

                    foreach (var parallel in Stats.Parallels)
                    {
                        if (Stats.Values.Last().Time > parallel.OffTime)
                        {
                            ParallelsVisibility = Visibility.Collapsed;
                        }
                    }


                    if (Stats.SelectedParallel == null)
                    {
                        if (Stats.Values.Count == 0)
                        {
                            Current = $"Time: {GetHours():0}:{GetMinutes():00}:{GetSeconds():00}\nDeaths: {GetDeaths()}";
                        }
                        else
                        {
                            var idk = GTC.CalculateDifference(Stats.Values.Last().Time,
                                                              new ComplexTime {
                                Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                            });
                            Current =
                                $"Time: {idk.Hours:0}:{idk.Minutes:00}:{idk.Seconds:00}\nDeaths: {GetDeaths() - Stats.Values.Last().Deaths}";
                        }

                        if (Stats.Values.Count == 0)
                        {
                            Last = "";
                        }
                        else if (Stats.Values.Count == 1)
                        {
                            Last =
                                $"Time: {Stats.Values.Last().Time.Hours}:{Stats.Values.Last().Time.Minutes}:{Stats.Values.Last().Time.Seconds}\nDeaths: {Stats.Values.Last().Deaths}";
                        }
                        else
                        {
                            var idk = GTC.CalculateDifference(Stats.Values[Stats.Values.Count - 2].Time,
                                                              Stats.Values.Last().Time);
                            Last =
                                $"Time:{idk.Hours:0}:{idk.Minutes:00}:{idk.Seconds:00}\nDeaths: {Stats.Values.Last().Deaths - Stats.Values[Stats.Values.Count - 2].Deaths}";
                        }
                    }
                    else
                    {
                        var currentParallel = Stats.Parallels.FirstOrDefault(i => i.ID.Equals(Stats.SelectedParallel));
                        if (!currentParallel.Cleared)
                        {
                            var timenow = new ComplexTime {
                                Hours = GetHours(), Minutes = GetMinutes(), Seconds = GetSeconds()
                            };
                            var idk = GTC.CalculateSum(GTC.CalculateDifference(currentParallel.BackTime, timenow), currentParallel.ElapsedTime);
                            currentParallel.ElapsedTimeString = idk.Hours + ":" + idk.Minutes + ":" + idk.Seconds;
                            var idk2 = currentDeaths - currentParallel.BackDeaths + currentParallel.ElapsedDeaths;
                            currentParallel.ElapsedDeathsString = idk2.ToString();
                        }
                    }
                }
            }
            else
            {
                Stats = null;
                LoadWindows();
            }
        }