public static void Load(TaskSerializationData taskData, BehaviorSource behaviorSource) { behaviorSource.EntryTask = null; behaviorSource.RootTask = null; behaviorSource.DetachedTasks = null; behaviorSource.Variables = null; Dictionary <string, object> dictionary; if (!DeserializeJSON.serializationCache.TryGetValue(taskData.JSONSerialization.GetHashCode(), out dictionary)) { dictionary = (MiniJSON.Deserialize(taskData.JSONSerialization) as Dictionary <string, object>); DeserializeJSON.serializationCache.Add(taskData.JSONSerialization.GetHashCode(), dictionary); } if (dictionary == null) { Debug.Log("Failed to deserialize"); return; } DeserializeJSON.taskIDs = new Dictionary <DeserializeJSON.TaskField, List <int> >(); Dictionary <int, Task> dictionary2 = new Dictionary <int, Task>(); DeserializeJSON.DeserializeVariables(behaviorSource, dictionary, taskData.fieldSerializationData.unityObjects); if (dictionary.ContainsKey("EntryTask")) { behaviorSource.EntryTask = DeserializeJSON.DeserializeTask(behaviorSource, dictionary["EntryTask"] as Dictionary <string, object>, ref dictionary2, taskData.fieldSerializationData.unityObjects); } if (dictionary.ContainsKey("RootTask")) { behaviorSource.RootTask = DeserializeJSON.DeserializeTask(behaviorSource, dictionary["RootTask"] as Dictionary <string, object>, ref dictionary2, taskData.fieldSerializationData.unityObjects); } if (dictionary.ContainsKey("DetachedTasks")) { List <Task> list = new List <Task>(); foreach (Dictionary <string, object> dict in dictionary["DetachedTasks"] as IEnumerable) { list.Add(DeserializeJSON.DeserializeTask(behaviorSource, dict, ref dictionary2, taskData.fieldSerializationData.unityObjects)); } behaviorSource.DetachedTasks = list; } if (DeserializeJSON.taskIDs != null && DeserializeJSON.taskIDs.Count > 0) { foreach (DeserializeJSON.TaskField current in DeserializeJSON.taskIDs.Keys) { List <int> list2 = DeserializeJSON.taskIDs[current]; Type fieldType = current.fieldInfo.FieldType; if (current.fieldInfo.FieldType.IsArray) { int num = 0; for (int i = 0; i < list2.Count; i++) { Task task = dictionary2[list2[i]]; if (task.GetType().Equals(fieldType.GetElementType()) || task.GetType().IsSubclassOf(fieldType.GetElementType())) { num++; } } Array array = Array.CreateInstance(fieldType.GetElementType(), num); int num2 = 0; for (int j = 0; j < list2.Count; j++) { Task task2 = dictionary2[list2[j]]; if (task2.GetType().Equals(fieldType.GetElementType()) || task2.GetType().IsSubclassOf(fieldType.GetElementType())) { array.SetValue(task2, num2); num2++; } } current.fieldInfo.SetValue(current.task, array); } else { Task task3 = dictionary2[list2[0]]; if (task3.GetType().Equals(current.fieldInfo.FieldType) || task3.GetType().IsSubclassOf(current.fieldInfo.FieldType)) { current.fieldInfo.SetValue(current.task, task3); } } } DeserializeJSON.taskIDs = null; } }
public static Task DeserializeTask(BehaviorSource behaviorSource, Dictionary <string, object> dict, ref Dictionary <int, Task> IDtoTask, List <UnityEngine.Object> unityObjects) { Task task = null; try { Type type = TaskUtility.GetTypeWithinAssembly(dict["ObjectType"] as string); if (type == null) { if (dict.ContainsKey("Children")) { type = typeof(UnknownParentTask); } else { type = typeof(UnknownTask); } } task = (TaskUtility.CreateInstance(type) as Task); } catch (Exception) { } if (task == null) { Debug.Log("Error: task is null of type " + dict["ObjectType"]); return(null); } task.Owner = (behaviorSource.Owner.GetObject() as Behavior); task.ID = Convert.ToInt32(dict["ID"]); object obj; if (dict.TryGetValue("Name", out obj)) { task.FriendlyName = (string)obj; } if (dict.TryGetValue("Instant", out obj)) { task.IsInstant = Convert.ToBoolean(obj); } IDtoTask.Add(task.ID, task); task.NodeData = DeserializeJSON.DeserializeNodeData(dict["NodeData"] as Dictionary <string, object>, task); if (task.GetType().Equals(typeof(UnknownTask)) || task.GetType().Equals(typeof(UnknownParentTask))) { if (!task.FriendlyName.Contains("Unknown ")) { task.FriendlyName = string.Format("Unknown {0}", task.FriendlyName); } if (!task.NodeData.Comment.Contains("Loaded from an unknown type. Was a task renamed or deleted?")) { task.NodeData.Comment = string.Format("Loaded from an unknown type. Was a task renamed or deleted?{0}", (!task.NodeData.Comment.Equals(string.Empty)) ? string.Format("\0{0}", task.NodeData.Comment) : string.Empty); } } DeserializeJSON.DeserializeObject(task, task, dict, behaviorSource, unityObjects); if (task is ParentTask && dict.TryGetValue("Children", out obj)) { ParentTask parentTask = task as ParentTask; if (parentTask != null) { foreach (Dictionary <string, object> dict2 in obj as IEnumerable) { Task child = DeserializeJSON.DeserializeTask(behaviorSource, dict2, ref IDtoTask, unityObjects); int index = (parentTask.Children != null) ? parentTask.Children.Count : 0; parentTask.AddChild(child, index); } } } return(task); }