Exemple #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);
            }
        }
Exemple #2
0
        // invoked when character is being saved

        internal string INTERNAL_OnPrepareSave(Character character)
        {
            string ret = null;

            try
            {
                ret = OnCharacterBeingSaved?.Invoke(character);
            }
            catch (Exception e)
            {
                SL.LogWarning("Exception invoking OnCharacterBeingSaved for template '" + this.UID + "'");
                SL.LogInnerException(e);
            }

            return(ret);
        }
Exemple #3
0
        internal bool GetShouldSpawn()
        {
            if (this.ShouldSpawn != null)
            {
                try
                {
                    return(ShouldSpawn.Invoke());
                }
                catch (Exception ex)
                {
                    SL.LogWarning("Exception invoking ShouldSpawn callback for " + this.Name + " (" + this.UID + "), not spawning.");
                    SL.LogInnerException(ex);
                    return(false);
                }
            }

            return(true);
        }
        /// <summary>
        /// Load an Audio Clip from a given byte array, and optionally put it in the provided SL Pack.<br/><br/>
        /// WARNING: AudioClips loaded from byte arrays are currently unreliable and may be glitched, use at own risk!
        /// </summary>
        /// <param name="data">The byte[] array from <see cref="File.ReadAllBytes(string)"/> on the wav file path.</param>
        /// <param name="name">The name to give to the audio clip.</param>
        /// <param name="pack">Optional SL Pack to put the audio clip inside.</param>
        /// <returns>The loaded audio clip, if successful.</returns>
        public static AudioClip LoadAudioClip(byte[] data, string name, SLPack pack = null)
        {
            SL.LogWarning("WARNING: AudioClips loaded from embedded .zip archives are currently unreliable and may be glitched, use at own risk!");

            try
            {
                var clip = ConvertByteArrayToAudioClip(data, name);

                if (!clip)
                {
                    return(null);
                }

                return(FinalizeAudioClip(clip, name, pack));
            }
            catch (Exception ex)
            {
                SL.LogWarning("Exception loading AudioClip!");
                SL.LogInnerException(ex);
                return(null);
            }
        }
Exemple #5
0
        public static object LoadFromXml(Stream stream, Type baseType)
        {
            try
            {
                var xml = GetXmlSerializer(baseType);

                object obj = null;

                using (var reader = new StreamReader(stream))
                {
                    obj = xml.Deserialize(reader);
                }

                return(obj);
            }
            catch (Exception ex)
            {
                SL.LogWarning("Exception loading XML stream!");
                SL.LogInnerException(ex);
                return(null);
            }
        }
Exemple #6
0
        /// <summary>
        /// Save an SL_Type object to xml.
        /// </summary>
        public static void SaveToXml(string dir, string saveName, object obj)
        {
            if (!string.IsNullOrEmpty(dir))
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                dir += "/";
            }

            saveName = ReplaceInvalidChars(saveName);

            string path = dir + saveName + ".xml";

            if (File.Exists(path))
            {
                //SL.LogWarning("SaveToXml: A file already exists at " + path + "! Deleting...");
                File.Delete(path);
            }

            var xml = GetXmlSerializer(obj.GetType());

            using (var file = File.OpenWrite(path))
            {
                try
                {
                    xml.Serialize(file, obj);
                }
                catch (Exception ex)
                {
                    SL.LogWarning("Exception saving object to XML!");
                    SL.LogInnerException(ex);
                }
            }
        }
Exemple #7
0
        internal void Internal_ApplyRecipe()
        {
            try
            {
                SL.Log("Defining recipe UID: " + this.UID);

                if (string.IsNullOrEmpty(this.UID))
                {
                    SL.LogWarning("No UID was set! Please set a UID, for example 'myname.myrecipe'. Aborting.");
                    return;
                }

                if (m_applied)
                {
                    SL.Log("Trying to apply an SL_Recipe that is already applied! This is not allowed.");
                    return;
                }

                var results = new List <ItemReferenceQuantity>();
                foreach (var result in this.Results)
                {
                    var resultItem = ResourcesPrefabManager.Instance.GetItemPrefab(result.ItemID);
                    if (!resultItem)
                    {
                        SL.Log("Error: Could not get recipe result id : " + result.ItemID);
                        return;
                    }
                    results.Add(new ItemReferenceQuantity(resultItem, result.Quantity));
                }

                var ingredients = new List <RecipeIngredient>();
                foreach (var ingredient in this.Ingredients)
                {
                    // legacy support
                    if (!string.IsNullOrEmpty(ingredient.Ingredient_Tag))
                    {
                        ingredient.SelectorValue = ingredient.Ingredient_Tag;
                    }
                    else if (ingredient.Ingredient_ItemID != 0)
                    {
                        ingredient.SelectorValue = ingredient.Ingredient_ItemID.ToString();
                    }

                    if (ingredient.Type == RecipeIngredient.ActionTypes.AddGenericIngredient)
                    {
                        var tag = CustomTags.GetTag(ingredient.SelectorValue);
                        if (tag == Tag.None)
                        {
                            SL.LogWarning("Could not get a tag by the name of '" + ingredient.SelectorValue);
                            return;
                        }

                        ingredients.Add(new RecipeIngredient
                        {
                            ActionType          = ingredient.Type,
                            AddedIngredientType = new TagSourceSelector(tag)
                        });
                    }
                    else
                    {
                        int.TryParse(ingredient.SelectorValue, out int id);
                        if (id == 0)
                        {
                            SL.LogWarning("Picking an Ingredient based on Item ID, but no ID was set. Check your XML and make sure there are no logical errors. Aborting");
                            return;
                        }

                        var ingredientItem = ResourcesPrefabManager.Instance.GetItemPrefab(id);
                        if (!ingredientItem)
                        {
                            SL.Log("Error: Could not get ingredient id : " + id);
                            return;
                        }

                        // The item needs the station type tag in order to be used in a manual recipe on that station
                        var tag = TagSourceManager.GetCraftingIngredient(StationType);
                        if (!ingredientItem.HasTag(tag))
                        {
                            //SL.Log($"Adding tag {tag.TagName} to " + ingredientItem.name);

                            if (!ingredientItem.GetComponent <TagSource>())
                            {
                                ingredientItem.gameObject.AddComponent <TagSource>();
                            }

                            ((List <TagSourceSelector>)At.GetField <TagListSelectorComponent>(ingredientItem.GetComponent <TagSource>(), "m_tagSelectors"))
                            .Add(new TagSourceSelector(tag));
                        }

                        ingredients.Add(new RecipeIngredient()
                        {
                            ActionType      = RecipeIngredient.ActionTypes.AddSpecificIngredient,
                            AddedIngredient = ingredientItem,
                        });
                    }
                }

                var recipe = ScriptableObject.CreateInstance <Recipe>();

                recipe.SetCraftingType(this.StationType);

                At.SetField(recipe, "m_results", results.ToArray());
                recipe.SetRecipeIngredients(ingredients.ToArray());

                // set or generate UID
                if (string.IsNullOrEmpty(this.UID))
                {
                    var uid = $"{recipe.Results[0].ItemID}{recipe.Results[0].Quantity}";
                    foreach (var ing in recipe.Ingredients)
                    {
                        if (ing.AddedIngredient != null)
                        {
                            uid += $"{ing.AddedIngredient.ItemID}";
                        }
                        else if (ing.AddedIngredientType != null)
                        {
                            uid += $"{ing.AddedIngredientType.Tag.TagName}";
                        }
                    }
                    this.UID = uid;
                    At.SetField(recipe, "m_uid", new UID(uid));
                }
                else
                {
                    At.SetField(recipe, "m_uid", new UID(this.UID));
                }

                recipe.Init();

                // fix Recipe Manager dictionaries to contain our recipe
                var dict  = References.ALL_RECIPES;
                var dict2 = References.RECIPES_PER_UTENSIL;

                if (dict.ContainsKey(recipe.UID))
                {
                    dict[recipe.UID] = recipe;
                }
                else
                {
                    dict.Add(recipe.UID, recipe);
                }

                if (!dict2.ContainsKey(recipe.CraftingStationType))
                {
                    dict2.Add(recipe.CraftingStationType, new List <UID>());
                }

                if (!dict2[recipe.CraftingStationType].Contains(recipe.UID))
                {
                    dict2[recipe.CraftingStationType].Add(recipe.UID);
                }

                m_applied = true;
            }
            catch (Exception e)
            {
                SL.LogWarning("Error applying recipe!");
                SL.LogInnerException(e);
            }
        }