Exemplo n.º 1
0
        private MyVoxelPhysics CreateVoxelPhysics(ref Vector3I increment, ref Vector3I_RangeIterator it)
        {
            if (m_physicsShapes == null)
            {
                m_physicsShapes = new MyConcurrentDictionary <Vector3I, MyVoxelPhysics>();
            }

            MyVoxelPhysics voxelMap = null;

            if (!m_physicsShapes.TryGetValue(it.Current, out voxelMap))
            {
                Vector3I storageMin = it.Current * increment;
                Vector3I storageMax = storageMin + increment;

                BoundingBox check = new BoundingBox(storageMin, storageMax);

                if (Storage.Intersect(ref check, false) == ContainmentType.Intersects)
                {
                    voxelMap = new MyVoxelPhysics();

                    voxelMap.Init(m_storage,
                                  this.PositionLeftBottomCorner + storageMin * MyVoxelConstants.VOXEL_SIZE_IN_METRES, storageMin,
                                  storageMax, this);
                    voxelMap.Save = false;
                    MyEntities.Add(voxelMap);
                }

                m_physicsShapes.Add(it.Current, voxelMap);
            }
            return(voxelMap);
        }
        public void InitWriteCache(int prealloc = 128)
        {
            //Debug.Assert(m_cachedChunks == null, "Error: Cache already initialized"); disabled due to shared storages

            if (m_cachedChunks != null)
            {
                return;
            }

            if (OperationsComponent != null)
            {
                CachedWrites = true;
            }
            else
            {
                return;
            }

            m_cachedChunks         = new MyConcurrentDictionary <Vector3I, VoxelChunk>(prealloc, Vector3I.Comparer);
            m_pendingChunksToWrite = new MyConcurrentQueue <Vector3I>(prealloc / 10);
            m_chunksbyAge          = new MyQueue <Vector3I>(prealloc);

            m_cacheMap = new MyDynamicAABBTree(Vector3.Zero);

            m_cacheLock = new FastResourceLock();

            OperationsComponent.Add(this);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Pool&lt;T&gt;"/> class.
        /// </summary>
        public Pool()
        {
#if WINDOWS_PHONE
            m_instances = new MyConcurrentDictionary <Thread, MyConcurrentQueue <T> >(1);
#else
            m_instances = new MyConcurrentDictionary <Thread, MyConcurrentQueue <T> >(Environment.ProcessorCount);
#endif
        }
Exemplo n.º 4
0
 public static void AddOrUpdate <T>(this MyConcurrentDictionary <T, float> dic, T key, float value)
 {
     if (dic.ContainsKey(key))
     {
         dic[key] += value;
     }
     else
     {
         dic[key] = value;
     }
 }
Exemplo n.º 5
0
        public void UpdateBlacklist()
        {
            var blacklist  = MyScriptCompiler.Static.Whitelist.OpenIngameBlacklistBatch();
            var types      = new MyConcurrentHashSet <Type>();
            var memberDict = new MyConcurrentDictionary <Type, List <string> >();

            Parallel.ForEach(PluginSettings.Instance.BlacklistItems, item =>
            {
                if (string.IsNullOrEmpty(item.Type))
                {
                    return;
                }

                var targetType = FindTypeInAllAssemblies(item.Type);
                if (targetType == null)
                {
                    return;
                }

                if (string.IsNullOrEmpty(item.Member))
                {
                    lock (types)
                        types.Add(targetType);
                }

                var members = targetType.GetMember(item.Member);
                if (members.Length != 0)
                {
                    if (!memberDict.ContainsKey(targetType))
                    {
                        memberDict.Add(targetType, new List <string>());
                    }
                    memberDict[targetType].Add(item.Member);
                }
            });

            if (types.Count > 0)
            {
                blacklist.AddTypes(types.ToArray());
                foreach (var type in types)
                {
                    Essentials.Log.Info($"Added type {type.Name} to PB blacklist.");
                }
            }
            foreach (var entry in memberDict)
            {
                blacklist.AddMembers(entry.Key, entry.Value.ToArray());
                foreach (var name in entry.Value)
                {
                    Essentials.Log.Info($"Added {entry.Key.Name}.{name} to PB blacklist.");
                }
            }
        }
Exemplo n.º 6
0
        public static void Punish(MyConcurrentDictionary <MySlimBlock, LimitItem.PunishmentType> removalCollection)
        {
            if (removalCollection.Count == 0 || !BlockLimiterConfig.Instance.EnableLimits)
            {
                return;
            }
            var chatManager = BlockLimiter.Instance.Torch.CurrentSession.Managers.GetManager <ChatManagerServer>();

            lock (removalCollection)
            {
                Task.Run(() =>
                {
                    Parallel.ForEach(removalCollection, collective =>
                    {
                        var(block, punishment) = collective;
                        var ownerSteamId       = MySession.Static.Players.TryGetSteamId(block.OwnerId);
                        if (block.IsDestroyed || block.FatBlock.Closed || block.FatBlock.MarkedForClose)
                        {
                            return;
                        }
                        Color color = Color.Yellow;

                        switch (punishment)
                        {
                        case LimitItem.PunishmentType.None:
                            return;

                        case LimitItem.PunishmentType.DeleteBlock:
                            BlockLimiter.Instance.Torch.InvokeAsync(() =>
                            {
                                block.CubeGrid?.RemoveBlock(block);
                            });

                            BlockLimiter.Instance.Log.Info(
                                $"Removed {block.BlockDefinition} from {block.CubeGrid.DisplayName}");
                            break;

                        case LimitItem.PunishmentType.ShutOffBlock:
                            if (!(block.FatBlock is MyFunctionalBlock fBlock))
                            {
                                return;
                            }
                            KillBlock(fBlock);
                            break;
Exemplo n.º 7
0
        public void InitWriteCache(int prealloc = 128)
        {
            //Debug.Assert(m_cachedChunks == null, "Error: Cache already initialized"); disabled due to shared storages

            if (m_cachedChunks != null) return;

            if (OperationsComponent != null)
                CachedWrites = true;
            else
                return;

            m_cachedChunks = new MyConcurrentDictionary<Vector3I, VoxelChunk>(prealloc, Vector3I.Comparer);
            m_pendingChunksToWrite = new MyConcurrentQueue<Vector3I>(prealloc / 10);
            m_chunksbyAge = new MyQueue<Vector3I>(prealloc);

            m_cacheMap = new MyDynamicAABBTree(Vector3.Zero);

            m_cacheLock = new FastResourceLock();

            OperationsComponent.Add(this);
        }
Exemplo n.º 8
0
        public override void Handle()
        {
            if (!BlockLimiterConfig.Instance.EnableLimits)
            {
                return;
            }
            var limitItems = BlockLimiterConfig.Instance.AllLimits;


            if (!limitItems.Any())
            {
                return;
            }


            _blockCache.Clear();
            GridCache.GetBlocks(_blockCache);


            if (!_blockCache.Any())
            {
                return;
            }

            var removeBlocks = new MyConcurrentDictionary <MySlimBlock, LimitItem.PunishmentType>();

            var punishCount = 0;
            var blocks      = _blockCache.ToList();

            foreach (var item in limitItems)
            {
                if (!item.FoundEntities.Any() ||
                    item.Punishment == LimitItem.PunishmentType.None)
                {
                    continue;
                }

                foreach (var(id, count) in item.FoundEntities)
                {
                    if (id == 0 || Utilities.IsExcepted(id, item.Exceptions))
                    {
                        item.FoundEntities.Remove(id);
                        continue;
                    }

                    if (count <= item.Limit)
                    {
                        continue;
                    }

                    for (var i = blocks.Count; i-- > 0;)
                    {
                        var block = blocks[i];
                        if (!Block.IsMatch(block.BlockDefinition, item))
                        {
                            continue;
                        }

                        if (Math.Abs(punishCount - count) <= item.Limit)
                        {
                            break;
                        }

                        if (item.IgnoreNpcs)
                        {
                            if (MySession.Static.Players.IdentityIsNpc(block.FatBlock.BuiltBy) || MySession.Static.Players.IdentityIsNpc(block.FatBlock.OwnerId))

                            {
                                item.FoundEntities.Remove(id);
                                continue;
                            }
                        }

                        if (item.LimitGrids && block.CubeGrid.EntityId == id)
                        {
                            punishCount++;
                            removeBlocks[block] = item.Punishment;
                            continue;
                        }

                        if (item.LimitPlayers)
                        {
                            if (Block.IsOwner(block, id))
                            {
                                punishCount++;
                                removeBlocks[block] = item.Punishment;
                                continue;
                            }
                        }

                        if (!item.LimitFaction)
                        {
                            continue;
                        }
                        var faction = MySession.Static.Factions.TryGetFactionById(id);
                        if (faction == null || !block.FatBlock.GetOwnerFactionTag().Equals(faction.Tag))
                        {
                            continue;
                        }
                        punishCount++;
                        removeBlocks[block] = item.Punishment;
                    }
                }
            }

            _blockCache.Clear();


            if (!removeBlocks.Keys.Any())
            {
                return;
            }

            Task.Run(() =>
            {
                MySandboxGame.Static.Invoke(() =>
                {
                    foreach (var(block, punishment) in removeBlocks)
                    {
                        try
                        {
                            switch (punishment)
                            {
                            case LimitItem.PunishmentType.DeleteBlock:
                                if (BlockLimiterConfig.Instance.EnableLog)
                                {
                                    Log.Info(
                                        $"removed {block.BlockDefinition.BlockPairName} from {block.CubeGrid.DisplayName}");
                                }
                                block.CubeGrid.RemoveBlock(block);
                                continue;

                            case LimitItem.PunishmentType.ShutOffBlock:
                                if (!(block.FatBlock is MyFunctionalBlock funcBlock) || funcBlock.Enabled == false)
                                {
                                    continue;
                                }
                                if (BlockLimiterConfig.Instance.EnableLog)
                                {
                                    Log.Info(
                                        $"Turned off {block.BlockDefinition.BlockPairName} from {block.CubeGrid.DisplayName}");
                                }
                                funcBlock.Enabled = false;
                                continue;

                            case LimitItem.PunishmentType.Explode:
                                if (BlockLimiterConfig.Instance.EnableLog)
                                {
                                    Log.Info(
                                        $"Destroyed {block.BlockDefinition.BlockPairName} from {block.CubeGrid.DisplayName}");
                                }
                                block.DoDamage(block.BlockDefinition.MaxIntegrity * 10, MyDamageType.Explosion);
                                continue;

                            default:
                                throw new ArgumentOutOfRangeException();
                            }
                        }
                        catch (Exception e)
                        {
                            if (BlockLimiterConfig.Instance.EnableLog)
                            {
                                Log.Error(e);
                            }
                        }
                    }
                }, "BlockLimiter");
            });
        }
Exemplo n.º 9
0
        public static int RunPunishment(HashSet <MySlimBlock> blocks, List <LimitItem.PunishmentType> punishmentTypes = null)
        {
            var totalBlocksPunished = 0;

            if (!blocks.Any() || !BlockLimiterConfig.Instance.EnableLimits)
            {
                return(0);
            }

            var limitItems = BlockLimiterConfig.Instance.AllLimits;

            BlockSwitchPatch.KeepOffBlocks.Clear();

            if (limitItems.Count == 0)
            {
                return(0);
            }

            var punishBlocks = new MyConcurrentDictionary <MySlimBlock, LimitItem.PunishmentType>();

            var punishCount = 0;

            foreach (var item in limitItems.Where(item => item.FoundEntities.Count > 0 && item.Punishment != LimitItem.PunishmentType.None))
            {
                if (punishmentTypes != null && !punishmentTypes.Contains(item.Punishment))
                {
                    continue;
                }
                var idsToRemove = new HashSet <long>();

                foreach (var(id, count) in item.FoundEntities)
                {
                    if (id == 0 || Utilities.IsExcepted(id, item))
                    {
                        idsToRemove.Add(id);
                        continue;
                    }

                    if (count <= item.Limit)
                    {
                        continue;
                    }
                    foreach (var block in blocks)
                    {
                        if (block?.BuiltBy == null || block.CubeGrid.IsPreview)
                        {
                            continue;
                        }
                        if (!item.IsMatch(block.BlockDefinition))
                        {
                            continue;
                        }

                        var defBase = MyDefinitionManager.Static.GetDefinition(block.BlockDefinition.Id);

                        if (defBase != null && !_firstCheckCompleted && !defBase.Context.IsBaseGame)
                        {
                            continue;
                        }

                        if (Math.Abs(punishCount - count) <= item.Limit)
                        {
                            break;
                        }

                        if (item.IgnoreNpcs)
                        {
                            if (MySession.Static.Players.IdentityIsNpc(block.FatBlock.BuiltBy) || MySession.Static.Players.IdentityIsNpc(block.FatBlock.OwnerId))

                            {
                                idsToRemove.Add(id);
                                continue;
                            }
                        }

                        if (item.Punishment == LimitItem.PunishmentType.ShutOffBlock &&
                            block.FatBlock is MyFunctionalBlock fBlock && (!fBlock.Enabled || block.FatBlock.MarkedForClose || block.FatBlock.Closed))
                        {
                            punishCount++;
                            continue;
                        }

                        if (item.LimitGrids && block.CubeGrid.EntityId == id)
                        {
                            punishCount++;
                            punishBlocks[block] = item.Punishment;
                            continue;
                        }

                        if (item.LimitPlayers)
                        {
                            if (Block.IsOwner(block, id))
                            {
                                punishCount++;
                                punishBlocks[block] = item.Punishment;
                                continue;
                            }
                        }

                        if (!item.LimitFaction)
                        {
                            continue;
                        }
                        var faction = MySession.Static.Factions.TryGetFactionById(id);
                        if (faction == null || block.FatBlock.GetOwnerFactionTag()?.Equals(faction.Tag) == false)
                        {
                            continue;
                        }
                        punishCount++;
                        punishBlocks[block] = item.Punishment;
                    }
                }


                idsToRemove.ForEach(x => item.FoundEntities.Remove(x));
            }

            if (punishBlocks.Count == 0)
            {
                return(totalBlocksPunished);
            }

            totalBlocksPunished  = punishBlocks.Count;
            _firstCheckCompleted = !_firstCheckCompleted;
            Log.Info($"Punishing {totalBlocksPunished} blocks");
            Block.Punish(punishBlocks);
            return(totalBlocksPunished);
        }
Exemplo n.º 10
0
        public static int RunPunishment(HashSet <MySlimBlock> blocks, List <LimitItem.PunishmentType> punishmentTypes = null)
        {
            var totalBlocksPunished = 0;

            if (blocks.Count == 0 || !BlockLimiterConfig.Instance.EnableLimits)
            {
                return(0);
            }

            var limitItems = BlockLimiterConfig.Instance.AllLimits.Where(item => item.FoundEntities.Count > 0 && item.Punishment != LimitItem.PunishmentType.None).ToList();

            if (limitItems.Count == 0)
            {
                return(0);
            }

            var punishBlocks = new MyConcurrentDictionary <MySlimBlock, LimitItem.PunishmentType>();

            for (var i = limitItems.Count - 1; i >= 0; i--)
            {
                var item = limitItems[i];
                if (punishmentTypes != null && !punishmentTypes.Contains(item.Punishment))
                {
                    continue;
                }
                var idsToRemove = new HashSet <long>();
                var punishCount = 0;
                foreach (var(id, count) in item.FoundEntities)
                {
                    if (id == 0 || item.IsExcepted(id))
                    {
                        idsToRemove.Add(id);
                        continue;
                    }

                    if (count <= item.Limit)
                    {
                        continue;
                    }


                    foreach (var block in blocks)
                    {
                        if (block.CubeGrid.IsPreview || !item.IsMatch(block.BlockDefinition))
                        {
                            continue;
                        }


                        var defBase = MyDefinitionManager.Static.GetDefinition(block.BlockDefinition.Id);

                        if (defBase != null && !_firstCheckCompleted && !defBase.Context.IsBaseGame)
                        {
                            continue;
                        }

                        if (Math.Abs(punishCount - count) <= item.Limit)
                        {
                            break;
                        }

                        if (item.IgnoreNpcs)
                        {
                            if (MySession.Static.Players.IdentityIsNpc(block.FatBlock.BuiltBy) ||
                                MySession.Static.Players.IdentityIsNpc(block.FatBlock.OwnerId))

                            {
                                idsToRemove.Add(id);
                                continue;
                            }
                        }

                        //Reverting to old shutoff due to performance issues
                        if (item.Punishment == LimitItem.PunishmentType.ShutOffBlock &&
                            block.FatBlock is MyFunctionalBlock fBlock && (!fBlock.Enabled ||
                                                                           block.FatBlock.MarkedForClose ||
                                                                           block.FatBlock.Closed))
                        {
                            punishCount++;
                            continue;
                        }

                        //Todo Fix this function and re-implement. Currently too expensive

                        /*
                         * if (item.Punishment == LimitItem.PunishmentType.ShutOffBlock && Math.Abs(GetDisabledBlocks(id,item) - count) <= item.Limit )
                         * {
                         *  continue;
                         * }
                         */
                        var playerSteamId = MySession.Static.Players.TryGetSteamId(id);

                        if (playerSteamId > 0 && !Annoy.AnnoyQueue.ContainsKey(playerSteamId))
                        {
                            Annoy.AnnoyQueue[playerSteamId] = DateTime.Now;
                            break;
                        }

                        if (item.LimitGrids && block.CubeGrid.EntityId == id)
                        {
                            punishCount++;
                            punishBlocks[block] = item.Punishment;
                            continue;
                        }

                        if (item.LimitPlayers)
                        {
                            if (Block.IsOwner(block, id))
                            {
                                punishCount++;
                                punishBlocks[block] = item.Punishment;
                                continue;
                            }
                        }

                        if (!item.LimitFaction)
                        {
                            continue;
                        }
                        var faction = MySession.Static.Factions.TryGetFactionById(id);
                        if (faction == null || block.FatBlock.GetOwnerFactionTag()?.Equals(faction.Tag) == false)
                        {
                            continue;
                        }
                        punishCount++;
                        punishBlocks[block] = item.Punishment;
                    }
                }
            }

            totalBlocksPunished  = punishBlocks.Count;
            _firstCheckCompleted = !_firstCheckCompleted;
            if (totalBlocksPunished == 0)
            {
                return(totalBlocksPunished);
            }
            Log.Debug($"Punishing {punishBlocks.Count} blocks");
            Block.Punish(punishBlocks);

            /*
             * List<MySlimBlock> GetDisabledBlocks(long id, LimitItem limit)
             * {
             *  var disabledBlocks = new List<MySlimBlock>();
             *  foreach (var block in blocks)
             *  {
             *      if (!(block.FatBlock is MyFunctionalBlock fBlock) || block.FatBlock.MarkedForClose || block.FatBlock.Closed) continue;
             *      if (block.CubeGrid.EntityId != id && !Block.IsOwner(block,id)) continue;
             *      if (BlockSwitchPatch.KeepOffBlocks.Contains(block.FatBlock))
             *      {
             *          disabledBlocks.Add(block);
             *          continue;
             *      }
             *      if (fBlock.Enabled)continue;
             *      disabledBlocks.Add(block);
             *  }
             *
             *  return disabledBlocks;
             * }
             *
             * int GetDisabledCount (long id, LimitItem limit)
             * {
             *  var disabledCount = 0;
             *  foreach (var block in blocks)
             *  {
             *      if (!limit.IsGridType(block.CubeGrid)) continue;
             *      if (!limit.IsMatch(block.BlockDefinition)) continue;
             *      if (!(block.FatBlock is MyFunctionalBlock fBlock) || block.FatBlock.MarkedForClose || block.FatBlock.Closed) continue;
             *      if (block.CubeGrid.EntityId != id && !Block.IsOwner(block,id)) continue;
             *      if (BlockSwitchPatch.KeepOffBlocks.Contains(block.FatBlock))
             *      {
             *          disabledCount++;
             *          continue;
             *      }
             *      if (fBlock.Enabled)continue;
             *      disabledCount++;
             *  }
             *  return disabledCount;
             * }
             */
            return(totalBlocksPunished);
        }
Exemplo n.º 11
0
        public static void Punish(MyConcurrentDictionary <MySlimBlock, LimitItem.PunishmentType> removalCollection)
        {
            if (removalCollection.Count == 0)
            {
                return;
            }
            var log = BlockLimiter.Instance.Log;

            if (!BlockLimiterConfig.Instance.EnableLimits)
            {
                return;
            }
            var chatManager = BlockLimiter.Instance.Torch.CurrentSession.Managers.GetManager <ChatManagerServer>();

            lock (removalCollection)
            {
                Task.Run(() =>
                {
                    Parallel.ForEach(removalCollection, collective =>
                    {
                        var(block, punishment) = collective;
                        var ownerSteamId       = MySession.Static.Players.TryGetSteamId(block.OwnerId);
                        if (block.IsDestroyed || block.FatBlock.Closed || block.FatBlock.MarkedForClose)
                        {
                            return;
                        }
                        Color color = Color.Yellow;

                        switch (punishment)
                        {
                        case LimitItem.PunishmentType.None:
                            return;

                        case LimitItem.PunishmentType.DeleteBlock:
                            MySandboxGame.Static.Invoke(() =>
                            {
                                block.CubeGrid.RemoveBlock(block);
                            }, "BlockLimiter");
                            log.Info(
                                $"Removed {block.BlockDefinition} from {block.CubeGrid.DisplayName}");
                            break;

                        case LimitItem.PunishmentType.ShutOffBlock:
                            KillBlock(block.FatBlock);
                            break;

                        case LimitItem.PunishmentType.Explode:
                            log.Info(
                                $"Destroyed {block.BlockDefinition} from {block.CubeGrid.DisplayName}");
                            MySandboxGame.Static.Invoke(() =>
                            {
                                block.DoDamage(block.BlockDefinition.MaxIntegrity, MyDamageType.Fire);
                            }, "BlockLimiter");
                            break;

                        default:
                            return;
                        }

                        if (ownerSteamId != 0 && MySession.Static.Players.IsPlayerOnline(block.OwnerId))
                        {
                            chatManager?.SendMessageAsOther(BlockLimiterConfig.Instance.ServerName, $"Punishing {((MyTerminalBlock)block.FatBlock).CustomName} from {block.CubeGrid.DisplayName} with {punishment}", color, ownerSteamId);
                        }
                    });
                });
            }
        }