Command.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace TapSDK.Core
  4. {
  5. public class Command
  6. {
  7. [SerializeField] public string service;
  8. [SerializeField] public string method;
  9. [SerializeField] public string args;
  10. [SerializeField] public bool withCallback;
  11. [SerializeField] public string callbackId;
  12. [SerializeField] public bool disposable;
  13. public Command()
  14. {
  15. }
  16. public Command(string json)
  17. {
  18. JsonUtility.FromJsonOverwrite(json, this);
  19. }
  20. public string ToJSON()
  21. {
  22. return JsonUtility.ToJson(this);
  23. }
  24. public Command(string service, string method, bool callback, Dictionary<string, object> dic)
  25. {
  26. this.args = dic == null ? null : Json.Serialize(dic);
  27. this.service = service;
  28. this.method = method;
  29. this.withCallback = callback;
  30. this.callbackId = this.withCallback ? TapUUID.UUID() : null;
  31. }
  32. public Command(string service, string method, bool callback, bool onceTime, Dictionary<string, object> dic)
  33. {
  34. this.args = dic == null ? null : Json.Serialize(dic);
  35. this.service = service;
  36. this.method = method;
  37. this.withCallback = callback;
  38. this.callbackId = this.withCallback ? TapUUID.UUID() : null;
  39. this.disposable = onceTime;
  40. }
  41. public class Builder
  42. {
  43. private string service;
  44. private string method;
  45. private bool withCallback;
  46. private string callbackId;
  47. private bool disposable;
  48. private Dictionary<string, object> args;
  49. public Builder()
  50. {
  51. }
  52. public Builder Service(string service)
  53. {
  54. this.service = service;
  55. return this;
  56. }
  57. public Builder Method(string method)
  58. {
  59. this.method = method;
  60. return this;
  61. }
  62. public Builder OnceTime(bool onceTime)
  63. {
  64. this.disposable = onceTime;
  65. return this;
  66. }
  67. public Builder Args(Dictionary<string, object> dic)
  68. {
  69. this.args = dic;
  70. return this;
  71. }
  72. public Builder Args(string key, object value)
  73. {
  74. if (this.args == null)
  75. {
  76. this.args = new Dictionary<string, object>();
  77. }
  78. if(value is Dictionary<string,object>)
  79. {
  80. value = Json.Serialize(value);
  81. }
  82. this.args.Add(key, value);
  83. return this;
  84. }
  85. public Builder Callback(bool callback)
  86. {
  87. this.withCallback = callback;
  88. this.callbackId = this.withCallback ? TapUUID.UUID() : null;
  89. return this;
  90. }
  91. public Command CommandBuilder()
  92. {
  93. return new Command(this.service, this.method, this.withCallback, this.disposable, this.args);
  94. }
  95. }
  96. }
  97. }