Example #1
0
    // Token: 0x06001E4C RID: 7756 RVA: 0x0017D4BC File Offset: 0x0017B6BC
    public static void LoadData(string targetSave, bool recreateMissing = false)
    {
        if (!File.Exists(Path.Combine(YanSave.SaveDataPath, targetSave + ".yansave")))
        {
            return;
        }
        YanSaveData yanSaveData = JsonConvert.DeserializeObject <YanSaveData>(File.ReadAllText(Path.Combine(YanSave.SaveDataPath, targetSave + ".yansave")));

        if (SceneManager.GetActiveScene().name != yanSaveData.LoadedLevelName)
        {
            SceneManager.LoadScene(yanSaveData.LoadedLevelName);
        }
        SerializedGameObject[] serializedGameObjects = yanSaveData.SerializedGameObjects;
        int i = 0;

        while (i < serializedGameObjects.Length)
        {
            SerializedGameObject serializedGameObject = serializedGameObjects[i];
            GameObject           gameObject           = YanSaveIdentifier.GetObject(serializedGameObject);
            if (!(gameObject == null))
            {
                goto IL_BA;
            }
            if (recreateMissing)
            {
                gameObject = new GameObject();
                gameObject.AddComponent <YanSaveIdentifier>().ObjectID = serializedGameObject.ObjectID;
                gameObject.SetActive(serializedGameObject.ActiveSelf);
                goto IL_BA;
            }
IL_15D:
            i++;
            continue;
IL_BA:
            gameObject.isStatic = serializedGameObject.IsStatic;
            gameObject.layer    = serializedGameObject.Layer;
            gameObject.tag      = serializedGameObject.Tag;
            gameObject.name     = serializedGameObject.Name;
            gameObject.SetActive(serializedGameObject.ActiveSelf);
            foreach (SerializedComponent serializedComponent in serializedGameObject.SerializedComponents)
            {
                if (gameObject != null)
                {
                    Type type = YanSave.GetType(serializedComponent.TypePath);
                    if (recreateMissing && gameObject.GetComponent(type) == null)
                    {
                        gameObject.AddComponent(type);
                    }
                }
            }
            goto IL_15D;
        }
        foreach (SerializedGameObject serializedGameObject2 in yanSaveData.SerializedGameObjects)
        {
            GameObject @object = YanSaveIdentifier.GetObject(serializedGameObject2);
            if (!(@object == null))
            {
                foreach (SerializedComponent serializedComponent2 in serializedGameObject2.SerializedComponents)
                {
                    Type      type2     = YanSave.GetType(serializedComponent2.TypePath);
                    Component component = @object.GetComponent(type2);
                    @object.GetComponent <YanSaveIdentifier>();
                    if (!(component == null))
                    {
                        if (serializedComponent2.IsMonoBehaviour)
                        {
                            ((MonoBehaviour)component).enabled = serializedComponent2.IsEnabled;
                        }
                        foreach (PropertyInfo propertyInfo in YanSave.GetCachedProperties(type2))
                        {
                            if (propertyInfo.CanWrite)
                            {
                                bool flag = typeof(Component).IsAssignableFrom(propertyInfo.PropertyType);
                                if (!flag && propertyInfo.PropertyType != typeof(GameObject))
                                {
                                    if (serializedComponent2.PropertyValues.ContainsKey(propertyInfo.Name))
                                    {
                                        object obj = serializedComponent2.PropertyValues[propertyInfo.Name];
                                        if (obj == null)
                                        {
                                            propertyInfo.SetValue(component, null);
                                        }
                                        else
                                        {
                                            if (obj.GetType() == typeof(JObject))
                                            {
                                                try
                                                {
                                                    propertyInfo.SetValue(component, ((JObject)obj).ToObject(propertyInfo.PropertyType));
                                                    goto IL_522;
                                                }
                                                catch
                                                {
                                                    goto IL_522;
                                                }
                                            }
                                            if (obj.GetType() == typeof(JArray))
                                            {
                                                try
                                                {
                                                    propertyInfo.SetValue(component, ((JArray)obj).ToObject(propertyInfo.PropertyType));
                                                    goto IL_522;
                                                }
                                                catch
                                                {
                                                    goto IL_522;
                                                }
                                            }
                                            bool isEnum = propertyInfo.PropertyType.IsEnum;
                                            bool flag2  = typeof(IConvertible).IsAssignableFrom(obj.GetType());
                                            propertyInfo.SetValue(component, isEnum ? Enum.ToObject(propertyInfo.PropertyType, obj) : (flag2 ? Convert.ChangeType(obj, propertyInfo.PropertyType) : obj));
                                        }
                                    }
                                }
                                else if (serializedComponent2.PropertyReferences.ContainsKey(propertyInfo.Name))
                                {
                                    bool       flag3   = propertyInfo.PropertyType == typeof(GameObject);
                                    GameObject object2 = YanSaveIdentifier.GetObject(serializedComponent2.FieldReferences[propertyInfo.Name]);
                                    if (!(object2 == null))
                                    {
                                        if (flag)
                                        {
                                            propertyInfo.SetValue(component, object2.GetComponent(propertyInfo.PropertyType));
                                        }
                                        else if (flag3)
                                        {
                                            propertyInfo.SetValue(component, object2);
                                        }
                                    }
                                }
                                else if (serializedComponent2.PropertyReferenceArrays.ContainsKey(propertyInfo.Name))
                                {
                                    bool          flag4       = typeof(Component[]).IsAssignableFrom(propertyInfo.PropertyType);
                                    bool          flag5       = typeof(GameObject[]).IsAssignableFrom(propertyInfo.PropertyType);
                                    List <string> list        = serializedComponent2.PropertyReferenceArrays[propertyInfo.Name];
                                    Type          elementType = propertyInfo.PropertyType.GetElementType();
                                    if (flag4)
                                    {
                                        IList list2 = Array.CreateInstance(elementType, list.Count);
                                        for (int l = 0; l < list.Count; l++)
                                        {
                                            YanSaveIdentifier.GetObject(list[l]);
                                            Component value = (@object != null) ? @object.GetComponent(elementType) : null;
                                            list2[l] = value;
                                        }
                                        propertyInfo.SetValue(component, list2);
                                    }
                                    else if (flag5)
                                    {
                                        IList list3 = Array.CreateInstance(elementType, list.Count);
                                        for (int m = 0; m < list.Count; m++)
                                        {
                                            GameObject object3 = YanSaveIdentifier.GetObject(list[m]);
                                            list3[m] = object3;
                                        }
                                        propertyInfo.SetValue(component, list3);
                                    }
                                }
                            }
                            IL_522 :;
                        }
                        foreach (FieldInfo fieldInfo in YanSave.GetCachedFields(type2))
                        {
                            bool flag6 = typeof(Component).IsAssignableFrom(fieldInfo.FieldType);
                            bool flag7 = typeof(Component[]).IsAssignableFrom(fieldInfo.FieldType);
                            bool flag8 = typeof(GameObject[]).IsAssignableFrom(fieldInfo.FieldType);
                            if (!flag7 && !flag8 && !flag6 && fieldInfo.FieldType != typeof(GameObject))
                            {
                                if (serializedComponent2.FieldValues.ContainsKey(fieldInfo.Name))
                                {
                                    object obj2 = serializedComponent2.FieldValues[fieldInfo.Name];
                                    if (obj2 == null)
                                    {
                                        fieldInfo.SetValue(component, null);
                                    }
                                    else
                                    {
                                        if (obj2.GetType() == typeof(JObject))
                                        {
                                            try
                                            {
                                                fieldInfo.SetValue(component, ((JObject)obj2).ToObject(fieldInfo.FieldType));
                                                goto IL_85E;
                                            }
                                            catch
                                            {
                                                goto IL_85E;
                                            }
                                        }
                                        if (obj2.GetType() == typeof(JArray))
                                        {
                                            try
                                            {
                                                fieldInfo.SetValue(component, ((JArray)obj2).ToObject(fieldInfo.FieldType));
                                                goto IL_85E;
                                            }
                                            catch
                                            {
                                                goto IL_85E;
                                            }
                                        }
                                        bool isEnum2 = fieldInfo.FieldType.IsEnum;
                                        bool flag9   = typeof(IConvertible).IsAssignableFrom(obj2.GetType());
                                        fieldInfo.SetValue(component, isEnum2 ? Enum.ToObject(fieldInfo.FieldType, obj2) : (flag9 ? Convert.ChangeType(obj2, fieldInfo.FieldType) : obj2));
                                    }
                                }
                            }
                            else if (serializedComponent2.FieldReferences.ContainsKey(fieldInfo.Name))
                            {
                                bool       flag10  = fieldInfo.FieldType == typeof(GameObject);
                                GameObject object4 = YanSaveIdentifier.GetObject(serializedComponent2.FieldReferences[fieldInfo.Name]);
                                if (!(object4 == null))
                                {
                                    if (flag6)
                                    {
                                        fieldInfo.SetValue(component, object4.GetComponent(fieldInfo.FieldType));
                                    }
                                    else if (flag10)
                                    {
                                        fieldInfo.SetValue(component, object4);
                                    }
                                }
                            }
                            else if (serializedComponent2.FieldReferenceArrays.ContainsKey(fieldInfo.Name))
                            {
                                List <string> list4        = serializedComponent2.FieldReferenceArrays[fieldInfo.Name];
                                Type          elementType2 = fieldInfo.FieldType.GetElementType();
                                if (flag7)
                                {
                                    IList list5 = Array.CreateInstance(elementType2, list4.Count);
                                    for (int n = 0; n < list4.Count; n++)
                                    {
                                        YanSaveIdentifier.GetObject(list4[n]);
                                        Component value2 = (@object != null) ? @object.GetComponent(elementType2) : null;
                                        list5[n] = value2;
                                    }
                                    fieldInfo.SetValue(component, list5);
                                }
                                else if (flag8)
                                {
                                    IList list6 = Array.CreateInstance(elementType2, list4.Count);
                                    for (int num = 0; num < list4.Count; num++)
                                    {
                                        GameObject object5 = YanSaveIdentifier.GetObject(list4[num]);
                                        list6[num] = object5;
                                    }
                                    fieldInfo.SetValue(component, list6);
                                }
                            }
                            IL_85E :;
                        }
                    }
                }
            }
        }
        Action onLoad = YanSave.OnLoad;

        if (onLoad == null)
        {
            return;
        }
        onLoad();
    }