internal static AICanvas Deserialize(AIUI host, string data) { var canvasElement = SerializationMaster.Deserialize(data); var canvas = new AICanvas { offset = canvasElement.ValueOrDefault <Vector2>(ElementName.Offset), zoom = canvasElement.ValueOrDefault(ElementName.Zoom, 1f) }; var selectorViews = canvasElement.Elements(ElementName.SelectorView); foreach (var sve in selectorViews) { var sv = ReadSelectorView(sve, host); canvas.views.Add(sv); } var linkViews = canvasElement.Elements(ElementName.AILinkView); foreach (var lve in linkViews) { var lv = ReadAILinkView(lve, host); canvas.views.Add(lv); } return(canvas); }
internal static string Serialize(AICanvas canvas) { var root = new StageElement( ElementName.Canvas, SerializationMaster.Stage(ElementName.Offset, canvas.offset), SerializationMaster.ToStageValue(ElementName.Zoom, canvas.zoom)); var views = canvas.views; int svCount = views.Count; for (int i = 0; i < svCount; i++) { var view = views[i]; StageElement viewElement; if (view is SelectorView) { viewElement = WriteSelectorView((SelectorView)view); } else if (view is AILinkView) { viewElement = WriteAILinkView((AILinkView)view); } else { throw new NotImplementedException("The view type has not been implemented for serialization."); } root.Add(viewElement); } return(SerializationMaster.Serialize(root)); }
private static StageElement WriteSelectorView(SelectorView sv) { var sve = new StageElement( ElementName.SelectorView, SerializationMaster.Stage(ElementName.ViewArea, sv.viewArea)); sve.AddTextValue(ElementName.Name, sv.name); sve.AddTextValue(ElementName.Description, sv.description); var qualifierViews = sv.qualifierViews; int qvCount = qualifierViews.Count; for (int i = 0; i < qvCount; i++) { var qv = WriteQualifierView(qualifierViews[i]); sve.Add(qv); } var defQv = WriteQualifierView(sv.defaultQualifierView); sve.Add(defQv); return(sve); }
private static void DeserializeAction(StageElement aiElement, StageElement viewElement, AIUI targetUI) { var parent = targetUI.currentQualifier; if (parent == null) { EditorUtility.DisplayDialog("Invalid Action", "You must select a Qualifier before pasting an Action.", "Ok"); } else if (parent.actionView != null) { if (!EditorUtility.DisplayDialog("Confirm Replace", "The selected Qualifier already has an action, do you wish to replace it?", "Yes", "No")) { return; } } var requiresInit = new List <IInitializeAfterDeserialization>(); var actionView = ReadActionView(viewElement, parent); var action = SerializationMaster.Unstage <IAction>(aiElement, requiresInit); actionView.action = action; //Do the post deserialize initialization here if (requiresInit.Count == 1) { requiresInit[0].Initialize(targetUI.ai); } targetUI.SetAction(actionView, parent); parent.PruneBrokenConnections(); }
public object UnstageValue(StageItem item, Type targetType) { Type elementType; StageItem[] array = ((StageList)item).Items().ToArray <StageItem>(); if (targetType.IsArray) { elementType = targetType.GetElementType(); Array arrays = Array.CreateInstance(elementType, (int)array.Length); for (int i = 0; i < (int)array.Length; i++) { object obj = SerializationMaster.Unstage(array[i], elementType); arrays.SetValue(obj, i); } return(arrays); } elementType = (!targetType.IsGenericType ? typeof(object) : targetType.GetGenericArguments()[0]); IList lists = Activator.CreateInstance(targetType, new object[] { (int)array.Length }) as IList; for (int j = 0; j < (int)array.Length; j++) { object obj1 = SerializationMaster.Unstage(array[j], elementType); lists.Add(obj1); } return(lists); }
private static void DeserializeSelectorViews(StageElement[] aiElements, StageElement[] viewElements, AIUI targetUI, Vector2 referencePos, Vector2 mousePos, List <TopLevelView> newViews, bool regenIds) { var requiresInit = new List <IInitializeAfterDeserialization>(); var selectorViews = new List <SelectorView>(aiElements.Length); for (int i = 0; i < aiElements.Length; i++) { var selectorView = ReadSelectorView(viewElements[i], targetUI); var selector = SerializationMaster.Unstage <Selector>(aiElements[i], requiresInit); targetUI.canvas.views.Add(selectorView); targetUI.ai.AddSelector(selector); selectorView.Reconnect(selector); selectorViews.Add(selectorView); newViews.Add(selectorView); targetUI.undoRedo.Do(new AddSelectorOperation(targetUI, selectorView)); } //Set the root if pasting to a blank canvas if (targetUI.ai.rootSelector == null) { targetUI.ai.rootSelector = selectorViews[0].selector; targetUI.undoRedo.Do(new SetRootOperation(targetUI, null, selectorViews[0].selector)); } //Do the post deserialize initialization here var count = requiresInit.Count; for (int i = 0; i < count; i++) { requiresInit[i].Initialize(targetUI.ai); } //Finally adjust the positioning of pasted elements and prune broken connections, i.e. SelectorAction that no longer points to a selector for (int i = 0; i < aiElements.Length; i++) { var selectorView = selectorViews[i]; selectorView.PruneBrokenConnections(); if (regenIds) { //We also regen the id since this allows for pasting the same copy multiple times selectorView.selector.RegenerateId(); } // first treat the top-left-most selector as the origin point (reference pos) selectorView.viewArea.position -= referencePos; // secondly add the mouse offset to the position so as to paste it in where the mouse is at selectorView.viewArea.position += mousePos; } }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var val = (Vector2)value; return(new StageElement( name, SerializationMaster.ToStageAttribute("x", val.x), SerializationMaster.ToStageAttribute("y", val.y))); }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var val = (Color)value; return(new StageElement( name, SerializationMaster.ToStageAttribute("r", val.r), SerializationMaster.ToStageAttribute("g", val.g), SerializationMaster.ToStageAttribute("b", val.b), SerializationMaster.ToStageAttribute("a", val.a))); }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var val = (Rect)value; return(new StageElement( name, SerializationMaster.ToStageAttribute("left", val.xMin), SerializationMaster.ToStageAttribute("top", val.yMin), SerializationMaster.ToStageAttribute("width", val.width), SerializationMaster.ToStageAttribute("height", val.height))); }
private static StageElement WriteAILinkView(AILinkView lv) { var lve = new StageElement( ElementName.AILinkView, SerializationMaster.Stage(ElementName.ViewArea, lv.viewArea), SerializationMaster.ToStageAttribute(AttributeName.Id, lv.aiId)); lve.AddTextValue(ElementName.Name, lv.name); lve.AddTextValue(ElementName.Description, lv.description); return(lve); }
public StageItem StageValue(string name, object value) { Keyframe[] keyframeArray = ((AnimationCurve)value).keys; StageElement stageElement = new StageElement(name); for (int i = 0; i < (int)keyframeArray.Length; i++) { Keyframe keyframe = keyframeArray[i]; stageElement.Add(new StageElement("key", new StageItem[] { SerializationMaster.ToStageAttribute("time", keyframe.time), SerializationMaster.ToStageAttribute("value", keyframe.@value), SerializationMaster.ToStageAttribute("inTangent", keyframe.inTangent), SerializationMaster.ToStageAttribute("outTangent", keyframe.outTangent), SerializationMaster.ToStageAttribute("tangentMode", keyframe.tangentMode) })); } return(stageElement); }
public StageItem StageValue(string name, object value) { IDictionary dictionaries = value as IDictionary; int count = dictionaries.Count; StageList stageList = new StageList(name); foreach (object key in dictionaries.Keys) { stageList.Add(new StageElement(string.Empty, new StageItem[] { SerializationMaster.Stage("key", key), SerializationMaster.Stage("value", dictionaries[key]) })); } return(stageList); }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var val = (Bounds)value; return(new StageElement( name, SerializationMaster.ToStageAttribute("center.x", val.center.x), SerializationMaster.ToStageAttribute("center.y", val.center.y), SerializationMaster.ToStageAttribute("center.z", val.center.z), SerializationMaster.ToStageAttribute("size.x", val.size.x), SerializationMaster.ToStageAttribute("size.y", val.size.y), SerializationMaster.ToStageAttribute("size.z", val.size.z))); }
public StageItem StageValue(string name, object value) { IList lists = value as IList; int count = lists.Count; StageList stageList = new StageList(name); for (int i = 0; i < count; i++) { StageItem stageItem = SerializationMaster.Stage("Item", lists[i]); stageList.Add(stageItem); } return(stageList); }
private bool LoadFrom(AIStorage data, bool refreshState) { _aiStorage = data; try { if (EditorApplication.isPlaying) { _ai = _visualizedAI = AIManager.GetAI(new Guid(data.aiId)); } else { _ai = _visualizedAI = SerializationMaster.Deserialize <UtilityAI>(_aiStorage.configuration); } this.canvas = GuiSerializer.Deserialize(this, _aiStorage.editorConfiguration); } catch (Exception e) { if (EditorUtility.DisplayDialog("Load Error", "The AI could not be loaded, deserialization failed - see the console for details.\n\nDo you wish to open the AI repair tool?.", "Yes", "No")) { RepairWindow.ShowWindow(data.name, data.aiId); } Debug.LogWarning("Failed to load AI: " + e.Message); return(false); } var selectorViews = this.canvas.selectorViews.ToArray(); int selectorCount = _ai.selectorCount; if (!VerifyCountMatch(selectorCount, selectorViews.Length)) { return(false); } for (int i = 0; i < selectorCount; i++) { if (!selectorViews[i].Reconnect(_ai[i])) { return(false); } } if (refreshState) { this.inspectorState.Refresh(); } return(true); }
internal static string Serialize(IList <TopLevelView> items) { var aiPart = new StageElement(ElementName.AIPart); var uiPart = new StageElement(ElementName.UIPart); var root = new StageElement(ElementName.ViewSnippet, aiPart, uiPart); root.AddTextAttribute(AttributeName.SnippetType, ElementName.ViewSnippet); var referencePos = new Vector2(float.MaxValue, float.MaxValue); for (int i = 0; i < items.Count; i++) { var view = items[i]; var selectorView = view as SelectorView; if (selectorView != null) { var item = selectorView.selector; var aiItem = SerializationMaster.Stage(typeof(Selector).Name, item); aiPart.Add(aiItem); var viewItem = WriteSelectorView(selectorView); uiPart.Add(viewItem); //Even though reid also happens on paste we have to do it here as well, otherwise copying linked selectors will fail. item.RegenerateId(); } var linkView = view as AILinkView; if (linkView != null) { var viewItem = WriteAILinkView(linkView); uiPart.Add(viewItem); } if (view.viewArea.xMin < referencePos.x) { referencePos.x = view.viewArea.xMin; } if (view.viewArea.yMin < referencePos.y) { referencePos.y = view.viewArea.yMin; } } uiPart.AddValue(ElementName.ReferencePosition, referencePos); return(SerializationMaster.Serialize(root)); }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var list = value as IList; var count = list.Count; var listElement = new StageList(name); for (int i = 0; i < count; i++) { var item = SerializationMaster.Stage("Item", list[i]); listElement.Add(item); } return(listElement); }
private StageElement GetEditorDoc(int index) { if (_stagedEditorConfigs == null) { _stagedEditorConfigs = new StageElement[_ais.Length]; } if (_stagedEditorConfigs[index] == null) { _stagedEditorConfigs[index] = SerializationMaster.Deserialize(_ais[index].editorConfiguration); } return(_stagedEditorConfigs[index]); }
/// <summary> /// Unstages the value. /// </summary> /// <param name="item">The stage item to unstage.</param> /// <param name="targetType">Type of the value.</param> /// <returns> /// The unstaged value. /// </returns> public object UnstageValue(StageItem item, Type targetType) { var el = (StageElement)item; return(new Bounds( new Vector3( SerializationMaster.FromString <float>(el.Attribute("center.x").value), SerializationMaster.FromString <float>(el.Attribute("center.y").value), SerializationMaster.FromString <float>(el.Attribute("center.z").value)), new Vector3( SerializationMaster.FromString <float>(el.Attribute("size.x").value), SerializationMaster.FromString <float>(el.Attribute("size.y").value), SerializationMaster.FromString <float>(el.Attribute("size.z").value)))); }
public object UnstageValue(StageItem item, Type targetType) { StageElement[] array = ((StageList)item).Elements().ToArray <StageElement>(); Type[] genericArguments = targetType.GetGenericArguments(); Type type = genericArguments[0]; Type type1 = genericArguments[1]; IDictionary dictionaries = Activator.CreateInstance(targetType, new object[] { (int)array.Length }) as IDictionary; for (int i = 0; i < (int)array.Length; i++) { StageElement stageElement = array[i]; object obj = SerializationMaster.Unstage(stageElement.Item("key"), type); object obj1 = SerializationMaster.Unstage(stageElement.Item("value"), type1); dictionaries.Add(obj, obj1); } return(dictionaries); }
internal void Save(string newName) { if (_ai == null || _ai.rootSelector == null) { return; } // new name is not null or empty when using 'Save As' if (!string.IsNullOrEmpty(newName)) { this.name = StoredAIs.EnsureValidName(newName, _aiStorage); // If we are saving under a new name (as opposed to saving a new AI), we need to save copy of the current AI with new Ids. if (_aiStorage != null) { _aiStorage = null; _ai.RegenerateIds(); } } bool saveNew = (_aiStorage == null); if (saveNew) { _aiStorage = AIStorage.Create(_ai.id.ToString(), this.name); StoredAIs.AIs.Add(_aiStorage); AssetPath.EnsurePath(AIGeneralSettings.instance.storagePath); var assetPath = AssetPath.Combine(AIGeneralSettings.instance.storagePath, this.name + ".asset"); AssetDatabase.CreateAsset(_aiStorage, assetPath); } _aiStorage.version = _aiVersion.version; _aiStorage.configuration = SerializationMaster.Serialize(_ai); _aiStorage.editorConfiguration = GuiSerializer.Serialize(this.canvas); EditorUtility.SetDirty(_aiStorage); AssetDatabase.SaveAssets(); this.isDirty = false; _undoRedo.SetSavePoint(); if (saveNew && UserSettings.instance.autoGenerateNameMap) { AINameMapGenerator.WriteNameMapFile(); } }
internal static string Serialize(ActionView action) { var aiPart = new StageElement(ElementName.AIPart); var uiPart = new StageElement(ElementName.UIPart); var root = new StageElement(ElementName.ActionSnippet, aiPart, uiPart); root.AddTextAttribute(AttributeName.SnippetType, ElementName.ActionSnippet); var aiItem = SerializationMaster.Stage(ElementName.Action, action.action); aiPart.Add(aiItem); var viewItem = WriteActionView(action); uiPart.Add(viewItem); return(SerializationMaster.Serialize(root)); }
internal static string Serialize(QualifierView qualifier) { var aiPart = new StageElement(ElementName.AIPart); var uiPart = new StageElement(ElementName.UIPart); var root = new StageElement(ElementName.QualifierSnippet, aiPart, uiPart); root.AddTextAttribute(AttributeName.SnippetType, ElementName.QualifierSnippet); var aiItem = SerializationMaster.Stage(ElementName.Qualifier, qualifier.qualifier); aiPart.Add(aiItem); var viewItem = WriteQualifierView(qualifier); uiPart.Add(viewItem); return(SerializationMaster.Serialize(root)); }
/// <summary> /// Stages the value. /// </summary> /// <param name="name">The name to give the resulting <see cref="StageItem" />.</param> /// <param name="value">The value to stage</param> /// <returns> /// The element containing the staged value. /// </returns> public StageItem StageValue(string name, object value) { var dic = value as IDictionary; var count = dic.Count; var listElement = new StageList(name); foreach (var key in dic.Keys) { var item = new StageElement( string.Empty, SerializationMaster.Stage("key", key), SerializationMaster.Stage("value", dic[key])); listElement.Add(item); } return(listElement); }
/// <summary> /// Unstages the value. /// </summary> /// <param name="item">The stage item to unstage.</param> /// <param name="targetType">Type of the value.</param> /// <returns> /// The unstaged value. /// </returns> public object UnstageValue(StageItem item, Type targetType) { var el = (StageList)item; var items = el.Items().ToArray(); Type itemType; if (targetType.IsArray) { itemType = targetType.GetElementType(); var arr = Array.CreateInstance(itemType, items.Length); for (int i = 0; i < items.Length; i++) { var itemValue = SerializationMaster.Unstage(items[i], itemType); arr.SetValue(itemValue, i); } return(arr); } //A list if (targetType.IsGenericType) { itemType = targetType.GetGenericArguments()[0]; } else { itemType = typeof(object); } var list = Activator.CreateInstance(targetType, items.Length) as IList; for (int i = 0; i < items.Length; i++) { var itemValue = SerializationMaster.Unstage(items[i], itemType); list.Add(itemValue); } return(list); }
private static void ReadAndInit(AIManager.AIData data) { List <IInitializeAfterDeserialization> initializeAfterDeserializations = new List <IInitializeAfterDeserialization>(); try { data.ai = SerializationMaster.Deserialize <UtilityAI>(data.storedData.configuration, initializeAfterDeserializations); data.ai.name = data.storedData.name; } catch (Exception exception1) { Exception exception = exception1; UnityEngine.Debug.LogWarning(string.Format("Unable to load the AI: {0}. Additional details: {1}\n{2}", data.storedData.name, exception.Message, exception.StackTrace)); return; } int count = initializeAfterDeserializations.Count; for (int i = 0; i < count; i++) { initializeAfterDeserializations[i].Initialize(data.ai); } }
public override void OnInspectorGUI() { var ai = (AIStorage)this.target; EditorGUILayout.Separator(); EditorGUILayout.LabelField("Apex AI: " + ai.name); this.serializedObject.Update(); EditorGUILayout.Separator(); EditorGUILayout.PropertyField(_description); this.serializedObject.ApplyModifiedProperties(); if (GUILayout.Button("Open")) { AIEditorWindow.Open(ai.aiId); } EditorGUILayout.Separator(); _debug = EditorGUILayout.ToggleLeft("Show debug options", _debug); if (_debug) { if (GUILayout.Button("Copy AI Configuration to clipboard")) { try { var json = new StringBuilder(); var aiData = SerializationMaster.Deserialize(ai.configuration); var guiData = SerializationMaster.Deserialize(ai.editorConfiguration); json.AppendLine(SerializationMaster.Serialize(aiData, true)); json.AppendLine(SerializationMaster.Serialize(guiData, true)); EditorGUIUtility.systemCopyBuffer = json.ToString(); } catch { EditorUtility.DisplayDialog("Error", "Copying failed, unable to read AI Configuration.", "OK"); } } } }
/// <summary> /// Unstages the value. /// </summary> /// <param name="item">The stage item to unstage.</param> /// <param name="targetType">Type of the value.</param> /// <returns> /// The unstaged value. /// </returns> public object UnstageValue(StageItem item, Type targetType) { var el = (StageList)item; var elements = el.Elements().ToArray(); var typeArgs = targetType.GetGenericArguments(); Type keyType = typeArgs[0]; Type valueType = typeArgs[1]; var dic = Activator.CreateInstance(targetType, elements.Length) as IDictionary; for (int i = 0; i < elements.Length; i++) { var element = elements[i]; var key = SerializationMaster.Unstage(element.Item("key"), keyType); var value = SerializationMaster.Unstage(element.Item("value"), valueType); dic.Add(key, value); } return(dic); }
private static void ReadAndInit(AIData data) { var requiresInit = new List <IInitializeAfterDeserialization>(); try { data.ai = SerializationMaster.Deserialize <UtilityAI>(data.storedData.configuration, requiresInit); data.ai.name = data.storedData.name; } catch (Exception e) { Debug.LogWarning(string.Format("Unable to load the AI: {0}. Additional details: {1}\n{2}", data.storedData.name, e.Message, e.StackTrace)); return; } var initCount = requiresInit.Count; for (int i = 0; i < initCount; i++) { requiresInit[i].Initialize(data.ai); } }
internal static void DeserializeSnippet(string data, AIUI targetUI, Vector2 mousePos, bool regenIds) { var root = SerializationMaster.Deserialize(data); var snippetType = root.AttributeValue <string>(AttributeName.SnippetType); var aiElements = root.Element(ElementName.AIPart).Elements().ToArray(); if (snippetType == ElementName.ViewSnippet) { var uiPart = root.Element(ElementName.UIPart); var referencePos = uiPart.ValueOrDefault(ElementName.ReferencePosition, Vector2.zero); var newViews = new List <TopLevelView>(uiPart.Items().Count()); using (targetUI.undoRedo.bulkOperation) { //Deserialize links first so that connections are not lost var linkElements = uiPart.Elements(ElementName.AILinkView).ToArray(); DeserializeAILinkViews(linkElements, targetUI, referencePos, mousePos, newViews); var selectorElements = uiPart.Elements(ElementName.SelectorView).ToArray(); DeserializeSelectorViews(aiElements, selectorElements, targetUI, referencePos, mousePos, newViews, regenIds); } targetUI.MultiSelectViews(newViews); return; } var viewElement = root.Element(ElementName.UIPart).Elements().First(); if (snippetType == ElementName.QualifierSnippet) { DeserializeQualifier(aiElements[0], viewElement, targetUI); } else if (snippetType == ElementName.ActionSnippet) { DeserializeAction(aiElements[0], viewElement, targetUI); } }