コード例 #1
0
        /// <summary>
        /// Tries to load commands.
        /// </summary>
        /// <param name="commands">The commands.</param>
        /// <returns></returns>
        public override bool TryLoadCommands(out IEnumerable <ICommand> commands)
        {
            var outputCommands = new List <ICommand>();

            if (m_ServerCommandState != null)
            {
                OnError("This server's commands have been loaded already!");
                commands = outputCommands;
                return(false);
            }

            var serverCommandState = new ServerCommandState
            {
                CommandUpdater = (o) => OnUpdated(GetUpdatedCommands(o)),
                Sources        = new List <IScriptSource>()
            };

            m_ServerCommandState = serverCommandState;

            var scriptSources = GetScriptSources(m_RootConfig, m_AppServer);

            foreach (var source in scriptSources)
            {
                ICommand command;

                try
                {
                    command = (ICommand)Activator.CreateInstance(m_DynamicCommandType, ScriptRuntime, source);
                    serverCommandState.Sources.Add(source);
                    outputCommands.Add(command);
                }
                catch (Exception e)
                {
                    OnError(new Exception("Failed to load command source: " + source.Tag + "!", e));
                }
            }

            commands = outputCommands;

            return(true);
        }
コード例 #2
0
        /// <summary>
        /// Loads the commands.
        /// </summary>
        /// <typeparam name="TAppSession">The type of the app session.</typeparam>
        /// <typeparam name="TRequestInfo">The type of the request info.</typeparam>
        /// <param name="appServer">The app server.</param>
        /// <param name="commandRegister">The command register.</param>
        /// <param name="commandUpdater">The command updater.</param>
        /// <returns></returns>
        public bool LoadCommands <TAppSession, TRequestInfo>(IAppServer appServer, Func <ICommand <TAppSession, TRequestInfo>, bool> commandRegister, Action <IEnumerable <CommandUpdateInfo <ICommand <TAppSession, TRequestInfo> > > > commandUpdater)
            where TAppSession : IAppSession, IAppSession <TAppSession, TRequestInfo>, new()
            where TRequestInfo : IRequestInfo
        {
            if (m_ServerCommandStateLib.ContainsKey(appServer.Name))
            {
                throw new Exception("This server's commands have been loaded already!");
            }

            ServerCommandState serverCommandState = new ServerCommandState
            {
                CommandUpdater = (o) =>
                {
                    commandUpdater(UpdateCommands <TAppSession, TRequestInfo>(appServer, o));
                },
                Commands = new List <CommandFileInfo>()
            };


            var commandDir       = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Command");
            var serverCommandDir = Path.Combine(commandDir, appServer.Name);

            if (!Directory.Exists(commandDir))
            {
                return(true);
            }

            List <string> commandFiles = new List <string>();

            commandFiles.AddRange(GetCommandFiles(commandDir, SearchOption.TopDirectoryOnly));

            if (Directory.Exists(serverCommandDir))
            {
                commandFiles.AddRange(GetCommandFiles(serverCommandDir, SearchOption.TopDirectoryOnly));
            }

            if (!commandFiles.Any())
            {
                return(true);
            }

            foreach (var file in commandFiles)
            {
                DynamicCommand <TAppSession, TRequestInfo> command;

                try
                {
                    var lastUpdatedTime = File.GetLastWriteTime(file);
                    command = new DynamicCommand <TAppSession, TRequestInfo>(m_ScriptRuntime, file, lastUpdatedTime);
                    serverCommandState.Commands.Add(new CommandFileInfo
                    {
                        FilePath        = file,
                        LastUpdatedTime = lastUpdatedTime
                    });

                    if (!commandRegister(command))
                    {
                        return(false);
                    }
                }
                catch (Exception e)
                {
                    throw new Exception("Failed to load command file: " + file + "!", e);
                }
            }

            m_ServerCommandStateLib.Add(appServer.Name, serverCommandState);

            return(true);
        }