Skip to content
This repository was archived by the owner on May 16, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ZeroFormatter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sandbox.Shared", "sandbox\S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PerformanceComparison", "sandbox\PerformanceComparison\PerformanceComparison.csproj", "{0FB6577C-2FCD-430B-8BB3-E4ED0833E8FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZeroFormatter.Portable", "src\ZeroFormatter.Portable\ZeroFormatter.Portable.csproj", "{EEB02197-AE4B-4559-BF3D-D187AD69744F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ZeroFormatter.Interfaces.Portable", "src\ZeroFormatter.Interfaces.Portable\ZeroFormatter.Interfaces.Portable.csproj", "{F1365355-4B82-4A13-9865-25573E5DBEB0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -133,6 +137,18 @@ Global
{0FB6577C-2FCD-430B-8BB3-E4ED0833E8FC}.Release|Any CPU.Build.0 = Release|Any CPU
{0FB6577C-2FCD-430B-8BB3-E4ED0833E8FC}.Unity|Any CPU.ActiveCfg = Release|Any CPU
{0FB6577C-2FCD-430B-8BB3-E4ED0833E8FC}.Unity|Any CPU.Build.0 = Release|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Release|Any CPU.Build.0 = Release|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Unity|Any CPU.ActiveCfg = Release|Any CPU
{EEB02197-AE4B-4559-BF3D-D187AD69744F}.Unity|Any CPU.Build.0 = Release|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Release|Any CPU.Build.0 = Release|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Unity|Any CPU.ActiveCfg = Release|Any CPU
{F1365355-4B82-4A13-9865-25573E5DBEB0}.Unity|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -151,5 +167,7 @@ Global
{0763A385-4EE0-45F3-8B20-0B0A71C74870} = {4A106901-0A11-4D37-B5E6-D4DB7A43C41C}
{944F22D3-20B1-4F6D-83FA-9331CD4ADF75} = {F29BFD74-5462-485D-851E-69B683F08C84}
{0FB6577C-2FCD-430B-8BB3-E4ED0833E8FC} = {F29BFD74-5462-485D-851E-69B683F08C84}
{EEB02197-AE4B-4559-BF3D-D187AD69744F} = {2CE059FD-1C8D-49B7-851E-4A5B385F63D5}
{F1365355-4B82-4A13-9865-25573E5DBEB0} = {2CE059FD-1C8D-49B7-851E-4A5B385F63D5}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{f1365355-4b82-4a13-9865-25573e5dbeb0}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZeroFormatter.Interfaces.Portable</RootNamespace>
<AssemblyName>ZeroFormatter.Interfaces.Portable</AssemblyName>
<DefaultLanguage>ja-JP</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile111</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .NET Framework is automatically included -->
</ItemGroup>
<ItemGroup>
<Compile Include="..\ZeroFormatter.Interfaces\*.cs" />
<Compile Include="..\ZeroFormatter.Interfaces\*\*.cs" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->

</Project>
68 changes: 68 additions & 0 deletions src/ZeroFormatter.Portable/Original/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ZeroFormatter
{
#if PORTABLE
public enum TypeCode
{
Unknown,
Boolean,
Char,
SByte,
Byte,
Int16,
UInt16,
Int32,
UInt32,
Int64,
UInt64,
Single,
Double,
Decimal,
DateTime,
String,
TimeSpan,
ByteArray,
Guid,
Uri,
Type
}

public static class Helpers
{
private static readonly Dictionary<Type, TypeCode> KnownTypes = new Dictionary<Type, TypeCode>
{
{ typeof(bool), TypeCode.Boolean },
{ typeof(char), TypeCode.Char },
{ typeof(sbyte), TypeCode.SByte },
{ typeof(byte), TypeCode.Byte },
{ typeof(short), TypeCode.Int16 },
{ typeof(ushort), TypeCode.UInt16 },
{ typeof(int), TypeCode.Int32 },
{ typeof(uint), TypeCode.UInt32 },
{ typeof(long), TypeCode.Int64 },
{ typeof(ulong), TypeCode.UInt64 },
{ typeof(float), TypeCode.Single },
{ typeof(double), TypeCode.Double },
{ typeof(decimal), TypeCode.Decimal },
{ typeof(string), TypeCode.String },
{ typeof(DateTime), TypeCode.DateTime },
{ typeof(TimeSpan), TypeCode.TimeSpan },
{ typeof(Guid), TypeCode.Guid },
{ typeof(Uri), TypeCode.Uri },
{ typeof(byte[]), TypeCode.ByteArray },
{ typeof(Type), TypeCode.Type },
};

public static TypeCode GetTypeCode(Type type)
{
TypeCode code;
return KnownTypes.TryGetValue(type, out code) ? code : TypeCode.Unknown;
}
}
#endif
}
60 changes: 60 additions & 0 deletions src/ZeroFormatter.Portable/ZeroFormatter.Portable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<MinimumVisualStudioVersion>10.0</MinimumVisualStudioVersion>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EEB02197-AE4B-4559-BF3D-D187AD69744F}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZeroFormatter.Portable</RootNamespace>
<AssemblyName>ZeroFormatter.Portable</AssemblyName>
<DefaultLanguage>ja-JP</DefaultLanguage>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>TRACE;DEBUG;UNITY;NET_CORE;PORTABLE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;UNITY;NET_CORE;PORTABLE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\ZeroFormatter\*.cs" />
<Compile Include="..\ZeroFormatter\*\*.cs" />
<Compile Include="Original\Helpers.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ZeroFormatter.Interfaces\ZeroFormatter.Interfaces.csproj">
<Project>{c051f473-427f-4062-94fb-cae3fa460fc8}</Project>
<Name>ZeroFormatter.Interfaces</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.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.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
49 changes: 47 additions & 2 deletions src/ZeroFormatter/Formatters/Formatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,46 @@ internal static object GetBuiltinFormatter<TTypeResolver
}
else
{
#if PORTABLE
switch (Helpers.GetTypeCode(elementType))
{
case TypeCode.Boolean:
formatter = new BooleanArrayFormatter<TTypeResolver>();
break;
case TypeCode.Char:
formatter = new CharArrayFormatter<TTypeResolver>();
break;
case TypeCode.SByte:
formatter = new SByteArrayFormatter<TTypeResolver>();
break;
case TypeCode.Byte:
formatter = new ByteArrayFormatter<TTypeResolver>();
break;
case TypeCode.Int16:
formatter = new Int16ArrayFormatter<TTypeResolver>();
break;
case TypeCode.UInt16:
formatter = new UInt16ArrayFormatter<TTypeResolver>();
break;
case TypeCode.Int32:
formatter = new Int32ArrayFormatter<TTypeResolver>();
break;
case TypeCode.UInt32:
formatter = new UInt32ArrayFormatter<TTypeResolver>();
break;
case TypeCode.Int64:
formatter = new Int64ArrayFormatter<TTypeResolver>();
break;
case TypeCode.UInt64:
formatter = new UInt64ArrayFormatter<TTypeResolver>();
break;
case TypeCode.Single:
formatter = new SingleArrayFormatter<TTypeResolver>();
break;
case TypeCode.Double:
formatter = new DoubleArrayFormatter<TTypeResolver>();
break;
#else
switch (Type.GetTypeCode(elementType))
{
case TypeCode.Boolean:
Expand Down Expand Up @@ -401,12 +441,13 @@ internal static object GetBuiltinFormatter<TTypeResolver
case TypeCode.Double:
formatter = new DoubleArrayFormatter<TTypeResolver>();
break;
default:
#endif
#if !UNITY
default:
var formatterType = typeof(ArrayFormatter<,>).MakeGenericType(typeof(TTypeResolver), elementType);
formatter = Activator.CreateInstance(formatterType);
#endif
break;
#endif
}
}
}
Expand Down Expand Up @@ -1057,7 +1098,11 @@ public ErrorFormatter()
if (ti.IsGenericType)
{
// depth 1 only:)
#if PORTABLE
message = t.Name + "<" + string.Join(", ", ti.GenericTypeArguments.Select(x => x.Name).ToArray()) + ">";
#else
message = t.Name + "<" + string.Join(", ", ti.GetGenericArguments().Select(x => x.Name).ToArray()) + ">";
#endif
}
else
{
Expand Down
2 changes: 1 addition & 1 deletion src/ZeroFormatter/Internal/PseudoTypeInfo.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#if UNITY
#if UNITY && !PORTABLE

using System;

Expand Down
25 changes: 17 additions & 8 deletions src/ZeroFormatter/ZeroFormatterSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public static void Serialize<T>(Stream stream, T obj)
{
var bufferRef = zeroFormatterObj.GetBufferReference();

#if NET_CORE
#if NET_CORE && !PORTABLE
ArraySegment<byte> buf;
if (ms.TryGetBuffer(out buf))
{
Expand All @@ -315,6 +315,11 @@ public static void Serialize<T>(Stream stream, T obj)
Buffer.BlockCopy(bufferRef.Array, bufferRef.Offset, dest, 0, bufferRef.Count);
return;
}
#elif PORTABLE
ms.SetLength(bufferRef.Count);
var dest = ms.ToArray();
Buffer.BlockCopy(bufferRef.Array, bufferRef.Offset, dest, 0, bufferRef.Count);
return;
#else
ms.SetLength(bufferRef.Count);
var dest = ms.GetBuffer();
Expand Down Expand Up @@ -355,14 +360,18 @@ public static T Deserialize<T>(Stream stream)
var tracker = formatter.NoUseDirtyTracker ? DirtyTracker.NullTracker : new DirtyTracker();
if (ms != null)
{
#if NET_CORE
ArraySegment<byte> b;
if (ms.TryGetBuffer(out b))
{
var buffer = b.Array;
#if NET_CORE &&!PORTABLE
ArraySegment<byte> b;
if (ms.TryGetBuffer(out b))
{
var buffer = b.Array;
int _;
return formatter.Deserialize(ref buffer, b.Offset, tracker, out _);
}
#elif PORTABLE
var buffer = ms.ToArray();
int _;
return formatter.Deserialize(ref buffer, b.Offset, tracker, out _);
}
return formatter.Deserialize(ref buffer, (int)ms.Position, tracker, out _);
#else
var buffer = ms.GetBuffer();
int _;
Expand Down
Loading