Skip to content

Commit 9e575d3

Browse files
committed
add: coding goodies
1 parent 0ca6932 commit 9e575d3

8 files changed

+237
-1
lines changed

Editor/StringBuilderExtension.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Runtime.CompilerServices;
5+
using System.Text;
6+
using UnityEngine;
7+
8+
9+
namespace SatorImaging.UnitySourceGenerator
10+
{
11+
public static class StringBuilderExtension
12+
{
13+
public static char CurrentIndentChar = ' ';
14+
public static int CurrentIndentSize = 4;
15+
public static int CurrentIndentLevel = 0;
16+
17+
18+
private static int s_lastIndentLevel = int.MinValue; // init with different value to current to build string
19+
20+
private static string s_indentString = string.Empty;
21+
public static string IndentString
22+
{
23+
get
24+
{
25+
if (s_lastIndentLevel == CurrentIndentLevel)
26+
return s_indentString;
27+
28+
s_lastIndentLevel = CurrentIndentLevel;
29+
s_indentString = new string(CurrentIndentChar, CurrentIndentLevel * CurrentIndentSize);
30+
return s_indentString;
31+
}
32+
}
33+
34+
35+
36+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IndentChar(this StringBuilder sb, char value) => CurrentIndentChar = value;
37+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IndentSize(this StringBuilder sb, int size) => CurrentIndentSize = Math.Max(0, size);
38+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IndentLevel(this StringBuilder sb, int level) => CurrentIndentLevel = Math.Max(0, level);
39+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IndentBegin(this StringBuilder sb) => CurrentIndentLevel = Math.Max(0, CurrentIndentLevel + 1);
40+
[MethodImpl(MethodImplOptions.AggressiveInlining)] public static void IndentEnd(this StringBuilder sb) => CurrentIndentLevel = Math.Max(0, CurrentIndentLevel - 1);
41+
42+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
43+
public static void IndentLine(this StringBuilder sb, string value)
44+
{
45+
sb.IndentAppend(value);
46+
sb.AppendLine();
47+
}
48+
49+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
50+
public static void IndentAppend(this StringBuilder sb, string value)
51+
{
52+
if (string.IsNullOrEmpty(value))
53+
return;
54+
55+
sb.Append(IndentString);
56+
sb.Append(value);
57+
}
58+
59+
60+
}
61+
}

Editor/StringBuilderExtension.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/USGFullNameOf.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using UnityEngine;
5+
6+
namespace SatorImaging.UnitySourceGenerator
7+
{
8+
public static class USGFullNameOf
9+
{
10+
///<summary>usage: usg&lt;MyClass&gt;(nameof(MyClass.MyProperty))</summary>
11+
///<remarks>each memberNames will be prepended with '.'</remarks>
12+
///<returns>full name of the class or method</returns>
13+
public static string usg<T>(params string[] memberNames)
14+
=> usg(typeof(T), memberNames);
15+
16+
public static string usg(Type cls, params string[] memberNames)
17+
{
18+
if (cls == null) throw new ArgumentNullException(nameof(cls));
19+
20+
21+
// TODO: support generic class.
22+
// --> Generic`1 -> Generic<TSomething>
23+
// NOTE: class or struct in class is separated with + sign. ex: Namespace.MyClass+ClassInClass
24+
var ret = cls.FullName.Replace('+', '.');
25+
26+
for (int i = 0; i < memberNames.Length; i++)
27+
{
28+
ret += "." + memberNames[i];
29+
}
30+
return ret;
31+
}
32+
33+
34+
}
35+
}

Editor/USGFullNameOf.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Editor/USGReflection.cs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Reflection;
5+
using UnityEngine;
6+
using UnityEngine.UI;
7+
using Debug = UnityEngine.Debug;
8+
9+
10+
namespace SatorImaging.UnitySourceGenerator
11+
{
12+
public static class USGReflection
13+
{
14+
public readonly static BindingFlags PUBLIC_INSTANCE_FIELD_OR_PROPERTY
15+
= BindingFlags.Public | BindingFlags.Instance
16+
| BindingFlags.DeclaredOnly
17+
| BindingFlags.GetProperty | BindingFlags.SetProperty
18+
| BindingFlags.GetField | BindingFlags.SetField
19+
;
20+
21+
22+
///<summary>Note that constructor (`.ctor`) is always ignored.</summary>
23+
public static MemberInfo[] GetAllPublicInstanceFieldAndProperty(Type cls, params string[] namesToIgnore)
24+
=> GetMembers(cls, PUBLIC_INSTANCE_FIELD_OR_PROPERTY, namesToIgnore ?? new string[] { });
25+
26+
static MemberInfo[] GetMembers(Type cls, BindingFlags flags, string[] namesToIgnore)
27+
{
28+
if (cls == null) throw new ArgumentNullException(nameof(cls));
29+
30+
31+
var members = cls.GetMembers(flags)
32+
.Where(x =>
33+
{
34+
if (namesToIgnore.Contains(x.Name) || x.Name == ".ctor")
35+
return false;
36+
return true;
37+
})
38+
.ToArray();
39+
;
40+
41+
return members;
42+
}
43+
44+
45+
///<summary>Try get value type of field or property.</summary>
46+
public static bool TryGetFieldOrPropertyType(MemberInfo info, out Type outType)
47+
{
48+
if (info == null) throw new ArgumentNullException(nameof(info));
49+
50+
51+
outType = null;
52+
if (info is FieldInfo field)
53+
{
54+
outType = field.FieldType;
55+
}
56+
else if (info is PropertyInfo property)
57+
{
58+
outType = property.PropertyType;
59+
}
60+
61+
62+
if (outType == null)
63+
return false;
64+
65+
return true;
66+
}
67+
68+
69+
///<summary>Enum names and values as a dictionary.</summary>
70+
///<typeparam name="TValue">int, uint, long or something</typeparam>
71+
public static Dictionary<string, TValue> GetEnumNamesAndValuesAsDictionary<TValue>(Type enumType)
72+
where TValue : struct
73+
{
74+
if (enumType?.IsEnum != true) throw new ArgumentException(nameof(enumType));
75+
76+
77+
var names = enumType.GetEnumNames();
78+
var dict = new Dictionary<string, TValue>(capacity: names.Length);
79+
80+
int iter = -1;
81+
foreach (TValue val in enumType.GetEnumValues())
82+
{
83+
iter++;
84+
dict.Add(names[iter], val);
85+
}
86+
87+
return dict;
88+
}
89+
90+
91+
#if UNITY_2021_3_OR_NEWER
92+
///<summary>Enum names and values as a tuple.</summary>
93+
///<typeparam name="TValue">int, uint, long or something</typeparam>
94+
public static (string[], TValue[]) GetEnumNamesAndValuesAsTuple<TValue>(Type enumType)
95+
where TValue : struct
96+
{
97+
if (enumType?.IsEnum != true) throw new ArgumentException(nameof(enumType));
98+
99+
return (enumType.GetEnumNames(), enumType.GetEnumValues().Cast<TValue>().ToArray());
100+
}
101+
#endif //UNITY_2021_3_OR_NEWER
102+
103+
104+
}
105+
}

Editor/USGReflection.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Template/Template_GenericGenerator.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma warning disable IDE0051
22

3+
using static SatorImaging.UnitySourceGenerator.USGFullNameOf;
34
using SatorImaging.UnitySourceGenerator;
45
using System.Text;
56
using Debug = UnityEngine.Debug;

Template/Template_MethodGenerator.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma warning disable IDE0051
22

3+
using static SatorImaging.UnitySourceGenerator.USGFullNameOf;
34
using SatorImaging.UnitySourceGenerator;
45
using System.Text;
56
using Debug = UnityEngine.Debug;
@@ -32,7 +33,7 @@ namespace {context.TargetClass.Namespace}
3233
{{
3334
");
3435
// Method Implementation
35-
var INDENT = " ";
36+
sb.IndentLevel(3);
3637

3738

3839

0 commit comments

Comments
 (0)