private IEnumerator CheckDeletionQueue() { while (true) { for (int n = 0; n < CheckNum; n++) { if (deletionPool.Count > 0) { generationRequests.run = false; long key = long.MaxValue; key = deletionPool.Dequeue(); if (key != long.MaxValue) { chunkMap[key] = null; chunkMap.Remove(key); chunkList.Remove(key); } } else { generationRequests.run = true; } } yield return(new WaitForEndOfFrame()); } }
private void CheckUpdateMap() { while (true) { if (KillThread) { return; } if (updateMap.Count > 0 && generationRequests.Size() == 0 && generationRequests.Empty() && finishedGeneration.Empty()) { try { List <long> keys = new List <long>(); keys.AddRange(updateMap.Keys); foreach (long key in keys) { if (!lockMap.ContainsKey(key)) { lockMap.Add(key, new object()); } lock (lockMap[key]) { generationRequests.Push(new ChunkRequest(ChunkRequest.RequestType.Update, updateMap[key])); updateMap.Remove(key); } } } catch (Exception e) { Debug.Log(e.Message + "\n" + e.StackTrace); } } } }
//Repeating Thread Method. private void GenerateChunk() { while (true) { if (KillThread) { return; } try { ChunkRequest request = generationRequests.Pop(); if (request == null) { return; } switch (request.chunkRequestType) { #region Generation case ChunkRequest.RequestType.Generation: { long hash = Hash(request.chunk); request.chunk.Generate(); if (!lockMap.ContainsKey(hash)) { lockMap.Add(hash, new object()); } lock (lockMap[hash]) { if (updateGenerationMap.ContainsKey(hash)) { ChunkUpdate update = updateGenerationMap[hash]; updateGenerationMap.Remove(hash); request.chunk.UpdateData(update); } } request.chunk.Mesh(); finishedGeneration.Push(request.chunk); break; } #endregion #region Update case ChunkRequest.RequestType.Update: { ChunkUpdate update = request.update; long hash = update.positions[0][0]; if (!lockMap.ContainsKey(hash)) { lockMap.Add(hash, new object()); } lock (lockMap[hash]) { if (chunkMap.ContainsKey(hash)) { chunkMap[hash].UpdateData(update); chunkMap[hash].Mesh(); finishedUpdate.Push(chunkMap[hash]); } else if (generatedChunk.ContainsKey(hash)) { Debug.Log("Could not find Hashed chunk at: " + hash + " but it has been generated!"); if (updateMap.ContainsKey(hash)) { updateMap[hash].positions.AddRange(update.positions); updateMap[hash].values.AddRange(update.values); } updateMap.Add(hash, update); } else { Debug.Log("Chunk at: " + (hash % int.MaxValue) + "," + (hash >> 31) + " doesn't exist!"); if (updateGenerationMap.ContainsKey(hash)) { updateGenerationMap[hash].positions.AddRange(update.positions); updateGenerationMap[hash].values.AddRange(update.values); } else { updateGenerationMap.Add(hash, update); } } } break; } #endregion #region Load case ChunkRequest.RequestType.Load: { break; } #endregion #region Save case ChunkRequest.RequestType.Save: { break; } #endregion #region Delete case ChunkRequest.RequestType.Deletion: { break; } #endregion } } catch (Exception e) { Debug.Log(e.Message + " :: " + e.StackTrace + "::" + e.Data); } } }