12using System.Reflection;
13using System.Runtime.InteropServices;
14using System.Collections.Generic;
20 [CreateAssetMenu(menuName =
"Generator/BitConverter", order = 900)]
42 private void generateCode() {
44 lines = lines.Select(l => l.Replace(
"Leap.Unity.Generation",
"Leap.Unity").
45 Replace(
"BitConverterNonAlloc_Template_",
"BitConverterNonAlloc")).
48 using (var writer = File.CreateText(Path.Combine(
targetFolder.
Path,
"BitConverterNonAlloc.cs"))) {
49 for (
int i = 0; i < lines.Count; i++) {
50 string line = lines[i];
53 List<string> methodTemplate =
new List<string>();
56 string methodLine = lines[i];
57 if (methodLine.Contains(
END_KEY)) {
60 methodTemplate.Add(methodLine);
63 Func<int, string> byteExpr;
64 if (line.Contains(
TO_KEY)) {
65 byteExpr = b =>
"_c.Byte" + b +
" = bytes[offset++];";
66 }
else if (line.Contains(
GET_KEY)) {
67 byteExpr = b =>
"bytes[offset++] = _c.Byte" + b +
";";
69 throw new InvalidOperationException(
"Invalid template type [" + line +
"]");
72 expandMethodTemplate(methodTemplate, writer, byteExpr);
74 writer.WriteLine(line);
80 private void expandMethodTemplate(List<string> methodTemplate, StreamWriter writer, Func<int, string> byteExpr) {
82 Type type = Assembly.GetAssembly(typeof(
int)).GetTypes().First(t => t.Name == primitiveType);
84 int bytes = Marshal.SizeOf(type);
86 for (
int i = 0; i < methodTemplate.Count; i++) {
87 string line = methodTemplate[i];
88 line = line.Replace(
"Single", primitiveType);
91 string indent =
new string(line.TakeWhile(c =>
char.IsWhiteSpace(c)).ToArray());
92 for (
int j = 0; j < bytes; j++) {
94 writer.WriteLine(byteExpr(j));
97 writer.WriteLine(line);
103 private void generateUnitTests() {
106 lines = lines.Select(l => l.Replace(
"Leap.Unity.Generation",
"Leap.Unity.Tests").
107 Replace(
"_Template_",
"").
108 Replace(
"_BitConverterTestMock_",
"BitConverterNonAlloc")).
114 TakeWhile(l => !l.Contains(
END_KEY)).
115 Select(s => s +
"\n").
116 Fold((a, b) => a + b);
118 string beforeCode = lines.Query().
120 Select(s => s +
"\n").
121 Fold((a, b) => a + b);
123 string afterCode = lines.Query().
124 SkipWhile(l => !l.Contains(
END_KEY)).
126 Select(s => s +
"\n").
127 Fold((a, b) => a + b);
129 using (var writer =
File.CreateText(Path.Combine(
testFolder.
Path,
"BitConverterNonAllocTests.cs"))) {
130 writer.Write(beforeCode);
135 writer.Write(
codeTemplate.Replace(
"Single", primitiveType).
136 Replace(
"//[Test]",
"[Test]"));
139 writer.Write(afterCode);
143 private List<string> getLines(TextAsset asset) {
144 List<string> lines =
new List<string>();
145 using (var reader =
new StringReader(asset.text)) {
147 string line = reader.ReadLine();
158 public static class _BitConverterTestMock_ {
159 public static System.Single ToSingle(
byte[] bytes,
int offset) {
return 0; }
160 public static void GetBytes(System.Single value,
byte[] bytes, ref
int offset) {
return; }
A convenient serializable representation of an asset folder. Only useful for editor scripts since ass...
virtual string Path
Gets or sets the folder path. This path will always be a path relative to the asset folder,...
const string FILL_BYTES_KEY
A Query object is a type of immutable ordered collection of elements that can be used to perform usef...