Ejemplo n.º 1
0
        public void CreateMultipleSame()
        {
            // preparation
            var dic = Dictionary;

            dic.Clear();
            Assert.AreEqual(0, dic.Count);

            var lockItems = new List <KeyLock>();

            // action
            for (var i = 1; i < 256; i++)
            {
                // create
                var l = new KeyLock($"UUID_{i}");
                lockItems.Add(l);
                // Assert
                Assert.AreEqual(i, dic.Count);
            }
            // Assert
            Assert.AreEqual(255, dic.Count);

            foreach (var lockItem in lockItems.ToArray())
            {
                // dispose
                lockItem.Dispose();
                lockItems.Remove(lockItem);
                // Assert
                Assert.AreEqual(lockItems.Count, dic.Count);
            }

            // Assert
            Assert.AreEqual(0, dic.Count);
        }
Ejemplo n.º 2
0
 private bool CanOpenKeyLock(KeyLock door, BasePlayer player)
 {
     return(door.HasLockPermission(player) ||
            (player.CanBuild() &&
             checker.IsPlayerAuthorized() &&
             (handler.IsInClan(player.UserIDString, door.OwnerID.ToString()) || handler.IsFriend(player.UserIDString, door.OwnerID.ToString()))));
 }
    public T ExecuteSynchronized <T>(TKey key, Func <TKey, T> function)
    {
        KeyLock keyLock = null;

        try
        {
            lock (keyLocks)
            {
                if (!keyLocks.TryGetValue(key, out keyLock))
                {
                    keyLock = new KeyLock();
                    keyLocks.Add(key, keyLock);
                }
                keyLock.Count++;
            }
            lock (keyLock)
            {
                return(function(key));
            }
        }
        finally
        {
            lock (keyLocks)
            {
                if (keyLock != null && --keyLock.Count == 0)
                {
                    keyLocks.Remove(key);
                }
            }
        }
    }
Ejemplo n.º 4
0
        private static void DemonstrateInheritance()
        {
            int[]         keySequence = { 5, 7, 2, 3, 5 };
            List <KeyPin> key         = new List <KeyPin>();

            key.Add(new KeyPin(5));
            key.Add(new KeyPin(7));
            key.Add(new KeyPin(2));
            key.Add(new KeyPin(3));
            key.Add(new KeyPin(5));

            AbstractLock myLock = new KeyLock(key);

            myLock.Lock();
            myLock.Unlock(keySequence);
            if (myLock.IsLocked)
            {
                Console.WriteLine("Hmm. It should be unlocked...");
            }
            else
            {
                Console.WriteLine("Good! The key worked for my new lock");
            }

            myLock.Lock();
            int tries = PickLock(myLock, 5);

            Console.WriteLine("The computer picked the lock after " + tries.ToString() + " tries for sequence: ");
            DisplayKeySequence(keySequence);
        }
Ejemplo n.º 5
0
            private bool CanOpenKeyLock(KeyLock door, BasePlayer player)
            {
                bool canUse = false;

                canUse = door.HasLockPermission(player) || (player.CanBuild() && checker.IsPlayerAuthorized());

                return(canUse);
            }
Ejemplo n.º 6
0
        public void CreateMethod(string uuid)
        {
            // preparation
            var dic = Dictionary;

            dic.Clear();
            // action
            using (var l = new KeyLock(uuid)) { }
        }
Ejemplo n.º 7
0
        object CanUseDoor(BasePlayer player, BaseLock lockItem)
        {
            if (!keyring)
            {
                return(null);
            }

            if (MasterKey != null)
            {
                var result = MasterKey.Call("CanUseDoor", player, lockItem);
                if (result is bool)
                {
                    return(null);
                }
            }
            if (lockItem is KeyLock && banks.ContainsKey(player.userID))
            {
                KeyLock keyLock = (KeyLock)lockItem;

                BankProfile bank = banks[player.userID];

                List <int> codes = new List <int>();
                foreach (ItemProfile profile in bank.items)
                {
                    if (profile.dataInt != 0)
                    {
                        codes.Add(profile.dataInt);
                    }
                }

                if (!keyLock.IsLocked())
                {
                    return(null);
                }

                if (keyLock.HasLockPermission(player))
                {
                    return(null);
                }

                int keyCode = (int)keyCodeField.GetValue(keyLock);

                foreach (int code in codes)
                {
                    if (code == keyCode)
                    {
                        return(true);
                    }
                }

                return(false);
            }

            return(null);
        }
Ejemplo n.º 8
0
        object CanUseLockedEntity(BasePlayer player, BaseLock lockItem)
        {
            if (!keyring)
            {
                return(null);
            }

            if (MasterKey != null)
            {
                var result = MasterKey.Call("CanUseLockedEntity", player, lockItem);
                if (result is bool)
                {
                    return(null);
                }
            }

            BankProfile bank;

            if (lockItem is KeyLock && banks.TryGetValue(player.userID, out bank))
            {
                KeyLock keyLock = (KeyLock)lockItem;

                List <int> codes = bank.items.Select(profile => profile.dataInt).Where(dataInt => dataInt != 0).ToList();
                //foreach(ItemProfile profile in bank.items) {
                //    if(profile.dataInt != 0) {
                //        codes.Add(profile.dataInt);
                //    }
                //}

                if (!keyLock.IsLocked())
                {
                    return(null);
                }

                if (keyLock.HasLockPermission(player))
                {
                    return(null);
                }

                int keyCode = (int)keyCodeField.GetValue(keyLock);

                foreach (int code in codes)
                {
                    if (code == keyCode)
                    {
                        return(true);
                    }
                }

                return(false);
            }

            return(null);
        }
Ejemplo n.º 9
0
        public void CreateMethodThrowException()
        {
            // preparation
            var dic = Dictionary;

            dic.Clear();
            // action
            Assert.ThrowsException <ArgumentNullException>(() =>
            {
                using (var l = new KeyLock(null)) { }
            });
        }
Ejemplo n.º 10
0
 private void OfferToPool(TKey key)
 {
     KeyLock.AcquireReaderLock(new TimeSpan(-1));
     try
     {
         Keys[key].Offer(new TimeRecord <TItem>(Factory.MakeItem(key)));
     }
     finally
     {
         KeyLock.ReleaseReaderLock();
     }
 }
Ejemplo n.º 11
0
 public Log(
     CommandContext context,
     IConsole console,
     KeyLock <ILog> logLock)
 {
     _context = context
                ?? throw new ArgumentNullException(nameof(context));
     _console = console
                ?? throw new ArgumentNullException(nameof(console));
     _logLock = logLock
                ?? throw new ArgumentNullException(nameof(logLock));
 }
Ejemplo n.º 12
0
        public TokenKeyLock()
        {
            _command     = "Key Lock";
            _description = "Prevent user from inputing key commands";
            _length      = 2;

            Value = (KeyLock)Tokenizer.ReadUInt8(1); if (!Enum.IsDefined(typeof(KeyLock), Value))
            {
                throw new ArgumentOutOfRangeException();
            }

            UpdateData();
        }
Ejemplo n.º 13
0
 public static string KeyTypeDescription(KeyLock keyLock)
 {
     string result = string.Empty;
     switch (keyLock.KeyLockTypeEnum)
     {
         case KeyLockTypeEnum.AccessionOrder:
             result = "Accession Order";
             break;
         case KeyLockTypeEnum.SpecimenLog:
             result = "Specimen Log";
             break;
     }
     return result;
 }
Ejemplo n.º 14
0
        public IDisposable Lock(TKey key)
        {
            KeyLock keyLock = null;

            lock (_dictionaryLock)
            {
                if (!_locks.TryGetValue(key, out keyLock))
                {
                    keyLock     = new KeyLock();
                    _locks[key] = keyLock;
                }
                keyLock.Increment();
            }
            Monitor.Enter(keyLock.Token);
            return(new DisposableAction(() => Unlock(key)));
        }
        public async Task <ulong> GetNextAsync(string key, ulong startNo)
        {
            _logger.LogInformation($"start {nameof(GetNextAsync)}");
            var pk = new PartitionKey(key);

            using (var l = new KeyLock(key))
            {
                for (var i = 0; i < 100; i++)
                {
                    try
                    {
                        var r = await _db.Sequence.ReadItemAsync <SequenceModel>(key, pk);

                        r.Resource.value++;
                        var opt = new RequestOptions();
                        opt.IfMatchEtag = r.ETag;
                        var rReplece = await _db.Sequence.ReplaceItemAsync(r.Resource, key, pk);

                        return(r.Resource.value);
                    }
                    catch (CosmosException ex)
                    {
                        if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                        {
                            var resultCreate = await _db.Sequence.CreateItemAsync(new SequenceModel()
                            {
                                id           = key,
                                PartitionKey = key,
                                value        = startNo
                            }, pk);

                            return(startNo);
                        }
                        _logger.LogInformation(ex, $"GetNextAsync Retry {i}");
                        await Task.Delay(100);

                        continue;
                    }
                }
            }
            _logger.LogWarning("GetNextAsync is over retry count.");
            throw new ApplicationException("GetNextAsync is over retry count.");
        }
Ejemplo n.º 16
0
        public T ExecuteSynchronized <T>(TKey key, Func <TKey, T> function)
        {
            if (function == null)
            {
                throw new ArgumentNullException(nameof(function));
            }
            KeyLock keyLock = null;

            try
            {
                lock (keyLocks)
                {
                    try
                    { }
                    finally
                    {
                        if (!keyLocks.TryGetValue(key, out keyLock))
                        {
                            keyLock = new KeyLock();
                            keyLocks.Add(key, keyLock);
                        }
                        keyLock.Count++;
                    }
                }
                lock (keyLock)
                {
                    return(function(key));
                }
            }
            finally
            {
                lock (keyLocks)
                {
                    if (keyLock != null && --keyLock.Count == 0)
                    {
                        keyLocks.Remove(key);
                    }
                }
            }
        }
 public StructureComponent(BuildingPart bp, SerializedVector3 v3, SerializedQuaternion q)
 {
     Grade         = bp.buildingBlock.grade;
     Prefab        = bp.buildingBlock.LookupPrefabName();
     LocalPosition = v3;
     LocalRotation = q;
     Health        = (float)((int)Math.Floor((double)(bp.Health / 85)) * 85);
     if (bp.buildingBlock.HasSlot(BaseEntity.Slot.Lock))
     {
         var baseLock = bp.buildingBlock.GetSlot(BaseEntity.Slot.Lock) as BaseLock;
         if (baseLock == null)
         {
             HasCodeLock = false;
             HasKeyLock  = false;
         }
         else if (baseLock.GetComponent <CodeLock>())
         {
             HasCodeLock = true;
             HasKeyLock  = false;
             CodeLock codeLock = baseLock.GetComponent <CodeLock>();
             if (!string.IsNullOrEmpty((string)codeLock.GetFieldValue("code")))
             {
                 LockCode  = (string)codeLock.GetFieldValue("code");
                 LockWList = new List <ulong>();
                 LockWList = (List <ulong>)codeLock.GetFieldValue("whitelistPlayers");
             }
         }
         else if (baseLock.GetComponent <KeyLock>())
         {
             HasCodeLock = false;
             HasKeyLock  = true;
             KeyLock keyLock = baseLock.GetComponent <KeyLock>();
             int     keyCode = (int)keyLock.GetFieldValue("keyCode");
             keyCode  = (bool)keyLock.GetFieldValue("firstKeyCreated") ? keyCode |= 0x80 : (int)keyLock.GetFieldValue("keyCode");
             LockCode = keyCode.ToString();
         }
     }
 }
Ejemplo n.º 18
0
        public void CreateMultiple()
        {
            // preparation
            var dic = Dictionary;

            dic.Clear();
            Assert.AreEqual(0, dic.Count);

            // action
            for (var i = 0; i < 256; i++)
            {
                // create
                using (var l = new KeyLock($"UUID_{i}"))
                {
                    // Assert
                    Assert.AreEqual(1, dic.Count);
                }
                // Assert
                Assert.AreEqual(0, dic.Count);
            }
            // Assert
            Assert.AreEqual(0, dic.Count);
        }
Ejemplo n.º 19
0
            public bool CanOpen()
            {
                bool canUse = false;

                if (BaseDoor.IsLocked())
                {
                    if (BaseDoor is CodeLock)
                    {
                        CodeLock codeLock = (CodeLock)BaseDoor;
                        canUse = CanOpenCodeLock(codeLock, Player);
                    }
                    else if (BaseDoor is KeyLock)
                    {
                        KeyLock keyLock = (KeyLock)BaseDoor;
                        canUse = CanOpenKeyLock(keyLock, Player);
                    }
                }
                else
                {
                    canUse = true;
                }
                return(canUse);
            }
Ejemplo n.º 20
0
 public CleanupHandler(ILog log, KeyLock <ILog> logLock)
 {
     _log     = log ?? throw new ArgumentNullException(nameof(log));
     _logLock = logLock
                ?? throw new ArgumentNullException(nameof(logLock));
 }
Ejemplo n.º 21
0
 private void Start()
 {
     m_keyLock   = FindObjectOfType <KeyLock>();
     m_col       = GetComponent <Collider2D>();
     m_pairedKey = m_keyLock.findPairedKey(this);
 }
Ejemplo n.º 22
0
 public DeployableComponent(Deployable deployable, SerializedVector3 v3, SerializedQuaternion q)
 {
     Prefab        = deployable.GetComponent <BaseNetworkable>().LookupPrefabName();
     LocalPosition = v3;
     LocalRotation = q;
     if (deployable.GetComponent <SleepingBag>())
     {
         SleepingBag sleepingBag = deployable.GetComponent <SleepingBag>();
         DeployedBy  = sleepingBag.deployerUserID;
         BagName     = sleepingBag.niceName;
         HasOwner    = true;
         HasStorage  = false;
         HasPainting = false;
         IsCupBoard  = false;
     }
     else if (deployable.GetComponent <BuildingPrivlidge>())
     {
         IsCupBoard = true;
         BuildingPrivlidge buildingPrivlidge = deployable.GetComponent <BuildingPrivlidge>();
         AuthedPlayers = new List <ProtoBuf.PlayerNameID>();
         AuthedPlayers = buildingPrivlidge.authorizedPlayers;
     }
     else if (deployable.GetComponent <StorageContainer>())
     {
         HasOwner    = false;
         HasStorage  = true;
         HasPainting = false;
         IsCupBoard  = false;
         StorageContainer storageContainer = deployable.GetComponent <StorageContainer>();
         ItemList = new List <Dictionary <string, object> >();
         foreach (Item item in storageContainer.inventory.itemList)
         {
             var itemData = new Dictionary <string, object>();
             itemData.Add("blueprint", item.IsBlueprint());
             itemData.Add("id", item.info.itemid);
             itemData.Add("amount", item.amount);
             ItemList.Add(itemData);
         }
         if (storageContainer.HasSlot(BaseEntity.Slot.Lock))
         {
             var baseLock = storageContainer.GetSlot(BaseEntity.Slot.Lock) as BaseLock;
             if (baseLock == null)
             {
                 HasCodeLock = false;
                 HasKeyLock  = false;
             }
             else if (baseLock.GetComponent <CodeLock>())
             {
                 HasCodeLock = true;
                 HasKeyLock  = false;
                 CodeLock codeLock = baseLock.GetComponent <CodeLock>();
                 if (!string.IsNullOrEmpty((string)codeLock.GetFieldValue("code")))
                 {
                     LockCode  = (string)codeLock.GetFieldValue("code");
                     LockWList = new List <ulong>();
                     LockWList = (List <ulong>)codeLock.GetFieldValue("whitelistPlayers");
                 }
             }
             else if (baseLock.GetComponent <KeyLock>())
             {
                 HasCodeLock = false;
                 HasKeyLock  = true;
                 KeyLock keyLock = baseLock.GetComponent <KeyLock>();
                 int     keyCode = (int)keyLock.GetFieldValue("keyCode");
                 keyCode  = (bool)keyLock.GetFieldValue("firstKeyCreated") ? keyCode |= 0x80 : (int)keyLock.GetFieldValue("keyCode");
                 LockCode = keyCode.ToString();
             }
         }
     }
     else if (deployable.GetComponent <Signage>())
     {
         HasOwner    = false;
         HasStorage  = false;
         HasPainting = true;
         IsCupBoard  = false;
         Signage signage = deployable.GetComponent <Signage>();
         byte[]  tempImg = FileStorage.server.Get(signage.textureID, FileStorage.Type.png, signage.net.ID);
         if (signage.textureID > 0 && tempImg != null)
         {
             Painting = tempImg;
         }
         PaintingLocked = signage.IsLocked();
     }
     else
     {
         HasOwner    = false;
         HasStorage  = false;
         HasPainting = false;
         IsCupBoard  = false;
     }
 }
Ejemplo n.º 23
0
 public void Build(Vector3 spawnAt)
 {
     foreach (StructureComponent component in StructureComponents.Values)
     {
         Vector3    v3  = (component.LocalPosition.ToVector3() + spawnAt);
         BaseEntity ent = GameManager.server.CreateEntity(component.Prefab, v3, component.LocalRotation.ToQuaternion());
         ent.SpawnAsMapEntity();
         BuildingBlock bb = ent.GetComponent <BuildingBlock>();
         bb.blockDefinition = PrefabAttribute.server.Find <Construction>(bb.prefabID);
         bb.grade           = component.Grade;
         bb.health          = component.Health;
         if (bb.HasSlot(BaseEntity.Slot.Lock))
         {
             if (component.HasCodeLock)
             {
                 BaseEntity baseEntity = GameManager.server.CreateEntity("build/locks/lock.code", Vector3.zero, new Quaternion());
                 baseEntity.OnDeployed(bb);
                 if (!string.IsNullOrEmpty(component.LockCode))
                 {
                     CodeLock codeLock = baseEntity.GetComponent <CodeLock>();
                     codeLock.SetFlag(BaseEntity.Flags.Locked, true);
                     codeLock.SetFieldValue("code", component.LockCode);
                     codeLock.SetFieldValue("whitelistPlayers", component.LockWList);
                 }
                 baseEntity.gameObject.Identity();
                 baseEntity.SetParent(bb, "lock");
                 baseEntity.Spawn();
                 bb.SetSlot(BaseEntity.Slot.Lock, baseEntity);
             }
             else if (component.HasKeyLock)
             {
                 BaseEntity baseEntity = GameManager.server.CreateEntity("build/locks/lock.key", Vector3.zero, new Quaternion());
                 baseEntity.OnDeployed(bb);
                 int code = int.Parse(component.LockCode);
                 if ((code & 0x80) != 0)
                 {
                     KeyLock keyLock = baseEntity.GetComponent <KeyLock>();
                     keyLock.SetFieldValue("keycode", (code & 0x7F));
                     keyLock.SetFieldValue("firstKeyCreated", true);
                     keyLock.SetFlag(BaseEntity.Flags.Locked, true);
                 }
                 baseEntity.gameObject.Identity();
                 baseEntity.SetParent(bb, "lock");
                 baseEntity.Spawn();
                 bb.SetSlot(BaseEntity.Slot.Lock, baseEntity);
             }
         }
         bb.SendNetworkUpdateImmediate();
     }
     foreach (DeployableComponent component in DeployableComponents.Values)
     {
         Vector3    v3  = (component.LocalPosition.ToVector3() + spawnAt);
         BaseEntity ent = GameManager.server.CreateEntity(component.Prefab, v3, component.LocalRotation.ToQuaternion());
         ent.SpawnAsMapEntity();
         if (component.HasOwner)
         {
             SleepingBag sleepingBag = ent.GetComponent <SleepingBag>();
             sleepingBag.deployerUserID = component.DeployedBy;
             sleepingBag.niceName       = component.BagName;
         }
         else if (component.IsCupBoard)
         {
             BuildingPrivlidge buildingPrivlidge = ent.GetComponent <BuildingPrivlidge>();
             buildingPrivlidge.authorizedPlayers = component.AuthedPlayers;
         }
         else if (component.HasStorage)
         {
             StorageContainer storageContainer = ent.GetComponent <StorageContainer>();
             var items = component.ItemList;
             foreach (var item in items)
             {
                 Item newItem = ItemManager.CreateByItemID((int)item["id"], (int)item["amount"], (bool)item["blueprint"]);
                 newItem.MoveToContainer(storageContainer.inventory);
             }
             if (ent.HasSlot(BaseEntity.Slot.Lock))
             {
                 if (component.HasCodeLock)
                 {
                     BaseEntity baseEntity = GameManager.server.CreateEntity("build/locks/lock.code", Vector3.zero, new Quaternion());
                     baseEntity.OnDeployed(ent);
                     if (!string.IsNullOrEmpty(component.LockCode))
                     {
                         CodeLock codeLock = baseEntity.GetComponent <CodeLock>();
                         codeLock.SetFlag(BaseEntity.Flags.Locked, true);
                         codeLock.SetFieldValue("code", component.LockCode);
                         codeLock.SetFieldValue("whitelistPlayers", component.LockWList);
                     }
                     baseEntity.gameObject.Identity();
                     baseEntity.SetParent(ent, "lock");
                     baseEntity.Spawn();
                     ent.SetSlot(BaseEntity.Slot.Lock, baseEntity);
                 }
                 else if (component.HasKeyLock)
                 {
                     BaseEntity baseEntity = GameManager.server.CreateEntity("build/locks/lock.key", Vector3.zero, new Quaternion());
                     baseEntity.OnDeployed(ent);
                     int code = int.Parse(component.LockCode);
                     if ((code & 0x80) != 0)
                     {
                         KeyLock keyLock = baseEntity.GetComponent <KeyLock>();
                         keyLock.SetFieldValue("keycode", (code & 0x7F));
                         keyLock.SetFieldValue("firstKeyCreated", true);
                         keyLock.SetFlag(BaseEntity.Flags.Locked, true);
                     }
                     baseEntity.gameObject.Identity();
                     baseEntity.SetParent(ent, "lock");
                     baseEntity.Spawn();
                     ent.SetSlot(BaseEntity.Slot.Lock, baseEntity);
                 }
             }
         }
         else if (component.HasPainting)
         {
             Signage signage = ent.GetComponent <Signage>();
             if (component.Painting != null)
             {
                 byte[] painting = component.Painting;
                 signage.textureID = FileStorage.server.Store(painting, FileStorage.Type.png, signage.net.ID);
             }
             if (component.PaintingLocked)
             {
                 signage.SetFlag(BaseEntity.Flags.Locked, true);
             }
             signage.SendNetworkUpdate();
         }
         ent.SendNetworkUpdateImmediate();
     }
     foreach (SpawnableComponent component in SpawnableComponents.Values)
     {
         Vector3    v3  = (component.LocalPosition.ToVector3() + spawnAt);
         BaseEntity ent = GameManager.server.CreateEntity(component.Prefab, v3, component.LocalRotation.ToQuaternion());
         ent.SpawnAsMapEntity();
         ent.SendNetworkUpdateImmediate();
     }
 }