Пример #1
0
        // ============ Class Setups ============== //

        public void ApplyCustomItem(CustomItem template)
        {
            if (ResourcesPrefabManager.Instance.GetItemPrefab(template.CloneTarget_ItemID) is Item origItem)
            {
                SideLoader.Log("  - Applying template for " + template.Name);

                // clone it, set inactive, and dont destroy on load
                Item item = CloneItem(origItem, template.New_ItemID);

                // set name and description
                string name = template.Name;
                string desc = template.Description;
                SetNameAndDesc(item, name, desc);

                // set item icon
                if (!string.IsNullOrEmpty(template.ItemIconName) && SL.Instance.TextureData.ContainsKey(template.ItemIconName))
                {
                    Texture2D icon = SL.Instance.TextureData[template.ItemIconName];
                    if (icon)
                    {
                        SetItemIcon(item, icon);
                    }
                }

                bool noVisualsFlag      = false;
                bool noArmorVisualsFlag = false;

                // check if AssetBundle name is defined
                if (!string.IsNullOrEmpty(template.AssetBundle_Name) &&
                    SL.Instance.LoadedBundles.ContainsKey(template.AssetBundle_Name) &&
                    SL.Instance.LoadedBundles[template.AssetBundle_Name] is AssetBundle bundle)
                {
                    // set normal visual prefab
                    if (!string.IsNullOrEmpty(template.VisualPrefabName) &&
                        bundle.LoadAsset <GameObject>(template.VisualPrefabName) is GameObject customModel)
                    {
                        Vector3 posoffset = template.Visual_PosOffset;
                        Vector3 rotoffset = template.Visual_RotOffset;

                        // setting armor "ground item" visuals, dont use the user's values here.
                        if (item is Armor)
                        {
                            posoffset = new Vector3(-1, -1, -1);
                            rotoffset = new Vector3(-1, -1, -1);
                        }

                        SetItemVisualPrefab(item, item.VisualPrefab, customModel.transform, posoffset, rotoffset);
                    }
                    else
                    {
                        // no visual prefab to set. clone the original and rename the material.

                        noVisualsFlag = true;
                    }

                    // set armor visual prefab
                    if (item is Armor)
                    {
                        if (!string.IsNullOrEmpty(template.ArmorVisualPrefabName) && bundle.LoadAsset <GameObject>(template.ArmorVisualPrefabName) is GameObject armorModel)
                        {
                            SetItemVisualPrefab(item,
                                                item.SpecialVisualPrefabDefault,
                                                armorModel.transform,
                                                template.Visual_PosOffset,
                                                template.Visual_RotOffset,
                                                true,
                                                template.HelmetHideFace,
                                                template.HelmetHideHair);
                        }
                        else // no armor prefab to set
                        {
                            noArmorVisualsFlag = true;
                        }
                    }
                }
                else // no asset bundle.
                {
                    noVisualsFlag = true; noArmorVisualsFlag = true;
                }

                // if we should overwrite normal visual prefab, do that now
                if (noVisualsFlag && item.VisualPrefab != null)
                {
                    bool customDefined = false;

                    foreach (string suffix in TexReplacer.TextureSuffixes.Keys)
                    {
                        string search = "tex_itm_" + template.New_ItemID + "_" + template.Name + suffix;
                        if (SL.Instance.TextureData.ContainsKey(search))
                        {
                            customDefined = true;
                            break;
                        }
                    }

                    if (customDefined)
                    {
                        Transform newVisuals = Instantiate(item.VisualPrefab);
                        newVisuals.gameObject.SetActive(false);
                        item.VisualPrefab = newVisuals;
                        DontDestroyOnLoad(newVisuals);

                        // bows are a unique case
                        if (item is Weapon && (item as Weapon).Type == Weapon.WeaponType.Bow)
                        {
                            if (newVisuals.GetComponentInChildren <SkinnedMeshRenderer>() is SkinnedMeshRenderer mesh)
                            {
                                SideLoader.Log("Apply custom Bow ItemVisuals for " + item.Name);

                                string newMatName = "tex_itm_" + template.New_ItemID + "_" + template.Name;

                                Material m = Instantiate(mesh.material);
                                DontDestroyOnLoad(m);

                                mesh.material = m;
                                OverwriteMaterials(m, newMatName);
                            }
                        }
                        else
                        {
                            foreach (Transform child in newVisuals)
                            {
                                if (child.GetComponent <BoxCollider>() && child.GetComponent <MeshRenderer>() is MeshRenderer mesh)
                                {
                                    SideLoader.Log("Apply custom ItemVisuals for " + item.Name);

                                    string newMatName = "tex_itm_" + template.New_ItemID + "_" + template.Name;

                                    Material m = Instantiate(mesh.material);
                                    DontDestroyOnLoad(m);

                                    mesh.material = m;
                                    OverwriteMaterials(m, newMatName);
                                }
                                else
                                {
                                    continue;
                                }
                                break;
                            }
                        }
                    }
                    else
                    {
                        SideLoader.Log("No custom ItemVisuals defined for " + item.Name);
                    }
                }

                // if we should overwrite armor visuals, do that now
                if (noArmorVisualsFlag && item.SpecialVisualPrefab != null)
                {
                    bool customDefined = false;

                    foreach (string suffix in TexReplacer.TextureSuffixes.Keys)
                    {
                        if (SL.Instance.TextureData.ContainsKey("tex_cha_" + template.New_ItemID + "_" + template.Name + suffix))
                        {
                            customDefined = true;
                            break;
                        }
                    }

                    if (customDefined)
                    {
                        Transform newArmorVisuals = Instantiate(item.SpecialVisualPrefabDefault);
                        item.SpecialVisualPrefabDefault = newArmorVisuals;
                        DontDestroyOnLoad(newArmorVisuals);

                        if (newArmorVisuals.GetComponent <SkinnedMeshRenderer>() is SkinnedMeshRenderer mesh)
                        {
                            SideLoader.Log("Overwriting SpecialVisualPrefab visuals for " + item.Name);

                            string newMatName = "tex_cha_" + template.New_ItemID + "_" + template.Name;

                            OverwriteMaterials(mesh.material, newMatName);
                        }
                    }
                    else
                    {
                        SideLoader.Log("No custom ArmorVisuals defined for " + item.Name);
                    }
                }

                // ========== set custom stats ==========
                SetBaseItemStats(item, template.Durability, template.BaseValue, template.Weight);

                SideLoader.Log("initialized item " + template.Name, 0);
            }
            else
            {
                SideLoader.Log("::CustomItems - could not find CloneTarget_ItemID \"" + template.CloneTarget_ItemID + "\" for template " + template.Name, 0);
            }
        }
Пример #2
0
        public IEnumerator LoadItems()
        {
            SideLoader.Log("Loading custom items...");

            foreach (string path in SL.Instance.FilePaths[ResourceTypes.CustomItems])
            {
                string json = File.ReadAllText(path);

                try
                {
                    CustomItem template = new CustomItem();
                    JsonUtility.FromJsonOverwrite(json, template);

                    Item target = ResourcesPrefabManager.Instance.GetItemPrefab(template.CloneTarget_ItemID);

                    ApplyCustomItem(template);
                    SL.Instance.LoadedCustomItems.Add(template.New_ItemID, ResourcesPrefabManager.Instance.GetItemPrefab(template.New_ItemID));

                    if (target is Weapon)
                    {
                        SetWeaponStats(JsonUtility.FromJson <CustomWeapon>(json));
                    }
                    else if (target is Equipment)
                    {
                        SetEquipmentStats(JsonUtility.FromJson <CustomEquipment>(json));
                    }

                    if (target is Skill)
                    {
                        SetSkillStats(JsonUtility.FromJson <CustomSkill>(json));
                    }
                }
                catch (Exception e)
                {
                    SideLoader.Log("Error applying custom json!\r\nError:" + e.Message + "\r\nStack Trace:" + e.StackTrace, 1);
                }

                yield return(null);
            }

            SideLoader.Log("Loaded custom items", 0);

            // custom recipes
            foreach (string path in Directory.GetDirectories(SL.Instance.loadDir))
            {
                if (!Directory.Exists(path + @"\CustomItems\Recipes"))
                {
                    continue;
                }

                foreach (string path2 in Directory.GetFiles(path + @"\CustomItems\Recipes"))
                {
                    string json = File.ReadAllText(path2);

                    if (JsonUtility.FromJson <CustomRecipe>(json) is CustomRecipe template)
                    {
                        DefineRecipe(template.Result_ItemID, template.CraftingType, template.Ingredient_ItemIDs);
                    }
                    yield return(null);
                }
            }

            SL.Instance.Loading = false;
            SideLoader.Log("Loaded custom recipes", 0);
        }