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