예제 #1
0
        public override void Deserialize(MyObjectBuilder_EntityComponent bbase)
        {
            var ob = (MyObjectBuilder_ConstructableComponent)bbase;

            if (!string.IsNullOrEmpty(ob.SPacked))
            {
                try
                {
                    var stream = new MemStream(Convert.FromBase64String(ob.SPacked));
                    if (_stockpile == null)
                    {
                        _stockpile = new ConstructionStockpile();
                    }
                    _stockpile.Items.Clear();
                    var count = (int)stream.Read7BitEncoded();
                    for (var i = 0; i < count; i++)
                    {
                        _stockpile.AddItem(Controller.Decode((int)stream.Read7BitEncoded()),
                                           (int)stream.Read7BitEncoded());
                    }
                }
                catch (Exception e)
                {
                    this.GetLogger().Warning($"Failed to deserialize packed stockpile data {e}");
                }
            }
            else
            {
                _stockpile?.Clear();
            }

            BuildIntegrity = ob.BInteg;
        }
예제 #2
0
 private void ParseState(bool reset, IReadOnlyCollection <SyncComponentBlit> info)
 {
     if (info.Count > 0)
     {
         if (_stockpile == null)
         {
             _stockpile = new ConstructionStockpile();
         }
         else if (reset)
         {
             _stockpile.Clear();
         }
         foreach (var entry in info)
         {
             if (entry.Count == 0)
             {
                 _stockpile.Items.Remove(entry.Id);
             }
             else
             {
                 _stockpile.Items[entry.Id] = entry.Count;
             }
         }
     }
     else if (reset)
     {
         _stockpile = null;
     }
 }
예제 #3
0
        public void Uninstall <TU>(DelTryRemoveItem <TU> uninstaller, TU userData)
        {
            if (_stockpile == null)
            {
                return;
            }
            using (new StockpileChangeWatcher(this))
            {
                _tmpStockpile.Items.Clear();
                foreach (var k in _stockpile.Items)
                {
                    _tmpStockpile.AddItem(k.Key, k.Value);
                }

                {
                    // remove stockpile items that are in use
                    int componentsRemain = LockedComponents;
                    foreach (var c in Definition.Components)
                    {
                        componentsRemain -=
                            _tmpStockpile.RemoveItemFuzzy(c.Required, Math.Min(c.Count, componentsRemain));
                        if (componentsRemain <= 0)
                        {
                            break;
                        }
                    }

                    Assert.Equals(0, componentsRemain, "Unexpected number of components remaining");
                }

                {
                    // flush remaining items from the stockpile
                    foreach (var item in _tmpStockpile.Items)
                    {
                        var count = item.Value;
                        uninstaller.Invoke(userData, item.Key, ref count);
                        if (count > 0)
                        {
                            Assert.Equals(count, _stockpile.RemoveItem(item.Key, count),
                                          "Removed count not equal to uninstalled count");
                        }
                    }

                    if (_stockpile.IsEmpty())
                    {
                        _stockpile = null;
                    }
                }
                _tmpStockpile.Clear();
            }
        }
예제 #4
0
        public void Install <TU>(SupplyForInstall <TU> supplier, TU userData)
        {
            using (new StockpileChangeWatcher(this))
            {
                _tmpStockpile.Items.Clear();
                if (_stockpile != null)
                {
                    foreach (var k in _stockpile.Items)
                    {
                        _tmpStockpile.AddItem(k.Key, k.Value);
                    }
                }

                foreach (var c in Definition.Components)
                {
                    var req      = (MyDefinitionId)c.Required;
                    var existing = _tmpStockpile.RemoveItemFuzzy(req, c.Count);
                    var remain   = c.Count - existing;
                    if (remain <= 0)
                    {
                        continue;
                    }

                    if (req.TypeId == typeof(MyObjectBuilder_ItemTagDefinition))
                    {
                        var tag = MyDefinitionManager.Get <MyItemTagDefinition>(req);
                        foreach (var subReq in tag.Items)
                        {
                            var removed = remain;
                            supplier.Invoke(userData, subReq.Id, ref removed);
                            if (removed == 0)
                            {
                                continue;
                            }
                            if (_stockpile == null)
                            {
                                _stockpile = new ConstructionStockpile();
                            }
                            _stockpile.AddItem(subReq.Id, removed);
                            remain -= removed;
                            if (remain <= 0)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        var removed = remain;
                        supplier.Invoke(userData, req, ref removed);
                        if (removed > 0)
                        {
                            if (_stockpile == null)
                            {
                                _stockpile = new ConstructionStockpile();
                            }
                            _stockpile.AddItem(req, removed);
                            remain -= removed;
                        }
                    }

                    if (remain > 0)
                    {
                        break;
                    }
                }

                _tmpStockpile.Clear();
            }
        }