-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
317 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Command | ||
{ | ||
/// <summary> | ||
/// A common abstract class for each commands in macro. | ||
/// </summary> | ||
[JsonConverter(typeof(Converter.CommandBaseConverter))] | ||
public abstract class CommandBase | ||
{ | ||
public override abstract string ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Command.Converter | ||
{ | ||
public class CommandBaseConverter : JsonConverter | ||
{ | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(Command.CommandBase); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// This class is write-only. | ||
/// </summary> | ||
public override bool CanWrite => false; | ||
|
||
/// <summary> | ||
/// Throws NotImplementedException. This class is read-only. | ||
/// </summary> | ||
/// <param name="writer"></param> | ||
/// <param name="value"></param> | ||
/// <param name="serializer"></param> | ||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
throw new NotImplementedException("This class is read-only."); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// Deserialize JSON object as child class of CommandBase. | ||
/// This method first scans "CommandType" property of given JSON object, | ||
/// then calls ReadJson method on appropriate Converter class corresponding to "CommandType". | ||
/// </summary> | ||
/// <param name="reader"></param> | ||
/// <param name="objectType"></param> | ||
/// <param name="existingValue"></param> | ||
/// <param name="serializer"></param> | ||
/// <returns></returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var jobject = JObject.Load(reader); | ||
|
||
JsonConverter converter; | ||
switch ((string)jobject["CommandType"]) | ||
{ | ||
case "Wait": | ||
converter = new WaitConverter(); | ||
break; | ||
|
||
case "Button": | ||
converter = new OperateButtonConverter(); | ||
break; | ||
|
||
case "Stick": | ||
converter = new OperateStickConverter(); | ||
break; | ||
|
||
default: | ||
throw new JsonReaderException("Invalid CommandType specified"); | ||
} | ||
var newReader = jobject.CreateReader(); //As JsonReader cannot be used twice, create new one and pass it. | ||
return converter.ReadJson(newReader, objectType, existingValue, serializer); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
|
||
namespace Command.Converter | ||
{ | ||
public class OperateButtonConverter : JsonConverter | ||
{ | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(OperateButton); | ||
} | ||
|
||
|
||
/// <summary> | ||
/// This class is write-only. | ||
/// </summary> | ||
public override bool CanRead => false; | ||
|
||
|
||
/// <summary> | ||
/// Throws NotImplementedException. This class is write-only. | ||
/// </summary> | ||
/// <param name="reader"></param> | ||
/// <param name="objectType"></param> | ||
/// <param name="existingValue"></param> | ||
/// <param name="serializer"></param> | ||
/// <returns></returns> | ||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
JObject jobject = JObject.Load(reader); | ||
return new OperateButton( | ||
(ButtonID)Enum.Parse(typeof(ButtonID), (string)jobject["ButtonID"], ignoreCase: true), | ||
(ButtonState)(byte)jobject["TargetState"]); | ||
} | ||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var operation = (OperateButton)value; | ||
if (operation == null) throw new ArgumentException("value is not OperateButton", "value"); | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName("CommandType"); | ||
writer.WriteValue("Button"); | ||
writer.WritePropertyName("ButtonID"); | ||
writer.WriteValue(operation.ButtonID.ToString()); | ||
writer.WritePropertyName("TargetState"); | ||
writer.WriteValue(operation.TargetState); | ||
writer.WriteEndObject(); | ||
} | ||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Command.Converter | ||
{ | ||
public class OperateStickConverter : JsonConverter | ||
{ | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(Command.OperateStick); | ||
} | ||
|
||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
var jobject = JObject.Load(reader); | ||
return new OperateStick( | ||
(StickID)Enum.Parse(typeof(StickID), (string)jobject["StickID"], ignoreCase: true), | ||
(byte)jobject["X"], (byte)jobject["Y"]); | ||
} | ||
|
||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var operation = (Command.OperateStick)value; | ||
if (operation == null) throw new ArgumentException("value is not OperateButton", "value"); | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName("CommandType"); | ||
writer.WriteValue("Stick"); | ||
writer.WritePropertyName("StickID"); | ||
writer.WriteValue(operation.StickID.ToString()); | ||
writer.WritePropertyName("X"); | ||
writer.WriteValue(operation.TargetXAngle); | ||
writer.WritePropertyName("Y"); | ||
writer.WriteValue(operation.TargetYAngle); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using Newtonsoft.Json; | ||
using Newtonsoft.Json.Linq; | ||
|
||
namespace Command.Converter | ||
{ | ||
public class WaitConverter : JsonConverter | ||
{ | ||
|
||
public override bool CanConvert(Type objectType) | ||
{ | ||
return objectType == typeof(Command.Wait); | ||
} | ||
|
||
|
||
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) | ||
{ | ||
JObject jobject = JObject.Load(reader); | ||
return new Wait((int)jobject["WaitTime"]); | ||
} | ||
|
||
|
||
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) | ||
{ | ||
var operation = (Command.Wait)value; | ||
if (operation == null) throw new ArgumentException("value is not OperateButton", "value"); | ||
|
||
writer.WriteStartObject(); | ||
writer.WritePropertyName("CommandType"); | ||
writer.WriteValue("Wait"); | ||
writer.WritePropertyName("WaitTime"); | ||
writer.WriteValue(operation.WaitTime); | ||
writer.WriteEndObject(); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.