AxibugEmuOnline/AxibugEmuOnline.Client/Assets/Script/Manager/RomLib/RomLib.cs

123 lines
4.0 KiB
C#
Raw Normal View History

2024-08-13 18:35:20 +08:00
using AxibugEmuOnline.Client.ClientCore;
2024-09-11 16:48:23 +08:00
using Codice.Client.Common;
2024-08-13 18:35:20 +08:00
using System;
using System.Collections.Generic;
2024-09-11 16:48:23 +08:00
using System.IO;
2024-08-22 15:16:58 +08:00
using static AxibugEmuOnline.Client.HttpAPI;
2024-08-13 18:35:20 +08:00
namespace AxibugEmuOnline.Client
{
public class RomLib
{
2024-08-22 15:16:58 +08:00
/// <summary> Rom请求,一页的大小 </summary>
private const int PAGE_SIZE = 10;
/// <summary> 请求指令 </summary>
private HashSet<int> FetchPageCmd = new HashSet<int>();
private RomFile[] nesRomFetchList;
2024-08-14 13:09:22 +08:00
private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>();
2024-08-22 15:16:58 +08:00
private HttpAPI.GetRomListAPI m_romGetFunc;
private EnumPlatform m_platform;
public RomLib(EnumPlatform platform)
{
m_platform = platform;
switch (platform)
{
case EnumPlatform.NES:
m_romGetFunc = AppAxibugEmuOnline.httpAPI.GetNesRomList;
break;
}
}
2024-08-13 18:35:20 +08:00
2024-08-22 15:16:58 +08:00
public RomFile GetRomFile(string romFileName)
2024-08-13 18:35:20 +08:00
{
2024-08-16 10:20:00 +08:00
RomFile romFile;
nesRomFileNameMapper.TryGetValue(romFileName, out romFile);
2024-08-14 13:09:22 +08:00
return romFile;
}
2024-09-11 16:48:23 +08:00
/// <summary> 清除所有下载的Rom文件 </summary>
public void ClearRomFile()
{
var path = $"{AppAxibugEmuOnline.PersistentDataPath}/RemoteRoms/{m_platform}";
if (Directory.Exists(path)) Directory.Delete(path, true);
}
/// <summary> 移除一个已下载的Rom </summary>
public void RemoveOneRomFile(RomFile romFile)
{
if (romFile.RomReady)
File.Delete(romFile.LocalFilePath);
}
2024-08-22 15:16:58 +08:00
/// <summary>
/// 获得所有Rom文件
/// </summary>
/// <param name="callback"></param>
public void FetchRomCount(Action<RomFile[]> callback)
2024-08-14 13:09:22 +08:00
{
2024-08-22 15:16:58 +08:00
m_romGetFunc((romList) =>
2024-08-13 18:35:20 +08:00
{
2024-08-22 15:16:58 +08:00
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
nesRomFetchList = new RomFile[romList.resultAllCount];
for (int i = 0; i < nesRomFetchList.Length; i++)
2024-08-13 18:35:20 +08:00
{
2024-08-22 15:16:58 +08:00
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(m_platform, i, i / PAGE_SIZE);
2024-08-13 18:35:20 +08:00
}
2024-08-22 15:16:58 +08:00
SaveRomInfoFromWeb(romList);
2024-08-14 13:09:22 +08:00
2024-08-22 15:16:58 +08:00
callback.Invoke(nesRomFetchList);
}, 0, PAGE_SIZE);
}
2024-08-14 13:09:22 +08:00
2024-08-22 15:16:58 +08:00
public void BeginFetchRomInfo(RomFile romFile)
{
if (romFile.InfoReady) return;
2024-08-14 13:09:22 +08:00
2024-08-22 15:16:58 +08:00
FetchPageCmd.Add(romFile.Page);
}
public void ExecuteFetchRomInfo()
{
if (FetchPageCmd.Count == 0) return;
foreach (var pageNo in FetchPageCmd)
{
m_romGetFunc(SaveRomInfoFromWeb, pageNo, PAGE_SIZE);
}
FetchPageCmd.Clear();
}
private void SaveRomInfoFromWeb(Resp_GameList resp)
{
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;
}
2024-08-14 13:09:22 +08:00
}
public static string CalcHash(byte[] data)
{
2024-08-14 13:20:52 +08:00
return string.Empty; //todo : 等待远程仓库敲定hash算法
//var hashBytes = MD5.Create().ComputeHash(data);
//StringBuilder sb = new StringBuilder();
//foreach (byte b in hashBytes)
//{
// sb.Append(b.ToString("x2"));
//}
2024-08-14 13:09:22 +08:00
2024-08-14 13:20:52 +08:00
//return sb.ToString();
2024-08-13 18:35:20 +08:00
}
}
}