Exemple #1
0
 void ExtractEnvironmentLightEntity(string xmlstr)
 {
     try
     {
         if (xmlstr.Length > 0)
         {
             TextReader tr        = new StringReader(xmlstr);
             XDocument  xml       = XDocument.Load(tr);
             string     layerguid = GuidUtility.GenID("Main");
             foreach (XElement el in xml.Descendants("Entity"))
             {
                 if (el.Attribute("EntityClass") != null)
                 {
                     if (el.Attribute("EntityClass").Value == "EnvironmentLight")
                     {
                         PrefabObject prObj = new PrefabObject();
                         prObj.Name = el.Attribute("Name").Value;
                         if (el.Attribute("Pos") != null)
                         {
                             prObj.Pos = el.Attribute("Pos").Value;
                         }
                         if (el.Attribute("Rotate") != null)
                         {
                             prObj.Rotate = el.Attribute("Rotate").Value;
                         }
                         if (el.Attribute("Scale") != null)
                         {
                             prObj.Scale = el.Attribute("Scale").Value;
                         }
                         prObj.EntityClass = el.Attribute("EntityClass").Value;
                         prObj.Type        = el.Attribute("EntityClass").Value;
                         prObj.Id          = GuidUtility.GenID("dfgdfg453" + prObj.Name + prObj.Pos + prObj.EntityClass);
                         prObj.Layer       = el.Attribute("Layer").Value;
                         byte[] asciiBytes = Encoding.ASCII.GetBytes(el.Attribute("Layer").Value);
                         prObj.LayerGUID  = layerguid;
                         prObj.Properties = el.Element("Properties");
                         prefabObjects.Add(prObj);
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
         Console.WriteLine("|{0}|", xmlstr);
         Console.WriteLine("Press Enter to continue...");
         Console.Read();
         //throw;
     }
 }
Exemple #2
0
 void ExtractPortalEntity(string xmlstr)
 {
     try
     {
         if (xmlstr.Length > 0)
         {
             TextReader tr  = new StringReader(xmlstr);
             XDocument  xml = XDocument.Load(tr);
             foreach (XElement el in xml.Descendants("Object"))
             {
                 if (el.Attribute("Type") != null)
                 {
                     if (el.Attribute("Type").Value == "Portal")
                     {
                         PrefabObject prObj = new PrefabObject();
                         prObj.Name = el.Attribute("Name").Value;
                         prObj.Pos  = el.Attribute("Pos").Value;
                         //prObj.Pos = "0,0,0";
                         //if (el.Attribute("Rotate") != null) prObj.Rotate = el.Attribute("Rotate").Value;
                         if (el.Attribute("Rotate") != null)
                         {
                             prObj.Rotate = "1,0,0,0";
                         }
                         if (el.Attribute("Scale") != null)
                         {
                             prObj.Scale = el.Attribute("Scale").Value;
                         }
                         prObj.Type            = el.Attribute("Type").Value;
                         prObj.Id              = GuidUtility.GenID("dfghk55ggu" + prObj.Name + prObj.Pos + prObj.Type);
                         prObj.Points          = el.Element("Points");
                         prObj.DisplayFilled   = el.Attribute("DisplayFilled").Value;
                         prObj.Height          = el.Attribute("Height").Value;
                         prObj.LightBlending   = el.Attribute("LightBlending").Value;
                         prObj.LightBlendValue = el.Attribute("LightBlendValue").Value;
                         if (VisAreaPos != null)
                         {
                             prObj.VisAreaPos = VisAreaPos;
                         }
                         else
                         {
                             Console.WriteLine(SocpakPath); Console.WriteLine(el.Attribute("Name").Value);
                         };
                         if (name == "frnt")
                         {
                             Console.WriteLine(VisAreaPos);
                         }
                         prObj.FixPoints();
                         PrefabObject prObjFromChunk = areaShapes[0].GetPrefabObjByName(el.Attribute("Name").Value);
                         foreach (AreaShapes arShape in areaShapes)
                         {
                             prObjFromChunk = arShape.GetPrefabObjByName(el.Attribute("Name").Value);
                         }
                         if (prObjFromChunk.Name != null)
                         {
                             prObj.Pos    = prObjFromChunk.Pos;
                             prObj.Rotate = prObjFromChunk.Rotate;
                             prObj.Height = prObjFromChunk.Height;
                             prObj.Points = prObjFromChunk.Points;
                         }
                         prefabObjects.Add(prObj);
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("|{0}|", xmlstr);
         Console.WriteLine(name);
         Console.WriteLine(SocpakPath);
         throw;
     }
 }
Exemple #3
0
 void ExtractLightEntity(string xmlstr)
 {
     if (xmlstr.Length > 0)
     {
         TextReader tr        = new StringReader(xmlstr);
         XDocument  xml       = XDocument.Load(tr);
         string     layerguid = GuidUtility.GenID("Main");
         foreach (XElement el in xml.Descendants("Entity"))
         {
             if (el.Attribute("EntityClass") != null)
             {
                 if (el.Attribute("EntityClass").Value == "Light")
                 {
                     PrefabObject prObj = new PrefabObject();
                     prObj.Name = el.Attribute("Name").Value;
                     if (el.Attribute("Pos") != null)
                     {
                         prObj.Pos = el.Attribute("Pos").Value;
                     }
                     else
                     {
                         prObj.Pos = "0,0,0";
                     }
                     if (el.Attribute("Rotate") != null)
                     {
                         prObj.Rotate = el.Attribute("Rotate").Value;
                     }
                     if (el.Attribute("Scale") != null)
                     {
                         prObj.Scale = el.Attribute("Scale").Value;
                     }
                     prObj.EntityClass = el.Attribute("EntityClass").Value;
                     prObj.Type        = el.Attribute("EntityClass").Value;
                     prObj.Id          = GuidUtility.GenID("sfdf4rgds" + prObj.Name);;
                     prObj.Layer       = el.Attribute("Layer").Value;
                     byte[] asciiBytes = Encoding.ASCII.GetBytes(el.Attribute("Layer").Value);
                     prObj.LayerGUID  = layerguid;
                     prObj.Properties = el.Element("Properties");
                     prefabObjects.Add(prObj);
                 }
             }
             if (el.Attribute("EntityClass") != null)
             {
                 if (el.Attribute("EntityClass").Value == "LightGroup")
                 {
                     PrefabObject prObj = new PrefabObject();
                     prObj.lights = new List <Light>();
                     prObj.Name   = el.Attribute("Name").Value;
                     if (el.Attribute("Rotate") != null)
                     {
                         prObj.Rotate = el.Attribute("Rotate").Value;
                     }
                     if (el.Attribute("Pos") != null)
                     {
                         prObj.Pos = el.Attribute("Pos").Value;
                     }
                     if (el.Attribute("Scale") != null)
                     {
                         prObj.Scale = el.Attribute("Scale").Value;
                     }
                     prObj.EntityClass = "LightGroup";
                     prObj.Layer       = el.Attribute("Layer").Value;
                     prObj.Id          = GuidUtility.GenID(prObj.Name + "dfgjhdkjf");
                     int i = 0;
                     foreach (XElement ell in el.Descendants("Light"))
                     {
                         XElement relativeXForm = ell.Element("RelativeXForm");
                         Light    lightEnt      = new Light();
                         lightEnt.Name = el.Attribute("Name").Value;
                         if (relativeXForm.Attribute("translation") != null)
                         {
                             lightEnt.translation = relativeXForm.Attribute("translation").Value;
                         }
                         if (relativeXForm.Attribute("rotation") != null)
                         {
                             lightEnt.rotation = relativeXForm.Attribute("rotation").Value;
                         }
                         if (el.Attribute("Scale") != null)
                         {
                             lightEnt.Scale = el.Attribute("Scale").Value;
                         }
                         lightEnt.EntityClass = "Light";
                         lightEnt.Type        = "Entity";
                         lightEnt.Id          = GuidUtility.GenID();
                         lightEnt.Layer       = el.Attribute("Layer").Value;
                         lightEnt.LayerGUID   = layerguid;
                         //lightEnt.AttachmentPointName = el.Attribute("Layer").Value; ;
                         lightEnt.Properties = ell.Element("Properties");
                         prObj.lights.Add(lightEnt);
                         i++;
                     }
                     i = 0;
                     foreach (XElement ell in el.Descendants("EnvironmentProbe"))
                     {
                         XElement relativeXForm = ell.Element("RelativeXForm");
                         Light    lightEnt      = new Light();
                         lightEnt.Name = el.Attribute("Name").Value;
                         if (relativeXForm.Attribute("translation") != null)
                         {
                             lightEnt.translation = relativeXForm.Attribute("translation").Value;
                         }
                         if (relativeXForm.Attribute("rotation") != null)
                         {
                             lightEnt.rotation = relativeXForm.Attribute("rotation").Value;
                         }
                         if (el.Attribute("Scale") != null)
                         {
                             lightEnt.Scale = el.Attribute("Scale").Value;
                         }
                         lightEnt.EntityClass = "EnvironmentLight";
                         lightEnt.Type        = "Entity";
                         lightEnt.Id          = GuidUtility.GenID();
                         lightEnt.Layer       = el.Attribute("Layer").Value;
                         lightEnt.LayerGUID   = layerguid;
                         //lightEnt.AttachmentPointName = el.Attribute("Layer").Value; ;
                         lightEnt.Properties = ell.Element("Properties");
                         prObj.lights.Add(lightEnt);
                         i++;
                     }
                     prefabObjects.Add(prObj);
                 }
             }
         }
     }
 }
        public XDocument GenerateShipOLD(GenerateOptions genOptions)
        {
            string             shipName = genOptions.shipName;
            ShipImplementation ship     = exporter.GetShipImplementation(shipName);
            Paint paint = ship.GetPaintByName(genOptions.paintName);
            //shipName = "AEGS_Gladius";
            XDocument xml         = new XDocument();
            string    loadoutName = null;

            if (genOptions.loadoutName != null)
            {
                loadoutName = genOptions.loadoutName;
            }
            else
            {
                loadoutName = "Default_Loadout_" + shipName;
            }

            string prefLibName = "";

            if (paint != null)
            {
                prefLibName = shipName + "-" + paint.name;
            }
            else
            {
                prefLibName = shipName;
            }
            prefLibName = "ships/" + prefLibName;
            XElement PrefabsLibrary = new XElement("PrefabsLibrary");

            //PrefabsLibrary.Attribute("Name").Value = shipName+"_Ship";
            //PrefabsLibrary.Add(new XAttribute("Name", shipName + "_Ship"));
            PrefabsLibrary.Add(new XAttribute("Name", prefLibName));

            XElement Prefab   = new XElement("Prefab");
            string   prefabId = GuidUtility.GenPrefabID(prefLibName);

            //Prefab.Add(new XAttribute("Name", shipName));
            Prefab.Add(new XAttribute("Name", "Ship"));
            Prefab.Add(new XAttribute("Id", prefabId));
            // Prefab.Add(new XAttribute("Library", shipName + "_Ship"));
            Prefab.Add(new XAttribute("Library", prefLibName));

            XElement Objects = new XElement("Objects");


            Loadout loadout = exporter.GetLoadout(loadoutName);

            Console.WriteLine(loadout.name);


            XElement Object = new XElement("Object");
            //string partid = GuidUtility.GenID("jhk777gt" + shipName  );
            //string layerid = GuidUtility.GenID("ghjg557kk" + shipName  );
            string partid   = GuidUtility.GenID();
            string layerid  = GuidUtility.GenID();
            string parentid = "0";

            parentid = partid;

            Object = AddObjects(Object, ship.parts.First(), loadout, parentid, partid, layerid, paint);
            i      = 0;
            //Object = AddPrefabsFromObjectContainers(Object, ship, parentid, layerid);
            Object = AddObjectsFromObjectContainers(Object, ship.objectContainers, parentid, layerid);

            if (Object.Attribute("Name") != null)
            {
                Objects.Add(Object);
            }
            //Objects.Add(Object);

            XElement ObjectsNew = new XElement("Objects");

            foreach (XElement obj in Objects.Descendants("Object"))
            {
                XElement ObjectNew = new XElement("Object");
                //ObjectNew.Add(new XElement("Properties"));
                if (obj.Element("Properties") != null)
                {
                    ObjectNew.Add(obj.Element("Properties"));
                }
                foreach (XAttribute atr in obj.Attributes())
                {
                    ObjectNew.Add(atr);
                }
                if (obj.Element("Components") != null)
                {
                    ObjectNew.Add(obj.Element("Components"));
                }

                if (obj.Element("Points") != null)
                {
                    ObjectNew.Add(obj.Element("Points"));
                }

                if (ObjectNew.Attribute("Name") != null)
                {
                    ObjectsNew.Add(ObjectNew);
                }
            }

            Prefab.Add(ObjectsNew);
            PrefabsLibrary.Add(Prefab);
            foreach (XElement pr in prefabs)
            {
                PrefabsLibrary.Add(pr);
            }
            xml.Add(PrefabsLibrary);

            //XDocument xmlpreflib = XDocument.Load(prefablib);
            //XElement preflibel = xmlpreflib.Element("PrefabsLibrary");
            //xml.Add(preflibel);

            return(xml);
        }
Exemple #5
0
 /// <summary>
 /// Exctract GeomEntity objects from XML chunk
 /// </summary>
 /// <param name="xml"></param>
 void ExtractGeomEntityOLD(string xmlstr)
 {
     //try
     //{
     if (xmlstr.Length > 0)
     {
         //bool decoded = false;
         //string label = xmlstr.Substring(0,6);
         //Console.WriteLine("label {0}", label);
         //if (label != "CryXml") decoded = true;
         //if(!decoded)
         //{
         //    xmlstr = DecodeCryXml(xmlstr, cryXmlHeaderOffset);
         //    decoded = true;
         //}
         //if (decoded)
         //{
         TextReader tr        = new StringReader(xmlstr);
         XDocument  xml       = XDocument.Load(tr);
         string     layerguid = Guid.NewGuid().ToString();
         foreach (XElement el in xml.Descendants("Entity"))
         {
             if (el.Attribute("EntityClass") != null)
             {
                 if (el.Attribute("EntityClass").Value == "GeomEntity")
                 {
                     PrefabObject prObj = new PrefabObject();
                     prObj.Name = el.Attribute("Name").Value;
                     //Console.WriteLine(prObj.Name);
                     //if(prObj.Name=="lmp_emergency_a-071")
                     //{
                     //    throw new System.ArgumentException("lmp_emergency_a-071", "original");
                     //}
                     if (el.Attribute("Pos") != null)
                     {
                         prObj.Pos = el.Attribute("Pos").Value;
                     }
                     if (el.Attribute("Rotate") != null)
                     {
                         prObj.Rotate = el.Attribute("Rotate").Value;
                         // Quaternion quat = new Quaternion( prObj.GetRotate().y, prObj.GetRotate().z, prObj.GetRotate().w, prObj.GetRotate().x);
                         //quat =ValidateQuaternion(quat);
                         // Vector4 quatV = new Vector4(quat.x, quat.y, quat.z, quat.w);
                         // prObj.SetRotate(quatV);
                     }
                     if (el.Attribute("Scale") != null)
                     {
                         prObj.Scale = el.Attribute("Scale").Value;
                     }
                     prObj.EntityClass = el.Attribute("EntityClass").Value;
                     prObj.Type        = el.Attribute("EntityClass").Value;
                     prObj.Id          = GuidUtility.GenID("sdfsdf3424" + prObj.Name + prObj.EntityClass);
                     prObj.Layer       = el.Attribute("Layer").Value;
                     if (el.Attribute("Geometry") != null)
                     {
                         prObj.Geometry = el.Attribute("Geometry").Value;
                     }
                     else
                     {
                         prObj.Geometry = el.Element("PropertiesDataCore").Element("EntityGeometryResource").Element("Geometry").Element("Geometry").Element("Geometry").Attribute("path").Value;
                     }
                     byte[] asciiBytes = Encoding.ASCII.GetBytes(el.Attribute("Layer").Value);
                     prObj.LayerGUID = layerguid;
                     prObj.LodRatio  = lodRatio;
                     prefabObjects.Add(prObj);
                 }
             }
         }
         //}
     }
     //}
     //catch(Exception e)
     //{
     //    Console.WriteLine("|{0}|",xmlstr);
     //    Console.Read();
     //    throw;
     //}
 }
        public XDocument GenerateShip(ShipDefinition ship)
        {
            XDocument          xml                = new XDocument();
            string             shipName           = ship.GetShipName();
            ShipImplementation shipImplementation = ship.GetShipImplementation();

            if (shipImplementation != null)
            {
                Paint paint = ship.GetShipPaint();
                //shipName = "AEGS_Gladius";

                string prefLibName = "";
                prefLibName = shipName;
                prefLibName = ship.GetVehicleType() + "/" + ship.GetManufacturer() + "/" + prefLibName;
                XElement PrefabsLibrary = new XElement("PrefabsLibrary");
                //PrefabsLibrary.Attribute("Name").Value = shipName+"_Ship";
                //PrefabsLibrary.Add(new XAttribute("Name", shipName + "_Ship"));
                PrefabsLibrary.Add(new XAttribute("Name", prefLibName));

                XElement Prefab   = new XElement("Prefab");
                string   prefabId = GuidUtility.GenPrefabID(prefLibName);
                //Prefab.Add(new XAttribute("Name", shipName));
                Prefab.Add(new XAttribute("Name", "Ship"));
                Prefab.Add(new XAttribute("Id", prefabId));
                // Prefab.Add(new XAttribute("Library", shipName + "_Ship"));
                Prefab.Add(new XAttribute("Library", prefLibName));

                XElement Objects = new XElement("Objects");


                Loadout loadout = ship.GetLoadout();
                //Console.WriteLine(loadout.name);


                XElement Object = new XElement("Object");
                //string partid = GuidUtility.GenID("jhk777gt" + shipName  );
                //string layerid = GuidUtility.GenID("ghjg557kk" + shipName  );
                string partid   = GuidUtility.GenID();
                string layerid  = GuidUtility.GenID();
                string parentid = "0";
                parentid = partid;

                Object = AddObjects(Object, shipImplementation.parts.First(), loadout, parentid, partid, layerid, paint);
                i      = 0;
                //Object = AddPrefabsFromObjectContainers(Object, ship, parentid, layerid);
                Object = AddObjectsFromObjectContainers(Object, shipImplementation.objectContainers, parentid, layerid);

                if (Object.Attribute("Name") != null)
                {
                    Objects.Add(Object);
                }
                //Objects.Add(Object);

                XElement ObjectsNew = new XElement("Objects");
                foreach (XElement obj in Objects.Descendants("Object"))
                {
                    XElement ObjectNew = new XElement("Object");
                    //ObjectNew.Add(new XElement("Properties"));
                    if (obj.Element("Properties") != null)
                    {
                        ObjectNew.Add(obj.Element("Properties"));
                    }
                    foreach (XAttribute atr in obj.Attributes())
                    {
                        ObjectNew.Add(atr);
                    }
                    if (obj.Element("Components") != null)
                    {
                        ObjectNew.Add(obj.Element("Components"));
                    }

                    if (obj.Element("Points") != null)
                    {
                        ObjectNew.Add(obj.Element("Points"));
                    }

                    if (ObjectNew.Attribute("Name") != null)
                    {
                        ObjectsNew.Add(ObjectNew);
                    }
                }

                Prefab.Add(ObjectsNew);
                PrefabsLibrary.Add(Prefab);
                foreach (XElement pr in prefabs)
                {
                    PrefabsLibrary.Add(pr);
                }
                xml.Add(PrefabsLibrary);

                //XDocument xmlpreflib = XDocument.Load(prefablib);
                //XElement preflibel = xmlpreflib.Element("PrefabsLibrary");
                //xml.Add(preflibel);
            }

            return(xml);
        }
        public XElement AddObjects(XElement el, Part part, Loadout loadout, string parentid, string partid, string layerid, Paint paint = null)
        {
            XElement Object = el;

            if (part.partClass == "AnimatedJoint")
            {
                partid      = parentid;
                Object.Name = "Node";
                Object.Add(new XAttribute("Name", part.name));
                Object.Add(new XAttribute("LinkedTo", parentid));
                Object.Add(new XAttribute("Id", partid));
            }

            if (part.partClass == "Animated" || part.partClass == "SubPartWheel")
            {
                if (i == 0)
                {
                    if (paint != null)
                    {
                        Object.Add(new XAttribute("Material", paint.material));
                    }
                }
                if (i != 0)
                {
                    Object.Add(new XAttribute("LinkedTo", parentid));
                }
                Object.Add(new XAttribute("Name", part.name));
                Object.Add(new XAttribute("Id", partid));
                Object.Add(new XAttribute("LayerGUID", layerid));
                if (part.animatedFilename != null)
                {
                    Object.Add(new XAttribute("Geometry", part.animatedFilename));
                }
                else
                {
                    Object.Add(new XAttribute("Geometry", ""));
                }
                Object.Add(new XAttribute("Layer", "Main"));
                Object.Add(new XAttribute("Type", "GeomEntity"));
                Object.Add(new XAttribute("EntityClass", "GeomEntity"));
                Object.Add(new XAttribute("LodRatio", lodRatio));
                if (part.partClass == "SubPartWheel")
                {
                    Object.Add(new XAttribute("AttachmentType", "CharacterBone"));
                    Object.Add(new XAttribute("AttachmentTarget", part.name));
                }
                else
                {
                    if (part.helper != "")
                    {
                        Object.Add(new XAttribute("AttachmentType", "CharacterBone"));
                    }
                    if (part.helper != "")
                    {
                        Object.Add(new XAttribute("AttachmentTarget", part.helper));
                    }
                }


                XElement Components = new XElement("Components");
                XElement Component  = new XElement("Component");
                Component.Add(new XAttribute("typeId", "ec0cd266-a6d1-4774-b499-690bd6fb61ee"));
                // Component.Add(new XAttribute("guid", GuidUtility.GenID(part.name+loadout.name+part.animatedFilename)));
                Component.Add(new XAttribute("guid", GuidUtility.GenID()));
                Component.Add(new XAttribute("CryXmlVersion", "2"));
                if (part.animatedFilename != null)
                {
                    Component.Add(new XAttribute("Geometry", part.animatedFilename));
                }
                else
                {
                    Component.Add(new XAttribute("Geometry", ""));
                }
                Component.Add(new XAttribute("Physicalize", "ePhysicalizationType_Static"));
                Component.Add(new XAttribute("ReceiveCollisionEvents", "false"));
                Component.Add(new XAttribute("Mass", "1"));
                Component.Add(new XAttribute("Animation", ""));
                Component.Add(new XAttribute("Speed", "1"));
                Component.Add(new XAttribute("Loop", "true"));
                Components.Add(Component);
                Object.Add(Components);
                i++;
            }
            if (part.partClass == "ItemPort")
            {
                ItemPort itemport = loadout.GetItemPort(part.name);
                Item     item     = exporter.GetItem(itemport.itemName, loadout.name);
                if (item.geometry != "" && item.itemclass != "VehicleItemMultiLight" && allowedItemClasses.Any(item.itemclass.Contains))
                {
                    Object.Add(new XAttribute("Name", part.name));
                    Object.Add(new XAttribute("LinkedTo", parentid));
                    Object.Add(new XAttribute("Id", partid));
                    Object.Add(new XAttribute("LayerGUID", layerid));
                    Object.Add(new XAttribute("Geometry", item.geometry));
                    Object.Add(new XAttribute("AttachmentType", "CharacterBone"));
                    Object.Add(new XAttribute("AttachmentTarget", itemport.portName));
                    Object.Add(new XAttribute("Layer", "Main"));
                    Object.Add(new XAttribute("Type", "GeomEntity"));
                    Object.Add(new XAttribute("EntityClass", "GeomEntity"));
                    Object.Add(new XAttribute("LodRatio", lodRatio));

                    XElement Components = new XElement("Components");
                    XElement Component  = new XElement("Component");
                    Component.Add(new XAttribute("typeId", "ec0cd266-a6d1-4774-b499-690bd6fb61ee"));
                    //Component.Add(new XAttribute("guid", GuidUtility.GenID(part.name + loadout.name + part.animatedFilename+ itemport.portName)));
                    Component.Add(new XAttribute("guid", GuidUtility.GenID()));
                    Component.Add(new XAttribute("CryXmlVersion", "2"));
                    Component.Add(new XAttribute("Geometry", item.geometry));
                    Component.Add(new XAttribute("Physicalize", "ePhysicalizationType_Static"));
                    Component.Add(new XAttribute("ReceiveCollisionEvents", "false"));
                    Component.Add(new XAttribute("Mass", "1"));
                    Component.Add(new XAttribute("Animation", ""));
                    Component.Add(new XAttribute("Speed", "1"));
                    Component.Add(new XAttribute("Loop", "true"));
                    Components.Add(Component);
                    Object.Add(Components);

                    foreach (ItemPort ip in itemport.itemPorts)
                    {
                        Item     item2 = exporter.GetItem(ip.itemName, loadout.name);
                        XElement obj2  = new XElement("Object");
                        obj2.Add(new XAttribute("Name", ip.portName));
                        obj2.Add(new XAttribute("LinkedTo", partid));
                        //obj2.Add(new XAttribute("Id", GuidUtility.GenID("skjdfu"+part.name + loadout.name + part.animatedFilename + ip.portName+item2.name)));
                        obj2.Add(new XAttribute("Id", GuidUtility.GenID()));
                        obj2.Add(new XAttribute("LayerGUID", layerid));
                        obj2.Add(new XAttribute("Geometry", item2.geometry));
                        obj2.Add(new XAttribute("AttachmentType", "CharacterBone"));
                        obj2.Add(new XAttribute("AttachmentTarget", ip.portName));
                        obj2.Add(new XAttribute("Layer", "Main"));
                        obj2.Add(new XAttribute("Type", "GeomEntity"));
                        obj2.Add(new XAttribute("EntityClass", "GeomEntity"));
                        obj2.Add(new XAttribute("LodRatio", lodRatio));

                        XElement Components2 = new XElement("Components");
                        XElement Component2  = new XElement("Component");
                        Component2.Add(new XAttribute("typeId", "ec0cd266-a6d1-4774-b499-690bd6fb61ee"));
                        //Component2.Add(new XAttribute("guid", GuidUtility.GenID(part.name + loadout.name + part.animatedFilename + ip.portName)));
                        Component2.Add(new XAttribute("guid", GuidUtility.GenID()));
                        Component2.Add(new XAttribute("CryXmlVersion", "2"));
                        Component2.Add(new XAttribute("Geometry", item2.geometry));
                        Component2.Add(new XAttribute("Physicalize", "ePhysicalizationType_Static"));
                        Component2.Add(new XAttribute("ReceiveCollisionEvents", "false"));
                        Component2.Add(new XAttribute("Mass", "1"));
                        Component2.Add(new XAttribute("Animation", ""));
                        Component2.Add(new XAttribute("Speed", "1"));
                        Component2.Add(new XAttribute("Loop", "true"));
                        Components2.Add(Component2);
                        obj2.Add(Components2);

                        Object.Add(obj2);
                    }
                }
                if (item.itemclass == "VehicleItemMultiLight")
                {
                    foreach (AttachmentPoint at in part.attachmentPoints)
                    {
                        //Console.WriteLine(at.name);
                        XElement obj1  = new XElement("Object");
                        string   entid = GuidUtility.GenID(at.name + at.bone + part.name + loadout.name);
                        obj1.Add(new XAttribute("Type", "GeomEntity"));
                        obj1.Add(new XAttribute("Name", at.bone));
                        obj1.Add(new XAttribute("LinkedTo", parentid));
                        obj1.Add(new XAttribute("Id", entid));
                        obj1.Add(new XAttribute("LayerGUID", layerid));
                        obj1.Add(new XAttribute("AttachmentType", "CharacterBone"));
                        obj1.Add(new XAttribute("AttachmentTarget", at.bone));
                        obj1.Add(new XAttribute("Layer", "Main"));
                        obj1.Add(new XAttribute("EntityClass", "GeomEntity"));
                        Object.Add(obj1);

                        //PrefabAttachment prefabAtt = item.getAttachment(at.name);
                        //XElement obj2 = getPrefab(prefabAtt.prefabLibrary, prefabAtt.prefabName);
                        XElement         obj2       = new XElement("Object");
                        PrefabAttachment prefabAtt  = item.getAttachment(at.name);
                        string           PrefabGUID = "";
                        try
                        {
                            PrefabGUID = GetPrefabGUID(prefabAtt.prefabLibrary, prefabAtt.prefabName);
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Error:");
                            Console.WriteLine("prefabLibrary: {0} prefabName: {1} attachmentPoint: {2}", prefabAtt.prefabLibrary, prefabAtt.prefabName, prefabAtt.attachmentPoint);
                            Console.WriteLine("at.name: {0}", at.name);
                            Console.WriteLine("item.name: {0}", item.name);
                            //throw e;
                        }
                        string prefid        = GuidUtility.GenID(at.name + at.bone + part.name + loadout.name + "prefab");
                        string prefablibname = Path.GetFileNameWithoutExtension(prefabAtt.prefabLibrary);
                        obj2.Add(new XAttribute("Type", "Prefab"));
                        obj2.Add(new XAttribute("Name", at.name));
                        obj2.Add(new XAttribute("LinkedTo", entid));
                        obj2.Add(new XAttribute("Id", prefid));
                        obj2.Add(new XAttribute("LayerGUID", layerid));
                        obj2.Add(new XAttribute("Layer", "Main"));
                        obj2.Add(new XAttribute("PrefabGUID", PrefabGUID));
                        obj2.Add(new XAttribute("PrefabLibrary", prefablibname));
                        obj2.Add(new XAttribute("PrefabName", prefabAtt.prefabName));
                        prefablib = prefabAtt.prefabLibrary;
                        prefabs.Add(GetPrefab(prefabAtt.prefabLibrary, prefabAtt.prefabName));
                        Object.Add(obj2);
                    }
                }
            }

            foreach (Part pr in part.parts)
            {
                XElement obj = new XElement("Object");
                if (pr.partClass == "ItemPort")
                {
                    ItemPort itemport = loadout.GetItemPort(part.name);
                    if (itemport.itemName != "")
                    {
                        obj = AddObjects(obj, pr, loadout, partid, GuidUtility.GenID(pr.name + itemport.itemName + loadout.name + "kjhkh" + pr.animatedFilename), layerid);
                    }
                    else
                    {
                        obj = AddObjects(obj, pr, loadout, parentid, GuidUtility.GenID(pr.name + itemport.itemName + loadout.name + "5tyht" + pr.animatedFilename), layerid);
                    }
                }
                else
                {
                    obj = AddObjects(obj, pr, loadout, partid, GuidUtility.GenID(pr.name + loadout.name + "asddf" + pr.animatedFilename), layerid);
                }
                //if (obj.Attribute("Name") != null)
                Object.Add(obj);
            }

            return(Object);
        }
        public XDocument GenerateShipModification(GenerateOptions genOptions)
        {
            string shipName         = genOptions.shipName;
            string modificationName = genOptions.modificationName;

            XDocument xml = new XDocument();

            ShipImplementation ship           = exporter.GetShipImplementation(shipName);
            Paint        paint                = ship.GetPaintByName(genOptions.paintName);
            Modification shipModification     = ship.GetModificationByName(modificationName);
            string       modificationTrueName = shipModification.trueName;
            string       loadoutName          = null;

            if (genOptions.loadoutName != null)
            {
                loadoutName = genOptions.loadoutName;
            }
            else
            {
                loadoutName = "Default_Loadout_" + modificationTrueName;
            }
            Loadout loadout = exporter.GetLoadout(loadoutName);

            Console.WriteLine(loadout.name);

            string prefLibName = "";

            if (paint != null)
            {
                prefLibName = modificationTrueName + "-" + paint.name;
            }
            else
            {
                prefLibName = modificationTrueName;
            }
            prefLibName = "ships/Variants/" + prefLibName;
            XElement PrefabsLibrary = new XElement("PrefabsLibrary");

            PrefabsLibrary.Add(new XAttribute("Name", prefLibName));
            XElement Prefab   = new XElement("Prefab");
            string   prefabId = GuidUtility.GenPrefabID(prefLibName);

            Prefab.Add(new XAttribute("Name", "Ship"));
            Prefab.Add(new XAttribute("Id", prefabId));
            Prefab.Add(new XAttribute("Library", prefLibName));

            XElement Objects  = new XElement("Objects");
            XElement Object   = new XElement("Object");
            string   partid   = GuidUtility.GenID("werw34" + shipName + modificationName + prefLibName);
            string   layerid  = GuidUtility.GenID("dsfhf555hyyh" + shipName + modificationName + prefLibName);
            string   parentid = "0";

            parentid = partid;

            if (shipModification.parts.Count != 0)
            {
                Object = AddObjects(Object, shipModification.parts.First(), loadout, parentid, partid, layerid, paint);
                Object = AddObjectsFromObjectContainers(Object, shipModification.objectContainers, parentid, layerid);
            }
            else
            {
                Object = AddObjects(Object, ship.parts.First(), loadout, parentid, partid, layerid, paint);
                Object = AddObjectsFromObjectContainers(Object, ship.objectContainers, parentid, layerid);
            }
            i = 0;


            if (Object.Attribute("Name") != null)
            {
                Objects.Add(Object);
            }

            XElement ObjectsNew = new XElement("Objects");

            foreach (XElement obj in Objects.Descendants("Object"))
            {
                XElement ObjectNew = new XElement("Object");
                if (obj.Element("Properties") != null)
                {
                    ObjectNew.Add(obj.Element("Properties"));
                }
                foreach (XAttribute atr in obj.Attributes())
                {
                    ObjectNew.Add(atr);
                }
                if (obj.Element("Components") != null)
                {
                    ObjectNew.Add(obj.Element("Components"));
                }

                if (ObjectNew.Attribute("Name") != null)
                {
                    ObjectsNew.Add(ObjectNew);
                }
            }

            Prefab.Add(ObjectsNew);
            PrefabsLibrary.Add(Prefab);
            foreach (XElement pr in prefabs)
            {
                PrefabsLibrary.Add(pr);
            }
            xml.Add(PrefabsLibrary);

            return(xml);
        }
        XElement AddObjectsFromObjectContainers(XElement el, List <ObjectContainer> objCons, string parentid, string layerid)
        {
            XElement Object = el;

            layerid = GuidUtility.GenLayerID("Main");
            string layerName = "Main";
            List <ObjectContainer> objContainers = new List <ObjectContainer>();

            foreach (ObjectContainer oc in objCons)
            {
                objContainers.Add(oc);
                foreach (Child child in oc.childs)
                {
                    objContainers.Add(child.Soc);
                }
            }
            foreach (ObjectContainer oc in objContainers)
            {
                string portName = oc.portName;
                foreach (PrefabObject po in oc.prefabObjects)
                {
                    if (po.Geometry != null)
                    {
                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        //string layerName = oc.name;
                        //string entid = GuidUtility.GenID("ent" + parentid +oc.name+po.Name+ po.Geometry);
                        //string compGuid = GuidUtility.GenID("entcomp" + parentid + oc.name + po.Name + po.Geometry);
                        string entid         = GuidUtility.GenID();
                        string componentGuid = GuidUtility.GenID();
                        //string prefid = GuidUtility.GenID("entpref" + parentid + oc.name + po.Name+ po.Geometry);

                        XElement obj1 = po.GetAsGeomEntity(parentid, layerid, entid, componentGuid, portName, layerName, lodRatio);

                        Object.Add(obj1);

                        foreach (PrefabObject childPo in po.attachments)
                        {
                            string childentid         = GuidUtility.GenID();
                            string childcomponentGuid = GuidUtility.GenID();

                            XElement childObj = childPo.GetAsGeomEntity(entid, layerid, childentid, childcomponentGuid, childPo.AttachmentTarget, layerName, lodRatio);

                            Object.Add(childObj);
                        }
                    }
                    if (po.EntityClass == "AnimObject")
                    {
                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        string entid         = GuidUtility.GenID();
                        string componentGuid = GuidUtility.GenID();

                        XElement obj1 = po.GetAsAnimObject(parentid, layerid, entid, componentGuid, portName, layerName, lodRatio);

                        Object.Add(obj1);
                    }
                    if (po.EntityClass == "LightGroup")
                    {
                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        XElement obj1 = po.GetAsFlowgraphEntity(parentid, layerid, layerName, oc.portName);

                        Object.Add(obj1);

                        int i = 0;
                        //lights and probes (enveroinmentlight)
                        foreach (Light lt in po.lights)
                        {
                            if (lt.EntityClass == "Light")
                            {
                                obj1 = po.GetAsLight(i, layerid, layerName);

                                Object.Add(obj1);
                            }
                            if (lt.EntityClass == "EnvironmentLight")
                            {
                                obj1 = po.GetAsEnvironmentLight(i, layerid, layerName);

                                Object.Add(obj1);
                            }
                            i++;
                        }
                    }
                    //if (po.Type == "VisArea")
                    //{
                    //    layerid = GuidUtility.GenLayerID("Main");
                    //    layerName = "Main";
                    //    string entid = GuidUtility.GenID();
                    //    XElement obj1 = po.GetAsVisArea(entid, layerid, layerName);

                    //    Object.Add(obj1);
                    //}
                    //if (po.Type == "Portal")
                    //{
                    //    layerid = GuidUtility.GenLayerID("Main");
                    //    layerName = "Main";
                    //    string entid = GuidUtility.GenID();
                    //    XElement obj1 = po.GetAsPortal(entid, layerid, layerName);

                    //    Object.Add(obj1);
                    //}
                    if (po.Type == "EnvironmentLight")
                    {
                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        XElement obj1 = po.GetAsFlowgraphEntity(parentid, layerid, layerName, oc.portName);

                        Object.Add(obj1);

                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        string entid = GuidUtility.GenID();
                        obj1 = po.GetAsEnvironmentLight2(entid, layerid, layerName);

                        Object.Add(obj1);
                    }
                    if (po.EntityClass == "Light")
                    {
                        layerid   = GuidUtility.GenLayerID("Main");
                        layerName = "Main";
                        XElement obj1   = new XElement("Object");
                        string   entid  = GuidUtility.GenID();
                        string   prefid = GuidUtility.GenID();

                        obj1.Add(new XAttribute("Name", po.Name));
                        obj1.Add(new XAttribute("LinkedTo", parentid));
                        obj1.Add(new XAttribute("Id", entid));
                        obj1.Add(new XAttribute("LayerGUID", layerid));
                        obj1.Add(new XAttribute("Layer", layerName));
                        if (po.Pos != null)
                        {
                            obj1.Add(new XAttribute("Pos", po.Pos));
                        }
                        else
                        {
                            obj1.Add(new XAttribute("Pos", "0,0,0"));
                        }
                        if (po.Rotate != null)
                        {
                            obj1.Add(new XAttribute("Rotate", po.Rotate));
                        }
                        obj1.Add(new XAttribute("Type", "Entity"));
                        obj1.Add(new XAttribute("EntityClass", "Light"));
                        obj1.Add(new XAttribute("ColorRGB", "65535"));
                        obj1.Add(po.Properties);

                        Object.Add(obj1);
                    }
                }

                //foreach (Part part in ship.parts)
                //{
                //    if (part.attachmentPoints != null)
                //    {
                //        List<PrefabObject> prefObjects = oc.GetPrefabObjectsByAttachmentName(p);
                //    }
                //}
            }

            return(Object);
        }
        XElement AddPrefabsFromObjectContainers(XElement el, ShipImplementation ship, string parentid, string layerid)
        {
            XElement Object = el;

            layerid = GuidUtility.GenLayerID("Main");
            foreach (ObjectContainer oc in ship.objectContainers)
            {
                string layername = oc.name;
                //layerid = stringToNumbers(layername);
                //Console.WriteLine(at.name);
                XElement obj1   = new XElement("Object");
                string   entid  = GuidUtility.GenID("ent" + ship.name + oc.name + parentid);
                string   prefid = GuidUtility.GenID("entpref" + ship.name + oc.name + parentid);
                obj1.Add(new XAttribute("Type", "Entity"));
                obj1.Add(new XAttribute("Name", oc.portName));
                obj1.Add(new XAttribute("LinkedTo", parentid));
                obj1.Add(new XAttribute("Id", entid));
                obj1.Add(new XAttribute("LayerGUID", layerid));
                obj1.Add(new XAttribute("AttachmentType", "CharacterBone"));
                obj1.Add(new XAttribute("AttachmentTarget", oc.portName));
                obj1.Add(new XAttribute("Layer", layername));
                obj1.Add(new XAttribute("EntityClass", "FlowgraphEntity"));
                Object.Add(obj1);

                //PrefabAttachment prefabAtt = item.getAttachment(at.name);
                //XElement obj2 = getPrefab(prefabAtt.prefabLibrary, prefabAtt.prefabName);
                XElement obj2          = new XElement("Object");
                string   id            = GuidUtility.GenID("entid" + ship.name + oc.name + parentid);
                string   prefablibname = ship.name;
                obj2.Add(new XAttribute("Type", "Prefab"));
                obj2.Add(new XAttribute("Name", oc.name));
                obj2.Add(new XAttribute("LinkedTo", entid));
                obj2.Add(new XAttribute("Id", id));
                obj2.Add(new XAttribute("LayerGUID", layerid));
                obj2.Add(new XAttribute("Layer", layername));
                obj2.Add(new XAttribute("PrefabGUID", prefid));
                obj2.Add(new XAttribute("PrefabLibrary", prefablibname));
                obj2.Add(new XAttribute("PrefabName", "ObjectContainers." + oc.name));
                //prefablib = prefabAtt.prefabLibrary;
                prefabs.Add(oc.GetAsPrefab(prefablibname, layerid, prefid, layername));
                Object.Add(obj2);

                foreach (Child child in oc.childs)
                {
                    ObjectContainer ocChild = child.Soc;
                    // Console.WriteLine("ading child {0}", ocChild.name);
                    prefid = GuidUtility.GenID("entprefchild" + ship.name + ocChild.name + parentid);
                    XElement obj3           = new XElement("Object");
                    string   id2            = GuidUtility.GenID("entprefchild2sdfsd" + ship.name + ocChild.name + parentid);
                    string   prefablibname2 = ship.name;
                    obj3.Add(new XAttribute("Type", "Prefab"));
                    obj3.Add(new XAttribute("Name", ocChild.name));
                    obj3.Add(new XAttribute("LinkedTo", entid));
                    obj3.Add(new XAttribute("Id", id2));
                    obj3.Add(new XAttribute("LayerGUID", layerid));
                    obj3.Add(new XAttribute("Layer", layername));
                    obj3.Add(new XAttribute("PrefabGUID", prefid));
                    obj3.Add(new XAttribute("PrefabLibrary", prefablibname2));
                    obj3.Add(new XAttribute("PrefabName", "ObjectContainers." + ocChild.name));
                    //prefablib = prefabAtt.prefabLibrary;
                    prefabs.Add(ocChild.GetAsPrefab(prefablibname2, layerid, prefid, layername));
                    Object.Add(obj3);
                }
            }
            return(Object);
        }