Example #1
0
        void CreateStateMachine()
        {
            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => true,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var process = Process.GetProcessesByName(ProcessName).FirstOrDefault();
                    if (process != null && !process.HasExited)
                    {
                        Process = process;
                        return(true);
                    }

                    return(false);
                },
                    () =>
                {
                    Log.WriteLine($"Attached to {Process.MainWindowTitle}");
                })
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.PatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        var memoryScan = new ThreadedMemoryScan(Process, pattern, true, ThreadsPerScan);
                        m_MemoryScans.Add(memoryScan);
                    }
                })
            }));

            m_StateMachine.Add(State.PatternScanning, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Working,
                    () =>
                {
                    var finishedWithResults = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted && memoryScan.Results.SelectMany(result => result.Matches).Any());
                    return(finishedWithResults.Count() == m_MemoryScans.Count);
                },
                    () =>
                {
                    var orderedMatches = m_MemoryScans.Select(memoryScan => memoryScan.Results.Where(result => result.Matches.Any()).First().Matches.First()).OrderBy(match => match);
                    Log.WriteLine($"Match Range: {orderedMatches.First():X} - {orderedMatches.Last():X}");
                }),
                new StateMachine <State> .Transition(
                    State.PatternScanFailed,
                    () =>
                {
                    var completedScans = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted);
                    if (completedScans.Count() == m_MemoryScans.Count)
                    {
                        var finishedWithoutResults = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                        return(finishedWithoutResults.Any());
                    }

                    return(false);
                },
                    () =>
                {
                    var failedMemoryScans = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                    string failedPatterns = String.Join("\r\n", failedMemoryScans.Select(failedMemoryScan => failedMemoryScan.Pattern.Config.String));
                    Log.WriteLine($"Failed Patterns:\r\n{failedPatterns}");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));

            m_StateMachine.Add(State.Working, new StateMachine <State> .StateData(
                                   () =>
            {
                try
                {
                    UpdateMemory();
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex);
                }
            },
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));
        }
Example #2
0
        void CreateStateMachine()
        {
            var updater = new Updater();

            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.CheckingForUpdates,
                    () => ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Log.WriteLine("Searching for updates (You can disable this feature in file 'Config.json')!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !ConfigHelper.Main.Values.Overlay.MonsterWidget.UseNetworkServer && !ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Initialize();
                }),
                new StateMachine <State> .Transition(
                    State.ServerChecking,
                    () => ConfigHelper.Main.Values.Overlay.MonsterWidget.UseNetworkServer && !ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Log.WriteLine("Checking Server...");
                    ServerManager.Instance.RequestCommadWithHandler(ServerManager.Command.ALIVE, null, null, false, 0, null, (result, ping) =>
                    {
                        if (result != null)
                        {
                            if (result["status"].ToString().Equals("ok"))
                            {
                                Log.WriteLine($"Server is online with response time of {ping} ms");
                                ServerManager.Instance.IsServerOline = 1;
                            }
                            else
                            {
                                if (result["result"].ToString().Equals("v"))
                                {
                                    Log.WriteLine("You are using an outdated version of SmartHunter please update it to use this feature");
                                }
                                else if (result["result"].ToString().Equals("dev"))
                                {
                                    Log.WriteLine("Server is under maintenance, a new version should be available soon");
                                }
                                else
                                {
                                    Log.WriteLine("An internal server error has occured, please restart the application if you want to use this function (The overlay will work fine even withouth the server)");
                                }
                                ServerManager.Instance.IsServerOline = -1;
                            }
                        }
                        else
                        {
                            Log.WriteLine("Server appears to be offline. Please check your connection (The overlay will work fine even withouth the server)");
                            ServerManager.Instance.IsServerOline = -1;
                        }
                        ServerManager.Instance.ResetStats();
                    }, (error) =>
                    {
                        Log.WriteLine("An error has occured while performing the request, please restart the application if you want to use this function (The overlay will work fine even withouth the server)");
                        ServerManager.Instance.IsServerOline = -1;
                        ServerManager.Instance.ResetStats();
                    });
                })
            }));

            m_StateMachine.Add(State.CheckingForUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !ConfigHelper.Main.Values.Overlay.MonsterWidget.UseNetworkServer && !updater.CheckForUpdates(),
                    () =>
                {
                    Initialize();
                }),
                new StateMachine <State> .Transition(
                    State.DownloadingUpdates,
                    () => updater.CheckForUpdates(),
                    () =>
                {
                    Log.WriteLine("Starting to download Updates!");
                }),
                new StateMachine <State> .Transition(
                    State.ServerChecking,
                    () => ConfigHelper.Main.Values.Overlay.MonsterWidget.UseNetworkServer && !updater.CheckForUpdates(),
                    () =>
                {
                    Log.WriteLine("Checking Server...");
                    ServerManager.Instance.RequestCommadWithHandler(ServerManager.Command.ALIVE, null, null, false, 0, null, (result, ping) =>
                    {
                        if (result != null)
                        {
                            if (result["status"].ToString().Equals("ok"))
                            {
                                Log.WriteLine($"Server is online with response time of {ping} ms");
                                ServerManager.Instance.IsServerOline = 1;
                            }
                            else
                            {
                                if (result["result"].ToString().Equals("v"))
                                {
                                    Log.WriteLine("You are using an outdated version of SmartHunter please update it to use this feature");
                                }
                                else if (result["result"].ToString().Equals("dev"))
                                {
                                    Log.WriteLine("Server is under maintenance, a new version should be available soon");
                                }
                                else
                                {
                                    Log.WriteLine("An internal server error has occured, please restart the application if you want to use this function (The overlay will work fine even withouth the server)");
                                }
                                ServerManager.Instance.IsServerOline = -1;
                            }
                        }
                        else
                        {
                            Log.WriteLine("Server appears to be offline. Please check your connection (The overlay will work fine even withouth the server)");
                            ServerManager.Instance.IsServerOline = -1;
                        }
                        ServerManager.Instance.ResetStats();
                    }, (error) =>
                    {
                        Log.WriteLine("An error has occured while performing the request, please restart the application if you want to use this function (The overlay will work fine even withouth the server)");
                        ServerManager.Instance.IsServerOline = -1;
                        ServerManager.Instance.ResetStats();
                    });
                })
            }));

            m_StateMachine.Add(State.DownloadingUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Successfully downloaded all files!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Failed to download Updates... Resuming the normal flow of the application!");
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.Restarting, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => true,
                    () =>
                {
                    Log.WriteLine("Restarting Application!");
                    string update = ".\\SmartHunter_NEW.exe";
                    string exec   = Assembly.GetEntryAssembly()?.Location;
                    if (File.Exists(update) && exec != null && File.Exists(exec))
                    {
                        File.Move(exec, "SmartHunter_OLD.exe");
                        File.Move(update, "SmartHunter.exe");
                        Process.Start("SmartHunter.exe");
                    }
                    Environment.Exit(1);
                })
            }));

            m_StateMachine.Add(State.ServerChecking, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => ServerManager.Instance.IsServerOline != 0,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var processes = Process.GetProcesses();
                    foreach (var p in processes)
                    {
                        try
                        {
                            if (p != null && p.ProcessName.Equals(ProcessName) && !p.HasExited)
                            {
                                Process = p;
                                return(true);
                            }
                        }
                        catch
                        {
                            // nothing here
                        }
                    }
                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.FastPatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        if (pattern.Config.LastResultAddress.Length > 0)
                        {
                            if (MhwHelper.TryParseHex(pattern.Config.LastResultAddress, out var address))
                            {
                                var memoryScan = new ThreadedMemoryScan(Process, pattern, new AddressRange((ulong)address, (ulong)pattern.Bytes.Length), true, ThreadsPerScan);
                                m_FastMemoryScans.Add(memoryScan);
                            }
                        }
                    }
                })
            }));
Example #3
0
        void CreateStateMachine()
        {
            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => true,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var processes = Process.GetProcesses();
                    foreach (var p in processes)
                    {
                        try
                        {
                            if (p != null && p.ProcessName.Equals(ProcessName) && !p.HasExited)
                            {
                                Process = p;
                                return(true);
                            }
                        }
                        catch
                        {
                            // nothing here
                        }
                    }
                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.FastPatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        if (pattern.Config.LastResultAddress.Length > 0)
                        {
                            if (MhwHelper.TryParseHex(pattern.Config.LastResultAddress, out var address))
                            {
                                var memoryScan = new ThreadedMemoryScan(Process, pattern, new AddressRange((ulong)address, (ulong)pattern.Bytes.Length), true, ThreadsPerScan);
                                m_FastMemoryScans.Add(memoryScan);
                            }
                        }
                    }
                })
            }));
Example #4
0
        void CreateStateMachine()
        {
            var updater = new Updater();

            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.CheckingForUpdates,
                    () => ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Log.WriteLine("Searching for updates (You can disable this feature in file 'Config.json')!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.CheckingForUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.CheckForUpdates(),
                    () =>
                {
                    Initialize();
                }),
                new StateMachine <State> .Transition(
                    State.DownloadingUpdates,
                    () => updater.CheckForUpdates(),
                    () =>
                {
                    Log.WriteLine("Starting to download Updates!");
                })
            }));

            m_StateMachine.Add(State.DownloadingUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Successfully downloaded all files!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Failed to download Updates... Resuming the normal flow of the application!");
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.Restarting, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => true,
                    () =>
                {
                    Log.WriteLine("Restarting Application!");
                    string update = ".\\SmartHunter_NEW.exe";
                    string exec   = Assembly.GetEntryAssembly()?.Location;
                    if (File.Exists(update) && exec != null && File.Exists(exec))
                    {
                        File.Move(exec, "SmartHunter_OLD.exe");
                        File.Move(update, "SmartHunter.exe");
                        Process.Start("SmartHunter.exe");
                    }
                    Environment.Exit(1);
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var processes = Process.GetProcesses();
                    foreach (var p in processes)
                    {
                        try
                        {
                            if (p != null && p.ProcessName.Equals(ProcessName) && !p.HasExited)
                            {
                                Process = p;
                                return(true);
                            }
                        }
                        catch
                        {
                            // nothing here
                        }
                    }
                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.FastPatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        if (pattern.Config.LastResultAddress.Length > 0)
                        {
                            if (MhwHelper.TryParseHex(pattern.Config.LastResultAddress, out var address))
                            {
                                var memoryScan = new ThreadedMemoryScan(Process, pattern, new AddressRange((ulong)address, (ulong)pattern.Bytes.Length), true, ThreadsPerScan);
                                m_FastMemoryScans.Add(memoryScan);
                            }
                        }
                    }
                })
            }));
Example #5
0
        void CreateStateMachine()
        {
            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => true,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var process = Process.GetProcessesByName(ProcessName).FirstOrDefault();
                    if (process != null && !process.HasExited)
                    {
                        Process = process;
                        return(true);
                    }

                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.PatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        var memoryScan = new ThreadedMemoryScan(Process, ScanAddressRange, pattern, true, ThreadCount);
                        m_MemoryScans.Add(memoryScan);
                    }
                })
            }));

            m_StateMachine.Add(State.PatternScanning, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Working,
                    () =>
                {
                    var finishedWithResults = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted && memoryScan.Results.SelectMany(result => result.Matches).Any());
                    return(finishedWithResults.Count() == m_MemoryScans.Count);
                },
                    null),
                new StateMachine <State> .Transition(
                    State.PatternScanFailed,
                    () =>
                {
                    var finishedWithoutResults = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted && !memoryScan.Results.SelectMany(result => result.Matches).Any());
                    return(finishedWithoutResults.Any());
                },
                    null)
            }));

            m_StateMachine.Add(State.Working, new StateMachine <State> .StateData(
                                   () =>
            {
                try
                {
                    UpdateMemory();
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex);
                }
            },
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize();
                })
            }));
        }
Example #6
0
        void CreateStateMachine()
        {
            var updater = new Updater();

            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.CheckingForUpdates,
                    () => ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Log.WriteLine("Searching for updates (You can disable this feature in file 'Config.json')!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.CheckingForUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.CheckForUpdates(),
                    () =>
                {
                    Initialize();
                }),
                new StateMachine <State> .Transition(
                    State.DownloadingUpdates,
                    () => updater.CheckForUpdates(),
                    () =>
                {
                    Log.WriteLine("Starting to download Updates!");
                })
            }));

            m_StateMachine.Add(State.DownloadingUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Successfully downloaded all files!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Failed to download Updates... Resuming the normal flow of the application!");
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.Restarting, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => true,
                    () =>
                {
                    Log.WriteLine("Restarting Application!");
                    string file = $".\\SmartHunter_{ConfigHelper.Versions.Values.SmartHunter}.exe";
                    if (File.Exists(file))
                    {
                        Process.Start(file);
                    }
                    System.Environment.Exit(1);
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var processes = Process.GetProcesses();
                    foreach (var p in processes)
                    {
                        try
                        {
                            if (p != null && p.ProcessName.Equals(ProcessName) && !p.HasExited)
                            {
                                Process = p;
                                return(true);
                            }
                        }
                        catch
                        {
                            // nothing here
                        }
                    }
                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.PatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        var memoryScan = new ThreadedMemoryScan(Process, pattern, true, ThreadsPerScan);
                        m_MemoryScans.Add(memoryScan);
                    }
                })
            }));

            m_StateMachine.Add(State.PatternScanning, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Working,
                    () =>
                {
                    var finishedWithResults = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted && memoryScan.Results.SelectMany(result => result.Matches).Any());
                    return(finishedWithResults.Count() == m_MemoryScans.Count);
                },
                    () =>
                {
                    var orderedMatches = m_MemoryScans.Select(memoryScan => memoryScan.Results.Where(result => result.Matches.Any()).First().Matches.First()).OrderBy(match => match);
                    Log.WriteLine($"Match Range: {orderedMatches.First():X} - {orderedMatches.Last():X}");
                }),
                new StateMachine <State> .Transition(
                    State.PatternScanFailed,
                    () =>
                {
                    var completedScans = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted);
                    if (completedScans.Count() == m_MemoryScans.Count)
                    {
                        var finishedWithoutResults = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                        return(finishedWithoutResults.Any());
                    }

                    return(false);
                },
                    () =>
                {
                    var failedMemoryScans = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                    string failedPatterns = String.Join("\r\n", failedMemoryScans.Select(failedMemoryScan => failedMemoryScan.Pattern.Config.String));
                    Log.WriteLine($"Failed Patterns:\r\n{failedPatterns}");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));

            m_StateMachine.Add(State.Working, new StateMachine <State> .StateData(
                                   () =>
            {
                try
                {
                    UpdateMemory();
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex);
                }
            },
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));
        }
Example #7
0
        void CreateStateMachine()
        {
            var updater = new Updater();

            m_StateMachine = new StateMachine <State>();

            m_StateMachine.Add(State.None, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.CheckingForUpdates,
                    () => ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Log.WriteLine("Searching for updates (You can disable this feature in file 'Config.json')!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !ConfigHelper.Main.Values.AutomaticallyCheckAndDownloadUpdates,
                    () =>
                {
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.CheckingForUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.CheckForUpdates(),
                    () =>
                {
                    Initialize();
                }),
                new StateMachine <State> .Transition(
                    State.DownloadingUpdates,
                    () => updater.CheckForUpdates(),
                    () =>
                {
                    Log.WriteLine("Starting to download Updates!");
                })
            }));

            m_StateMachine.Add(State.DownloadingUpdates, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Successfully downloaded all files!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () => !updater.DownloadUpdates(),
                    () =>
                {
                    Log.WriteLine("Failed to download Updates... Resuming the normal flow of the application!");
                    Initialize();
                })
            }));

            m_StateMachine.Add(State.Restarting, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Restarting,
                    () => true,
                    () =>
                {
                    Log.WriteLine("Restarting Application!");
                    string update = ".\\SmartHunter_NEW.exe";
                    string exec   = Assembly.GetEntryAssembly()?.Location;
                    if (File.Exists(update) && exec != null && File.Exists(exec))
                    {
                        File.Move(exec, "SmartHunter_OLD.exe");
                        File.Move(update, "SmartHunter.exe");
                        Process.Start("SmartHunter.exe");
                    }
                    Environment.Exit(1);
                })
            }));

            m_StateMachine.Add(State.WaitingForProcess, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.ProcessFound,
                    () =>
                {
                    var processes = Process.GetProcesses();
                    foreach (var p in processes)
                    {
                        try
                        {
                            if (p != null && p.ProcessName.Equals(ProcessName) && !p.HasExited)
                            {
                                Process = p;
                                return(true);
                            }
                        }
                        catch
                        {
                            // nothing here
                        }
                    }
                    return(false);
                },
                    null)
            }));

            m_StateMachine.Add(State.ProcessFound, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.PatternScanning,
                    () => true,
                    () =>
                {
                    foreach (var pattern in Patterns)
                    {
                        var memoryScan = new ThreadedMemoryScan(Process, pattern, true, ThreadsPerScan);
                        m_MemoryScans.Add(memoryScan);
                    }
                })
            }));

            m_StateMachine.Add(State.PatternScanning, new StateMachine <State> .StateData(
                                   null,
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.Working,
                    () =>
                {
                    var completedScans = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted);
                    if (completedScans.Count() == m_MemoryScans.Count())
                    {
                        var finishedWithResults = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted && memoryScan.Results.SelectMany(result => result.Matches).Any());
                        return(finishedWithResults.Any());
                    }

                    return(false);
                },
                    () =>
                {
                    var failedMemoryScans = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                    if (failedMemoryScans.Any())
                    {
                        string failedPatterns = String.Join(" ", failedMemoryScans.Select(failedMemoryScan => failedMemoryScan.Pattern.Config.Name));
                        Log.WriteLine($"Failed Patterns [{failedMemoryScans.Count()}/{m_MemoryScans.Count()}]: {failedPatterns}");
                        Log.WriteLine($"The application will continue to work but with limited functionalities...");
                        m_MemoryScans.RemoveAll(scan => failedMemoryScans.Contains(scan));
                    }

                    var orderedMatches = m_MemoryScans.Select(memoryScan => memoryScan.Results.Where(result => result.Matches.Any()).First().Matches.First()).OrderBy(match => match);
                    Log.WriteLine($"Match Range: {orderedMatches.First():X} - {orderedMatches.Last():X}");
                }),
                new StateMachine <State> .Transition(
                    State.PatternScanFailed,
                    () =>
                {
                    var completedScans = m_MemoryScans.Where(memoryScan => memoryScan.HasCompleted);
                    if (completedScans.Count() == m_MemoryScans.Count())
                    {
                        var finishedWithoutResults = m_MemoryScans.Where(memoryScan => !memoryScan.Results.SelectMany(result => result.Matches).Any());
                        return(finishedWithoutResults.Count() == m_MemoryScans.Count());
                    }

                    return(false);
                },
                    () =>
                {
                    Log.WriteLine($"All pattern failed... Aborting!");
                }),
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));

            m_StateMachine.Add(State.Working, new StateMachine <State> .StateData(
                                   () =>
            {
                try
                {
                    UpdateMemory();
                }
                catch (Exception ex)
                {
                    Log.WriteException(ex);
                }
            },
                                   new StateMachine <State> .Transition[]
            {
                new StateMachine <State> .Transition(
                    State.WaitingForProcess,
                    () =>
                {
                    return(Process.HasExited);
                },
                    () =>
                {
                    Initialize(true);
                })
            }));
        }