文件下载功能重构到单独的FileDownloader类中,以解决多个Rom同时下载的冲突(例如依赖关系的Rom)

This commit is contained in:
ALIENJACK\alien 2025-01-23 16:14:01 +08:00
parent c147e1dd91
commit 206715b482
6 changed files with 173 additions and 71 deletions

View File

@ -30,6 +30,7 @@ namespace AxibugEmuOnline.Client.ClientCore
public static AppShare share;
public static GamePadManager gamePadMgr;
public static SaveSlotManager SavMgr;
public static FileDownloader FileDownloader;
static bool bTest;
static string mTestSrvIP;
@ -75,7 +76,7 @@ namespace AxibugEmuOnline.Client.ClientCore
{
PSP2Init();
}
FileDownloader = new FileDownloader();
settings = new AppSettings();
network = new NetworkHelper();
login = new AppLogin();
@ -225,6 +226,7 @@ namespace AxibugEmuOnline.Client.ClientCore
{
foreach (var romLib in s_romLibs.Values) romLib.ExecuteFetchRomInfo();
starRomLib.ExecuteFetchRomInfo();
FileDownloader.Update();
gamePadMgr.Update();
}

View File

@ -0,0 +1,62 @@
using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class FileDownloader
{
Dictionary<string, AxiHttpProxy.SendDownLoadProxy> m_downloadingTasks = new Dictionary<string, AxiHttpProxy.SendDownLoadProxy>();
Dictionary<string, Action<byte[]>> m_completeCallback = new Dictionary<string, Action<byte[]>>();
public void BeginDownload(string url, Action<byte[]> callback)
{
if (m_downloadingTasks.TryGetValue(url, out var downloadProxy)) return;
m_completeCallback[url] = callback;
var downloadRequest = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{url}");
m_downloadingTasks[url] = downloadRequest;
}
public float? GetDownloadProgress(string url)
{
m_downloadingTasks.TryGetValue(url, out var proxy);
if (proxy == null) return null;
return Mathf.Clamp01(proxy.downloadHandler.DownLoadPr);
}
HashSet<string> temp = new HashSet<string>();
public void Update()
{
temp.Clear();
foreach (var item in m_downloadingTasks)
{
var url = item.Key;
var proxy = item.Value;
if (proxy.downloadHandler.isDone)
{
temp.Add(url);
}
}
foreach (var url in temp)
{
var overTask = m_downloadingTasks[url];
m_downloadingTasks.Remove(url);
if (!overTask.downloadHandler.bHadErr)
{
m_completeCallback[url].Invoke(overTask.downloadHandler.data);
m_completeCallback.Remove(url);
}
else
{
Debug.LogError(overTask.downloadHandler.ErrInfo);
}
}
}
}
}

View File

@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 86823d6828e1cf24da330443483b060b

View File

@ -13,8 +13,6 @@ namespace AxibugEmuOnline.Client
public class RomFile
{
private HttpAPI.Resp_RomInfo webData;
private bool hasLocalFile;
private AxiHttpProxy.SendDownLoadProxy downloadRequest;
/// <summary> 依赖的Rom文件 </summary>
private List<RomFile> dependencies = new List<RomFile>();
@ -26,12 +24,32 @@ namespace AxibugEmuOnline.Client
switch (Platform)
{
case RomPlatformType.Nes: return false;
case RomPlatformType.Cps1: return true;
case RomPlatformType.Igs:
case RomPlatformType.Cps1:
case RomPlatformType.Cps2:
case RomPlatformType.Neogeo:
return true;
default: throw new NotImplementedException($"未实现的平台{Platform}");
}
}
}
bool m_hasLocalFile;
public bool HasLocalFile
{
get
{
if (!m_hasLocalFile) return false;
foreach (var depRom in dependencies)
{
if (!depRom.HasLocalFile) return false;
}
return true;
}
}
/// <summary> 指示该Rom文件的存放路径 </summary>
public string LocalFilePath => $"{App.PersistentDataPath(Platform)}/RemoteRoms/{FileName}";
@ -40,7 +58,7 @@ namespace AxibugEmuOnline.Client
{
get
{
if (!hasLocalFile) return false;
if (!HasLocalFile) return false;
foreach (var depRom in dependencies)
{
@ -55,8 +73,10 @@ namespace AxibugEmuOnline.Client
{
get
{
var selfDownloading = downloadRequest != null && !downloadRequest.downloadHandler.isDone;
if (selfDownloading) return true;
if (!InfoReady) return false;
var progress = App.FileDownloader.GetDownloadProgress(webData.url);
if (progress.HasValue) return true;
foreach (var depRom in dependencies)
{
@ -73,15 +93,21 @@ namespace AxibugEmuOnline.Client
{
if (!IsDownloading) return 0;
float total = 0f;
float progress = 0f;
total += downloadRequest != null ? downloadRequest.downloadHandler.DownLoadPr : 0;
foreach (var depRom in dependencies)
if (HasLocalFile) progress = 1f;
else
{
total += depRom.Progress;
var downloadProgress = App.FileDownloader.GetDownloadProgress(webData.url);
progress = downloadProgress.HasValue ? downloadProgress.Value : 0;
}
return total;
foreach (var depRom in dependencies)
{
progress += depRom.Progress;
}
return progress / (dependencies.Count + 1);
}
}
@ -139,17 +165,38 @@ namespace AxibugEmuOnline.Client
Page = insidePage;
}
public void CheckLocalFileState()
{
if (webData == null) m_hasLocalFile = false;
else
{
if (App.FileDownloader.GetDownloadProgress(webData.url) == null)
{
if (MultiFileRom)
m_hasLocalFile = Directory.Exists(LocalFilePath);
else
m_hasLocalFile = File.Exists(LocalFilePath);
}
}
foreach (var depRom in dependencies)
depRom.CheckLocalFileState();
}
public void BeginDownload()
{
if (RomReady) return;
if (IsDownloading) return;
//检查依赖Rom的下载情况
App.StartCoroutine(DownloadRemoteRom((bytes) =>
foreach (var depRom in dependencies)
{
depRom.BeginDownload();
}
App.FileDownloader.BeginDownload(webData.url, (bytes) =>
{
HandleRomFilePostProcess(bytes);
}));
});
}
private void HandleRomFilePostProcess(byte[] bytes)
@ -161,6 +208,8 @@ namespace AxibugEmuOnline.Client
Dictionary<string, byte[]> unzipFiles = new Dictionary<string, byte[]>();
//多rom文件的平台,下载下来的数据直接解压放入文件夹内
var zip = new ZipInputStream(new MemoryStream(bytes));
List<string> depth0Files = new List<string>();
while (true)
{
var currentEntry = zip.GetNextEntry();
@ -180,10 +229,18 @@ namespace AxibugEmuOnline.Client
unzipFiles[$"{LocalFilePath}/{currentEntry.Name}"] = output.ToArray();
}
string rootDirName = null;
//如果第一层目录只有一个文件并且是文件夹,则所有文件层级外提一层
if (depth0Files.Count == 1 && depth0Files[0].Contains('.'))
{
rootDirName = depth0Files[0];
}
foreach (var item in unzipFiles)
{
var path = item.Key;
var path = rootDirName != null ? item.Key.Substring(0, rootDirName.Length + 1) : item.Key;
var data = item.Value;
Directory.CreateDirectory(Path.GetDirectoryName(path));
File.WriteAllBytes(path, data);
}
}
@ -193,10 +250,8 @@ namespace AxibugEmuOnline.Client
Directory.CreateDirectory(directPath);
File.WriteAllBytes(LocalFilePath, bytes);
hasLocalFile = true;
Eventer.Instance.PostEvent(EEvent.OnRomFileDownloaded, ID);
}
Eventer.Instance.PostEvent(EEvent.OnRomFileDownloaded, ID);
OnDownloadOver?.Invoke(this);
}
@ -238,39 +293,16 @@ namespace AxibugEmuOnline.Client
throw new Exception("Not Valid Rom Data");
}
private IEnumerator DownloadRemoteRom(Action<byte[]> callback)
{
downloadRequest = AxiHttpProxy.GetDownLoad($"{App.httpAPI.WebHost}/{webData.url}");
while (!downloadRequest.downloadHandler.isDone)
{
yield return null;
Debug.Log($"下载进度:{downloadRequest.downloadHandler.DownLoadPr} ->{downloadRequest.downloadHandler.loadedLenght}/{downloadRequest.downloadHandler.NeedloadedLenght}");
}
AxiHttpProxy.ShowAxiHttpDebugInfo(downloadRequest.downloadHandler);
var request = downloadRequest;
downloadRequest = null;
if (!request.downloadHandler.bHadErr)
callback(request.downloadHandler.data);
else
callback(null);
}
public void SetWebData(HttpAPI.Resp_RomInfo resp_RomInfo)
{
webData = resp_RomInfo;
FileName = MultiFileRom ? Path.GetFileNameWithoutExtension(webData.url) : Path.GetFileName(webData.url);
FileName = System.Net.WebUtility.UrlDecode(FileName);
if (MultiFileRom)
hasLocalFile = Directory.Exists(LocalFilePath);
else
hasLocalFile = File.Exists(LocalFilePath);
//收集依赖Rom
if (webData.parentRomIdsList != null)
{
dependencies.Clear();
foreach (var romID in webData.parentRomIdsList)
{
var romFile = new RomFile(Index, Page);
@ -282,6 +314,8 @@ namespace AxibugEmuOnline.Client
}
}
CheckLocalFileState();
App.StartCoroutine(WaitInfoReady());
}

View File

@ -17,9 +17,9 @@ namespace AxibugEmuOnline.Client
/// <summary> 请求指令 </summary>
private HashSet<int> FetchPageCmd = new HashSet<int>();
private RomFile[] nesRomFetchList;
private Dictionary<int, RomFile> nesRomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> nesRomFileNameMapper = new Dictionary<string, RomFile>();
private RomFile[] RomFetchList;
private Dictionary<int, RomFile> RomFileIdMapper = new Dictionary<int, RomFile>();
private Dictionary<string, RomFile> RomFileNameMapper = new Dictionary<string, RomFile>();
private HttpAPI.GetRomListAPI m_romGetFunc;
private HttpAPI.SearchRomListAPI m_romSearchFunc;
private RomPlatformType m_platform;
@ -43,9 +43,9 @@ namespace AxibugEmuOnline.Client
private void OnRomStarStateChanged(int romID, bool star)
{
if (nesRomFetchList == null) return;
if (RomFetchList == null) return;
var targetRom = nesRomFetchList.FirstOrDefault(rom => rom.ID == romID);
var targetRom = RomFetchList.FirstOrDefault(rom => rom.ID == romID);
if (targetRom == null) return;
targetRom.Star = star;
@ -54,7 +54,7 @@ namespace AxibugEmuOnline.Client
public RomFile GetRomFile(string romFileName)
{
RomFile romFile;
nesRomFileNameMapper.TryGetValue(romFileName, out romFile);
RomFileNameMapper.TryGetValue(romFileName, out romFile);
return romFile;
}
@ -83,22 +83,22 @@ namespace AxibugEmuOnline.Client
m_romGetFunc((page, romList) =>
{
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
RomFileIdMapper.Clear();
RomFileNameMapper.Clear();
if (romList != null)
nesRomFetchList = new RomFile[romList.resultAllCount];
RomFetchList = new RomFile[romList.resultAllCount];
else
nesRomFetchList = new RomFile[0];
RomFetchList = new RomFile[0];
for (int i = 0; i < nesRomFetchList.Length; i++)
for (int i = 0; i < RomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(i, i / PAGE_SIZE);
RomFetchList[i] = new RomFile(i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList);
callback.Invoke(RomFetchList);
}, m_platform, 0, PAGE_SIZE);
}
else
@ -106,22 +106,22 @@ namespace AxibugEmuOnline.Client
m_romSearchFunc((page, romList) =>
{
FetchPageCmd.Clear();
nesRomFileIdMapper.Clear();
nesRomFileNameMapper.Clear();
RomFileIdMapper.Clear();
RomFileNameMapper.Clear();
if (romList != null)
nesRomFetchList = new RomFile[romList.resultAllCount];
RomFetchList = new RomFile[romList.resultAllCount];
else
nesRomFetchList = new RomFile[0];
RomFetchList = new RomFile[0];
for (int i = 0; i < nesRomFetchList.Length; i++)
for (int i = 0; i < RomFetchList.Length; i++)
{
//以后考虑用对象池实例化RomFile
nesRomFetchList[i] = new RomFile(i, i / PAGE_SIZE);
RomFetchList[i] = new RomFile(i, i / PAGE_SIZE);
}
SaveRomInfoFromWeb(romList);
callback.Invoke(nesRomFetchList);
callback.Invoke(RomFetchList);
}, m_platform, searchKey, 0, PAGE_SIZE);
}
}
@ -170,11 +170,11 @@ namespace AxibugEmuOnline.Client
for (int i = 0; i < resp.gameList.Count; i++)
{
var webData = resp.gameList[i];
RomFile targetRomFile = nesRomFetchList[webData.orderid];
RomFile targetRomFile = RomFetchList[webData.orderid];
targetRomFile.SetWebData(webData);
nesRomFileIdMapper[webData.id] = nesRomFetchList[webData.orderid];
nesRomFileNameMapper[targetRomFile.FileName] = targetRomFile;
RomFileIdMapper[webData.id] = RomFetchList[webData.orderid];
RomFileNameMapper[targetRomFile.FileName] = targetRomFile;
}
}
@ -185,7 +185,7 @@ namespace AxibugEmuOnline.Client
public void AddRomFile(RomFile rom)
{
nesRomFileNameMapper[rom.FileName] = rom;
RomFileNameMapper[rom.FileName] = rom;
}
}
}

View File

@ -54,9 +54,11 @@ namespace AxibugEmuOnline.Client
private void OnRomDownloaded(int romID)
{
if (m_romfile == null || m_romfile.ID != romID) return;
if (m_romfile == null) return;
DownloadComplete.Play();
m_romfile.CheckLocalFileState();
if (m_romfile.RomReady)
DownloadComplete.Play();
}
public void SetData(object data)