summaryrefslogtreecommitdiff
path: root/Xamarin.Forms.Build.Tasks/CompiledConverters/RectangleTypeConverter.cs
blob: af480667cc3d24d2c86b85ef86c4dae4b6967003 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;

using Mono.Cecil;
using Mono.Cecil.Cil;

using Xamarin.Forms.Xaml;

namespace Xamarin.Forms.Core.XamlC
{
	class RectangleTypeConverter : ICompiledTypeConverter
	{
		public IEnumerable<Instruction> ConvertFromString(string value, ModuleDefinition module, BaseNode node)
		{
			if (string.IsNullOrEmpty(value))
				throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Rectangle)}", node);
			double x, y, w, h;
			var xywh = value.Split(',');
			if (xywh.Length != 4 ||
				!double.TryParse(xywh [0], NumberStyles.Number, CultureInfo.InvariantCulture, out x) ||
				!double.TryParse(xywh [1], NumberStyles.Number, CultureInfo.InvariantCulture, out y) ||
				!double.TryParse(xywh [2], NumberStyles.Number, CultureInfo.InvariantCulture, out w) ||
				!double.TryParse(xywh [3], NumberStyles.Number, CultureInfo.InvariantCulture, out h))
				throw new XamlParseException($"Cannot convert \"{value}\" into {typeof(Rectangle)}", node);

			return GenerateIL(x, y, w, h, module);
		}

		IEnumerable<Instruction> GenerateIL(double x, double y, double w, double h, ModuleDefinition module)
		{
//			IL_0000:  ldc.r8 3.1000000000000001
//			IL_0009:  ldc.r8 4.2000000000000002
//			IL_0012:  ldc.r8 5.2999999999999998
//			IL_001b:  ldc.r8 6.4000000000000004
//			IL_0024:  newobj instance void valuetype Test.Rectangle::'.ctor'(float64, float64, float64, float64)

			yield return Instruction.Create(OpCodes.Ldc_R8, x);
			yield return Instruction.Create(OpCodes.Ldc_R8, y);
			yield return Instruction.Create(OpCodes.Ldc_R8, w);
			yield return Instruction.Create(OpCodes.Ldc_R8, h);

			var rectangleCtor = module.Import(typeof(Rectangle)).Resolve().Methods.FirstOrDefault(md => md.IsConstructor && md.Parameters.Count == 4);
			var rectangleCtorRef = module.Import(rectangleCtor);
			yield return Instruction.Create(OpCodes.Newobj, rectangleCtorRef);
		}
	}
}