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

View File

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

View File

@ -50,8 +50,7 @@ namespace WatchFace.Parser
Images = new ResourcesReader(_stream).Read((uint) imagesCount); Images = new ResourcesReader(_stream).Read((uint) imagesCount);
} }
private List<Parameter> ReadParameters(long coordinatesTableSize, private List<Parameter> ReadParameters(long coordinatesTableSize, ICollection<Parameter> parametersDescriptors)
IReadOnlyCollection<Parameter> parametersDescriptors)
{ {
var parametersStream = StreamBlock(_stream, (int) coordinatesTableSize); 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; long? lastImageIndexValue = null;
foreach (var kv in SortedPropertiesDictionary<T>()) foreach (var kv in ElementsHelper.SortedProperties<T>())
{ {
var id = kv.Key; var id = kv.Key;
var currentPath = string.IsNullOrEmpty(path) var currentPath = string.IsNullOrEmpty(path)
@ -42,12 +42,8 @@ namespace WatchFace.Parser.Utils
var propertyType = propertyInfo.PropertyType; var propertyType = propertyInfo.PropertyType;
dynamic propertyValue = propertyInfo.GetValue(serializable, null); dynamic propertyValue = propertyInfo.GetValue(serializable, null);
var imageIndexAttribute = (ParameterImageIndexAttribute) propertyInfo.GetCustomAttribute( var imageIndexAttribute =ElementsHelper.GetCustomAttributeFor<ParameterImageIndexAttribute>(propertyInfo);
typeof(ParameterImageIndexAttribute) var imagesCountAttribute =ElementsHelper.GetCustomAttributeFor<ParameterImagesCountAttribute>(propertyInfo);
);
var imagesCountAttribute = (ParameterImagesCountAttribute) propertyInfo.GetCustomAttribute(
typeof(ParameterImagesCountAttribute)
);
if (imagesCountAttribute != null && imageIndexAttribute != null) if (imagesCountAttribute != null && imageIndexAttribute != null)
throw new ArgumentException( throw new ArgumentException(
@ -65,7 +61,7 @@ namespace WatchFace.Parser.Utils
lastImageIndexValue = imageIndex; lastImageIndexValue = imageIndex;
var mappedIndex = LoadImage(imageIndex); var mappedIndex = LoadImage(imageIndex);
propertyInfo.SetValue(serializable, mappedIndex); propertyInfo.SetValue(serializable, mappedIndex, null);
} }
else if (imagesCountAttribute != null) else if (imagesCountAttribute != null)
{ {
@ -112,27 +108,5 @@ namespace WatchFace.Parser.Utils
_mapping[index] = newImageIndex; _mapping[index] = newImageIndex;
return 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)) if (!string.IsNullOrEmpty(path))
Logger.Trace("{0} '{1}'", path, currentType.Name); 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 id = kv.Key;
var currentPath = string.IsNullOrEmpty(path) 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() 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 result = new T();
var currentType = typeof(T); var currentType = typeof(T);
@ -81,7 +81,7 @@ namespace WatchFace.Parser.Utils
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{ {
Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, parameter.Value); 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) if (propertyType.IsGenericType && propertyValue != null)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); 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) if (!propertyType.IsGenericType && propertyValue != 0)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); 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<>)) else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{ {
dynamic propertyValue = propertyInfo.GetValue(result); dynamic propertyValue = propertyInfo.GetValue(result, null);
if (propertyValue == null) if (propertyValue == null)
{ {
propertyValue = Activator.CreateInstance(propertyType); propertyValue = Activator.CreateInstance(propertyType);
propertyInfo.SetValue(result, propertyValue); propertyInfo.SetValue(result, propertyValue, null);
} }
try try
@ -114,7 +114,7 @@ namespace WatchFace.Parser.Utils
} }
else else
{ {
dynamic propertyValue = propertyInfo.GetValue(result); dynamic propertyValue = propertyInfo.GetValue(result, null);
if (propertyValue != null) if (propertyValue != null)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}"); 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); var generic = thisMethod.MakeGenericMethod(propertyType);
dynamic parsedValue = generic.Invoke(null, new object[] {parameter.Children, currentPath}); dynamic parsedValue = generic.Invoke(null, new object[] {parameter.Children, currentPath});
propertyInfo.SetValue(result, parsedValue); propertyInfo.SetValue(result, parsedValue, null);
} }
catch (TargetInvocationException e) catch (TargetInvocationException e)
{ {
@ -132,27 +132,5 @@ namespace WatchFace.Parser.Utils
} }
return result; 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> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>WatchFace.Parser</RootNamespace> <RootNamespace>WatchFace.Parser</RootNamespace>
<AssemblyName>WatchFace.Parser</AssemblyName> <AssemblyName>WatchFace.Parser</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment> <FileAlignment>512</FileAlignment>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols> <DebugSymbols>true</DebugSymbols>
@ -31,10 +32,10 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <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>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL"> <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>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.Core" /> <Reference Include="System.Core" />
@ -83,6 +84,7 @@
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Reader.cs" /> <Compile Include="Reader.cs" />
<Compile Include="Attributes\ParameterImagesCountAttribute.cs" /> <Compile Include="Attributes\ParameterImagesCountAttribute.cs" />
<Compile Include="Utils\ElementsHelper.cs" />
<Compile Include="Utils\ImagesLoader.cs" /> <Compile Include="Utils\ImagesLoader.cs" />
<Compile Include="Utils\ParametersConverter.cs" /> <Compile Include="Utils\ParametersConverter.cs" />
<Compile Include="Attributes\ParameterImageIndexAttribute.cs" /> <Compile Include="Attributes\ParameterImageIndexAttribute.cs" />
@ -90,14 +92,14 @@
<Compile Include="WatchFace.cs" /> <Compile Include="WatchFace.cs" />
<Compile Include="Writer.cs" /> <Compile Include="Writer.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Resources\Resources.csproj"> <ProjectReference Include="..\Resources\Resources.csproj">
<Project>{edd55d5d-9e80-451b-ac8a-0746ba6dc6e9}</Project> <Project>{edd55d5d-9e80-451b-ac8a-0746ba6dc6e9}</Project>
<Name>Resources</Name> <Name>Resources</Name>
</ProjectReference> </ProjectReference>
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

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

View File

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

View File

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

View File

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