From d6882ccfe3f3f5bd64f89ae24b79489cbd27d0bd Mon Sep 17 00:00:00 2001 From: Fabian Terhorst Date: Sun, 22 May 2022 16:19:19 +0200 Subject: [PATCH] Merge pull request #575 from zziger/dev Change Key enum for Keypress events and make dependency download optional --- api/AltV.Net.Client.Example/Class1.cs | 7 +- api/AltV.Net.Client/Core.Events.cs | 5 +- api/AltV.Net.Client/Elements/Data/Key.cs | 784 +++++++++++++++++++++++ api/AltV.Net.Client/Events/Events.cs | 6 +- api/AltV.Net.Client/ModuleWrapper.cs | 5 +- api/AltV.Net.Host/Config.cs | 295 +++++++++ api/AltV.Net.Host/Host.cs | 161 +++-- 7 files changed, 1185 insertions(+), 78 deletions(-) create mode 100644 api/AltV.Net.Client/Elements/Data/Key.cs create mode 100644 api/AltV.Net.Host/Config.cs diff --git a/api/AltV.Net.Client.Example/Class1.cs b/api/AltV.Net.Client.Example/Class1.cs index 49309ca41f..5ff783dc58 100644 --- a/api/AltV.Net.Client.Example/Class1.cs +++ b/api/AltV.Net.Client.Example/Class1.cs @@ -1,4 +1,5 @@ -using AltV.Net.Client.Elements.Interfaces; +using AltV.Net.Client.Elements.Data; +using AltV.Net.Client.Elements.Interfaces; using AltV.Net.Data; namespace AltV.Net.Client.Example @@ -26,11 +27,11 @@ public override void OnStart() { switch (key) { - case ConsoleKey.X: + case Key.X: drawNametags = !drawNametags; webView.Emit("test"); break; - case ConsoleKey.M: + case Key.M: Alt.Log($"HasLocalMeta Test3: {Alt.HasLocalMetaData("test3").ToString()}"); Alt.Log($"HasLocalMeta Test: {Alt.HasLocalMetaData("test").ToString()}"); Alt.GetLocalMetaData("test", out var meta); diff --git a/api/AltV.Net.Client/Core.Events.cs b/api/AltV.Net.Client/Core.Events.cs index f2ba272023..9ccecbdc77 100644 --- a/api/AltV.Net.Client/Core.Events.cs +++ b/api/AltV.Net.Client/Core.Events.cs @@ -1,4 +1,5 @@ using System.Numerics; +using AltV.Net.Client.Elements.Data; using AltV.Net.Client.Elements.Interfaces; using AltV.Net.Client.Events; using AltV.Net.Client.Extensions; @@ -215,12 +216,12 @@ public void OnAnyResourceStop(string name) AnyResourceStopEventHandler.GetEvents().ForEachCatching(fn => fn(name), $"event {nameof(OnAnyResourceStop)} \"{name}\""); } - public void OnKeyDown(ConsoleKey key) + public void OnKeyDown(Key key) { KeyDownEventHandler.GetEvents().ForEachCatching(fn => fn(key), $"event {nameof(OnKeyDown)}"); } - public void OnKeyUp(ConsoleKey key) + public void OnKeyUp(Key key) { KeyUpEventHandler.GetEvents().ForEachCatching(fn => fn(key), $"event {nameof(OnKeyUp)}"); } diff --git a/api/AltV.Net.Client/Elements/Data/Key.cs b/api/AltV.Net.Client/Elements/Data/Key.cs new file mode 100644 index 0000000000..fa3693f64f --- /dev/null +++ b/api/AltV.Net.Client/Elements/Data/Key.cs @@ -0,0 +1,784 @@ +namespace AltV.Net.Client.Elements.Data; + +public enum Key +{ + // Summary: + // The bitmask to extract modifiers from a key value. + Modifiers = -65536, + // + // Summary: + // No key pressed. + None = 0, + // + // Summary: + // The left mouse button. + LButton = 1, + // + // Summary: + // The right mouse button. + RButton = 2, + // + // Summary: + // The CANCEL key. + Cancel = 3, + // + // Summary: + // The middle mouse button (three-button mouse). + MButton = 4, + // + // Summary: + // The first x mouse button (five-button mouse). + XButton1 = 5, + // + // Summary: + // The second x mouse button (five-button mouse). + XButton2 = 6, + // + // Summary: + // The BACKSPACE key. + Back = 8, + // + // Summary: + // The TAB key. + Tab = 9, + // + // Summary: + // The LINEFEED key. + LineFeed = 10, + // + // Summary: + // The CLEAR key. + Clear = 12, + // + // Summary: + // The ENTER key. + Enter = 13, + // + // Summary: + // The RETURN key. + Return = 13, + // + // Summary: + // The SHIFT key. + ShiftKey = 16, + // + // Summary: + // The CTRL key. + ControlKey = 17, + // + // Summary: + // The ALT key. + Menu = 18, + // + // Summary: + // The PAUSE key. + Pause = 19, + // + // Summary: + // The CAPS LOCK key. + CapsLock = 20, + // + // Summary: + // The CAPS LOCK key. + Capital = 20, + // + // Summary: + // The IME Kana mode key. + KanaMode = 21, + // + // Summary: + // The IME Hanguel mode key. (maintained for compatibility; use HangulMode) + HanguelMode = 21, + // + // Summary: + // The IME Hangul mode key. + HangulMode = 21, + // + // Summary: + // The IME Junja mode key. + JunjaMode = 23, + // + // Summary: + // The IME final mode key. + FinalMode = 24, + // + // Summary: + // The IME Kanji mode key. + KanjiMode = 25, + // + // Summary: + // The IME Hanja mode key. + HanjaMode = 25, + // + // Summary: + // The ESC key. + Escape = 27, + // + // Summary: + // The IME convert key. + IMEConvert = 28, + // + // Summary: + // The IME nonconvert key. + IMENonconvert = 29, + // + // Summary: + // The IME accept key. Obsolete, use System.Windows.Forms.Keys.IMEAccept instead. + IMEAceept = 30, + // + // Summary: + // The IME accept key, replaces System.Windows.Forms.Keys.IMEAceept. + IMEAccept = 30, + // + // Summary: + // The IME mode change key. + IMEModeChange = 31, + // + // Summary: + // The SPACEBAR key. + Space = 32, + // + // Summary: + // The PAGE UP key. + Prior = 33, + // + // Summary: + // The PAGE UP key. + PageUp = 33, + // + // Summary: + // The PAGE DOWN key. + Next = 34, + // + // Summary: + // The PAGE DOWN key. + PageDown = 34, + // + // Summary: + // The END key. + End = 35, + // + // Summary: + // The HOME key. + Home = 36, + // + // Summary: + // The LEFT ARROW key. + Left = 37, + // + // Summary: + // The UP ARROW key. + Up = 38, + // + // Summary: + // The RIGHT ARROW key. + Right = 39, + // + // Summary: + // The DOWN ARROW key. + Down = 40, + // + // Summary: + // The SELECT key. + Select = 41, + // + // Summary: + // The PRINT key. + Print = 42, + // + // Summary: + // The EXECUTE key. + Execute = 43, + // + // Summary: + // The PRINT SCREEN key. + PrintScreen = 44, + // + // Summary: + // The PRINT SCREEN key. + Snapshot = 44, + // + // Summary: + // The INS key. + Insert = 45, + // + // Summary: + // The DEL key. + Delete = 46, + // + // Summary: + // The HELP key. + Help = 47, + // + // Summary: + // The 0 key. + D0 = 48, + // + // Summary: + // The 1 key. + D1 = 49, + // + // Summary: + // The 2 key. + D2 = 50, + // + // Summary: + // The 3 key. + D3 = 51, + // + // Summary: + // The 4 key. + D4 = 52, + // + // Summary: + // The 5 key. + D5 = 53, + // + // Summary: + // The 6 key. + D6 = 54, + // + // Summary: + // The 7 key. + D7 = 55, + // + // Summary: + // The 8 key. + D8 = 56, + // + // Summary: + // The 9 key. + D9 = 57, + // + // Summary: + // The A key. + A = 65, + // + // Summary: + // The B key. + B = 66, + // + // Summary: + // The C key. + C = 67, + // + // Summary: + // The D key. + D = 68, + // + // Summary: + // The E key. + E = 69, + // + // Summary: + // The F key. + F = 70, + // + // Summary: + // The G key. + G = 71, + // + // Summary: + // The H key. + H = 72, + // + // Summary: + // The I key. + I = 73, + // + // Summary: + // The J key. + J = 74, + // + // Summary: + // The K key. + K = 75, + // + // Summary: + // The L key. + L = 76, + // + // Summary: + // The M key. + M = 77, + // + // Summary: + // The N key. + N = 78, + // + // Summary: + // The O key. + O = 79, + // + // Summary: + // The P key. + P = 80, + // + // Summary: + // The Q key. + Q = 81, + // + // Summary: + // The R key. + R = 82, + // + // Summary: + // The S key. + S = 83, + // + // Summary: + // The T key. + T = 84, + // + // Summary: + // The U key. + U = 85, + // + // Summary: + // The V key. + V = 86, + // + // Summary: + // The W key. + W = 87, + // + // Summary: + // The X key. + X = 88, + // + // Summary: + // The Y key. + Y = 89, + // + // Summary: + // The Z key. + Z = 90, + // + // Summary: + // The left Windows logo key (Microsoft Natural Keyboard). + LWin = 91, + // + // Summary: + // The right Windows logo key (Microsoft Natural Keyboard). + RWin = 92, + // + // Summary: + // The application key (Microsoft Natural Keyboard). + Apps = 93, + // + // Summary: + // The computer sleep key. + Sleep = 95, + // + // Summary: + // The 0 key on the numeric keypad. + NumPad0 = 96, + // + // Summary: + // The 1 key on the numeric keypad. + NumPad1 = 97, + // + // Summary: + // The 2 key on the numeric keypad. + NumPad2 = 98, + // + // Summary: + // The 3 key on the numeric keypad. + NumPad3 = 99, + // + // Summary: + // The 4 key on the numeric keypad. + NumPad4 = 100, + // + // Summary: + // The 5 key on the numeric keypad. + NumPad5 = 101, + // + // Summary: + // The 6 key on the numeric keypad. + NumPad6 = 102, + // + // Summary: + // The 7 key on the numeric keypad. + NumPad7 = 103, + // + // Summary: + // The 8 key on the numeric keypad. + NumPad8 = 104, + // + // Summary: + // The 9 key on the numeric keypad. + NumPad9 = 105, + // + // Summary: + // The multiply key. + Multiply = 106, + // + // Summary: + // The add key. + Add = 107, + // + // Summary: + // The separator key. + Separator = 108, + // + // Summary: + // The subtract key. + Subtract = 109, + // + // Summary: + // The decimal key. + Decimal = 110, + // + // Summary: + // The divide key. + Divide = 111, + // + // Summary: + // The F1 key. + F1 = 112, + // + // Summary: + // The F2 key. + F2 = 113, + // + // Summary: + // The F3 key. + F3 = 114, + // + // Summary: + // The F4 key. + F4 = 115, + // + // Summary: + // The F5 key. + F5 = 116, + // + // Summary: + // The F6 key. + F6 = 117, + // + // Summary: + // The F7 key. + F7 = 118, + // + // Summary: + // The F8 key. + F8 = 119, + // + // Summary: + // The F9 key. + F9 = 120, + // + // Summary: + // The F10 key. + F10 = 121, + // + // Summary: + // The F11 key. + F11 = 122, + // + // Summary: + // The F12 key. + F12 = 123, + // + // Summary: + // The F13 key. + F13 = 124, + // + // Summary: + // The F14 key. + F14 = 125, + // + // Summary: + // The F15 key. + F15 = 126, + // + // Summary: + // The F16 key. + F16 = 127, + // + // Summary: + // The F17 key. + F17 = 128, + // + // Summary: + // The F18 key. + F18 = 129, + // + // Summary: + // The F19 key. + F19 = 130, + // + // Summary: + // The F20 key. + F20 = 131, + // + // Summary: + // The F21 key. + F21 = 132, + // + // Summary: + // The F22 key. + F22 = 133, + // + // Summary: + // The F23 key. + F23 = 134, + // + // Summary: + // The F24 key. + F24 = 135, + // + // Summary: + // The NUM LOCK key. + NumLock = 144, + // + // Summary: + // The SCROLL LOCK key. + Scroll = 145, + // + // Summary: + // The left SHIFT key. + LShiftKey = 160, + // + // Summary: + // The right SHIFT key. + RShiftKey = 161, + // + // Summary: + // The left CTRL key. + LControlKey = 162, + // + // Summary: + // The right CTRL key. + RControlKey = 163, + // + // Summary: + // The left ALT key. + LMenu = 164, + // + // Summary: + // The right ALT key. + RMenu = 165, + // + // Summary: + // The browser back key (Windows 2000 or later). + BrowserBack = 166, + // + // Summary: + // The browser forward key (Windows 2000 or later). + BrowserForward = 167, + // + // Summary: + // The browser refresh key (Windows 2000 or later). + BrowserRefresh = 168, + // + // Summary: + // The browser stop key (Windows 2000 or later). + BrowserStop = 169, + // + // Summary: + // The browser search key (Windows 2000 or later). + BrowserSearch = 170, + // + // Summary: + // The browser favorites key (Windows 2000 or later). + BrowserFavorites = 171, + // + // Summary: + // The browser home key (Windows 2000 or later). + BrowserHome = 172, + // + // Summary: + // The volume mute key (Windows 2000 or later). + VolumeMute = 173, + // + // Summary: + // The volume down key (Windows 2000 or later). + VolumeDown = 174, + // + // Summary: + // The volume up key (Windows 2000 or later). + VolumeUp = 175, + // + // Summary: + // The media next track key (Windows 2000 or later). + MediaNextTrack = 176, + // + // Summary: + // The media previous track key (Windows 2000 or later). + MediaPreviousTrack = 177, + // + // Summary: + // The media Stop key (Windows 2000 or later). + MediaStop = 178, + // + // Summary: + // The media play pause key (Windows 2000 or later). + MediaPlayPause = 179, + // + // Summary: + // The launch mail key (Windows 2000 or later). + LaunchMail = 180, + // + // Summary: + // The select media key (Windows 2000 or later). + SelectMedia = 181, + // + // Summary: + // The start application one key (Windows 2000 or later). + LaunchApplication1 = 182, + // + // Summary: + // The start application two key (Windows 2000 or later). + LaunchApplication2 = 183, + // + // Summary: + // The OEM 1 key. + Oem1 = 186, + // + // Summary: + // The OEM Semicolon key on a US standard keyboard (Windows 2000 or later). + OemSemicolon = 186, + // + // Summary: + // The OEM plus key on any country/region keyboard (Windows 2000 or later). + Oemplus = 187, + // + // Summary: + // The OEM comma key on any country/region keyboard (Windows 2000 or later). + Oemcomma = 188, + // + // Summary: + // The OEM minus key on any country/region keyboard (Windows 2000 or later). + OemMinus = 189, + // + // Summary: + // The OEM period key on any country/region keyboard (Windows 2000 or later). + OemPeriod = 190, + // + // Summary: + // The OEM question mark key on a US standard keyboard (Windows 2000 or later). + OemQuestion = 191, + // + // Summary: + // The OEM 2 key. + Oem2 = 191, + // + // Summary: + // The OEM tilde key on a US standard keyboard (Windows 2000 or later). + Oemtilde = 192, + // + // Summary: + // The OEM 3 key. + Oem3 = 192, + // + // Summary: + // The OEM 4 key. + Oem4 = 219, + // + // Summary: + // The OEM open bracket key on a US standard keyboard (Windows 2000 or later). + OemOpenBrackets = 219, + // + // Summary: + // The OEM pipe key on a US standard keyboard (Windows 2000 or later). + OemPipe = 220, + // + // Summary: + // The OEM 5 key. + Oem5 = 220, + // + // Summary: + // The OEM 6 key. + Oem6 = 221, + // + // Summary: + // The OEM close bracket key on a US standard keyboard (Windows 2000 or later). + OemCloseBrackets = 221, + // + // Summary: + // The OEM 7 key. + Oem7 = 222, + // + // Summary: + // The OEM singled/double quote key on a US standard keyboard (Windows 2000 + // or later). + OemQuotes = 222, + // + // Summary: + // The OEM 8 key. + Oem8 = 223, + // + // Summary: + // The OEM 102 key. + Oem102 = 226, + // + // Summary: + // The OEM angle bracket or backslash key on the RT 102 key keyboard (Windows + // 2000 or later). + OemBackslash = 226, + // + // Summary: + // The PROCESS KEY key. + ProcessKey = 229, + // + // Summary: + // Used to pass Unicode characters as if they were keystrokes. The Packet key + // value is the low word of a 32-bit virtual-key value used for non-keyboard + // input methods. + Packet = 231, + // + // Summary: + // The ATTN key. + Attn = 246, + // + // Summary: + // The CRSEL key. + Crsel = 247, + // + // Summary: + // The EXSEL key. + Exsel = 248, + // + // Summary: + // The ERASE EOF key. + EraseEof = 249, + // + // Summary: + // The PLAY key. + Play = 250, + // + // Summary: + // The ZOOM key. + Zoom = 251, + // + // Summary: + // A constant reserved for future use. + NoName = 252, + // + // Summary: + // The PA1 key. + Pa1 = 253, + // + // Summary: + // The CLEAR key. + OemClear = 254, + // + // Summary: + // The bitmask to extract a key code from a key value. + KeyCode = 65535, + // + // Summary: + // The SHIFT modifier key. + Shift = 65536, + // + // Summary: + // The CTRL modifier key. + Control = 131072, + // + // Summary: + // The ALT modifier key. + Alt = 262144, +} \ No newline at end of file diff --git a/api/AltV.Net.Client/Events/Events.cs b/api/AltV.Net.Client/Events/Events.cs index b6ee294a80..6bc58d3432 100644 --- a/api/AltV.Net.Client/Events/Events.cs +++ b/api/AltV.Net.Client/Events/Events.cs @@ -1,5 +1,7 @@ using System.Numerics; using AltV.Net.Client.Elements.Interfaces; +using System.Windows.Input; +using AltV.Net.Client.Elements.Data; namespace AltV.Net.Client.Events { @@ -18,8 +20,8 @@ namespace AltV.Net.Client.Events public delegate void AnyResourceStartDelegate(string name); public delegate void AnyResourceStopDelegate(string name); - public delegate void KeyUpDelegate(ConsoleKey key); - public delegate void KeyDownDelegate(ConsoleKey key); + public delegate void KeyUpDelegate(Key key); + public delegate void KeyDownDelegate(Key key); public delegate void ConnectionCompleteDelegate(); diff --git a/api/AltV.Net.Client/ModuleWrapper.cs b/api/AltV.Net.Client/ModuleWrapper.cs index 675c9b4222..1c5d7599ec 100644 --- a/api/AltV.Net.Client/ModuleWrapper.cs +++ b/api/AltV.Net.Client/ModuleWrapper.cs @@ -2,6 +2,7 @@ using System.Reflection; using System.Runtime.InteropServices; using AltV.Net.CApi; +using AltV.Net.Client.Elements.Data; using AltV.Net.Client.Elements.Factories; using AltV.Net.Client.Elements.Pools; using AltV.Net.Client.Extensions; @@ -257,13 +258,13 @@ public static void OnAnyResourceStop(string name) public static void OnKeyDown(uint key) { - var consoleKey = (ConsoleKey) key; + var consoleKey = (Key) key; _core.OnKeyDown(consoleKey); } public static void OnKeyUp(uint key) { - var consoleKey = (ConsoleKey) key; + var consoleKey = (Key) key; _core.OnKeyUp(consoleKey); } diff --git a/api/AltV.Net.Host/Config.cs b/api/AltV.Net.Host/Config.cs new file mode 100644 index 0000000000..113e8f497f --- /dev/null +++ b/api/AltV.Net.Host/Config.cs @@ -0,0 +1,295 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Runtime.InteropServices; + +namespace AltV.Net.Host +{ + public interface IConfigNode + { + bool GetString(out string value); + string? GetString(); + bool GetInt(out int value); + int? GetInt(); + bool GetUInt(out uint value); + uint? GetUInt(); + bool GetLong(out long value); + long? GetLong(); + bool GetULong(out ulong value); + ulong? GetULong(); + bool GetFloat(out float value); + float? GetFloat(); + bool GetDouble(out double value); + double? GetDouble(); + bool GetList(out ConfigNode[] value); + ConfigNode[]? GetList(); + bool GetDict(out Dictionary value); + Dictionary? GetDict(); + public ConfigNode.Type ElementType { get; } + + public ConfigNode Get(string key); + public ConfigNode Get(int index); + ConfigNode this[string key] { get; } + ConfigNode this[int index] { get; } + } + + public interface IConfig : IConfigNode, IDisposable + { + } + + public class ConfigNode : IConfigNode + { + public enum Type : byte + { + NONE, + SCALAR, + LIST, + DICT, + } + + [StructLayout(LayoutKind.Sequential)] + internal struct ConfigNodeData + { + private readonly byte type; + public readonly IntPtr strValue; + public readonly int elements; + public readonly IntPtr keys; + public readonly IntPtr values; + + public Type Type => (Type) type; + } + + private readonly ConfigNodeData data; + + internal ConfigNode(ConfigNodeData data) + { + this.data = data; + } + + public Type ElementType => data.Type; + + public bool GetString(out string value) + { + if (data.Type != Type.SCALAR) + { + value = null; + return false; + } + + value = Marshal.PtrToStringUTF8(data.strValue)!; + return true; + } + public string? GetString() => GetString(out var value) ? value : null; + + public bool GetInt(out int value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = int.Parse(str); + return true; + } + public int? GetInt() => GetInt(out var value) ? value : null; + + public bool GetUInt(out uint value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = uint.Parse(str); + return true; + } + public uint? GetUInt() => GetUInt(out var value) ? value : null; + + public bool GetLong(out long value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = long.Parse(str); + return true; + } + public long? GetLong() => GetLong(out var value) ? value : null; + + public bool GetULong(out ulong value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = ulong.Parse(str); + return true; + } + public ulong? GetULong() => GetULong(out var value) ? value : null; + + public bool GetFloat(out float value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = float.Parse(str); + return true; + } + public float? GetFloat() => GetFloat(out var value) ? value : null; + + public bool GetDouble(out double value) + { + if (!GetString(out var str)) + { + value = default; + return false; + } + + value = double.Parse(str); + return true; + } + public double? GetDouble() => GetDouble(out var value) ? value : null; + + public bool GetList(out ConfigNode[] value) + { + if (data.Type != Type.LIST) + { + value = null; + return false; + } + + var valuesPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.values, valuesPtrs, 0, data.elements); + var values = new ConfigNode[data.elements]; + for (var i = 0; i < data.elements; i++) + { + values[i] = new ConfigNode(Marshal.PtrToStructure(valuesPtrs[i])); + } + + value = values; + return true; + } + public ConfigNode[]? GetList() => GetList(out var value) ? value : null; + + public bool GetBool(out bool value) + { + if (!GetString(out var str) || str is not ("yes" or "true" or "no" or "false")) + { + value = default; + return false; + } + + value = str == "yes" || str == "true"; + return true; + } + public bool? GetBool() => GetBool(out var value) ? value : null; + + public bool GetDict(out Dictionary value) + { + if (data.Type != Type.DICT) + { + value = null; + return false; + } + + var keysPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.keys, keysPtrs, 0, data.elements); + + var valuesPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.values, valuesPtrs, 0, data.elements); + + var pairs = new KeyValuePair[data.elements]; + for (var i = 0; i < data.elements; i++) + { + pairs[i] = new KeyValuePair(Marshal.PtrToStringUTF8(keysPtrs[i])!, new ConfigNode(Marshal.PtrToStructure(valuesPtrs[i]))); + } + + value = pairs.ToDictionary(e => e.Key, e => e.Value); + return true; + } + public Dictionary? GetDict() => GetDict(out var value) ? value : null; + + public ConfigNode Get(int index) + { + if (data.Type != Type.LIST || index >= data.elements) + { + return new ConfigNode(default); + } + + var valuesPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.values, valuesPtrs, 0, data.elements); + + return new ConfigNode( Marshal.PtrToStructure(valuesPtrs[index])); + } + + public ConfigNode Get(string key) + { + if (data.Type != Type.DICT) + { + return new ConfigNode(default); + } + + var keysPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.keys, keysPtrs, 0, data.elements); + + var foundIndex = -1; + for (var i = 0; i < data.elements; i++) + { + var currentKey = Marshal.PtrToStringUTF8(keysPtrs[i]); + if (key != currentKey) continue; + foundIndex = i; + break; + } + + if (foundIndex == -1) return new ConfigNode(default); + + var valuesPtrs = new IntPtr[data.elements]; + Marshal.Copy(data.values, valuesPtrs, 0, data.elements); + + return new ConfigNode(Marshal.PtrToStructure(valuesPtrs[foundIndex])); + } + + public ConfigNode this[int index] => Get(index); + public ConfigNode this[string key] => Get(key); + } + + public class Config : ConfigNode, IConfig + { + private IntPtr pointer; + + const DllImportSearchPath dllImportSearchPath = DllImportSearchPath.LegacyBehavior + | DllImportSearchPath.AssemblyDirectory + | DllImportSearchPath.SafeDirectories + | DllImportSearchPath.System32 + | DllImportSearchPath.UserDirectories + | DllImportSearchPath.ApplicationDirectory + | DllImportSearchPath.UseDllDirectoryForDependencies; + + private static IntPtr handle = NativeLibrary.Load("csharp-module", Assembly.GetExecutingAssembly(), dllImportSearchPath); + private static unsafe delegate* unmanaged[Cdecl] ConfigDelete = (delegate* unmanaged[Cdecl]) NativeLibrary.GetExport(handle, "Config_Delete"); + + public void Dispose() + { + unsafe + { + if (pointer == IntPtr.Zero) return; + ConfigDelete(pointer); + } + } + + public Config(IntPtr pointer) : base(Marshal.PtrToStructure(pointer)) + { + this.pointer = pointer; + } + } +} \ No newline at end of file diff --git a/api/AltV.Net.Host/Host.cs b/api/AltV.Net.Host/Host.cs index 7cce087bfa..16baa403d6 100644 --- a/api/AltV.Net.Host/Host.cs +++ b/api/AltV.Net.Host/Host.cs @@ -104,94 +104,117 @@ private static string GetCApiVersion() /// public static int Main(string[] args) { - try + IConfig config; + unsafe + { + const DllImportSearchPath dllImportSearchPath = DllImportSearchPath.LegacyBehavior + | DllImportSearchPath.AssemblyDirectory + | DllImportSearchPath.SafeDirectories + | DllImportSearchPath.System32 + | DllImportSearchPath.UserDirectories + | DllImportSearchPath.ApplicationDirectory + | DllImportSearchPath.UseDllDirectoryForDependencies; + var handle = NativeLibrary.Load(DllName, Assembly.GetExecutingAssembly(), dllImportSearchPath); + + var core = (delegate* unmanaged[Cdecl]) NativeLibrary.GetExport(handle, "Core_GetCoreInstance"); + var getConfig = (delegate* unmanaged[Cdecl]) NativeLibrary.GetExport(handle, "Core_GetServerConfig"); + config = new Config(getConfig(core())); + } + + if (!config["csharp-module"].GetDict()?["disableDependencyDownload"].GetBool() ?? true) { - var version = GetCApiVersion(); - if (!string.IsNullOrEmpty(version)) + try { - for (int i = 0, length = _packets.Length; i < length; ++i) + Console.WriteLine("[csharp-module] Checking dependencies... this might take a few minutes..."); + Console.WriteLine("[csharp-module] Add \n csharp-module: { \n disableDependencyDownload: true\n }\n to your server.cfg to disable this check."); + var version = GetCApiVersion(); + if (!string.IsNullOrEmpty(version)) { - var packet = _packets[i] + version; - try + for (int i = 0, length = _packets.Length; i < length; ++i) { - var task = Task.Run(() => _httpClient.GetStreamAsync(packet)); - task.Wait(); - var result = task.Result; - using var zip = new ZipArchive(result, ZipArchiveMode.Read); - foreach (var entry in zip.Entries) + var packet = _packets[i] + version; + try { - var fullName = entry.FullName; - if (!fullName.EndsWith(".dll")) + var task = Task.Run(() => _httpClient.GetStreamAsync(packet)); + task.Wait(); + var result = task.Result; + using var zip = new ZipArchive(result, ZipArchiveMode.Read); + foreach (var entry in zip.Entries) { - continue; + var fullName = entry.FullName; + if (!fullName.EndsWith(".dll")) + { + continue; + } + + var fileName = Path.GetFileName(fullName); + + using var stream = entry.Open(); + byte[] bytes; + using (var ms = new MemoryStream()) + { + stream.CopyTo(ms); + bytes = ms.ToArray(); + } + + _packetContents[fileName] = bytes; } - - var fileName = Path.GetFileName(fullName); - - using var stream = entry.Open(); - byte[] bytes; - using (var ms = new MemoryStream()) - { - stream.CopyTo(ms); - bytes = ms.ToArray(); - } - - _packetContents[fileName] = bytes; + } + catch (Exception exception) + { + Console.WriteLine("----Error while downloading: " + packet + "----"); + Console.WriteLine(exception); + Console.WriteLine("------------------------------------------------"); } } - catch (Exception exception) - { - Console.WriteLine("----Error while downloading: " + packet + "----"); - Console.WriteLine(exception); - Console.WriteLine("------------------------------------------------"); - } - } - for (int i = 0, length = _packetsSymbol.Length; i < length; ++i) - { - var packet = _packetsSymbol[i] + version; - try + for (int i = 0, length = _packetsSymbol.Length; i < length; ++i) { - var task = Task.Run(() => _httpClient.GetStreamAsync(packet)); - task.Wait(); - var result = task.Result; - using var zip = new ZipArchive(result, ZipArchiveMode.Read); - foreach (var entry in zip.Entries) + var packet = _packetsSymbol[i] + version; + try { - var fullName = entry.FullName; - if (!fullName.EndsWith(".pdb")) + var task = Task.Run(() => _httpClient.GetStreamAsync(packet)); + task.Wait(); + var result = task.Result; + using var zip = new ZipArchive(result, ZipArchiveMode.Read); + foreach (var entry in zip.Entries) { - continue; + var fullName = entry.FullName; + if (!fullName.EndsWith(".pdb")) + { + continue; + } + + var fileName = Path.GetFileName(fullName); + + using var stream = entry.Open(); + byte[] bytes; + using (var ms = new MemoryStream()) + { + stream.CopyTo(ms); + bytes = ms.ToArray(); + } + + _packetSymbols[fileName] = bytes; } - - var fileName = Path.GetFileName(fullName); - - using var stream = entry.Open(); - byte[] bytes; - using (var ms = new MemoryStream()) - { - stream.CopyTo(ms); - bytes = ms.ToArray(); - } - - _packetSymbols[fileName] = bytes; } - } - catch (Exception exception) - { - Console.WriteLine("----Error while downloading: " + packet + "----"); - Console.WriteLine(exception); - Console.WriteLine("------------------------------------------------"); + catch (Exception exception) + { + Console.WriteLine("----Error while downloading: " + packet + "----"); + Console.WriteLine(exception); + Console.WriteLine("------------------------------------------------"); + } } } } + catch (Exception e) + { + Console.WriteLine("----Error while downloading AltV Standard Dll's.----"); + Console.WriteLine(e); + Console.WriteLine("----------------------------------------------------"); + } } - catch (Exception e) - { - Console.WriteLine("----Error while downloading AltV Standard Dll's.----"); - Console.WriteLine(e); - Console.WriteLine("----------------------------------------------------"); - } + config.Dispose(); _runtimeBlockingSemaphore = new Semaphore(0, 1); SetDelegates();