public bool IsDefinitionMatch(CubeBlockEntity cubeToCheck, int recurseDepth = 0)
        {
            if (cubeToCheck == null)
            {
                return(false);
            }
            if (cubeToCheck.IsDisposed)
            {
                return(false);
            }
            if (cubeToCheck.Parent == null)
            {
                return(false);
            }
            if (cubeToCheck.Parent.IsDisposed)
            {
                return(false);
            }
            if (cubeToCheck.Parent.IsLoading)
            {
                return(false);
            }
            if (recurseDepth < 0 || recurseDepth > 2)
            {
                return(false);
            }

            bool     isMatch  = false;
            Vector3I cubePos  = cubeToCheck.Min;
            Type     cubeType = cubeToCheck.GetType();

            Dictionary <Vector3I, StructureEntry> structureDef = GetMultiblockDefinition();
            StructureEntry anchorDef = structureDef[Vector3I.Zero];

            //Check if this block isn't the anchor type
            if (cubeType != anchorDef.type)
            {
                //Check if this block is anywhere in the definition
                bool foundMatch = false;
                foreach (StructureEntry entry in structureDef.Values)
                {
                    if (entry.type == cubeType)
                    {
                        foundMatch = true;
                        break;
                    }
                }
                if (!foundMatch)
                {
                    return(false);
                }

                //Recursively search through possible anchor blocks
                foreach (Vector3I key in structureDef.Keys)
                {
                    StructureEntry entry = structureDef[key];

                    if (cubeType == entry.type)
                    {
                        Vector3I        possibleAnchorPos = cubePos - key;
                        CubeBlockEntity posCubeBlock      = cubeToCheck.Parent.GetCubeBlock(possibleAnchorPos);
                        isMatch = IsDefinitionMatch(posCubeBlock, recurseDepth + 1);
                        if (isMatch)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                isMatch = true;
                foreach (Vector3I key in structureDef.Keys)
                {
                    StructureEntry  entry        = structureDef[key];
                    Vector3I        defPos       = cubePos + key;
                    CubeBlockEntity posCubeBlock = cubeToCheck.Parent.GetCubeBlock(defPos);
                    if (posCubeBlock == null)
                    {
                        isMatch = false;
                        break;
                    }

                    //Compare the block type
                    if (!entry.type.IsAssignableFrom(posCubeBlock.GetType()))
                    {
                        isMatch = false;
                        break;
                    }

                    //Compare the block color, if set
                    if (entry.color != null && entry.color.ColorToHSV() != Vector3.Zero)
                    {
                        if (entry.color.ColorToHSV() != (Vector3)posCubeBlock.ColorMaskHSV)
                        {
                            isMatch = false;
                            break;
                        }
                    }

                    //Compare the block orientation, if set
                    if (entry.useOrientation)
                    {
                        if (entry.orientation.Forward != posCubeBlock.BlockOrientation.Forward || entry.orientation.Up != posCubeBlock.BlockOrientation.Up)
                        {
                            isMatch = false;
                            break;
                        }
                    }
                }
                if (isMatch)
                {
                    m_anchor = cubeToCheck;
                }
            }

            if (isMatch && SandboxGameAssemblyWrapper.IsDebugging && recurseDepth == 0)
            {
                //LogManager.APILog.WriteLine("Found multiblock match in cube grid '" + cubeToCheck.Parent.Name + "' anchored at " + ((Vector3I)m_anchor.Min).ToString());
            }

            return(isMatch);
        }
        public static void DoUpdate(object _parameters)
        {
            try
            {
                if (_parameters == null)
                {
                    return;
                }
                PluginManagerThreadParams parameters = (PluginManagerThreadParams)_parameters;

                List <EntityEventManager.EntityEvent> events     = parameters.events;
                List <ChatManager.ChatEvent>          chatEvents = parameters.chatEvents;
                Object plugin = parameters.plugin;
                Dictionary <Guid, Object> plugins     = parameters.plugins;
                Dictionary <Guid, bool>   pluginState = parameters.pluginState;

                //Run entity events
                foreach (EntityEventManager.EntityEvent entityEvent in events)
                {
                    //If this is a cube block created event and the parent cube grid is still loading then defer the event
                    if (entityEvent.type == EntityEventManager.EntityEventType.OnCubeBlockCreated)
                    {
                        CubeBlockEntity cubeBlock = (CubeBlockEntity)entityEvent.entity;
                        if (cubeBlock.Parent.IsLoading)
                        {
                            EntityEventManager.Instance.AddEvent(entityEvent);
                            continue;
                        }
                    }

                    switch (entityEvent.type)
                    {
                    case EntityEventManager.EntityEventType.OnPlayerJoined:
                        try
                        {
                            MethodInfo updateMethod = plugin.GetType().GetMethod("OnPlayerJoined");
                            if (updateMethod != null)
                            {
                                //FIXME - Temporary hack to pass along the player's steam id
                                ulong steamId = (ulong)entityEvent.entity;
                                updateMethod.Invoke(plugin, new object[] { steamId });
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.ErrorLog.WriteLine(ex);
                        }
                        break;

                    case EntityEventManager.EntityEventType.OnPlayerLeft:
                        try
                        {
                            MethodInfo updateMethod = plugin.GetType().GetMethod("OnPlayerLeft");
                            if (updateMethod != null)
                            {
                                //FIXME - Temporary hack to pass along the player's steam id
                                ulong steamId = (ulong)entityEvent.entity;
                                updateMethod.Invoke(plugin, new object[] { steamId });
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.ErrorLog.WriteLine(ex);
                        }
                        break;

                    case EntityEventManager.EntityEventType.OnPlayerWorldSent:
                        try
                        {
                            MethodInfo updateMethod = plugin.GetType().GetMethod("OnPlayerWorldSent");
                            if (updateMethod != null)
                            {
                                //FIXME - Temporary hack to pass along the player's steam id
                                ulong steamId = (ulong)entityEvent.entity;
                                updateMethod.Invoke(plugin, new object[] { steamId });
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.ErrorLog.WriteLine(ex);
                        }
                        break;

                    default:
                        try
                        {
                            string     methodName   = entityEvent.type.ToString();
                            MethodInfo updateMethod = plugin.GetType().GetMethod(methodName);
                            if (updateMethod != null)
                            {
                                updateMethod.Invoke(plugin, new object[] { entityEvent.entity });
                            }
                        }
                        catch (Exception ex)
                        {
                            LogManager.ErrorLog.WriteLine(ex);
                        }
                        break;
                    }
                }

                //Run chat events
                foreach (ChatManager.ChatEvent chatEvent in chatEvents)
                {
                    try
                    {
                        bool discard = false;
                        HookChatMessage(plugin, plugins, pluginState, chatEvent, out discard);

                        if (discard)
                        {
                            continue;
                        }

                        string     methodName   = chatEvent.type.ToString();
                        MethodInfo updateMethod = plugin.GetType().GetMethod(methodName);
                        if (updateMethod != null)
                        {
                            updateMethod.Invoke(plugin, new object[] { chatEvent });
                        }
                    }
                    catch (Exception ex)
                    {
                        LogManager.ErrorLog.WriteLine(ex);
                    }
                }

                //Run update
                try
                {
                    MethodInfo updateMethod = plugin.GetType().GetMethod("Update");
                    updateMethod.Invoke(plugin, new object[] { });
                }
                catch (Exception ex)
                {
                    LogManager.ErrorLog.WriteLine(ex);
                }
            }
            catch (Exception ex)
            {
                LogManager.ErrorLog.WriteLine(ex);
            }
        }
        protected bool RunCubeBlockReflectionTests()
        {
            bool result = true;

            if (!CubeBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("CubeBlockEntity reflection validation failed!");
            }

            if (!TerminalBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("TerminalBlockEntity reflection validation failed!");
            }

            if (!FunctionalBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("FunctionalBlockEntity reflection validation failed!");
            }

            if (!ProductionBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ProductionBlockEntity reflection validation failed!");
            }

            if (!LightEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("LightEntity reflection validation failed!");
            }

            if (!BatteryBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("BatteryBlockEntity reflection validation failed!");
            }

            if (!BatteryBlockNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("BatteryBlockNetworkManager reflection validation failed!");
            }

            if (!DoorEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("DoorEntity reflection validation failed!");
            }

            if (!GravityBaseEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("GravityBaseEntity reflection validation failed!");
            }

            if (!GravityGeneratorEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("GravityGeneratorEntity reflection validation failed!");
            }

            if (!GravitySphereEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("GravitySphereEntity reflection validation failed!");
            }

            if (!BeaconEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("BeaconEntity reflection validation failed!");
            }

            if (!AntennaEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("AntennaEntity reflection validation failed!");
            }

            if (!ThrustEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ThrustEntity reflection validation failed!");
            }

            if (!ThrustNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ThrustNetworkManager reflection validation failed!");
            }

            if (!GyroEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("GyroEntity reflection validation failed!");
            }

            if (!GyroNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("GyroNetworkManager reflection validation failed!");
            }

            if (!CockpitEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("CockpitEntity reflection validation failed!");
            }

            if (!TurretBaseEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("TurretBaseEntity reflection validation failed!");
            }

            if (!TurretNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("TurretNetworkManager reflection validation failed!");
            }

            if (!LandingGearEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("LandingGearEntity reflection validation failed!");
            }

            if (!LandingGearNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("LandingGearNetworkManager reflection validation failed!");
            }

            if (!ReactorEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ReactorEntity reflection validation failed!");
            }

            if (!SolarPanelEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("SolarPanelEntity reflection validation failed!");
            }

            if (!SmallGatlingGunEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("SmallGatlingGunEntity reflection validation failed!");
            }

            if (!MergeBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("MergeBlockEntity reflection validation failed!");
            }

            if (!PistonEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("PistonEntity reflection validation failed!");
            }

            if (!PistonNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("PistonNetworkManager reflection validation failed!");
            }

            if (!RotorEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("RotorEntity reflection validation failed!");
            }

            if (!VirtualMassEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("VirtualMassEntity reflection validation failed!");
            }

            if (!CameraBlockEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("CameraBlockEntity reflection validation failed!");
            }

            if (!OreDetectorEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("OreDetectorEntity reflection validation failed!");
            }

            if (!ButtonPanelEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ButtonPanelEntity reflection validation failed!");
            }

            if (!ShipControllerEntity.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ShipControllerEntity reflection validation failed!");
            }

            if (!ShipControllerNetworkManager.ReflectionUnitTest())
            {
                result = false;
                BaseLog.Warn("ShipControllerNetworkManager reflection validation failed!");
            }

            if (result)
            {
                BaseLog.Info("All block types passed reflection unit tests!");
            }

            return(result);
        }
Ejemplo n.º 4
0
        public void UpdateCubeBlock(CubeGridEntity parent, CubeBlockEntity cubeBlock)
        {
            LogManager.APILog.WriteLineAndConsole("WCF Service - Received cube block entity '" + cubeBlock.Name + "' with id '" + cubeBlock.EntityId + "'");

            foreach (CubeGridEntity cubeGrid in GetSectorCubeGridEntities())
            {
                if (cubeGrid.EntityId == parent.EntityId)
                {
                    //Find the matching block by either the entity id or the position of the block in the grid
                    foreach (CubeBlockEntity baseBlock in cubeGrid.CubeBlocks)
                    {
                        //If entity id is defined but there is a mismatch, skip this block
                        if (baseBlock.EntityId != 0 && baseBlock.EntityId != cubeBlock.EntityId)
                        {
                            continue;
                        }

                        //If entity is is NOT defined but there is a position mismatch, skip this block
                        if (baseBlock.EntityId == 0 && baseBlock.Position != cubeBlock.Position)
                        {
                            continue;
                        }

                        //Copy over the deserialized dummy cube block to the actual cube block
                        Type           type       = baseBlock.GetType();
                        PropertyInfo[] properties = type.GetProperties();
                        foreach (PropertyInfo property in properties)
                        {
                            try
                            {
                                //Check if the property can be publicly set
                                if (!property.CanWrite)
                                {
                                    continue;
                                }
                                if (property.GetSetMethod() == null)
                                {
                                    continue;
                                }

                                //Check if the property has the [DataMember] attribute
                                object[] dataMemberAttributes = property.GetCustomAttributes(typeof(DataMemberAttribute), true);
                                if (dataMemberAttributes == null || dataMemberAttributes.Length == 0)
                                {
                                    continue;
                                }

                                Object newValue = property.GetValue(cubeBlock, new object[] { });
                                if (newValue == null)
                                {
                                    continue;
                                }

                                Object oldValue = property.GetValue(baseBlock, new object[] { });
                                if (newValue.Equals(oldValue))
                                {
                                    continue;
                                }

                                property.SetValue(baseBlock, newValue, new object[] { });
                            }
                            catch (Exception)
                            {
                                //Do nothing
                            }
                        }
                        break;
                    }
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        public void DeleteCubeBlock(CubeBlockEntity cubeBlock)
        {
            _cubeBlockToAddRemove = cubeBlock;

            MySandboxGame.Static.Invoke(InternalRemoveCubeBlock);
        }
        protected bool RunReflectionUnitTests()
        {
            bool result = true;

            if (!BaseObject.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("BaseObject reflection validation failed!");
            }

            if (!BaseEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("BaseEntity reflection validation failed!");
            }

            if (!BaseEntityNetworkManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("BaseEntityNetworkManager reflection validation failed!");
            }

            if (!CubeGridEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("CubeGridEntity reflection validation failed!");
            }

            if (!CubeGridNetworkManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("CubeGridNetworkManager reflection validation failed!");
            }

            if (!CubeBlockEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("CubeBlockEntity reflection validation failed!");
            }

            if (!TerminalBlockEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("TerminalBlockEntity reflection validation failed!");
            }

            if (!FunctionalBlockEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("FunctionalBlockEntity reflection validation failed!");
            }

            if (!SectorObjectManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("SectorObjectManager reflection validation failed!");
            }

            if (!CharacterEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("CharacterEntity reflection validation failed!");
            }

            if (!InventoryEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("InventoryEntity reflection validation failed!");
            }

            if (!InventoryItemEntity.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("InventoryItemEntity reflection validation failed!");
            }

            if (!PlayerMap.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("PlayerMap reflection validation failed!");
            }

            if (!PlayerManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("PlayerManager reflection validation failed!");
            }

            if (!WorldManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("WorldManager reflection validation failed!");
            }

            if (!RadioManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("RadioManager reflection validation failed!");
            }

            if (!RadioManagerNetworkManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("RadioManagerNetworkManager reflection validation failed!");
            }

            if (!PowerManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("PowerManager reflection validation failed!");
            }

            if (!FactionsManager.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("FactionsManager reflection validation failed!");
            }

            if (!Faction.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("Faction reflection validation failed!");
            }

            if (!PowerProducer.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("PowerProducer reflection validation failed!");
            }

            if (!PowerReceiver.ReflectionUnitTest())
            {
                result = false;
                Console.WriteLine("PowerReceiver reflection validation failed!");
            }

            result &= RunCubeBlockReflectionTests();

            if (result)
            {
                Console.WriteLine("All main types passed reflection unit tests!");
            }

            return(result);
        }