using System; using System.IO; using System.Text; using System.Xml; using System.Globalization; using UnityEngine; using System.Linq; using System.Collections.Generic; public static class SaveXML { static int SN = 0; // Call this to get a ready-to-save UTF-8 byte[] (includes marker) 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)) { 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)); //New Without Globals WriteElem(xw, "YOUR_DOORS", GetYourDoors(_Add.YOUR_DOORS)); WriteElem(xw, "YOUR_PANELS", GetYourDoors(_Add.YOUR_PANELS)); WriteElem(xw, "YOUR_TEXTURES", GetYourDoors(_Add.YOUR_TEXTURES)); WriteElem(xw, "ClientInfo", GetClientInfoSafe()); WriteElem(xw, "storezone", _G.StoreZone); if (_G.PATH == "free" || _G.PATH == "laminam"){WriteElem(xw, "CabtextureList", GetYourLibrarySafe());} xw.WriteEndElement(); // GLOBAL // 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(); // CABINETS + OBJECTS xw.WriteRaw(CabinetsXmlSafe()); 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 WriteElem(XmlWriter xw, string name, string value) { xw.WriteStartElement(name); xw.WriteString(value ?? string.Empty); // XmlWriter escapes automatically xw.WriteEndElement(); } static string CabinetsXmlSafe() { SN = 0; var sb = new StringBuilder(); for (int i = 0; i <= _G.OBJnum; i++) { var obj = _G.OBJs[i]; if (obj == null) continue; if (obj.Length == 0) continue; if (obj[2] == "Cabinet" && obj[0] != "null" && IsInsideRoom(obj)) { var tag = $"CABINET{SN}"; sb.Append('<').Append(tag).Append('>'); var idPrefix = (obj[0]?.Length ?? 0) >= 4 ? obj[0].Substring(0, 4) : obj[0]; AppendTag(sb, "O0", $"{idPrefix}{SN}"); // write known range safely for (int n = 1; n < Math.Min(100, obj.Length); n++) { var v = obj[n]; if (v == null) continue; AppendTag(sb, $"O{n}", v); } sb.Append("'); SN++; } } return sb.ToString(); } static string ObjectsXmlSafe() { var sb = new StringBuilder(); for (int i = 0; i <= _G.OBJnum; i++) { var obj = _G.OBJs[i]; if (obj == null) continue; if (obj.Length == 0) continue; if (obj[2] != "Cabinet" && obj[0] != "null" && IsInsideRoom(obj)) { var tag = $"OBJECT{SN}"; sb.Append('<').Append(tag).Append('>'); var idPrefix = (obj[0]?.Length ?? 0) >= 4 ? obj[0].Substring(0, 4) : obj[0]; AppendTag(sb, "O0", $"{idPrefix}{SN}"); for (int n = 1; n < Math.Min(100, obj.Length); n++) { var v = obj[n]; if (string.IsNullOrEmpty(v) || v == "none") continue; AppendTag(sb, $"O{n}", v); } sb.Append("'); SN++; } } return sb.ToString(); } // minimal XML escaping for Raw appends static void AppendTag(StringBuilder sb, string name, string value) { sb.Append('<').Append(name).Append('>'); XmlEscape(sb, value ?? string.Empty); sb.Append("'); } static void XmlEscape(StringBuilder sb, string s) { foreach (var ch in s) { switch (ch) { case '&': sb.Append("&"); break; case '<': sb.Append("<"); break; case '>': sb.Append(">"); break; case '"': sb.Append("""); break; case '\'': sb.Append("'"); break; default: sb.Append(ch); break; } } } static string GetTextureCode(string W) { if (GameObject.Find(W) && W != SceneModeManager.Ceil.name) return Get_TextureCODE(GameObject.Find(W).transform.Find("s1").GetComponent().material); else if (W == SceneModeManager.Ceil.name) return Get_TextureCODE(GameObject.Find(W).transform.GetChild(0).GetComponent().material); return string.Empty; } static string Get_TextureCODE(Material MAT) => TextureCall.getcodeTextureFromMat(MAT); 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 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 GetYourDoors(List YourLibrary) { Debug.Log("YourLibrary=====" + string.Join("__", YourLibrary)); return string.Join("__", YourLibrary); } static string GetLightColorSafe() { if (_G.LightColor == null || _G.LightColor.Count == 0) return string.Empty; return string.Join("?", _G.LightColor.Select(kv => { var hex = ColorUtility.ToHtmlStringRGBA(kv.Value); return $"{kv.Key}:{hex}"; })); } 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 bool IsInsideRoom(string[] ADN) { float px = DOIT.ConvertStringToNumber(ADN[15]); float py = DOIT.ConvertStringToNumber(ADN[16]); float pz = DOIT.ConvertStringToNumber(ADN[17]); if (px > _G.WIDE * 0.5f + 12 || px < -_G.WIDE * 0.5f - 12) return false; if (pz > _G.DEPTH * 0.5f + 12 || pz < -_G.DEPTH * 0.5f - 12) return false; if (py > _G.HEIGHT * 0.5f + 12 || py < -_G.HEIGHT * 0.5f - 12) return false; return true; } } // using System.Collections; // using System.Collections.Generic; // using UnityEngine; // using UnityEngine.UI; // using System.Xml; // using System.Globalization; // using System.Linq; // [RequireComponent(typeof(Button))] // public class SaveXML : MonoBehaviour // { // static int SN = 0; // public static string buildXMLData() // { // //_G.FLCs = string.Join(",", _G.FLC.Select(i => i.ToString()).ToArray()); // string xmlString; // xmlString = ""; // xmlString += ""; // xmlString += "" + _G.PATH + ""; // xmlString += "" + _G.VER + ""; // xmlString += "" + _G.FileName + "";//-----------------------------------------------------------------// // if (SaveCompatibility.Replace_RoomID_with_RefID) // { // xmlString += "" + SaveCompatibility.Replace_RoomID_with_RefID + ""; // } // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + _G.HEIGHT + ""; // xmlString += "" + _G.WIDE + ""; // xmlString += "" + _G.DEPTH + ""; // xmlString += "" + _G.ROOM + ""; // xmlString += "" + _G.SPC + ""; // xmlString += "" + _G.WMKs + ""; // xmlString += "" + _G.WMOs + ""; // xmlString += "" + _G.SUN + ""; // xmlString += "" + _G.OBJnum + ""; // xmlString += "" + _G.PIN + "";//????????????????????? // xmlString += "" + _G.PCC + "";//????????????????????? // xmlString += "" + _G.HCC + "";//????????????????????? // xmlString += "" + _G.NW + ""; // xmlString += "" + GetLightSetting() + ""; // xmlString += "" + GetGlobalSetting(1) + ""; // xmlString += "" + GetGlobalSetting(2) + ""; // xmlString += "" + GetGlobalSetting(3) + ""; // xmlString += "" + GetGlobalSetting(4) + ""; // xmlString += "" + GetGlobalSetting(5) + ""; // xmlString += "" + GetGlobalSetting(6) + ""; // xmlString += "" + GetClientInfo() + ""; // xmlString += "" + _G.StoreZone + ""; // xmlString += "" + _G.BKN + "";//????????????????????? // //Save Cabtexture // if(_G.PATH=="free" || _G.PATH=="laminam" ){ xmlString += "" + GetYourLibrary() + "";} // //xmlString += "" + GetYourLibrary()+ ""; // 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. WallsWidth[i - 1] + ""; // xmlString += "" + _G. WallsPointCenter[i - 1].x + ""; // xmlString += "" + _G. WallsPointCenter[i - 1].y + ""; // xmlString += "" + _G. WallsAngle[i - 1] + ""; // xmlString += "" + GetTextureCode("w" + i.ToString()) + ""; // xmlString += ""; // } // xmlString += ""; // foreach (RectTransform item in WallCreationManager.GetWallPointContainer()) // { // Vector2 position = item.GetComponent().GetAnchoredPosition(); // xmlString += position.x.ToString() + "," + position.y.ToString() + ";"; // } // xmlString += ""; // xmlString += ""; // foreach (RectTransform item in WallCreationManager.GetWallLineContainer()) // { // WallLine wall = item.GetComponent(); // Vector2Int indexPair = new(wall._pointStart.transform.GetSiblingIndex(), wall._pointEnd.transform.GetSiblingIndex()); // xmlString += indexPair.x.ToString() + "," + indexPair.y.ToString() + ";"; // } // xmlString += ""; // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + GetTextureCode("ceil") + ""; // xmlString += "";//-----------------------------------------------------------------// // xmlString += "";//-----------------------------------------------------------------// // xmlString += "" + _G.FLCs + ""; // xmlString += "";//-----------------------------------------------------------------// // xmlString += Cabinets(); // xmlString += Objects(); // xmlString += ""; // return xmlString; // } // public static string Cabinets() // { // SN = 0; // string OS = ""; // string ns = ""; // int n = 0; // for (int i = 0; i < _G.OBJnum + 1; i++) // { // if (_G.OBJs[i] != null) // { // if (_G.OBJs[i][2] == "Cabinet" && _G.OBJs[i][0] != "null" && IsInsideRoom(_G.OBJs[i])) // { // //print("SN====" + SN); // OS += ""; // OS += "" + _G.OBJs[i][0].Substring(0, 4) + SN.ToString() + ""; // for (n = 1; n < 100; n++) // { // ns = n.ToString(); // if (_G.OBJs[i][n] != null) // { // OS += "" + _G.OBJs[i][n] + ""; // } // if(n==83)print("Saving 83===="+_G.OBJs[i][n]); // } // OS += ""; // SN += 1; // } // } // } // return OS; // } // 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" && IsInsideRoom(_G.OBJs[i])) // { // 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 GetTextureCode(string W) // { // if (GameObject.Find(W) && W != SceneModeManager.Ceil.name) // { // return Get_TextureCODE(GameObject.Find(W).transform.Find("s1").gameObject.GetComponent().material); // } // else if (W == SceneModeManager.Ceil.name) // { // return Get_TextureCODE(GameObject.Find(W).transform.GetChild(0).GetComponent().material); // } // return ""; // } // public static string Get_TextureCODE(Material MAT) // { // return TextureCall.getcodeTextureFromMat(MAT); // } // private static byte ToByte(float f) // { // f = Mathf.Clamp01(f); // return (byte)(f * 255); // } // public static string GetYourLibrary(){ // string L=""; // foreach(string Line in _G.UIT_LibraryCabTextures){ // Line.Replace("Unique_Name","UniqueName"); // L+=Line+"_"; // } // print("ListXml===L"+L); // return L[..(L.Length - 1)]; // } // public static string GetLightSetting(){ // string LigthSettingString=""; // foreach(var pair in _G.LightSetting){ // LigthSettingString+=pair.Key+":"+_G.LightSetting[pair.Key].ToString()+"?"; // } // return LigthSettingString[..^1]; // } // public static string GetGlobalSetting(int number){ // if(_G.UIT_LibraryGlobalStart.Count>number+1){//2 3 or 4 // return _G.UIT_LibraryGlobalStart[number]; // } // return "none"; // } // public static string GetLightColor(){ // string LigthColor=""; // foreach(var pair in _G.LightColor){ // string Hex=ColorUtility.ToHtmlStringRGBA(_G.LightColor[pair.Key]); // LigthColor+=pair.Key+":"+Hex+"?"; // } // return LigthColor[..^1]; // } // public static string GetClientInfo(){ // string ClientInfo=""; // foreach(var pair in _G.ClientInfo){ // string Info=_G.ClientInfo[pair.Key]; // ClientInfo+=pair.Key+":"+Info+"?"; // } // return ClientInfo[..^1]; // } // public static bool IsInsideRoom(string[] ADN){ // float px=DOIT.ConvertStringToNumber(ADN[15]); // float py=DOIT.ConvertStringToNumber(ADN[16]); // float pz=DOIT.ConvertStringToNumber(ADN[17]); // if(px>_G.WIDE*0.5f+12 || px<-_G.WIDE*0.5f-12)return false; // if(pz>_G.DEPTH*0.5f+12 || pz<-_G.DEPTH*0.5f-12)return false; // if(py>_G.HEIGHT*0.5f+12 || py<-_G.HEIGHT*0.5f-12)return false; // return true; // } // }