Пример #1
0
        public void Read(BlockingDictionary <int, byte[]> dictionary, BinaryReader reader)
        {
            var i = 0;

            while (reader.BaseStream.Length != reader.BaseStream.Position)
            {
                _timer.Start();
                var content = reader.ReadBytes(_chunkSize);
                _timer.Stop();
                dictionary.Add(i, content);
                _logger.Write($"Read chunk {i}");
                i++;
            }
        }
Пример #2
0
 private void Processing()
 {
     try
     {
         while (_processingBuffer.TryDequeue(out var blockToProcess) && _error == null)
         {
             var processedBlock = _processor.Process(blockToProcess);
             _writingBuffer.Add(processedBlock.Id, processedBlock);
         }
     }
     catch (Exception e)
     {
         _error = e;
     }
 }
Пример #3
0
 public void ProcessChunks(BlockingDictionary <int, byte[]> inputDictionary,
                           BlockingDictionary <int, byte[]> outputDictionary)
 {
     while (!inputDictionary.IsComplete())
     {
         try
         {
             var(id, chunk) = inputDictionary.GetFirstItem();
             var processedChunk = _contentProcessor.Process(chunk);
             outputDictionary.Add(id, processedChunk);
             _logger.Write($"Process chunk {id}");
         }
         catch (InvalidOperationException)
         {
             break;
         }
     }
 }
Пример #4
0
    //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);
            }
        }
    }
Пример #5
0
    public void SetBlock(long x, int y, long z, uint value)
    {
        long hash = HashBlock(x, z);
        int  xVal = (int)(x % Constants.ChunkWidth);
        int  zVal = (int)(z % Constants.ChunkWidth);

        #region Specified Block
        if (!lockMap.ContainsKey(hash))
        {
            lockMap.Add(hash, new object());
        }
        lock (lockMap[hash])
        {
            if (xVal < 0)
            {
                xVal = Constants.ChunkWidth + xVal;
            }
            if (zVal < 0)
            {
                zVal = Constants.ChunkWidth + zVal;
            }
            if (updateMap.ContainsKey(hash))
            {
                updateMap[hash].positions.Add(new long[] { hash, xVal, y, zVal });
                updateMap[hash].values.Add(value);
            }
            else
            {
                ChunkUpdate update = new ChunkUpdate();
                update.positions.Add(new long[] { hash, xVal, y, zVal });
                update.values.Add(value);
                updateMap[hash] = update;
            }
        }
        #endregion
        #region x=0
        if (xVal == 0)
        {
            hash = HashBlock(x - 1, z);
            if (!lockMap.ContainsKey(hash))
            {
                lockMap.Add(hash, new object());
            }
            lock (lockMap[hash])
            {
                xVal = Constants.ChunkWidth;
                zVal = (int)(z % Constants.ChunkWidth);
                if (zVal < 0)
                {
                    zVal = Constants.ChunkWidth + zVal;
                }
                if (updateMap.ContainsKey(hash))
                {
                    updateMap[hash].positions.Add(new long[] { hash, xVal, y, zVal });
                    updateMap[hash].values.Add(value);
                }
                else
                {
                    ChunkUpdate update = new ChunkUpdate();
                    update.positions.Add(new long[] { hash, xVal, y, zVal });
                    update.values.Add(value);
                    updateMap.Add(hash, update);
                }
            }
        }
        xVal = (int)(x % Constants.ChunkWidth);
        #endregion
        #region z=0
        if (zVal == 0)
        {
            hash = HashBlock(x, z - 1);
            if (!lockMap.ContainsKey(hash))
            {
                lockMap.Add(hash, new object());
            }
            lock (lockMap[hash])
            {
                xVal = (int)(x % Constants.ChunkWidth);
                zVal = Constants.ChunkWidth;
                if (xVal < 0)
                {
                    xVal = Constants.ChunkWidth + xVal;
                }
                if (updateMap.ContainsKey(hash))
                {
                    updateMap[hash].positions.Add(new long[] { hash, xVal, y, zVal });
                    updateMap[hash].values.Add(value);
                }
                else
                {
                    ChunkUpdate update = new ChunkUpdate();
                    update.positions.Add(new long[] { hash, xVal, y, zVal });
                    update.values.Add(value);
                    updateMap.Add(hash, update);
                }
            }
        }
        #endregion
        zVal = (int)(z % Constants.ChunkWidth);
        #region x&z=0
        if (xVal == 0 && zVal == 0)
        {
            hash = HashBlock(x - 1, z - 1);
            if (!lockMap.ContainsKey(hash))
            {
                lockMap.Add(hash, new object());
            }
            lock (lockMap[hash])
            {
                xVal = Constants.ChunkWidth;
                zVal = Constants.ChunkWidth;
                if (updateMap.ContainsKey(hash))
                {
                    updateMap[hash].positions.Add(new long[] { hash, xVal, y, zVal });
                    updateMap[hash].values.Add(value);
                }
                else
                {
                    ChunkUpdate update = new ChunkUpdate();
                    update.positions.Add(new long[] { hash, xVal, y, zVal });
                    update.values.Add(value);
                    updateMap.Add(hash, update);
                }
            }
        }
        #endregion
    }