Exemplo n.º 1
0
        internal void RegisterEvents(bool register = true)
        {
            if (register)
            {
                if (Registered)
                {
                    Log.Line($"Comp RegisterEvents error");
                }
                //TODO change this
                Registered = true;
                TerminalBlock.AppendingCustomInfo += AppendingCustomInfo;
                MyCube.IsWorkingChanged           += IsWorkingChanged;
                MyCube.OnMarkForClose             += OnMarkForClose;
                IsWorkingChanged(MyCube);

                if (BlockInventory == null)
                {
                    Log.Line($"BlockInventory is null");
                }
                else
                {
                    BlockInventory.InventoryContentChanged     += OnContentsChanged;
                    Session.BlockInventoryItems[BlockInventory] = new ConcurrentDictionary <uint, BetterInventoryItem>();
                    Session.AmmoThreadItemList[BlockInventory]  = Session.BetterItemsListPool.Get();

                    var items = BlockInventory.GetItems();
                    for (int i = 0; i < items.Count; i++)
                    {
                        var bItem = Session.BetterInventoryItems.Get();
                        var item  = items[i];
                        bItem.Amount  = (int)item.Amount;
                        bItem.Item    = item;
                        bItem.Content = item.Content;

                        Session.BlockInventoryItems[BlockInventory][items[i].ItemId] = bItem;
                    }
                }
            }
            else
            {
                if (!Registered)
                {
                    Log.Line($"Comp UnRegisterEvents error");
                }

                if (Registered)
                {
                    //TODO change this
                    Registered = false;
                    TerminalBlock.AppendingCustomInfo -= AppendingCustomInfo;

                    MyCube.IsWorkingChanged -= IsWorkingChanged;
                    MyCube.OnMarkForClose   -= OnMarkForClose;

                    if (BlockInventory == null)
                    {
                        Log.Line($"BlockInventory is null");
                    }
                    else
                    {
                        BlockInventory.InventoryContentChanged -= OnContentsChanged;
                        ConcurrentDictionary <uint, BetterInventoryItem> removedItems;
                        MyConcurrentList <BetterInventoryItem>           removedList;

                        if (Session.BlockInventoryItems.TryRemove(BlockInventory, out removedItems))
                        {
                            foreach (var inventoryItems in removedItems)
                            {
                                Session.BetterInventoryItems.Return(inventoryItems.Value);
                            }

                            removedItems.Clear();
                        }

                        if (Session.AmmoThreadItemList.TryRemove(BlockInventory, out removedList))
                        {
                            Session.BetterItemsListPool.Return(removedList);
                        }
                    }
                }
            }
        }