diff --git a/Artemis/Artemis/Artemis.csproj b/Artemis/Artemis/Artemis.csproj index 7b4e9355e..9412d78ea 100644 --- a/Artemis/Artemis/Artemis.csproj +++ b/Artemis/Artemis/Artemis.csproj @@ -513,13 +513,15 @@ - + + + diff --git a/Artemis/Artemis/DAL/ProfileProvider.cs b/Artemis/Artemis/DAL/ProfileProvider.cs index 1e0a61dca..5b757726e 100644 --- a/Artemis/Artemis/DAL/ProfileProvider.cs +++ b/Artemis/Artemis/DAL/ProfileProvider.cs @@ -67,7 +67,7 @@ namespace Artemis.DAL lock (prof) { // Store the file - if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Name?.Length > 1)) + if (!(prof.GameName?.Length > 1) || !(prof.KeyboardSlug?.Length > 1) || !(prof.Slug?.Length > 1)) throw new ArgumentException("Profile is invalid. Name, GameName and KeyboardSlug are required"); var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}"; @@ -84,11 +84,11 @@ namespace Artemis.DAL } catch (Exception e) { - Logger.Error(e, "Couldn't save profile '{0}.json'", prof.Name); + Logger.Error(e, "Couldn't save profile '{0}.json'", prof.Slug); return; } - File.WriteAllText(path + $@"\{prof.Name}.json", json); + File.WriteAllText(path + $@"\{prof.Slug}.json", json); Logger.Debug("Saved profile {0}/{1}/{2}", prof.KeyboardSlug, prof.GameName, prof.Name); } } @@ -114,7 +114,7 @@ namespace Artemis.DAL public static void DeleteProfile(ProfileModel prof) { // Remove the file - var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}\{prof.Name}.json"; + var path = ProfileFolder + $@"\{prof.KeyboardSlug}\{prof.GameName}\{prof.Slug}.json"; if (File.Exists(path)) File.Delete(path); } diff --git a/Artemis/Artemis/Profiles/Lua/Events/LuaDeviceDrawingEventArgs.cs b/Artemis/Artemis/Profiles/Lua/Events/LuaDeviceDrawingEventArgs.cs index 3e6b03f81..8b07b3f07 100644 --- a/Artemis/Artemis/Profiles/Lua/Events/LuaDeviceDrawingEventArgs.cs +++ b/Artemis/Artemis/Profiles/Lua/Events/LuaDeviceDrawingEventArgs.cs @@ -1,5 +1,6 @@ using System; using Artemis.Models.Interfaces; +using Artemis.Profiles.Lua.Modules; using MoonSharp.Interpreter; namespace Artemis.Profiles.Lua.Events @@ -7,7 +8,7 @@ namespace Artemis.Profiles.Lua.Events [MoonSharpUserData] public class LuaDeviceDrawingEventArgs : EventArgs { - public LuaDeviceDrawingEventArgs(string deviceType, IDataModel dataModel, bool preview, LuaDrawWrapper luaDrawWrapper) + public LuaDeviceDrawingEventArgs(string deviceType, IDataModel dataModel, bool preview, LuaDrawModule luaDrawWrapper) { DeviceType = deviceType; DataModel = dataModel; @@ -18,6 +19,6 @@ namespace Artemis.Profiles.Lua.Events public string DeviceType { get; set; } public IDataModel DataModel { get; } public bool Preview { get; } - public LuaDrawWrapper Drawing { get; set; } + public LuaDrawModule Drawing { get; set; } } } \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/Lua/Events/LuaEventsWrapper.cs b/Artemis/Artemis/Profiles/Lua/Events/LuaEventsWrapper.cs index fd48f066f..c46d16205 100644 --- a/Artemis/Artemis/Profiles/Lua/Events/LuaEventsWrapper.cs +++ b/Artemis/Artemis/Profiles/Lua/Events/LuaEventsWrapper.cs @@ -2,6 +2,7 @@ using System.Windows.Forms; using System.Windows.Media; using Artemis.Models.Interfaces; +using Artemis.Profiles.Lua.Modules; using MoonSharp.Interpreter; using NLog; @@ -36,7 +37,7 @@ namespace Artemis.Profiles.Lua.Events try { LuaInvoke(profileModel, () => OnDeviceDrawing(new LuaProfileWrapper(profileModel), - new LuaDeviceDrawingEventArgs(deviceType, dataModel, preview, new LuaDrawWrapper(c)))); + new LuaDeviceDrawingEventArgs(deviceType, dataModel, preview, new LuaDrawModule(c)))); } catch (Exception) { diff --git a/Artemis/Artemis/Profiles/Lua/LuaKeyboardWrapper.cs b/Artemis/Artemis/Profiles/Lua/LuaKeyboardWrapper.cs index 95a247c29..136370d2d 100644 --- a/Artemis/Artemis/Profiles/Lua/LuaKeyboardWrapper.cs +++ b/Artemis/Artemis/Profiles/Lua/LuaKeyboardWrapper.cs @@ -42,5 +42,10 @@ namespace Artemis.Profiles.Lua { SendKeys.SendWait(keys); } + + public void GetKeyPosition(Keys key) + { + _keyboardProvider.GetKeyPosition(key); + } } } \ No newline at end of file diff --git a/Artemis/Artemis/Profiles/Lua/LuaWrapper.cs b/Artemis/Artemis/Profiles/Lua/LuaWrapper.cs index 2b4e5f315..5b221e6cf 100644 --- a/Artemis/Artemis/Profiles/Lua/LuaWrapper.cs +++ b/Artemis/Artemis/Profiles/Lua/LuaWrapper.cs @@ -78,15 +78,6 @@ namespace Artemis.Profiles.Lua } } - #region Private lua functions - - private static void LuaPrint(string s) - { - Logger.Debug("[{0}-LUA]: {1}", ProfileModel?.Name, s); - } - - #endregion - public static void Clear() { lock (LuaScript) @@ -137,6 +128,15 @@ namespace Artemis.Profiles.Lua } } + #region Private lua functions + + private static void LuaPrint(string s) + { + Logger.Debug("[{0}-LUA]: {1}", ProfileModel?.Name, s); + } + + #endregion + #region Editor public static void OpenEditor() diff --git a/Artemis/Artemis/Profiles/Lua/Modules/ILuaModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/ILuaModule.cs new file mode 100644 index 000000000..ae57bc35d --- /dev/null +++ b/Artemis/Artemis/Profiles/Lua/Modules/ILuaModule.cs @@ -0,0 +1,10 @@ +using System; + +namespace Artemis.Profiles.Lua.Modules +{ + public interface ILuaModule : IDisposable + { + bool AlwaysPresent { get; } + string ModuleName { get; } + } +} diff --git a/Artemis/Artemis/Profiles/Lua/LuaDrawWrapper.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaDrawModule.cs similarity index 86% rename from Artemis/Artemis/Profiles/Lua/LuaDrawWrapper.cs rename to Artemis/Artemis/Profiles/Lua/Modules/LuaDrawModule.cs index 86af7bb83..cf6a48bde 100644 --- a/Artemis/Artemis/Profiles/Lua/LuaDrawWrapper.cs +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaDrawModule.cs @@ -1,26 +1,31 @@ using System; -using System.Drawing.Text; using System.Globalization; -using System.Linq; using System.Windows; using System.Windows.Media; using Artemis.Profiles.Lua.Brushes; using MoonSharp.Interpreter; -namespace Artemis.Profiles.Lua +namespace Artemis.Profiles.Lua.Modules { [MoonSharpUserData] - public class LuaDrawWrapper + public class LuaDrawModule : ILuaModule { private readonly DrawingContext _ctx; - private FontFamily _font; + private readonly FontFamily _font; - public LuaDrawWrapper(DrawingContext ctx) + public LuaDrawModule(DrawingContext ctx) { _ctx = ctx; _font = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Silkscreen"); } + public bool AlwaysPresent => false; + public string ModuleName => null; + + public void Dispose() + { + } + public void DrawEllipse(LuaBrush luaBrush, double x, double y, double height, double width) { x *= 4; diff --git a/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs b/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs new file mode 100644 index 000000000..59e834c04 --- /dev/null +++ b/Artemis/Artemis/Profiles/Lua/Modules/LuaKeyboardModule.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; +using Artemis.DeviceProviders; +using MoonSharp.Interpreter.Interop; + +namespace Artemis.Profiles.Lua.Modules +{ + public class LuaKeyboardModule : ILuaModule + { + public bool AlwaysPresent => true; + public string ModuleName => "Keyboard"; + + private readonly KeyboardProvider _keyboardProvider; + + public LuaKeyboardModule(KeyboardProvider keyboardProvider) + { + _keyboardProvider = keyboardProvider; + + KeyboardHook.KeyDownCallback += KeyboardHookOnKeyDownCallback; + } + + public string Name => _keyboardProvider.Name; + public string Slug => _keyboardProvider.Slug; + public int Width => _keyboardProvider.Width; + public int Height => _keyboardProvider.Height; + + [MoonSharpVisible(false)] + public void Dispose() + { + KeyboardHook.KeyDownCallback -= KeyboardHookOnKeyDownCallback; + } + + private void KeyboardHookOnKeyDownCallback(KeyEventArgs e) + { + var keyMatch = _keyboardProvider.GetKeyPosition(e.KeyCode); + if (keyMatch != null) + LuaWrapper.LuaEventsWrapper.InvokeKeyPressed(LuaWrapper.ProfileModel, this, keyMatch.Value.KeyCode, + keyMatch.Value.X, keyMatch.Value.Y); + } + + public void PressKeys(string keys) + { + SendKeys.SendWait(keys); + } + + public void GetKeyPosition(Keys key) + { + _keyboardProvider.GetKeyPosition(key); + } + } +} diff --git a/Artemis/Artemis/Profiles/ProfileModel.cs b/Artemis/Artemis/Profiles/ProfileModel.cs index 84ebaac4d..7ec550697 100644 --- a/Artemis/Artemis/Profiles/ProfileModel.cs +++ b/Artemis/Artemis/Profiles/ProfileModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using System.Linq; using System.Windows; using System.Windows.Media; @@ -11,6 +12,7 @@ using Artemis.Profiles.Layers.Models; using Artemis.Profiles.Lua; using Artemis.Utilities; using Artemis.Utilities.ParentChild; +using Newtonsoft.Json; using Color = System.Windows.Media.Color; using Point = System.Windows.Point; using Size = System.Windows.Size; @@ -19,8 +21,11 @@ namespace Artemis.Profiles { public class ProfileModel { + private readonly char[] _invalidFileNameChars; + public ProfileModel() { + _invalidFileNameChars = Path.GetInvalidFileNameChars(); Layers = new ChildItemCollection(this); } @@ -33,6 +38,9 @@ namespace Artemis.Profiles public int Height { get; set; } public string LuaScript { get; set; } + [JsonIgnore] + public string Slug => new string(Name.Where(ch => !_invalidFileNameChars.Contains(ch)).ToArray()); + public void FixOrder() { Layers.Sort(l => l.Order); @@ -195,7 +203,7 @@ namespace Artemis.Profiles protected bool Equals(ProfileModel other) { - return string.Equals(Name, other.Name) && + return string.Equals(Slug, other.Slug) && string.Equals(KeyboardSlug, other.KeyboardSlug) && string.Equals(GameName, other.GameName); } @@ -212,7 +220,7 @@ namespace Artemis.Profiles { unchecked { - var hashCode = Name?.GetHashCode() ?? 0; + var hashCode = Slug?.GetHashCode() ?? 0; hashCode = (hashCode*397) ^ (KeyboardSlug?.GetHashCode() ?? 0); hashCode = (hashCode*397) ^ (GameName?.GetHashCode() ?? 0); return hashCode;