BF2Statistics.Redirector.Initialize C# (CSharp) Method

Initialize() public static method

The main entry point for the redirector
public static Initialize ( ) : Task
return Task
        public static Task<bool> Initialize()
        {
            return Task.Run<bool>(() =>
            {
                // Only initialize once
                if (!IsInitialized)
                {
                    IsInitialized = true;

                    // Set the System.Net DNS Cache refresh timeout to 1 millisecond
                    ServicePointManager.DnsRefreshTimeout = 1;

                    // Get config options
                    RedirectMethod = Program.Config.RedirectMode;

                    // Create new Instances
                    HostsFileSys = new SysHostsFile();
                    HostsFileIcs = new HostsFileIcs();

                    // Detect redirects
                    bool IcsHasRedirects = HostsFileIcs.HasAnyEntry(GamespyHosts) || HostsFileIcs.HasEntry(Bf2StatsHost);
                    bool HostsHasRedirects = HostsFileSys.HasAnyEntry(GamespyHosts) || HostsFileSys.HasEntry(Bf2StatsHost);

                    // Both files cannot have redirects!
                    if (IcsHasRedirects && HostsHasRedirects)
                    {
                        try
                        {
                            // Remove all redirects
                            RemoveRedirects(HostsFileIcs);
                            RemoveRedirects(HostsFileSys);
                        }
                        catch { }
                    }
                    else if (IcsHasRedirects || HostsHasRedirects)
                    {
                        // Do the settings match?
                        bool match = (RedirectMethod == RedirectMode.HostsFile && HostsHasRedirects)
                            || (RedirectMethod == RedirectMode.HostsIcsFile && IcsHasRedirects);

                        // Perform switch if the settings don't match
                        if (!match)
                            RedirectMethod = (RedirectMethod == RedirectMode.HostsFile) ? RedirectMode.HostsIcsFile : RedirectMode.HostsFile;

                        // === Fetch our redirect data

                        // Grab hosts file base
                        HostsFile hFile = (RedirectMethod == RedirectMode.HostsFile)
                            ? HostsFileSys as HostsFile
                            : HostsFileIcs as HostsFile;

                        // Parse Stats Address
                        if (hFile.HasEntry("bf2web.gamespy.com"))
                            StatsServerAddress = hFile.Get("bf2web.gamespy.com");

                        // Parse Gamespy Address
                        if (hFile.HasEntry("master.gamespy.com"))
                            GamespyServerAddress = hFile.Get("master.gamespy.com");

                        // Lock hosts file
                        if (RedirectMethod == RedirectMode.HostsFile && Program.Config.LockHostsFile)
                            HostsFileSys.Lock();

                    }
                    else if (RedirectMethod == RedirectMode.DnsServer)
                    {
                        IPAddress addy;

                        // Parse last used Stats Server Address
                        if (Networking.TryGetIpAddress(Program.Config.LastStatsServerAddress, out addy))
                            StatsServerAddress = addy;

                        // Parse gamespy saved address
                        if (Networking.TryGetIpAddress(Program.Config.LastLoginServerAddress, out addy))
                            GamespyServerAddress = addy;
                    }
                }

                // Verify cache
                return (RedirectsEnabled) ? VerifyDNSCache() : true;
            });
        }