Пример #1
0
		public override void Load(ILSpySettings settings)
		{
			// For loading options, use ILSpySetting's indexer.
			// If the specified section does exist, the indexer will return a new empty element.
			XElement e = settings[ns + "CustomOptions"];
			// Now load the options from the XML document:
			Options s = new Options();
			s.UselessOption1 = (bool?)e.Attribute("useless1") ?? s.UselessOption1;
			s.UselessOption2 = (double?)e.Attribute("useless2") ?? s.UselessOption2;
			this.options = s;
		}
        static void RunPlugin(Options options)
        {
            ManualResetEvent connectEvent    = new ManualResetEvent(false);
            ManualResetEvent disconnectEvent = new ManualResetEvent(false);

            StreamDeckConnection connection = new StreamDeckConnection(options.Port, options.PluginUUID, options.RegisterEvent);

            connection.OnConnected += (sender, args) =>
            {
                connectEvent.Set();
            };

            connection.OnDisconnected += (sender, args) =>
            {
                disconnectEvent.Set();
            };

            connection.OnApplicationDidLaunch += (sender, args) =>
            {
                System.Diagnostics.Debug.WriteLine($"App Launch: {args.Event.Payload.Application}");
            };

            connection.OnApplicationDidTerminate += (sender, args) =>
            {
                System.Diagnostics.Debug.WriteLine($"App Terminate: {args.Event.Payload.Application}");
            };

            Dictionary <string, int>     counters = new Dictionary <string, int>();
            List <string>                images   = new List <string>();
            Dictionary <string, JObject> settings = new Dictionary <string, JObject>();

            connection.OnWillAppear += (sender, args) =>
            {
                switch (args.Event.Action)
                {
                case "com.tyren.testplugin.counter":
                    lock (counters)
                    {
                        counters[args.Event.Context] = 0;
                    }
                    break;

                case "com.tyren.testplugin.changeimage":
                    lock (images)
                    {
                        images.Add(args.Event.Context);
                    }
                    break;
                }
            };

            connection.OnDidReceiveSettings += (sender, args) =>
            {
                switch (args.Event.Action)
                {
                case "com.tyren.testplugin.pidemo":
                    lock (settings)
                    {
                        settings[args.Event.Context] = args.Event.Payload.Settings;
                        if (settings[args.Event.Context] == null)
                        {
                            settings[args.Event.Context] = new JObject();
                        }
                        if (settings[args.Event.Context]["selectedValue"] == null)
                        {
                            settings[args.Event.Context]["selectedValue"] = JValue.CreateString("20");
                        }
                        if (settings[args.Event.Context]["textDemoValue"] == null)
                        {
                            settings[args.Event.Context]["textDemoValue"] = JValue.CreateString("");
                        }
                    }
                    break;
                }
            };

            connection.OnWillDisappear += (sender, args) =>
            {
                lock (counters)
                {
                    if (counters.ContainsKey(args.Event.Context))
                    {
                        counters.Remove(args.Event.Context);
                    }
                }

                lock (images)
                {
                    if (images.Contains(args.Event.Context))
                    {
                        images.Remove(args.Event.Context);
                    }
                }

                lock (settings)
                {
                    if (settings.ContainsKey(args.Event.Context))
                    {
                        settings.Remove(args.Event.Context);
                    }
                }
            };

            // Start the connection
            connection.Run();

            // Current Directory is the base Stream Deck Install path.
            // For example: C:\Program Files\Elgato\StreamDeck\
            Image image = Image.FromFile(@"Images\TyDidIt40x40.png");

            // Wait for up to 10 seconds to connect
            if (connectEvent.WaitOne(TimeSpan.FromSeconds(10)))
            {
                // We connected, loop every second until we disconnect
                while (!disconnectEvent.WaitOne(TimeSpan.FromMilliseconds(1000)))
                {
                    lock (counters)
                    {
                        foreach (KeyValuePair <string, int> kvp in counters.ToArray())
                        {
                            _ = connection.SetTitleAsync(kvp.Value.ToString(), kvp.Key, SDKTarget.HardwareAndSoftware);
                            counters[kvp.Key]++;
                        }
                    }

                    lock (images)
                    {
                        foreach (string imageContext in images)
                        {
                            _ = connection.SetImageAsync(image, imageContext, SDKTarget.HardwareAndSoftware);
                        }

                        images.Clear();
                    }
                }
            }
        }