解决翻页时丢失搜索关键字的问题

This commit is contained in:
sin365 2025-01-06 00:22:30 +08:00
parent 90f23a1d3f
commit 7c2709f335

View File

@ -7,147 +7,156 @@ 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(); FetchPageCmd.Clear();
nesRomFileNameMapper.Clear(); nesRomFileIdMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount]; nesRomFileNameMapper.Clear();
for (int i = 0; i < nesRomFetchList.Length; i++) nesRomFetchList = new RomFile[romList.resultAllCount];
{ for (int i = 0; i < nesRomFetchList.Length; i++)
//以后考虑用对象池实例化RomFile {
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE); //以后考虑用对象池实例化RomFile
} nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
SaveRomInfoFromWeb(romList); }
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList); callback.Invoke(nesRomFetchList);
}, 0, PAGE_SIZE); }, 0, PAGE_SIZE);
} }
else else
{ {
m_romSearchFunc((romList) => m_romSearchFunc((romList) =>
{ {
FetchPageCmd.Clear(); FetchPageCmd.Clear();
nesRomFileIdMapper.Clear(); nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear(); nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount]; nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++) for (int i = 0; i < nesRomFetchList.Length; i++)
{ {
//以后考虑用对象池实例化RomFile //以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE); nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
} }
SaveRomInfoFromWeb(romList); SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList); callback.Invoke(nesRomFetchList);
}, searchKey, 0, PAGE_SIZE); }, searchKey, 0, PAGE_SIZE);
} }
} }
public void BeginFetchRomInfo(RomFile romFile) public void BeginFetchRomInfo(RomFile romFile)
{ {
if (romFile.InfoReady) return; if (romFile.InfoReady) return;
FetchPageCmd.Add(romFile.Page); FetchPageCmd.Add(romFile.Page);
} }
public void ExecuteFetchRomInfo() public void ExecuteFetchRomInfo()
{ {
if (FetchPageCmd.Count == 0) return; if (FetchPageCmd.Count == 0) return;
foreach (var pageNo in FetchPageCmd) foreach (var pageNo in FetchPageCmd)
{ {
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE); if (!string.IsNullOrEmpty(lastSearchKey))
} {
FetchPageCmd.Clear(); m_romSearchFunc(SaveRomInfoFromWeb, lastSearchKey, pageNo, PAGE_SIZE);
} }
else
{
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
}
FetchPageCmd.Clear();
}
private void SaveRomInfoFromWeb(Resp_GameList resp) private void SaveRomInfoFromWeb(Resp_GameList resp)
{ {
for (int i = 0; i < resp.gameList.Count; i++) for (int i = 0; i < resp.gameList.Count; i++)
{ {
var webData = resp.gameList[i]; var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid]; RomFile targetRomFile = nesRomFetchList[webData.orderid];
targetRomFile.SetWebData(webData); targetRomFile.SetWebData(webData);
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid]; nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile; nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
} }
} }
public static string CalcHash(byte[] data) public static string CalcHash(byte[] data)
{ {
return Helper.FileMD5Hash(data); return Helper.FileMD5Hash(data);
} }
public void AddRomFile(RomFile rom) public void AddRomFile(RomFile rom)
{ {
nesRomFileNameMapper[rom.FileName] = rom; nesRomFileNameMapper[rom.FileName] = rom;
} }
} }
} }