From 18ae9a3ccc6591a44da20e3abb29ebb2dffb78fc Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 28 Mar 2022 00:34:16 +0300 Subject: [PATCH] first steps in writing DtsodV24 wrapper --- DTLib.Dtsod/DTLib.Dtsod.csproj | 7 +++ DTLib.Dtsod/V24/KerepFunctions.cs | 77 ++++++++++++++++++++++++++++ DTLib.Dtsod/V24/KerepStructs.cs | 35 +++++++++++++ DTLib.Network/FSP.cs | 8 ++- DTLib.Tests/DtsodV2X/TestDtsodV23.cs | 12 ++--- DTLib.Tests/Program.cs | 8 ++- DTLib/Filesystem/Directory.cs | 12 ++--- DTLib/Filesystem/File.cs | 8 +-- DTLib/Filesystem/Path.cs | 25 --------- DTLib/Filesystem/Путь.cs | 31 +++++++++++ kerep | 2 +- 11 files changed, 179 insertions(+), 46 deletions(-) create mode 100644 DTLib.Dtsod/V24/KerepFunctions.cs create mode 100644 DTLib.Dtsod/V24/KerepStructs.cs delete mode 100644 DTLib/Filesystem/Path.cs create mode 100644 DTLib/Filesystem/Путь.cs diff --git a/DTLib.Dtsod/DTLib.Dtsod.csproj b/DTLib.Dtsod/DTLib.Dtsod.csproj index c2278f5..f2b58c1 100644 --- a/DTLib.Dtsod/DTLib.Dtsod.csproj +++ b/DTLib.Dtsod/DTLib.Dtsod.csproj @@ -8,6 +8,7 @@ False portable Debug;Release;Release-net48 + true @@ -25,5 +26,11 @@ + + + + Always + + diff --git a/DTLib.Dtsod/V24/KerepFunctions.cs b/DTLib.Dtsod/V24/KerepFunctions.cs new file mode 100644 index 0000000..7d79baf --- /dev/null +++ b/DTLib.Dtsod/V24/KerepFunctions.cs @@ -0,0 +1,77 @@ +using System.Runtime.InteropServices; +using DtsodPtr=System.IntPtr; + +namespace DTLib.Dtsod.V24; + +public static unsafe class KerepFunctions +{ + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void pinvoke_print(string msg); + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void test_marshalling(string text, out KVPair* k); + public static void TestMarshalling() + { + pinvoke_print("UwU"); + string msg = "hello!"; + test_marshalling(msg, out KVPair* kptr); + Log("kptr: " + kptr->ToString()); + KVPair k = *kptr; + Log("y", + $"{{{Marshal.PtrToStringAnsi(k.key)}, {{{k.value.type.ToString()}, {Marshal.PtrToStringAnsi(k.value.VoidPtr)} }} }}"); + } + + + //parses text to binary values + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_deserialize(string text, out DtsodPtr output); + internal static DtsodPtr Deserialize(string text) + { + kerep_DtsodV24_deserialize(text, out var dtsod); + return dtsod; + } + + //creates text representation of dtsod + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_serialize(DtsodPtr dtsod, out string output); + internal static string Serialize(DtsodPtr dtsod) + { + kerep_DtsodV24_serialize(dtsod, out var text); + return text; + } + + //returns value or UniNull if key not found + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_get(DtsodPtr dtsod, string key, out Unitype output); + + internal static Unitype Get(DtsodPtr dtsod, string key) + { + kerep_DtsodV24_get(dtsod, key, out var output); + return output; + } + + //adds or sets value + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_addOrSet(DtsodPtr dtsod, string key, Unitype value); + internal static void AddOrSet(DtsodPtr dtsod, string key, Unitype value) + { + kerep_DtsodV24_addOrSet(dtsod, key, value); + } + + //checks for dtsod contains value or dont + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_contains(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output); + internal static bool Contains(DtsodPtr dtsod, string key) + { + kerep_DtsodV24_contains(dtsod, key, out var output); + return output; + } + + //replaces value with UniNull if key exists in dtsod + [DllImport("kerep.dll", CallingConvention = CallingConvention.Cdecl)] + static extern void kerep_DtsodV24_remove(DtsodPtr dtsod, string key, [MarshalAs(UnmanagedType.I1)] out bool output); + internal static bool Remove(DtsodPtr dtsod, string key) + { + kerep_DtsodV24_remove(dtsod, key, out var output); + return output; + } +} \ No newline at end of file diff --git a/DTLib.Dtsod/V24/KerepStructs.cs b/DTLib.Dtsod/V24/KerepStructs.cs new file mode 100644 index 0000000..bbefaae --- /dev/null +++ b/DTLib.Dtsod/V24/KerepStructs.cs @@ -0,0 +1,35 @@ +using System.Runtime.InteropServices; + +namespace DTLib.Dtsod.V24; + +internal enum my_type : byte +{ + Null, Float, Double, Char, Bool, + UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, + UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr, + CharPtr, STNodePtr, HashtablePtr, + UniversalType, + AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr, + AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr, + AutoarrUnitypePtr, AutoarrKVPairPtr +} + +[StructLayout(LayoutKind.Explicit)] +internal struct Unitype +{ + [FieldOffset(0)] internal long Int64; + [FieldOffset(0)] internal ulong UInt64; + [FieldOffset(0)] internal double Double; + [FieldOffset(0)] internal byte Char; + [MarshalAs(UnmanagedType.I1)] + [FieldOffset(0)] internal bool Bool; + [FieldOffset(0)] internal IntPtr VoidPtr; + [FieldOffset(8)] internal my_type type; +} + +[StructLayout(LayoutKind.Sequential)] +internal struct KVPair +{ + internal IntPtr key; + internal Unitype value; +} diff --git a/DTLib.Network/FSP.cs b/DTLib.Network/FSP.cs index 85cf5a0..a092916 100644 --- a/DTLib.Network/FSP.cs +++ b/DTLib.Network/FSP.cs @@ -18,6 +18,8 @@ public class FSP // скачивает файл с помощью FSP протокола public void DownloadFile(string filePath_server, string filePath_client) { + Путь.Предупредить(filePath_server); + Путь.Предупредить(filePath_client); lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); @@ -29,6 +31,7 @@ public class FSP public void DownloadFile(string filePath_client) { + Путь.Предупредить(filePath_client); using System.IO.Stream fileStream = File.OpenWrite(filePath_client); Download_SharedCode(fileStream, true); fileStream.Close(); @@ -37,6 +40,7 @@ public class FSP public byte[] DownloadFileToMemory(string filePath_server) { + Путь.Предупредить(filePath_server); lock (MainSocket) { Debug("b", $"requesting file download: {filePath_server}"); @@ -79,8 +83,7 @@ public class FSP fileStream.Flush(); n = 0; } - else - n++; + else n++; } } // получение остатка @@ -99,6 +102,7 @@ public class FSP // отдаёт файл с помощью FSP протокола public void UploadFile(string filePath) { + Путь.Предупредить(filePath); BytesUploaded = 0; Debug("b", $"uploading file {filePath}"); using System.IO.FileStream fileStream = File.OpenRead(filePath); diff --git a/DTLib.Tests/DtsodV2X/TestDtsodV23.cs b/DTLib.Tests/DtsodV2X/TestDtsodV23.cs index 6e32cb7..b805916 100644 --- a/DTLib.Tests/DtsodV2X/TestDtsodV23.cs +++ b/DTLib.Tests/DtsodV2X/TestDtsodV23.cs @@ -15,7 +15,7 @@ public static class TestDtsodV23 public static void TestBaseTypes() { Info.Log("c", "-----[TestDtsodV23/TestBaseTypes]------"); - DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}base_types.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}base_types.dtsod")); foreach (var pair in dtsod) Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key + ' ', "c", pair.Value.ToString()); Info.Log("g", "test completed"); @@ -23,7 +23,7 @@ public static class TestDtsodV23 public static void TestLists() { Info.Log("c", "-------[TestDtsodV23/TestLists]--------"); - DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}lists.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}lists.dtsod")); foreach (var pair in dtsod) { Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, "c", @@ -37,7 +37,7 @@ public static class TestDtsodV23 public static void TestComplexes() { Info.Log("c", "-----[TestDtsodV23/TestComplexes]------"); - DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")); + DtsodV23 dtsod = new(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod")); foreach (var pair in dtsod) { Info.LogNoTime("b", pair.Value.GetType().Name + ' ', "w", pair.Key, @@ -51,7 +51,7 @@ public static class TestDtsodV23 { Info.Log("c", "---[TestDtsodV23/TestReSerialization]--"); var dtsod = new DtsodV23(new DtsodV23(new DtsodV23( - new DtsodV23(File.ReadAllText($"DtsodV2X{Path.Sep}complexes.dtsod")).ToString()).ToString()).ToString()); + new DtsodV23(File.ReadAllText($"DtsodV2X{Путь.Разд}complexes.dtsod")).ToString()).ToString()).ToString()); Info.Log("y", dtsod.ToString()); Info.Log("g", "test completed"); } @@ -60,7 +60,7 @@ public static class TestDtsodV23 { Info.Log("c", "--------[TestDtsodV23/TestSpeed]-------"); IDtsod dtsod=null; - string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); + string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod"); LogOperationTime("V21 deserialization",100,()=>dtsod=new DtsodV21(text)); LogOperationTime("V21 serialization", 100, () => _=dtsod.ToString()); LogOperationTime("V23 deserialization", 100, () => dtsod = new DtsodV23(text)); @@ -71,7 +71,7 @@ public static class TestDtsodV23 public static void TestMemoryConsumption() { Info.Log("c", "-----[TestDtsodV23/TestMemConsumpt]----"); - string text = File.ReadAllText($"DtsodV2X{Path.Sep}messages.dtsod"); + string text = File.ReadAllText($"DtsodV2X{Путь.Разд}messages.dtsod"); var a = GC.GetTotalMemory(true); var dtsods = new DtsodV23[100]; for (int i = 0; i < dtsods.Length; i++) diff --git a/DTLib.Tests/Program.cs b/DTLib.Tests/Program.cs index 4887f8d..9288652 100644 --- a/DTLib.Tests/Program.cs +++ b/DTLib.Tests/Program.cs @@ -21,7 +21,6 @@ public static class Program public static readonly DefaultLogger Info = new(); public static void Main() { - Info.Enable(); PublicLog.LogEvent += Info.Log; PublicLog.LogNoTimeEvent += Info.LogNoTime; @@ -32,7 +31,12 @@ public static class Program { Info.Log("c", "-------------[DTLib.Tests]-------------"); //TestDtsodV23.TestAll(); - DictTest.Test(); + //DictTest.Test(); + for (uint i = 0; i < 10; i++) + { + Dtsod.V24.KerepFunctions.TestMarshalling(); + Info.Log($"{i}"); + } } catch (Exception ex) { Info.Log("r", ex.ToString()); } diff --git a/DTLib/Filesystem/Directory.cs b/DTLib/Filesystem/Directory.cs index 94b9e80..4a671c3 100644 --- a/DTLib/Filesystem/Directory.cs +++ b/DTLib/Filesystem/Directory.cs @@ -10,8 +10,8 @@ public static class Directory if (!Directory.Exists(dir)) { // проверяет существование папки, в которой нужно создать dir - if (dir.Contains(Path.Sep) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Path.Sep)))) - Create(dir.Remove(dir.LastIndexOf(Path.Sep))); + if (dir.Contains(Путь.Разд) && !Directory.Exists(dir.Remove(dir.LastIndexOf(Путь.Разд)))) + Create(dir.Remove(dir.LastIndexOf(Путь.Разд))); System.IO.Directory.CreateDirectory(dir); } } @@ -100,8 +100,8 @@ public static class Directory public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains(Path.Sep)) - Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); + if (symlinkName.Contains(Путь.Разд)) + Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.Directory)) throw new InvalidOperationException($"some error occured while creating symlink\nDirectory.CreateSymlink({symlinkName}, {sourceName})"); } @@ -110,8 +110,8 @@ public static class Directory public static int SymCopy(string srcdir, string newdir) { List files = Directory.GetAllFiles(srcdir); - if (!srcdir.EndsWith(Path.Sep)) srcdir += Path.Sep; - if (!newdir.EndsWith(Path.Sep)) newdir += Path.Sep; + if (!srcdir.EndsWith(Путь.Разд)) srcdir += Путь.Разд; + if (!newdir.EndsWith(Путь.Разд)) newdir += Путь.Разд; int i = 0; for (; i < files.Count; i++) File.CreateSymlink(files[i], files[i].Replace(srcdir, newdir)); diff --git a/DTLib/Filesystem/File.cs b/DTLib/Filesystem/File.cs index 5cfdb6e..ddb8d11 100644 --- a/DTLib/Filesystem/File.cs +++ b/DTLib/Filesystem/File.cs @@ -11,8 +11,8 @@ public static class File { if (!File.Exists(file)) { - if (file.Contains(Path.Sep)) - Directory.Create(file.Remove(file.LastIndexOf(Path.Sep))); + if (file.Contains(Путь.Разд)) + Directory.Create(file.Remove(file.LastIndexOf(Путь.Разд))); using System.IO.FileStream stream = System.IO.File.Create(file); stream.Close(); } @@ -75,8 +75,8 @@ public static class File public static void CreateSymlink(string sourceName, string symlinkName) { - if (symlinkName.Contains(Path.Sep)) - Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Path.Sep))); + if (symlinkName.Contains(Путь.Разд)) + Directory.Create(symlinkName.Remove(symlinkName.LastIndexOf(Путь.Разд))); if (!Symlink.CreateSymbolicLink(symlinkName, sourceName, Symlink.SymlinkTarget.File)) throw new InvalidOperationException($"some error occured while creating symlink\nFile.CreateSymlink({symlinkName}, {sourceName})"); } diff --git a/DTLib/Filesystem/Path.cs b/DTLib/Filesystem/Path.cs deleted file mode 100644 index 3e25243..0000000 --- a/DTLib/Filesystem/Path.cs +++ /dev/null @@ -1,25 +0,0 @@ -namespace DTLib.Filesystem; - -public static class Path -{ - public static readonly char Sep = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; - - public static string CorrectSeparator(this string path) - { - if (Sep == '\\') - { - if (path.Contains('/')) - path = path.Replace('/', '\\'); - } - else - { - if (path.Contains('\\')) - path = path.Replace('\\', '/'); - } - return path; - } - - // replaces wrong characters to use string as path - public static string NormalizeAsPath(this string str) => - str.Replace(':', '-').Replace(' ', '_'); -} \ No newline at end of file diff --git a/DTLib/Filesystem/Путь.cs b/DTLib/Filesystem/Путь.cs new file mode 100644 index 0000000..7e8baf1 --- /dev/null +++ b/DTLib/Filesystem/Путь.cs @@ -0,0 +1,31 @@ +namespace DTLib.Filesystem; + +public static class Путь +{ + public static readonly char Разд = Environment.OSVersion.Platform == PlatformID.Win32NT ? '\\' : '/'; + + public static string ИсправитьРазд(this string путь) + { + if (Разд == '\\') + { + if (путь.Contains('/')) + путь = путь.Replace('/', '\\'); + } + else + { + if (путь.Contains('\\')) + путь = путь.Replace('\\', '/'); + } + return путь; + } + + // replaces wrong characters to use string as путь + public static string НормализоватьДляПути(this string путь) => + путь.Replace(':', '-').Replace(' ', '_'); + + public static void Предупредить(string может_быть_с_точками) + { + if(может_быть_с_точками.Contains("..")) + throw new Exception($"лее точки двойные убери: {может_быть_с_точками}"); + } +} \ No newline at end of file diff --git a/kerep b/kerep index 20ce758..c1d004f 160000 --- a/kerep +++ b/kerep @@ -1 +1 @@ -Subproject commit 20ce758528fe57fdacac1960682ac8759c5a4c9c +Subproject commit c1d004f411c5c2963bbbcb6172f08ad55188ed88