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;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
|
|
using UnityEngine.Networking;
|
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
using BestHTTP;
|
|
|
|
|
|
using Cysharp.Threading.Tasks;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
using PhxhSDK;
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Gameplay.Net
|
|
|
|
|
|
{
|
|
|
|
|
|
public struct AddrInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string ip;
|
|
|
|
|
|
public uint port;
|
|
|
|
|
|
}
|
|
|
|
|
|
public struct ServerInfo
|
|
|
|
|
|
{
|
|
|
|
|
|
public string name;
|
|
|
|
|
|
|
|
|
|
|
|
public AddrInfo[] addrs;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public interface IAppConfigHandler
|
|
|
|
|
|
{
|
|
|
|
|
|
void OnAppConfigDone();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-10-19 15:00:19 +08:00
|
|
|
|
public sealed class AppConfig : Singlenton<AppConfig>, IInitable
|
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
|
|
|
|
|
|
|
|
|
|
|
|
private readonly string appURLPrefix = "https://nldgame.oss-cn-chengdu.aliyuncs.com";// editor/server.0.0.1.xml";
|
|
|
|
|
|
|
|
|
|
|
|
private string m_sAppUrl;
|
|
|
|
|
|
|
|
|
|
|
|
private List<ServerInfo> m_ServerInfos;
|
|
|
|
|
|
|
|
|
|
|
|
public ServerInfo Current { get; private set; }
|
|
|
|
|
|
|
|
|
|
|
|
public void Init()
|
|
|
|
|
|
{
|
|
|
|
|
|
string version = Application.version;
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("AppConfig Current App Version:{0}", version);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
m_sAppUrl = $"{appURLPrefix}/{appDir}/server.{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()
|
|
|
|
|
|
{
|
|
|
|
|
|
return m_ServerInfos;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public ServerInfo GetCurrent()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Current;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Release()
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async UniTask Request()
|
|
|
|
|
|
{
|
2023-10-19 15:00:19 +08:00
|
|
|
|
DebugUtil.Log("AppConfig Request url={0}", m_sAppUrl);
|
2023-08-22 19:08:45 +08:00
|
|
|
|
|
|
|
|
|
|
HTTPRequest request = new HTTPRequest(new Uri(m_sAppUrl), OnRequestFinished);
|
|
|
|
|
|
request.Send();
|
|
|
|
|
|
|
|
|
|
|
|
while(m_ServerInfos.Count <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
await UniTask.Delay(500);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private IAppConfigHandler m_handler;
|
|
|
|
|
|
public void RequestWithCallback(IAppConfigHandler h)
|
|
|
|
|
|
{
|
|
|
|
|
|
m_handler = h;
|
|
|
|
|
|
HTTPRequest request = new HTTPRequest(new Uri(m_sAppUrl), OnRequestFinished);
|
|
|
|
|
|
request.Send();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void OnRequestFinished(HTTPRequest request, HTTPResponse response)
|
|
|
|
|
|
{
|
|
|
|
|
|
switch (request.State)
|
|
|
|
|
|
{
|
|
|
|
|
|
// The request finished without any problem.
|
|
|
|
|
|
case HTTPRequestStates.Finished:
|
|
|
|
|
|
//Debug.Log("Request Finished! Text received: " + response.DataAsText);
|
|
|
|
|
|
OnServerInfo(response.DataAsText);
|
|
|
|
|
|
if(m_handler != null)
|
|
|
|
|
|
m_handler.OnAppConfigDone();
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
string text = "Request Finished with Error! " + (request.Exception != null ? (request.Exception.Message + "\n" + request.Exception.StackTrace) : "No Exception");
|
|
|
|
|
|
Debug.LogError(text);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void 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);
|
|
|
|
|
|
|
|
|
|
|
|
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-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);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(EventManager.Instance != null)
|
|
|
|
|
|
EventManager.Instance.Send(EventManager.EventName.NetLoginServerInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void Select(int nIndex)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (nIndex >= m_ServerInfos.Count)
|
|
|
|
|
|
{
|
|
|
|
|
|
Current = m_ServerInfos[0];
|
2023-10-19 15:00:19 +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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|