Ejemplo n.º 1
0
        /// <inheritdoc />
        public override void Init(MyObjectBuilder_EntityBase objectBuilder)
        {
            base.Init(objectBuilder);

            Stator = Entity as IMyMotorStator;

            if (Stator == null)
            {
                return;
            }

            if (Stator.IsProjected())
            {
                return;
            }

            if (Mod.Static.Network == null || Mod.Static.Network.IsServer)
            {
                if (Entity.Storage == null)
                {
                    Entity.Storage = new MyModStorageComponent();
                }

                try {
                    _settings = Stator.Load <PocketGearBaseSettings>(new Guid(PocketGearBaseSettings.GUID));
                    if (_settings != null)
                    {
                        if (_settings.Version < PocketGearBaseSettings.VERSION)
                        {
                            // todo: merge old and new settings in future versions.
                        }
                    }
                    else
                    {
                        _settings = new PocketGearBaseSettings();
                    }
                } catch (Exception exception) {
                    if (exception.GetType().ToString() == "ProtoBuf.ProtoException")
                    {
                        var old = Stator.Load <Settings.V0.PocketGearBaseSettings>(new Guid(Settings.V0.PocketGearBaseSettings.GUID));
                        if (old != null)
                        {
                            Log.Warning("Old settings version found. Converting to current version.");
                            _settings = new PocketGearBaseSettings {
                                DeployVelocity      = old.DeployVelocity,
                                ShouldDeploy        = old.ShouldDeploy,
                                LockRetractBehavior = old.LockRetractBehavior
                            };
                        }
                        else
                        {
                            Log.Error(exception);
                            _settings = new PocketGearBaseSettings();
                        }
                    }
                    else
                    {
                        Log.Error(exception);
                        _settings = new PocketGearBaseSettings();
                    }
                }

                Stator.LowerLimitRad = FORCED_LOWER_LIMIT_RAD;
                Stator.UpperLimitRad = FORCED_UPPER_LIMIT_RAD;

                NeedsUpdate |= ~MyEntityUpdateEnum.EACH_FRAME;

                // todo: check if it is enough to run this on server.
                if (Mod.Static.DamageHandler != null)
                {
                    Stator.CubeGrid.OnBlockAdded   += OnBlockAdded;
                    Stator.CubeGrid.OnBlockRemoved += OnBlockRemoved;

                    GetNeighbors(Stator.SlimBlock, Mod.Static.Settings.ProtectionRadius, ref _neighbors);
                }

                if (Mod.Static.Network != null)
                {
                    Mod.Static.Network.Register <PlacePadRequestMessage>(Entity.EntityId, OnPlacePadRequestMessageReceived);
                    Mod.Static.Network.Register <DeployRetractRequestMessage>(Entity.EntityId, OnDeployRetractRequestMessageReceived);
                }
            }
            else
            {
                _settings = new PocketGearBaseSettings();
            }

            Stator.AttachedEntityChanged += OnAttachedEntityChanged;

            if (Mod.Static.Network != null)
            {
                Mod.Static.Network.Register <PropertySyncMessage>(Entity.EntityId, OnPropertySyncMessage);
            }

            if (!Mod.Static.Controls.AreTerminalControlsInitialized)
            {
                Mod.Static.Controls.InitializeControls();
            }
        }