-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathStructs.cs
447 lines (396 loc) · 11.4 KB
/
Structs.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
using ILRuntimeTest.TestFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestCases
{
public struct Fixed64Vector3
{
public Fixed64 x;
public Fixed64 y;
public Fixed64 z;
public static readonly Fixed64Vector3 Zero3;
public Fixed64Vector3(int x, int y, int z)
{
this.x = new Fixed64(x);
this.y = new Fixed64(y);
this.z = new Fixed64(z);
}
static Fixed64Vector3()
{
Zero3 = new Fixed64Vector3(0, 0, 0);
}
}
public struct Fixed64
{
private long m_rawValue;
public static readonly Fixed64 Zero;
public Fixed64(long value)
{
m_rawValue = value;
}
static Fixed64()
{
Zero = new Fixed64(0);
}
}
public class Color32
{
public Color32(byte r, byte g, byte b, byte a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
public byte r;
public byte g;
public byte b;
public byte a;
public void GetA(out byte _byte)
{
_byte = a;
}
public void GetB(ref byte _byte)
{
_byte = b;
}
public override string ToString()
{
return r + "," + g + "," + b + "," + a;
}
public void TestType(Type t)
{
Console.WriteLine("type=" + t);
}
}
public struct TT
{
public float abc;
}
public struct TestStruct
{
public int ID;
}
class TestStaticClass
{
public static TestStruct sss = new TestStruct();
}
public struct Vector3
{
public Vector3(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}
public static Vector3 operator +(Vector3 left, Vector3 right)
{
return new Vector3(left.x + right.x, left.y + right.y, left.z + right.z);
}
public static Vector3 operator *(Vector3 left, float right)
{
return new Vector3(left.x * right, left.y * right, left.z * right);
}
public static Vector3 operator -(Vector3 left, Vector3 right)
{
return new Vector3(left.x - right.x, left.y - right.y, left.z - right.z);
}
public static bool operator ==(Vector3 left, Vector3 right)
{
return (left.x == right.x && left.y == right.y && left.z == right.z);
}
public static bool operator !=(Vector3 left, Vector3 right)
{
return !(left.x == right.x && left.y == right.y && left.z == right.z);
}
public override string ToString()
{
return "(" + x + "," + y + "," + z + ")";
}
public float x;
public float y;
public float z;
public float len
{
get
{
return (float)Math.Sqrt(x * x + y * y + z * z);
}
}
public Vector3 Normalized()
{
float _len = len;
return new Vector3(x / _len, y / _len, z / _len);
}
public static Vector3 Cross(Vector3 left, Vector3 right)
{
return new Vector3(left.y * right.z - left.z * right.y, left.z * right.x - left.x * right.z, left.x * right.y - left.y * right.x);
}
public static Vector3 Cross(Vector3 left, float v = 0)
{
return left;
}
public static float Dot(Vector3 left, Vector3 right)
{
return left.x * right.x + left.y * right.y + left.z * right.z;
}
static Vector3 one = new Vector3(1, 1, 1);
public static Vector3 One
{
get
{
return one;
}
}
public static Vector3 Zero
{
get
{
return new Vector3(0, 0, 0);
}
}
public static string typetag;
//测试支持性
public static explicit operator int(Vector3 b) { return 0; } //这是一个显式转换
public static implicit operator float(Vector3 a) { return a.x; } //这是一个隐式转换
public Vector3 TestReturnThis(float x)
{
this.x += x;
return this;
}
}
public class StructTests
{
class MyClass
{
MyStruct stru = new MyStruct();
}
struct MyStruct
{
public int i;
public EnumTest.TestEnum e;
}
public static void StructTest1()
{
var m = new MyStruct[10];
m[1] = new MyStruct(); // `Throw exception here.`
}
public static void StructTest2()
{
MyClass c = new MyClass();
Console.WriteLine(c.ToString());
}
public static void StructTest3()
{
var s = new MyStruct();
s.i = 123;
s.e = EnumTest.TestEnum.Enum2;
var b = s;
Console.WriteLine(s.e + " " + s.i);
Console.WriteLine(b.e + " " + b.i);
}
public struct Number
{
public static readonly Number maxValue = new Number(Double.MaxValue);
public static readonly Number minValue = new Number(Double.MinValue);
public double val;
public Number(double val)
{
this.val = val;
}
}
public static void StructTest4()
{
Console.WriteLine(Number.minValue.val);
}
struct EnumTestStruct
{
public ILRuntimeTest.TestFramework.TestCLREnum value;
}
public static void StructTest5()
{
EnumTestStruct val = new EnumTestStruct();
Console.WriteLine(val.value);
}
private struct StructTest
{
public Object objAsset;
public string type;
}
public static void StructTest6()
{
Dictionary<string, StructTest> m_dictAsset = new Dictionary<string, StructTest>();
StructTest cube = new StructTest();
cube.type = "111";
m_dictAsset["123"] = cube;
cube.type = "123";
string strId = "123";
if (!m_dictAsset.TryGetValue(strId, out cube)) //注释:这句代码报错,提示错误InvalidCastException: Specified cast is not valid
{
throw new Exception();
}
if (cube.type != "111")
throw new Exception();
}
class TestClass
{
public ILRuntimeTest.TestFramework.TestVector3 v21 = new ILRuntimeTest.TestFramework.TestVector3(111, 222, 333);
public ILRuntimeTest.TestFramework.TestVector3 v22 = new ILRuntimeTest.TestFramework.TestVector3();
public void Test()
{
v21.X = 123; //没问题
v22.X = 222; //有问题, 报空 NullRef
}
}
public static void StructTest7()
{
var cl = new TestClass();
cl.Test();
}
public static void StructTest8()
{
Vector3 vec = new Vector3(1, 1, 1);
Vector3 vec2 = vec.TestReturnThis(2);
Console.WriteLine($"vec2.x ={vec2.x}");
if (vec2.x != 3)
throw new Exception();
}
class ClassA
{
public string str;
public ClassA(string s)
{
str = s;
}
public static implicit operator string(ClassA a) => a.str;
}
struct StructA
{
public ClassA A;
}
public static void StructTest9()
{
StructTest9Sub(new ClassA("aaa"));
}
static void StructTest9Sub(ClassA a)
{
StructTest9Sub2(a);
string str2 = a;
if (str2 != "aaa")
throw new Exception();
}
static StructA StructTest9Sub2(ClassA a)
{
StructA s = new StructA()
{
A = a,
};
return s;
}
public static void StructTest10()
{
ILRuntimeTest.TestFramework.TestVector3 vec = StructTest10Sub();
if (vec.X != 1)
throw new Exception();
}
static ILRuntimeTest.TestFramework.TestVector3 StructTest10Sub()
{
ILRuntimeTest.TestFramework.TestVector3 res = ILRuntimeTest.TestFramework.TestVector3.One2;
switch(res.X)
{
case 2:
Console.WriteLine("113");
break;
case 6:
Console.WriteLine("1136");
break;
case 100:
Console.WriteLine("11399");
break;
case 133:
Console.WriteLine("1131");
break;
}
object[] arr = new object[] { res };
Console.WriteLine("vec=" + arr[0]);
return res;
}
public static void StructTest11()
{
List<Anim> lst = new List<Anim>();
for(int i = 0; i < 50; i++)
{
lst.Add(new Anim(i.ToString(), i + 10f));
}
if (Math.Abs(lst[12].duration - 22) > 0.000001f)
throw new Exception();
}
struct Anim
{
public string name;
public float duration;
public Anim(string name, float duration)
{
this.name = name;
this.duration = duration;
}
}
public interface ITestStruct
{
int i { get; set; }
}
public struct MyStruct2 : ITestStruct
{
public int i { get; set; }
}
static void StructTest12Sub<T>() where T : struct, ITestStruct
{
T ins = new T() { i = 10 };
Console.WriteLine(ins.i);
if (ins.i != 10) //输出结果为0
throw new Exception();
}
public static void StructTest12()
{
StructTest12Sub<MyStruct2>();
}
public static void StructTest13()
{
StructTest13Sub<TestVector3>();
}
static void StructTest13Sub<T>() where T : struct
{
T ins = new T();
Console.WriteLine(ins);
}
struct NestedStruct
{
Vector3 v;
public NestedStruct(Vector3 v)
{
this.v = v;
}
}
public static void StructTest14()
{
List<NestedStruct> lst = new List<NestedStruct>();
lst.Add(new NestedStruct(new Vector3(1, 2, 3)));
}
class EventArgs
{
public object[] args;
}
public static void StructTest15()
{
EventArgs arg = new EventArgs();
arg.args = new object[] { new Vector3() };
if(arg.args[0] is Vector3)
{
Vector3 a = (Vector3)arg.args[0];
Console.WriteLine(a.ToString());
}
}
}
}