ConcreteCast.cs
2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System;
using System.IO;
using System.Text;
using UnityEngine;
using System.Reflection;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniJSON
{
public static partial class ConcreteCast
{
public static string GetMethodName(Type src, Type dst)
{
return string.Format("Cast{0}To{1}", src.Name, dst.Name);
}
public static MethodInfo GetMethod(Type src, Type dst)
{
var name = GetMethodName(src, dst);
var mi = typeof(ConcreteCast).GetMethod(name,
BindingFlags.Static | BindingFlags.Public);
return mi;
}
#if UNITY_EDITOR
static Type[] s_castTypes = new Type[]
{
typeof(byte),
typeof(ushort),
typeof(uint),
typeof(ulong),
typeof(sbyte),
typeof(short),
typeof(int),
typeof(long),
typeof(float),
typeof(double),
};
[MenuItem("VRM/UniJSON/Generate ConcreteCast")]
public static void GenerateGenericCast()
{
var s = new StringBuilder();
using (var w = new StringWriter(s))
{
w.WriteLine(@"
using System;
namespace UniJSON {
public static partial class ConcreteCast
{
");
foreach (var x in s_castTypes)
{
foreach (var y in s_castTypes)
{
w.WriteLine(@"
public static $1 $2($0 src)
{
return ($1)src;
}
".Replace("$0", x.Name).Replace("$1", y.Name).Replace("$2", GetMethodName(x, y)));
}
}
w.WriteLine(@"
}
}
");
}
var path = Application.dataPath + SOURCE;
Debug.LogFormat("{0}", path);
File.WriteAllText(path, s.ToString().Replace("\r\n", "\n"), new UTF8Encoding(false));
AssetDatabase.ImportAsset("Assets" + SOURCE);
}
const string SOURCE = "/VRM/UniJSON/Scripts/ConcreteCast.g.cs";
#endif
}
}