説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

CSharpCodeHelpers.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Text;
  5. ////REVIEW: this seems like it should be #if UNITY_EDITOR
  6. namespace UnityEngine.InputSystem.Utilities
  7. {
  8. internal static class CSharpCodeHelpers
  9. {
  10. public static bool IsProperIdentifier(string name)
  11. {
  12. if (string.IsNullOrEmpty(name))
  13. return false;
  14. if (char.IsDigit(name[0]))
  15. return false;
  16. for (var i = 0; i < name.Length; ++i)
  17. {
  18. var ch = name[i];
  19. if (!char.IsLetterOrDigit(ch) && ch != '_')
  20. return false;
  21. }
  22. return true;
  23. }
  24. public static bool IsEmptyOrProperIdentifier(string name)
  25. {
  26. if (string.IsNullOrEmpty(name))
  27. return true;
  28. return IsProperIdentifier(name);
  29. }
  30. public static bool IsEmptyOrProperNamespaceName(string name)
  31. {
  32. if (string.IsNullOrEmpty(name))
  33. return true;
  34. return name.Split('.').All(IsProperIdentifier);
  35. }
  36. ////TODO: this one should add the @escape automatically so no other code has to worry
  37. public static string MakeIdentifier(string name, string suffix = "")
  38. {
  39. if (string.IsNullOrEmpty(name))
  40. throw new ArgumentNullException(nameof(name));
  41. if (char.IsDigit(name[0]))
  42. name = "_" + name;
  43. // See if we have invalid characters in the name.
  44. var nameHasInvalidCharacters = false;
  45. for (var i = 0; i < name.Length; ++i)
  46. {
  47. var ch = name[i];
  48. if (!char.IsLetterOrDigit(ch) && ch != '_')
  49. {
  50. nameHasInvalidCharacters = true;
  51. break;
  52. }
  53. }
  54. // If so, create a new string where we remove them.
  55. if (nameHasInvalidCharacters)
  56. {
  57. var buffer = new StringBuilder();
  58. for (var i = 0; i < name.Length; ++i)
  59. {
  60. var ch = name[i];
  61. if (char.IsLetterOrDigit(ch) || ch == '_')
  62. buffer.Append(ch);
  63. }
  64. name = buffer.ToString();
  65. }
  66. return name + suffix;
  67. }
  68. public static string MakeTypeName(string name, string suffix = "")
  69. {
  70. var symbolName = MakeIdentifier(name, suffix);
  71. if (char.IsLower(symbolName[0]))
  72. symbolName = char.ToUpper(symbolName[0]) + symbolName.Substring(1);
  73. return symbolName;
  74. }
  75. #if UNITY_EDITOR
  76. public static string MakeAutoGeneratedCodeHeader(string toolName, string toolVersion, string sourceFileName = null)
  77. {
  78. return
  79. "//------------------------------------------------------------------------------\n"
  80. + "// <auto-generated>\n"
  81. + $"// This code was auto-generated by {toolName}\n"
  82. + $"// version {toolVersion}\n"
  83. + (string.IsNullOrEmpty(sourceFileName) ? "" : $"// from {sourceFileName}\n")
  84. + "//\n"
  85. + "// Changes to this file may cause incorrect behavior and will be lost if\n"
  86. + "// the code is regenerated.\n"
  87. + "// </auto-generated>\n"
  88. + "//------------------------------------------------------------------------------\n";
  89. }
  90. public static string ToLiteral(this object value)
  91. {
  92. if (value == null)
  93. return "null";
  94. var type = value.GetType();
  95. if (type == typeof(bool))
  96. {
  97. if ((bool)value)
  98. return "true";
  99. return "false";
  100. }
  101. if (type == typeof(char))
  102. return $"'\\u{(int)(char)value:X2}'";
  103. if (type == typeof(float))
  104. return value + "f";
  105. if (type == typeof(uint) || type == typeof(ulong))
  106. return value + "u";
  107. if (type == typeof(long))
  108. return value + "l";
  109. if (type.IsEnum)
  110. {
  111. var enumValue = type.GetEnumName(value);
  112. if (!string.IsNullOrEmpty(enumValue))
  113. return $"{type.FullName.Replace("+", ".")}.{enumValue}";
  114. }
  115. return value.ToString();
  116. }
  117. public static string GetInitializersForPublicPrimitiveTypeFields(this object instance)
  118. {
  119. var type = instance.GetType();
  120. var defaults = Activator.CreateInstance(type);
  121. var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public);
  122. var fieldInits = string.Join(", ",
  123. fields.Where(f => (f.FieldType.IsPrimitive || f.FieldType.IsEnum) && !f.GetValue(instance).Equals(f.GetValue(defaults)))
  124. .Select(f => $"{f.Name} = {f.GetValue(instance).ToLiteral()}"));
  125. if (string.IsNullOrEmpty(fieldInits))
  126. return "()";
  127. return " { " + fieldInits + " }";
  128. }
  129. #endif
  130. }
  131. }