using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Xml; using System.Globalization; using System.Linq; using System.Text; public static class SaveXML { static int SN = 0; public static byte[] BuildXmlBytes() { using (var ms = new MemoryStream()) { var settings = new XmlWriterSettings { Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false), Indent = false, NewLineHandling = NewLineHandling.None, CloseOutput = false }; using (var xw = XmlWriter.Create(ms, settings)) { SetGlobal(); xw.WriteStartDocument(); xw.WriteStartElement("Room"); WriteElem(xw, "CLIENT", _G.PATH); WriteElem(xw, "VERSION", _G.VER.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "FILENAME", _G.FileName); //if (SaveCompatibility.Replace_RoomID_with_RefID) WriteElem(xw, "REPLACE_ROOMID", "true"); // GLOBAL xw.WriteStartElement("GLOBAL"); WriteElem(xw, "HEIGHT", _G.HEIGHT.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "WIDE", _G.WIDE.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "DEPTH", _G.DEPTH.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "ROOM", _G.ROOM); WriteElem(xw, "WMKs", _G.WMKs); WriteElem(xw, "WMOs", _G.WMOs); WriteElem(xw, "SUN", _G.SUN); WriteElem(xw, "OBJnum", _G.OBJnum.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "NW", _G.NW.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "LightSetting", GetLightSettingSafe()); WriteElem(xw, "Global1", GetGlobalSettingSafe(1)); WriteElem(xw, "Global2", GetGlobalSettingSafe(2)); WriteElem(xw, "Global3", GetGlobalSettingSafe(3)); WriteElem(xw, "Global4", GetGlobalSettingSafe(4)); WriteElem(xw, "Global5", GetGlobalSettingSafe(5)); WriteElem(xw, "Global6", GetGlobalSettingSafe(6)); WriteElem(xw, "ClientInfo", GetClientInfoSafe()); WriteElem(xw, "storezone", _G.StoreZone); if (_G.PATH == "free" || _G.PATH == "laminam") { WriteElem(xw, "CabtextureList", GetYourLibrarySafe()); } xw.WriteEndElement(); // GLOBAL BuildWallsData_NonClosedButLoopLastToFirst(); // Walls for (int i = 1; i < _G.NW + 1; i++) { xw.WriteStartElement($"W{i}"); WriteElem(xw, "w", _G.WallsWidth[i - 1].ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "px", _G.WallsPointCenter[i - 1].x.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "pz", _G.WallsPointCenter[i - 1].y.ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "wa", _G.WallsAngle[i - 1].ToString(CultureInfo.InvariantCulture)); WriteElem(xw, "texturecode", GetTextureCode("w" + i.ToString())); xw.WriteEndElement(); } // WallPointPositions (robust, no LINQ, null-safe) { var sb = new StringBuilder(); foreach (var node in WallCreationManager.GetWallPointContainer()) { RectTransform rt = null; if (node is RectTransform rtx) rt = rtx; else if (node is Transform tf) rt = tf as RectTransform; else if (node is GameObject go) rt = go.transform as RectTransform; else if (node is Component comp) rt = comp.transform as RectTransform; if (rt == null) continue; Vector2 pos; var wp = rt.GetComponent(); if (wp != null) pos = wp.GetAnchoredPosition(); else pos = rt.anchoredPosition; if (sb.Length > 0) sb.Append(';'); sb.Append(pos.x.ToString(CultureInfo.InvariantCulture)) .Append(',') .Append(pos.y.ToString(CultureInfo.InvariantCulture)); } WriteElem(xw, "WallPointPositions", sb.ToString()); } //WallPointPairings (robust, no LINQ, null-safe) // { // var sb = new StringBuilder(); // foreach (var node in WallCreationManager.GetWallLineContainer()) // { // // Normalize to a Component to access transform + GetComponent // Component comp = null; // if (node is Component c) comp = c; // else if (node is GameObject go) comp = go.transform; // else if (node is Transform tf) comp = tf; // if (comp == null) continue; // var wall = comp.GetComponent(); // if (wall == null || wall._pointStart == null || wall._pointEnd == null) continue; // int a = wall._pointStart.transform.GetSiblingIndex(); // int b = wall._pointEnd.transform.GetSiblingIndex(); // if (sb.Length > 0) sb.Append(';'); // sb.Append(a.ToString(CultureInfo.InvariantCulture)) // .Append(',') // .Append(b.ToString(CultureInfo.InvariantCulture)); // } // WriteElem(xw, "WallPointPairings", sb.ToString()); // } // CEIL xw.WriteStartElement("CEIL"); WriteElem(xw, "texturecode", GetTextureCode("ceil")); xw.WriteEndElement(); // FLOOR xw.WriteStartElement("FLOOR"); WriteElem(xw, "code", _G.FLCs ?? string.Empty); xw.WriteEndElement(); //xw.WriteRaw(ObjectsXmlSafe()); xw.WriteComment(" END "); // footer marker xw.WriteEndElement(); // Room xw.WriteEndDocument(); xw.Flush(); } // return AFTER disposing XmlWriter, inside the MemoryStream using return ms.ToArray(); } } // ---------- helpers ---------- static void SetGlobal() { _G.WallsWidth = new List(_G.wW); _G.WallsPointCenter=new List(_G.wCP); //G.wCP _G.WallsAngle=new List(_G.wa); } static void WriteElem(XmlWriter xw, string name, string value) { xw.WriteStartElement(name); xw.WriteString(value ?? string.Empty); // XmlWriter escapes automatically xw.WriteEndElement(); } static string GetLightSettingSafe() { if (_G.LightSetting == null || _G.LightSetting.Count == 0) return string.Empty; return string.Join("?", _G.LightSetting.Select(kv => $"{kv.Key}:{kv.Value.ToString()}")); } static string GetGlobalSettingSafe(int number) { if (_G.UIT_LibraryGlobalStart != null && number >= 0 && number < _G.UIT_LibraryGlobalStart.Count) { var val = _G.UIT_LibraryGlobalStart[number]; return string.IsNullOrWhiteSpace(val) ? "none" : val; } return "none"; } static string GetClientInfoSafe() { if (_G.ClientInfo == null || _G.ClientInfo.Count == 0) return string.Empty; return string.Join("?", _G.ClientInfo.Select(kv => $"{kv.Key}:{(kv.Value ?? string.Empty)}")); } static string GetYourLibrarySafe() { if (_G.UIT_LibraryCabTextures == null || _G.UIT_LibraryCabTextures.Count == 0) return string.Empty; var cleaned = _G.UIT_LibraryCabTextures .Select(line => (line ?? string.Empty).Replace("Unique_Name", "UniqueName")) .ToArray(); return string.Join("_", cleaned); } static string GetTextureCode(string W) { string Color = "#ffffff"; return "none," + Color + ",#A6A6A6,#ffffff,0.25-0.25,0,1,0.25,0,0"; } public static void BuildWallsData_NonClosedButLoopLastToFirst() { var pts = _G.WallPoints2D; // List if (pts == null || pts.Count < 2) return; int nPts = pts.Count; // Nombre de murs = nombre de points (car on ferme dernier->premier) _G.NW = nPts; EnsureSize(ref _G.WallsWidth, _G.NW); EnsureSize(ref _G.WallsPointCenter, _G.NW); EnsureSize(ref _G.WallsAngle, _G.NW); for (int i = 0; i < _G.NW; i++) { Vector2 a = pts[i]; Vector2 b = (i == nPts - 1) ? pts[0] : pts[i + 1]; // ferme le dernier mur Vector2 ab = b - a; // Largeur (longueur) du mur _G.WallsWidth[i] = ab.magnitude; // Centre du mur _G.WallsPointCenter[i] = (a + b) * 0.5f; // Angle 0..360 (0 = +X, 90 = +Y) float ang = Mathf.Atan2(ab.y, ab.x) * Mathf.Rad2Deg; if (ang < 0f) ang += 360f; _G.WallsAngle[i] = ang; } } private static void EnsureSize(ref List list, int size) { if (list == null) list = new List(size); if (list.Count > size) list.RemoveRange(size, list.Count - size); while (list.Count < size) list.Add(default); } } // setGlobal123(); // string colorWalls=ColorWall(); // _G.FLCs =FloorCode(); // //_G.FLCs = string.Join(",", _G.FLC.Select(i => i.ToString()).ToArray()); // string xmlString; // xmlString = ""; // xmlString += ""; // xmlString += "" + _G.FileName + "";//-----------------------------------------------------------------// // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + _G.HEIGHT + ""; // xmlString += "" + _G.WIDE + ""; // xmlString += "" + _G.DEPTH + ""; // xmlString += "" + _G.ROOM + ""; // xmlString += "" + _G.NW + ""; // xmlString += "" + _G.Ds + ""; // xmlString += "" + _G.Ws + ""; // xmlString += "" + _G.CTC + ""; // xmlString += "" + _G.SPC + ""; // xmlString += "" + _G.BSC + ""; // xmlString += "" + _G.KNCs + ""; // xmlString += "" + _G.WMKs + ""; // xmlString += "" + _G.WMOs + ""; // xmlString += "" + _G.SUN + ""; // xmlString += "" + _G.OBJnum + ""; // xmlString += "" + _G.PIN + ""; // xmlString += "" + _G.DCC + ""; // xmlString += "" + _G.BCC + ""; // xmlString += "" + _G.PCC + ""; // xmlString += "" + _G.KCC + ""; // xmlString += "" + _G.OCC + ""; // xmlString += "" + _G.UCC + ""; // xmlString += "" + _G.HCC + ""; // //Version universal // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + true + ""; // xmlString += "" + _G.DOORCAB1 + ""; // xmlString += "" + _G.DOORCAB2 + ""; // xmlString += "" + _G.DOORCAB3 + ""; // xmlString += "" + _G.INTPNL1 + ""; // xmlString += "" + _G.INTPNL2 + ""; // xmlString += "" + _G.INTPNL3 + ""; // xmlString += "" + _G.EXTPNL1 + ""; // xmlString += "" + _G.EXTPNL2 + ""; // xmlString += "" + _G.EXTPNL3 + ""; // xmlString += "" + _G.KICK1 + ""; // xmlString += "" + _G.KICK2 + ""; // xmlString += "" + _G.KICK3 + ""; // xmlString += "" + _G.FACIA1 + ""; // xmlString += "" + _G.FACIA2 + ""; // xmlString += "" + _G.FACIA3 + ""; // xmlString += "" + _G.OGEE1 + ""; // xmlString += "" + _G.OGEE2 + ""; // xmlString += "" + _G.OGEE3 + ""; // xmlString += "" + _G.VALA1 + ""; // xmlString += "" + _G.VALA2 + ""; // xmlString += "" + _G.VALA3 + ""; // xmlString += "" + _G.KNOB1 + ""; // xmlString += "" + _G.KNOB2 + ""; // xmlString += "" + _G.KNOB3 + ""; // xmlString += "" + _G.COUN1 + ""; // xmlString += "" + _G.COUN2 + ""; // xmlString += "" + _G.COUN3 + ""; // xmlString += "" + _G.COUN1 + ""; // xmlString += "" + _G.COUN2 + ""; // xmlString += "" + _G.COUN3 + ""; // xmlString += "" + _G.KNOBT1 + ""; // xmlString += "" + _G.KNOBT2 + ""; // xmlString += "" + _G.KNOBT3 + ""; // xmlString += "" + _G.ClientInfo + ""; // xmlString += "" + _G.status + ""; // xmlString += "";//-----------------------------------------------------------------// // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + _G.Camx + ""; // xmlString += "" + _G.Camy + ""; // xmlString += "" + _G.Camz + ""; // xmlString += "" + _G.CamA + ""; // xmlString += "" + _G.CamWA + ""; // xmlString += "";//-----------------------------------------------------------------// // for(int i=1; i<_G.NW+1; i++){ // xmlString += ""; // xmlString += "" + _G.wW[i-1] + ""; // xmlString += "" + _G.wCP[i-1].x + ""; // xmlString += "" + _G.wCP[i-1].y + ""; // xmlString += "" + _G.wa[i-1] + ""; // xmlString += "" + _G.Wallcolor + ""; // xmlString += "" + "none" + ""; // xmlString += "" + colorWalls +""; // xmlString += ""; // } // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + "ffffff" + ""; // xmlString += "" + "none" + ""; // xmlString += "" + _G.ceiltsw + ""; // xmlString += "" + _G.ceiltsh + ""; // xmlString += "" + "null,0.25,0.5,ffffff,ffffff,0,0,0.5,ffffff,1" +""; // xmlString += "";//-----------------------------------------------------------------// // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + _G.FLCs + ""; // xmlString += "";//-----------------------------------------------------------------// // //xmlString += Cabinets(); // xmlString += Objects(); // xmlString += ""; // print("_G.Wallcolor==="+_G.Wallcolor); // print("_G.DOORCAB1==="+_G.DOORCAB1); // print("_G.EXTPNL1==="+_G.EXTPNL1); // return xmlString; // } // public static string Objects() // { // string OSo = ""; // string nso = ""; // int no = 0; // print("Save===object====" + _G.OBJnum); // for (int i = 0; i < _G.OBJnum + 1; i++) // { // print("Save===object i==" + i); // if (_G.OBJs[i] != null) // { // print("Save===object name==" + _G.OBJs[i][0]); // if (_G.OBJs[i][2] != "Cabinet" && _G.OBJs[i][0] != "null") // { // OSo += ""; // OSo += "" + _G.OBJs[i][0].Substring(0, 4) + SN.ToString() + ""; // for (no = 1; no < 100; no++) // { // nso = no.ToString(); // if (_G.OBJs[i][no] != null && _G.OBJs[i][no] != "none") // { // OSo += "" + _G.OBJs[i][no] + ""; // } // } // OSo += ""; // SN += 1; // } // } // } // return OSo; // } // public static string getColor(string W){ // Color c = new Color(); // if (GameObject.Find(W)) { c = GameObject.Find(W).transform.Find("s1").gameObject.GetComponent().material.color; } // return string.Format("{0:X2}{1:X2}{2:X2}", ToByte(c.r), ToByte(c.g), ToByte(c.b)); // } // public static string getColorCeil(string W){ // //Color c = new Color(); // //c = GameObject.Find(W).gameObject.GetComponent().material.color; // //return string.Format("{0:X2}{1:X2}{2:X2}", ToByte(c.r), ToByte(c.g), ToByte(c.b)); // return "ffffff"; // } // private static byte ToByte(float f){ // f = Mathf.Clamp01(f); // return (byte)(f * 255); // } // public static string getTexture(string W){ // string T = GameObject.Find("w1").gameObject.GetComponent().sharedMaterial.mainTexture.name; // if (GameObject.Find(W)) { T = GameObject.Find(W).gameObject.GetComponent().sharedMaterial.mainTexture.name; ; } // return T; // } // public static void setGlobal123(){ // _G.DOORCAB1="7031 L105C"; // _G.DOORCAB2="7035 802SM"; // _G.DOORCAB3="7035 K13"; // _G.INTPNL1 = "MELAMINEWHITE"+"?"+"90";//INT // _G.INTPNL2 = "MELAMINEWHITE"+"?"+"90";//INT // _G.INTPNL3 = "MELAMINEWHITE"+"?"+"90";//INT // _G.EXTPNL1 = "123"+"?"+"0"; // _G.EXTPNL2 = "319"+"?"+"0";//EXT // _G.EXTPNL3 = "3021"+"?"+"0";//EXT // _G.KICK1 = "123"+"?"+"90"+"?"+"4.5"; // _G.KICK2 = "319"+"?"+"90"+"?"+"4.5"; // _G.KICK3 = "3021"+"?"+"90"+"?"+"4.5"; // _G.KNOB1= "KN.18?V"; // _G.KNOB2= "KN.1?V"; // _G.KNOB3= "KN.3?V"; // _G.FACIA1 = "NONE" + "?" + "90" + "?" + "6"; // _G.FACIA2 = "NONE" + "?" + "90" + "?" + "6"; // _G.FACIA3 = "NONE" + "?" + "90" + "?" + "6"; // _G.OGEE1 = "NONE" + "?" + "90" + "?" + "3" + "?" + "0"; // _G.OGEE2 = "NONE" + "?" + "90" + "?" + "3" + "?" + "0"; // _G.OGEE3 = "NONE" + "?" + "90" + "?" + "3" + "?" + "0"; // _G.VALA1 = "NONE" + "?" + "90" + "?" + "6"; // _G.VALA2 = "NONE" + "?" + "90" + "?" + "6"; // _G.VALA3 = "NONE" + "?" + "90" + "?" + "6"; // _G.COUN1 = "Ardoise Basalte,1,1,FFFFFF,FFFFFF,1,90,S,ffffff,1,none"; // _G.COUN2 = "Argento Romano1,1,FFFFFF,FFFFFF,1,90,S,ffffff,1,none"; // _G.COUN3 = "Blanc,1,1,FFFFFF,FFFFFF,1,90,S,ffffff,1,none"; // _G.SPLA1 = "SPLA.1,4,4,FFFFFF,FFFFFF,NA,r90,S,ffffff,1"; // _G.SPLA2 = "SPLA.2,4,4,FFFFFF,FFFFFF,NA,r90,S,ffffff,1"; // _G.SPLA3 = "SPLA.3,4,4,FFFFFF,FFFFFF,NA,r90,S,ffffff,1"; // string HEX=_G.Wallcolor; // //_G.Wallcolor="null,0.25,0.5,"+ HEX+","+ HEX+",0,0,0.5,ffffff,1"; // } // public static string ColorWall(){ // return "null,0.25,0.5,"+ _G.Wallcolor+","+ _G.Wallcolor+",0,0,0.5,ffffff,1"; // } // public static string FloorCode(){ // return "CERA.12,1.085874,0.3857305,F9E9DE,421000,12.95484,0,0.8905505,ffffff,1.381024"; // } // }