Example #1
0
        /// <summary>
        /// Tries to retrieve TDU version by looking at Bnk1.map file size.
        /// </summary>
        /// <returns>TDU installed version or UNKNOWN value if not found.</returns>
        private static TduVersion _SearchForTduVersionFromMap()
        {
            TduVersion installedVersion = TduVersion.Unknown;

            // Locating Bnk1.map file
            string mapFileName = LibraryConstants.GetSpecialFile(LibraryConstants.TduSpecialFile.BnkMap);

            // DEBUG
            Log.Info("_SearchForTduVersionFromMap, Bnk1.map:" + mapFileName);

            // According to MAP size...
            FileInfo mapInfo = new FileInfo(mapFileName);

            if (mapInfo.Exists)
            {
                long mapSize = mapInfo.Length;

                Log.Info("Bnk1.map file found, size=" + mapSize);

                if (mapSize < MAP.SIZE_1_66_MAP)
                {
                    installedVersion = TduVersion.V1_45;
                }
                else if (mapSize < MAP.SIZE_1_66_MEGAPACK_MAP)
                {
                    installedVersion = TduVersion.V1_66;
                }
                else
                {
                    installedVersion = TduVersion.V1_66_Megapack;
                }
            }

            return(installedVersion);
        }
Example #2
0
        /// <summary>
        /// Tries to retrieve TDU version by seeking into Windows registry first, or by alternative ways if it fails.
        /// </summary>
        /// <returns>TDU installed version or Unknown value if not found.</returns>
        internal static TduVersion _SearchForTduVersion()
        {
            TduVersion installedVersion = TduVersion.Unknown;

            // Registry search
            try
            {
                RegistryKey key = Registry.LocalMachine;

                // 64bit OS support
                string keyName = (System64.Is64BitOs() ?
                                  _REGKEY_TDU_64
                               : _REGKEY_TDU_32);

                key = key.OpenSubKey(keyName);

                if (key == null)
                {
                    throw new Exception("TDU registry key not found under LOCAL_MACHINE: " + keyName);
                }

                string version = key.GetValue(_REGVALUE_TDU_GAME_VERSION) as string;

                if (version == null)
                {
                    throw new Exception("TDU version value not found: " + _REGVALUE_TDU_GAME_VERSION);
                }

                switch (version)
                {
                case _REGDATA_1_45_VERSION:
                    installedVersion = TduVersion.V1_45;
                    break;

                case _REGDATA_1_66_VERSION:
                    installedVersion = TduVersion.V1_66;
                    break;

                case _REGDATA_1_66_MEGAPACK_VERSION:
                    installedVersion = TduVersion.V1_66_Megapack;
                    break;

                default:
                    throw new Exception("Invalid version data in registry: " + installedVersion);
                }
            }
            catch (Exception ex)
            {
                // Silent
                Exception2.PrintStackTrace(new Exception(_ERROR_SEARCH_TDU_VERSION, ex));
            }

            // Alternative ways...
            if (installedVersion == TduVersion.Unknown)
            {
                Log.Info("Searching TDU version from Bnk1.map file size...");

                try
                {
                    // Searching for version according to Bnk1.map file size
                    installedVersion = _SearchForTduVersionFromMap();
                }
                catch (Exception ex)
                {
                    // Silent
                    Exception2.PrintStackTrace(new Exception(_ERROR_SEARCH_TDU_VERSION, ex));
                }
            }

            // If nothing works...
            if (installedVersion == TduVersion.Unknown)
            {
                Log.Warning(_ERROR_SEARCH_TDU_VERSION);
            }

            return(installedVersion);
        }
Example #3
0
        /// <summary>
        /// Repairs data into Windows registry (TDU install location and TDU version)
        /// </summary>
        /// <param name="tduInstallPath">Location where TDU is installed, e.g. C:\Program Files\Atari\Test Drive Unlimited</param>
        public static void FixRegistry(string tduInstallPath)
        {
            if (string.IsNullOrEmpty(tduInstallPath) || !Directory.Exists(tduInstallPath))
            {
                throw new Exception("Install path is invalid: " + tduInstallPath);
            }

            // Retrieving TDU version according to Bnk1.map file size
            TduVersion currentVersion = InstalledTduVersion;

            // Writing values into Windows registry
            try
            {
                string      version = "";
                RegistryKey key     = Registry.LocalMachine;

                // Main key
                // 64bit OS support
                string keyName = (System64.Is64BitOs() ?
                                  _REGKEY_TDU_64
                               : _REGKEY_TDU_32);

                key = key.OpenSubKey(keyName, true);

                if (key == null)
                {
                    // 64bit OS support
                    key = Registry.LocalMachine.CreateSubKey(keyName);

                    if (key == null)
                    {
                        throw new Exception("TDU registry key cannot be created: " + keyName);
                    }
                }

                // Install path
                key.SetValue(_REGVALUE_TDU_INSTALL_PATH, tduInstallPath);

                // TDU version
                switch (currentVersion)
                {
                case TduVersion.Unknown:
                case TduVersion.V1_45:
                    version = _REGDATA_1_45_VERSION;
                    break;

                case TduVersion.V1_66:
                    version = _REGDATA_1_66_VERSION;
                    break;

                case TduVersion.V1_66_Megapack:
                    version = _REGDATA_1_66_MEGAPACK_VERSION;
                    break;
                }

                key.SetValue(_REGVALUE_TDU_GAME_VERSION, version);
            }
            catch (SecurityException sEx)
            {
                throw new UnauthorizedAccessException("You don't have rights to fix registry for TDU.", sEx);
            }
            catch (UnauthorizedAccessException uaEx)
            {
                throw new UnauthorizedAccessException("You don't have rights to fix registry for TDU.", uaEx);
            }
            catch (Exception ex)
            {
                throw new Exception("Internal error: unable to fix registry for TDU.", ex);
            }
        }