public IEnumerator LoadAssetBundles() { float start = Time.time; SideLoader.Log("Loading Asset Bundles..."); // get all bundle folders foreach (string filepath in SL.Instance.FilePaths[ResourceTypes.AssetBundle]) { try { var bundle = AssetBundle.LoadFromFile(filepath); if (bundle && // not sure if necessary, just to be safe bundle is AssetBundle) { SL.Instance.LoadedBundles.Add(Path.GetFileNameWithoutExtension(filepath), bundle); SideLoader.Log(" - Loaded bundle: " + filepath); } } catch (Exception e) { SideLoader.Log(string.Format("Error loading bundle: {0}\r\nMessage: {1}\r\nStack Trace: {2}", filepath, e.Message, e.StackTrace), 1); } yield return(null); } SL.Instance.Loading = false; SideLoader.Log("Asset Bundles loaded. Time: " + (Time.time - start)); }
public IEnumerator LoadTextures() { SideLoader.Log("Reading Texture2D data..."); float start = Time.time; foreach (string filepath in SL.Instance.FilePaths[ResourceTypes.Texture]) { Texture2D texture2D = LoadPNG(filepath); string texname = Path.GetFileNameWithoutExtension(filepath); if (SL.Instance.TextureData.ContainsKey(texname)) { SideLoader.Log("Adding " + texname + " to texture data dictionary, but a key with this name already exists! Overwriting..."); SL.Instance.TextureData[texname] = texture2D; } else { SL.Instance.TextureData.Add(texname, texture2D); } SideLoader.Log(" - Texture loaded: " + texname + ", from " + filepath); yield return(null); } SL.Instance.Loading = false; SideLoader.Log("Textures loaded. Time: " + (Time.time - start), 0); }
public override void OnEnable() { base.OnEnable(); obj = new GameObject(ID); GameObject.DontDestroyOnLoad(obj); BaseInstance = this; Instance = obj.AddComponent <SideLoader>(); //Instance._base = this; }
// custom recipes public static void DefineRecipe(int ItemID, int craftingType, List <int> IngredientIDs) { if (ResourcesPrefabManager.Instance.GetItemPrefab(ItemID) is Item item) { SideLoader.Log(" - Defining recipe for " + item.Name); Recipe recipe = Recipe.CreateInstance("Recipe") as Recipe; recipe.SetCraftingType((Recipe.CraftingType)craftingType); recipe.SetRecipeID(ItemID); recipe.SetRecipeName(item.Name); recipe.SetRecipeResults(item, 1); RecipeIngredient[] ingredients = new RecipeIngredient[IngredientIDs.Count()]; for (int i = 0; i < IngredientIDs.Count(); i++) { int id = IngredientIDs[i]; RecipeIngredient ingredient = new RecipeIngredient() { ActionType = RecipeIngredient.ActionTypes.AddSpecificIngredient, AddedIngredient = ResourcesPrefabManager.Instance.GetItemPrefab(id), }; ingredients[i] = ingredient; } recipe.SetRecipeIngredients(ingredients); recipe.Init(); // add to recipe dictionary if (At.GetValue(typeof(RecipeManager), RecipeManager.Instance, "m_recipes") is Dictionary <string, Recipe> dict && At.GetValue(typeof(RecipeManager), RecipeManager.Instance, "m_recipeUIDsPerUstensils") is Dictionary <Recipe.CraftingType, List <UID> > dict2) { // add to main recipe dictionary dict.Add(recipe.UID, recipe); At.SetValue(dict, typeof(RecipeManager), RecipeManager.Instance, "m_recipes"); // add to the "UID per Utensil" dictionary thing if (!dict2.ContainsKey(recipe.CraftingStationType)) { dict2.Add(recipe.CraftingStationType, new List <UID>()); } dict2[recipe.CraftingStationType].Add(recipe.UID); At.SetValue(dict2, typeof(RecipeManager), RecipeManager.Instance, "m_recipeUIDsPerUstensils"); SideLoader.Log("added " + item.Name + " to custom recipe to dict"); } } }
// ============ 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); } }
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); }
public IEnumerator ReplaceActiveAssets() { SideLoader.Log("Replacing Materials.."); float start = Time.time; // ============ materials ============ var list = Resources.FindObjectsOfTypeAll <Material>() .Where(x => x.mainTexture != null && SL.Instance.TextureData.ContainsKey(x.mainTexture.name)) .ToList(); SideLoader.Log(string.Format("Found {0} materials to replace.", list.Count)); int i = 0; foreach (Material m in list) { string name = m.mainTexture.name; i++; SideLoader.Log(string.Format(" - Replacing material {0} of {1}: {2}", i, list.Count, name)); // set maintexture (diffuse map) m.mainTexture = SL.Instance.TextureData[name]; // ======= set other shader material layers ======= if (name.EndsWith("_d")) { name = name.Substring(0, name.Length - 2); } // try remove the _d suffix, if its there // check each shader material suffix name foreach (KeyValuePair <string, string> entry in TextureSuffixes) { if (entry.Key == "_d") { continue; } // already set MainTex if (SL.Instance.TextureData.ContainsKey(name + entry.Key)) { SideLoader.Log(" - Setting " + entry.Value + " for " + m.name); m.SetTexture(entry.Value, SL.Instance.TextureData[name + entry.Key]); } } yield return(null); } // ========= sprites ========= SideLoader.Log("Replacing PrefabManager icons..."); if (At.GetValue(typeof(ResourcesPrefabManager), null, "ITEM_PREFABS") is Dictionary <string, Item> dict) { foreach (Item item in dict.Values .Where(x => x.ItemID > 2000000 && x.ItemIcon != null && x.ItemIcon.texture != null && SL.Instance.TextureData.ContainsKey(x.ItemIcon.texture.name))) { string name = item.ItemIcon.texture.name; SideLoader.Log(string.Format(" - Replacing item icon: {0}", name)); var tex = SL.Instance.TextureData[name]; var sprite = CreateSprite(tex); At.SetValue(sprite, typeof(Item), item, "m_itemIcon"); } } // ============================================== SideLoader.Log("Active assets replaced. Time: " + (Time.time - start), 0); SL.Instance.Loading = false; }