CommandLineParser.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using Unity.RenderStreaming.Signaling;
  5. using Unity.WebRTC;
  6. using UnityEngine;
  7. namespace Unity.RenderStreaming
  8. {
  9. [Serializable]
  10. internal struct CommandLineInfo
  11. {
  12. public string signalingType;
  13. public string signalingUrl;
  14. public RTCIceServer[] iceServers;
  15. public string pollingInterval;
  16. }
  17. static class CommandLineParser
  18. {
  19. internal static readonly StringArgument SignalingUrl = new StringArgument("-signalingUrl");
  20. internal static readonly StringArgument SignalingType = new StringArgument("-signalingType");
  21. internal static readonly StringArrayArgument IceServerUrls = new StringArrayArgument("-iceServerUrl");
  22. internal static readonly StringArgument IceServerUsername = new StringArgument("-iceServerUsername");
  23. internal static readonly StringArgument IceServerCredential = new StringArgument("-iceServerCredential");
  24. internal static readonly EnumArgument<IceCredentialType> IceServerCredentialType = new EnumArgument<IceCredentialType>("-iceServerCredentialType");
  25. internal static readonly IntArgument PollingInterval = new IntArgument("-pollingInterval");
  26. internal static readonly JsonFileArgument<CommandLineInfo> ImportJson = new JsonFileArgument<CommandLineInfo>("-importJson");
  27. static readonly List<IArgument> options = new List<IArgument>() { SignalingUrl, SignalingType, IceServerUrls, IceServerUsername, IceServerCredential, IceServerCredentialType, PollingInterval, ImportJson };
  28. internal delegate bool TryParseDelegate<T>(string[] arguments, string argumentName, out T result);
  29. internal interface IArgument
  30. {
  31. bool TryParse(string[] arguments);
  32. }
  33. internal abstract class BaseArgument<T> : IArgument
  34. {
  35. /// <summary>
  36. /// A switch that will either retrieve the argument using the resolver, or use the readonly
  37. /// argument name string set on construction.
  38. /// </summary>
  39. public string ArgumentName { get; }
  40. /// <summary>
  41. ///
  42. /// </summary>
  43. public bool Defined => m_defined;
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. public readonly bool Required;
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. public T Value => m_value;
  52. protected bool m_defined;
  53. protected T m_value;
  54. readonly TryParseDelegate<T> m_parser;
  55. protected abstract bool DefaultParser(string[] arguments, string argumentName, out T parsedResult);
  56. public bool TryParse(string[] arguments)
  57. {
  58. m_defined =
  59. m_parser != null &&
  60. m_parser(arguments, ArgumentName, out m_value);
  61. return m_defined;
  62. }
  63. internal BaseArgument(string argumentName, bool required = false)
  64. {
  65. Required = required;
  66. ArgumentName = argumentName;
  67. m_parser = DefaultParser;
  68. }
  69. internal BaseArgument(string argumentName, TryParseDelegate<T> tryParseDelegate, bool required = false)
  70. {
  71. Required = required;
  72. ArgumentName = argumentName;
  73. m_parser = tryParseDelegate;
  74. }
  75. }
  76. internal class StringArgument : BaseArgument<string>
  77. {
  78. protected override bool DefaultParser(string[] arguments, string argumentName, out string parsedResult) => TryParseStringArgument(arguments, argumentName, out parsedResult, Required);
  79. internal StringArgument(string argumentName, bool required = false) : base(argumentName, required) { }
  80. internal StringArgument(string argumentName, TryParseDelegate<string> tryParse, bool required = false) : base(argumentName, tryParse, required) { }
  81. public static implicit operator string(StringArgument argument) => !argument.Defined ? null : argument.Value;
  82. }
  83. internal class EnumArgument<T> : BaseArgument<T?> where T : struct
  84. {
  85. protected override bool DefaultParser(string[] arguments, string argumentName, out T? parsedResult) => TryParseEnumArgument(arguments, argumentName, out parsedResult, Required);
  86. internal EnumArgument(string argumentName, bool required = false) : base(argumentName, required) { }
  87. public static implicit operator T?(EnumArgument<T> argument) => !argument.Defined ? null : argument.Value;
  88. }
  89. internal class StringArrayArgument : BaseArgument<string[]>
  90. {
  91. protected override bool DefaultParser(string[] arguments, string argumentName, out string[] parsedResult) => TryParseStringArrayArgument(arguments, argumentName, out parsedResult, Required);
  92. internal StringArrayArgument(string argumentName, bool required = false) : base(argumentName, required) { }
  93. public static implicit operator string[](StringArrayArgument argument) => !argument.Defined ? null : argument.Value;
  94. }
  95. internal class IntArgument : BaseArgument<int?>
  96. {
  97. protected override bool DefaultParser(string[] arguments, string argumentName, out int? parsedResult) => TryParseIntArgument(arguments, argumentName, out parsedResult, Required);
  98. internal IntArgument(string argumentName, bool required = false) : base(argumentName, required) { }
  99. public static implicit operator int?(IntArgument argument) => !argument.Defined ? null : argument.Value;
  100. }
  101. internal class JsonFileArgument<T> : BaseArgument<T?> where T : struct
  102. {
  103. protected override bool DefaultParser(string[] arguments, string argumentName, out T? parsedResult) => TryParseJsonFileArgument(arguments, argumentName, out parsedResult, Required);
  104. internal JsonFileArgument(string argumentName, bool required = false) : base(argumentName, required) { }
  105. public static implicit operator T?(JsonFileArgument<T> argument) => !argument.Defined ? null : argument.Value;
  106. }
  107. static bool TryParseStringArgument(string[] arguments, string argumentName, out string argumentValue, bool required = false)
  108. {
  109. var startIndex = System.Array.FindIndex(arguments, x => x == argumentName);
  110. if (startIndex < 0)
  111. {
  112. argumentValue = null;
  113. return !required;
  114. }
  115. if (startIndex + 1 >= arguments.Length)
  116. {
  117. argumentValue = null;
  118. return false;
  119. }
  120. argumentValue = arguments[startIndex + 1];
  121. return !string.IsNullOrEmpty(argumentValue);
  122. }
  123. static bool TryParseEnumArgument<T>(string[] arguments, string argumentName, out T? argumentValue,
  124. bool required = false) where T : struct
  125. {
  126. bool result = TryParseStringArgument(arguments, argumentName, out string value, required);
  127. if (result && !string.IsNullOrEmpty(value))
  128. {
  129. if (Enum.TryParse(value, true, out T enumValue))
  130. {
  131. argumentValue = enumValue;
  132. return true;
  133. }
  134. result = false;
  135. }
  136. argumentValue = null;
  137. return result;
  138. }
  139. static bool TryParseIntArgument(string[] arguments, string argumentName, out int? argumentValue, bool required = false)
  140. {
  141. var startIndex = System.Array.FindIndex(arguments, x => x == argumentName);
  142. if (startIndex < 0)
  143. {
  144. argumentValue = null;
  145. return !required;
  146. }
  147. if (startIndex + 1 >= arguments.Length)
  148. {
  149. argumentValue = null;
  150. return false;
  151. }
  152. if (!int.TryParse(arguments[startIndex + 1], out var result))
  153. {
  154. argumentValue = null;
  155. return false;
  156. }
  157. argumentValue = result;
  158. return true;
  159. }
  160. static bool TryParseStringArrayArgument(string[] arguments, string argumentName, out string[] argumentValue, bool required = false)
  161. {
  162. List<string> list = new List<string>();
  163. for (int i = 0; i < arguments.Length;)
  164. {
  165. var startIndex = Array.FindIndex(arguments, i, x => x == argumentName);
  166. if (startIndex < 0)
  167. break;
  168. if (startIndex + 1 >= arguments.Length)
  169. break;
  170. list.Add(arguments[startIndex + 1]);
  171. i = startIndex + 2;
  172. }
  173. if (list.Count == 0 && required)
  174. {
  175. argumentValue = null;
  176. return false;
  177. }
  178. argumentValue = list.ToArray();
  179. return true;
  180. }
  181. static bool TryParseJsonFileArgument<T>(string[] arguments, string argumentName, out T? argumentValue,
  182. bool required = false) where T : struct
  183. {
  184. bool result = TryParseFilePathArgument(arguments, argumentName, out string value);
  185. if (result && !string.IsNullOrEmpty(value))
  186. {
  187. string text = File.ReadAllText(value);
  188. try
  189. {
  190. argumentValue = JsonUtility.FromJson<T>(text);
  191. return true;
  192. }
  193. catch (Exception)
  194. {
  195. result = false;
  196. }
  197. }
  198. else if (required)
  199. result = false;
  200. argumentValue = null;
  201. return result;
  202. }
  203. static bool TryParseFilePathArgument(string[] arguments, string argumentName, out string argumentValue)
  204. {
  205. bool ret = TryParseStringArgument(arguments, argumentName, out string value);
  206. if (!ret)
  207. {
  208. argumentValue = null;
  209. return false;
  210. }
  211. if (string.IsNullOrEmpty(value))
  212. {
  213. argumentValue = null;
  214. return true;
  215. }
  216. if (!File.Exists(value))
  217. {
  218. argumentValue = null;
  219. return false;
  220. }
  221. argumentValue = value;
  222. return true;
  223. }
  224. internal static bool TryParse(string[] arguments)
  225. {
  226. foreach (var option in options)
  227. {
  228. if (!option.TryParse(arguments))
  229. return false;
  230. }
  231. return true;
  232. }
  233. }
  234. }