Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 23.90 KB | None | 0 0
  1. public static Type GetType(string TypeName)
  2.     {
  3.  
  4.         var type = Type.GetType(TypeName);
  5.  
  6.         if (type != null)
  7.             return type;
  8.  
  9.         var assemblyName = TypeName.Substring(0, TypeName.IndexOf('.'));
  10.  
  11.         var assembly = Assembly.LoadWithPartialName(assemblyName);
  12.         if (assembly == null)
  13.             return null;
  14.  
  15.         return assembly.GetType(TypeName);
  16.  
  17.     }
  18.  
  19.     [MenuItem("Custom/Save RAINpuppet")]
  20.     public static void SaveRAINpuppet()
  21.     {
  22.         GameObject obj = GameObject.Find("RAINPuppet1");
  23.         if (null == obj)
  24.         {
  25.             Debug.LogError("Save failed - RAINpuppet1 object not found");
  26.             return;
  27.         }
  28.  
  29.         XmlDocument doc = new XmlDocument();
  30.         XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
  31.         doc.AppendChild(declaration);
  32.  
  33.         XmlElement rainElems = doc.CreateElement("RAINComponents");
  34.         doc.AppendChild(rainElems);
  35.  
  36.         RAIN.Core.RAINComponent[] rainComponents = obj.GetComponentsInChildren<RAIN.Core.RAINComponent>();
  37.         Dictionary<Type, int> componentTypesCounter = new Dictionary<Type, int>();
  38.  
  39.         foreach (RAIN.Core.RAINComponent component in rainComponents)
  40.         {
  41.             int count;
  42.             if (!componentTypesCounter.TryGetValue(component.GetType(), out count))
  43.                 componentTypesCounter.Add(component.GetType(), 1);
  44.             else
  45.                 componentTypesCounter[component.GetType()] = count + 1;
  46.  
  47.             component.Serialize();
  48.  
  49.             XmlElement componentElem = doc.CreateElement("RAINComponent");
  50.             componentElem.SetAttribute("type", component.GetType().ToString());
  51.             componentElem.SetAttribute("typeIndex", (componentTypesCounter[component.GetType()] - 1).ToString());
  52.             rainElems.AppendChild(componentElem);
  53.  
  54.             byte[] data = System.Text.Encoding.UTF8.GetBytes(component.DataSerializer.SerializedData);
  55.             string base64data = Convert.ToBase64String(data);
  56.  
  57.             XmlElement componentData = doc.CreateElement("data");
  58.             componentElem.AppendChild(componentData);
  59.             componentData.InnerText = base64data;
  60.  
  61.             XmlElement componentCustomData = doc.CreateElement("customData");
  62.             componentElem.AppendChild(componentCustomData);
  63.  
  64.             foreach (RAIN.Serialization.FieldSerializer.CustomSerializedData c in component.DataSerializer.SerializedCustomData)
  65.             {
  66.                 XmlElement customDataEntry = doc.CreateElement("entry");
  67.                 componentCustomData.AppendChild(customDataEntry);
  68.  
  69.                 string base64customData = Convert.ToBase64String(c.Data);
  70.                 customDataEntry.InnerText = base64customData;
  71.  
  72.             }
  73.  
  74.             XmlElement componentObjectsData = doc.CreateElement("objectsData");
  75.             componentElem.AppendChild(componentObjectsData);
  76.  
  77.             foreach (UnityEngine.Object o in component.DataSerializer.SerializedGameObjects)
  78.             {
  79.                 Debug.Log(o);
  80.                 XmlElement objectDataEntry = doc.CreateElement("entry");
  81.                 componentObjectsData.AppendChild(objectDataEntry);
  82.  
  83.                 objectDataEntry.SetAttribute("type", o.GetType().ToString());
  84.                 objectDataEntry.SetAttribute("name", o.name);
  85.                 string path = AssetDatabase.GetAssetPath(o);
  86.                 Debug.Log(path);
  87.                 if (path != "")
  88.                     objectDataEntry.SetAttribute("path", path);
  89.                 else
  90.                 {
  91.                     GameObject g;
  92.                     try
  93.                     {
  94.                         g = o as GameObject;
  95.                     }
  96.                     catch
  97.                     {
  98.                         Debug.LogException(new Exception("cannot cast object " + o.name + " to GameObject"));
  99.                         g = null;
  100.                     }
  101.                     if (g != null)
  102.                     {
  103.                         if (g.GetComponent<UniqueIdentifier>() != null)
  104.                         {
  105.                             objectDataEntry.SetAttribute("UniqueIdentifier", g.GetComponent<UniqueIdentifier>()._id);
  106.                         }
  107.  
  108.                         if (g.GetComponent<PrefabIdentifier>() != null)
  109.                         {
  110.                             objectDataEntry.SetAttribute("PrefabIdentifier", g.GetComponent<PrefabIdentifier>()._id);
  111.                         }
  112.  
  113.                         if (g.GetComponent<StoreInformation>() != null)
  114.                         {
  115.                             objectDataEntry.SetAttribute("StoreInformation", g.GetComponent<StoreInformation>()._id);
  116.                         }
  117.                     }
  118.  
  119.                 }
  120.             }
  121.         }
  122.  
  123.         using (var writer = new XmlTextWriter("D:\\data.txt", null))
  124.         {
  125.             writer.Formatting = Formatting.Indented;
  126.             doc.WriteTo(writer);
  127.             writer.Flush();
  128.             writer.Close();
  129.         }
  130.  
  131.  
  132.     }
  133.  
  134.     [MenuItem("Custom/Load RAINpuppet")]
  135.     public static void LoadRAINpuppet()
  136.     {
  137.         GameObject obj = GameObject.Find("RAINPuppet1");
  138.         if (null == obj)
  139.         {
  140.             Debug.LogError("Load failed - RAINpuppet1 object not found");
  141.             return;
  142.         }
  143.  
  144.         RAIN.Core.AIRig[] aiRigs = obj.GetComponentsInChildren<RAIN.Core.AIRig>();
  145.         RAIN.Entities.EntityRig[] entityRigs = obj.GetComponentsInChildren<RAIN.Entities.EntityRig>();
  146.         RAIN.Navigation.NavMesh.NavMeshRig[] navMeshRigs = obj.GetComponentsInChildren<RAIN.Navigation.NavMesh.NavMeshRig>();
  147.         RAIN.Navigation.Targets.NavigationTargetRig[] navTargetRigs = obj.GetComponentsInChildren<RAIN.Navigation.Targets.NavigationTargetRig>();
  148.         RAIN.Navigation.Waypoints.WaypointRig[] waypointRigs = obj.GetComponentsInChildren<RAIN.Navigation.Waypoints.WaypointRig>();
  149.  
  150.         RAIN.Core.RAINComponent component = (aiRigs.Length > 0) ? aiRigs.ElementAt(0) : null;
  151.         component = (null == component && entityRigs.Length > 0) ? entityRigs.ElementAt(0) : component;
  152.         component = (null == component && navMeshRigs.Length > 0) ? navMeshRigs.ElementAt(0) : component;
  153.         component = (null == component && navTargetRigs.Length > 0) ? navTargetRigs.ElementAt(0) : component;
  154.         component = (null == component && waypointRigs.Length > 0) ? waypointRigs.ElementAt(0) : component;
  155.  
  156.         if (null == component)
  157.         {
  158.             Debug.LogError("no RAIN component found on GameObject \"RAINPuppet1\"!");
  159.             return;
  160.         }
  161.  
  162.         UnityEngine.Object[] objects = UnityEngine.Object.FindObjectsOfType<UnityEngine.Object>();
  163.  
  164.         using (System.IO.StreamReader dataReader = new System.IO.StreamReader("D:\\data.txt"))
  165.         {
  166.             string data = dataReader.ReadToEnd();
  167.             dataReader.Close();          
  168.  
  169.             XmlDocument doc = new XmlDocument();
  170.             doc.LoadXml(data);
  171.            
  172.             XmlNodeList nodes = doc.GetElementsByTagName("RAINComponent");
  173.             foreach (XmlNode node in nodes)
  174.             {
  175.                 XmlNode typeNode = node.Attributes.GetNamedItem("type");
  176.                 XmlNode typeIndexNode = node.Attributes.GetNamedItem("typeIndex");
  177.                 XmlNodeList componentNodes = node.ChildNodes;
  178.  
  179.                 string componentData = "";
  180.                 List<RAIN.Serialization.FieldSerializer.CustomSerializedData> customData = new List<RAIN.Serialization.FieldSerializer.CustomSerializedData>();
  181.                 List<UnityEngine.Object> objectsData = new List<UnityEngine.Object>();
  182.  
  183.                 foreach (XmlNode n in componentNodes)
  184.                 {
  185.                     switch (n.Name)
  186.                     {
  187.                         case "data":
  188.                             componentData = n.InnerText;
  189.                             byte[] nodeData = Convert.FromBase64String(componentData);
  190.                             componentData = System.Text.Encoding.UTF8.GetString(nodeData);
  191.                             break;
  192.  
  193.                         case "customData":
  194.                             XmlNodeList customDataNodes = n.ChildNodes;
  195.                             foreach(XmlNode no in customDataNodes)
  196.                             {
  197.                                 byte[] customRawData = Convert.FromBase64String(no.InnerText);
  198.                                 RAIN.Serialization.FieldSerializer.CustomSerializedData c = new RAIN.Serialization.FieldSerializer.CustomSerializedData(customRawData);
  199.                                 customData.Add(c);
  200.                             }
  201.                             break;
  202.  
  203.                         case "objectsData":
  204.                             XmlNodeList objectsDataNodes = n.ChildNodes;
  205.                             foreach (XmlNode no in objectsDataNodes)
  206.                             {
  207.                                 string name, type, path, uniqueIdentifier, prefabIdentifier, storeInformation;
  208.                                 name = type = path = uniqueIdentifier = prefabIdentifier = storeInformation = "";
  209.  
  210.                                 XmlAttributeCollection noAttrs = no.Attributes;
  211.                                 foreach (XmlAttribute a in noAttrs)
  212.                                 {
  213.                                     switch (a.Name)
  214.                                     {
  215.                                         case "name":
  216.                                             name = a.Value;
  217.                                             break;
  218.  
  219.                                         case "type":
  220.                                             type = a.Value;
  221.                                             break;
  222.  
  223.                                         case "path":
  224.                                             path = a.Value;
  225.                                             break;
  226.  
  227.                                         case "UniqueIdentifier":
  228.                                             uniqueIdentifier = a.Value;
  229.                                             break;
  230.  
  231.                                         case "PrefabIdentifier":
  232.                                             prefabIdentifier = a.Value;
  233.                                             break;
  234.  
  235.                                         case "StoreInformation":
  236.                                             storeInformation = a.Value;
  237.                                             break;
  238.  
  239.                                         default:
  240.                                             Debug.Log("unknown object entry attribute " + a.Name);
  241.                                             break;
  242.                                     }
  243.                                 }
  244.  
  245.                                 if (path != "")
  246.                                 {
  247.                                     objectsData.Add(AssetDatabase.LoadAssetAtPath(path, GetType(type)));
  248.                                 }
  249.                                 else
  250.                                 {
  251.                                     UnityEngine.Object oFound = null;
  252.                                     foreach (UnityEngine.Object o in objects)
  253.                                     {
  254.                                         if (o == null)
  255.                                             continue;
  256.  
  257.                                         GameObject g = null;
  258.                                         try
  259.                                         {
  260.                                             g = o as GameObject;
  261.                                         }
  262.                                         catch
  263.                                         {
  264.                                             Debug.LogException(new Exception("cannot cast object " + o.name + " to GameObject"));
  265.                                         }
  266.  
  267.                                         if (g != null)
  268.                                         {
  269.                                             UniqueIdentifier ui = g.GetComponent<UniqueIdentifier>();
  270.                                             PrefabIdentifier pi = g.GetComponent<PrefabIdentifier>();
  271.                                             StoreInformation si = g.GetComponent<StoreInformation>();
  272.  
  273.                                             if (uniqueIdentifier != "" && ui != null && uniqueIdentifier == ui._id)
  274.                                             {
  275.                                                 oFound = o;
  276.                                                 break;
  277.                                             }
  278.  
  279.                                             if (prefabIdentifier != "" && pi != null && prefabIdentifier == pi._id)
  280.                                             {
  281.                                                 oFound = o;
  282.                                                 break;
  283.                                             }
  284.  
  285.                                             if (storeInformation != "" && si != null && storeInformation == si._id)
  286.                                             {
  287.                                                 oFound = o;
  288.                                                 break;
  289.                                             }
  290.  
  291.                                             if (ui == null && pi == null && si == null && name == g.name && type == g.GetType().ToString())
  292.                                             {
  293.                                                 oFound = o;
  294.                                                 break;
  295.                                             }
  296.  
  297.                                         }
  298.                                         else
  299.                                         {
  300.                                             if (uniqueIdentifier == "" && prefabIdentifier == "" && uniqueIdentifier == "")
  301.                                             {
  302.                                                 if (name == o.name && (o.GetType().IsAssignableFrom(GetType(type)) || GetType(type).IsAssignableFrom(o.GetType()) || type == o.GetType().ToString()))
  303.                                                 {
  304.                                                     oFound = o;
  305.                                                     break;
  306.                                                 }
  307.                                             }
  308.                                         }
  309.                                     }
  310.  
  311.                                     if (oFound != null)
  312.                                     {
  313.                     /* HERE IS THE PROBLEM - WHAT ABOUT OTHER TYPES, THAT CAN BE REFERENCES WITHIN ANY CUSTOM              
  314.                         PROPERTY OR FIELD OF ANY CUSTOM RAIN ELEMENTS
  315.                        I JUST HANDLED THE MOST SERIOUS TYPES, BUT THAT CAN DRIVE INTO MANY PROBLEMS
  316.                     */
  317.                                         switch (type)
  318.                                         {
  319.                                             case "UnityEngine.GameObject":
  320.                                                 if (oFound.GetType() != typeof(GameObject))
  321.                                                 {
  322.                                                     try
  323.                                                     {
  324.                                                         objectsData.Add(oFound as GameObject);
  325.                                                     }
  326.                                                     catch
  327.                                                     {
  328.                                                          Debug.Log(oFound.name + "of type " + oFound.GetType().ToString() + " cannot be cast to GameObject");
  329.                                                     }
  330.                                                 }
  331.                                                 else
  332.                                                 {
  333.                                                     objectsData.Add(oFound);
  334.                                                 }
  335.                                                 break;
  336.  
  337.                                             case "UnityEngine.Transform":
  338.                                                 if (oFound.GetType() != typeof(Transform))
  339.                                                 {
  340.                                                     try
  341.                                                     {
  342.                                                         objectsData.Add((oFound as GameObject).transform);
  343.                                                     }
  344.                                                     catch
  345.                                                     {
  346.                                                         Debug.Log(oFound.name + "of type " + oFound.GetType().ToString() + " cannot be cast to GameObject");
  347.                                                     }
  348.                                                 }
  349.                                                 else
  350.                                                 {
  351.                                                     objectsData.Add(oFound);
  352.                                                 }
  353.  
  354.                                                 break;
  355.  
  356.                                             case "UnityEngine.ScriptableObject":
  357.                                                 if (oFound.GetType() != typeof(ScriptableObject))
  358.                                                 {
  359.                                                     try
  360.                                                     {
  361.                                                         objectsData.Add(oFound as ScriptableObject);
  362.                                                     }
  363.                                                     catch
  364.                                                     {
  365.                                                          Debug.Log(oFound.name + "of type " + oFound.GetType().ToString() + " cannot be cast to ScriptableObject");
  366.                                                     }
  367.                                                 }
  368.                                                 else
  369.                                                 {
  370.                                                     objectsData.Add(oFound);
  371.                                                 }
  372.  
  373.                                                 break;
  374.  
  375.                                             case "UnityEngine.MonoBehaviour":
  376.                                                 if (oFound.GetType() != typeof(MonoBehaviour))
  377.                                                 {
  378.                                                     try
  379.                                                     {
  380.                                                         objectsData.Add(oFound as MonoBehaviour);
  381.                                                     }
  382.                                                     catch
  383.                                                     {
  384.                                                         Debug.Log(oFound.name + "of type " + oFound.GetType().ToString() + " cannot be cast to MonoBehaviour");
  385.                                                     }
  386.                                                 }
  387.                                                 else
  388.                                                 {
  389.                                                     objectsData.Add(oFound);
  390.                                                 }
  391.  
  392.                                                 break;
  393.  
  394.                                             default:
  395.                                                 objectsData.Add(oFound);
  396.                                                 break;
  397.                                         }
  398.                                     }
  399.                                     else
  400.                                     {
  401.                                         Debug.Log(name + " of type " + type + " NOT FOUND !!!!!!");
  402.                                     }
  403.                                 }
  404.                             }
  405.                             break;
  406.  
  407.                         default:
  408.                             Debug.LogError("Unknown node " + n.Name + " in RAIN component!");
  409.                             break;
  410.                     }
  411.                 }            
  412.  
  413.                
  414.                
  415.                 int index = Convert.ToInt32(typeIndexNode.Value);
  416.                 switch (typeNode.Value)
  417.                 {
  418.                     case "RAIN.Core.AIRig":
  419.                         if (index >= aiRigs.Length)
  420.                             Debug.LogError("RAINComponent AIRig with index " + index.ToString() + " was not found in the parent object!");
  421.                         if (index >= 0 && index < aiRigs.Length)
  422.                         {
  423.                             aiRigs.ElementAt(index).AI.WorkingMemory.Clear();
  424.                             aiRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  425.                             //aiRigs.ElementAt(index).ApplySerializationChanges();
  426.                         }
  427.                         break;
  428.  
  429.                     case "RAIN.Entities.EntityRig":
  430.                         if (index >= entityRigs.Length)
  431.                             Debug.LogError("RAINComponent EntityRig with index " + index.ToString() + " was not found in the parent object!");
  432.  
  433.                         if (index >= 0 && index < entityRigs.Length)
  434.                         {
  435.                             entityRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  436.                             //entityRigs.ElementAt(index).ApplySerializationChanges();
  437.                         }
  438.                         break;
  439.  
  440.                     case "RAIN.Navigation.NavMesh.NavMeshRig":
  441.                         if (index >= navMeshRigs.Length)
  442.                             Debug.LogError("RAINComponent NavMeshRig with index " + index.ToString() + " was not found in the parent object!");
  443.  
  444.                         if (index >= 0 && index < navMeshRigs.Length)
  445.                         {
  446.                             navMeshRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  447.                             //navMeshRigs.ElementAt(index).ApplySerializationChanges();
  448.                         }
  449.                         break;
  450.  
  451.                     case "RAIN.Navigation.Targets.NavigationTargetRig":
  452.                         if (index >= navTargetRigs.Length)
  453.                             Debug.LogError("RAINComponent NavigationTargetRig with index " + index.ToString() + " was not found in the parent object!");
  454.  
  455.                         if (index >= 0 && index < navTargetRigs.Length)
  456.                         {
  457.                             navTargetRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  458.                             //navTargetRigs.ElementAt(index).ApplySerializationChanges();
  459.                         }
  460.                         break;
  461.  
  462.                     case "RAIN.Navigation.Waypoints.WaypointRig":
  463.                         if (index >= waypointRigs.Length)
  464.                             Debug.LogError("RAINComponent WaypointRig with index " + index.ToString() + " was not found in the parent object!");
  465.  
  466.                         if (index >= 0 && index < waypointRigs.Length)
  467.                         {
  468.                             waypointRigs.ElementAt(index).DeserializeInPlace(componentData, (objectsData.Count > 0) ? objectsData : null, (customData.Count > 0) ? customData : null);
  469.                             //waypointRigs.ElementAt(index).ApplySerializationChanges();
  470.                         }
  471.                         break;
  472.  
  473.                     default:
  474.                         Debug.LogError("unknown RAINComponent type");
  475.                         break;
  476.                 }
  477.  
  478.             }
  479.         }
  480.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement