diff --git a/Conari/Conari.csproj b/Conari/Conari.csproj index 7332676..200cc3a 100644 --- a/Conari/Conari.csproj +++ b/Conari/Conari.csproj @@ -79,6 +79,7 @@ + diff --git a/Conari/ConariL.cs b/Conari/ConariL.cs index b2eed6e..229b60d 100644 --- a/Conari/ConariL.cs +++ b/Conari/ConariL.cs @@ -55,6 +55,16 @@ public dynamic DLR protected set; } + /// + /// Access to logger and its events. + /// + public ISender Log + { + get { + return LSender._; + } + } + /// /// DLR Features with `__cdecl` calling convention. /// diff --git a/Conari/IConari.cs b/Conari/IConari.cs index 15e2157..39b6a6d 100644 --- a/Conari/IConari.cs +++ b/Conari/IConari.cs @@ -24,6 +24,7 @@ using System; using net.r_eg.Conari.Core; +using net.r_eg.Conari.Log; namespace net.r_eg.Conari { @@ -40,6 +41,11 @@ public interface IConari: IBinder, IProvider, ILoader, IDisposable /// dynamic DLR { get; } + /// + /// Access to logger and its events. + /// + ISender Log { get; } + /// /// DLR Features with `__cdecl` calling convention. /// diff --git a/Conari/Native/Core/BType.cs b/Conari/Native/Core/BType.cs index b555809..60d6b6b 100644 --- a/Conari/Native/Core/BType.cs +++ b/Conari/Native/Core/BType.cs @@ -50,14 +50,17 @@ public byte[] FieldsBinary var ret = new List(); foreach(var f in Fields) { - byte[] raw = BitConverter.GetBytes(f.value); - ret.AddRange(raw); + ret.AddRange(f.toBytes()); } - return ret.ToArray(); } } + public Field getFieldByName(string name) + { + return Fields.Where(f => f?.name == name).FirstOrDefault(); + } + /// /// Magic fields. Get. /// diff --git a/Conari/Native/Core/Field.cs b/Conari/Native/Core/Field.cs index ae6d7ce..50eae77 100644 --- a/Conari/Native/Core/Field.cs +++ b/Conari/Native/Core/Field.cs @@ -45,6 +45,15 @@ public sealed class Field /// public dynamic value; + /// + /// Get bytes from current field. + /// + /// + public byte[] toBytes() + { + return BitConverter.GetBytes(value); + } + public Field(Type type, int tsize) { this.type = type; diff --git a/Conari/Native/Core/Raw.cs b/Conari/Native/Core/Raw.cs index 634b63d..58399c6 100644 --- a/Conari/Native/Core/Raw.cs +++ b/Conari/Native/Core/Raw.cs @@ -41,6 +41,9 @@ public sealed class Raw private int offset; private int length; + /// + /// Final byte-sequence from values (via pointer or local data). + /// public byte[] Values { get @@ -52,6 +55,9 @@ public byte[] Values } } + /// + /// Access to byte-sequence from values (via pointer or local data). + /// public IEnumerable Iter { get @@ -63,6 +69,10 @@ public IEnumerable Iter } } + /// + /// Generates dynamic type for current data. + /// /+DLR + /// public BType Type { get { diff --git a/Conari/Native/NativeData.cs b/Conari/Native/NativeData.cs index 56bca7c..de4eade 100644 --- a/Conari/Native/NativeData.cs +++ b/Conari/Native/NativeData.cs @@ -219,6 +219,10 @@ public NativeData h(string file, string typedef) throw new NotImplementedException("Not yet implemented."); } + public NativeData t(Type type, string name = null) { return _a(track(name, type)); } + public NativeData t(Type[] types, params string[] names) { return _a(track(names, types)); } + public NativeData t(int size, string name = null) { return _a(track(name, size)); } + public NativeData t(string name = null) { return _a(track(name, typeof(T))); } public NativeData t(params string[] names) { return _a(track(names, typeof(T), typeof(T2))); } public NativeData t(params string[] names) { return _a(track(names, typeof(T), typeof(T2), typeof(T3))); } @@ -255,12 +259,18 @@ public NativeData h(string file, string typedef) /// pointer to data structure. public NativeData(IntPtr ptr) { + if(ptr == IntPtr.Zero) { + throw new ArgumentException("NativeData: pointer must be non-zero."); + } pointer = ptr; } /// local raw data. public NativeData(byte[] bytes) { + if(bytes == null) { + bytes = new byte[0]; + } local = bytes; } @@ -279,7 +289,18 @@ protected int track(string[] names, params Type[] types) protected virtual int track(string name, Type type) { int size = SizeOf(type); - map.Add(new Field(type, size) { name = fieldName(name) }); + map.Add(new Field(type, size) { + name = fieldName(name) + }); + + return size; + } + + protected virtual int track(string name, int size) + { + map.Add(new Field(typeof(byte[]), size) { + name = fieldName(name) + }); return size; } diff --git a/Conari/Native/NativeExtension.cs b/Conari/Native/NativeExtension.cs new file mode 100644 index 0000000..eb78e8e --- /dev/null +++ b/Conari/Native/NativeExtension.cs @@ -0,0 +1,62 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Denis Kuzmin + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. +*/ + +using System; + +namespace net.r_eg.Conari.Native +{ + public static class NativeExtension + { + /// + /// To work with native data via pointer. + /// + /// pointer to data structure. + /// + public static NativeData Native(this IntPtr ptr) + { + return NativeData._(ptr); + } + + /// + /// To work with native data via byte-array. + /// + /// local raw data. + /// + public static NativeData Native(this byte[] bytes) + { + return NativeData._(bytes); + } + + /// + /// Alias to `NativeData.SizeOf ...` + /// Gets size of selected type in bytes that's should be considered as unmanaged type. + /// + /// + /// + public static int NativeSize(this Type type) + { + return NativeData.SizeOf(type); + } + } +} diff --git a/Readme.md b/Readme.md index f620747..881b28f 100644 --- a/Readme.md +++ b/Readme.md @@ -99,11 +99,11 @@ dynamic idd = (new NativeData(data)) ``` ```csharp -Raw mt = NativeData - ._(ptr) - .align(2, "a", "b") - .t("name") - .Raw; +IntPtr ptr ... +Raw mt = ptr.Native() // v1.3+ / NativeData._(ptr) + .align(2, "a", "b") + .t("name") + .Raw; - {byte[0x0000000c]} byte[] [0] 0x05 byte --