using System; using System.Collections.Generic; using System.Security; using Thousandto.Update.Flow; using Thousandto.Update.Log; using MonoXmlUtils = UnityEngine.Gonbest.MagicCube.MonoXmlUtils; using System.Xml; namespace Thousandto.Update.Xml { /// /// 服务器端配置的xml,用于资源更新 /// public partial class ResourceVersionXml { string _TAG = "ResourceVersion.cs "; //解析xml public int parseResouceVersionXml(string path) { UpdateLog.INFO_LOG(_TAG + "parseResouceVersionXml(string path):" + path); int ret = CodeDefine.RET_SUCCESS; try { var sp = MonoXmlUtils.LoadXmlEx(path); if( sp == null || sp.ToXml() == null ) { return CodeDefine.RET_FAIL_PARSE_RES_XML_FILE; } var dom = sp.ToXml(); //正式流程 parse(dom, "ResourceVersion/VersionBase", NormalFollow.VersionModelBaseList); parse(dom, "ResourceVersion/VersionPatch", NormalFollow.VersionModelPatchList); NormalFollow.LoginIp = parse(dom, "ResourceVersion/loginSever/login_Ip"); NormalFollow.LoginPort = parse(dom, "ResourceVersion/loginSever/login_Port"); NormalFollow.AppVersion = parse(dom, "ResourceVersion/CodeVersion_last/Version"); NormalFollow.ClientUrl = parse(dom, "ResourceVersion/CodeVersion_last/url"); NormalFollow.AppSize = parse(dom, "ResourceVersion/CodeVersion_last/size"); NormalFollow.Language = parse(dom, "ResourceVersion/loginSever/language"); NormalFollow.ServerList = parse(dom, "ResourceVersion/serverListURL"); bool enable = true; if (!Boolean.TryParse(parse(dom, "ResourceVersion/ForceUpdate"), out enable)) { enable = true; } NormalFollow.EnableForceUpdate = enable; NormalFollow.PatchVersion = GetMaxPatchVersion(NormalFollow.VersionModelPatchList); //测试流程 parse(dom, "ResourceVersion/test_tag/VersionBase", TestFollow.VersionModelBaseList); parse(dom, "ResourceVersion/test_tag/VersionPatch", TestFollow.VersionModelPatchList); TestFollow.ServerIp = parse(dom, "ResourceVersion/test_tag/get_client_ip_server_ip"); TestFollow.ServerPort = parse(dom, "ResourceVersion/test_tag/get_client_ip_server_port"); TestFollow.LoginIp = parse(dom, "ResourceVersion/test_tag/login_Ip"); TestFollow.LoginPort = parse(dom, "ResourceVersion/test_tag/login_Port"); TestFollow.BigAppVersion = parse(dom, "ResourceVersion/test_tag/big_app_version"); TestFollow.SmallAppVersion = parse(dom, "ResourceVersion/test_tag/small_app_version"); TestFollow.AppVersion = parse(dom, "ResourceVersion/test_tag/app_current_version"); TestFollow.ClientUrl = parse(dom, "ResourceVersion/test_tag/app_update_url"); TestFollow.AppSize = parse(dom, "ResourceVersion/test_tag/test_size"); TestFollow.Language = parse(dom, "ResourceVersion/test_tag/language"); TestFollow.ServerList = parse(dom, "ResourceVersion/test_tag/serverListURL"); if (!Boolean.TryParse(parse(dom, "ResourceVersion/test_tag/ForceUpdate"), out enable)) { enable = true; } TestFollow.EnableForceUpdate = enable; TestFollow.PatchVersion = GetMaxPatchVersion(TestFollow.VersionModelPatchList); //白名单 mac地址 var macList = parseNodes( dom, "ResourceVersion/test_tag/legal_client_machine_list/legal_client_machine" ); for (int i = 0; macList != null && i < macList.Length; i++) { if( macList[i] == null ) { continue; } WhiteCode.Add(macList[i].Text); } //白名单 用户名 var userList = parseNodes( dom, "ResourceVersion/test_tag/legal_client_user_list/legal_client_user" ); for( int i = 0; userList != null && i < userList.Length; i++ ) { if( userList[i] == null ) { continue; } WhiteUsers.Add( userList[i].Text ); } //白名单 ip地址 var ipList = parseNodes( dom, "ResourceVersion/test_tag/legal_client_ip_list/legal_client_ip" ); for( int i = 0; ipList != null && i < ipList.Length; i++ ) { if( ipList[i] == null ) { continue; } WhiteIp.Add( ipList[i].Text ); } } catch (System.Exception ex) { ret = CodeDefine.RET_FAIL_PARSE_RES_XML_FILE; UpdateLog.ERROR_LOG(_TAG + ex.Message + "/n" + ex.StackTrace); UpdateLog.EXCEPTION_LOG(ex); } return ret; } public string GetMaxPatchVersion(List verList) { int count = verList.Count; if (count > 0) { return verList[count - 1].ToVersion; } return ""; } //解析节点 private void parse(SecurityElement dom, string domPath, List versionList) { var nodes = parseNodes( dom, domPath ); if( nodes == null ) { return; } foreach( SecurityElement item in nodes ) { if( item == null ) { continue; } VersionModel model = new VersionModel(); var fromNode = item.SearchForChildByTag( "FromVersion" ); if( fromNode != null ) { model.FromVersion = fromNode.Text; } var ToVersion = item.SearchForChildByTag( "ToVersion" ); if( ToVersion != null ) { model.ToVersion = ToVersion.Text; } var PatchFile = item.SearchForChildByTag( "PatchFile" ); if( PatchFile != null ) { model.ResourceUrl = PatchFile.Text; } var PatchFileMD5 = item.SearchForChildByTag( "PatchFileMD5" ); if( PatchFileMD5 != null ) { model.Md5 = PatchFileMD5.Text; } var FileSize = item.SearchForChildByTag( "FileSize" ); if( FileSize != null ) { model.FileSize = FileSize.Text; } model.Stage = model.ToVersion; var mapFile = item.SearchForChildByTag( "mapFile" ); if( mapFile != null ) { model.Map_url = mapFile.Text; } var mapFileMD5 = item.SearchForChildByTag( "mapFileMD5" ); if( mapFileMD5 != null ) { model.Map_md5 = mapFileMD5.Text; } var mapFileSize = item.SearchForChildByTag( "mapFileSize" ); if( mapFileSize != null ) { model.Map_size = mapFileSize.Text; } versionList.Add( model ); } } private string parse(SecurityElement dom, string domPath) { if( dom == null || string.IsNullOrEmpty( domPath ) ) { return string.Empty; } SecurityElement node = dom; var subpaths = domPath.Split( '/' ); for( int i = 1; node != null && i < subpaths.Length; ++i ) { var tag = subpaths[i]; node = node.SearchForChildByTag( tag ); } return node != null ? node.Text : string.Empty; } private SecurityElement[] parseNodes(SecurityElement dom, string domPath) { if( dom == null || string.IsNullOrEmpty( domPath ) ) { return null; } SecurityElement node = dom; List retval = null; var subpaths = domPath.Split( '/' ); for( int i = 1; node != null && i < subpaths.Length; ++i ) { var tag = subpaths[i]; if( i + 1 >= subpaths.Length && node != null && node.Children != null ) { foreach( SecurityElement item in node.Children ){ if( item == null ) { continue; } if( item.Tag == tag ) { if( retval == null ) { retval = new List(); } retval.Add( item ); } } break; } node = node.SearchForChildByTag( tag ); } return retval != null ? retval.ToArray() : null; } private void SetNodeInnerText(XmlDocument doc, XmlNode root, string nodePath, string value) { XmlNode xmlNode = root.SelectSingleNode(nodePath); if (xmlNode == null) { xmlNode = doc.CreateElement(nodePath); xmlNode.InnerText = value; root.AppendChild(xmlNode); } else { xmlNode.InnerText = value; } } public void saveNormalFlow(XmlDocument xmlDocument) { try { XmlNode xmlNode = xmlDocument.SelectSingleNode("ResourceVersion"); XmlNodeList xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/VersionBase"); foreach (XmlNode item in xmlNodeList) { item.ParentNode.RemoveChild(item); } foreach (VersionModel item2 in NormalFollow.VersionModelBaseList) { XmlNode xmlNode2 = xmlDocument.CreateElement("VersionBase"); XmlNode xmlNode3 = xmlDocument.CreateElement("FromVersion"); XmlNode xmlNode4 = xmlDocument.CreateElement("ToVersion"); XmlNode xmlNode5 = xmlDocument.CreateElement("PatchFile"); XmlNode xmlNode6 = xmlDocument.CreateElement("PatchFileMD5"); XmlNode xmlNode7 = xmlDocument.CreateElement("FileSize"); xmlNode.AppendChild(xmlNode2); xmlNode2.AppendChild(xmlNode3); xmlNode2.AppendChild(xmlNode4); xmlNode2.AppendChild(xmlNode5); xmlNode2.AppendChild(xmlNode6); xmlNode2.AppendChild(xmlNode7); if (item2.Map_url != null && item2.Map_url != "") { XmlNode xmlNode8 = xmlDocument.CreateElement("mapFile"); XmlNode xmlNode9 = xmlDocument.CreateElement("mapFileMD5"); XmlNode xmlNode10 = xmlDocument.CreateElement("mapFileSize"); xmlNode8.InnerText = item2.Map_url; xmlNode9.InnerText = item2.Map_md5; xmlNode10.InnerText = item2.Map_size; xmlNode2.AppendChild(xmlNode8); xmlNode2.AppendChild(xmlNode9); xmlNode2.AppendChild(xmlNode10); } xmlNode3.InnerText = item2.FromVersion; xmlNode4.InnerText = item2.ToVersion; xmlNode5.InnerText = item2.ResourceUrl; xmlNode6.InnerText = item2.Md5; xmlNode7.InnerText = item2.FileSize; } XmlNodeList xmlNodeList2 = xmlDocument.SelectNodes("ResourceVersion/VersionPatch"); foreach (XmlNode item3 in xmlNodeList2) { item3.ParentNode.RemoveChild(item3); } foreach (VersionModel item4 in NormalFollow.VersionModelPatchList) { XmlNode xmlNode2 = xmlDocument.CreateElement("VersionPatch"); XmlNode xmlNode3 = xmlDocument.CreateElement("FromVersion"); XmlNode xmlNode4 = xmlDocument.CreateElement("ToVersion"); XmlNode xmlNode5 = xmlDocument.CreateElement("PatchFile"); XmlNode xmlNode6 = xmlDocument.CreateElement("PatchFileMD5"); XmlNode xmlNode7 = xmlDocument.CreateElement("FileSize"); xmlNode.AppendChild(xmlNode2); xmlNode2.AppendChild(xmlNode3); xmlNode2.AppendChild(xmlNode4); xmlNode2.AppendChild(xmlNode5); xmlNode2.AppendChild(xmlNode6); xmlNode2.AppendChild(xmlNode7); xmlNode3.InnerText = item4.FromVersion; xmlNode4.InnerText = item4.ToVersion; xmlNode5.InnerText = item4.ResourceUrl; xmlNode6.InnerText = item4.Md5; xmlNode7.InnerText = item4.FileSize; } XmlNodeList xmlNodeList3 = xmlDocument.SelectNodes("ResourceVersion/VersionDll"); if (xmlNodeList3 != null && xmlNodeList3.Count > 0) { foreach (XmlNode item5 in xmlNodeList3) { item5.ParentNode.RemoveChild(item5); } } //foreach (VersionModel item6 in NormalFollow.dllModel) //{ // XmlNode xmlNode2 = xmlDocument.CreateElement("VersionDll"); // XmlNode xmlNode3 = xmlDocument.CreateElement("FromVersion"); // XmlNode xmlNode4 = xmlDocument.CreateElement("ToVersion"); // XmlNode xmlNode5 = xmlDocument.CreateElement("PatchFile"); // XmlNode xmlNode6 = xmlDocument.CreateElement("PatchFileMD5"); // XmlNode xmlNode7 = xmlDocument.CreateElement("FileSize"); // xmlNode.AppendChild(xmlNode2); // xmlNode2.AppendChild(xmlNode3); // xmlNode2.AppendChild(xmlNode4); // xmlNode2.AppendChild(xmlNode5); // xmlNode2.AppendChild(xmlNode6); // xmlNode2.AppendChild(xmlNode7); // xmlNode3.InnerText = item6.fromVersion; // xmlNode4.InnerText = item6.toVersion; // xmlNode5.InnerText = item6.resourceUrl; // xmlNode6.InnerText = item6.md5; // xmlNode7.InnerText = item6.fileSize; //} XmlNode xmlNode13 = xmlDocument.SelectSingleNode("ResourceVersion/CodeVersion_last/Version"); XmlNode xmlNode14 = xmlDocument.SelectSingleNode("ResourceVersion/CodeVersion_last/url"); XmlNode xmlNode15 = xmlDocument.SelectSingleNode("ResourceVersion/CodeVersion_last/size"); XmlNode xmlNode16 = xmlDocument.SelectSingleNode("ResourceVersion/loginSever/login_Ip"); XmlNode xmlNode17 = xmlDocument.SelectSingleNode("ResourceVersion/loginSever/login_Port"); SetNodeInnerText(xmlDocument, xmlNode, "ForceUpdate", string.Concat(NormalFollow.EnableForceUpdate)); XmlNode xmlNode18 = null; try { xmlNode18 = xmlDocument.SelectSingleNode("ResourceVersion/loginSever/language"); if (xmlNode18 != null) { xmlNode18.InnerText = NormalFollow.Language; } else { xmlNode18 = xmlDocument.CreateElement("language"); xmlNode18.InnerText = NormalFollow.Language; XmlNode xmlNode19 = xmlDocument.SelectSingleNode("ResourceVersion/loginSever"); xmlNode19.AppendChild(xmlNode18); } } catch (Exception) { xmlNode18 = xmlDocument.CreateElement("language"); xmlNode18.InnerText = NormalFollow.Language; XmlNode xmlNode19 = xmlDocument.SelectSingleNode("ResourceVersion/loginSever"); xmlNode19.AppendChild(xmlNode18); } xmlNode13.InnerText = NormalFollow.AppVersion; xmlNode14.InnerText = NormalFollow.ClientUrl; if (xmlNode15 != null) { xmlNode15.InnerText = NormalFollow.AppSize; } xmlNode16.InnerText = NormalFollow.LoginIp; xmlNode17.InnerText = NormalFollow.LoginPort; } catch (Exception ex) { UnityEngine.Debug.Log(ex); } } public void saveTestFlow(XmlDocument xmlDocument) { try { XmlNodeList xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/test_tag/VersionBase"); XmlNode xmlNode = xmlDocument.SelectSingleNode("ResourceVersion/test_tag"); SetNodeInnerText(xmlDocument, xmlNode, "ForceUpdate", string.Concat(TestFollow.EnableForceUpdate)); foreach (XmlNode item in xmlNodeList) { item.ParentNode.RemoveChild(item); } foreach (VersionModel item2 in TestFollow.VersionModelBaseList) { XmlNode xmlNode2 = xmlDocument.CreateElement("VersionBase"); XmlNode xmlNode3 = xmlDocument.CreateElement("FromVersion"); XmlNode xmlNode4 = xmlDocument.CreateElement("ToVersion"); XmlNode xmlNode5 = xmlDocument.CreateElement("PatchFile"); XmlNode xmlNode6 = xmlDocument.CreateElement("PatchFileMD5"); XmlNode xmlNode7 = xmlDocument.CreateElement("FileSize"); xmlNode.AppendChild(xmlNode2); xmlNode2.AppendChild(xmlNode3); xmlNode2.AppendChild(xmlNode4); xmlNode2.AppendChild(xmlNode5); xmlNode2.AppendChild(xmlNode6); xmlNode2.AppendChild(xmlNode7); if (item2.Map_url != null && item2.Map_url != "") { XmlNode xmlNode8 = xmlDocument.CreateElement("mapFile"); XmlNode xmlNode9 = xmlDocument.CreateElement("mapFileMD5"); XmlNode xmlNode10 = xmlDocument.CreateElement("mapFileSize"); xmlNode8.InnerText = item2.Map_url; xmlNode9.InnerText = item2.Map_md5; xmlNode10.InnerText = item2.Map_size; xmlNode2.AppendChild(xmlNode8); xmlNode2.AppendChild(xmlNode9); xmlNode2.AppendChild(xmlNode10); } xmlNode3.InnerText = item2.FromVersion; xmlNode4.InnerText = item2.ToVersion; xmlNode5.InnerText = item2.ResourceUrl; xmlNode6.InnerText = item2.Md5; xmlNode7.InnerText = item2.FileSize; } XmlNodeList xmlNodeList2 = xmlDocument.SelectNodes("ResourceVersion/test_tag/VersionPatch"); foreach (XmlNode item3 in xmlNodeList2) { item3.ParentNode.RemoveChild(item3); } foreach (VersionModel item4 in TestFollow.VersionModelPatchList) { XmlNode xmlNode2 = xmlDocument.CreateElement("VersionPatch"); XmlNode xmlNode3 = xmlDocument.CreateElement("FromVersion"); XmlNode xmlNode4 = xmlDocument.CreateElement("ToVersion"); XmlNode xmlNode5 = xmlDocument.CreateElement("PatchFile"); XmlNode xmlNode6 = xmlDocument.CreateElement("PatchFileMD5"); XmlNode xmlNode7 = xmlDocument.CreateElement("FileSize"); xmlNode.AppendChild(xmlNode2); xmlNode2.AppendChild(xmlNode3); xmlNode2.AppendChild(xmlNode4); xmlNode2.AppendChild(xmlNode5); xmlNode2.AppendChild(xmlNode6); xmlNode2.AppendChild(xmlNode7); xmlNode3.InnerText = item4.FromVersion; xmlNode4.InnerText = item4.ToVersion; xmlNode5.InnerText = item4.ResourceUrl; xmlNode6.InnerText = item4.Md5; xmlNode7.InnerText = item4.FileSize; } XmlNodeList xmlNodeList3 = xmlDocument.SelectNodes("ResourceVersion/test_tag/VersionDll"); if (xmlNodeList3 != null && xmlNodeList3.Count > 0) { foreach (XmlNode item5 in xmlNodeList3) { item5.ParentNode.RemoveChild(item5); } } XmlNode xmlNode13 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/big_app_version"); XmlNode xmlNode14 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/small_app_version"); XmlNode xmlNode15 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/login_Ip"); XmlNode xmlNode16 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/login_Port"); XmlNode xmlNode17 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/get_client_ip_server_ip"); XmlNode xmlNode18 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/get_client_ip_server_port"); XmlNode xmlNode19 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/app_current_version"); XmlNode xmlNode20 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/app_update_url"); XmlNode xmlNode21 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/test_size"); XmlNode xmlNode22 = null; try { xmlNode22 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/language"); if (xmlNode22 != null) { xmlNode22.InnerText = TestFollow.Language; } else { xmlNode22 = xmlDocument.CreateElement("language"); xmlNode22.InnerText = TestFollow.Language; xmlNode.AppendChild(xmlNode22); } } catch (Exception) { xmlNode22 = xmlDocument.CreateElement("language"); xmlNode22.InnerText = TestFollow.Language; xmlNode.AppendChild(xmlNode22); } xmlNode15.InnerText = TestFollow.LoginIp; xmlNode16.InnerText = TestFollow.LoginPort; xmlNode20.InnerText = TestFollow.ClientUrl; if (xmlNode21 != null) { xmlNode21.InnerText = TestFollow.AppSize; } xmlNode19.InnerText = TestFollow.AppVersion; xmlNode13.InnerText = TestFollow.BigAppVersion; xmlNode14.InnerText = TestFollow.SmallAppVersion; xmlNode17.InnerText = TestFollow.ServerIp; xmlNode18.InnerText = TestFollow.ServerPort; } catch (Exception ex) { UnityEngine.Debug.Log(ex); } } public void saveNoticeFlow() { //try //{ // XmlDocument xmlDocument = new XmlDocument(); // xmlDocument.Load(m_TempManifestPath); // XmlNode xmlNode = xmlDocument.SelectSingleNode("ResourceVersion"); // XmlNode xmlNode2 = xmlDocument.SelectSingleNode("ResourceVersion/game_notice_title"); // XmlNode xmlNode3 = xmlDocument.SelectSingleNode("ResourceVersion/game_notice_message"); // XmlNodeList xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/notice"); // xmlNode2.InnerText = m_NoticeModel.m_NoticeTitle; // xmlNode3.InnerText = m_NoticeModel.m_NoticeContent; // XmlNodeList xmlNodeList2 = xmlDocument.SelectNodes("ResourceVersion/notice"); // foreach (XmlNode item in xmlNodeList2) // { // item.ParentNode.RemoveChild(item); // } // foreach (TextBox notice in m_NoticeModel.NoticeList) // { // string text = notice.Text; // if (!(text.Trim() == "")) // { // XmlNode xmlNode5 = xmlDocument.CreateElement("notice"); // XmlNode xmlNode6 = xmlDocument.CreateElement("message"); // xmlNode5.AppendChild(xmlNode6); // xmlNode.AppendChild(xmlNode5); // xmlNode6.InnerText = notice.Text; // } // } // xmlDocument.Save(m_TempManifestPath); //} //catch (Exception ex) //{ // Form1.g_Context.ErrorMsg.Text = ex.Message; //} } public void saveWhiteListModel(XmlDocument xmlDocument) { try { XmlNode xmlNode = xmlDocument.SelectSingleNode("ResourceVersion"); XmlNodeList xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/test_tag/legal_client_ip_list/legal_client_ip"); foreach (XmlNode item in xmlNodeList) { item.ParentNode.RemoveChild(item); } xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/test_tag/legal_client_user_list/legal_client_user"); foreach (XmlNode item2 in xmlNodeList) { item2.ParentNode.RemoveChild(item2); } xmlNodeList = xmlDocument.SelectNodes("ResourceVersion/test_tag/legal_client_machine_list/legal_client_machine"); foreach (XmlNode item3 in xmlNodeList) { item3.ParentNode.RemoveChild(item3); } XmlNode xmlNode3 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/legal_client_ip_list"); XmlNode xmlNode4 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/legal_client_machine_list"); XmlNode xmlNode5 = xmlDocument.SelectSingleNode("ResourceVersion/test_tag/legal_client_user_list"); foreach (string mac in WhiteIp) { if (!(mac.Trim() == "")) { XmlNode xmlNode6 = xmlDocument.CreateElement("legal_client_ip"); xmlNode6.InnerText = mac; xmlNode3.AppendChild(xmlNode6); } } foreach (string account in WhiteUsers) { if (!(account.Trim() == "")) { XmlNode xmlNode6 = xmlDocument.CreateElement("legal_client_user"); xmlNode6.InnerText = account; xmlNode5.AppendChild(xmlNode6); } } foreach (string imei in WhiteCode) { if (!(imei.Trim() == "")) { XmlNode xmlNode6 = xmlDocument.CreateElement("legal_client_machine"); xmlNode6.InnerText = imei; xmlNode4.AppendChild(xmlNode6); } } } catch (Exception ex) { UnityEngine.Debug.Log(ex); } } public void saveAll(string path) { XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(path); saveNormalFlow(xmlDocument); saveTestFlow(xmlDocument); saveWhiteListModel(xmlDocument); xmlDocument.Save(path); } } }