Compare commits

...

6 Commits

6 changed files with 211 additions and 188 deletions

View File

@ -130,9 +130,9 @@ namespace AxibugEmuOnline.Client.ClientCore
if (!request.downloadHandler.isDone) if (!request.downloadHandler.isDone)
yield break; yield break;
if (request.downloadHandler.Err != null) if (request.downloadHandler.bHadErr)
{ {
App.log.Error(request.downloadHandler.Err); App.log.Error(request.downloadHandler.ErrInfo);
yield break; yield break;
} }

View File

@ -88,7 +88,7 @@ public static class AxiHttp
public static long index = 0; public static long index = 0;
static int singlePkgMaxRead = 1024; static int singlePkgMaxRead = 1024;
public class WaitAxiRequest : UnityEngine.CustomYieldInstruction public class WaitAxiRequest : UnityEngine.CustomYieldInstruction
{ {
public AxiRespInfo mReqAsync; public AxiRespInfo mReqAsync;
public WaitAxiRequest(AxiRespInfo reqAsync) public WaitAxiRequest(AxiRespInfo reqAsync)
@ -117,7 +117,22 @@ public static class AxiHttp
{ {
public bool isDone = false; public bool isDone = false;
public AxiDownLoadMode downloadMode = AxiDownLoadMode.NotDownLoad; public AxiDownLoadMode downloadMode = AxiDownLoadMode.NotDownLoad;
public string Err = null; public bool bHadErr
{
get
{
return
isDone = true
&&
(
!string.IsNullOrEmpty(ErrInfo)
||
code != 200
);
}
}
public string ErrInfo;
//public string Err = null;
public string host = "";//host主机头 public string host = "";//host主机头
public string url = "";//pathAndQuery public string url = "";//pathAndQuery
public int port = 80; public int port = 80;
@ -231,10 +246,10 @@ public static class AxiHttp
bool foward_302 = true; bool foward_302 = true;
string ourErrMsg = ""; string ourErrMsg = "";
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath,ref ourErrMsg)) if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{ {
Log("ParseURI False"); Log("ParseURI False");
respinfo.Err = ourErrMsg; respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0; respinfo.code = 0;
respinfo.isDone = true; respinfo.isDone = true;
return; return;
@ -496,7 +511,7 @@ public static class AxiHttp
} }
catch (Exception ex) catch (Exception ex)
{ {
respinfo.Err = $"ex : {ex.ToString()}"; respinfo.ErrInfo = $"ex : {ex.ToString()}";
} }
finally finally
{ {
@ -534,7 +549,7 @@ public static class AxiHttp
if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg)) if (!ParseURI(strURI, ref bSSL, ref strHost, ref strIP, ref port, ref strRelativePath, ref ourErrMsg))
{ {
Log("ParseURI False"); Log("ParseURI False");
respinfo.Err = ourErrMsg; respinfo.ErrInfo = ourErrMsg;
respinfo.code = 0; respinfo.code = 0;
respinfo.isDone = true; respinfo.isDone = true;
return; return;
@ -778,7 +793,7 @@ public static class AxiHttp
} }
catch (Exception ex) catch (Exception ex)
{ {
respinfo.Err = $"ex : {ex.ToString()}"; respinfo.ErrInfo = $"ex : {ex.ToString()}";
} }
finally finally
{ {
@ -806,9 +821,9 @@ public static class AxiHttp
{ {
if (i == 0) if (i == 0)
{ {
respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]); respinfo.code = Tools.convertToInt(headers[i].Split(' ')[1]);
if (respinfo.code != 200 && respinfo.code != 301 && respinfo.code != 302)
respinfo.ErrInfo = "code:" + respinfo.code;
} }
else else
{ {
@ -979,11 +994,11 @@ public static class AxiHttp
{ {
return true; return true;
} }
public static bool ParseURI(string strURI, public static bool ParseURI(string strURI,
ref bool bIsSSL, ref bool bIsSSL,
ref string strHost, ref string strHost,
ref string strIP, ref string strIP,
ref int Port, ref int Port,
ref string strRelativePath, ref string strRelativePath,
ref string errMsg) ref string errMsg)
{ {
@ -1043,7 +1058,7 @@ public static class AxiHttp
{ {
strIPRet = GetDnsIP(strAddressRet).ToString(); strIPRet = GetDnsIP(strAddressRet).ToString();
} }
catch(Exception ex) catch (Exception ex)
{ {
errMsg = ex.ToString(); errMsg = ex.ToString();
return false; return false;
@ -1164,7 +1179,6 @@ public static class AxiHttp
Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}"); Log($"BeginConnect {host}:{port} timeoutMSec=>{timeoutMSec}");
tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient); tcpclient.BeginConnect(host, port, new AsyncCallback(CallBackMethod), tcpclient);
if (TimeoutObject.WaitOne(timeoutMSec, false)) if (TimeoutObject.WaitOne(timeoutMSec, false))
{ {
if (IsConnectionSuccessful) if (IsConnectionSuccessful)

View File

@ -36,7 +36,7 @@ namespace AxibugEmuOnline.Client
} }
AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler); AxiHttpProxy.ShowAxiHttpDebugInfo(request.downloadHandler);
if (request.downloadHandler.Err == null) if (!request.downloadHandler.bHadErr)
{ {
Directory.CreateDirectory(path); Directory.CreateDirectory(path);
File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data); File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data);

View File

@ -4,7 +4,6 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using UnityEngine; using UnityEngine;
using UnityEngine.Networking;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
@ -15,27 +14,7 @@ namespace AxibugEmuOnline.Client
public delegate void GetRomListAPI(Action<Resp_GameList> callback, int page, int pageSize = 10); public delegate void GetRomListAPI(Action<Resp_GameList> callback, int page, int pageSize = 10);
public delegate void SearchRomListAPI(Action<Resp_GameList> callback, string searchKey, int page, int pageSize = 10); public delegate void SearchRomListAPI(Action<Resp_GameList> callback, string searchKey, int page, int pageSize = 10);
public static string UrlEncode(string str)
{
StringBuilder sb = new StringBuilder();
foreach (char c in str)
{
if ((c >= '0' && c <= '9') ||
(c >= 'a' && c <= 'z') ||
(c >= 'A' && c <= 'Z') ||
c == '-' || c == '_' || c == '.' || c == '!' || c == '~' || c == '*' || c == '\'' || c == '(' || c == ')')
{
sb.Append(c);
}
else
{
sb.Append('%').Append(((int)c).ToString("X2"));
}
}
return sb.ToString();
}
public void GetNesRomList(Action<Resp_GameList> callback, int page, int pageSize = 10) public void GetNesRomList(Action<Resp_GameList> callback, int page, int pageSize = 10)
{ {
App.StartCoroutine(GetNesRomListFlow(page, pageSize, callback)); App.StartCoroutine(GetNesRomListFlow(page, pageSize, callback));
@ -47,15 +26,25 @@ namespace AxibugEmuOnline.Client
} }
private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<Resp_GameList> callback) private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<Resp_GameList> callback)
{ {
//避免特殊字符和个别文字编码问题
byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
// 将UTF-8编码的字节数组转换回字符串此时是UTF-8编码的字符串
string utf8String = Encoding.UTF8.GetString(utf8Bytes);
searchKey = UrlEncode(utf8String);
App.log.Info($"search->{utf8String} ->{searchKey}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}"); if (!string.IsNullOrEmpty(searchKey))
{
string oldsearch = searchKey;
//searchKey = System.Net.WebUtility.UrlEncode(searchKey);
searchKey = AxiHttp.UrlEncode(searchKey);
App.log.Info($"search->{oldsearch} ->{searchKey}");
//searchKey = HttpUtility.UrlDecode(searchKey);
}
//避免特殊字符和个别文字编码问题
//byte[] gb2312Bytes = Encoding.Default.GetBytes(searchKey);
//byte[] utf8Bytes = Encoding.Convert(Encoding.Default, Encoding.UTF8, gb2312Bytes);
//// 将UTF-8编码的字节数组转换回字符串此时是UTF-8编码的字符串
//string utf8String = Encoding.UTF8.GetString(utf8Bytes);
//searchKey = UrlEncode(utf8String);
//App.log.Info($"search->{utf8String} ->{searchKey}");
string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}";
App.log.Info($"GetRomList=>{url}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get(url);
yield return request.SendWebRequest; yield return request.SendWebRequest;
if (!request.downloadHandler.isDone) if (!request.downloadHandler.isDone)
{ {
@ -63,13 +52,16 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
} }
if (request.downloadHandler.Err != null) if (!request.downloadHandler.bHadErr)
{ {
App.log.Error(request.downloadHandler.Err); var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(null); callback.Invoke(resp);
yield break; yield break;
} }
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/* /*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}"); UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}&SearchKey={searchKey}");
yield return request.SendWebRequest(); yield return request.SendWebRequest();
@ -80,12 +72,12 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
}*/ }*/
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
} }
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback) private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback)
{ {
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}"); string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}";
App.log.Info($"GetRomList=>{url}");
AxiHttpProxy.SendWebRequestProxy request = AxiHttpProxy.Get(url);
yield return request.SendWebRequest; yield return request.SendWebRequest;
if (!request.downloadHandler.isDone) if (!request.downloadHandler.isDone)
{ {
@ -93,12 +85,16 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
} }
if (request.downloadHandler.Err != null) //请求成功
if (!request.downloadHandler.bHadErr)
{ {
App.log.Error(request.downloadHandler.Err); var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(null); callback.Invoke(resp);
yield break; yield break;
} }
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/* /*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}"); UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest(); yield return request.SendWebRequest();
@ -109,8 +105,6 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
} }
*/ */
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
} }
public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback) public IEnumerator GetNesRomInfo(int RomID, Action<Resp_RomInfo> callback)
@ -124,13 +118,17 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
} }
if (request.downloadHandler.Err != null) //成功
if (!request.downloadHandler.bHadErr)
{ {
App.log.Error(request.downloadHandler.Err); var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(null); callback.Invoke(resp);
yield break; yield break;
} }
App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null);
/* /*
UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}"); UnityWebRequest request = UnityWebRequest.Get($"{WebSiteApi}/RomInfo?PType={PlatformType.Nes}&RomID={RomID}");
yield return request.SendWebRequest(); yield return request.SendWebRequest();
@ -141,8 +139,6 @@ namespace AxibugEmuOnline.Client
yield break; yield break;
}*/ }*/
var resp = JsonUtility.FromJson<Resp_RomInfo>(request.downloadHandler.text);
callback.Invoke(resp);
} }
enum PlatformType : byte enum PlatformType : byte

View File

@ -136,7 +136,7 @@ namespace AxibugEmuOnline.Client
var request = downloadRequest; var request = downloadRequest;
downloadRequest = null; downloadRequest = null;
if (request.downloadHandler.Err == null) if (!request.downloadHandler.bHadErr)
callback(request.downloadHandler.data); callback(request.downloadHandler.data);
else else
callback(null); callback(null);

View File

@ -7,147 +7,160 @@ using static AxibugEmuOnline.Client.HttpAPI;
namespace AxibugEmuOnline.Client namespace AxibugEmuOnline.Client
{ {
public class RomLib public class RomLib
{ {
/// <summary> Rom请求,一页的大小 </summary> /// <summary> Rom请求,一页的大小 </summary>
private const int PAGE_SIZE = 10; private const int PAGE_SIZE = 10;
/// <summary> 请求指令 </summary> /// <summary> 请求指令 </summary>
private HashSet<int> FetchPageCmd = new HashSet<int>(); private HashSet<int> FetchPageCmd = new HashSet<int>();
private RomFile[] nesRomFetchList; private RomFile[] nesRomFetchList;
private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>(); private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>(); private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>();
private HttpAPI.GetRomListAPI m_romGetFunc; private HttpAPI.GetRomListAPI m_romGetFunc;
private HttpAPI.SearchRomListAPI m_romSearchFunc; private HttpAPI.SearchRomListAPI m_romSearchFunc;
private EnumSupportEmuPlatform m_platform; private EnumSupportEmuPlatform m_platform;
private string lastSearchKey;
public RomLib(EnumSupportEmuPlatform platform) public RomLib(EnumSupportEmuPlatform platform)
{ {
m_platform = platform; m_platform = platform;
switch (platform) switch (platform)
{ {
case EnumSupportEmuPlatform.NES: case EnumSupportEmuPlatform.NES:
m_romGetFunc = App.httpAPI.GetNesRomList; m_romGetFunc = App.httpAPI.GetNesRomList;
m_romSearchFunc = App.httpAPI.SearchNesRomList; m_romSearchFunc = App.httpAPI.SearchNesRomList;
break; break;
} }
} }
public RomFile GetExistRom(string fileName) public RomFile GetExistRom(string fileName)
{ {
var res = RomFile.CreateExistRom(m_platform, fileName); var res = RomFile.CreateExistRom(m_platform, fileName);
nesRomFileNameMapper[res.FileName] = res; nesRomFileNameMapper[res.FileName] = res;
return res; return res;
} }
public RomFile GetRomFile(string romFileName) public RomFile GetRomFile(string romFileName)
{ {
RomFile romFile; RomFile romFile;
nesRomFileNameMapper.TryGetValue(romFileName, out romFile); nesRomFileNameMapper.TryGetValue(romFileName, out romFile);
return romFile; return romFile;
} }
/// <summary> 清除所有下载的Rom文件 </summary> /// <summary> 清除所有下载的Rom文件 </summary>
public void ClearRomFile() public void ClearRomFile()
{ {
var path = $"{App.PersistentDataPath}/RemoteRoms/{m_platform}"; var path = $"{App.PersistentDataPath}/RemoteRoms/{m_platform}";
if (Directory.Exists(path)) Directory.Delete(path, true); if (Directory.Exists(path)) Directory.Delete(path, true);
} }
/// <summary> 移除一个已下载的Rom </summary> /// <summary> 移除一个已下载的Rom </summary>
public void RemoveOneRomFile(RomFile romFile) public void RemoveOneRomFile(RomFile romFile)
{ {
if (romFile.RomReady) if (romFile.RomReady)
File.Delete(romFile.LocalFilePath); File.Delete(romFile.LocalFilePath);
} }
/// <summary> /// <summary>
/// 获得所有Rom文件 /// 获得所有Rom文件
/// </summary> /// </summary>
/// <param name="callback"></param> /// <param name="callback"></param>
public void FetchRomCount(Action<RomFile[]> callback, string searchKey = null) public void FetchRomCount(Action<RomFile[]> callback, string searchKey = null)
{ {
if (string.IsNullOrWhiteSpace(searchKey)) lastSearchKey = searchKey;
{ if (string.IsNullOrWhiteSpace(searchKey))
m_romGetFunc((romList) => {
{ m_romGetFunc((romList) =>
FetchPageCmd.Clear(); {
nesRomFileIdMapper.Clear(); //TODO 请求失败对于romList为空时的处理
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList); FetchPageCmd.Clear();
}, 0, PAGE_SIZE); nesRomFileIdMapper.Clear();
} nesRomFileNameMapper.Clear();
else nesRomFetchList = new RomFile[romList.resultAllCount];
{ for (int i = 0; i < nesRomFetchList.Length; i++)
m_romSearchFunc((romList) => {
{ //以后考虑用对象池实例化RomFile
FetchPageCmd.Clear(); nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
nesRomFileIdMapper.Clear(); }
nesRomFileNameMapper.Clear(); SaveRomInfoFromWeb(romList);
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList); callback.Invoke(nesRomFetchList);
}, searchKey, 0, PAGE_SIZE); }, 0, PAGE_SIZE);
} }
} else
{
m_romSearchFunc((romList) =>
{
//TODO 请求失败对于romList为空时的处理
public void BeginFetchRomInfo(RomFile romFile) FetchPageCmd.Clear();
{ nesRomFileIdMapper.Clear();
if (romFile.InfoReady) return; nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
FetchPageCmd.Add(romFile.Page); callback.Invoke(nesRomFetchList);
} }, searchKey, 0, PAGE_SIZE);
}
}
public void ExecuteFetchRomInfo() public void BeginFetchRomInfo(RomFile romFile)
{ {
if (FetchPageCmd.Count == 0) return; if (romFile.InfoReady) return;
foreach (var pageNo in FetchPageCmd) FetchPageCmd.Add(romFile.Page);
{ }
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
FetchPageCmd.Clear();
}
private void SaveRomInfoFromWeb(Resp_GameList resp) public void ExecuteFetchRomInfo()
{ {
for (int i = 0; i < resp.gameList.Count; i++) if (FetchPageCmd.Count == 0) return;
{
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
targetRomFile.SetWebData(webData); foreach (var pageNo in FetchPageCmd)
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid]; {
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile; if (!string.IsNullOrEmpty(lastSearchKey))
} {
} m_romSearchFunc(SaveRomInfoFromWeb, lastSearchKey, pageNo, PAGE_SIZE);
}
else
{
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
}
FetchPageCmd.Clear();
}
public static string CalcHash(byte[] data) private void SaveRomInfoFromWeb(Resp_GameList resp)
{ {
return Helper.FileMD5Hash(data); for (int i = 0; i < resp.gameList.Count; i++)
} {
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
public void AddRomFile(RomFile rom) targetRomFile.SetWebData(webData);
{ nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[rom.FileName] = rom; nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
} }
} }
public static string CalcHash(byte[] data)
{
return Helper.FileMD5Hash(data);
}
public void AddRomFile(RomFile rom)
{
nesRomFileNameMapper[rom.FileName] = rom;
}
}
} }