/// <summary> /// Removes the oldest item in the cache and returns it. /// </summary> private BlockHolder GetOldestCacheItem() { Debug.Assert(m_cache.Count > 0, "There have to be items in the cache"); BlockHolder toRet = m_cache[0]; m_cache.Remove(toRet); return(toRet); }
public void TurnOffBlocks(int _from, int _to) { for (int i = _from; i < _to; i++) { BlockHolder bh = allBlockHolders[i]; foreach (Block b in bh.allblocks) { b.TurnOff(); } } }
void ApplyColors() { for (int i = 0; i < blocksOnRow; i++) { for (int j = 0; j < blocksOnColumn; j++) { BlockHolder bh = allBlockHolders[j]; Color randomColor = activeColorPalette[Random.Range(0, activeColorPalette.Length)]; bh.allblocks[i].Setup(blockMaterial, randomColor); } } }
public BlockHolder getBlockAt(BlockPos pos) { foreach (Chunk chunk in chunks) { BlockHolder block = chunk.getBlockAt(pos); if (block != null) { return(block); } } return(null); }
public void destroyBlockAt(BlockPos pos) { foreach (Chunk chunk in chunks) { BlockHolder block = chunk.getBlockAt(pos); if (block != null) { chunk.destroyBlock(block); return; } } }
// The following methods provide cache operations that abstract away the implementation of the cache. /// <summary> /// Gets the block holder for the given blockNum if it is in the cache, otherwise returns null. /// </summary> private BlockHolder GetHolderForBlockNumInCache(int blockNum) { BlockHolder toRet = null; foreach (BlockHolder bh in m_cache) { if (bh.BlockNum == blockNum) { toRet = bh; break; } } return(toRet); }
BlockHolder GetNearestBlock() { float NearestDistance = 100000; BlockHolder nearestBlock = null; for (int i = 0; i <= NearsetObject.Count - 1; i++) { float CurrentDistance = Vector3.Distance(NearsetObject[i].transform.position, transform.position); if (CurrentDistance < NearestDistance) { NearestDistance = CurrentDistance; nearestBlock = NearsetObject[i].GetComponent <BlockHolder>(); } } return(nearestBlock); }
void CreateBlocks() { allBlockHolders = new List <BlockHolder>(); GameObject blocksWrap = new GameObject(); for (int i = 0; i < blocksOnColumn; i++) { Vector3 newYpos = new Vector3(0f, i * blocksYOffset, 0f); BlockHolder bh = Instantiate(blockHolderPrefab, newYpos, Quaternion.identity); bh.transform.parent = blocksWrap.transform; bh.id = i; bh.gameObject.name = "bh_" + i; allBlockHolders.Add(bh); } blocksWrap.transform.position = transform.position; blocksWrap.transform.parent = dynamicObjects.transform; }
void Update() { if (!TwB) { TwB = this.GetComponent <TextWithBlock>(); } if (NearsetObject.Count != 0) { NearestBlock = GetNearestBlock(); } if (!bIsDragging && NearestBlock && TwB.BlockNumber >= 0) { if (bIsClear) { transform.position = FirstBlock.transform.position; //NearestBlock.CurrentBlockNumber = TwB.BlockNumber; DetachBlock(); bIsClear = false; } else { transform.position = NearestBlock.transform.position; NearestBlock.CurrentBlockNumber = TwB.BlockNumber; } if (NearestBlock.CompareTag("Block_Holder")) { bBlockIsHold = true; } if (NearestBlock.CompareTag("Block_Holder_R") && !bIsFirstHolder) { FirstBlock = NearestBlock; bIsFirstHolder = true; } } else { DetachBlock(); } }
private void HandleShipBlockCollision(BlockHolder blockHolder) { var ship = GetHolder().GetComponent <Base>(); if (!ship) { return; } if (blockHolder.IsHoldingDownPickUpButton() && blockEffectController.IsSelected()) { ship.WorkOnUnscrewingBlock(this); } if ((blockHolder.IsTryingToPickUp() && isExplosive) || ship.BlockIsUnscrewed(this)) { ship.DetachBlock(this); blockHolder.SetHoldingBlock(this); } }
public void destroyBlock(BlockHolder block) { BlockHolder toDestroy = null; BlockPos pos = block.getPos(); foreach (BlockHolder b in blocks) { if (block.getPos().getX() == b.getPos().getX()) { if (block.getPos().getY() == b.getPos().getY()) { if (block.getPos().getZ() == b.getPos().getZ()) { toDestroy = b; } } } } if (toDestroy == null) { return; } else { blocks.Remove(toDestroy); GameObject.Destroy(toDestroy.gameObject); try { getWorld().getBlockAt(new BlockPos(pos.getX() - 1, pos.getY(), pos.getZ())).updateSides(); } catch (System.Exception e) { } try { getWorld().getBlockAt(new BlockPos(pos.getX() + 1, pos.getY(), pos.getZ())).updateSides(); } catch (System.Exception e) { } try { getWorld().getBlockAt(new BlockPos(pos.getX(), pos.getY() - 1, pos.getZ())).updateSides(); } catch (System.Exception e) { } try { getWorld().getBlockAt(new BlockPos(pos.getX(), pos.getY() + 1, pos.getZ())).updateSides(); } catch (System.Exception e) { } try { getWorld().getBlockAt(new BlockPos(pos.getX(), pos.getY(), pos.getZ() - 1)).updateSides(); } catch (System.Exception e) { } try { getWorld().getBlockAt(new BlockPos(pos.getX(), pos.getY(), pos.getZ() + 1)).updateSides(); } catch (System.Exception e) { } } }
IEnumerator blockMake() { for (int x = 0 + (pos.getX() * 16); x < 16 + (pos.getX() * 16); x++) { for (int y = 0; y < 4; y++) { for (int z = 0 + (pos.getZ() * 16); z < 16 + (pos.getZ() * 16); z++) { GameObject block = GameObject.Instantiate(blockTemplate); BlockHolder b = block.AddComponent <BlockHolder>(); b.pos = new BlockPos(x, y, z); block.transform.position = new Vector3(x, y, z); if (y == 3) { b.blockNameForTestingBecauseImTooLazyTooWriteMoreCodeForThis = "dirt"; } else if (y == 0) { b.blockNameForTestingBecauseImTooLazyTooWriteMoreCodeForThis = "bedrock"; } else { b.blockNameForTestingBecauseImTooLazyTooWriteMoreCodeForThis = "stone"; } b.updateSides(); blocks.Add(b); yield return(new WaitForSeconds(0.1f)); } } } }
public override void SendResponse(INextLayer nextLayer, Exchange exchange, Response response) { if (exchange.OscoapContext != null) { SecurityContext ctx = exchange.OscoapContext; Codec.IMessageEncoder me = Spec.NewMessageEncoder(); Response encryptedResponse = new Response((StatusCode)response.Code); if (response.Payload != null) { encryptedResponse.Payload = response.Payload; } MoveResponseHeaders(response, encryptedResponse); if (_Log.IsInfoEnabled) { _Log.Info("SendResponse: New inner response message"); _Log.Info(encryptedResponse.ToString()); } // Build AAD CBORObject aad = CBORObject.NewArray(); aad.Add(1); aad.Add(CBORObject.NewArray()); aad[1].Add(ctx.Sender.Algorithm); aad.Add(exchange.OscoapSenderId); aad.Add(exchange.OscoapSequenceNumber); aad.Add(CBORObject.FromObject(new byte[0])); // Options if (ctx.GroupId != null) { aad.Add(ctx.GroupId); } if (_Log.IsInfoEnabled) { _Log.Info("SendResponse: AAD = " + BitConverter.ToString(aad.EncodeToBytes())); } Encrypt0Message enc = new Encrypt0Message(false); byte[] msg = me.Encode(encryptedResponse); int tokenSize = msg[0] & 0xf; byte[] msg2 = new byte[msg.Length - (3 + tokenSize)]; Array.Copy(msg, 4 + tokenSize, msg2, 1, msg2.Length - 1); msg2[0] = msg[1]; enc.SetContent(msg2); enc.SetExternalData(aad.EncodeToBytes()); if (response.HasOption(OptionType.Observe) || ctx.GroupId != null) { enc.AddAttribute(HeaderKeys.PartialIV, CBORObject.FromObject(ctx.Sender.PartialIV), Attributes.UNPROTECTED); enc.AddAttribute(HeaderKeys.IV, ctx.Sender.GetIV(ctx.Sender.PartialIV), Attributes.DO_NOT_SEND); ctx.Sender.IncrementSequenceNumber(); if (ctx.GroupId != null) { enc.AddAttribute(HeaderKeys.KeyId, CBORObject.FromObject(ctx.Sender.Id), Attributes.UNPROTECTED); } } else { CBORObject iv = ctx.Recipient.GetIV(exchange.OscoapSequenceNumber); enc.AddAttribute(HeaderKeys.IV, iv, Attributes.DO_NOT_SEND); } _Log.Info(m => m($"SendResponse: IV = {BitConverter.ToString(enc.FindAttribute(HeaderKeys.IV, Attributes.DO_NOT_SEND).GetByteString())}")); _Log.Info(m => m($"SendResponse: Key = {BitConverter.ToString(ctx.Sender.Key)}")); enc.AddAttribute(HeaderKeys.Algorithm, ctx.Sender.Algorithm, Attributes.DO_NOT_SEND); enc.Encrypt(ctx.Sender.Key); byte[] finalBody = DoCompression(enc); OscoapOption o = new OscoapOption(OptionType.Oscoap); o.Set(finalBody); response.AddOption(o); response.StatusCode = StatusCode.Content; response.Payload = enc.GetEncryptedContent(); // Need to be able to retrieve this again undersome cirumstances. if (encryptedResponse.HasOption(OptionType.Block2)) { Request request = exchange.CurrentRequest; Exchange.KeyUri keyUri = new Exchange.KeyUri(request.URI, null, response.Destination); // Observe notification only send the first block, hence do not store them as ongoing if (exchange.OSCOAP_ResponseBlockStatus != null && !encryptedResponse.HasOption(OptionType.Observe)) { // Remember ongoing blockwise GET requests BlockHolder blockInfo = new BlockHolder(exchange); if (Util.Utils.Put(_ongoingExchanges, keyUri, blockInfo) == null) { if (_Log.IsInfoEnabled) { _Log.Info("Ongoing Block2 started late, storing " + keyUri + " for " + request); } } else { if (_Log.IsInfoEnabled) { _Log.Info("Ongoing Block2 continued, storing " + keyUri + " for " + request); } } } else { if (_Log.IsInfoEnabled) { _Log.Info("Ongoing Block2 completed, cleaning up " + keyUri + " for " + request); } BlockHolder exc; _ongoingExchanges.TryRemove(keyUri, out exc); } } } base.SendResponse(nextLayer, exchange, response); }
/// <summary> /// Adds the given holder to the cache as the most recently used item /// </summary> private void AddMostRecentlyUsed(BlockHolder bh) { m_cache.Add(bh); }
/// <summary> /// Updates the given block holer to be the most recently used item in the cache /// </summary> private void UpdateLastUsedTime(BlockHolder bh) { m_cache.Remove(bh); AddMostRecentlyUsed(bh); }
/// <summary> /// Looks to see if the block is in the cache. if it is, it performs /// the cache hit operation on the data. If the block was not in the cache /// it pulls the block holder out of the cache so that no other thread can /// access the block. It then performs the blocking read/write disk i/o /// after giving up all locks. Finally it gets the locks again to insert /// the new block back into the cache /// </summary> /// <param name="cacheHit">Operation to perform if the block is in cache (cannot block)</param> /// <param name="chacheMiss">Operation to perform if the block is not in cache (can block)</param> private int PerformBlockOperation(int blockNum, BlockOperation cacheHit, BlockOperation chacheMiss) { BlockHolder bh = null; bool success = false; do { lock (m_cache) { bh = GetHolderForBlockNumInCache(blockNum); if (bh == null) { // the block is not in cache while (m_cache.Count == 0) { // condition wait on the cache until there is a block in // the cache we can read into. This is a very rare case. // It would only happen if more threads were waiting on // the cache than there were spaces for blocks in the cache } // remove the oldest thing on the cache. lock (m_diskQueue) { if (!m_diskQueue.Contains(blockNum)) { bh = GetOldestCacheItem(); m_diskQueue.Add(blockNum); } else { // someone else is already fetching this block from disk. // After we release the lock } } } else { // the block is in the cache, so just perform the operation, // and update it's position to maintain LRU semantics cacheHit(bh); UpdateLastUsedTime(bh); success = true; } }// unlock cache if (!success) { if (bh == null) { // if we missed the cache (i.e. !success) and the holder is null // it means another thread is fetching that block from disk. lock (m_diskQueue) { while (m_diskQueue.Contains(blockNum)) { // condition wait on m_diskQueue } } // If we get here the block should be in the cache (at least for now) // success is still false, so we'll loop in this method and hopefully // get a cache hit, or else work on getting the block again. } else { // The holder is now isolated, so we can make all the blocking calls we want chacheMiss(bh); lock (m_cache) { lock (m_diskQueue) { AddMostRecentlyUsed(bh); m_diskQueue.Remove(bh.BlockNum); // broadcast on the condition variable associated with m_diskQueue } success = true; } } } } while (!success); return(0); }