Skip to content

Commit f5320c1

Browse files
committed
v3.1.0 Release.
1 parent 1900ca6 commit f5320c1

6 files changed

Lines changed: 51 additions & 49 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ const int l = 100;
200200
const int r = 200;
201201
MyConstExpression.Add(l, r);
202202

203-
// NG *May be acceptable if traceable at IL level.
203+
// NG
204204
int l = 100;
205205
int r = 200;
206206
MyConstExpression.Add(l, r);
207207

208-
// NG *May be acceptable if traceable at IL level.
208+
// NG
209209
readonly int l = 100;
210210
readonly int r = 200;
211211
MyConstExpression.Add(l, r);
@@ -358,12 +358,12 @@ const int l = 100;
358358
const int r = 200;
359359
MyStaticExpression.Add(l, r);
360360

361-
// NG *May be acceptable if traceable at IL level.
361+
// NG
362362
int l = 100;
363363
int r = 200;
364364
MyStaticExpression.Add(l, r);
365365

366-
// NG *May be acceptable if traceable at IL level.
366+
// NG
367367
readonly int l = 100;
368368
readonly int r = 200;
369369
MyStaticExpression.Add(l, r);

README_ja.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,12 @@ const int l = 100;
200200
const int r = 200;
201201
MyConstExpression.Add(l, r);
202202

203-
// NG ※ILレベルでトレース可能であればOKになる場合があります
203+
// NG
204204
int l = 100;
205205
int r = 200;
206206
MyConstExpression.Add(l, r);
207207

208-
// NG ※ILレベルでトレース可能であればOKになる場合があります
208+
// NG
209209
readonly int l = 100;
210210
readonly int r = 200;
211211
MyConstExpression.Add(l, r);
@@ -356,12 +356,12 @@ const int l = 100;
356356
const int r = 200;
357357
MyStaticExpression.Add(l, r);
358358

359-
// NG ※ILレベルでトレース可能であればOKになる場合があります
359+
// NG
360360
int l = 100;
361361
int r = 200;
362362
MyStaticExpression.Add(l, r);
363363

364-
// NG ※ILレベルでトレース可能であればOKになる場合があります
364+
// NG
365365
readonly int l = 100;
366366
readonly int r = 200;
367367
MyStaticExpression.Add(l, r);

packages/Editor/CodeGen/ConstExpressionILPostProcessor.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
3636

3737
try
3838
{
39-
ILPostProcessorUtils.InitLog<ConstExpressionILPostProcessor>(compiledAssembly);
39+
ILPPUtils.InitLog<ConstExpressionILPostProcessor>(compiledAssembly);
4040
_constExprTable = FindConstExprMethods();
41-
using (var assembly = ILPostProcessorUtils.LoadAssemblyDefinition(compiledAssembly))
41+
using (var assembly = ILPPUtils.LoadAssemblyDefinition(compiledAssembly))
4242
{
4343
var mainModule = assembly.MainModule;
4444
_allowedStaticExpressionTypes.Add(mainModule.ImportReference(typeof(Type)));
@@ -87,7 +87,7 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
8787
continue;
8888
}
8989

90-
ILPostProcessorUtils.ResolveInstructionOpCode(body.Instructions);
90+
ILPPUtils.ResolveInstructionOpCode(body.Instructions);
9191
}
9292
}
9393
}
@@ -102,40 +102,40 @@ public override ILPostProcessResult Process(ICompiledAssembly compiledAssembly)
102102
};
103103

104104
assembly.Write(pe, writeParameter);
105-
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), ILPostProcessorUtils.Logger.Messages);
105+
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), ILPPUtils.Logger.Messages);
106106
}
107107
}
108108
catch (Exception e)
109109
{
110-
ILPostProcessorUtils.LogException(e);
110+
ILPPUtils.LogException(e);
111111
}
112-
return new ILPostProcessResult(null, ILPostProcessorUtils.Logger.Messages);
112+
return new ILPostProcessResult(null, ILPPUtils.Logger.Messages);
113113
}
114114

115115
private Dictionary<string, Delegate> FindConstExprMethods()
116116
{
117117
var result = new Dictionary<string, Delegate>();
118-
foreach (var method in ILPostProcessorUtils.FindMethods<ConstExpressionAttribute>(typeof(ConstExpressionEntry).Assembly))
118+
foreach (var method in ILPPUtils.FindMethods<ConstExpressionAttribute>(typeof(ConstExpressionEntry).Assembly))
119119
{
120120
if (!CheckConstExpressionError(method))
121121
{
122122
continue;
123123
}
124124

125-
var delType = ILPostProcessorUtils.GetDelegateType(method.GetParameters().Select(v => v.ParameterType), method.ReturnType);
125+
var delType = ILPPUtils.GetDelegateType(method.GetParameters().Select(v => v.ParameterType), method.ReturnType);
126126
var del = Delegate.CreateDelegate(delType, method);
127127
var args = string.Empty;
128128
foreach (var parameter in method.GetParameters())
129129
{
130-
args = $"{args}{ILPostProcessorUtils.GetTypeName(parameter.ParameterType)},";
130+
args = $"{args}{ILPPUtils.GetTypeName(parameter.ParameterType)},";
131131
}
132132

133133
if (!string.IsNullOrEmpty(args))
134134
{
135135
args = args.Remove(args.Length - 1, 1);
136136
}
137-
string returnName = ILPostProcessorUtils.GetTypeName(method.ReturnType);
138-
result.Add($"{returnName} {ILPostProcessorUtils.GetTypeName(method.ReflectedType)}::{method.Name}({args})", del);
137+
string returnName = ILPPUtils.GetTypeName(method.ReturnType);
138+
result.Add($"{returnName} {ILPPUtils.GetTypeName(method.ReflectedType)}::{method.Name}({args})", del);
139139
}
140140

141141
return result;
@@ -161,7 +161,7 @@ private bool ConstExpressionProcess(ILProcessor ilProcessor, MethodDefinition me
161161
{
162162
if (constExpr.Method.GetCustomAttribute<ConstExpressionAttribute>().CalculationFailedWarning)
163163
{
164-
ILPostProcessorUtils.LogWarning($"ConstExpression calculation failed.", method, argInstruction);
164+
ILPPUtils.LogWarning("CONSTEXPR0501", "ConstExpression warning.", $"ConstExpression accepts only constants as arguments.", method, instruction);
165165
}
166166
return false;
167167
}
@@ -183,7 +183,7 @@ private bool ConstExpressionProcess(ILProcessor ilProcessor, MethodDefinition me
183183
e = t.InnerException;
184184
}
185185

186-
ILPostProcessorUtils.LogException(e);
186+
ILPPUtils.LogException(e);
187187
return false;
188188
}
189189

@@ -200,21 +200,21 @@ loadLiteral.Operand is FieldReference field &&
200200
while (argInstruction != instruction)
201201
{
202202
--instructionDiff;
203-
ILPostProcessorUtils.ReplaceTarget(ilProcessor, argInstruction, loadLiteral);
203+
ILPPUtils.ReplaceTarget(ilProcessor, argInstruction, loadLiteral);
204204
argInstruction = argInstruction.Next;
205205
ilProcessor.Remove(argInstruction.Previous);
206206
}
207207

208208
--instructionDiff;
209-
ILPostProcessorUtils.ReplaceTarget(ilProcessor, instruction, loadLiteral);
209+
ILPPUtils.ReplaceTarget(ilProcessor, instruction, loadLiteral);
210210
ilProcessor.Remove(instruction);
211211

212212
return true;
213213
}
214214

215215
private bool TryGetConstValue(ref Instruction instruction, Type type, out object result)
216216
{
217-
if (ILPostProcessorUtils.TryGetConstValue(ref instruction, type, out result))
217+
if (ILPPUtils.TryGetConstValue(ref instruction, type, out result))
218218
{
219219
return true;
220220
}
@@ -227,6 +227,7 @@ private bool TryGetConstValue(ref Instruction instruction, Type type, out object
227227
return ret;
228228
}
229229

230+
/*
230231
if (instruction.OpCode == OpCodes.Ldloc_0)
231232
{
232233
var ret = TryGetConstValue(instruction, type, OpCodes.Stloc_0, instruction.Operand, out result);
@@ -262,6 +263,7 @@ private bool TryGetConstValue(ref Instruction instruction, Type type, out object
262263
var ret = TryGetConstValue(instruction, type, OpCodes.Stloc, instruction.Operand, out result);
263264
return ret;
264265
}
266+
*/
265267

266268
result = null;
267269
return false;
@@ -288,20 +290,20 @@ private bool CheckConstExpressionError(MethodInfo method)
288290
{
289291
if (!method.IsStatic)
290292
{
291-
ILPostProcessorUtils.LogError($"ConstExpression is static method only.", method);
293+
ILPPUtils.LogError("CONSTEXPR0001", "ConstExpression failed.", $"ConstExpression is static method only.", method);
292294
return false;
293295
}
294296

295297
if (method.IsGenericMethod ||
296298
method.ReflectedType.IsGenericType)
297299
{
298-
ILPostProcessorUtils.LogError($"ConstExpression does not support generic argument.", method);
300+
ILPPUtils.LogError("CONSTEXPR0002", "ConstExpression failed.", $"ConstExpression does not support generic argument.", method);
299301
return false;
300302
}
301303

302304
if (!IsAllowConstExpressionType(method.ReturnType))
303305
{
304-
ILPostProcessorUtils.LogError($"ConstExpression does not support return of type \"{method.ReturnType.FullName}\".", method);
306+
ILPPUtils.LogError("CONSTEXPR0003", "ConstExpression failed.", $"ConstExpression does not support return of type \"{method.ReturnType.FullName}\".", method);
305307
return false;
306308
}
307309

@@ -312,7 +314,7 @@ private bool CheckConstExpressionError(MethodInfo method)
312314
continue;
313315
}
314316

315-
ILPostProcessorUtils.LogError($"ConstExpression does not support parameters of type \"{parameter.ParameterType.FullName}\".", method);
317+
ILPPUtils.LogError("CONSTEXPR0004", "ConstExpression failed.", $"ConstExpression does not support parameters of type \"{parameter.ParameterType.FullName}\".", method);
316318
return false;
317319
}
318320

@@ -332,7 +334,7 @@ private static bool IsAllowConstExpressionType(Type type)
332334

333335
var result = type == typeof(string) ||
334336
type.IsEnum ||
335-
ILPostProcessorUtils.IsStructRecursive(type);
337+
ILPPUtils.IsStructRecursive(type);
336338
return result;
337339
}
338340

@@ -359,7 +361,7 @@ private bool StaticExpressionProcess(ILProcessor ilProcessor, MethodDefinition m
359361
{
360362
if (calculationFailedWarning)
361363
{
362-
ILPostProcessorUtils.LogWarning($"StaticExpression calculation failed.", method, instruction);
364+
ILPPUtils.LogWarning("CONSTEXPR1502", "ConstExpression warning.", $"StaticExpression cannot use GenericParameter as a type arguments.", method, instruction);
363365
}
364366
return false;
365367
}
@@ -372,11 +374,11 @@ private bool StaticExpressionProcess(ILProcessor ilProcessor, MethodDefinition m
372374
for (int i = 0; i < parameters.Count; ++i)
373375
{
374376
argInstruction = argInstruction.Previous;
375-
if (!ILPostProcessorUtils.TryGetConstInstructions(ref argInstruction, argInstructions))
377+
if (!ILPPUtils.TryGetConstInstructions(ref argInstruction, argInstructions))
376378
{
377379
if (calculationFailedWarning)
378380
{
379-
ILPostProcessorUtils.LogWarning($"StaticExpression calculation failed.", method, argInstruction);
381+
ILPPUtils.LogWarning("CONSTEXPR1501", "ConstExpression warning.", $"StaticExpression accepts only constants as arguments.", method, instruction);
380382
}
381383
return false;
382384
}
@@ -392,13 +394,13 @@ private bool StaticExpressionProcess(ILProcessor ilProcessor, MethodDefinition m
392394
while (argInstruction != instruction)
393395
{
394396
--instructionDiff;
395-
ILPostProcessorUtils.ReplaceTarget(ilProcessor, argInstruction, loadLiteral);
397+
ILPPUtils.ReplaceTarget(ilProcessor, argInstruction, loadLiteral);
396398
argInstruction = argInstruction.Next;
397399
ilProcessor.Remove(argInstruction.Previous);
398400
}
399401

400402
--instructionDiff;
401-
ILPostProcessorUtils.ReplaceTarget(ilProcessor, instruction, loadLiteral);
403+
ILPPUtils.ReplaceTarget(ilProcessor, instruction, loadLiteral);
402404
ilProcessor.Remove(instruction);
403405
return true;
404406
}
@@ -454,19 +456,19 @@ private bool CheckStaticExpressionError(MethodReference methodRef)
454456
var method = methodRef.Resolve();
455457
if (!method.IsStatic)
456458
{
457-
ILPostProcessorUtils.LogError($"StaticExpression is static method only.", method);
459+
ILPPUtils.LogError("CONSTEXPR1001", "ConstExpression failed.", $"StaticExpression is static method only.", method);
458460
return false;
459461
}
460462

461463
if (!method.IsPublic)
462464
{
463-
ILPostProcessorUtils.LogError($"StaticExpression is public method only.", method);
465+
ILPPUtils.LogError("CONSTEXPR1002", "ConstExpression failed.", $"StaticExpression is public method only.", method);
464466
return false;
465467
}
466468

467469
if (!IsAllowStaticExpressionType(methodRef.ReturnType))
468470
{
469-
ILPostProcessorUtils.LogError($"StaticExpression does not support return of type \"{methodRef.ReturnType.FullName}\".", method);
471+
ILPPUtils.LogError("CONSTEXPR1003", "ConstExpression failed.", $"StaticExpression does not support return of type \"{methodRef.ReturnType.FullName}\".", method);
470472
return false;
471473
}
472474

@@ -477,7 +479,7 @@ private bool CheckStaticExpressionError(MethodReference methodRef)
477479
continue;
478480
}
479481

480-
ILPostProcessorUtils.LogError($"StaticExpression does not support parameters of type \"{parameter.ParameterType.FullName}\".", method);
482+
ILPPUtils.LogError("CONSTEXPR1004", "ConstExpression failed.", $"StaticExpression does not support parameters of type \"{parameter.ParameterType.FullName}\".", method);
481483
return false;
482484
}
483485

@@ -499,7 +501,7 @@ private static bool IsAllowStaticExpressionType(TypeReference type)
499501
var result = type.IsString() ||
500502
type.IsEnum() ||
501503
_allowedStaticExpressionTypes.Contains(type) ||
502-
ILPostProcessorUtils.IsStructRecursive(type);
504+
ILPPUtils.IsStructRecursive(type);
503505
return result;
504506
}
505507
}

packages/Tests/Runtime/ConstExpressionEntry/ConstExpressionTestFunctions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ public static ReadOnlyArray<DayOfWeek> MakeArray(DayOfWeek e1, DayOfWeek e2, Day
175175
}
176176

177177
[ConstExpression]
178-
public static PackedStruct<int> MakeGeneric(int value)
178+
public static PackedStruct<Vector3> MakeGeneric(float x, float y, float z)
179179
{
180-
return new PackedStruct<int>()
180+
return new PackedStruct<Vector3>()
181181
{
182-
value = value,
182+
value = new Vector3(x, y, z),
183+
hoge = 100.0f
183184
};
184185
}
185186

packages/Tests/Runtime/ConstExpressionTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ public void EnumArray()
278278
[Test]
279279
public void GenericStructure()
280280
{
281-
var result = ConstExpressionTestFunctions.MakeGeneric(100);
282-
Assert.AreEqual(result.value, 100);
281+
var result = ConstExpressionTestFunctions.MakeGeneric(10.0f, 20.0f, 30.0f);
282+
Assert.AreEqual(result.value, new Vector3(10.0f, 20.0f, 30.0f));
283283
}
284284

285285
[Test]
@@ -345,8 +345,7 @@ public void ExpressionChain()
345345
{
346346
var z = ConstExpressionTestFunctions.GetZ(ConstExpressionTestFunctions.MakeVector3(1f, 2.5f, 3.45f));
347347
Assert.AreEqual(z, 3.45f);
348-
var arr = ConstExpressionTestFunctions.MakeArray(10, 20, 30);
349-
var a = ConstExpressionTestFunctions.GetArrayElement(arr, 1);
348+
var a = ConstExpressionTestFunctions.GetArrayElement(ConstExpressionTestFunctions.MakeArray(10, 20, 30), 1);
350349
Assert.AreEqual(a, 20);
351350
}
352351
}

packages/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
"name": "com.katuusagi.constexpressionforunity",
33
"displayName": "ConstExpressionForUnity",
44
"description": "Library for compile-time function execution in Unity.",
5-
"version": "3.0.0",
5+
"version": "3.1.0",
66
"unity": "2021.3",
77
"dependencies": {
8-
"com.katuusagi.ilpostprocessorcommon": "2.0.0",
8+
"com.katuusagi.ilpostprocessorcommon": "2.1.0",
99
"com.unity.test-framework": "1.1.31",
1010
"com.unity.test-framework.performance": "3.0.2",
1111
"com.unity.nuget.mono-cecil": "1.10.2"
@@ -21,5 +21,5 @@
2121
},
2222
"documentationUrl": "https://github.com/Katsuya100/ConstExpressionForUnity",
2323
"changelogUrl": "https://github.com/Katsuya100/ConstExpressionForUnity/releases",
24-
"licensesUrl": "https://github.com/Katsuya100/ConstExpressionForUnity/blob/v3.0.0/LICENSE"
24+
"licensesUrl": "https://github.com/Katsuya100/ConstExpressionForUnity/blob/v3.1.0/LICENSE"
2525
}

0 commit comments

Comments
 (0)