Advertisement
Guest User

Untitled

a guest
Sep 10th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.25 KB | None | 0 0
  1. [MenuItem("Custom/Save RAINpuppet")]
  2.     public static void SaveRAINpuppet()
  3.     {
  4.         GameObject obj = GameObject.Find("RAINPuppet1");
  5.         if (null == obj)
  6.         {
  7.             Debug.LogError("Save failed - RAINpuppet1 object not found");
  8.             return;
  9.         }
  10.  
  11.         XmlDocument doc = new XmlDocument();
  12.         XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  13.         doc.AppendChild(declaration);
  14.  
  15.         XmlElement rainElems = doc.CreateElement("RAINComponents");
  16.         doc.AppendChild(rainElems);
  17.  
  18.         RAIN.Core.RAINComponent[] rainComponents = obj.GetComponentsInChildren<RAIN.Core.RAINComponent>();
  19.         Dictionary<Type, int> componentTypesCounter = new Dictionary<Type, int>();
  20.  
  21.         foreach (RAIN.Core.RAINComponent component in rainComponents)
  22.         {
  23.             int count;
  24.             if (!componentTypesCounter.TryGetValue(component.GetType(), out count))
  25.                 componentTypesCounter.Add(component.GetType(), 1);
  26.             else
  27.                 componentTypesCounter[component.GetType()] = count + 1;
  28.  
  29.             component.Serialize();
  30.  
  31.             XmlElement componentElem = doc.CreateElement("RAINComponent");
  32.             componentElem.SetAttribute("type", component.GetType().ToString());
  33.             componentElem.SetAttribute("typeIndex", (componentTypesCounter[component.GetType()] - 1).ToString());
  34.             rainElems.AppendChild(componentElem);
  35.  
  36.             byte[] data = System.Text.Encoding.UTF8.GetBytes(component.DataSerializer.SerializedData);
  37.             string base64data = Convert.ToBase64String(data);
  38.  
  39.             XmlElement componentData = doc.CreateElement("data");
  40.             componentElem.AppendChild(componentData);
  41.             componentData.InnerText = base64data;
  42.  
  43.             XmlElement componentCustomData = doc.CreateElement("customData");
  44.             componentElem.AppendChild(componentCustomData);
  45.  
  46.             foreach (RAIN.Serialization.FieldSerializer.CustomSerializedData c in component.DataSerializer.SerializedCustomData)
  47.             {
  48.                 XmlElement customDataEntry = doc.CreateElement("entry");
  49.                 componentCustomData.AppendChild(customDataEntry);
  50.  
  51.                 string base64customData = Convert.ToBase64String(c.Data);
  52.                 customDataEntry.InnerText = base64customData;
  53.  
  54.             }
  55.  
  56.             XmlElement componentObjectsData = doc.CreateElement("objectsData");
  57.             componentElem.AppendChild(componentObjectsData);
  58.  
  59.             foreach (UnityEngine.Object o in component.DataSerializer.SerializedGameObjects)
  60.             {
  61.                 XmlElement objectDataEntry = doc.CreateElement("entry");
  62.                 componentObjectsData.AppendChild(objectDataEntry);
  63.  
  64.                 objectDataEntry.SetAttribute("type", o.GetType().ToString());
  65.                 objectDataEntry.SetAttribute("name", o.name);
  66.             }
  67.         }
  68.  
  69.         using (var writer = new XmlTextWriter("D:\\data.txt", null))
  70.         {
  71.             writer.Formatting = Formatting.Indented;
  72.             doc.WriteTo(writer);
  73.             writer.Flush();
  74.             writer.Close();
  75.         }
  76.  
  77.  
  78.     }
  79.  
  80. [MenuItem("Custom/Load RAINpuppet")]
  81.     public static void LoadRAINpuppet()
  82.     {
  83.         GameObject obj = GameObject.Find("RAINPuppet1");
  84.         if (null == obj)
  85.         {
  86.             Debug.LogError("Load failed - RAINpuppet1 object not found");
  87.             return;
  88.         }
  89.  
  90.         RAIN.Core.AIRig[] aiRigs = obj.GetComponentsInChildren<RAIN.Core.AIRig>();
  91.         RAIN.Entities.EntityRig[] entityRigs = obj.GetComponentsInChildren<RAIN.Entities.EntityRig>();
  92.         RAIN.Navigation.NavMesh.NavMeshRig[] navMeshRigs = obj.GetComponentsInChildren<RAIN.Navigation.NavMesh.NavMeshRig>();
  93.         RAIN.Navigation.Targets.NavigationTargetRig[] navTargetRigs = obj.GetComponentsInChildren<RAIN.Navigation.Targets.NavigationTargetRig>();
  94.         RAIN.Navigation.Waypoints.WaypointRig[] waypointRigs = obj.GetComponentsInChildren<RAIN.Navigation.Waypoints.WaypointRig>();
  95.  
  96.         RAIN.Core.RAINComponent component = (aiRigs.Length > 0) ? aiRigs.ElementAt(0) : null;
  97.         component = (null == component && entityRigs.Length > 0) ? entityRigs.ElementAt(0) : component;
  98.         component = (null == component && navMeshRigs.Length > 0) ? navMeshRigs.ElementAt(0) : component;
  99.         component = (null == component && navTargetRigs.Length > 0) ? navTargetRigs.ElementAt(0) : component;
  100.         component = (null == component && waypointRigs.Length > 0) ? waypointRigs.ElementAt(0) : component;
  101.  
  102.         if (null == component)
  103.         {
  104.             Debug.LogError("no RAIN component found on GameObject \"RAINPuppet1\"!");
  105.             return;
  106.         }
  107.  
  108.         GameObject[] gos = GameObject.FindObjectsOfType<GameObject>();
  109.  
  110.         using (System.IO.StreamReader dataReader = new System.IO.StreamReader("D:\\data.txt"))
  111.         {
  112.             string data = dataReader.ReadToEnd();
  113.             dataReader.Close();          
  114.  
  115.             XmlDocument doc = new XmlDocument();
  116.             doc.LoadXml(data);
  117.            
  118.             XmlNodeList nodes = doc.GetElementsByTagName("RAINComponent");
  119.             foreach (XmlNode node in nodes)
  120.             {
  121.                 XmlNode typeNode = node.Attributes.GetNamedItem("type");
  122.                 XmlNode typeIndexNode = node.Attributes.GetNamedItem("typeIndex");
  123.                 XmlNodeList componentNodes = node.ChildNodes;
  124.  
  125.                 string componentData = "";
  126.                 List<RAIN.Serialization.FieldSerializer.CustomSerializedData> customData = new List<RAIN.Serialization.FieldSerializer.CustomSerializedData>();
  127.                 List<UnityEngine.Object> objectsData = new List<UnityEngine.Object>();
  128.  
  129.                 foreach (XmlNode n in componentNodes)
  130.                 {
  131.                     switch (n.Name)
  132.                     {
  133.                         case "data":
  134.                             componentData = n.InnerText;
  135.                             byte[] nodeData = Convert.FromBase64String(componentData);
  136.                             componentData = System.Text.Encoding.UTF8.GetString(nodeData);
  137.                             break;
  138.  
  139.                         case "customData":
  140.                             XmlNodeList customDataNodes = n.ChildNodes;
  141.                             foreach(XmlNode no in customDataNodes)
  142.                             {
  143.                                 byte[] customRawData = Convert.FromBase64String(no.InnerText);
  144.                                 RAIN.Serialization.FieldSerializer.CustomSerializedData c = new RAIN.Serialization.FieldSerializer.CustomSerializedData(customRawData);
  145.                                 customData.Add(c);
  146.                             }
  147.                             break;
  148.  
  149.                         case "objectsData":
  150.                             XmlNodeList objectsDataNodes = n.ChildNodes;
  151.                             foreach (XmlNode no in objectsDataNodes)
  152.                             {
  153.                                 string name, type;
  154.                                 name = type = "";
  155.  
  156.                                 XmlAttributeCollection noAttrs = no.Attributes;
  157.                                 foreach (XmlAttribute a in noAttrs)
  158.                                 {
  159.                                     switch (a.Name)
  160.                                     {
  161.                                         case "name":
  162.                                             name = a.Value;
  163.                                             break;
  164.  
  165.                                         case "type":
  166.                                             type = a.Value;
  167.                                             break;
  168.  
  169.                                         default:
  170.                                             Debug.Log("unknown object entry attribute " + a.Name);
  171.                                             break;
  172.                                     }
  173.                                 }
  174.  
  175.                                 //GameObject go = GameObject.Find(name);
  176.                                 GameObject go = null;
  177.                                 foreach (GameObject g in gos)
  178.                                 {
  179.  
  180.                                     if (g.name == name )
  181.                                         go = g;
  182.                                 }
  183.  
  184.                                 if (go != null)
  185.                                 {
  186.                                     switch (type)
  187.                                     {
  188.                                         case "UnityEngine.GameObject":
  189.                                             objectsData.Add(go);
  190.                                             break;
  191.  
  192.                                         case "UnityEngine.Transform":
  193.                                             objectsData.Add(go.transform);
  194.                                             break;
  195.  
  196.                                         default:
  197.                                             Debug.Log(type);
  198.                                             Debug.LogError("unknown gameobject type");
  199.                                             break;
  200.                                     }
  201.                                 }
  202.                             }
  203.                             break;
  204.  
  205.                         default:
  206.                             Debug.LogError("Unknown node " + n.Name + " in RAIN component!");
  207.                             break;
  208.                     }
  209.                 }            
  210.  
  211.                
  212.                
  213.                 int index = Convert.ToInt32(typeIndexNode.Value);
  214.                 switch (typeNode.Value)
  215.                 {
  216.                     case "RAIN.Core.AIRig":
  217.                         if (index >= aiRigs.Length)
  218.                             Debug.LogError("RAINComponent AIRig with index " + index.ToString() + " was not found in the parent object!");
  219.                         if (index >= 0 && index < aiRigs.Length)
  220.                         {
  221.                             aiRigs.ElementAt(index).AI.WorkingMemory.Clear();
  222.                             aiRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  223.                             //aiRigs.ElementAt(index).ApplySerializationChanges();
  224.                         }
  225.                         break;
  226.  
  227.                     case "RAIN.Entities.EntityRig":
  228.                         if (index >= entityRigs.Length)
  229.                             Debug.LogError("RAINComponent EntityRig with index " + index.ToString() + " was not found in the parent object!");
  230.  
  231.                         if (index >= 0 && index < entityRigs.Length)
  232.                         {
  233.                             entityRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  234.                             //entityRigs.ElementAt(index).ApplySerializationChanges();
  235.                         }
  236.                         break;
  237.  
  238.                     case "RAIN.Navigation.NavMesh.NavMeshRig":
  239.                         if (index >= navMeshRigs.Length)
  240.                             Debug.LogError("RAINComponent NavMeshRig with index " + index.ToString() + " was not found in the parent object!");
  241.  
  242.                         if (index >= 0 && index < navMeshRigs.Length)
  243.                         {
  244.                             navMeshRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  245.                             //navMeshRigs.ElementAt(index).ApplySerializationChanges();
  246.                         }
  247.                         break;
  248.  
  249.                     case "RAIN.Navigation.Targets.NavigationTargetRig":
  250.                         if (index >= navTargetRigs.Length)
  251.                             Debug.LogError("RAINComponent NavigationTargetRig with index " + index.ToString() + " was not found in the parent object!");
  252.  
  253.                         if (index >= 0 && index < navTargetRigs.Length)
  254.                         {
  255.                             navTargetRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  256.                             //navTargetRigs.ElementAt(index).ApplySerializationChanges();
  257.                         }
  258.                         break;
  259.  
  260.                     case "RAIN.Navigation.Waypoints.WaypointRig":
  261.                         if (index >= waypointRigs.Length)
  262.                             Debug.LogError("RAINComponent WaypointRig with index " + index.ToString() + " was not found in the parent object!");
  263.  
  264.                         if (index >= 0 && index < waypointRigs.Length)
  265.                         {
  266.                             waypointRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  267.                             //waypointRigs.ElementAt(index).ApplySerializationChanges();
  268.                         }
  269.                         break;
  270.  
  271.                     default:
  272.                         Debug.LogError("unknown RAINComponent type");
  273.                         break;
  274.                 }
  275.  
  276.             }
  277.         }
  278.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement