summaryrefslogtreecommitdiff
path: root/Client/Assets/Scripts/XMainClient/ProtoBuf/Serializers/ParseableSerializer.cs
blob: 8cc1b68df473462e89037bf921e42731288ed1f1 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Reflection;
using ProtoBuf.Meta;

namespace ProtoBuf.Serializers
{
	internal sealed class ParseableSerializer : IProtoSerializer
	{
		public Type ExpectedType
		{
			get
			{
				return this.parse.DeclaringType;
			}
		}

		bool IProtoSerializer.RequiresOldValue
		{
			get
			{
				return false;
			}
		}

		bool IProtoSerializer.ReturnsValue
		{
			get
			{
				return true;
			}
		}

		private readonly MethodInfo parse;

		public static ParseableSerializer TryCreate(Type type, TypeModel model)
		{
			bool flag = type == null;
			if (flag)
			{
				throw new ArgumentNullException("type");
			}
			MethodInfo method = type.GetMethod("Parse", BindingFlags.DeclaredOnly | BindingFlags.Static | BindingFlags.Public, null, new Type[]
			{
				model.MapType(typeof(string))
			}, null);
			bool flag2 = method != null && method.ReturnType == type;
			ParseableSerializer result;
			if (flag2)
			{
				bool flag3 = Helpers.IsValueType(type);
				if (flag3)
				{
					MethodInfo customToString = ParseableSerializer.GetCustomToString(type);
					bool flag4 = customToString == null || customToString.ReturnType != model.MapType(typeof(string));
					if (flag4)
					{
						return null;
					}
				}
				result = new ParseableSerializer(method);
			}
			else
			{
				result = null;
			}
			return result;
		}

		private static MethodInfo GetCustomToString(Type type)
		{
			return type.GetMethod("ToString", BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public, null, Helpers.EmptyTypes, null);
		}

		private ParseableSerializer(MethodInfo parse)
		{
			this.parse = parse;
		}

		public object Read(object value, ProtoReader source)
		{
			Helpers.DebugAssert(value == null);
			ProtoDecoratorBase.s_argsRead[0] = source.ReadString();
			return this.parse.Invoke(null, ProtoDecoratorBase.s_argsRead);
		}

		public void Write(object value, ProtoWriter dest)
		{
			ProtoWriter.WriteString(value.ToString(), dest);
		}
	}
}