Rom下载系统

This commit is contained in:
ALIENJACK\alien 2024-08-13 18:35:20 +08:00
parent 209c31fd72
commit fe6c427603
12 changed files with 248 additions and 3 deletions

View File

@ -1,6 +1,7 @@
using AxibugEmuOnline.Client.Manager;
using AxibugEmuOnline.Client.Network;
using System;
using System.Collections;
using UnityEngine;
using static AxibugEmuOnline.Client.Manager.LogManager;
@ -19,8 +20,13 @@ namespace AxibugEmuOnline.Client.ClientCore
public static UserDataManager user;
public static AppNetGame netgame;
public static AppEmu emu;
public static void Init()
public static RomLib romLib;
public static HttpAPI httpAPI;
private static CoroutineRunner coRunner;
[RuntimeInitializeOnLoadMethod]
static void Init()
{
log = new LogManager();
LogManager.OnLog += OnNoSugarNetLog;
@ -30,6 +36,20 @@ namespace AxibugEmuOnline.Client.ClientCore
user = new UserDataManager();
emu = new AppEmu();
netgame = new AppNetGame();
var go = new GameObject("[AppAxibugEmuOnline]");
GameObject.DontDestroyOnLoad(go);
coRunner = go.AddComponent<CoroutineRunner>();
}
public static Coroutine StartCoroutine(IEnumerator itor)
{
return coRunner.StartCoroutine(itor);
}
public static void StopCoroutine(Coroutine cor)
{
coRunner.StopCoroutine(cor);
}
public static bool Connect(string IP, int port)
@ -43,7 +63,7 @@ namespace AxibugEmuOnline.Client.ClientCore
}
static void OnNoSugarNetLog(int LogLevel, string msg)
{
Debug.Log("[AxibugEmuOnline]:"+msg);
Debug.Log("[AxibugEmuOnline]:" + msg);
}
}
}

View File

@ -0,0 +1,8 @@
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class CoroutineRunner : MonoBehaviour
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 26e0101eb56737b4f97fcaa3bd319d43
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,51 @@
using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
namespace AxibugEmuOnline.Client
{
public class HttpAPI
{
public string WebSite = "http://emu.axibug.com/api";
public void GetNesRomList(Action<Resp_GameList> callback, int page, int pageSize = 10)
{
AppAxibugEmuOnline.StartCoroutine(GetNesRomListFlow(page, pageSize, callback));
}
private IEnumerator GetNesRomListFlow(int page, int pageSize, Action<Resp_GameList> callback)
{
UnityWebRequest request = new UnityWebRequest($"{WebSite}/NesRomList?Page={page}&PageSize={pageSize}");
yield return request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
callback.Invoke(null);
yield break;
}
var resp = JsonUtility.FromJson<Resp_GameList>(request.downloadHandler.text);
callback.Invoke(resp);
}
public class Resp_GameList
{
public int Page { get; set; }
public int MaxPage { get; set; }
public int ResultAllCount { get; set; }
public List<Resp_RomInfo> GameList { get; set; }
}
public class Resp_RomInfo
{
public int ID { get; set; }
public string Hash { get; set; }
public string RomName { get; set; }
public string Url { get; set; }
public string ImgUrl { get; set; }
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 81bb136dc2c85304db4715953f751aa6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 006d5604a1d73de4e8d057974313dbaf
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AxibugEmuOnline.Client
{
public enum EnumPlatform
{
NES
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 355fa00c125158f4ba90003b0fd5d788
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,46 @@
using System;
using System.IO;
using UnityEngine;
namespace AxibugEmuOnline.Client
{
public class RomFile
{
private HttpAPI.Resp_RomInfo webData;
private bool hasLocalFile;
private string romName;
private EnumPlatform platform;
public string LocalFilePath => $"{Application.persistentDataPath}/RemoteRoms/{platform}/{romName}";
public RomFile(EnumPlatform platform)
{
this.platform = platform;
}
public void GetRomFileData(Action<byte[]> callback)
{
if (webData == null) { callback.Invoke(null); return; }
if (hasLocalFile)
{
var path = LocalFilePath;
}
}
public void SetWebData(HttpAPI.Resp_RomInfo resp_RomInfo)
{
webData = resp_RomInfo;
romName = Path.GetFileName(webData.Url);
if (File.Exists(LocalFilePath))
{
hasLocalFile = true;
}
else
{
hasLocalFile = false;
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3a876afb939308548a7b4bdc53a3f6df
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,44 @@
using AxibugEmuOnline.Client.ClientCore;
using System;
using System.Collections.Generic;
namespace AxibugEmuOnline.Client
{
public class RomLib
{
private List<RomFile> nesRomFiles = new List<RomFile>();
public void GetNesRomFile(int index, Action<RomFile> callback)
{
if (nesRomFiles.Count <= index)
{
int pageSize = 10;
int page = index / pageSize;
int indexInPage = index % page;
//填充空的RomFile数据
var needFill = index - nesRomFiles.Count + 1;
needFill += pageSize - indexInPage - 1;
for (int i = 0; i < needFill; i++)
{
nesRomFiles.Add(new RomFile(EnumPlatform.NES));
}
AppAxibugEmuOnline.httpAPI.GetNesRomList((romList) =>
{
if (romList == null)
{
callback.Invoke(null);
}
else
{
for (int i = 0; i < romList.GameList.Count; i++)
{
nesRomFiles[pageSize * (page - 1) + i].SetWebData(romList.GameList[i]);
}
}
}, page, pageSize);
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8b1860dd3aacece40a93ae8273fc71b1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: