From 6a664c52e01521d1f8a191cf6e292a1b640dd3d1 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 12:16:50 +0200 Subject: [PATCH 01/11] Added some class extensions for byte array conversion --- .../Utils/Extensions/BaseTypeExtensions.cs | 214 ++++++++++++++++++ .../Utils/Extensions/DateTimeExtensions.cs | 32 +++ SweetLib/Utils/SweetUtils.cs | 20 -- 3 files changed, 246 insertions(+), 20 deletions(-) create mode 100644 SweetLib/Utils/Extensions/BaseTypeExtensions.cs create mode 100644 SweetLib/Utils/Extensions/DateTimeExtensions.cs diff --git a/SweetLib/Utils/Extensions/BaseTypeExtensions.cs b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs new file mode 100644 index 0000000..1131ad2 --- /dev/null +++ b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs @@ -0,0 +1,214 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SweetLib.Utils.Extensions +{ + /// + /// Defines extensions to some general classes. + /// + public static class BaseTypeExtensions + { + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this short value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this ushort value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this int value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this uint value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this long value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this ulong value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this float value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this double value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this bool value) + { + return BitConverter.GetBytes(value); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + public static byte[] ToBytes(this DateTime value) + { + return BitConverter.GetBytes(value.ToUnixTimeStamp()); + } + + /// + /// Converts a value into a byte array. + /// + /// Value to convert. + /// Byte array representation of the value. + /// Using for converting. + public static byte[] ToBytes(this string value) + { + return Encoding.UTF32.GetBytes(value); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static short ToInt16(this byte[] bytes) + { + return BitConverter.ToInt16(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static ushort ToUInt16(this byte[] bytes) + { + return BitConverter.ToUInt16(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static int ToInt32(this byte[] bytes) + { + return BitConverter.ToInt32(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static uint ToUInt32(this byte[] bytes) + { + return BitConverter.ToUInt32(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static long ToInt64(this byte[] bytes) + { + return BitConverter.ToInt64(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static ulong ToUInt64(this byte[] bytes) + { + return BitConverter.ToUInt64(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static float ToSingle(this byte[] bytes) + { + return BitConverter.ToSingle(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + public static double ToDouble(this byte[] bytes) + { + return BitConverter.ToDouble(bytes, 0); + } + + /// + /// Converts a into a type. + /// + /// Byte array representation to convert. + /// Converted value. + /// Using for converting. + public static string ToString(this byte[] bytes) + { + return Encoding.UTF32.GetString(bytes); + } + } +} diff --git a/SweetLib/Utils/Extensions/DateTimeExtensions.cs b/SweetLib/Utils/Extensions/DateTimeExtensions.cs new file mode 100644 index 0000000..0ed3211 --- /dev/null +++ b/SweetLib/Utils/Extensions/DateTimeExtensions.cs @@ -0,0 +1,32 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace SweetLib.Utils.Extensions +{ + /// + /// Defines extensions to classes which are related to . + /// + public static class DateTimeExtensions + { + /// + /// Converts a into an Unix timestamp. + /// + /// to convert into Unix timestamp. + /// Converted Unix timestamp. + public static double ToUnixTimeStamp(this DateTime date) + { + return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; + } + + /// + /// Converts an Unix timestamp into a . + /// + /// Unix timestamp to convert. + /// Converted . + public static DateTime ToDateTime(this double timestamp) + { + return new DateTime(1970, 1, 1).AddMilliseconds(timestamp); + } + } +} diff --git a/SweetLib/Utils/SweetUtils.cs b/SweetLib/Utils/SweetUtils.cs index c32fc4b..0cfd330 100644 --- a/SweetLib/Utils/SweetUtils.cs +++ b/SweetLib/Utils/SweetUtils.cs @@ -36,25 +36,5 @@ namespace SweetLib.Utils return invalidChars.Aggregate(fileName, (current, c) => current.Replace(c, replaceChar)); } - - /// - /// Converts a into an Unix timestamp. - /// - /// to convert into Unix timestamp. - /// Converted Unix timestamp. - public static double DateTimeToUnixTimeStamp(DateTime date) - { - return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds; - } - - /// - /// Converts an Unix timestamp into a . - /// - /// Unix timestamp to convert. - /// Converted . - public static DateTime UnixTimestampToDateTime(double timestamp) - { - return new DateTime(1970, 1, 1).AddMilliseconds(timestamp); - } } } From e2a7b4e31070b4225063ce10e8875f5b2e697ea9 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 12:23:03 +0200 Subject: [PATCH 02/11] Some restructurings Added IO project --- SweetLib.IO/SweetLib.IO.csproj | 11 +++++++++++ SweetLib.sln | 17 +++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 SweetLib.IO/SweetLib.IO.csproj diff --git a/SweetLib.IO/SweetLib.IO.csproj b/SweetLib.IO/SweetLib.IO.csproj new file mode 100644 index 0000000..471d29a --- /dev/null +++ b/SweetLib.IO/SweetLib.IO.csproj @@ -0,0 +1,11 @@ + + + + netstandard2.0 + + + + + + + diff --git a/SweetLib.sln b/SweetLib.sln index 6673fc1..ec94494 100644 --- a/SweetLib.sln +++ b/SweetLib.sln @@ -9,6 +9,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib.Demo.Console", "Sw EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SweetLib", "SweetLib\SweetLib.csproj", "{7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Demo", "Demo", "{5C88E7F1-525D-4FF5-B7CE-2E02EC22BD9A}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{9036EBA5-45D0-441D-9C81-AF4F86C8A8F8}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SweetLib.IO", "SweetLib.IO\SweetLib.IO.csproj", "{0874043E-42B3-43B2-B3BB-726CEE4142E9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,10 +33,21 @@ Global {7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Debug|Any CPU.Build.0 = Debug|Any CPU {7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Release|Any CPU.ActiveCfg = Release|Any CPU {7C15F2F3-2BF2-46FB-B813-9EEC1B41D679}.Release|Any CPU.Build.0 = Release|Any CPU + {0874043E-42B3-43B2-B3BB-726CEE4142E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0874043E-42B3-43B2-B3BB-726CEE4142E9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0874043E-42B3-43B2-B3BB-726CEE4142E9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0874043E-42B3-43B2-B3BB-726CEE4142E9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {9350E0D4-9FE7-49D5-ACDE-E857E547889C} = {5C88E7F1-525D-4FF5-B7CE-2E02EC22BD9A} + {C8681E08-6F23-45E1-A16F-BBA72003219B} = {5C88E7F1-525D-4FF5-B7CE-2E02EC22BD9A} + {7C15F2F3-2BF2-46FB-B813-9EEC1B41D679} = {9036EBA5-45D0-441D-9C81-AF4F86C8A8F8} + {5C88E7F1-525D-4FF5-B7CE-2E02EC22BD9A} = {9036EBA5-45D0-441D-9C81-AF4F86C8A8F8} + {0874043E-42B3-43B2-B3BB-726CEE4142E9} = {9036EBA5-45D0-441D-9C81-AF4F86C8A8F8} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7AF4F0AF-F1C1-473C-BEE3-8C2B6DAEBF35} EndGlobalSection From f1934c525a604756da165cdc1375553b951be761 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 12:38:32 +0200 Subject: [PATCH 03/11] Fixed target framework --- SweetLib.IO/SweetLib.IO.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SweetLib.IO/SweetLib.IO.csproj b/SweetLib.IO/SweetLib.IO.csproj index 471d29a..87876bd 100644 --- a/SweetLib.IO/SweetLib.IO.csproj +++ b/SweetLib.IO/SweetLib.IO.csproj @@ -1,7 +1,7 @@ - netstandard2.0 + netstandard1.3 From c7ea85f9126729d0c96e751321238c0375a9b7fb Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 12:39:00 +0200 Subject: [PATCH 04/11] Removed unneccessarry uses --- SweetLib/Utils/Extensions/BaseTypeExtensions.cs | 1 - SweetLib/Utils/Extensions/DateTimeExtensions.cs | 2 -- SweetLib/Utils/SweetUtils.cs | 3 +-- 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/SweetLib/Utils/Extensions/BaseTypeExtensions.cs b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs index 1131ad2..ef41e45 100644 --- a/SweetLib/Utils/Extensions/BaseTypeExtensions.cs +++ b/SweetLib/Utils/Extensions/BaseTypeExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.Text; namespace SweetLib.Utils.Extensions diff --git a/SweetLib/Utils/Extensions/DateTimeExtensions.cs b/SweetLib/Utils/Extensions/DateTimeExtensions.cs index 0ed3211..0d7cb43 100644 --- a/SweetLib/Utils/Extensions/DateTimeExtensions.cs +++ b/SweetLib/Utils/Extensions/DateTimeExtensions.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace SweetLib.Utils.Extensions { diff --git a/SweetLib/Utils/SweetUtils.cs b/SweetLib/Utils/SweetUtils.cs index 0cfd330..b5a1ae5 100644 --- a/SweetLib/Utils/SweetUtils.cs +++ b/SweetLib/Utils/SweetUtils.cs @@ -1,5 +1,4 @@ -using System; -using System.IO; +using System.IO; using System.Linq; namespace SweetLib.Utils From 066f320a36c55034166c3ec4ecaf1c91eeac24b3 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 12:39:33 +0200 Subject: [PATCH 05/11] First draft for StreamManager including implementation for int values --- .../Classes/Streaming/StreamExceptions.cs | 38 ++++++++++ .../Classes/Streaming/StreamManager.cs | 71 +++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 SweetLib.IO/Classes/Streaming/StreamExceptions.cs create mode 100644 SweetLib.IO/Classes/Streaming/StreamManager.cs diff --git a/SweetLib.IO/Classes/Streaming/StreamExceptions.cs b/SweetLib.IO/Classes/Streaming/StreamExceptions.cs new file mode 100644 index 0000000..fd418fa --- /dev/null +++ b/SweetLib.IO/Classes/Streaming/StreamExceptions.cs @@ -0,0 +1,38 @@ +using System.IO; + +namespace SweetLib.IO.Classes.Streaming +{ + /// + /// Exception thrown by streaming related operations. + /// + public class StreamException : IOException + { + /// + /// Creates a new . + /// + public StreamException() { } + + /// + /// Creates a new . + /// + /// Exception message. + public StreamException(string message) : base(message) { } + } + + /// + /// Exception thrown by streaming related operations. May be thrown if the stream types do not match. + /// + public class StreamTypeException : StreamException + { + /// + /// Creates a new . + /// + public StreamTypeException() { } + + /// + /// Creates a new . + /// + /// Exception message. + public StreamTypeException(string message) : base(message) { } + } +} diff --git a/SweetLib.IO/Classes/Streaming/StreamManager.cs b/SweetLib.IO/Classes/Streaming/StreamManager.cs new file mode 100644 index 0000000..4ac9182 --- /dev/null +++ b/SweetLib.IO/Classes/Streaming/StreamManager.cs @@ -0,0 +1,71 @@ +using System.IO; +using SweetLib.Utils.Extensions; + +namespace SweetLib.IO.Classes.Streaming +{ + public static class StreamManager + { + enum StreamedObjectType : ushort // 2 bytes + { + Unknown = 0x0000, + StringValue = 0x1100, + BoolValue = 0x1200, + CharValue = 0x1300, + IntValue = 0x1400, + LongValue = 0x1410, + FloatValue = 0x1500, + DoubleValue = 0x1600, + DateTimeValue = 0x1700, + StreamValue = 0x1800, + ObjectValue = 0x1900, + } + + private static bool SaveMetaToStream(Stream stream, StreamedObjectType type, long length) + { + stream.Write(((ushort)type).ToBytes(), 0, sizeof(ushort)); + stream.Write(length.ToBytes(), 0, sizeof(long)); + + return true; + } + + private static bool LoadMetaDataFromStream(Stream stream, out StreamedObjectType type, out long length) + { + type = StreamedObjectType.Unknown; + length = -1; + + // read object type + var buffer = new byte[sizeof(ushort)]; + stream.Read(buffer, 0, buffer.Length); + type = (StreamedObjectType) buffer.ToUInt16(); + + // read data length + buffer = new byte[sizeof(long)]; + stream.Read(buffer, 0, buffer.Length); + length = buffer.ToInt64(); + + return true; + } + + public static void SaveToStream(Stream stream, int value) + { + SaveMetaToStream(stream, StreamedObjectType.IntValue, sizeof(int)); + + stream.Write(value.ToBytes(), 0, sizeof(int)); + } + + public static void LoadFromStream(Stream stream, out int value) + { + StreamedObjectType type; + long length; + + LoadMetaDataFromStream(stream, out type, out length); + + if(type!=StreamedObjectType.IntValue) + throw new StreamTypeException($"Expected {StreamedObjectType.IntValue} but found {type} instead."); + + var buffer = new byte[length]; + stream.Read(buffer, 0, buffer.Length); + value = buffer.ToInt32(); + } + } +} From 84e67b1e5c66fb3dc4ce0f1b96ef31dc8594dcb8 Mon Sep 17 00:00:00 2001 From: Serraniel Date: Fri, 6 Apr 2018 17:31:36 +0200 Subject: [PATCH 06/11] Refactored method names and added missing conversions from byte arrays --- SweetLib.Demo.Console/Program.cs | 18 ++++- .../SweetLib.Demo.Console.csproj | 13 ++- .../Utils/Extensions/BaseTypeExtensions.cs | 80 ++++++++++++++----- 3 files changed, 86 insertions(+), 25 deletions(-) diff --git a/SweetLib.Demo.Console/Program.cs b/SweetLib.Demo.Console/Program.cs index 61d0cfd..b865291 100644 --- a/SweetLib.Demo.Console/Program.cs +++ b/SweetLib.Demo.Console/Program.cs @@ -1,5 +1,6 @@ using System.IO; using SweetLib.Classes.Storer; +using SweetLib.IO.Classes.Streaming; using SweetLib.Utils.Logger; namespace SweetLib.Demo.Console @@ -17,11 +18,24 @@ namespace SweetLib.Demo.Console Logger.Warn("WARN!"); Logger.Error("Error :("); - System.Console.ReadLine(); - + var f = Path.GetTempFileName(); var ini = new IniFileStorer(f); System.Console.WriteLine(ini.ReadString("sec","key")); + + using (var stream = File.Create(@"S:\test.dat")) + { + StreamManager.SaveToStream(stream, 5); + } + + using (var stream = File.OpenRead(@"S:\test.dat")) + { + int value; + StreamManager.LoadFromStream(stream, out value); + System.Console.WriteLine(value); + } + + System.Console.ReadLine(); } } } diff --git a/SweetLib.Demo.Console/SweetLib.Demo.Console.csproj b/SweetLib.Demo.Console/SweetLib.Demo.Console.csproj index 5ef5e6c..01f5111 100644 --- a/SweetLib.Demo.Console/SweetLib.Demo.Console.csproj +++ b/SweetLib.Demo.Console/SweetLib.Demo.Console.csproj @@ -40,9 +40,6 @@ ..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll - - ..\packages\SweetLib.0.2.0-alpha\lib\netstandard1.3\SweetLib.dll - ..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll @@ -115,6 +112,16 @@ + + + {0874043e-42b3-43b2-b3bb-726cee4142e9} + SweetLib.IO + + + {7c15f2f3-2bf2-46fb-b813-9eec1b41d679} + SweetLib + +