Skip to content

Commit bd3a476

Browse files
committed
[+] 添加HashCode 代码的生成
1 parent c96ec26 commit bd3a476

17 files changed

Lines changed: 363 additions & 161 deletions

File tree

Unity/Assets/LockstepEngine/Collision2D/CTransform2D.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,6 @@
33
using Lockstep.Math;
44
using Lockstep.Util;
55

6-
namespace Lockstep.Collision2D {
7-
public static class TransformHashCodeExtension {
8-
public static int GetHash(this CTransform2D val){
9-
return val.Pos3.GetHash() * 13
10-
+ val.deg.GetHash() * 31;
11-
}
12-
}
13-
}
14-
156
namespace Lockstep.Collision2D {
167
[Serializable]
178
public partial class CTransform2D : IComponent {

Unity/Assets/LockstepEngine/ECS.Common/IdService.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using Lockstep.Math;
33

44
namespace Lockstep.Game {
5-
public class IdService : IIdService, IHashCode, ITimeMachine {
5+
public partial class IdService : IIdService, ITimeMachine {
66
public int CurTick { get; set; }
77

88
private int Id;
@@ -23,11 +23,5 @@ public void Backup(int tick){
2323

2424
public void Clean(int maxVerifiedTick){ }
2525

26-
public int GetHash(ref int idx){
27-
return Id * PrimerLUT.GetPrimer(idx++);
28-
}
29-
public void DumpStr(System.Text.StringBuilder sb,string prefix){
30-
sb.AppendLine(prefix + "Id"+":" + Id.ToString());
31-
}
3226
}
3327
}

Unity/Assets/LockstepEngine/ECS.Common/Interfaces/IService.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33
public interface IService {
44
}
55

6-
public interface IHashCode {
7-
int GetHash(ref int idx);
8-
}
6+
97
}

Unity/Assets/LockstepEngine/ECS.Common/Services/RandomService.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,10 @@
44
using Random = Lockstep.Math.Random;
55

66
namespace Lockstep.Game {
7-
public class RandomService : BaseService, IRandomService {
7+
public partial class RandomService : BaseService, IRandomService {
88
Random _i = new Math.Random();
99
public LFloat value => _i.value;
1010

11-
public override int GetHash(ref int idx){
12-
return (int) _i.randSeed * PrimerLUT.GetPrimer(idx++);
13-
}
14-
public override void DumpStr(System.Text.StringBuilder sb,string prefix){
15-
sb.AppendLine(prefix + "randSeed"+":" + _i.randSeed.ToString());
16-
}
17-
1811
public uint Next(){
1912
return _i.Next();
2013
}

Unity/Assets/LockstepEngine/Math/HashCodeExtension.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,51 +3,53 @@
33

44
namespace Lockstep.Util {
55
public static class HashCodeExtension {
6-
public static int GetHash(this byte val){
6+
7+
public static int GetHash(this byte val,ref int idx){
78
return val;
89
}
910

10-
public static int GetHash(this short val){
11+
public static int GetHash(this short val,ref int idx){
1112
return val;
1213
}
1314

14-
public static int GetHash(this int val){
15+
public static int GetHash(this int val,ref int idx){
1516
return val;
1617
}
17-
18-
public static int GetHash(this long val){
18+
public static int GetHash(this long val,ref int idx){
1919
return (int) val;
2020
}
2121

22-
public static int GetHash(this sbyte val){
22+
public static int GetHash(this sbyte val,ref int idx){
2323
return val;
2424
}
2525

26-
public static int GetHash(this ushort val){
26+
public static int GetHash(this ushort val,ref int idx){
2727
return val;
2828
}
2929

30-
public static int GetHash(this uint val){
30+
public static int GetHash(this uint val,ref int idx){
3131
return (int) val;
3232
}
3333

34-
public static int GetHash(this ulong val){
34+
public static int GetHash(this ulong val,ref int idx){
3535
return (int) val;
3636
}
3737

38-
public static int GetHash(this bool val){
38+
public static int GetHash(this bool val,ref int idx){
3939
return val ? 1 : 0;
4040
}
41-
42-
public static int GetHash(this LFloat val){
41+
public static int GetHash(this string val,ref int idx){
42+
return val?.GetHashCode() ?? 0;
43+
}
44+
public static int GetHash(this LFloat val,ref int idx){
4345
return PrimerLUT.GetPrimer(val._val);
4446
}
4547

46-
public static int GetHash(this LVector2 val){
48+
public static int GetHash(this LVector2 val,ref int idx){
4749
return PrimerLUT.GetPrimer(val._x) + PrimerLUT.GetPrimer(val._y) * 17;
4850
}
4951

50-
public static int GetHash(this LVector3 val){
52+
public static int GetHash(this LVector3 val,ref int idx){
5153
return PrimerLUT.GetPrimer(val._x)
5254
+ PrimerLUT.GetPrimer(val._y) * 31
5355
+ PrimerLUT.GetPrimer(val._z) * 37;

Unity/Assets/LockstepEngine/Serializaition/Src/Serializer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Net;
44
using System.Text;
5+
using Lockstep.Game;
56
using Lockstep.Math;
67

78
namespace Lockstep.Serialization {
@@ -274,8 +275,7 @@ public void WriteBytes_255(byte[] value){
274275

275276
public void Write(LVector2 val){Write(val._x);Write(val._y);}
276277

277-
public void Write( LVector3 val){Write(val._x);Write(val._y);Write(val._z);}
278-
278+
public void Write(LVector3 val){Write(val._x);Write(val._y);Write(val._z);}
279279

280280

281281
public void Write<T>(T[] value) where T : BaseFormater{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Lockstep.Game;
5+
using Lockstep.Serialization;
6+
7+
namespace Lockstep.Util {
8+
public static class BackUpUtil {
9+
public static void Write<T>(this Serializer writer, IList<T> lst) where T : class, IBackup, new(){
10+
writer.Write(lst?.Count ?? 0);
11+
foreach (var item in lst) {
12+
writer.Write(item == null);
13+
item?.WriteBackup(writer);
14+
}
15+
}
16+
17+
public static T[] ReadArray<T>(this Deserializer reader, T[] _) where T : class, IBackup, new(){
18+
var count = reader.ReadInt32();
19+
var lst = new T[count];
20+
for (int i = 0; i < count; i++) {
21+
var isNull = reader.ReadBoolean();
22+
T item = null;
23+
if (!isNull) {
24+
item = new T();
25+
item.ReadBackup(reader);
26+
}
27+
28+
lst[i] = item;
29+
}
30+
31+
return lst;
32+
}
33+
34+
public static List<T> ReadList<T>(this Deserializer reader, IList<T> _) where T : class, IBackup, new(){
35+
var count = reader.ReadInt32();
36+
var lst = new List<T>();
37+
for (int i = 0; i < count; i++) {
38+
var isNull = reader.ReadBoolean();
39+
T item = null;
40+
if (!isNull) {
41+
item = new T();
42+
item.ReadBackup(reader);
43+
}
44+
45+
lst.Add(item);
46+
}
47+
48+
return lst;
49+
}
50+
51+
public static void DumpList(string name, IList lst, StringBuilder sb, string prefix){
52+
sb.AppendLine(prefix + name + " Count" + ":" + lst.Count.ToString());
53+
sb.Append("[");
54+
for (int i = 0; i < lst.Count; i++) {
55+
var item = lst[i];
56+
if (item is IDumpStr dump) {
57+
dump?.DumpStr(sb, "\t" + prefix);
58+
}
59+
else {
60+
sb.Append(i + ":" + item.ToString());
61+
}
62+
}
63+
64+
sb.Append("]");
65+
}
66+
}
67+
}

Unity/Assets/LockstepEngine/Util/Src/DumpStrUtil.cs.meta renamed to Unity/Assets/LockstepEngine/Util/Src/BackUpUtil.cs.meta

File renamed without changes.

Unity/Assets/LockstepEngine/Util/Src/DumpStrUtil.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

Unity/Assets/LockstepEngineUnityExt/CodeGenerator/FiledHandler.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ public string DealUserClass(Type type, MemberInfo field){
8383
}
8484

8585
public string DealArray(Type t, MemberInfo field){
86-
return string.Format(arrayCodeTemplete, prefix, field.Name);
86+
return string.Format(arrayCodeTemplete, prefix, field.Name,t.GetElementType().FullName);
8787
}
8888

8989
public string DealList(Type t, MemberInfo field){
90-
return string.Format(lstCodeTemplete, prefix, field.Name);
90+
return string.Format(lstCodeTemplete, prefix, field.Name,t.GetGenericArguments()[0].FullName);
9191
}
9292

9393
public string DealDic(Type t, MemberInfo field){

0 commit comments

Comments
 (0)