Exemplo n.º 1
0
        internal static void Load(Assembly[] assembly)
        {
            clicmd            = new ClientCommand();
            contents          = new List <ExecutionContent>();
            mainThreadActions = new List <Key <Action, ExecutionContent> >();

            for (int i = 0; i < assembly.Length; i++)
            {
                Type[] types = assembly[i].GetTypes();
                for (int j = 0; j < types.Length; j++)
                {
                    if (types[j].IsSubclassOf(typeof(ExecutionContent)))
                    {
                        ExecutionContent content = (ExecutionContent)Activator.CreateInstance(types[j]);
                        content.Name    = types[j].FullName;
                        content.ExtName = Path.GetFileName(assembly[i].Location);
                        content.mdHash  = Program.assembliesMd5[i];
                        if (content.Name == Program.sudoExecutionContent)
                        {
                            content.canSudo = true;
                        }
                        else
                        {
                            for (int k = 0; k < ServerVersion.AllowedEXTs.Count; k++)
                            {
                                if (Program.assembliesMd5[i] == ServerVersion.AllowedEXTs[k])
                                {
                                    content.canSudo = true;
                                    break;
                                }
                            }
                        }
                        contents.Add(content);
                    }
                }
            }

            for (int i = 0; i < contents.Count; i++)
            {
                try
                {
                    contents[i].Init();
                    contents[i].canSudo = false;
                }
                catch (Exception e)
                {
                    if (e is CannotLoadExecutionContentException)
                    {
                        Logger.WriteLine(LogLevel.Default, "已禁用执行体 {0}", contents[i].Name);
                    }
                    else
                    {
                        Logger.WriteLine(LogLevel.Warning, "在初始化执行体 {0} 时引发异常,需要禁用这个执行体", contents[i].Name);
                        Logger.WriteLine(LogLevel.Warning, e.ToString());
                    }
                    contents.RemoveAt(i);
                    i--;
                }
            }

            for (int i = 0; i < contents.Count; i++)
            {
                try
                {
                    contents[i].InitMutual();
                    if (contents[i].MutualState != MutualExecuteContentState.Undef)
                    {
                        contents[i].MutualState = MutualExecuteContentState.Inited;
                    }
                }
                catch (Exception e)
                {
                    Logger.WriteLine(LogLevel.Warning, "在初始化执行体交互 {0} 时引发异常", contents[i].Name);
                    Logger.WriteLine(LogLevel.Warning, e.ToString());
                }
            }

            for (int i = 0; i < contents.Count; i++)
            {
                ExecutionContent execution = contents[i];
                Thread           thread    = new Thread(() =>
                {
                    Exception exception = null;
                    while (true)
                    {
                        try
                        {
                            execution.MainLoop();
                            if (!execution.useLoop)
                            {
                                break;
                            }
                        }
                        catch (Exception ex)
                        {
                            exception = ex;
                            break;
                        }
                        Thread.Sleep(33);
                    }
                    if (exception == null)
                    {
                        return;
                    }
                    Logger.WriteLine(LogLevel.Warning, "在执行体 {0} 执行 MainLoop 时中断", execution.Name);
                    Logger.WriteLine(LogLevel.Warning, exception.ToString());
                });
                thread.IsBackground = true;
                thread.Start();
            }

            firstTicker = DateTime.Now;
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.AutoReset = true;
            timer.Interval  = 100;
            timer.Elapsed  += OnTick;
            timer.Start();
        }