Пример #1
0
        public static async Task Main()
        {
            // 把你要连接到的 mirai-api-http 所需的主机名/IP, 端口 和 AuthKey 全部填好
            // !! 最好不要用我例子里边的 key 和 端口, 请自己生成一个, 比如 System.Guid.NewGuid().ToString("n") !!
            MiraiHttpSessionOptions options = new MiraiHttpSessionOptions("127.0.0.1", 8080, "INITKEYVCo5ktPW");

            // session 使用 DisposeAsync 模式, 所以使用 await using 自动调用 DisposeAsync 方法。
            // 你也可以不在这里 await using, 不过使用完 session 后请务必调用 DisposeAsync 方法
            await using MiraiHttpSession session = new MiraiHttpSession();
            // 把你实现了 Mirai_CSharp.Plugin.Interfaces 下的接口的类给 new 出来, 然后作为插件塞给 session
            GamePlugin plugin = new GamePlugin();

            // 你也可以一个个绑定事件。比如 session.GroupMessageEvt += plugin.GroupMessage;
            // 手动绑定事件后不要再调用AddPlugin, 否则可能导致重复调用
            session.AddPlugin(plugin);
            // 使用上边提供的信息异步连接到 mirai-api-http
            await session.ConnectAsync(options, 1018429593); // 自己填机器人QQ号

            while (true)
            {
                if (await Console.In.ReadLineAsync() == "exit")
                {
                    return;
                }
            }
        }
Пример #2
0
        private PluginExporterDialog(GamePlugin gamePlugin, EditorPlugin editorPlugin)
            : base("Export Plugin")
        {
            const float TotalWidth = 720;

            Width = TotalWidth;

            _options = new ExportOptions(gamePlugin, editorPlugin);

            // Header and help description
            var headerLabel = new Label(0, 0, TotalWidth, 40)
            {
                Text      = "Export plugin " + _options.Description.Name,
                DockStyle = DockStyle.Top,
                Parent    = this,
                Font      = new FontReference(Style.Current.FontTitle)
            };
            var infoLabel = new Label(10, headerLabel.Bottom + 5, TotalWidth - 20, 40)
            {
                Text = "Specify options for exporting plugin. To learn more about it see the online documentation.",
                HorizontalAlignment = TextAlignment.Near,
                Margin    = new Margin(7),
                DockStyle = DockStyle.Top,
                Parent    = this
            };

            // Buttons
            const float ButtonsWidth  = 60;
            const float ButtonsMargin = 8;
            var         exportButton  = new Button(TotalWidth - ButtonsMargin - ButtonsWidth, infoLabel.Bottom - 30, ButtonsWidth)
            {
                Text        = "Export",
                AnchorStyle = AnchorStyle.UpperRight,
                Parent      = this
            };

            exportButton.Clicked += OnExport;
            var cancelButton = new Button(exportButton.Left - ButtonsMargin - ButtonsWidth, exportButton.Y, ButtonsWidth)
            {
                Text        = "Cancel",
                AnchorStyle = AnchorStyle.UpperRight,
                Parent      = this
            };

            cancelButton.Clicked += OnCancel;

            // Settings editor
            var editor = new CustomEditorPresenter(null);

            editor.Panel.DockStyle = DockStyle.Fill;
            editor.Panel.Parent    = this;

            editor.Select(_options);

            Size = new Vector2(TotalWidth, 300);
        }
Пример #3
0
        protected override void Loaded(LoadedEventArgs loadedArgs)
        {
            base.Loaded(loadedArgs);

            _game = PluginManager.GetPluginByType <GamePlugin>();

            WriteEvent("Connecting to " + MasterIpAddress + ":" + MasterPort, LogType.Info);
            _client.MessageReceived += OnMessageFromMaster;
            _client.ConnectInBackground(MasterIpAddress, MasterPort, IPVersion.IPv4, OnConnectedToMaster);

            ClientManager.ClientConnected += OnPlayerConnected;
        }
Пример #4
0
            /// <summary>
            /// Initializes a new instance of the <see cref="ExportOptions"/> class.
            /// </summary>
            /// <param name="gamePlugin">The game plugin.</param>
            /// <param name="editorPlugin">The editor plugin.</param>
            public ExportOptions(GamePlugin gamePlugin, EditorPlugin editorPlugin)
            {
                if (gamePlugin == null && editorPlugin == null)
                {
                    throw new ArgumentException();
                }

                GamePlugin   = gamePlugin;
                EditorPlugin = editorPlugin;
                Description  = gamePlugin?.Description ?? editorPlugin.Description;
                ShortName    = ((Plugin)gamePlugin ?? editorPlugin).GetType().Name;
            }
Пример #5
0
        public MenuScreen(GamePlugin gamePlugin, MenuPlugin menuPlugin)
        {
            _menuPlugin = menuPlugin ?? throw new ArgumentNullException(nameof(menuPlugin));

            Title = gamePlugin.Details.Name;

            InitializeComponent();

            foreach (var item in menuPlugin.MenuItems)
            {
                var button = new Button {
                    Content             = item.Text,
                    HorizontalAlignment = HorizontalAlignment.Stretch
                };

                button.Click += async(sender, args) => {
                    this.IsEnabled = false;
                    try {
                        await Task.Delay(200);

                        var context = new ClickContext(gamePlugin);

                        await item.OnClick(context);
                    } catch (Exception ex) {
                        //TODO: Shutodwn error
                    } finally {
                        this.IsEnabled = true;
                    }
                };

                ButtonList.Items.Add(button);
            }

            //var converter = new BrushConverter();
            //var brush = (Brush)converter.ConvertFromString(plugin.Theme.Background);

            //this.Background = brush;
        }
Пример #6
0
        /// <summary>
        /// Gets the plugin to export (editor or game or both). Searches the game scripts assemblies only. Performs validation.
        /// </summary>
        /// <param name="gamePlugin">The game plugin.</param>
        /// <param name="editorPlugin">The editor plugin.</param>
        /// <param name="errorMsg">If searching fails, then it contains a result message with error information. Can be used to inform user about the actual problem.</param>
        /// <returns>True if found plugin is valid, otherwise false.</returns>
        public static bool GetPluginToExport(out GamePlugin gamePlugin, out EditorPlugin editorPlugin, out string errorMsg)
        {
            // Init
            gamePlugin   = null;
            editorPlugin = null;

            // Cache data
            var allAssemblies           = AppDomain.CurrentDomain.GetAssemblies();
            var gameAssembly            = Utils.GetAssemblyByName("Assembly", allAssemblies);
            var gameEditorAssembly      = Utils.GetAssemblyByName("Assembly.Editor", allAssemblies);
            var gameAssemblyTypes       = gameAssembly.GetTypes();
            var gameEditorAssemblyTypes = gameEditorAssembly.GetTypes();

            // Get those plugins
            var gamePluginType   = FirstOfSubType(gameAssemblyTypes, typeof(GamePlugin));
            var editorPluginType = FirstOfSubType(gameEditorAssemblyTypes, typeof(EditorPlugin));

            if (gamePluginType == null && editorPluginType == null)
            {
                errorMsg = "Cannot find any plugins in game scripts to export.";
                return(false);
            }

            // Count plugins (assembly can contain only one plugin)
            if (CountSubTypes(gameAssemblyTypes, typeof(GamePlugin)) > 1)
            {
                errorMsg = "Game assembly can contain only one GamePlugin implementation to export it.";
                return(false);
            }
            if (CountSubTypes(gameAssemblyTypes, typeof(EditorPlugin)) != 0)
            {
                errorMsg = "Game assembly cannot contain any EditorPlugin implementation";
                return(false);
            }
            if (CountSubTypes(gameEditorAssemblyTypes, typeof(EditorPlugin)) > 1)
            {
                errorMsg = "Editor assembly can contain only one EditorPlugin implementation to export it.";
                return(false);
            }
            if (CountSubTypes(gameEditorAssemblyTypes, typeof(GamePlugin)) != 0)
            {
                errorMsg = "Editor assembly cannot contain any GamePlugin implementation";
                return(false);
            }

            // Create objects
            try
            {
                if (gamePluginType != null)
                {
                    gamePlugin = (GamePlugin)Activator.CreateInstance(gamePluginType);
                }
                if (editorPluginType != null)
                {
                    editorPlugin = (EditorPlugin)Activator.CreateInstance(editorPluginType);
                }
            }
            catch (Exception ex)
            {
                Editor.LogWarning(ex);
                errorMsg = "Failed to create plugin objects. See log to learn more.";
                return(false);
            }

            // Validate relation
            if (gamePlugin != null && editorPlugin != null)
            {
                if (editorPlugin.GamePluginType != gamePluginType)
                {
                    errorMsg = "Cannot export game and editor plugins because editor plugin is not specifying game plugin type with GamePluginType property.";
                    return(false);
                }
            }

            errorMsg = null;
            return(true);
        }
Пример #7
0
 public ClickContext(GamePlugin gamePlugin)
 {
     GamePlugin = gamePlugin ?? throw new ArgumentNullException(nameof(gamePlugin));
 }
Пример #8
0
        private PluginExporterDialog(GamePlugin gamePlugin, EditorPlugin editorPlugin)
            : base("Export Plugin")
        {
            const float TotalWidth = 720;

            Width = TotalWidth;

            _options = new ExportOptions(gamePlugin, editorPlugin);

            // Header and help description
            var headerLabel = new Label
            {
                Text         = "Export plugin " + _options.Description.Name,
                AnchorPreset = AnchorPresets.HorizontalStretchTop,
                Offsets      = new Margin(0, 0, 0, 40),
                Parent       = this,
                Font         = new FontReference(Style.Current.FontTitle)
            };
            var infoLabel = new Label
            {
                Text = "Specify options for exporting plugin. To learn more about it see the online documentation.",
                HorizontalAlignment = TextAlignment.Near,
                Margin       = new Margin(7),
                AnchorPreset = AnchorPresets.HorizontalStretchTop,
                Offsets      = new Margin(10, -20, 45, 70),
                Parent       = this
            };

            // Buttons
            const float ButtonsWidth  = 60;
            const float ButtonsHeight = 24;
            const float ButtonsMargin = 8;
            var         exportButton  = new Button
            {
                Text         = "Export",
                AnchorPreset = AnchorPresets.BottomRight,
                Offsets      = new Margin(-ButtonsWidth - ButtonsMargin, ButtonsWidth, -ButtonsHeight - ButtonsMargin, ButtonsHeight),
                Parent       = this
            };

            exportButton.Clicked += OnExport;
            var cancelButton = new Button
            {
                Text         = "Cancel",
                AnchorPreset = AnchorPresets.BottomRight,
                Offsets      = new Margin(-ButtonsWidth - ButtonsMargin - ButtonsWidth - ButtonsMargin, ButtonsWidth, -ButtonsHeight - ButtonsMargin, ButtonsHeight),
                Parent       = this
            };

            cancelButton.Clicked += OnCancel;

            // Settings editor
            var editor = new CustomEditorPresenter(null);

            editor.Panel.AnchorPreset = AnchorPresets.StretchAll;
            editor.Panel.Offsets      = new Margin(2, 2, infoLabel.Bottom + 2, ButtonsHeight + ButtonsMargin + ButtonsMargin);
            editor.Panel.Parent       = this;

            editor.Select(_options);

            Size = _dialogSize = new Vector2(TotalWidth, 300);
        }