Ejemplo n.º 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);
        }
Ejemplo n.º 2
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);
        }