using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.Common; using AxibugEmuOnline.Client.Event; using AxibugProtobuf; using System; using System.Collections.Generic; using System.IO; using System.Linq; using static AxibugEmuOnline.Client.HttpAPI; namespace AxibugEmuOnline.Client { public class RomLib { /// Rom请求,一页的大小 private const int PAGE_SIZE = 10; /// 请求指令 private HashSet FetchPageCmd = new HashSet(); private RomFile[] nesRomFetchList; private Dictionary nesRomFileIdMapper = new Dictionary(); private Dictionary nesRomFileNameMapper = new Dictionary(); private HttpAPI.GetRomListAPI m_romGetFunc; private HttpAPI.SearchRomListAPI m_romSearchFunc; private RomPlatformType m_platform; private string lastSearchKey; public RomLib(RomPlatformType platform) { m_platform = platform; switch (platform) { case RomPlatformType.Nes: m_romGetFunc = App.httpAPI.GetNesRomList; m_romSearchFunc = App.httpAPI.SearchNesRomList; break; } Eventer.Instance.RegisterEvent(EEvent.OnRomStarStateChanged, OnRomStarStateChanged); } private void OnRomStarStateChanged(int romID, bool star) { var targetRom = nesRomFetchList.FirstOrDefault(rom => rom.ID == romID); if (targetRom == null) return; targetRom.Star = star; } public RomFile GetRomFile(string romFileName) { RomFile romFile; nesRomFileNameMapper.TryGetValue(romFileName, out romFile); return romFile; } /// 清除所有下载的Rom文件 public void ClearRomFile() { var path = $"{App.PersistentDataPath}/RemoteRoms/{m_platform}"; if (Directory.Exists(path)) Directory.Delete(path, true); } /// 移除一个已下载的Rom public void RemoveOneRomFile(RomFile romFile) { if (romFile.RomReady) File.Delete(romFile.LocalFilePath); } /// /// 获得所有Rom文件 /// /// public void FetchRomCount(Action callback, string searchKey = null) { lastSearchKey = searchKey; if (string.IsNullOrWhiteSpace(searchKey)) { m_romGetFunc((page, romList) => { FetchPageCmd.Clear(); nesRomFileIdMapper.Clear(); nesRomFileNameMapper.Clear(); if (romList != null) nesRomFetchList = new RomFile[romList.resultAllCount]; else nesRomFetchList = new RomFile[0]; for (int i = 0; i < nesRomFetchList.Length; i++) { //以后考虑用对象池实例化RomFile nesRomFetchList[i] = new RomFile(i, i / PAGE_SIZE); } SaveRomInfoFromWeb(romList); callback.Invoke(nesRomFetchList); }, m_platform, 0, PAGE_SIZE); } else { m_romSearchFunc((page, romList) => { FetchPageCmd.Clear(); nesRomFileIdMapper.Clear(); nesRomFileNameMapper.Clear(); if (romList != null) nesRomFetchList = new RomFile[romList.resultAllCount]; else nesRomFetchList = new RomFile[0]; for (int i = 0; i < nesRomFetchList.Length; i++) { //以后考虑用对象池实例化RomFile nesRomFetchList[i] = new RomFile(i, i / PAGE_SIZE); } SaveRomInfoFromWeb(romList); callback.Invoke(nesRomFetchList); }, m_platform, searchKey, 0, PAGE_SIZE); } } bool m_needFetch = false; public void BeginFetchRomInfo(RomFile romFile) { if (romFile.InfoReady) return; if (FetchPageCmd.Add(romFile.Page)) m_needFetch = true; } public void ExecuteFetchRomInfo() { if (FetchPageCmd.Count == 0) return; if (!m_needFetch) return; foreach (var pageNo in FetchPageCmd) { if (!string.IsNullOrEmpty(lastSearchKey)) { m_romSearchFunc((page, resp) => { FetchPageCmd.Remove(page); SaveRomInfoFromWeb(resp); }, m_platform, lastSearchKey, pageNo, PAGE_SIZE); } else { m_romGetFunc((page, resp) => { FetchPageCmd.Remove(page); SaveRomInfoFromWeb(resp); }, m_platform, pageNo, PAGE_SIZE); } } m_needFetch = false; } private void SaveRomInfoFromWeb(Resp_GameList resp) { 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; } } }