示例#1
0
        public virtual Dictionary <string, Kit> Load()
        {
            var loadedKits = new Dictionary <string, Kit>();

            if (!File.Exists(DataFilePath))
            {
                File.Create(DataFilePath).Close();
                LoadDefault();
            }

            JArray kitArr;

            try
            {
                kitArr = JArray.Parse(File.ReadAllText(DataFilePath));
            }
            catch (JsonReaderException ex)
            {
                UEssentials.Logger.LogError($"Invalid kit configuration ({DataFilePath})");
                UEssentials.Logger.LogError(ex.Message);
                kitArr = JArray.Parse("[]");
            }

            const StringComparison strCmp = StringComparison.InvariantCultureIgnoreCase;

            foreach (var kitObj in kitArr.Children <JObject>())
            {
                var kit = new Kit(
                    kitObj.GetValue("Name", strCmp).Value <string>(),
                    kitObj.GetValue("Cooldown", strCmp).Value <uint>(),
                    kitObj.GetValue("ResetCooldownWhenDie", strCmp).Value <bool>()
                    );

                var rawCost = kitObj.GetValue("Cost", strCmp);

                if (rawCost != null)
                {
                    kit.Cost = rawCost.Value <decimal>();
                }

                var itemIndex   = 0;
                var economyHook = EssCore.Instance.HookManager.GetActiveByType <UconomyHook>();

                foreach (var itemObj in kitObj.GetValue("items", strCmp).Children <JObject>())
                {
                    AbstractKitItem kitItem;

                    if (itemObj.GetValue("money", strCmp) != null && economyHook.IsPresent)
                    {
                        kitItem = new KitItemMoney(itemObj.GetValue("money", strCmp).Value <decimal>());
                        goto add;
                    }

                    if (itemObj.GetValue("xp", strCmp) != null)
                    {
                        kitItem = new KitItemExperience(itemObj.GetValue("xp", strCmp).Value <uint>());
                        goto add;
                    }

                    if (itemObj.GetValue("vehicle", strCmp) != null)
                    {
                        var vehicleId = itemObj.GetValue("vehicle", strCmp).Value <ushort>();

                        if (Assets.find(EAssetType.VEHICLE, vehicleId) == null)
                        {
                            UEssentials.Logger.LogWarning($"Invalid vehicle id. Kit: {kit.Name} item Index: {itemIndex++}");
                            continue;
                        }

                        kitItem = new KitItemVehicle(vehicleId);
                        goto add;
                    }

                    var tokKitItemId         = itemObj.GetValue("id", strCmp);
                    var tokKitItemDurability = itemObj.GetValue("Durability", strCmp);
                    var tokKitItemAmount     = itemObj.GetValue("Amount", strCmp);
                    var tokAmmo = itemObj.GetValue("Ammo", strCmp);

                    var itemAsset = (ItemAsset)Assets.find(EAssetType.ITEM,
                                                           tokKitItemId?.Value <ushort>() ?? 0);

                    if (tokKitItemId == null || itemAsset == null)
                    {
                        UEssentials.Logger.LogWarning($"Invalid item id. Kit: {kit.Name} item Index: {itemIndex++}");
                        continue;
                    }

                    var kitItemId         = tokKitItemId.Value <ushort>();
                    var kitItemAmount     = tokKitItemAmount?.Value <byte>() ?? 1;
                    var kitItemDurability = tokKitItemDurability?.Value <byte>() ?? 100;

                    if (itemAsset.UseableType == EUseableType.GUN)
                    {
                        goto weaponItem;
                    }

                    if (itemAsset is ItemMagazineAsset || itemAsset is ItemSupplyAsset)
                    {
                        kitItem = new KitItemMagazine(kitItemId, kitItemDurability, kitItemAmount, tokAmmo?.Value <byte>() ?? 1);
                    }
                    else
                    {
                        kitItem = new KitItem(kitItemId, kitItemDurability, kitItemAmount);

                        if (itemAsset is ItemFuelAsset)
                        {
                            ((KitItem)kitItem).Metadata[0] = 1;
                        }
                    }
                    goto add;

weaponItem:
                    var tokFireMode = itemObj.GetValue("FireMode", strCmp);
                    var tokBarrel   = itemObj.GetValue("Barrel", strCmp);
                    var tokSight    = itemObj.GetValue("Sight", strCmp);
                    var tokGrip     = itemObj.GetValue("Grip", strCmp);
                    var tokMagazine = itemObj.GetValue("Magazine", strCmp);
                    var tokTactical = itemObj.GetValue("Tatical", strCmp) ?? itemObj.GetValue("Tactical", strCmp);

                    EFiremode?fireMode = null;
                    var       ammo     = tokAmmo?.Value <byte>() ?? null;

                    if (tokFireMode != null)
                    {
                        try
                        {
                            fireMode = (EFiremode)Enum.Parse(typeof(EFiremode),
                                                             tokFireMode.Value <string>(), true);
                        }
                        catch (ArgumentException)
                        {
                            UEssentials.Logger.LogWarning($"Invalid firemode '{tokFireMode.Value<string>()}' " +
                                                          $"in kit '{kit.Name}', item '{itemIndex + 1}'!");
                        }
                    }

                    kitItem = new KitItemWeapon(kitItemId,
                                                kitItemDurability,
                                                kitItemAmount,
                                                ammo,
                                                fireMode);

                    var weaponItem = (KitItemWeapon)kitItem;

                    Func <JToken, Attachment> deserializeAttach = json => {
                        return(json == null ? null : JsonConvert.DeserializeObject <Attachment>(json.ToString()));
                    };

                    weaponItem.Barrel   = deserializeAttach(tokBarrel);
                    weaponItem.Sight    = deserializeAttach(tokSight);
                    weaponItem.Tactical = deserializeAttach(tokTactical);
                    weaponItem.Grip     = deserializeAttach(tokGrip);
                    weaponItem.Magazine = deserializeAttach(tokMagazine);

add:
                    kit.Items.Add(kitItem);
                }
                loadedKits.Add(kit.Name.ToLowerInvariant(), kit);
            }

            return(loadedKits);
        }
示例#2
0
        private AbstractKitItem ParseKitItem(Kit kit, JObject itemObj)
        {
            const StringComparison strCmp = StringComparison.InvariantCultureIgnoreCase;

            if (itemObj.TryGetValue("money", strCmp, out var moneyToken))
            {
                if (!UEssentials.EconomyProvider.IsPresent)
                {
                    UEssentials.Logger.LogWarning("Cannot add 'Money' item because there is no active economy system.");
                    return(null);
                }
                return(new KitItemMoney(moneyToken.Value <decimal>()));
            }

            if (itemObj.TryGetValue("xp", strCmp, out var expToken))
            {
                return(new KitItemExperience(expToken.Value <uint>()));
            }

            if (itemObj.TryGetValue("vehicle", strCmp, out var vehicleIdToken))
            {
                var vehicleId = vehicleIdToken.Value <ushort>();

                if (Assets.find(EAssetType.VEHICLE, vehicleId) == null)
                {
                    UEssentials.Logger.LogWarning($"Invalid vehicle id '{vehicleId}' in the item at {itemObj.Path} in the kit '{kit.Name}'");
                    return(null);
                }

                return(new KitItemVehicle(vehicleId));
            }

            var itemIdToken = itemObj.GetValue("id", strCmp);

            if (itemIdToken == null)
            {
                UEssentials.Logger.LogWarning($"Missing attribute 'Id' in the item at {itemObj.Path} in the kit '{kit.Name}'");
                return(null);
            }

            var itemId    = itemIdToken.Value <ushort>();
            var itemAsset = (ItemAsset)Assets.find(EAssetType.ITEM, itemId);

            var tokKitItemDurability = itemObj.GetValue("Durability", strCmp);
            var tokKitItemAmount     = itemObj.GetValue("Amount", strCmp);
            var tokAmmo = itemObj.GetValue("Ammo", strCmp);

            var kitItemAmount     = tokKitItemAmount?.Value <byte>() ?? 1;
            var kitItemDurability = tokKitItemDurability?.Value <byte>() ?? 100;

            // Parse weapon specific attributes
            if (itemAsset is ItemGunAsset)
            {
                var tokFireMode = itemObj.GetValue("FireMode", strCmp);

                EFiremode?fireMode = null;

                if (tokFireMode != null)
                {
                    try {
                        fireMode = (EFiremode)Enum.Parse(typeof(EFiremode), tokFireMode.Value <string>(), true);
                    } catch (ArgumentException) {
                        UEssentials.Logger.LogWarning($"Invalid firemode '{tokFireMode}' in the item at {itemObj.Path} in the kit '{kit.Name}'. " +
                                                      $"Valid Firemodes: ${string.Join(", ", Enum.GetNames(typeof(EFiremode)))}");
                    }
                }

                var weaponItem = new KitItemWeapon(itemId,
                                                   kitItemDurability,
                                                   kitItemAmount,
                                                   tokAmmo?.Value <byte>() ?? null,
                                                   fireMode
                                                   )
                {
                    Barrel   = itemObj.GetValue("Barrel", strCmp)?.ToObject <Attachment>(),
                    Sight    = itemObj.GetValue("Sight", strCmp)?.ToObject <Attachment>(),
                    Tactical = itemObj.GetValue("Tactical", strCmp)?.ToObject <Attachment>(),
                    Grip     = itemObj.GetValue("Grip", strCmp)?.ToObject <Attachment>(),
                    Magazine = itemObj.GetValue("Magazine", strCmp)?.ToObject <Attachment>()
                };
                return(weaponItem);
            }

            if (itemAsset is ItemMagazineAsset || itemAsset is ItemSupplyAsset)
            {
                var magazineAmmo = tokAmmo?.Value <byte>() ?? itemAsset.amount;
                return(new KitItemMagazine(itemId, kitItemDurability, kitItemAmount, magazineAmmo));
            }

            var kitItem = new KitItem(itemId, kitItemDurability, kitItemAmount);

            if (itemAsset is ItemFuelAsset)
            {
                var item           = kitItem;
                var fuelPercentage = itemObj.GetValue("FuelPercentage", strCmp)?.Value <float>() ?? 100;
                ItemUtil.Refuel(item.Metadata, item.Id, fuelPercentage);
            }
            return(kitItem);
        }
示例#3
0
        public override CommandResult OnExecute(ICommandSource src, ICommandArgs args)
        {
            var player = src.ToPlayer();

            if (args.Length < 1)
            {
                return(CommandResult.ShowUsage());
            }

            var  name                 = args[0].ToString();
            uint cooldown             = 0;
            var  resetCooldownWhenDie = false;
            var  cost                 = 0d;

            if (KitModule.Instance.KitManager.Contains(name))
            {
                return(CommandResult.Lang("KIT_ALREADY_EXIST", name));
            }

            if (args.Length > 1)
            {
                if (!args[1].IsInt)
                {
                    return(CommandResult.Lang("INVALID_NUMBER", args[1]));
                }

                if (args[1].ToInt < 0)
                {
                    return(CommandResult.Lang("MUST_POSITIVE"));
                }

                cooldown = args[1].ToUInt;
            }

            if (args.Length > 2)
            {
                if (!args[2].IsBool)
                {
                    return(CommandResult.Lang("INVALID_BOOLEAN", args[2]));
                }

                resetCooldownWhenDie = args[2].ToBool;
            }

            if (args.Length > 3)
            {
                if (!args[3].IsDouble)
                {
                    return(CommandResult.Lang("INVALID_NUMBER", args[3]));
                }

                if (args[3].ToDouble < 0)
                {
                    return(CommandResult.Lang("MUST_POSITIVE"));
                }

                cost = args[3].ToDouble;
            }

            var inventory = player.Inventory;
            var clothing  = player.Clothing;
            var items     = new List <AbstractKitItem>();

            Action <byte> addItemsFromPage = page => {
                var count = inventory.getItemCount(page);

                for (byte index = 0; index < count; index++)
                {
                    var item = inventory.getItem(page, index).item;

                    if (item == null)
                    {
                        continue;
                    }

                    var     asset = GetItem(item.id).Value;
                    KitItem kitItem;

                    if (asset is ItemWeaponAsset)
                    {
                        var ammo     = GetWeaponAmmo(item);
                        var firemode = GetWeaponFiremode(item).OrElse(EFiremode.SAFETY);

                        var kItem = new KitItemWeapon(item.id, item.durability, 1, ammo.OrElse(0), firemode)
                        {
                            Magazine = GetWeaponAttachment(item, AttachmentType.MAGAZINE).OrElse(null),
                            Barrel   = GetWeaponAttachment(item, AttachmentType.BARREL).OrElse(null),
                            Sight    = GetWeaponAttachment(item, AttachmentType.SIGHT).OrElse(null),
                            Grip     = GetWeaponAttachment(item, AttachmentType.GRIP).OrElse(null),
                            Tactical = GetWeaponAttachment(item, AttachmentType.TACTICAL).OrElse(null)
                        };

                        kitItem = kItem;
                    }
                    else if (asset is ItemMagazineAsset || asset is ItemSupplyAsset)
                    {
                        kitItem = new KitItemMagazine(item.id, item.durability, 1, item.amount);
                    }
                    else
                    {
                        kitItem = new KitItem(item.id, item.durability, 1);
                    }

                    kitItem.Metadata = item.metadata;

                    items.Add(kitItem);
                }
            };

            addItemsFromPage(0); // Primary slot
            addItemsFromPage(1); // Secondary slot
            addItemsFromPage(2); // Hands

            // Backpack

            if (clothing.backpack != 0)
            {
                items.Add(new KitItem(clothing.backpack, clothing.backpackQuality, 1)
                {
                    Metadata = clothing.backpackState
                });
            }

            addItemsFromPage(3);

            // End Backpack

            // Shirt

            if (clothing.shirt != 0)
            {
                items.Add(new KitItem(clothing.shirt, clothing.shirtQuality, 1)
                {
                    Metadata = clothing.shirtState
                });
            }

            addItemsFromPage(5);

            // End Shirt

            // Vest

            if (clothing.vest != 0)
            {
                items.Add(new KitItem(clothing.vest, clothing.vestQuality, 1)
                {
                    Metadata = clothing.vestState
                });
            }

            addItemsFromPage(4);

            // End Vest

            // Pants

            if (clothing.pants != 0)
            {
                items.Add(new KitItem(clothing.pants, clothing.pantsQuality, 1)
                {
                    Metadata = clothing.pantsState
                });
            }

            addItemsFromPage(6);

            // End Pants

            // Mask, Glasses & Hat

            if (clothing.mask != 0)
            {
                items.Add(new KitItem(clothing.mask, clothing.maskQuality, 1)
                {
                    Metadata = clothing.maskState
                });
            }

            if (clothing.hat != 0)
            {
                items.Add(new KitItem(clothing.hat, clothing.hatQuality, 1)
                {
                    Metadata = clothing.hatState
                });
            }

            if (clothing.glasses != 0)
            {
                items.Add(new KitItem(clothing.glasses, clothing.glassesQuality, 1)
                {
                    Metadata = clothing.glassesState
                });
            }

            // End Mask, Glasses & Hat

            var kit = new Kit(name, cooldown, (decimal)cost, resetCooldownWhenDie)
            {
                Items = items
            };

            KitModule.Instance.KitManager.Add(kit);
            EssLang.Send(src, "CREATED_KIT", name);

            return(CommandResult.Success());
        }
示例#4
0
        public virtual Dictionary <string, Kit> Load()
        {
            var loadedKits = new Dictionary <string, Kit>();

            if (!File.Exists(DataFilePath))
            {
                File.Create(DataFilePath).Close();
                LoadDefault();
            }

            JArray kitArr;

            try {
                kitArr = JArray.Parse(File.ReadAllText(DataFilePath));
            } catch (JsonReaderException ex) {
                UEssentials.Logger.LogError($"Invalid kit configuration ({DataFilePath})");
                UEssentials.Logger.LogError(ex.ToString());
                kitArr = new JArray();
            }

            const StringComparison strCmp = StringComparison.InvariantCultureIgnoreCase;

            foreach (var kitObj in kitArr.Children <JObject>())
            {
                var name = GetNotNullValue <string>(kitObj, "Name");

                if (loadedKits.ContainsKey(name.ToLowerInvariant()))
                {
                    UEssentials.Logger.LogWarning($"Duplicated kit name ({name})");
                    continue;
                }

                var kit = new Kit(
                    name,
                    GetValueOrDefault(kitObj, "Cooldown", 0u),
                    GetValueOrDefault(kitObj, "Cost", 0m),
                    GetValueOrDefault(kitObj, "ResetCooldownWhenDie", false)
                    );

                var itemIndex   = 0;
                var economyHook = UEssentials.EconomyProvider;

                foreach (var itemObj in kitObj.GetValue("Items", strCmp).Children <JObject>())
                {
                    AbstractKitItem kitItem;
                    JToken          val;

                    if ((val = itemObj.GetValue("money", strCmp)) != null)
                    {
                        if (!economyHook.IsPresent)
                        {
                            UEssentials.Logger.LogWarning("Cannot add 'Money' item because there is no active economy system.");
                            continue;
                        }
                        kitItem = new KitItemMoney(val.Value <decimal>());
                        goto add;
                    }

                    if ((val = itemObj.GetValue("xp", strCmp)) != null)
                    {
                        kitItem = new KitItemExperience(val.Value <uint>());
                        goto add;
                    }

                    if ((val = itemObj.GetValue("vehicle", strCmp)) != null)
                    {
                        var vehicleId = val.Value <ushort>();

                        if (Assets.find(EAssetType.VEHICLE, vehicleId) == null)
                        {
                            UEssentials.Logger.LogWarning(
                                $"Invalid vehicle id '{vehicleId}' in kit '{kit.Name}' at index {++itemIndex}");
                            continue;
                        }

                        kitItem = new KitItemVehicle(vehicleId);
                        goto add;
                    }

                    var kitItemIdToken = itemObj.GetValue("id", strCmp);

                    if (kitItemIdToken == null)
                    {
                        UEssentials.Logger.LogWarning($"Missing attribute 'Id' in kit '{kit.Name}' at index {++itemIndex}");
                        continue;
                    }

                    var kitItemId = kitItemIdToken.Value <ushort>();
                    var itemAsset = (ItemAsset)Assets.find(EAssetType.ITEM, kitItemId);

                    if (itemAsset == null)
                    {
                        UEssentials.Logger.LogWarning($"Invalid item id '{kitItemId}' in kit '{kit.Name}' at index {++itemIndex}");
                        continue;
                    }

                    var tokKitItemDurability = itemObj.GetValue("Durability", strCmp);
                    var tokKitItemAmount     = itemObj.GetValue("Amount", strCmp);
                    var tokAmmo = itemObj.GetValue("Ammo", strCmp);

                    var kitItemAmount     = tokKitItemAmount?.Value <byte>() ?? 1;
                    var kitItemDurability = tokKitItemDurability?.Value <byte>() ?? 100;

                    if (itemAsset is ItemGunAsset)   // TODO: check
                    {
                        goto parseWeaponItem;
                    }

                    if (itemAsset is ItemMagazineAsset || itemAsset is ItemSupplyAsset)
                    {
                        var magazineAmmo = tokAmmo?.Value <byte>() ?? itemAsset.amount;
                        kitItem = new KitItemMagazine(kitItemId, kitItemDurability, kitItemAmount, magazineAmmo);
                    }
                    else
                    {
                        kitItem = new KitItem(kitItemId, kitItemDurability, kitItemAmount);

                        if (itemAsset is ItemFuelAsset)
                        {
                            var item = (KitItem)kitItem;
                            ItemUtil.Refuel(item.Metadata, item.Id);
                        }
                    }
                    goto add;

parseWeaponItem:
                    var tokFireMode = itemObj.GetValue("FireMode", strCmp);
                    var tokBarrel   = itemObj.GetValue("Barrel", strCmp);
                    var tokSight    = itemObj.GetValue("Sight", strCmp);
                    var tokGrip     = itemObj.GetValue("Grip", strCmp);
                    var tokMagazine = itemObj.GetValue("Magazine", strCmp);
                    var tokTactical = itemObj.GetValue("Tatical", strCmp) ?? itemObj.GetValue("Tactical", strCmp);

                    EFiremode?fireMode = null;
                    var       ammo     = tokAmmo?.Value <byte>() ?? null;

                    if (tokFireMode != null)
                    {
                        try {
                            fireMode = (EFiremode)Enum.Parse(typeof(EFiremode),
                                                             tokFireMode.Value <string>(), true);
                        } catch (ArgumentException) {
                            UEssentials.Logger.LogWarning($"Invalid firemode '{tokFireMode.Value<string>()}' " +
                                                          $"in kit '{kit.Name}', item '{itemIndex + 1}'!");
                        }
                    }

                    var weaponItem = new KitItemWeapon(kitItemId,
                                                       kitItemDurability,
                                                       kitItemAmount,
                                                       ammo,
                                                       fireMode
                                                       );

                    Func <JToken, Attachment> deserializeAttach = json => {
                        return(json == null ? null : JsonConvert.DeserializeObject <Attachment>(json.ToString()));
                    };

                    weaponItem.Barrel   = deserializeAttach(tokBarrel);
                    weaponItem.Sight    = deserializeAttach(tokSight);
                    weaponItem.Tactical = deserializeAttach(tokTactical);
                    weaponItem.Grip     = deserializeAttach(tokGrip);
                    weaponItem.Magazine = deserializeAttach(tokMagazine);

                    kitItem = weaponItem;

add:
                    kit.Items.Add(kitItem);
                }
                loadedKits.Add(kit.Name.ToLowerInvariant(), kit);
            }

            return(loadedKits);
        }