Exemple #1
0
        // *** CONSTRUCTION *** //

        #region Constructor
        public Manager()
        {
            #region Create Member Objects
            GameClasses                 = new Dictionary <string, Type>();
            GameAttributes              = new Dictionary <string, GameAttribute>();
            AppearanceAttributes        = new Dictionary <string, AppearanceAttribute>();
            Environment                 = new Compiler.Environment();
            PieceTypeLibrary            = new PieceTypeLibrary();
            EngineLibrary               = new EngineLibrary();
            InternalEngine              = new EngineConfiguration();
            InternalEngine.FriendlyName = "ChessV";
            InternalEngine.InternalName = "ChessV 2.2 RC1";
            #endregion

            #region Add ChessV Data Types to Environment
            Environment.AddSymbol("MoveCapability", typeof(MoveCapability));
            Environment.AddSymbol("Direction", typeof(Direction));
            Environment.AddSymbol("BitBoard", typeof(BitBoard));
            Environment.AddSymbol("Rule", typeof(Rule));
            Environment.AddSymbol("Game", typeof(Game));
            Environment.AddSymbol("PieceType", typeof(PieceType));
            Environment.AddSymbol("Piece", typeof(Piece));
            Environment.AddSymbol("Location", typeof(Location));
            #endregion

            #region Load Internal Games

            // *** LOAD INTERNAL GAMES *** //

            //	Load games and piece types from the main ChessV.Base module
            Module module = typeof(Game).Module;
            loadPieceTypesFromModule(module);
            loadGamesFromModule(module);

            //	Load games and piece types from the ChessV.Games DLL
            string   moduleName    = module.FullyQualifiedName;
            string   modulePath    = moduleName.Substring(0, Math.Max(moduleName.LastIndexOf('\\'), moduleName.LastIndexOf('/')) + 1);
            string   gamesDllName  = modulePath + "ChessV.Games.dll";
            Assembly gamesAssembly = Assembly.UnsafeLoadFrom(gamesDllName);
            foreach (Module gamesModule in gamesAssembly.GetModules())
            {
                loadPieceTypesFromModule((Module)gamesModule);
                loadGamesFromModule((Module)gamesModule);
                loadPieceTypePropertyAttributesFromModule((Module)gamesModule);
                loadRulesFromModule((Module)gamesModule);
                loadEvaluationsFromModule((Module)gamesModule);
            }
            #endregion

            #region Load Games from Include Folder

            // *** LOAD GAMES FROM INCLUDE FOLDER *** //

            //	Search for the include folder.  We provide some flexibility
            //	regarding where this path is located
            string currPath    = Directory.GetCurrentDirectory();
            string includePath = Path.Combine(currPath, "Include");
            if (!Directory.Exists(includePath))
            {
                int iIndex = currPath.LastIndexOf("ChessV");
                if (iIndex > 0)
                {
                    iIndex = currPath.IndexOf(Path.DirectorySeparatorChar, iIndex);
                    if (iIndex > 0)
                    {
                        currPath    = currPath.Remove(iIndex);
                        includePath = Path.Combine(currPath, "Include");
                        if (!Directory.Exists(includePath))
                        {
                            currPath = Directory.GetCurrentDirectory();
                            iIndex   = currPath.IndexOf("ChessV");
                            if (iIndex > 0)
                            {
                                iIndex = currPath.IndexOf(Path.DirectorySeparatorChar, iIndex);
                                if (iIndex > 0)
                                {
                                    currPath    = currPath.Remove(iIndex);
                                    includePath = Path.Combine(currPath, "Include");
                                }
                            }
                        }
                    }
                }
            }

            if (Directory.Exists(includePath))
            {
                AppDomain    myDomain     = Thread.GetDomain();
                AssemblyName assemblyName = new AssemblyName();
                assemblyName.Name = "DynamicGamesAssembly";
                System.Reflection.Emit.AssemblyBuilder assemblyBuilder = myDomain.DefineDynamicAssembly(assemblyName, System.Reflection.Emit.AssemblyBuilderAccess.RunAndSave);
                System.Reflection.Emit.ModuleBuilder   dynamicModule   = assemblyBuilder.DefineDynamicModule("ChessVDynamicGames");
                Compiler.Compiler compiler     = new Compiler.Compiler(assemblyBuilder, dynamicModule, Environment);
                string[]          includeFiles = Directory.GetFiles(includePath, "*.cvc");
                foreach (string file in includeFiles)
                {
                    TextReader reader = new StreamReader(file);
                    compiler.ProcessInput(reader);
                    reader.Close();
                }
                foreach (KeyValuePair <string, Type> pair in compiler.GameTypes)
                {
                    GameClasses.Add(pair.Key, pair.Value);
                    GameAttributes.Add(pair.Key, compiler.GameAttributes[pair.Key]);
                    if (compiler.GameAppearances.ContainsKey(pair.Key))
                    {
                        AppearanceAttributes.Add(pair.Key, compiler.GameAppearances[pair.Key]);
                    }
                }
            }
            #endregion
        }