2023-08-22 19:08:45 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
//
|
|
|
|
|
|
// 文件: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;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
using PhxhSDK;
|
2024-05-21 13:33:44 +08:00
|
|
|
|
using PhxhSDK.AOT.VersionUpdate;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Gameplay.Net
|
|
|
|
|
|
{
|
|
|
|
|
|
public struct AddrInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string ip;
|
|
|
|
|
|
public uint port;
|
|
|
|
|
|
}
|
2023-11-20 18:46:43 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public struct ServerInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string name;
|
|
|
|
|
|
|
|
|
|
|
|
public AddrInfo[] addrs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
public class CompulsoryUpdate
|
|
|
|
|
|
{
|
|
|
|
|
|
public bool NeedUpdate;
|
|
|
|
|
|
public Dictionary<string, string> AppUrl;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-05 18:45:58 +08:00
|
|
|
|
public class NoticeInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string Title;
|
|
|
|
|
|
public string Content;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-14 15:56:56 +08:00
|
|
|
|
public enum ACDownloadStatus
|
|
|
|
|
|
{
|
|
|
|
|
|
None = 0,
|
|
|
|
|
|
OK,
|
|
|
|
|
|
Error,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public sealed class AppConfig : Singlenton<AppConfig>, IInitable, ACDownloadHanlder
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
#if UNITY_EDITOR
|
|
|
|
|
|
private readonly string appDir = "editor";
|
|
|
|
|
|
#elif UNITY_ANDROID
|
|
|
|
|
|
private readonly string appDir = "android";
|
|
|
|
|
|
#elif UNITY_IOS
|
|
|
|
|
|
private readonly string appDir = "ios";
|
|
|
|
|
|
#else
|
|
|
|
|
|
private readonly string appDir = "other";
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
private readonly string
|
2024-03-25 16:29:50 +08:00
|
|
|
|
appURLPrefix = "http://132.232.113.98/config"; // editor/server.0.0.1.xml";
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
private string m_sAppUrl;
|
|
|
|
|
|
|
|
|
|
|
|
private List<ServerInfo> m_ServerInfos;
|
|
|
|
|
|
|
2024-08-13 15:19:31 +08:00
|
|
|
|
public string LoginURL { get; private set; }
|
2023-11-20 18:46:43 +08:00
|
|
|
|
|
|
|
|
|
|
public CompulsoryUpdate UpdateInfo { get; private set; }
|
|
|
|
|
|
|
2023-12-05 18:45:58 +08:00
|
|
|
|
public List<NoticeInfo> NoticeInfos { get; private set; }
|
|
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public ServerInfo Current { get; private set; }
|
|
|
|
|
|
|
2024-03-14 15:56:56 +08:00
|
|
|
|
public string URL { get { return m_sAppUrl;}}
|
|
|
|
|
|
|
|
|
|
|
|
public ACDownloadStatus DownloadStatus { get; private set; }
|
|
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
string version = Application.version;
|
2024-05-21 13:33:44 +08:00
|
|
|
|
var workSpace = PackDataInst.inst == null ? "Dev" : PackDataInst.inst.localInfo.workSpace;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("AppConfig Current App Version:{0}", version);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
2024-05-21 13:33:44 +08:00
|
|
|
|
m_sAppUrl = $"{appURLPrefix}/{appDir}/server.{workSpace}.{version}.xml";
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("AppConfig Current App Config Url:{0}", m_sAppUrl);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
m_ServerInfos = new List<ServerInfo>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public List<ServerInfo> GetServerInfo()
|
|
|
|
|
|
{
|
2023-11-20 18:46:43 +08:00
|
|
|
|
return m_ServerInfos;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerInfo GetCurrent()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Current;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Release()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-14 15:56:56 +08:00
|
|
|
|
public async UniTask<ACDownloadStatus> UnityRequest()
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2024-03-14 15:56:56 +08:00
|
|
|
|
GameObject obj = new GameObject();
|
|
|
|
|
|
obj.name = "AppConfigDownload";
|
|
|
|
|
|
var mono = obj.AddComponent<AppConfigDownload>();
|
|
|
|
|
|
mono.Init(this);
|
2023-12-18 18:15:29 +08:00
|
|
|
|
|
2024-03-14 15:56:56 +08:00
|
|
|
|
while (DownloadStatus == ACDownloadStatus.None)
|
2023-12-18 19:47:23 +08:00
|
|
|
|
{
|
2024-03-14 15:56:56 +08:00
|
|
|
|
await UniTask.Yield();
|
|
|
|
|
|
// 在这里执行需要循环检测的操作
|
2023-12-18 19:47:23 +08:00
|
|
|
|
}
|
2024-03-14 15:56:56 +08:00
|
|
|
|
|
|
|
|
|
|
return DownloadStatus;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-03-14 15:56:56 +08:00
|
|
|
|
public void OnRequestFinished(UnityWebRequest request)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
2024-03-14 15:56:56 +08:00
|
|
|
|
DownloadStatus = ACDownloadStatus.Error;
|
2023-12-18 13:15:00 +08:00
|
|
|
|
switch (request.result)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
// The request finished without any problem.
|
2023-12-18 13:15:00 +08:00
|
|
|
|
case UnityWebRequest.Result.Success:
|
|
|
|
|
|
OnServerInfo(request.downloadHandler.text);
|
2024-03-14 15:56:56 +08:00
|
|
|
|
DownloadStatus = ACDownloadStatus.OK;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
break;
|
2024-03-14 15:56:56 +08:00
|
|
|
|
|
2024-01-02 14:00:05 +08:00
|
|
|
|
case UnityWebRequest.Result.ConnectionError:
|
|
|
|
|
|
// TODO, retry several times
|
|
|
|
|
|
DebugUtil.LogError("Request Finished with Error, error = {0} ", request.error);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case UnityWebRequest.Result.ProtocolError:
|
|
|
|
|
|
// TODO, version error
|
|
|
|
|
|
DebugUtil.LogError("Request Finished with Error, error = {0} ", request.error);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case UnityWebRequest.Result.DataProcessingError:
|
|
|
|
|
|
// TODO, version error
|
|
|
|
|
|
DebugUtil.LogError("Request Finished with Error, error = {0} ", request.error);
|
|
|
|
|
|
break;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
default:
|
2024-01-02 14:00:05 +08:00
|
|
|
|
DebugUtil.LogError("Request Finished with Error, error = {0} ", request.error);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnServerInfo(string text)
|
|
|
|
|
|
{
|
|
|
|
|
|
var settings = new XmlReaderSettings();
|
|
|
|
|
|
settings.IgnoreComments = true;
|
2023-12-20 12:08:07 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
var reader = XmlReader.Create(new MemoryStream(Encoding.UTF8.GetBytes(text)), settings);
|
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
|
doc.Load(reader);
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
//获取强更信息
|
|
|
|
|
|
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);
|
2023-12-18 13:15:00 +08:00
|
|
|
|
|
|
|
|
|
|
if (UpdateInfo.NeedUpdate)
|
|
|
|
|
|
{
|
|
|
|
|
|
OpenNeedUpdateWindows();
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
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);
|
|
|
|
|
|
}
|
2024-08-13 15:19:31 +08:00
|
|
|
|
//获取手机登录域名
|
|
|
|
|
|
var LoginUrl = configNode.SelectSingleNode("LoginUrl");
|
|
|
|
|
|
LoginURL = LoginUrl.InnerText;
|
|
|
|
|
|
DebugUtil.Log("AppConfig LoginURL={0}", LoginURL);
|
2023-12-05 18:45:58 +08:00
|
|
|
|
|
|
|
|
|
|
//获取公告信息
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-11-20 18:46:43 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-05 18:45:58 +08:00
|
|
|
|
foreach (var info in NoticeInfos)
|
|
|
|
|
|
{
|
|
|
|
|
|
DebugUtil.Log("AppConfig.NoticeInfo Title={0},Content={1}", info.Title, info.Content);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
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);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
m_ServerInfos.Add(sni);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
if (EventManager.Instance != null)
|
2023-08-22 19:08:45 +08:00
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.NetLoginServerInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-11-20 18:46:43 +08:00
|
|
|
|
|
2023-08-22 19:08:45 +08:00
|
|
|
|
public void Select(int nIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nIndex >= m_ServerInfos.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
Current = m_ServerInfos[0];
|
2023-11-20 18:46:43 +08:00
|
|
|
|
DebugUtil.LogError("AppConfig Select nIndex:{0} >= m_ServerInfos.Count:{1}, set default as 0", nIndex,
|
|
|
|
|
|
m_ServerInfos.Count);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Current = m_ServerInfos[nIndex];
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("AppConfig Select name={0}, length={1}", Current.name, Current.addrs.Length);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
2023-11-20 16:47:49 +08:00
|
|
|
|
|
|
|
|
|
|
private void OpenNeedUpdateWindows()
|
|
|
|
|
|
{
|
2024-05-16 12:40:15 +08:00
|
|
|
|
UIManager.Instance.CreateAndOpenWindow(UINameConst.UI_Update);
|
2023-11-20 16:47:49 +08:00
|
|
|
|
DebugUtil.LogError("需要下载正确客户端版本");
|
|
|
|
|
|
}
|
2023-08-22 19:08:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|