Advertisement
Guest User

Untitled

a guest
Mar 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.53 KB | None | 0 0
  1. using GameDevWare.Serialization;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using DataNode = GameDevWare.Serialization.IndexedDictionary<string, object>;
  6. using UnityEngine;
  7. using Colyseus;
  8. using System.Reflection;
  9.  
  10. namespace Assets.Scripts.Core.Utilities
  11. {
  12. public class CommonUtil
  13. {
  14. public static string GetResponseHeader(Dictionary<string, string> responseHeaders)
  15. {
  16. StringBuilder sb = new StringBuilder();
  17. foreach (KeyValuePair<string, string> dict in responseHeaders)
  18. {
  19. sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
  20. }
  21. return sb.ToString();
  22. }
  23.  
  24. public static string ParseDataNodeToJSON(object node, string tab = "")
  25. {
  26. var s = "";
  27. if (node is DataNode)
  28. {
  29. var dict = (DataNode)node;
  30. s += " {";
  31. foreach (var key in dict.Keys)
  32. {
  33. s += "\n" + tab + "\"" + key + "\" : " + ParseDataNode(dict[key], tab + " ");
  34. if (dict.Keys.IndexOf(key) < dict.Keys.Count - 1)
  35. {
  36. s += ",";
  37. }
  38. else
  39. {
  40. s += "\n";
  41. }
  42. }
  43. s += tab + "}";
  44. }
  45. else if (node is List<object>)
  46. {
  47. var list = (List<object>)node;
  48. s += "[ ";
  49. foreach (var item in list)
  50. {
  51. var i = list.IndexOf(item);
  52. s += "\n" + tab + i + ParseDataNode(item, tab + " ");
  53. if (i < list.Count - 1)
  54. {
  55. s += ",";
  56. }
  57. }
  58. s += " ]";
  59.  
  60. }
  61. else if (node is string)
  62. {
  63. s += "\"" + node + "\"";
  64. }
  65. else if (node is SByte || node is Byte)
  66. {
  67. s += Convert.ToSingle(node);
  68. }
  69. else if (node is Double)
  70. {
  71. s += float.Parse(node.ToString());
  72. }
  73. else if (node is null)
  74. {
  75. s += "null";
  76. }
  77. else
  78. {
  79.  
  80. Debug.Log("ERROR " + (node == null ? "null" : node.GetType().ToString()));
  81. }
  82. return s;
  83. }
  84.  
  85. public static T changeObjByDataChange<T>(string v, DataChange change, T data)
  86. {
  87. PropertyInfo prop = data.GetType().GetProperty(change.path[v].ToString(), BindingFlags.Public | BindingFlags.Instance);
  88. if (null != prop && prop.CanWrite)
  89. {
  90. prop.SetValue(data, change.value);
  91. }
  92.  
  93. return data;
  94. }
  95.  
  96. public static T ParseData<T>(object node)
  97. {
  98. if (node is T)
  99. {
  100. return (T)node;
  101. }
  102. else if (typeof(T) == typeof(int))
  103. {
  104. return (T)(object)int.Parse(node.ToString());
  105. }
  106. else if (typeof(T) == typeof(float))
  107. {
  108. return (T)(object)float.Parse(node.ToString());
  109. }
  110.  
  111. var json = ParseDataNode(node);
  112. Debug.Log("JSON : " + json);
  113. return JsonUtility.FromJson<T>(json);
  114. }
  115.  
  116. public static object QueryState(object node, params object[] paths)
  117. {
  118. var currentNode = node;
  119. for (int i = 0; i < paths.Length; i++)
  120. {
  121. if (paths[i] is string)
  122. {
  123. // Get Node from key
  124. var dict = (DataNode)(currentNode);
  125. currentNode = dict[(string)paths[i]];
  126. }
  127. else if (paths[i] is int)
  128. {
  129. var arr = (List<object>)(currentNode);
  130. currentNode = arr[(int)paths[i]];
  131. }
  132. }
  133.  
  134. return currentNode;
  135. }
  136.  
  137. public static T GetObject<T>(IndexedDictionary<string, object> dict)
  138. {
  139. Type type = typeof(T);
  140. var obj = Activator.CreateInstance(type);
  141.  
  142. foreach (var keyValue in dict)
  143. {
  144. if (type.GetProperty(keyValue.Key).CanWrite)
  145. {
  146. type.GetProperty(keyValue.Key).SetValue(obj, keyValue.Value, null);
  147. }
  148. }
  149. return (T)obj;
  150. }
  151.  
  152.  
  153.  
  154.  
  155. }
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement