OSDN Git Service

Block pos Protocol buffer fix (workaround)
[automap/automap.git] / Automap / Data / JSON / BlockPosJson.cs
1 using System;
2
3 using Newtonsoft.Json;
4
5 using ProtoBuf;
6
7 using Vintagestory.API.MathTools;
8
9 namespace Automap
10 {
11         //ProtocolBuffer does not actually INHERIT the base class without an extra attribute on it...'lovely, just freakin' lovely.'
12
13         [JsonConverter(typeof(BlockPosConverter))]
14         [ProtoContract(ImplicitFields = ImplicitFields.None)]
15         public class BlockPosJson : BlockPos
16         {
17
18                 public BlockPosJson()
19                 {
20
21                 }
22
23                 public BlockPosJson(BlockPos orig) : base(x: orig.X, y: orig.Y, z: orig.Z)
24                 {
25                 }
26
27                 [ProtoMember(1)]
28                 public new int X {
29                         get { return base.X; }
30                         set { base.X = value; }
31                 }
32
33                 [ProtoMember(2)]
34                 public new int Y {
35                         get { return base.Y; }
36                         set { base.Y = value; }
37                 }
38
39                 [ProtoMember(3)]
40                 public int Z {
41                         get { return base.Z; }
42                         set { base.Z = value; }
43                 }
44         }
45
46         /// <summary>
47         /// How JSON object conversion should be done for special formatting
48         /// </summary>
49         public class BlockPosConverter : JsonConverter
50         {
51                 public override bool CanConvert(Type objectType)
52                 {
53                 return objectType == typeof(BlockPosJson);
54                 }
55
56                 public override bool CanRead {
57                         get
58                         {
59                         return false;
60                         }
61                 }
62
63                 public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
64                 {
65                 throw new NotImplementedException( );
66                 }
67
68                 public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
69                 {
70                 if (value != null) {
71                 BlockPosJson bpj = value as BlockPosJson;
72
73                 writer.WriteStartArray( );
74                 writer.WriteValue(bpj.X);
75                 writer.WriteValue(bpj.Y);
76                 writer.WriteValue(bpj.Z);
77                 writer.WriteEndArray( );
78                 }
79                 else writer.WriteNull( );
80
81                 }
82
83         }
84 }
85