Fixed packing negative numbers, allowed some images be optional

fonts_experiment
Valeriy Mironov 2017-11-25 23:02:33 +02:00
parent e23093230e
commit 4767e9877d
6 changed files with 37 additions and 16 deletions

View File

@ -10,10 +10,10 @@ namespace WatchFace.Parser.Elements.ActivityElements
[ParameterId(2)] [ParameterId(2)]
[ParameterImageIndex] [ParameterImageIndex]
public long SuffixImageIndex { get; set; } public long? SuffixImageIndex { get; set; }
[ParameterId(3)] [ParameterId(3)]
[ParameterImageIndex] [ParameterImageIndex]
public long DecimalPointImageIndex { get; set; } public long? DecimalPointImageIndex { get; set; }
} }
} }

View File

@ -10,10 +10,10 @@ namespace WatchFace.Parser.Elements.WeatherElements
[ParameterId(2)] [ParameterId(2)]
[ParameterImageIndex] [ParameterImageIndex]
public long MinusImageIndex { get; set; } public long? MinusImageIndex { get; set; }
[ParameterId(3)] [ParameterId(3)]
[ParameterImageIndex] [ParameterImageIndex]
public long DegreesImageIndex { get; set; } public long? DegreesImageIndex { get; set; }
} }
} }

View File

@ -38,8 +38,9 @@ namespace WatchFace.Parser.Models
size += 1; size += 1;
if (HasChildren) if (HasChildren)
{ {
Logger.Trace(() => TraceWithOffset($"{Id} ({rawId:X2}):", traceOffset));
size += WriteList(stream, traceOffset + 1); size += WriteList(stream, traceOffset + 1);
Logger.Trace(() => TraceWithOffset($"{Id} ({rawId:X2}): {size} bytes", traceOffset)); Logger.Trace(() => TraceWithOffset($"{size} bytes", traceOffset));
return size; return size;
} }
size += WriteValue(stream, Value, traceOffset); size += WriteValue(stream, Value, traceOffset);
@ -60,16 +61,17 @@ namespace WatchFace.Parser.Models
public long WriteValue(Stream stream, long value, int traceOffset) public long WriteValue(Stream stream, long value, int traceOffset)
{ {
var unsignedValue = (ulong) value;
var size = 0; var size = 0;
byte currentByte; byte currentByte;
while (value >= 0x80) while (unsignedValue >= 0x80)
{ {
currentByte = (byte) ((value & 0x7f) | 0x80); currentByte = (byte) ((unsignedValue & 0x7f) | 0x80);
stream.WriteByte(currentByte); stream.WriteByte(currentByte);
size += 1; size += 1;
value = value >> 7; unsignedValue = unsignedValue >> 7;
} }
currentByte = (byte) (value & 0x7f); currentByte = (byte) (unsignedValue & 0x7f);
stream.WriteByte(currentByte); stream.WriteByte(currentByte);
size += 1; size += 1;
return size; return size;

View File

@ -54,12 +54,17 @@ namespace WatchFace.Parser.Utils
$"Property {propertyInfo.Name} can't have both ParameterImageIndexAttribute and ParameterImagesCountAttribute" $"Property {propertyInfo.Name} can't have both ParameterImageIndexAttribute and ParameterImagesCountAttribute"
); );
if (propertyType == typeof(long) || (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))) if (propertyType == typeof(long) || propertyType.IsGenericType &&
(propertyType.GetGenericTypeDefinition() == typeof(List<>) ||
propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{ {
if (imageIndexAttribute != null) if (imageIndexAttribute != null)
{ {
lastImageIndexValue = propertyValue; if (propertyValue == null) continue;
var mappedIndex = LoadImage(propertyValue); long imageIndex = propertyValue;
lastImageIndexValue = imageIndex;
var mappedIndex = LoadImage(imageIndex);
propertyInfo.SetValue(serializable, mappedIndex); propertyInfo.SetValue(serializable, mappedIndex);
} }
else if (imagesCountAttribute != null) else if (imagesCountAttribute != null)
@ -69,7 +74,11 @@ namespace WatchFace.Parser.Utils
$"Property {propertyInfo.Name} can't be processed becuase ImageIndex isn't present or it is zero" $"Property {propertyInfo.Name} can't be processed becuase ImageIndex isn't present or it is zero"
); );
var imagesCount = propertyType.IsGenericType ? propertyValue.Count : propertyValue; var imagesCount = propertyType.IsGenericType
? (propertyType.GetGenericTypeDefinition() == typeof(Nullable<>)
? propertyValue.Value
: propertyValue.Count)
: propertyValue;
for (var i = lastImageIndexValue + 1; i < lastImageIndexValue + imagesCount; i++) for (var i = lastImageIndexValue + 1; i < lastImageIndexValue + imagesCount; i++)
LoadImage(i.Value); LoadImage(i.Value);

View File

@ -38,6 +38,11 @@ namespace WatchFace.Parser.Utils
Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, propertyValue); Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, propertyValue);
result.Add(new Parameter(id, propertyValue)); result.Add(new Parameter(id, propertyValue));
} }
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
Logger.Trace("{0} '{1}': {2}", currentPath, propertyInfo.Name, propertyValue);
result.Add(new Parameter(id, propertyValue));
}
else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>)) else if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(List<>))
{ {
foreach (var item in propertyValue) foreach (var item in propertyValue)
@ -72,11 +77,16 @@ namespace WatchFace.Parser.Utils
var propertyInfo = properties[parameter.Id]; var propertyInfo = properties[parameter.Id];
var propertyType = propertyInfo.PropertyType; var propertyType = propertyInfo.PropertyType;
if (propertyType == typeof(long)) if (propertyType == typeof(long) || propertyType.IsGenericType &&
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);
if (propertyValue != 0)
if (propertyType.IsGenericType && propertyValue != null)
throw new ArgumentException($"Parameter {parameter.Id} is already set for {currentType.Name}");
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);

View File

@ -61,7 +61,7 @@ namespace WatchFace.Parser
var header = new Header var header = new Header
{ {
ParametersSize = (uint) encodedParametersPositions.Length, ParametersSize = (uint) encodedParametersPositions.Length,
Unknown = 0x37 // Value from Sydney (watchface with 0 doesn't work) Unknown = 0x159 // Maybe some kind of layers (the bigger number needed for more complex watch faces)
}; };
header.WriteTo(_stream); header.WriteTo(_stream);