Merge branch 'feature/Streaming' into develop
* feature/Streaming: Some nuget configurations Did some doc texts Did some inline implementations Added most other streaming implementations Added interface for streamable objects Refactored method names and added missing conversions from byte arrays First draft for StreamManager including implementation for int values Removed unneccessarry uses Fixed target framework Some restructurings Added IO project Added some class extensions for byte array conversion
This commit is contained in:
commit
8031d30644
|
@ -1,5 +1,7 @@
|
|||
using System.IO;
|
||||
using System;
|
||||
using System.IO;
|
||||
using SweetLib.Classes.Storer;
|
||||
using SweetLib.IO.Classes.Streaming;
|
||||
using SweetLib.Utils.Logger;
|
||||
|
||||
namespace SweetLib.Demo.Console
|
||||
|
@ -17,11 +19,33 @@ 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);
|
||||
StreamManager.SaveToStream(stream, "Hallo Wellt eksde");
|
||||
StreamManager.SaveToStream(stream, 12d);
|
||||
StreamManager.SaveToStream(stream, DateTime.UtcNow);
|
||||
}
|
||||
|
||||
using (var stream = File.OpenRead(@"S:\test.dat"))
|
||||
{
|
||||
StreamManager.LoadFromStream(stream, out int iValue);
|
||||
StreamManager.LoadFromStream(stream, out string sValue);
|
||||
StreamManager.LoadFromStream(stream, out double dValue);
|
||||
StreamManager.LoadFromStream(stream, out DateTime dtValue);
|
||||
|
||||
System.Console.WriteLine(iValue);
|
||||
System.Console.WriteLine(sValue);
|
||||
System.Console.WriteLine(dValue);
|
||||
System.Console.WriteLine(dtValue);
|
||||
}
|
||||
|
||||
System.Console.ReadLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,9 +40,6 @@
|
|||
<Reference Include="Microsoft.Win32.Registry, Version=4.0.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.Win32.Registry.4.3.0\lib\net46\Microsoft.Win32.Registry.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="SweetLib, Version=0.2.0.0, Culture=neutral, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\SweetLib.0.2.0-alpha\lib\netstandard1.3\SweetLib.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.AppContext, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\System.AppContext.4.3.0\lib\net46\System.AppContext.dll</HintPath>
|
||||
|
@ -115,6 +112,16 @@
|
|||
<None Include="App.config" />
|
||||
<None Include="packages.config" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SweetLib.IO\SweetLib.IO.csproj">
|
||||
<Project>{0874043e-42b3-43b2-b3bb-726cee4142e9}</Project>
|
||||
<Name>SweetLib.IO</Name>
|
||||
</ProjectReference>
|
||||
<ProjectReference Include="..\SweetLib\SweetLib.csproj">
|
||||
<Project>{7c15f2f3-2bf2-46fb-b813-9eec1b41d679}</Project>
|
||||
<Name>SweetLib</Name>
|
||||
</ProjectReference>
|
||||
</ItemGroup>
|
||||
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
|
||||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
|
||||
Other similar extension points exist, see Microsoft.Common.targets.
|
||||
|
|
25
SweetLib.IO/Classes/Streaming/IStreamable.cs
Normal file
25
SweetLib.IO/Classes/Streaming/IStreamable.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace SweetLib.IO.Classes.Streaming
|
||||
{
|
||||
/// <summary>
|
||||
/// Streamable objects
|
||||
/// </summary>
|
||||
public interface IStreamable
|
||||
{
|
||||
/// <summary>
|
||||
/// Saves the current object into a stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream in which the current object should be saved.</param>
|
||||
void SaveToStream(Stream stream);
|
||||
|
||||
/// <summary>
|
||||
/// Loads the object from a given stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">Stream from which the object is loaded.</param>
|
||||
void LoadFromStream(Stream stream);
|
||||
}
|
||||
}
|
38
SweetLib.IO/Classes/Streaming/StreamExceptions.cs
Normal file
38
SweetLib.IO/Classes/Streaming/StreamExceptions.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using System.IO;
|
||||
|
||||
namespace SweetLib.IO.Classes.Streaming
|
||||
{
|
||||
/// <summary>
|
||||
/// Exception thrown by streaming related operations.
|
||||
/// </summary>
|
||||
public class StreamException : IOException
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamException"/>.
|
||||
/// </summary>
|
||||
public StreamException() { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamException"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">Exception message.</param>
|
||||
public StreamException(string message) : base(message) { }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exception thrown by streaming related operations. May be thrown if the stream types do not match.
|
||||
/// </summary>
|
||||
public class StreamTypeException : StreamException
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamTypeException"/>.
|
||||
/// </summary>
|
||||
public StreamTypeException() { }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new <see cref="StreamTypeException"/>.
|
||||
/// </summary>
|
||||
/// <param name="message">Exception message.</param>
|
||||
public StreamTypeException(string message) : base(message) { }
|
||||
}
|
||||
}
|
262
SweetLib.IO/Classes/Streaming/StreamManager.cs
Normal file
262
SweetLib.IO/Classes/Streaming/StreamManager.cs
Normal file
|
@ -0,0 +1,262 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices.ComTypes;
|
||||
using SweetLib.Utils.Extensions;
|
||||
|
||||
namespace SweetLib.IO.Classes.Streaming
|
||||
{
|
||||
/// <summary>
|
||||
/// <see cref="StreamManager"/> wraps some useful methods for custom implementations of object streaming.
|
||||
/// </summary>
|
||||
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,
|
||||
}
|
||||
|
||||
private static bool SaveMetaToStream(Stream stream, StreamedObjectType type, long length)
|
||||
{
|
||||
stream.Write(((ushort)type).AsBytes(), 0, sizeof(ushort));
|
||||
stream.Write(length.AsBytes(), 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.AsUInt16();
|
||||
|
||||
// read data length
|
||||
buffer = new byte[sizeof(long)];
|
||||
stream.Read(buffer, 0, buffer.Length);
|
||||
length = buffer.AsInt64();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private static void SaveToStream(Stream stream, StreamedObjectType type, byte[] buffer)
|
||||
{
|
||||
SaveMetaToStream(stream, type, buffer.Length);
|
||||
stream.Write(buffer, 0, buffer.Length);
|
||||
}
|
||||
|
||||
private static byte[] LoadFromStream(Stream stream, StreamedObjectType type)
|
||||
{
|
||||
long length;
|
||||
|
||||
LoadMetaDataFromStream(stream, out StreamedObjectType foundType, out length);
|
||||
|
||||
if (foundType != type)
|
||||
throw new StreamTypeException($"Expected {type} but found {foundType} instead.");
|
||||
|
||||
var buffer = new byte[length];
|
||||
stream.Read(buffer, 0, buffer.Length);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="string"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="string"/> will be added.</param>
|
||||
/// <param name="value"><see cref="string"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, string value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.StringValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="bool"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="bool"/> will be added.</param>
|
||||
/// <param name="value"><see cref="bool"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, bool value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.BoolValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="char"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="char"/> will be added.</param>
|
||||
/// <param name="value"><see cref="char"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, char value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.CharValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="int"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="int"/> will be added.</param>
|
||||
/// <param name="value"><see cref="int"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, int value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.IntValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="long"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="long"/> will be added.</param>
|
||||
/// <param name="value"><see cref="long"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, long value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.LongValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="float"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="float"/> will be added.</param>
|
||||
/// <param name="value"><see cref="float"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, float value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.FloatValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="double"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="double"/> will be added.</param>
|
||||
/// <param name="value"><see cref="double"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, double value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.DoubleValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="DateTime"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="DateTime"/> will be added.</param>
|
||||
/// <param name="value"><see cref="DateTime"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, DateTime value)
|
||||
{
|
||||
SaveToStream(stream, StreamedObjectType.DateTimeValue, value.AsBytes());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a <see cref="Stream"/> to a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> to which the <see cref="Stream"/> will be added.</param>
|
||||
/// <param name="value"><see cref="Stream"/> value to add.</param>
|
||||
public static void SaveToStream(Stream stream, Stream value, bool resetSourceStream = false)
|
||||
{
|
||||
if (resetSourceStream)
|
||||
value.Position = 0;
|
||||
|
||||
var buffer = new byte[value.Length - value.Position];
|
||||
value.Read(buffer, 0, buffer.Length);
|
||||
|
||||
SaveToStream(stream, StreamedObjectType.StreamValue, buffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="string"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="string"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="string"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out string value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.StringValue).AsString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="bool"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="bool"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="bool"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out bool value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.BoolValue).AsBool();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="char"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="char"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="char"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out char value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.CharValue).AsChar();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="int"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="int"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="int"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out int value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.IntValue).AsInt32();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="long"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="long"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="long"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out long value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.LongValue).AsInt64();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="string"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="string"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="string"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out float value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.FloatValue).AsSingle();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="double"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="double"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="double"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out double value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.DoubleValue).AsDouble();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="DateTime"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="DateTime"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="DateTime"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out DateTime value)
|
||||
{
|
||||
value = LoadFromStream(stream, StreamedObjectType.DateTimeValue).AsDateTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a <see cref="Stream"/> from a given <see cref="Stream"/>.
|
||||
/// </summary>
|
||||
/// <param name="stream"><see cref="Stream"/> from which the <see cref="Stream"/> will be loaded.</param>
|
||||
/// <param name="value">Contains the loaded <see cref="Stream"/> value.</param>
|
||||
public static void LoadFromStream(Stream stream, out Stream value)
|
||||
{
|
||||
var buffer = LoadFromStream(stream, StreamedObjectType.StreamValue);
|
||||
value = new MemoryStream();
|
||||
value.Read(buffer, 0, buffer.Length);
|
||||
}
|
||||
}
|
||||
}
|
26
SweetLib.IO/SweetLib.IO.csproj
Normal file
26
SweetLib.IO/SweetLib.IO.csproj
Normal file
|
@ -0,0 +1,26 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netstandard1.3</TargetFramework>
|
||||
<Version>0.1.0-alpha</Version>
|
||||
<Description>SweetLib IO package</Description>
|
||||
<Authors>Serraniel</Authors>
|
||||
<Company />
|
||||
<Copyright>Copyright © 2017 by Serraniel</Copyright>
|
||||
<PackageLicenseUrl>https://joinup.ec.europa.eu/page/eupl-text-11-12</PackageLicenseUrl>
|
||||
<PackageProjectUrl>https://github.com/Serraniel/SweetLib</PackageProjectUrl>
|
||||
<RepositoryUrl>https://github.com/Serraniel/SweetLib</RepositoryUrl>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<PackageIconUrl>https://github.com/Serraniel/SweetLib/blob/develop/nuget_icon.png?raw=true</PackageIconUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<DocumentationFile>bin\Release\netstandard1.3\SweetLib.IO.xml</DocumentationFile>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\SweetLib\SweetLib.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
18
SweetLib.IO/SweetLib.IO.nuspec
Normal file
18
SweetLib.IO/SweetLib.IO.nuspec
Normal file
|
@ -0,0 +1,18 @@
|
|||
<?xml version="1.0"?>
|
||||
<package >
|
||||
<metadata>
|
||||
<id>$id$</id>
|
||||
<version>$version$-alpha</version>
|
||||
<title>$title$</title>
|
||||
<authors>Serraniel</authors>
|
||||
<owners>$author$</owners>
|
||||
<licenseUrl>https://opensource.org/licenses/GPL-3.0</licenseUrl>
|
||||
<projectUrl>https://github.com/Serraniel/SweetLib</projectUrl>
|
||||
<iconUrl>https://github.com/Serraniel/SweetLib/blob/develop/nuget_icon.png?raw=true</iconUrl>
|
||||
<requireLicenseAcceptance>true</requireLicenseAcceptance>
|
||||
<description>$description$</description>
|
||||
<releaseNotes>First pre version just to try that nuget package thing :)</releaseNotes>
|
||||
<copyright>Copyright © 2017 Serraniel</copyright>
|
||||
<tags>Utility</tags>
|
||||
</metadata>
|
||||
</package>
|
2
SweetLib.IO/build-nuget.bat
Normal file
2
SweetLib.IO/build-nuget.bat
Normal file
|
@ -0,0 +1,2 @@
|
|||
nuget pack SweetLib.IO.csproj -properties Configuration=Release -symbols
|
||||
pause
|
17
SweetLib.sln
17
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
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
<RepositoryUrl>https://github.com/Serraniel/SweetLib</RepositoryUrl>
|
||||
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
|
||||
<PackageIconUrl>https://github.com/Serraniel/SweetLib/blob/develop/nuget_icon.png?raw=true</PackageIconUrl>
|
||||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
|
|
253
SweetLib/Utils/Extensions/BaseTypeExtensions.cs
Normal file
253
SweetLib/Utils/Extensions/BaseTypeExtensions.cs
Normal file
|
@ -0,0 +1,253 @@
|
|||
using System;
|
||||
using System.Text;
|
||||
|
||||
namespace SweetLib.Utils.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines extensions to some general classes.
|
||||
/// </summary>
|
||||
public static class BaseTypeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="short"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this short value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="ushort"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this ushort value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="int"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this int value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="uint"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this uint value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="long"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this long value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="ulong"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this ulong value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="float"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this float value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="double"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this double value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="bool"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this bool value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this DateTime value)
|
||||
{
|
||||
return BitConverter.GetBytes(value.ToUnixTimeStamp());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="string"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
/// <remarks>Using <see cref="UTF32Encoding"/> for converting.</remarks>
|
||||
public static byte[] AsBytes(this string value)
|
||||
{
|
||||
return Encoding.UTF32.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="char"/> value into a byte array.
|
||||
/// </summary>
|
||||
/// <param name="value">Value to convert.</param>
|
||||
/// <returns>Byte array representation of the value.</returns>
|
||||
public static byte[] AsBytes(this char value)
|
||||
{
|
||||
return BitConverter.GetBytes(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="short"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static short AsInt16(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt16(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="ushort"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static ushort AsUInt16(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt16(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="int"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static int AsInt32(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt32(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="uint"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static uint AsUInt32(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt32(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="long"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static long AsInt64(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToInt64(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="ulong"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static ulong AsUInt64(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToUInt64(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="float"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static float AsSingle(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToSingle(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="double"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static double AsDouble(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToDouble(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="bool"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static bool AsBool(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToBoolean(bytes, 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="DateTime"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static DateTime AsDateTime(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToDouble(bytes, 0).ToDateTime();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="string"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
/// <remarks>Using <see cref="UTF32Encoding"/> for converting.</remarks>
|
||||
public static string AsString(this byte[] bytes)
|
||||
{
|
||||
return Encoding.UTF32.GetString(bytes);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a byte array into a <see cref="char"/> type.
|
||||
/// </summary>
|
||||
/// <param name="bytes">Byte array representation to convert.</param>
|
||||
/// <returns>Converted value.</returns>
|
||||
public static char AsChar(this byte[] bytes)
|
||||
{
|
||||
return BitConverter.ToChar(bytes, 0);
|
||||
}
|
||||
}
|
||||
}
|
30
SweetLib/Utils/Extensions/DateTimeExtensions.cs
Normal file
30
SweetLib/Utils/Extensions/DateTimeExtensions.cs
Normal file
|
@ -0,0 +1,30 @@
|
|||
using System;
|
||||
|
||||
namespace SweetLib.Utils.Extensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines extensions to classes which are related to <see cref="DateTime"/>.
|
||||
/// </summary>
|
||||
public static class DateTimeExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> into an Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="date"><see cref="DateTime"/> to convert into Unix timestamp.</param>
|
||||
/// <returns>Converted Unix timestamp.</returns>
|
||||
public static double ToUnixTimeStamp(this DateTime date)
|
||||
{
|
||||
return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Unix timestamp into a <see cref="DateTime"/>.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">Unix timestamp to convert.</param>
|
||||
/// <returns>Converted <see cref="DateTime"/>.</returns>
|
||||
public static DateTime ToDateTime(this double timestamp)
|
||||
{
|
||||
return new DateTime(1970, 1, 1).AddMilliseconds(timestamp);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,4 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace SweetLib.Utils
|
||||
|
@ -36,25 +35,5 @@ namespace SweetLib.Utils
|
|||
|
||||
return invalidChars.Aggregate(fileName, (current, c) => current.Replace(c, replaceChar));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a <see cref="DateTime"/> into an Unix timestamp.
|
||||
/// </summary>
|
||||
/// <param name="date"><see cref="DateTime"/> to convert into Unix timestamp.</param>
|
||||
/// <returns>Converted Unix timestamp.</returns>
|
||||
public static double DateTimeToUnixTimeStamp(DateTime date)
|
||||
{
|
||||
return date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an Unix timestamp into a <see cref="DateTime"/>.
|
||||
/// </summary>
|
||||
/// <param name="timestamp">Unix timestamp to convert.</param>
|
||||
/// <returns>Converted <see cref="DateTime"/>.</returns>
|
||||
public static DateTime UnixTimestampToDateTime(double timestamp)
|
||||
{
|
||||
return new DateTime(1970, 1, 1).AddMilliseconds(timestamp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue