Ejemplo n.º 1
0
        /// <summary>
        /// プラグインが有効化されたときに呼び出されます。
        /// </summary>
        /// <param name="pluginScreenSpace"></param>
        /// <param name="pluginStatusText"></param>
        public void InitPlugin(TabPage pluginScreenSpace, Label pluginStatusText)
        {
            try
            {
                this.tabPage = pluginScreenSpace;
                this.label   = pluginStatusText;

#if DEBUG
                Logger.Log(LogLevel.Warning, "##################################");
                Logger.Log(LogLevel.Warning, "    THIS IS THE DEBUG BUILD");
                Logger.Log(LogLevel.Warning, "##################################");
#endif

                Logger.Log(LogLevel.Info, "InitPlugin: PluginDirectory = {0}", PluginDirectory);

#if DEBUG
                Stopwatch watch = new Stopwatch();
                watch.Start();
#endif

                // ** Init phase 1
                // Only init stuff here that works without the FFXIV plugin or addons (event sources, overlays).
                // Everything else should be initialized in the second phase.
                NativeMethods.Init();
                FFXIVExportVariables.Init();
                EventDispatcher.Init();
                Registry.Register(new KeyboardHook());
                LoadConfig();

#if DEBUG
                Logger.Log(LogLevel.Debug, "Component init and config load took {0}s.", watch.Elapsed.TotalSeconds);
                watch.Reset();
#endif

                try
                {
                    Renderer.Initialize(PluginDirectory, ActGlobals.oFormActMain.AppDataFolder.FullName, Config.ErrorReports);
                }
                catch (Exception e)
                {
                    Logger.Log(LogLevel.Error, "InitPlugin: {0}", e);
                }

#if DEBUG
                Logger.Log(LogLevel.Debug, "CEF init took {0}s.", watch.Elapsed.TotalSeconds);
                watch.Reset();
#endif

                // プラグイン間のメッセージ関連
                OverlayApi.BroadcastMessage += (o, e) =>
                {
                    Task.Run(() =>
                    {
                        foreach (var overlay in this.Overlays)
                        {
                            overlay.SendMessage(e.Message);
                        }
                    });
                };
                OverlayApi.SendMessage += (o, e) =>
                {
                    Task.Run(() =>
                    {
                        var targetOverlay = this.Overlays.FirstOrDefault(x => x.Name == e.Target);
                        if (targetOverlay != null)
                        {
                            targetOverlay.SendMessage(e.Message);
                        }
                    });
                };
                OverlayApi.OverlayMessage += (o, e) =>
                {
                    Task.Run(() =>
                    {
                        var targetOverlay = this.Overlays.FirstOrDefault(x => x.Name == e.Target);
                        if (targetOverlay != null)
                        {
                            targetOverlay.OverlayMessage(e.Message);
                        }
                    });
                };

#if DEBUG
                watch.Reset();
#endif

                // コンフィグUI系初期化
                this.controlPanel      = new ControlPanel(this, this.Config);
                this.controlPanel.Dock = DockStyle.Fill;
                this.tabPage.Controls.Add(this.controlPanel);
                this.tabPage.Name = "OverlayPlugin";

                this.wsConfigPanel      = new WSConfigPanel(this.Config);
                this.wsConfigPanel.Dock = DockStyle.Fill;

                this.wsTabPage = new TabPage("OverlayPlugin WSServer");
                this.wsTabPage.Controls.Add(wsConfigPanel);
                ((TabControl)this.tabPage.Parent).TabPages.Add(this.wsTabPage);

                Logger.Log(LogLevel.Info, "InitPlugin: Initialized.");
                this.label.Text = "Initialized.";

                if (Config.UpdateCheck)
                {
                    Updater.Updater.PerformUpdateIfNecessary(PluginDirectory);
                }

                initTimer          = new Timer();
                initTimer.Interval = 300;
                initTimer.Tick    += async(o, e) =>
                {
                    if (ActGlobals.oFormActMain == null)
                    {
                        // Something went really wrong.
                        initTimer.Stop();
                    }
                    else if (ActGlobals.oFormActMain.InitActDone && ActGlobals.oFormActMain.Handle != IntPtr.Zero)
                    {
                        try
                        {
                            initTimer.Stop();

                            // ** Init phase 2

                            // Initialize the parser in the second phase since it needs the FFXIV plugin.
                            // If OverlayPlugin is placed above the FFXIV plugin, it won't be available in the first
                            // phase but it'll be loaded by the time we enter the second phase.
                            NetworkParser.Init();

                            TriggIntegration.Init();

                            // This timer runs on the UI thread (it has to since we create UI controls) but LoadAddons()
                            // can block for some time. We run it on the background thread to avoid blocking the UI.
                            // We can't run LoadAddons() in the first init phase since it checks other ACT plugins for
                            // addons. Plugins below OverlayPlugin wouldn't have been loaded in the first init phase.
                            // However, in the second phase all plugins have been loaded which means we can look for addons
                            // in that list.
                            await Task.Run(LoadAddons);

                            ActGlobals.oFormActMain.Invoke((Action)(() =>
                            {
                                // Now that addons have been loaded, we can finish the overlay setup.
                                InitializeOverlays();
                                controlPanel.InitializeOverlayConfigTabs();
                                OverlayHider.Init();
                                OverlayZCorrector.Init();

                                // WSServer has to start after the LoadAddons() call because clients can connect immediately
                                // after it's initialized and that requires the event sources to be initialized.
                                if (Config.WSServerRunning)
                                {
                                    WSServer.Init();
                                }
                            }));
                        }
                        catch (Exception ex)
                        {
                            Logger.Log(LogLevel.Error, "InitPlugin: {0}", ex);
                        }
                    }
                };
                initTimer.Start();
            }
            catch (Exception e)
            {
                Logger.Log(LogLevel.Error, "InitPlugin: {0}", e.ToString());
                MessageBox.Show(e.ToString());

                throw;
            }
        }
Ejemplo n.º 2
0
            protected override void OnClose(CloseEventArgs e)
            {
                base.OnClose(e);

                EventDispatcher.UnsubscribeAll(this);
            }
Ejemplo n.º 3
0
            protected override void OnMessage(MessageEventArgs e)
            {
                JObject data = null;

                try
                {
                    data = JObject.Parse(e.Data);
                }
                catch (JsonException ex)
                {
                    Log(LogLevel.Error, Resources.WSInvalidDataRecv, ex, e.Data);
                    return;
                }

                if (!data.ContainsKey("call"))
                {
                    return;
                }

                var msgType = data["call"].ToString();

                if (msgType == "subscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Subscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSNewSubFail, ex);
                    }

                    return;
                }
                else if (msgType == "unsubscribe")
                {
                    try
                    {
                        foreach (var item in data["events"].ToList())
                        {
                            EventDispatcher.Unsubscribe(item.ToString(), this);
                        }
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSUnsubFail, ex);
                    }
                    return;
                }

                Task.Run(() => {
                    try
                    {
                        var response = EventDispatcher.CallHandler(data);

                        if (response == null)
                        {
                            response            = new JObject();
                            response["$isNull"] = true;
                        }

                        if (data.ContainsKey("rseq"))
                        {
                            response["rseq"] = data["rseq"];
                        }

                        Send(response.ToString(Formatting.None));
                    } catch (Exception ex)
                    {
                        Log(LogLevel.Error, Resources.WSHandlerException, ex);
                    }
                });
            }
Ejemplo n.º 4
0
 protected void DispatchEvent(JObject e)
 {
     EventDispatcher.DispatchEvent(e);
 }
Ejemplo n.º 5
0
 protected void RegisterEventHandler(string name, Func <JObject, JToken> handler)
 {
     EventDispatcher.RegisterHandler(name, handler);
 }
Ejemplo n.º 6
0
 protected void RegisterEventTypes(List <string> types)
 {
     EventDispatcher.RegisterEventTypes(types);
 }