Ejemplo n.º 1
0
        /// <summary>
        /// Loads the game information.
        /// </summary>
        private static void LoadGameInfo()
        {
            Main.Log.AppendLine("Loading game library.");
            List <FileInfo> found = new List <FileInfo>();
            DirectoryInfo   dir   = new DirectoryInfo(FrameworkPath);
            var             files = dir.GetFiles();

            foreach (var x in files)
            {
                string fileName = x.Name;
                if (!fileName.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) || !fileName.StartsWith(Main.FrameworkName + ".", StringComparison.OrdinalIgnoreCase) || fileName.Equals(Main.FrameworkName + ".dll", StringComparison.OrdinalIgnoreCase) || fileName.EndsWith(".implementations.dll", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                fileName = fileName.Substring(Main.FrameworkName.Length + 1);
                fileName = fileName.Substring(0, fileName.Length - 4);

                if (fileName.Length == 0 || fileName.Equals("Runtime", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                found.Add(x);
            }

            if (found.Count == 0)
            {
                Main.Log.AppendLine("No game library DLL found! Game definitions will not be loaded but plugins may still load.");
                return;
            }

            if (found.Count > 1)
            {
                string fstr = string.Join(", ", found.Select(q => q.Name));
                throw new InvalidOperationException("Found more than one game library DLL! Only one game library may be active at a time. [" + fstr + "]");
            }

            {
                var    versionFile     = found[0];
                string versionFileName = versionFile.Name;
                int    ptIdx           = versionFileName.LastIndexOf('.');
                if (ptIdx >= 0)
                {
                    string verss = string.Join("_", Memory.GetMainModuleVersion());
                    versionFileName    = versionFileName.Substring(0, ptIdx) + "." + verss + ".bin";
                    VersionLibraryFile = new FileInfo(System.IO.Path.Combine(versionFile.DirectoryName, versionFileName));
                    InitializeVersionInfo();
                }
            }

            System.Reflection.Assembly assembly = null;
            Loader.Load(found[0], ref assembly);
            if (assembly == null)
            {
                throw new InvalidOperationException();
            }

            var  types = assembly.GetTypes();
            Type valid = null;

            foreach (var t in types)
            {
                if (!t.IsSubclassOf(typeof(Game)))
                {
                    continue;
                }

                if (t.IsAbstract)
                {
                    continue;
                }

                if (t.BaseType != typeof(Game))
                {
                    continue;
                }

                if (valid != null)
                {
                    throw new InvalidOperationException("Found more than one valid game header type!");
                }

                valid = t;
            }

            if (valid == null)
            {
                throw new InvalidOperationException("Found game library but no valid game header type!");
            }

            GameCreate = 1;
            Main._is_initializing_plugin += 2;
            try
            {
                Game   result = (Game)Activator.CreateInstance(valid);
                string print  = "`" + result.FullName + "` (" + result.LibraryVersion + ")";
                Main.Log.AppendLine("Loaded game library for " + print + ".");
                Main.Log.AppendLine("Running game version is " + string.Join(".", result.GameVersion));
                string supportedExecutable = result.ExecutableName;
                string haveExecutable      = System.Diagnostics.Process.GetCurrentProcess().MainModule.ModuleName;
                if (!haveExecutable.Equals(supportedExecutable, StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException("Game library " + print + " expected game executable `" + supportedExecutable + "` but have `" + haveExecutable + "`!");
                }
                if (!result.IsValidVersion)
                {
                    throw new InvalidOperationException("Game library " + print + " does not support game version " + string.Join(".", result.GameVersion) + "!");
                }

                Main.Game = result;
                ValidateVersionLibrary();

                if (VersionLibraryError != null)
                {
                    throw new InvalidOperationException("Version library error: " + VersionLibraryError + "!");
                }
                if (Main.GameInfo == null && result.VersionLibraryHash != 0)
                {
                    throw new InvalidOperationException("Game library " + print + " requires a version library but one is not loaded!");
                }

                result._initialize();
            }
            finally
            {
                Main._is_initializing_plugin -= 2;
            }
        }