• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

luttje / Key2Joy / 6517266969

14 Oct 2023 11:05AM UTC coverage: 12.469% (+0.2%) from 12.308%
6517266969

push

github

web-flow
Implementing plugins for better separation (#39)

* Start implementing plugins for better separation
* massive refactor in attempt to split appdomains for plugins
* (breaks old mapping profiles)
* Fix error when switching from mouse button trigger to keyboard trigger and clicking in the combobox where the mouse button capture textbox is.
* Simplify code by removing legacy
* SImplify grouping actions
* Fix profile and add helpful opposite mapping generator tool
* Change solution hierarchy
* Restrict AppDomain plugins went from Zone.MyComputer -> .Internet
* create keypair in ci
* Install the .NET framework tools
* Run command in workflow
* Plugin permissions. Plugins disabled by default
* update readme (icon is no longer used)
* Plugin action runs in seperated process
* Remove unused dependencies.
* Fix action name display for mapping
* Fix Lua plugin script calls (NOTE: laggy when using MessageBox)
* convert project to sdk style
* Add editorconfig and start cleaning up
* Fix documentation. Update namespaces to match files (breaks profiles)
* Include all projects in tests, disable building docs on debug
* Add messagebox script action
* Add tests for pluginhost
* Remove administrator from window title test
* add some icons to ui
* Add enabling/disabling plugins
* Close plugins when Key2Joy shuts down
* Fix appcommand failing
* Fix plugin permission form crashing. Fix plugin load exception not showing warning
* Handle plugin host closing better when app has crashed
* Seperate host and client logic in remote event subscriber
* Ensure the PluginHost shuts down if the app crashes
* Better error output for plugins
* Fix cmd interop not working, add some tests
* also generate docs on plugins
* Fix build order with docs
* Fix enum script parameters and ensure actions share environment scopes
* Fix _wpftmp folders being created dotnet/wpf#2930
* Fix sequence action. Add disabled trigger/action for unloaded plugins on start... (continued)

180 of 1703 branches covered (0.0%)

Branch coverage included in aggregate %.

6419 of 6419 new or added lines in 207 files covered. (100.0%)

1035 of 8041 relevant lines covered (12.87%)

8445.05 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

48.95
/Core/Key2Joy.Core/Mapping/Actions/Scripting/LuaScriptAction.cs
1
using System;
2
using System.Collections;
3
using System.Collections.Generic;
4
using System.Diagnostics;
5
using System.Linq;
6
using System.Text;
7
using System.Threading.Tasks;
8
using Key2Joy.Contracts;
9
using Key2Joy.Contracts.Mapping;
10
using Key2Joy.Contracts.Mapping.Actions;
11
using Key2Joy.Contracts.Mapping.Triggers;
12
using Key2Joy.Contracts.Plugins;
13
using Key2Joy.Plugins;
14
using NLua;
15

16
namespace Key2Joy.Mapping.Actions.Scripting;
17

18
[Action(
19
    Description = "Lua Script Action",
20
    NameFormat = "Lua Script: {0}",
21
    GroupName = "Scripting",
22
    GroupImage = "script_code"
23
)]
24
public class LuaScriptAction : BaseScriptActionWithEnvironment<Lua>
25
{
26
    public LuaScriptAction(string name)
27
        : base(name) => this.ImageResource = "Lua";
2✔
28

29
    public override async Task Execute(AbstractInputBag inputBag)
30
    {
1✔
31
        try
32
        {
1✔
33
            var source = "Key2Joy.Script.Inline";
1✔
34
            if (this.IsScriptPath)
1!
35
            {
×
36
                source = this.Script;
×
37
            }
×
38

39
            lock (LockObject)
1✔
40
            {
1✔
41
                if (this.Environment.State == null)
1!
42
                {
×
43
                    Debugger.Break(); // This really shouldn't happen.
×
44
                }
×
45

46
                this.Environment.DoString(this.GetExecutableScript(), this.Script);
1✔
47
            }
1✔
48
        }
1✔
49
        catch (NLua.Exceptions.LuaScriptException ex)
×
50
        {
×
51
            Output.WriteLine(ex.Message + ex.StackTrace);
×
52
            Debugger.Break();
×
53
            if (ex.InnerException != null)
×
54
            {
×
55
                throw ex.InnerException;
×
56
            }
57
        }
×
58

59
        await base.Execute(inputBag);
1✔
60
    }
1✔
61

62
    public override void RegisterScriptingEnum(ExposedEnumeration enumeration)
63
    {
210✔
64
        // TODO: Use https://github.com/NLua/NLua/blob/3aaff863c78e89a009c21ff3aef94502018f2566/src/LuaRegistrationHelper.cs#LL76C28-L76C39
65
        this.Environment.NewTable(enumeration.Name);
210✔
66

67
        foreach (var kvp in enumeration.KeyValues)
13,650✔
68
        {
6,510✔
69
            var enumKey = kvp.Key;
6,510✔
70
            var enumValue = kvp.Value;
6,510✔
71

72
            var path = enumeration.Name + "." + enumKey;
6,510✔
73
            this.Environment.SetObjectToPath(path, enumValue);
6,510✔
74
        }
6,510✔
75
    }
210✔
76

77
    public override void RegisterScriptingMethod(ExposedMethod exposedMethod, AbstractAction instance)
78
    {
29✔
79
        exposedMethod.Prepare(instance);
29✔
80

81
        var functionName = exposedMethod.FunctionName;
29✔
82
        var parents = functionName.Split('.');
29✔
83

84
        if (parents.Length > 1)
29✔
85
        {
20✔
86
            StringBuilder currentPath = new();
20✔
87

88
            for (var i = 0; i < parents.Length - 1; i++)
80✔
89
            {
20✔
90
                if (i > 0)
20!
91
                {
×
92
                    currentPath.Append('.');
×
93
                }
×
94

95
                currentPath.Append(parents[i]);
20✔
96
            }
20✔
97

98
            var path = currentPath.ToString();
20✔
99

100
            if (this.Environment.GetTable(path) == null)
20✔
101
            {
9✔
102
                this.Environment.NewTable(path);
9✔
103
            }
9✔
104
        }
20✔
105

106
        exposedMethod.RegisterParameterTransformer<LuaTable>((luaTable, expectedType) =>
29✔
107
        {
×
108
            var dictionary = new Dictionary<object, object>();
×
109
            var areKeysSequential = true;
×
110
            var sequentialKey = 1L;
×
111

29✔
112
            foreach (var key in luaTable.Keys)
×
113
            {
×
114
                var keyAsLong = key as long?;
×
115
                var value = luaTable[key];
×
116

29✔
117
                if (areKeysSequential
×
118
                && keyAsLong != null
×
119
                && keyAsLong == sequentialKey)
×
120
                {
×
121
                    dictionary.Add(sequentialKey, value);
×
122
                    sequentialKey++;
×
123
                }
×
124
                else
29✔
125
                {
×
126
                    areKeysSequential = false;
×
127
                    dictionary.Add(key, value);
×
128
                }
×
129
            }
×
130

29✔
131
            if (areKeysSequential)
×
132
            {
×
133
                return dictionary.Values.ToArray();
×
134
            }
29✔
135

29✔
136
            return dictionary;
×
137
        });
29✔
138
        exposedMethod.RegisterParameterTransformer<LuaFunction>((luaFunction, expectedType) => new WrappedPluginType(luaFunction.Call));
29✔
139
        this.Environment.RegisterFunction(functionName, exposedMethod, exposedMethod.GetExecutorMethodInfo());
29✔
140
    }
29✔
141

142
    public override Lua MakeEnvironment() => new Lua();
1✔
143

144
    public override void RegisterEnvironmentObjects()
145
    {
1✔
146
        this.Environment.RegisterFunction("print", this, typeof(LuaScriptAction).GetMethod(nameof(Print), new[] { typeof(object[]) }));
1✔
147
        this.Environment.RegisterFunction("Print", this, typeof(LuaScriptAction).GetMethod(nameof(Print), new[] { typeof(object[]) }));
1✔
148
        this.Environment.RegisterFunction("collection", this, typeof(LuaScriptAction).GetMethod(nameof(CollectionIterator), new[] { typeof(ICollection) }));
1✔
149

150
        base.RegisterEnvironmentObjects();
1✔
151
    }
1✔
152

153
    public override void OnStartListening(AbstractTriggerListener listener, ref IList<AbstractAction> otherActions)
154
        => base.OnStartListening(listener, ref otherActions);
×
155

156
    public override void OnStopListening(AbstractTriggerListener listener)
157
        => base.OnStopListening(listener);// environment.Dispose(); // Uncomment this to cause NullReferenceException problem on NLua state described here: https://github.com/luttje/Key2Joy/pull/39#issuecomment-1581537603
×
158

159
    /// <summary>
160
    /// Returns a function that, when called, will return the next value in the collection.
161
    /// </summary>
162
    /// <param name="data"></param>
163
    /// <returns></returns>
164
    public LuaFunction CollectionIterator(ICollection data)
165
    {
×
166
        LuaIterator iterator = new(data);
×
167

168
        return this.Environment.RegisterFunction("__iterator", iterator, iterator.GetType().GetMethod(nameof(LuaIterator.Next)));
×
169
    }
×
170

171
    public override bool Equals(object obj)
172
    {
×
173
        if (obj is not LuaScriptAction action)
×
174
        {
×
175
            return false;
×
176
        }
177

178
        return action.Name == this.Name
×
179
            && action.Script == this.Script;
×
180
    }
×
181
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc