using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Xml; using UnityEngine; public class DisasterInfosManager { private static DisasterInfosManager instance; public static string path; public int currentID = -1; public static DisasterInfosManager Instance { get { if (instance == null) { instance = new DisasterInfosManager(); path = Application.streamingAssetsPath + "/Data/DisasterInfos.xml"; if (!File.Exists(path)) { XmlDocument document = new XmlDocument(); XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes"); document.AppendChild(xmldecl); XmlElement newChild = document.CreateElement("DisasterInfoList"); document.AppendChild(newChild); document.Save(path); document = null; } } return instance; } } /// /// 单机版,获取所有灾情 /// /// public List GetAllDisasterInfos() { List list = new List(); XmlDocument document = new XmlDocument(); document.Load(path); XmlNodeList nodeList = document.SelectSingleNode("DisasterInfoList").ChildNodes; foreach (XmlElement fileNode in nodeList) { DisasterInfo info = new DisasterInfo(); foreach (XmlElement element in fileNode.ChildNodes) { if (element.Name == "Id") { info.Id = int.Parse(element.InnerText); } if (element.Name == "LastTime") { info.LastTime = element.InnerText; } if (element.Name == "Name") { info.Name = element.InnerText; } } /*string id = fileNode.GetElementsByTagName("Id"). info.Id = int.Parse(fileNode.GetAttribute("Id")); info.LastTime = fileNode.GetAttribute("LastTime"); info.Name = fileNode.GetAttribute("Name");*/ list.Add(info); if (info.Id > currentID) { currentID = info.Id; } } return list; } /// /// 获取灾情列表 /// /// public List GetDisasterInfos() { List list = new List(); XmlDocument document = new XmlDocument(); document.Load(path); XmlNodeList nodeList = document.SelectSingleNode("DisasterInfoList").ChildNodes; foreach (XmlElement fileNode in nodeList) { DisasterInfo info = new DisasterInfo(); foreach (XmlElement element in fileNode.ChildNodes) { if (element.Name == "Id") { info.Id = int.Parse(element.InnerText); } if (element.Name == "LastTime") { info.LastTime = element.InnerText; } if (element.Name == "Name") { info.Name = element.InnerText; } } /*string id = fileNode.GetElementsByTagName("Id"). info.Id = int.Parse(fileNode.GetAttribute("Id")); info.LastTime = fileNode.GetAttribute("LastTime"); info.Name = fileNode.GetAttribute("Name");*/ list.Add(info); if (info.Id > currentID) { currentID = info.Id; } } //var newlist = getFilteredList(list, queryOptions.PageSize, queryOptions.PageNumber); //return newlist; return list; } /*private List getFilteredList(List list,int pageSize,int pageNumber) { int count = list.Count; int totalPageCount = Mathf.CeilToInt(count / pageSize); totalPageCount = Mathf.Clamp(totalPageCount, 1, 99); int iniNum = pageSize * (pageNumber - 1); List newList = new List(); if(pageNumber < totalPageCount) { for(int i = iniNum; i < pageSize; i++) { newList.Add(list[i]); } } else { for(int i = iniNum; i < count; i++) { newList.Add(list[i]); } } return newList; }*/ /// /// 添加新灾情 /// /// public void AddNewDisasterInfo(DisasterInfo info) { info.Id = currentID + 1; XmlDocument document = new XmlDocument(); document.Load(path); XmlNode node = document.SelectSingleNode("DisasterInfoList"); XmlElement infoNode = document.CreateElement("DisasterInfo"); /*infoNode.SetAttribute("Id", info.Id.ToString()); infoNode.SetAttribute("LastTime", info.LastTime); infoNode.SetAttribute("Name", info.Name);*/ node.AppendChild(infoNode); XmlElement idNode = document.CreateElement("Id"); idNode.InnerText = info.Id.ToString(); infoNode.AppendChild(idNode); XmlElement LastTimeNode = document.CreateElement("LastTime"); LastTimeNode.InnerText = info.LastTime; infoNode.AppendChild(LastTimeNode); XmlElement nameNode = document.CreateElement("Name"); nameNode.InnerText = info.Name; infoNode.AppendChild(nameNode); document.Save(path); document = null; //在本地创建以灾情ID命名的文件夹 var directoryPath = Application.dataPath + "/Data/" + info.Id.ToString() + "/"; if (Directory.Exists(directoryPath)) { Directory.Delete(directoryPath, true); } Directory.CreateDirectory(directoryPath); //创建空的配置文件 var localFileXml = Application.dataPath + "/Data/" + info.Id.ToString() + "/" + info.Id.ToString() + ".xml"; createLocalFileXml(localFileXml); //重新回到第一页,申请第一页内容 PagesController.Instance.ApplyForInitPage(); } private void createLocalFileXml(string localFileXml) { XmlDocument document = new XmlDocument(); XmlDeclaration xmldecl = document.CreateXmlDeclaration("1.0", "UTF-8", "yes"); document.AppendChild(xmldecl); XmlElement newChild = document.CreateElement("Files"); document.AppendChild(newChild); document.Save(localFileXml); newChild = null; document = null; } /// /// 删除灾情 /// /// public void DeleteDisasterInfo(int delId) { XmlDocument document = new XmlDocument(); document.Load(path); XmlNode node = document.SelectSingleNode("DisasterInfoList"); XmlNodeList nodeList = node.ChildNodes; /*foreach (XmlElement fileNode in nodeList) { int id = int.Parse(fileNode.GetAttribute("Id")); if (id == delId) { node.RemoveChild(fileNode); break; } }*/ foreach (XmlElement fileNode in nodeList) { XmlNodeList nodelist = fileNode.GetElementsByTagName("Id"); XmlElement ele = (XmlElement)nodelist[0]; string id = ele.InnerText; if (id == delId.ToString()) { node.RemoveChild(fileNode); break; } } document.Save(path); document = null; DisasterList.Instance.DeleteDisaster(delId); //删除灾情文件夹 var directoryPath = Application.dataPath + "/Data/" + delId.ToString() + "/"; if (Directory.Exists(directoryPath)) { Directory.Delete(directoryPath, true); } //重新回到第一页,申请第一页内容 PagesController.Instance.ApplyForInitPage(); } /// /// 更新灾情名称 /// /// /// public void UpdateDisasterInfo(int uId, string uName) { XmlDocument document = new XmlDocument(); document.Load(path); XmlNode node = document.SelectSingleNode("DisasterInfoList"); XmlNodeList nodeList = node.ChildNodes; foreach (XmlElement fileNode in nodeList) { XmlElement element = (XmlElement)fileNode.GetElementsByTagName("Id")[0]; if (element.InnerText == uId.ToString()) { XmlElement nodeName = (XmlElement)fileNode.GetElementsByTagName("Name")[0]; nodeName.InnerText = uName; XmlElement nodeTime = (XmlElement)fileNode.GetElementsByTagName("LastTime")[0]; nodeTime.InnerText = DateTime.Now.ToString(); break; } } document.Save(path); document = null; } public void SaveDisasterInfo(string uId) { XmlDocument document = new XmlDocument(); document.Load(path); XmlNode node = document.SelectSingleNode("DisasterInfoList"); XmlNodeList nodeList = node.ChildNodes; foreach (XmlElement fileNode in nodeList) { XmlElement element = (XmlElement)fileNode.GetElementsByTagName("Id")[0]; if (element.InnerText == uId) { XmlElement nodeTime = (XmlElement)fileNode.GetElementsByTagName("LastTime")[0]; nodeTime.InnerText = DateTime.Now.ToString(); break; } } document.Save(path); document = null; } }