修复一些拉取rom列表的bug

This commit is contained in:
ALIENJACK\alien 2025-01-06 11:59:06 +08:00
parent 194150b911
commit 3bbb347e84
2 changed files with 191 additions and 172 deletions

View File

@ -1,4 +1,4 @@
using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.ClientCore;
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
@ -12,21 +12,20 @@ namespace AxibugEmuOnline.Client
public string WebHost = "http://emu.axibug.com"; public string WebHost = "http://emu.axibug.com";
public string WebSiteApi => WebHost + "/api"; public string WebSiteApi => WebHost + "/api";
public delegate void GetRomListAPI(Action<Resp_GameList> callback, int page, int pageSize = 10); public delegate void GetRomListAPI(Action<int, 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<int, Resp_GameList> callback, string searchKey, int page, int pageSize = 10);
public void GetNesRomList(Action<Resp_GameList> callback, int page, int pageSize = 10) public void GetNesRomList(Action<int, Resp_GameList> callback, int page, int pageSize = 10)
{ {
App.StartCoroutine(GetNesRomListFlow(page, pageSize, callback)); App.StartCoroutine(GetNesRomListFlow(page, pageSize, callback));
} }
public void SearchNesRomList(Action<Resp_GameList> callback, string searchKey, int page, int pageSize = 10) public void SearchNesRomList(Action<int, Resp_GameList> callback, string searchKey, int page, int pageSize = 10)
{ {
App.StartCoroutine(SearchNesRomListFlow(searchKey, page, pageSize, callback)); App.StartCoroutine(SearchNesRomListFlow(searchKey, page, pageSize, callback));
} }
private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<Resp_GameList> callback) private IEnumerator SearchNesRomListFlow(string searchKey, int page, int pageSize, Action<int, Resp_GameList> callback)
{ {
if (!string.IsNullOrEmpty(searchKey)) if (!string.IsNullOrEmpty(searchKey))
{ {
string oldsearch = searchKey; string oldsearch = searchKey;
@ -48,19 +47,19 @@ namespace AxibugEmuOnline.Client
yield return request.SendWebRequest; yield return request.SendWebRequest;
if (!request.downloadHandler.isDone) if (!request.downloadHandler.isDone)
{ {
callback.Invoke(null); callback.Invoke(page, null);
yield break; yield break;
} }
if (!request.downloadHandler.bHadErr) if (!request.downloadHandler.bHadErr)
{ {
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text); var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp); callback.Invoke(page, resp);
yield break; yield break;
} }
App.log.Error(request.downloadHandler.ErrInfo); App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null); callback.Invoke(page, 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}");
@ -73,7 +72,7 @@ namespace AxibugEmuOnline.Client
}*/ }*/
} }
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback) private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<int, Resp_GameList> callback)
{ {
string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}"; string url = $"{WebSiteApi}/NesRomList?Page={page}&PageSize={pageSize}";
App.log.Info($"GetRomList=>{url}"); App.log.Info($"GetRomList=>{url}");
@ -81,7 +80,7 @@ namespace AxibugEmuOnline.Client
yield return request.SendWebRequest; yield return request.SendWebRequest;
if (!request.downloadHandler.isDone) if (!request.downloadHandler.isDone)
{ {
callback.Invoke(null); callback.Invoke(page, null);
yield break; yield break;
} }
@ -89,12 +88,12 @@ namespace AxibugEmuOnline.Client
if (!request.downloadHandler.bHadErr) if (!request.downloadHandler.bHadErr)
{ {
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text); var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp); callback.Invoke(page, resp);
yield break; yield break;
} }
App.log.Error(request.downloadHandler.ErrInfo); App.log.Error(request.downloadHandler.ErrInfo);
callback.Invoke(null); callback.Invoke(page, 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();

View File

@ -6,161 +6,181 @@ using System.IO;
using static AxibugEmuOnline.Client.HttpAPI; 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; 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)
{ {
lastSearchKey = searchKey; lastSearchKey = searchKey;
if (string.IsNullOrWhiteSpace(searchKey)) if (string.IsNullOrWhiteSpace(searchKey))
{ {
m_romGetFunc((romList) => m_romGetFunc((page, romList) =>
{ {
//TODO 请求失败对于romList为空时的处理 FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
FetchPageCmd.Clear(); nesRomFileNameMapper.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear(); if (romList != null)
nesRomFetchList = new RomFile[romList.resultAllCount]; nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++) else
{ nesRomFetchList = new RomFile[0];
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE); for (int i = 0; i < nesRomFetchList.Length; i++)
} {
SaveRomInfoFromWeb(romList); //以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
callback.Invoke(nesRomFetchList); }
}, 0, PAGE_SIZE); SaveRomInfoFromWeb(romList);
}
else callback.Invoke(nesRomFetchList);
{ }, 0, PAGE_SIZE);
m_romSearchFunc((romList) => }
{ else
//TODO 请求失败对于romList为空时的处理 {
m_romSearchFunc((page, romList) =>
FetchPageCmd.Clear(); {
nesRomFileIdMapper.Clear(); FetchPageCmd.Clear();
nesRomFileNameMapper.Clear(); nesRomFileIdMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount]; nesRomFileNameMapper.Clear();
for (int i = 0; i < nesRomFetchList.Length; i++)
{ if (romList != null)
//以后考虑用对象池实例化RomFile nesRomFetchList = new RomFile[romList.resultAllCount];
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE); else
} nesRomFetchList = new RomFile[0];
SaveRomInfoFromWeb(romList);
for (int i = 0; i < nesRomFetchList.Length; i++)
callback.Invoke(nesRomFetchList); {
}, searchKey, 0, PAGE_SIZE); //以后考虑用对象池实例化RomFile
} nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
} }
SaveRomInfoFromWeb(romList);
public void BeginFetchRomInfo(RomFile romFile)
{ callback.Invoke(nesRomFetchList);
if (romFile.InfoReady) return; }, searchKey, 0, PAGE_SIZE);
}
FetchPageCmd.Add(romFile.Page); }
}
bool m_needFetch = false;
public void ExecuteFetchRomInfo() public void BeginFetchRomInfo(RomFile romFile)
{ {
if (FetchPageCmd.Count == 0) return; if (romFile.InfoReady) return;
foreach (var pageNo in FetchPageCmd) if (FetchPageCmd.Add(romFile.Page))
{ m_needFetch = true;
if (!string.IsNullOrEmpty(lastSearchKey)) }
{
m_romSearchFunc(SaveRomInfoFromWeb, lastSearchKey, pageNo, PAGE_SIZE); public void ExecuteFetchRomInfo()
} {
else if (FetchPageCmd.Count == 0) return;
{ if (!m_needFetch) return;
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
} foreach (var pageNo in FetchPageCmd)
} {
FetchPageCmd.Clear(); if (!string.IsNullOrEmpty(lastSearchKey))
} {
m_romSearchFunc((page, resp) =>
private void SaveRomInfoFromWeb(Resp_GameList resp) {
{ FetchPageCmd.Remove(page);
for (int i = 0; i < resp.gameList.Count; i++) SaveRomInfoFromWeb(resp);
{ }, lastSearchKey, pageNo, PAGE_SIZE);
var webData = resp.gameList[i]; }
RomFile targetRomFile = nesRomFetchList[webData.orderid]; else
{
targetRomFile.SetWebData(webData); m_romGetFunc((page, resp) =>
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid]; {
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile; FetchPageCmd.Remove(page);
} SaveRomInfoFromWeb(resp);
} }, pageNo, PAGE_SIZE);
}
public static string CalcHash(byte[] data) }
{
return Helper.FileMD5Hash(data); m_needFetch = false;
} }
public void AddRomFile(RomFile rom) private void SaveRomInfoFromWeb(Resp_GameList resp)
{ {
nesRomFileNameMapper[rom.FileName] = rom; if (resp == null) return;
}
} for (int i = 0; i < resp.gameList.Count; i++)
{
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
targetRomFile.SetWebData(webData);
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
}
}
public static string CalcHash(byte[] data)
{
return Helper.FileMD5Hash(data);
}
public void AddRomFile(RomFile rom)
{
nesRomFileNameMapper[rom.FileName] = rom;
}
}
} }