public NoiseGenerator(int seed, float frequency) { _hash1 = new XXHash(seed); _hash2 = new XXHash(seed ^ 0x1327495a); _hash3 = new XXHash(seed ^ 0x3cbe84f2); _frequency = frequency; }
public void Execute([ReadOnly] ref Voxel voxel, ref Translation position, ref Scale scale) { // Per-instance random number var hash = new XXHash(voxel.ID); var rand1 = hash.Value01(1); var rand2 = hash.Value01(2); // Extract the current position/scale. var _pos = position.Value; var _scale = scale.Value; // Move/Shrink. _pos += new float3(0.1f, -2.0f, 0.3f) * (rand2 + 0.1f) * dt; _scale *= math.lerp(0.9f, 0.98f, rand1); //Build a new position and scale position = new Translation { Value = _pos }; scale = new Scale { Value = _scale }; // Build a new matrix. //matrix = new TransformMatrix { // Value = new float4x4( // new float4(scale, 0, 0, 0), // new float4(0, scale, 0, 0), // new float4(0, 0, scale, 0), // new float4(pos.x, pos.y, pos.z, 1) // ) //}; }
public SourceCodeFile(string fileName, string sourceCode) { FileName = fileName; SourceCode = null; SourceCode = sourceCode; HashCode = XXHash.XXH32(Encoding.UTF8.GetBytes(SourceCode)); }
/// <summary> /// A representative hash code. /// </summary> /// <returns>A number that is guaranteed to be the same when generated from two objects that are the same.</returns> public override int GetHashCode() { int hash = 0; int flags = 0; flags |= (receiveShadows ? 1 : 0) << 0; flags |= (needMotionVectorPass ? 1 : 0) << 1; unsafe { var buffer = stackalloc[] { ReferenceEquals(mesh, null) ? 0 : mesh.GetHashCode(), ReferenceEquals(material, null) ? 0 : material.GetHashCode(), subMesh.GetHashCode(), layer.GetHashCode(), castShadows.GetHashCode(), flags }; hash = (int)XXHash.Hash32((byte *)buffer, 6 * 4); } return(hash); }
internal void GenerateGlobalObjectIdHash() { // HEAD: DO NOT USE! TEST ONLY TEMP IMPL, WILL BE REMOVED if (TempGlobalObjectIdHashOverride != 0) { GlobalObjectIdHash = TempGlobalObjectIdHashOverride; return; } // TAIL: DO NOT USE! TEST ONLY TEMP IMPL, WILL BE REMOVED // do NOT regenerate GlobalObjectIdHash for NetworkPrefabs while Editor is in PlayMode if (UnityEditor.EditorApplication.isPlaying && !string.IsNullOrEmpty(gameObject.scene.name)) { return; } // do NOT regenerate GlobalObjectIdHash if Editor is transitining into or out of PlayMode if (!UnityEditor.EditorApplication.isPlaying && UnityEditor.EditorApplication.isPlayingOrWillChangePlaymode) { return; } var globalObjectIdString = UnityEditor.GlobalObjectId.GetGlobalObjectIdSlow(this).ToString(); GlobalObjectIdHash = XXHash.Hash32(globalObjectIdString); }
public NoiseGenerator(float frequency) { _hash1 = new XXHash(Random.Range(0, 0x7fffffff)); _hash2 = new XXHash(Random.Range(0, 0x7fffffff)); _hash3 = new XXHash(Random.Range(0, 0x7fffffff)); _frequency = frequency; }
public void Hash64_LongStream_ReturnsExpected() { var buffer = getLargeBuffer(); using var stream = new MemoryStream(buffer); Assert.AreEqual(17345881079506341799, XXHash.Hash64(stream)); }
private static void doRename(string[] args) { List <String> images = new List <String>(); rescursiveDig(args[1], images); List <String> rawDataFiles = new List <String>(); rescursiveDig(args[2], rawDataFiles); foreach (String imageName in images) { if (imageName.Contains(".png")) { int count = 0; String sub = imageName.Substring(args[1].Length, imageName.Length - 5 - args[1].Length); foreach (String rawFileName in rawDataFiles) { if (!rawFileName.Contains(".c") && rawFileName.Contains(sub)) { byte[] bytes = File.ReadAllBytes(rawFileName); uint result = XXHash.Hash32(bytes); Console.WriteLine("Found one " + count + " - " + sub + " crc " + getHex((int)result, 8) + rawFileName); byte[] imageData = File.ReadAllBytes(imageName); File.WriteAllBytes(args[3] + getHex((int)result, 8) + ".png", imageData); count++; } } } } }
public Region GetOrGenerateRegion(Vector2Int position) { if (this.regions.TryGetValue(position, out Region region)) { return(region); } byte[] buf = BitConverter.GetBytes(position.x).Concat(BitConverter.GetBytes(position.y)).ToArray(); uint valueX = XXHash.CalculateHash(buf, buf.Length, seed: Convert.ToUInt32(seed)); uint valueZ = XXHash.CalculateHash(buf, buf.Length, seed: Convert.ToUInt32(seed + 1)); Vector2 relativeVoronoiPoint = new Vector2(valueX, valueZ) / uint.MaxValue; Vector2 voronoiPoint = (position + relativeVoronoiPoint); float humidity = (float)SimplexNoise.Scale(NoiseGenerator.Eval(voronoiPoint.x, voronoiPoint.y), 0d, 1d); float temperature = (float)SimplexNoise.Scale(NoiseGenerator.Eval(voronoiPoint.y, voronoiPoint.x), 0d, 1d); region = new Region( name: "", regionID: position, relativeVoronoiPoint, humidity, temperature ); Debug.Log($"Generated Region {region.RegionID} with Humidity: {region.Humidity} and Temperature: {region.Temperature}"); regions.Add(region.RegionID, region); // TODO: Remove this later, it is for debugging // GameObject go = new GameObject($"Voronoi {x} {z}"); // go.transform.position = new Vector3(region.VoronoiWorldPoint.x, 0, region.VoronoiWorldPoint.y); return(region); }
public void Hash32_LongStream_ReturnsExpected() { var buffer = getLargeBuffer(); using var stream = new MemoryStream(buffer); Assert.AreEqual(3662909991, XXHash.Hash32(stream)); }
public void Execute([ReadOnly] ref Voxel voxel, ref TransformMatrix matrix) { // Per-instance random number var hash = new XXHash(voxel.ID); var rand1 = hash.Value01(1); var rand2 = hash.Value01(2); // Extract the current position/scale. var pos = matrix.Value.m3.xyz; var scale = matrix.Value.m0.x; // Move/Shrink. pos += new float3(0.1f, -2.0f, 0.3f) * (rand2 + 0.1f) * dt; scale *= math.lerp(0.9f, 0.98f, rand1); // Build a new matrix. matrix = new TransformMatrix { Value = new float4x4( scale, 0, 0, 0, 0, scale, 0, 0, 0, 0, scale, 0, pos.x, pos.y, pos.z, 1 ) }; }
public void Hash64_BinaryTests(string text, uint seed, uint _, ulong hash64) { var buffer = Encoding.UTF8.GetBytes(text); var result = XXHash.Hash64(buffer, seed); Assert.AreEqual(hash64, result); }
void Start() { var hash = new XXHash(_randomSeed); var mat = new MaterialOverride(_randomSeed); var parent = transform; var rot = quaternion.identity; // Column - Row - Depth for (var col = 0u; col < _extent.x; col++) { for (var row = 0u; row < _extent.y; row++) { // Per-strip parameters var seed = hash.UInt(col) + hash.UInt(row); mat.SetParameters(seed + 500); for (var depth = 0u; depth < _extent.z; depth++) { // Random removal if (hash.Float(seed + depth) < _removalRate) { continue; } // Instance position var p = math.float3(col, row, depth); p.xy = p.xy - _extent.xy / 2 + math.float2(0, 0.5f); // Instantiation and material overriding mat.Apply(Instantiate(_prefab, p, rot, parent)); } } } }
public void SetParameters(uint seed) { var hash = new XXHash(seed); _props.SetFloat(_seed1Key, hash.Float(0)); _props.SetFloat(_seed2Key, hash.Float(1)); }
static void Scan(ScanOptions options) { var files = Directory.GetFiles(options.Source, "*.*", SearchOption.AllDirectories); var entries = new List <JsonFileEntry>(); foreach (var file in files) { using var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read); using var streamReader = new BufferedStreamReader(fileStream, 8); // Check if compressed archive (magic header) var header = streamReader.Read <uint>(); if (header == 0x80000001) { Console.WriteLine($"Found: {Path.GetFileName(file)}, hashing and adding."); var wholeFile = File.ReadAllBytes(file); var checksum = XXHash.Hash32(wholeFile); entries.Add(new JsonFileEntry(Path.GetFileName(file), streamReader.Read <int>(), (int)fileStream.Length, checksum)); } } var jsonFile = new JsonFile(entries); File.WriteAllText(options.SavePath, jsonFile.ToFile()); }
/// <summary> /// Sends a named message /// </summary> /// <param name="name">The message name to send</param> /// <param name="clientId">The client to send the message to</param> /// <param name="stream">The message stream containing the data</param> /// <param name="networkChannel">The channel to send the data on</param> public void SendNamedMessage(string name, ulong clientId, Stream stream, NetworkChannel networkChannel = NetworkChannel.Internal) { ulong hash = 0; switch (m_NetworkManager.NetworkConfig.RpcHashSize) { case HashSize.VarIntFourBytes: hash = XXHash.Hash32(name); break; case HashSize.VarIntEightBytes: hash = XXHash.Hash64(name); break; } using (var messageBuffer = PooledNetworkBuffer.Get()) using (var writer = PooledNetworkWriter.Get(messageBuffer)) { writer.WriteUInt64Packed(hash); messageBuffer.CopyFrom(stream); m_NetworkManager.MessageSender.Send(clientId, NetworkConstants.NAMED_MESSAGE, networkChannel, messageBuffer); PerformanceDataManager.Increment(ProfilerConstants.NamedMessageSent); } }
public StatementInfo(string statement, int start, int end, int startLine) { Hashcode = XXHash.XXH32(Encoding.UTF8.GetBytes(statement)); Start = start; End = end; StartLine = startLine; }
private void PackWadFolders(string wadLocation) { //Loop through each WAD folder foreach (string wadFolder in Directory.EnumerateDirectories(wadLocation)) { char separator = Pathing.GetPathSeparator(wadFolder); string wadName = wadFolder.Split(separator).Last(); using (WADFile wad = new WADFile(3, 0)) { //Add each file to the WAD foreach (string wadFolderFile in Directory.EnumerateFiles(wadFolder, "*", SearchOption.AllDirectories)) { string path = wadFolderFile.Replace(wadFolder + separator, "").Replace('\\', '/'); ulong hash = XXHash.XXH64(Encoding.ASCII.GetBytes(path.ToLower())); string extension = Path.GetExtension(wadFolderFile); wad.AddEntry(hash, File.ReadAllBytes(wadFolderFile), extension != ".wpk" && extension != ".bnk" ? true : false); } //After WAD creation is finished we can write the WAD to the ZIP ZipArchiveEntry archiveEntry = this.Content.CreateEntry(string.Format(@"WAD\{0}", wadName)); wad.Write(archiveEntry.Open()); } } }
public void Hash32_BinaryTests(string text, uint seed, uint hash32, ulong _) { var buffer = Encoding.UTF8.GetBytes(text); var result = XXHash.Hash32(buffer, seed); Assert.That(result, Is.EqualTo(hash32)); }
private void PackWadFolder(string wadFolderLocation) { string[] wadFolderFiles = Directory.GetFiles(wadFolderLocation, "*", SearchOption.AllDirectories); if (wadFolderFiles.Length > 0) { char separator = Pathing.GetPathSeparator(wadFolderLocation); string wadName = wadFolderLocation.Split(separator).Last(); WadBuilder wad = new WadBuilder(); //Add each file to the WAD foreach (string wadFolderFile in Directory.EnumerateFiles(wadFolderLocation, "*", SearchOption.AllDirectories)) { string path = wadFolderFile.Replace(wadFolderLocation + separator, "").Replace('\\', '/'); ulong hash = XXHash.XXH64(Encoding.ASCII.GetBytes(path.ToLower())); WadEntryBuilder entryBuilder = new(); entryBuilder .WithPathXXHash(hash) .WithFileDataStream(File.OpenRead(wadFolderFile)); wad.WithEntry(entryBuilder); } //After WAD creation is finished we can write the WAD to the ZIP ZipArchiveEntry archiveEntry = this.Content.CreateEntry(string.Format("WAD/{0}", wadName)); wad.Build(archiveEntry.Open(), false); } }
public static string ComputeURLHash(string value) { byte[] vba = Encoding.UTF8.GetBytes(value); ulong hv = XXHash.XXH64(vba); return(Convert.ToBase64String(BitConverter.GetBytes(hv))); }
/// <summary>XXHash 256 bytes.</summary> /// <param name="bytes">The bytes.</param> /// <returns> /// <br /> /// </returns> public static byte[] Twox256(byte[] bytes) { return(BitConverter.GetBytes(XXHash.XXH64(bytes, 0)) .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 1))) .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 2))) .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 3))) .ToArray()); }
public void Hash32_BigEndian_BinaryTests(string text, uint seed, uint hash32, ulong _) { Bits.IsBigEndian = true; var buffer = Encoding.UTF8.GetBytes(text); var result = XXHash.Hash32(buffer, seed); Assert.AreEqual(hash32, result); }
Vector3 RandomPoint(XXHash hash, int id) { float u = hash.Range(-Mathf.PI, Mathf.PI, id * 2); float z = hash.Range(-1.0f, 1.0f, id * 2 + 1); float l = Mathf.Sqrt(1 - z * z); return(new Vector3(Mathf.Cos(u) * l, Mathf.Sin(u) * l, z)); }
/// <summary> /// Writes a frame of input to the file. /// </summary> /// <param name="inputs">a frame of input to write.</param> public unsafe void WriteInputs(NativeSlice <PlayerInput> inputs) { Assert.IsTrue(inputs.Length == _config.PlayerCount); var ptr = (byte *)NativeSliceUnsafeUtility.GetUnsafeReadOnlyPtr(inputs); int size = _config.PlayerCount * UnsafeUtility.SizeOf <PlayerInput>(); _writer.Write(XXHash.Hash64(ptr, size)); _writer.WriteBytes(ptr, size); }
public void Hash32_StreamTests(string text, uint seed, uint hash32, ulong _) { var buffer = Encoding.UTF8.GetBytes(text); using var stream = new MemoryStream(buffer); var result = XXHash.Hash32(stream, seed); Assert.That(result, Is.EqualTo(hash32)); }
public void Hash64_StreamTests(string text, uint seed, uint _, ulong hash64) { var buffer = Encoding.UTF8.GetBytes(text); using var stream = new MemoryStream(buffer); var result = XXHash.Hash64(stream, seed); Assert.AreEqual(hash64, result); }
public static string GetStorageKey(Hasher type, byte[] bytes) { // byte[] key = new byte[2 * Consts.STORAGE_KEY_BYTELENGTH + 3]; string key = string.Empty; if (type == Hasher.XXHASH) { var xxhash1 = XXHash.XXH64(bytes, 0, bytes.Length, 0); byte[] bytes1 = new byte[] { (byte)(xxhash1 & 0xFF), (byte)((xxhash1 & 0xFF00) >> 8), (byte)((xxhash1 & 0xFF0000) >> 16), (byte)((xxhash1 & 0xFF000000) >> 24), (byte)((xxhash1 & 0xFF00000000) >> 32), (byte)((xxhash1 & 0xFF0000000000) >> 40), (byte)((xxhash1 & 0xFF000000000000) >> 48), (byte)((xxhash1 & 0xFF00000000000000) >> 56) }; var xxhash2 = XXHash.XXH64(bytes, 0, bytes.Length, 1); byte[] bytes2 = new byte[] { (byte)(xxhash2 & 0xFF), (byte)((xxhash2 & 0xFF00) >> 8), (byte)((xxhash2 & 0xFF0000) >> 16), (byte)((xxhash2 & 0xFF000000) >> 24), (byte)((xxhash2 & 0xFF00000000) >> 32), (byte)((xxhash2 & 0xFF0000000000) >> 40), (byte)((xxhash2 & 0xFF000000000000) >> 48), (byte)((xxhash2 & 0xFF00000000000000) >> 56) }; foreach (var bt in bytes1) { key += bt.ToString("X2"); } foreach (var bt in bytes2) { key += bt.ToString("X2"); } } else if (type == Hasher.BLAKE2) { var config = new Blake2Core.Blake2BConfig { OutputSizeInBytes = 16 }; var hash = Blake2Core.Blake2B.ComputeHash(bytes, 0, bytes.Length, config); foreach (var bt in hash) { key += bt.ToString("X2"); } } return(key); }
public void SameSeedSameNumbers() { HashFunction duplicate = new XXHash(seed); for (int i = 0; i < 100; i++) { int key = random.Next(int.MinValue, int.MaxValue); Assert.AreEqual(function.Value(key), duplicate.Value(key), "Hash functions of same seed given same value should produce same output."); } }
public void Hash32_StreamTests(string text, uint seed, uint hash32, ulong _) { var buffer = Encoding.UTF8.GetBytes(text); using (var stream = new MemoryStream(buffer)) { var result = XXHash.Hash32(stream, seed); Assert.AreEqual(hash32, result); } }
void Start() { var hash = new XXHash(); var texture = new Texture2D(size, size); for (var y = 0; y < size; y++) { for (var x = 0; x < size; x++) { var c = hash.Value01(x + y * size); texture.SetPixel(x, y, new Color(c, c, c)); } } texture.Apply(); GetComponent<Renderer>().material.mainTexture = texture; }