PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/unity/iOS/PubnubUnity/Assets/Editor/Common.cs

https://github.com/Sfyler/c-sharp
C# | 260 lines | 195 code | 25 blank | 40 comment | 8 complexity | 18e01b5a6fe0aa095d6ffb68be2c8c4c MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #define USE_JSONFX_UNITY_IOS
  2. //#define USE_MiniJSON
  3. using System;
  4. using PubNubMessaging.Core;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Timers;
  8. using System.Xml;
  9. using System.Text.RegularExpressions;
  10. using System.Globalization;
  11. using System.Linq;
  12. #if (USE_JSONFX) || (USE_JSONFX_UNITY)
  13. using JsonFx.Json;
  14. #elif (USE_JSONFX_UNITY_IOS)
  15. using Pathfinding.Serialization.JsonFx;
  16. #elif (USE_DOTNET_SERIALIZATION)
  17. using System.Runtime.Serialization.Json;
  18. using System.Web.Script.Serialization;
  19. #elif (USE_MiniJSON)
  20. using MiniJSON;
  21. #else
  22. using Newtonsoft.Json;
  23. using Newtonsoft.Json.Linq;
  24. using Newtonsoft.Json.Converters;
  25. #endif
  26. namespace PubNubMessaging.Tests
  27. {
  28. public class Common
  29. {
  30. public static string PublishKey = "demo-36";
  31. public static string SubscribeKey = "demo-36";
  32. public static string SecretKey = "demo-36";
  33. public object Response { get; set; }
  34. public bool DeliveryStatus { get; set; }
  35. /// <summary>
  36. /// Blocks the current thread unit the response is received
  37. /// or timeout occurs
  38. /// </summary>
  39. /// <param name="countdownSeconds">seconds to timeout</param>
  40. public void WaitForResponse (int countdownSeconds = 30)
  41. {
  42. Timer timer = new Timer ();
  43. DateTime start = DateTime.UtcNow;
  44. DateTime endTime = start.AddSeconds (countdownSeconds);
  45. timer.Enabled = true;
  46. timer.Start ();
  47. timer.Elapsed += delegate(object sender, ElapsedEventArgs e) {
  48. TimeSpan remainingTime = endTime - DateTime.UtcNow;
  49. if (remainingTime < TimeSpan.Zero) {
  50. timer.Enabled = false;
  51. DeliveryStatus = true;
  52. }
  53. };
  54. while (!DeliveryStatus)
  55. ;
  56. timer.Stop ();
  57. }
  58. public PubnubUnitTest CreateUnitTestInstance (string testClassName, string testCaseName)
  59. {
  60. PubnubUnitTest unitTest = new PubnubUnitTest ();
  61. unitTest.TestClassName = testClassName;
  62. unitTest.TestCaseName = testCaseName;
  63. return unitTest;
  64. }
  65. public void DisplayErrorMessage2 (PubnubClientError result)
  66. {
  67. Response = result;
  68. DeliveryStatus = true;
  69. UnityEngine.Debug.Log ("DisplayErrorMessage1:" + result.ToString ());
  70. }
  71. public void DisplayErrorMessage (PubnubClientError result)
  72. {
  73. //Response = result;
  74. //DeliveryStatus = true;
  75. UnityEngine.Debug.Log ("DisplayErrorMessage:" + result.ToString ());
  76. }
  77. public void DisplayReturnMessageDummy (object result)
  78. {
  79. //deliveryStatus = true;
  80. //Response = result;
  81. UnityEngine.Debug.Log ("DisplayReturnMessageDummy:" + result.ToString ());
  82. }
  83. public void DisplayReturnMessage (object result)
  84. {
  85. UnityEngine.Debug.Log ("DisplayReturnMessageO:" + result.ToString ());
  86. Response = result;
  87. DeliveryStatus = true;
  88. }
  89. public void DisplayReturnMessage (string result)
  90. {
  91. UnityEngine.Debug.Log ("DisplayReturnMessageS:" + result.ToString ());
  92. Response = (object)result.ToString ();
  93. DeliveryStatus = true;
  94. }
  95. public long Timestamp (Pubnub pubnub)
  96. {
  97. DeliveryStatus = false;
  98. pubnub.Time (DisplayReturnMessage, DisplayReturnMessage);
  99. while (!DeliveryStatus)
  100. ;
  101. IList<object> fields = Response as IList<object>;
  102. return Convert.ToInt64 (fields [0].ToString ());
  103. }
  104. /// <summary>
  105. /// Deserialize the specified message using either JSONFX or NEWTONSOFT.JSON.
  106. /// The functionality is based on the pre-compiler flag
  107. /// </summary>
  108. /// <param name="message">Message.</param>
  109. public static T Deserialize<T> (string message)
  110. {
  111. object retMessage;
  112. #if (USE_JSONFX) || (USE_JSONFX_UNITY)
  113. var reader = new JsonFx.Json.JsonReader ();
  114. retMessage = reader.Read<T> (message);
  115. #elif (USE_JSONFX_UNITY_IOS)
  116. UnityEngine.Debug.Log("message: " + message);
  117. retMessage = JsonReader.Deserialize<T>(message);
  118. #elif (USE_MiniJSON)
  119. UnityEngine.Debug.Log("message: " + message);
  120. object retMessage1 = Json.Deserialize(message) as object;
  121. Type type = typeof(T);
  122. var expectedType2 = typeof(object[]);
  123. if(expectedType2.IsAssignableFrom(type)){
  124. retMessage = ((System.Collections.IEnumerable)retMessage1).Cast<object> ().ToArray ();
  125. } else {
  126. retMessage = retMessage1;
  127. }
  128. #else
  129. retMessage = JsonConvert.DeserializeObject<T> (message);
  130. #endif
  131. return (T)retMessage;
  132. }
  133. /// <summary>
  134. /// Deserialize the specified message using either JSONFX or NEWTONSOFT.JSON.
  135. /// The functionality is based on the pre-compiler flag
  136. /// </summary>
  137. /// <param name="message">Message.</param>
  138. public static T DeserializeMiniJson<T> (string message)
  139. {
  140. object retMessage;
  141. UnityEngine.Debug.Log("message: " + message);
  142. retMessage = MiniJSON.Json.Deserialize(message) as object;
  143. return (T)retMessage;
  144. }
  145. /// <summary>
  146. /// Serialize the specified message using either JSONFX or NEWTONSOFT.JSON.
  147. /// The functionality is based on the pre-compiler flag
  148. /// </summary>
  149. /// <param name="message">Message.</param>
  150. public static string Serialize (object message)
  151. {
  152. string retMessage;
  153. #if (USE_JSONFX) || (USE_JSONFX_UNITY)
  154. var writer = new JsonFx.Json.JsonWriter ();
  155. retMessage = writer.Write (message);
  156. retMessage = ConvertHexToUnicodeChars (retMessage);
  157. #elif (USE_JSONFX_UNITY_IOS)
  158. retMessage = JsonWriter.Serialize(message);
  159. retMessage = ConvertHexToUnicodeChars (retMessage);
  160. #elif (USE_MiniJSON)
  161. retMessage = Json.Serialize(message);
  162. UnityEngine.Debug.Log("retMessage: " + retMessage);
  163. #else
  164. retMessage = JsonConvert.SerializeObject (message);
  165. #endif
  166. return retMessage;
  167. }
  168. /// <summary>
  169. /// Serialize the specified message using either JSONFX or NEWTONSOFT.JSON.
  170. /// The functionality is based on the pre-compiler flag
  171. /// </summary>
  172. /// <param name="message">Message.</param>
  173. public static string SerializeMiniJson (object message)
  174. {
  175. string retMessage;
  176. retMessage = MiniJSON.Json.Serialize(message);
  177. UnityEngine.Debug.Log("retMessage: " + retMessage);
  178. return retMessage;
  179. }
  180. /// <summary>
  181. /// Converts the upper case hex to lower case hex.
  182. /// </summary>
  183. /// <returns>The lower case hex.</returns>
  184. /// <param name="value">Hex Value.</param>
  185. private static string ConvertHexToUnicodeChars (string value)
  186. {
  187. //if(;
  188. return Regex.Replace (
  189. value,
  190. @"\\u(?<Value>[a-zA-Z0-9]{4})",
  191. m => {
  192. return ((char)int.Parse (m.Groups ["Value"].Value, NumberStyles.HexNumber)).ToString ();
  193. }
  194. );
  195. }
  196. }
  197. /// <summary>
  198. /// Custom class for testing the encryption and decryption
  199. /// </summary>
  200. class CustomClass
  201. {
  202. public string foo = "hi!";
  203. public int[] bar = { 1, 2, 3, 4, 5 };
  204. }
  205. [Serializable]
  206. class PubnubDemoObject
  207. {
  208. public double VersionID = 3.4;
  209. public long Timetoken = 13601488652764619;
  210. public string OperationName = "Publish";
  211. public string[] Channels = { "ch1" };
  212. public PubnubDemoMessage DemoMessage = new PubnubDemoMessage ();
  213. public PubnubDemoMessage CustomMessage = new PubnubDemoMessage ("This is a demo message");
  214. public XmlDocument SampleXml = new PubnubDemoMessage ().TryXmlDemo ();
  215. }
  216. [Serializable]
  217. class PubnubDemoMessage
  218. {
  219. public string DefaultMessage = "~!@#$%^&*()_+ `1234567890-= qwertyuiop[]\\ {}| asdfghjkl;' :\" zxcvbnm,./ <>? ";
  220. //public string DefaultMessage = "\"";
  221. public PubnubDemoMessage ()
  222. {
  223. }
  224. public PubnubDemoMessage (string message)
  225. {
  226. DefaultMessage = message;
  227. }
  228. public XmlDocument TryXmlDemo ()
  229. {
  230. XmlDocument xmlDocument = new XmlDocument ();
  231. xmlDocument.LoadXml ("<DemoRoot><Person ID='ABCD123'><Name><First>John</First><Middle>P.</Middle><Last>Doe</Last></Name><Address><Street>123 Duck Street</Street><City>New City</City><State>New York</State><Country>United States</Country></Address></Person><Person ID='ABCD456'><Name><First>Peter</First><Middle>Z.</Middle><Last>Smith</Last></Name><Address><Street>12 Hollow Street</Street><City>Philadelphia</City><State>Pennsylvania</State><Country>United States</Country></Address></Person></DemoRoot>");
  232. return xmlDocument;
  233. }
  234. }
  235. }