231 lines
7.2 KiB
C#
231 lines
7.2 KiB
C#
//////////////////////////////////////////////////////////////////////////
|
||
//
|
||
// 文件:Assets/Code/Scripts/Gameplay/Net/Client/AppConfig.cs
|
||
// 作者:Xoen Xie
|
||
// 时间:2023/07/19
|
||
// 描述:app config
|
||
// 说明:
|
||
//
|
||
//////////////////////////////////////////////////////////////////////////
|
||
|
||
using Framework;
|
||
using UnityEngine;
|
||
using System.Collections.Generic;
|
||
using UnityEngine.Networking;
|
||
using System.Xml;
|
||
using System;
|
||
using System.IO;
|
||
using System.Text;
|
||
using Cysharp.Threading.Tasks;
|
||
using PhxhSDK;
|
||
using PhxhSDK.AOT.Net;
|
||
using PhxhSDK.AOT.PreConfig;
|
||
|
||
namespace Gameplay.Net
|
||
{
|
||
public struct AddrInfo
|
||
{
|
||
public string ip;
|
||
public uint port;
|
||
}
|
||
|
||
public struct ServerInfo
|
||
{
|
||
public string name;
|
||
|
||
public AddrInfo[] addrs;
|
||
}
|
||
|
||
public class CompulsoryUpdate
|
||
{
|
||
public bool NeedUpdate;
|
||
public Dictionary<string, string> AppUrl;
|
||
}
|
||
|
||
/*public class NoticeInfo
|
||
{
|
||
public string Title;
|
||
public string Content;
|
||
}*/
|
||
|
||
public sealed class AppConfig : Singlenton<AppConfig>, IInitable
|
||
{
|
||
private List<ServerInfo> m_ServerInfos;
|
||
|
||
public string LoginURL { get; private set; }
|
||
|
||
public CompulsoryUpdate UpdateInfo { get; private set; }
|
||
|
||
/*public List<NoticeInfo> NoticeInfos { get; private set; }*/
|
||
|
||
public ServerInfo Current { get; private set; }
|
||
|
||
public void Init()
|
||
{
|
||
string version = Application.version;
|
||
DebugUtil.Log("AppConfig Current App Version:{0}", version);
|
||
|
||
//m_sAppUrl = PreConfig.instance.appConfigUrl;
|
||
//DebugUtil.Log("AppConfig Current App Config Url:{0}", m_sAppUrl);
|
||
|
||
m_ServerInfos = new List<ServerInfo>();
|
||
#if UNITY_EDITOR
|
||
ServerInfo local = new ServerInfo();
|
||
local.name = "本地服";
|
||
local.addrs = new AddrInfo[2];
|
||
AddrInfo a1;
|
||
a1.ip = "127.0.0.1";
|
||
a1.port = 40000;
|
||
local.addrs[0] = a1;
|
||
a1.port = 40001;
|
||
local.addrs[1] = a1;
|
||
m_ServerInfos.Add(local);
|
||
#endif
|
||
}
|
||
|
||
public List<ServerInfo> GetServerInfo()
|
||
{
|
||
return m_ServerInfos;
|
||
}
|
||
|
||
public ServerInfo GetCurrent()
|
||
{
|
||
return Current;
|
||
}
|
||
|
||
public void Release()
|
||
{
|
||
NoticeManager.Instance.Release();
|
||
}
|
||
|
||
public async UniTask<bool> UnityRequest()
|
||
{
|
||
var timeTick = DateTime.Now.Ticks;
|
||
var forceRequest = new ForceRequest(await PreConfig.instance.GetAppConfigUrl());
|
||
var result = await forceRequest.StartRequest();
|
||
|
||
var timeSpan = new TimeSpan(DateTime.Now.Ticks - timeTick);
|
||
DebugUtil.Log($"[AppConfig] DownloadConfig Time:{timeSpan.TotalMilliseconds} ms");
|
||
|
||
if (string.IsNullOrEmpty(result))
|
||
{
|
||
Debug.LogError("[AppConfig] Download Config Failed");
|
||
return false;
|
||
}
|
||
|
||
return OnServerInfo(result);
|
||
}
|
||
|
||
private bool OnServerInfo(string text)
|
||
{
|
||
var settings = new XmlReaderSettings();
|
||
settings.IgnoreComments = true;
|
||
|
||
var reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(text)), settings);
|
||
var doc = new XmlDocument();
|
||
doc.Load(reader);
|
||
|
||
//获取强更信息
|
||
UpdateInfo = new CompulsoryUpdate();
|
||
var configNode = doc.SelectSingleNode("root/config");
|
||
UpdateInfo.NeedUpdate = bool.Parse(configNode.SelectSingleNode("AppUpdate").InnerText);
|
||
DebugUtil.Log("Need to force update:{0}", UpdateInfo.NeedUpdate);
|
||
|
||
if (UpdateInfo.NeedUpdate)
|
||
{
|
||
OpenNeedUpdateWindows();
|
||
return true;
|
||
}
|
||
|
||
var appUrls = configNode.SelectNodes("AppUrl/u");
|
||
UpdateInfo.AppUrl = new Dictionary<string, string>();
|
||
foreach (XmlNode urlNode in appUrls)
|
||
{
|
||
string type = urlNode.Attributes["type"].Value;
|
||
string data = urlNode.Attributes["data"].Value;
|
||
UpdateInfo.AppUrl.Add(type, data);
|
||
}
|
||
|
||
//获取手机登录域名
|
||
var LoginUrl = configNode.SelectSingleNode("LoginUrl");
|
||
LoginURL = LoginUrl.InnerText;
|
||
DebugUtil.Log("AppConfig LoginURL={0}", LoginURL);
|
||
|
||
/*//获取公告信息
|
||
NoticeInfos = new List<NoticeInfo>();
|
||
var noticeNode = doc.SelectSingleNode("root/notice");
|
||
if (noticeNode != null)
|
||
{
|
||
var noticeList = noticeNode.ChildNodes;
|
||
for (int i = 0; i < noticeList.Count; i++)
|
||
{
|
||
var noticeElement = noticeList[i] as XmlElement;
|
||
NoticeInfo noticeInfo = new NoticeInfo();
|
||
noticeInfo.Title = noticeElement.SelectSingleNode("title").InnerText;
|
||
noticeInfo.Content = noticeElement.SelectSingleNode("body").InnerText;
|
||
NoticeInfos.Add(noticeInfo);
|
||
}
|
||
}
|
||
|
||
foreach (var info in NoticeInfos)
|
||
{
|
||
DebugUtil.Log("AppConfig.NoticeInfo Title={0},Content={1}", info.Title, info.Content);
|
||
}
|
||
*/
|
||
|
||
NoticeManager.Instance.GetNotice();
|
||
|
||
var rootNode = doc.SelectSingleNode("root");
|
||
var nodeList = rootNode.SelectSingleNode("ServerList").ChildNodes;
|
||
for (var i = 0; i < nodeList.Count; ++i)
|
||
{
|
||
var ele = nodeList[i] as XmlElement;
|
||
|
||
ServerInfo sni;
|
||
sni.name = ele.GetAttribute("name");
|
||
sni.addrs = new AddrInfo[ele.ChildNodes.Count];
|
||
|
||
for (var J = 0; J < ele.ChildNodes.Count; J++)
|
||
{
|
||
var eleAddr = ele.ChildNodes[J] as XmlElement;
|
||
AddrInfo adi;
|
||
adi.ip = eleAddr.GetAttribute("ip");
|
||
string port = eleAddr.GetAttribute("port");
|
||
adi.port = Convert.ToUInt32(port);
|
||
sni.addrs[J] = adi;
|
||
}
|
||
|
||
DebugUtil.Log("AppConfig.OnServerInfo name={0}, count={1} ip={2}, port={3}",
|
||
sni.name, sni.addrs.Length, sni.addrs[0].ip, sni.addrs[0].port);
|
||
|
||
m_ServerInfos.Add(sni);
|
||
}
|
||
|
||
if (EventManager.Instance != null)
|
||
EventManager.Instance.Send(EventManager.EventName.NetLoginServerInfo);
|
||
|
||
return true;
|
||
}
|
||
|
||
|
||
public void Select(int nIndex)
|
||
{
|
||
if (nIndex >= m_ServerInfos.Count)
|
||
{
|
||
Current = m_ServerInfos[0];
|
||
DebugUtil.LogError("AppConfig Select nIndex:{0} >= m_ServerInfos.Count:{1}, set default as 0", nIndex,
|
||
m_ServerInfos.Count);
|
||
return;
|
||
}
|
||
|
||
Current = m_ServerInfos[nIndex];
|
||
DebugUtil.Log("AppConfig Select name={0}, length={1}", Current.name, Current.addrs.Length);
|
||
}
|
||
|
||
private void OpenNeedUpdateWindows()
|
||
{
|
||
UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_Update);
|
||
DebugUtil.LogError("需要下载正确客户端版本");
|
||
}
|
||
}
|
||
} |