Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using GameDevWare.Serialization;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using DataNode = GameDevWare.Serialization.IndexedDictionary<string, object>;
- using UnityEngine;
- using Colyseus;
- using System.Reflection;
- namespace Assets.Scripts.Core.Utilities
- {
- public class CommonUtil
- {
- public static string GetResponseHeader(Dictionary<string, string> responseHeaders)
- {
- StringBuilder sb = new StringBuilder();
- foreach (KeyValuePair<string, string> dict in responseHeaders)
- {
- sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n");
- }
- return sb.ToString();
- }
- public static string ParseDataNodeToJSON(object node, string tab = "")
- {
- var s = "";
- if (node is DataNode)
- {
- var dict = (DataNode)node;
- s += " {";
- foreach (var key in dict.Keys)
- {
- s += "\n" + tab + "\"" + key + "\" : " + ParseDataNode(dict[key], tab + " ");
- if (dict.Keys.IndexOf(key) < dict.Keys.Count - 1)
- {
- s += ",";
- }
- else
- {
- s += "\n";
- }
- }
- s += tab + "}";
- }
- else if (node is List<object>)
- {
- var list = (List<object>)node;
- s += "[ ";
- foreach (var item in list)
- {
- var i = list.IndexOf(item);
- s += "\n" + tab + i + ParseDataNode(item, tab + " ");
- if (i < list.Count - 1)
- {
- s += ",";
- }
- }
- s += " ]";
- }
- else if (node is string)
- {
- s += "\"" + node + "\"";
- }
- else if (node is SByte || node is Byte)
- {
- s += Convert.ToSingle(node);
- }
- else if (node is Double)
- {
- s += float.Parse(node.ToString());
- }
- else if (node is null)
- {
- s += "null";
- }
- else
- {
- Debug.Log("ERROR " + (node == null ? "null" : node.GetType().ToString()));
- }
- return s;
- }
- public static T changeObjByDataChange<T>(string v, DataChange change, T data)
- {
- PropertyInfo prop = data.GetType().GetProperty(change.path[v].ToString(), BindingFlags.Public | BindingFlags.Instance);
- if (null != prop && prop.CanWrite)
- {
- prop.SetValue(data, change.value);
- }
- return data;
- }
- public static T ParseData<T>(object node)
- {
- if (node is T)
- {
- return (T)node;
- }
- else if (typeof(T) == typeof(int))
- {
- return (T)(object)int.Parse(node.ToString());
- }
- else if (typeof(T) == typeof(float))
- {
- return (T)(object)float.Parse(node.ToString());
- }
- var json = ParseDataNode(node);
- Debug.Log("JSON : " + json);
- return JsonUtility.FromJson<T>(json);
- }
- public static object QueryState(object node, params object[] paths)
- {
- var currentNode = node;
- for (int i = 0; i < paths.Length; i++)
- {
- if (paths[i] is string)
- {
- // Get Node from key
- var dict = (DataNode)(currentNode);
- currentNode = dict[(string)paths[i]];
- }
- else if (paths[i] is int)
- {
- var arr = (List<object>)(currentNode);
- currentNode = arr[(int)paths[i]];
- }
- }
- return currentNode;
- }
- public static T GetObject<T>(IndexedDictionary<string, object> dict)
- {
- Type type = typeof(T);
- var obj = Activator.CreateInstance(type);
- foreach (var keyValue in dict)
- {
- if (type.GetProperty(keyValue.Key).CanWrite)
- {
- type.GetProperty(keyValue.Key).SetValue(obj, keyValue.Value, null);
- }
- }
- return (T)obj;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement