Example #1
0
        /// <summary>
        /// Attempts to load saved mod settings from file & updates loaded mods
        /// </summary>
        /// <returns></returns>
        public static bool LoadModSettings()
        {
            FullSerializer.fsResult result = new FullSerializer.fsResult();

            try
            {
                string filepath = Path.Combine(ModManager.instance.modDirectory, MODCONFIGFILENAME);
                if (!File.Exists(filepath))
                {
                    return(false);
                }

                var serializedData = File.ReadAllText(filepath);
                if (string.IsNullOrEmpty(serializedData))
                {
                    return(false);
                }


                List <Mod>            temp = new List <Mod>();
                FullSerializer.fsData data = FullSerializer.fsJsonParser.Parse(serializedData);
                result = _serializer.TryDeserialize <List <Mod> >(data, ref temp);

                if (result.Failed || temp.Count <= 0)
                {
                    return(false);
                }


                foreach (Mod _mod in temp)
                {
                    if (ModManager.instance.GetModIndex(_mod.Title) >= 0)
                    {
                        Mod mod = ModManager.Instance.GetMod(_mod.Title);
                        if (mod == null)
                        {
                            continue;
                        }
                        mod.Enabled      = _mod.Enabled;
                        mod.LoadPriority = _mod.LoadPriority;
                        ModManager.instance.Mods[ModManager.instance.GetModIndex(_mod.Title)] = mod;
                    }
                }

                return(true);
            }
            catch (Exception ex)
            {
                Debug.LogError(string.Format("Error trying to load mod settings: {0}", ex.Message));
                return(false);
            }
        }
Example #2
0
        /// <summary>
        /// Serializes components on gameobject & children w/ Idfmod_Serializable interface
        /// WARNING: This will destroy any serialized component from prefab
        /// </summary>
        /// <param name="prefab">base prefab</param>
        /// <param name="serialized">serialized string</param>
        /// <returns></returns>
        private static bool SerializePrefabHelper(GameObject prefab, out string serialized)
        {
            serialized = "";

            Dictionary <string, List <SerializedRecord> > recordDictionary = new Dictionary <string, List <SerializedRecord> >();
            List <Transform> transforms = new List <Transform>();

            ModManager.GetAllChildren(prefab.transform, ref transforms);

            for (int i = 0; i < transforms.Count; i++)
            {
                if (transforms[i] == null)
                {
                    continue;
                }

                GameObject go = transforms[i].gameObject;
                List <SerializedRecord> serializedRecords = new List <SerializedRecord>();
                Component[]             components        = go.GetComponents <Component>();

                for (int j = 0; j < components.Length; j++)
                {
                    if (components[j] == null)
                    {
                        continue;
                    }

                    Component           co            = components[j];
                    Idfmod_Serializable sModInterface = co as Idfmod_Serializable;

                    if (sModInterface == null)
                    {
                        continue;
                    }
                    else if (sModInterface.Ignore)
                    {
                        continue;
                    }

                    object[] toSerialize = sModInterface.ToSerialize();
                    if (toSerialize != null)
                    {
                        for (int k = 0; k < toSerialize.Length; k++)
                        {
                            if (toSerialize[k].GetType().IsSubclassOf(typeof(Component)))
                            {
                                Debug.LogError("Can't serialize monobehaviours: " + toSerialize[k].ToString());
                                return(false);
                            }
                        }
                    }

                    SerializedRecord sr = new SerializedRecord(go.name, co, toSerialize);
                    serializedRecords.Add(sr);
                    UnityEngine.Object.DestroyImmediate(co, true);
                }

                if (serializedRecords.Count > 0)
                {
                    if (recordDictionary.ContainsKey(go.name))
                    {
                        Debug.LogWarning("Please make sure all game objects have unique names, can't serialize objects for: " + go.name);
                        continue;
                    }
                    else
                    {
                        recordDictionary.Add(go.name, serializedRecords);
                    }
                }
            }

            FullSerializer.fsData   sData;
            FullSerializer.fsResult result = ModManager._serializer.TrySerialize(typeof(Dictionary <string, List <SerializedRecord> >), recordDictionary, out sData).AssertSuccessWithoutWarnings();

            serialized = FullSerializer.fsJsonPrinter.PrettyJson(sData);
            return(result.Succeeded);
        }