示例#1
0
        /// <summary>
        /// Load an SL_Type object from XML.
        /// </summary>
        public static object LoadFromXml(string path)
        {
            if (!File.Exists(path))
            {
                SL.Log("LoadFromXml :: Trying to load an XML but path doesnt exist: " + path);
                return(null);
            }

            try
            {
                var type = GetBaseTypeOfXmlDocument(path);

                object ret;
                using (var file = File.OpenRead(path))
                {
                    ret = LoadFromXml(file, type);
                }
                return(ret);
            }
            catch (Exception e)
            {
                SL.LogError($"Exception reading the XML file: '{path}'!");
                SL.LogInnerException(e);

                return(null);
            }
        }
示例#2
0
        internal void Internal_Apply()
        {
            // add uid to CustomCharacters callback dictionary
            if (!string.IsNullOrEmpty(this.UID))
            {
                if (CustomCharacters.Templates.ContainsKey(this.UID))
                {
                    SL.LogError("Trying to register an SL_Character Template, but one is already registered with this UID: " + UID);
                    return;
                }

                CustomCharacters.Templates.Add(this.UID, this);
            }

            if (this.LootableOnDeath && this.DropTableUIDs?.Length > 0 && !this.DropPouchContents)
            {
                SL.LogWarning($"SL_Character '{UID}' has LootableOnDeath=true and DropTableUIDs set, but DropPouchContents is false!" +
                              $"You should set DropPouchContents to true in this case or change your template behaviour. Forcing DropPouchContents to true.");
                this.DropPouchContents = true;
            }

            OnPrepare();

            SL.Log("Prepared SL_Character '" + Name + "' (" + UID + ").");
        }
        /// <summary>Replace a global sound with the provided AudioClip.</summary>
        public static void ReplaceAudio(GlobalAudioManager.Sounds sound, AudioClip clip)
        {
            if (!GAMInstance)
            {
                SL.LogWarning("Cannot find GlobalAudioManager Instance!");
                return;
            }

            if (ReplacedClips.Contains(sound))
            {
                SL.Log($"The Sound clip '{sound}' has already been replaced, replacing again...");
            }

            try
            {
                DoReplaceClip(sound, clip);
            }
            catch (Exception e)
            {
                SL.LogError($"Exception replacing clip '{sound}'.\r\nMessage: {e.Message}\r\nStack: {e.StackTrace}");
            }
        }
        /// <summary>
        /// Use this to create or modify an Imbue Effect status.
        /// </summary>
        /// <param name="template">The SL_ImbueEffect Template for this imbue.</param>
        public static ImbueEffectPreset CreateCustomImbue(SL_ImbueEffect template)
        {
            var original = (ImbueEffectPreset)GetOrigEffectPreset(template.TargetStatusID);

            if (!original)
            {
                SL.LogError("Could not find an ImbueEffectPreset with the Preset ID " + template.TargetStatusID);
                return(null);
            }

            ImbueEffectPreset newEffect;

            if (template.TargetStatusID == template.NewStatusID)
            {
                if (!OrigEffectPresets.ContainsKey(template.TargetStatusID))
                {
                    // instantiate and cache original
                    var cached = GameObject.Instantiate(original.gameObject).GetComponent <EffectPreset>();
                    cached.gameObject.SetActive(false);
                    GameObject.DontDestroyOnLoad(cached.gameObject);
                    OrigEffectPresets.Add(template.TargetStatusID, cached);
                }

                newEffect = original;
            }
            else
            {
                // instantiate original and use that as newEffect
                newEffect = GameObject.Instantiate(original.gameObject).GetComponent <ImbueEffectPreset>();
                newEffect.gameObject.SetActive(false);

                // Set Preset ID
                At.SetField <EffectPreset>(newEffect, "m_StatusEffectID", template.NewStatusID);

                // Fix localization
                GetImbueLocalization(original, out string name, out string desc);
                SetImbueLocalization(newEffect, name, desc);
            }

            newEffect.gameObject.name = template.NewStatusID + "_" + (template.Name ?? newEffect.Name);

            // fix RPM_Presets dictionary
            if (!References.RPM_EFFECT_PRESETS.ContainsKey(template.NewStatusID))
            {
                References.RPM_EFFECT_PRESETS.Add(template.NewStatusID, newEffect.GetComponent <EffectPreset>());
            }
            else
            {
                References.RPM_EFFECT_PRESETS[template.NewStatusID] = newEffect;
            }

            // Always do this
            GameObject.DontDestroyOnLoad(newEffect.gameObject);

            //// Apply template
            //if (SL.PacksLoaded)
            //    template.ApplyTemplate();
            //else
            //    SL.INTERNAL_ApplyStatuses += template.ApplyTemplate;

            return(newEffect);
        }
        /// <summary>
        /// Clones an item prefab and returns the clone to you. Caches the original prefab for other mods or other custom items to reference.
        /// </summary>
        /// <param name="cloneTargetID">The Item ID of the Item you want to clone from</param>
        /// <param name="newID">The new Item ID for your cloned item. Can be the same as the target, will overwrite.</param>
        /// <param name="name">Only used for the gameObject name, not the actual Item Name. This is the name thats used in Debug Menus.</param>
        /// <param name="template">[Optional] If provided, the item component may be changed to match the type of the template if necessary.</param>
        /// <returns>Your cloned Item prefab</returns>
        public static Item CreateCustomItem(int cloneTargetID, int newID, string name, SL_Item template = null)
        {
            Item original = GetOriginalItemPrefab(cloneTargetID);

            if (!original)
            {
                SL.LogError("CustomItems.CreateCustomItem - Error! Could not find the clone target Item ID: " + cloneTargetID);
                return(null);
            }

            Item item;

            original.transform.ResetLocal();

            // modifying an existing item
            if (newID == cloneTargetID)
            {
                // Modifying the original prefab for the first time. Cache it in case someone else wants the true original.
                if (!OrigItemPrefabs.ContainsKey(newID))
                {
                    var cached = GameObject.Instantiate(original.gameObject).GetComponent <Item>();
                    cached.gameObject.SetActive(false);
                    GameObject.DontDestroyOnLoad(cached.gameObject);
                    cached.transform.parent = SL.CloneHolder;
                    OrigItemPrefabs.Add(cached.ItemID, cached);
                }

                // apply to the original item prefab. this ensures direct prefab references to this item reflect the changes.
                item = original;
            }
            else // making a new item
            {
                item = GameObject.Instantiate(original.gameObject).GetComponent <Item>();
                item.gameObject.SetActive(false);
                item.gameObject.name = newID + "_" + name;

                // fix for name and description localization
                SetNameAndDescription(item, original.Name, original.Description);
            }

            if (template != null)
            {
                var gameType = Serializer.GetGameType(template.GetType());
                if (gameType != item.GetType())
                {
                    item = UnityHelpers.FixComponentType(gameType, item) as Item;
                }
            }

            item.ItemID = newID;
            SetItemID(newID, item);

            // Do this so that any changes we make are not destroyed on scene changes.
            // This is needed whether this is a clone or a new item.
            GameObject.DontDestroyOnLoad(item.gameObject);

            item.transform.parent = SL.CloneHolder;
            item.gameObject.SetActive(true);

            return(item);
        }