Reworked for Net 4.0 compatibility

fonts_experiment
Valeriy Mironov 2017-11-26 21:24:47 +02:00
parent 78c009f7e3
commit 067d0ce3fc
11 changed files with 74 additions and 86 deletions

View File

@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Resources</RootNamespace>
<AssemblyName>Resources</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -31,7 +32,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.4.12\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -40,7 +41,6 @@
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>

View File

@ -1,5 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="NLog" version="4.4.12" targetFramework="net461" />
<package id="NLog" version="4.4.12" targetFramework="net40-client" />
</packages>

View File

@ -50,8 +50,7 @@ namespace WatchFace.Parser
Images = new ResourcesReader(_stream).Read((uint) imagesCount);
}
private List<Parameter> ReadParameters(long coordinatesTableSize,
IReadOnlyCollection<Parameter> parametersDescriptors)
private List<Parameter> ReadParameters(long coordinatesTableSize, ICollection<Parameter> parametersDescriptors)
{
var parametersStream = StreamBlock(_stream, (int) coordinatesTableSize);

View File

@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using WatchFace.Parser.Attributes;
namespace WatchFace.Parser.Utils
{
internal class ElementsHelper
{
public static Dictionary<byte, PropertyInfo> SortedProperties<T>()
{
var typeInfo = typeof(T);
var properties = new Dictionary<byte, PropertyInfo>();
foreach (var propertyInfo in typeInfo.GetProperties())
{
var parameterIdAttribute = (ParameterIdAttribute) propertyInfo
.GetCustomAttributes(typeof(ParameterIdAttribute), false).FirstOrDefault();
if (parameterIdAttribute == null)
throw new ArgumentException(
$"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}"
);
if (properties.ContainsKey(parameterIdAttribute.Id))
throw new ArgumentException(
$"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}"
);
properties[parameterIdAttribute.Id] = propertyInfo;
}
return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
}
public static T GetCustomAttributeFor<T>(PropertyInfo propertyInfo)
{
return (T) propertyInfo.GetCustomAttributes(typeof(T), false).FirstOrDefault();
}
}
}

View File

@ -31,7 +31,7 @@ namespace WatchFace.Parser.Utils
long? lastImageIndexValue = null;
foreach (var kv in SortedPropertiesDictionary<T>())
foreach (var kv in ElementsHelper.SortedProperties<T>())
{
var id = kv.Key;
var currentPath = string.IsNullOrEmpty(path)
@ -42,12 +42,8 @@ namespace WatchFace.Parser.Utils
var propertyType = propertyInfo.PropertyType;
dynamic propertyValue = propertyInfo.GetValue(serializable, null);
var imageIndexAttribute = (ParameterImageIndexAttribute) propertyInfo.GetCustomAttribute(
typeof(ParameterImageIndexAttribute)
);
var imagesCountAttribute = (ParameterImagesCountAttribute) propertyInfo.GetCustomAttribute(
typeof(ParameterImagesCountAttribute)
);
var imageIndexAttribute =ElementsHelper.GetCustomAttributeFor<ParameterImageIndexAttribute>(propertyInfo);
var imagesCountAttribute =ElementsHelper.GetCustomAttributeFor<ParameterImagesCountAttribute>(propertyInfo);
if (imagesCountAttribute != null && imageIndexAttribute != null)
throw new ArgumentException(
@ -65,7 +61,7 @@ namespace WatchFace.Parser.Utils
lastImageIndexValue = imageIndex;
var mappedIndex = LoadImage(imageIndex);
propertyInfo.SetValue(serializable, mappedIndex);
propertyInfo.SetValue(serializable, mappedIndex, null);
}
else if (imagesCountAttribute != null)
{
@ -112,27 +108,5 @@ namespace WatchFace.Parser.Utils
_mapping[index] = newImageIndex;
return newImageIndex;
}
private static Dictionary<byte, PropertyInfo> SortedPropertiesDictionary<T>()
{
var typeInfo = typeof(T).GetTypeInfo();
var properties = new Dictionary<byte, PropertyInfo>();
foreach (var propertyInfo in typeInfo.DeclaredProperties)
{
var parameterIdAttribute =
(ParameterIdAttribute) propertyInfo.GetCustomAttribute(typeof(ParameterIdAttribute));
if (parameterIdAttribute == null)
throw new ArgumentException(
$"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}"
);
if (properties.ContainsKey(parameterIdAttribute.Id))
throw new ArgumentException(
$"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}"
);
properties[parameterIdAttribute.Id] = propertyInfo;
}
return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
}

View File

@ -19,7 +19,7 @@ namespace WatchFace.Parser.Utils
if (!string.IsNullOrEmpty(path))
Logger.Trace("{0} '{1}'", path, currentType.Name);
foreach (var kv in SortedPropertiesDictionary<T>())
foreach (var kv in ElementsHelper.SortedProperties<T>())
{
var id = kv.Key;
var currentPath = string.IsNullOrEmpty(path)
@ -58,7 +58,7 @@ namespace WatchFace.Parser.Utils
public static T Parse<T>(List<Parameter> descriptor, string path = "") where T : new()
{
var properties = SortedPropertiesDictionary<T>();
var properties = ElementsHelper.SortedProperties<T>();
var result = new T();
var currentType = typeof(T);
@ -81,7 +81,7 @@ namespace WatchFace.Parser.Utils
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, parameter.Value);
dynamic propertyValue = propertyInfo.GetValue(result);
dynamic propertyValue = propertyInfo.GetValue(result, null);
if (propertyType.IsGenericType && propertyValue != null)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
@ -89,15 +89,15 @@ namespace WatchFace.Parser.Utils
if (!propertyType.IsGenericType && propertyValue != 0)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
propertyInfo.SetValue(result, parameter.Value);
propertyInfo.SetValue(result, parameter.Value, null);
}
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{
dynamic propertyValue = propertyInfo.GetValue(result);
dynamic propertyValue = propertyInfo.GetValue(result, null);
if (propertyValue == null)
{
propertyValue = Activator.CreateInstance(propertyType);
propertyInfo.SetValue(result, propertyValue);
propertyInfo.SetValue(result, propertyValue, null);
}
try
@ -114,7 +114,7 @@ namespace WatchFace.Parser.Utils
}
else
{
dynamic propertyValue = propertyInfo.GetValue(result);
dynamic propertyValue = propertyInfo.GetValue(result, null);
if (propertyValue != null)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
@ -122,7 +122,7 @@ namespace WatchFace.Parser.Utils
{
var generic = thisMethod.MakeGenericMethod(propertyType);
dynamic parsedValue = generic.Invoke(null, new object[] {parameter.Children, currentPath});
propertyInfo.SetValue(result, parsedValue);
propertyInfo.SetValue(result, parsedValue, null);
}
catch (TargetInvocationException e)
{
@ -132,27 +132,5 @@ namespace WatchFace.Parser.Utils
}
return result;
}
private static Dictionary<byte, PropertyInfo> SortedPropertiesDictionary<T>()
{
var typeInfo = typeof(T).GetTypeInfo();
var properties = new Dictionary<byte, PropertyInfo>();
foreach (var propertyInfo in typeInfo.DeclaredProperties)
{
var parameterIdAttribute =
(ParameterIdAttribute) propertyInfo.GetCustomAttribute(typeof(ParameterIdAttribute));
if (parameterIdAttribute == null)
throw new ArgumentException(
$"Class {typeInfo.Name} doesn't have ParameterIdAttribute on property {propertyInfo.Name}"
);
if (properties.ContainsKey(parameterIdAttribute.Id))
throw new ArgumentException(
$"Class {typeInfo.Name} already has ParameterIdAttribute with Id {parameterIdAttribute.Id}"
);
properties[parameterIdAttribute.Id] = propertyInfo;
}
return properties.OrderBy(kv => kv.Key).ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
}

View File

@ -9,8 +9,9 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WatchFace.Parser</RootNamespace>
<AssemblyName>WatchFace.Parser</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -31,10 +32,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.4.12\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
@ -83,6 +84,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" />
<Compile Include="Attributes\ParameterImagesCountAttribute.cs" />
<Compile Include="Utils\ElementsHelper.cs" />
<Compile Include="Utils\ImagesLoader.cs" />
<Compile Include="Utils\ParametersConverter.cs" />
<Compile Include="Attributes\ParameterImageIndexAttribute.cs" />
@ -90,14 +92,14 @@
<Compile Include="WatchFace.cs" />
<Compile Include="Writer.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Resources\Resources.csproj">
<Project>{edd55d5d-9e80-451b-ac8a-0746ba6dc6e9}</Project>
<Name>Resources</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="NLog" version="4.4.12" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40-client" />
<package id="NLog" version="4.4.12" targetFramework="net40-client" />
</packages>

View File

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
</startup>
</configuration>
</configuration>

View File

@ -8,11 +8,12 @@
<OutputType>Exe</OutputType>
<RootNamespace>WatchFace</RootNamespace>
<AssemblyName>WatchFace</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<NuGetPackageImportStamp>
</NuGetPackageImportStamp>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
@ -35,10 +36,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\packages\Newtonsoft.Json.10.0.3\lib\net40\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.4.12\lib\net45\NLog.dll</HintPath>
<HintPath>..\packages\NLog.4.4.12\lib\net40\NLog.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />

View File

@ -1,6 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net461" />
<package id="NLog" version="4.4.12" targetFramework="net461" />
<package id="Newtonsoft.Json" version="10.0.3" targetFramework="net40-client" />
<package id="NLog" version="4.4.12" targetFramework="net40-client" />
</packages>