示例#1
0
        private IEnumerable <CommandUpdateInfo <ICommand <TAppSession, TRequestInfo> > > UpdateCommands <TAppSession, TRequestInfo>(IAppServer appServer, IEnumerable <CommandUpdateInfo <CommandFileInfo> > updatedCommands)
            where TAppSession : IAppSession, IAppSession <TAppSession, TRequestInfo>, new()
            where TRequestInfo : IRequestInfo
        {
            return(updatedCommands.Select(c =>
            {
                if (c.UpdateAction == CommandUpdateAction.Remove)
                {
                    return new CommandUpdateInfo <ICommand <TAppSession, TRequestInfo> >
                    {
                        Command = new MockupCommand <TAppSession, TRequestInfo>(Path.GetFileNameWithoutExtension(c.Command.FilePath)),
                        UpdateAction = c.UpdateAction
                    };
                }

                try
                {
                    var command = new DynamicCommand <TAppSession, TRequestInfo>(m_ScriptRuntime, c.Command.FilePath, c.Command.LastUpdatedTime);

                    return new CommandUpdateInfo <ICommand <TAppSession, TRequestInfo> >
                    {
                        Command = command,
                        UpdateAction = c.UpdateAction
                    };
                }
                catch (Exception e)
                {
                    if (appServer.Logger.IsErrorEnabled)
                    {
                        appServer.Logger.Error("Failed to load command file: " + c.Command.FilePath + "!", e);
                    }
                    return null;
                }
            }));
        }
示例#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);
        }