Ejemplo n.º 1
0
 public StockpileChangeWatcher(ConstructableComponent c)
 {
     _component = c;
     if (c._stockpile != null && !c._stockpile.IsEmpty())
     {
         PoolManager.Get(out _snapshot);
         _snapshot.Clear();
         foreach (var kv in c._stockpile.Items)
         {
             _snapshot[kv.Key] = kv.Value;
         }
     }
     else
     {
         _snapshot = null;
     }
 }
Ejemplo n.º 2
0
            public void Dispose()
            {
                #region Update Palette

                if (_component._stockpile != null)
                {
                    foreach (var id in _component._stockpile.Items.Keys)
                    {
                        Controller.Encode(id);
                    }
                }

                #endregion

                #region Network

                var alwaysFullSync = false;
                if (_snapshot != null && _snapshot.Count > 0 &&
                    (_component._stockpile == null || _component._stockpile.IsEmpty()))
                {
                    // full resync, clear
                    MyMultiplayerModApi.Static.RaiseEvent(_component, cc => cc.SyncFullStateBcast, SyncComponentBlit.Empty);
                }
                else if (alwaysFullSync || ((_snapshot == null || _snapshot.Count == 0) &&
                                            (_component._stockpile != null && !_component._stockpile.IsEmpty())))
                {
                    // full resync, set
                    MyMultiplayerModApi.Static.RaiseEvent(_component, cc => cc.SyncFullStateBcast,
                                                          _component._stockpile?.Items?.Select(x => (SyncComponentBlit)x).ToArray() ?? SyncComponentBlit.Empty);
                }
                else if (_snapshot != null && _component._stockpile != null)
                {
                    // find changes
                    foreach (var test in _component._stockpile.Items)
                    {
                        int oldVal;
                        if (_snapshot.TryGetValue(test.Key, out oldVal) && oldVal == test.Value)
                        {
                            _snapshot.Remove(test.Key);
                        }
                        else
                        {
                            _snapshot[test.Key] = test.Value;
                        }
                    }

                    var changes = new SyncComponentBlit[_snapshot.Count];
                    var i       = 0;
                    foreach (var k in _snapshot)
                    {
                        changes[i++] = new SyncComponentBlit()
                        {
                            Id = k.Key, Count = _component._stockpile.Items.GetValueOrDefault(k.Key)
                        }
                    }
                    ;
                    MyMultiplayerModApi.Static.RaiseEvent(_component, cc => cc.SyncPartialState, changes,
                                                          _component.ComputeComponentHash());
                }

                #endregion


                _component = null;
                if (_snapshot == null)
                {
                    return;
                }
                _snapshot.Clear();
                PoolManager.Return(_snapshot);
                _snapshot = null;
            }
        }