feat: non-generic FNV1a hasher added
This commit is contained in:
		
							
								
								
									
										21
									
								
								Engine.Systems/Network/Hasher.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								Engine.Systems/Network/Hasher.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,21 @@
 | 
				
			|||||||
 | 
					namespace Engine.Systems.Network;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public static class Hasher
 | 
				
			||||||
 | 
					{
 | 
				
			||||||
 | 
					    public static long FNV1a(string name)
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        const long fnvPrime = 1099511628211;
 | 
				
			||||||
 | 
					        long result = 1469598103934665603;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        unchecked
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            foreach (char c in name)
 | 
				
			||||||
 | 
					            {
 | 
				
			||||||
 | 
					                result ^= c;
 | 
				
			||||||
 | 
					                result *= fnvPrime;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return result;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -8,18 +8,7 @@ public static class TypeHasher<T>
 | 
				
			|||||||
        get
 | 
					        get
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            if (_fnv1a == 0)
 | 
					            if (_fnv1a == 0)
 | 
				
			||||||
                unchecked
 | 
					                _fnv1a = Hasher.FNV1a(typeof(T).FullName ?? typeof(T).Name);
 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    const long fnvPrime = 1099511628211;
 | 
					 | 
				
			||||||
                    _fnv1a = 1469598103934665603;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    string typeName = typeof(T).FullName ?? typeof(T).Name;
 | 
					 | 
				
			||||||
                    foreach (char c in typeName)
 | 
					 | 
				
			||||||
                    {
 | 
					 | 
				
			||||||
                        _fnv1a ^= c;
 | 
					 | 
				
			||||||
                        _fnv1a *= fnvPrime;
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return _fnv1a;
 | 
					            return _fnv1a;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user