diff --git a/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/Game_NES_Template.prefab b/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/Game_NES_Template.prefab index 65d70e7..8a39fca 100644 --- a/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/Game_NES_Template.prefab +++ b/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/Game_NES_Template.prefab @@ -388,6 +388,8 @@ GameObject: serializedVersion: 6 m_Component: - component: {fileID: 2303118795734916657} + - component: {fileID: 6792036789108903940} + - component: {fileID: 4997733671603798451} m_Layer: 5 m_Name: viewport m_TagString: Untagged @@ -415,6 +417,44 @@ RectTransform: m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 0, y: 0} m_Pivot: {x: 0.5, y: 0.5} +--- !u!222 &6792036789108903940 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3462611600707696275} + m_CullTransparentMesh: 1 +--- !u!114 &4997733671603798451 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3462611600707696275} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 0} + m_RaycastTarget: 1 + m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} + m_Maskable: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 0} + m_Type: 0 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 --- !u!1 &3811189825355447390 GameObject: m_ObjectHideFlags: 0 diff --git a/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/RomItemTemplate.prefab b/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/RomItemTemplate.prefab index b8c04ec..6221903 100644 --- a/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/RomItemTemplate.prefab +++ b/AxibugEmuOnline.Client/Assets/Resources/UIPrefabs/RomItemTemplate.prefab @@ -134,7 +134,7 @@ MonoBehaviour: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 734025543935719296} - m_Enabled: 1 + m_Enabled: 0 m_EditorHideFlags: 0 m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} m_Name: @@ -281,7 +281,7 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: m_Material: {fileID: 0} - m_Color: {r: 1, g: 0.4577373, b: 0, a: 1} + m_Color: {r: 1, g: 0.45882356, b: 0, a: 1} m_RaycastTarget: 1 m_RaycastPadding: {x: 0, y: 0, z: 0, w: 0} m_Maskable: 1 @@ -353,7 +353,7 @@ MonoBehaviour: Descript: {fileID: 3380485461544738227} Root: {fileID: 8754483333502849411} ShadowIcon: {fileID: 2619187604372594158} - InfoNode: {fileID: 6788248266412682264} + InfoNode: {fileID: 0} SubMenuItemGroup: {fileID: 0} SelectScale: 1 UnSelectScale: 1 diff --git a/AxibugEmuOnline.Client/Assets/Script/AppAxibugEmuOnline.cs b/AxibugEmuOnline.Client/Assets/Script/AppAxibugEmuOnline.cs index dc62738..5f8b656 100644 --- a/AxibugEmuOnline.Client/Assets/Script/AppAxibugEmuOnline.cs +++ b/AxibugEmuOnline.Client/Assets/Script/AppAxibugEmuOnline.cs @@ -21,9 +21,12 @@ namespace AxibugEmuOnline.Client.ClientCore public static AppEmu emu; public static RomLib nesRomLib; public static HttpAPI httpAPI; + public static CacheManager CacheMgr; private static CoroutineRunner coRunner; + public static string PersistentDataPath => Application.persistentDataPath; + [RuntimeInitializeOnLoadMethod] static void Init() { @@ -37,6 +40,7 @@ namespace AxibugEmuOnline.Client.ClientCore netgame = new AppNetGame(); httpAPI = new HttpAPI(); nesRomLib = new RomLib(EnumPlatform.NES); + CacheMgr = new CacheManager(); var go = new GameObject("[AppAxibugEmuOnline]"); GameObject.DontDestroyOnLoad(go); diff --git a/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs b/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs new file mode 100644 index 0000000..aed9c9c --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs @@ -0,0 +1,95 @@ +using AxibugEmuOnline.Client.ClientCore; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; +using UnityEngine.Networking; + +namespace AxibugEmuOnline.Client +{ + public class CacheManager + { + static readonly string CacheDirPath = $"{AppAxibugEmuOnline.PersistentDataPath}/Caches"; + static readonly string TextureCacheDirPath = $"{CacheDirPath}/Texture"; + + public void GetSpriteCache(string url, Action callback) + { + GetCacheData(url, TextureCacheDirPath, callback); + } + + IEnumerator DownloadFromURL(string url, string path, Action callback) + { + var request = UnityWebRequest.Get($"{AppAxibugEmuOnline.httpAPI.DownSite}/{url}"); + yield return request.SendWebRequest(); + + if (request.result == UnityWebRequest.Result.Success) + { + Directory.CreateDirectory(path); + File.WriteAllBytes($"{path}/{url.GetHashCode()}", request.downloadHandler.data); + callback.Invoke(request.downloadHandler.data); + } + else + callback.Invoke(null); + } + + private Dictionary cachesInMemory = new Dictionary(); + void GetCacheData(string url, string path, Action callback) where T : class + { + if (cachesInMemory.TryGetValue(url, out var cacheObj) && cacheObj is T obj) + { + callback.Invoke(obj, url); + } + + var fileName = $"{url.GetHashCode()}"; + byte[] rawData = null; + + var filePath = $"{path}/{fileName}"; + if (File.Exists(filePath)) + { + rawData = File.ReadAllBytes(filePath); + var @out = RawDataConvert(rawData); + cachesInMemory[url] = @out; + callback.Invoke(@out, url); + } + else + { + AppAxibugEmuOnline.StartCoroutine(DownloadFromURL(url, path, (data) => + { + var @out = RawDataConvert(data); + cachesInMemory[url] = @out; + callback.Invoke(@out, url); + })); + } + } + + Type t_texture2d = typeof(Texture2D); + Type t_sprite = typeof(Sprite); + Type t_byteArray = typeof(byte[]); + Type t_object = typeof(object); + + T RawDataConvert(byte[] data) where T : class + { + var vt = typeof(T); + + if (vt == t_texture2d || vt == t_sprite) + { + Texture2D texture = new Texture2D(2, 2); + texture.LoadImage(data); + + if (vt == t_texture2d) return texture as T; + else return Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f)) as T; + } + else if (vt == t_byteArray) + { + return data as T; + } + else + { + return data as T; + } + + throw new NotImplementedException($"{vt.Name}"); + } + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs.meta b/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs.meta new file mode 100644 index 0000000..78f9e19 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/Manager/CacheManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 03fa430ca22517548b510b71d54ab2d6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/Manager/RomLib/RomFile.cs b/AxibugEmuOnline.Client/Assets/Script/Manager/RomLib/RomFile.cs index 193d26a..b0e29cc 100644 --- a/AxibugEmuOnline.Client/Assets/Script/Manager/RomLib/RomFile.cs +++ b/AxibugEmuOnline.Client/Assets/Script/Manager/RomLib/RomFile.cs @@ -16,7 +16,7 @@ namespace AxibugEmuOnline.Client private EnumPlatform platform; /// 指示该Rom文件的存放路径 - public string LocalFilePath => $"{CoreSupporter.PersistentDataPath}/RemoteRoms/{platform}/{fileName}"; + public string LocalFilePath => $"{AppAxibugEmuOnline.PersistentDataPath}/RemoteRoms/{platform}/{fileName}"; /// 指示该Rom文件是否已下载完毕 public bool RomReady => hasLocalFile; /// 指示是否正在下载Rom文件 @@ -30,6 +30,8 @@ namespace AxibugEmuOnline.Client public string Alias => webData.romName; /// 描述 public string Descript => webData.desc; + /// 小图URL + public string ImageURL => webData.imgUrl; /// 文件名 public string FileName => fileName; diff --git a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs index 3a23b53..b99d035 100644 --- a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs +++ b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/CoreSupporter.cs @@ -8,14 +8,6 @@ namespace AxibugEmuOnline.Client { public class CoreSupporter : ISupporterImpl { - public static string PersistentDataPath - { - get - { - return Application.persistentDataPath; - } - } - public Stream OpenRom(string fname) { try @@ -48,7 +40,7 @@ namespace AxibugEmuOnline.Client public void SaveSRAMToFile(byte[] sramContent, string romName) { - string sramDirectoryPath = $"{Application.persistentDataPath}/sav"; + string sramDirectoryPath = $"{AppAxibugEmuOnline.PersistentDataPath}/sav"; Directory.CreateDirectory(sramDirectoryPath); romName = Path.GetFileNameWithoutExtension(romName); File.WriteAllBytes($"{sramDirectoryPath}/{romName}.sav", sramContent); @@ -56,7 +48,7 @@ namespace AxibugEmuOnline.Client public void SaveDISKToFile(byte[] diskFileContent, string romName) { - string diskFileDirectoryPath = $"{Application.persistentDataPath}/dsv"; + string diskFileDirectoryPath = $"{AppAxibugEmuOnline.PersistentDataPath}/dsv"; Directory.CreateDirectory(diskFileDirectoryPath); romName = Path.GetFileNameWithoutExtension(romName); File.WriteAllBytes($"{diskFileDirectoryPath}/{romName}.dsv", diskFileContent); @@ -66,14 +58,14 @@ namespace AxibugEmuOnline.Client public void PrepareDirectory(string directPath) { - Directory.CreateDirectory($"{Application.persistentDataPath}/{directPath}"); + Directory.CreateDirectory($"{AppAxibugEmuOnline.PersistentDataPath}/{directPath}"); } public void SaveFile(byte[] fileData, string directPath, string fileName) { PrepareDirectory(directPath); - var fileFullpath = $"{Application.persistentDataPath}/{directPath}/{fileName}"; + var fileFullpath = $"{AppAxibugEmuOnline.PersistentDataPath}/{directPath}/{fileName}"; File.WriteAllBytes(fileFullpath, fileData); } @@ -81,7 +73,7 @@ namespace AxibugEmuOnline.Client { try { - var data = File.ReadAllBytes($"{Application.persistentDataPath}/{directPath}/{fileName}"); + var data = File.ReadAllBytes($"{AppAxibugEmuOnline.PersistentDataPath}/{directPath}/{fileName}"); if (data == null) return null; return new MemoryStream(data); } diff --git a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesControllerMapper.cs b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesControllerMapper.cs index c9ab5f6..8bb8dbb 100644 --- a/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesControllerMapper.cs +++ b/AxibugEmuOnline.Client/Assets/Script/NesEmulator/NesControllerMapper.cs @@ -1,4 +1,5 @@ -using System; +using AxibugEmuOnline.Client.ClientCore; +using System; using System.IO; using UnityEngine; using VirtualNes.Core; @@ -7,7 +8,7 @@ namespace AxibugEmuOnline.Client { public class NesControllerMapper { - private static readonly string ConfigFilePath = $"{Application.persistentDataPath}/NES/ControllerMapper.json"; + private static readonly string ConfigFilePath = $"{AppAxibugEmuOnline.PersistentDataPath}/NES/ControllerMapper.json"; public MapperSetter Player1 = new MapperSetter(); public MapperSetter Player2 = new MapperSetter(); @@ -49,7 +50,7 @@ namespace AxibugEmuOnline.Client { try { - var json = File.ReadAllText($"{Application.persistentDataPath}/Nes/ControllerMapper.json"); + var json = File.ReadAllText($"{AppAxibugEmuOnline.PersistentDataPath}/Nes/ControllerMapper.json"); s_setting = JsonUtility.FromJson(json); } catch diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs b/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs new file mode 100644 index 0000000..fbbd9a4 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs @@ -0,0 +1,18 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace AxibugEmuOnline.Client +{ + [RequireComponent(typeof(CanvasGroup))] + public class AutoRaycastCanvasGroup : MonoBehaviour + { + private CanvasGroup canvasGroup; + private void Update() + { + if (canvasGroup == null) canvasGroup = gameObject.GetComponent(); + + canvasGroup.blocksRaycasts = canvasGroup.alpha == 0 ? false : true; + } + } +} diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs.meta b/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs.meta new file mode 100644 index 0000000..5cea066 --- /dev/null +++ b/AxibugEmuOnline.Client/Assets/Script/UI/AutoRaycastCanvasGroup.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4215c9d5a613e24428c8c705464719e3 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/MainMenuController.cs b/AxibugEmuOnline.Client/Assets/Script/UI/MainMenuController.cs index a998b83..74cbba9 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/MainMenuController.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/MainMenuController.cs @@ -32,6 +32,7 @@ namespace AxibugEmuOnline.Client.UI base.Start(); m_runtimeMenuUICanvas = m_runtimeMenuUI.Select(menu => menu.gameObject.AddComponent()).ToList(); + m_runtimeMenuUICanvas.ForEach(canv => canv.gameObject.AddComponent()); } diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/MenuItem.cs b/AxibugEmuOnline.Client/Assets/Script/UI/MenuItem.cs index 9d84719..f880967 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/MenuItem.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/MenuItem.cs @@ -39,26 +39,30 @@ namespace AxibugEmuOnline.Client.UI if (ShadowIcon != null) ShadowIcon.gameObject.SetActive(false); - InfoNode.alpha = 0; + if (InfoNode != null) InfoNode.alpha = 0; if (ShadowIcon != null) ShadowIcon.gameObject.SetActiveEx(false); if (SubMenuItemGroup != null) SubMenuItemGroup.SetSelect(false); } public void SetData(MenuData data) { - SetBaseInfo(data.Name, data.Description, data.Icon); + SetBaseInfo(data.Name, data.Description); + SetIcon(data.Icon); if (SubMenuItemGroup != null) SubMenuItemGroup.Init(data.SubMenus); } - protected void SetBaseInfo(string name, string descript, Sprite icon) + protected void SetBaseInfo(string name, string descript) { this.name = name; - if (Icon != null) Icon.sprite = icon; - if (ShadowIcon != null) ShadowIcon.sprite = icon; if (Txt != null) Txt.text = name; if (Descript != null) Descript.text = descript; + } + protected void SetIcon(Sprite icon) + { + if (Icon != null) Icon.sprite = icon; + if (ShadowIcon != null) ShadowIcon.sprite = icon; } public void SetSelectState(bool selected) @@ -75,7 +79,7 @@ namespace AxibugEmuOnline.Client.UI progressTween = DOTween.To(() => m_progress, (x) => m_progress = x, m_select ? 1 : 0, 5) .SetSpeedBased().OnUpdate(() => { - InfoNode.alpha = m_progress; + if (InfoNode != null) InfoNode.alpha = m_progress; Root.localScale = Vector3.one * Mathf.Lerp(UnSelectScale, SelectScale, m_progress); }); diff --git a/AxibugEmuOnline.Client/Assets/Script/UI/RomItem.cs b/AxibugEmuOnline.Client/Assets/Script/UI/RomItem.cs index e14858a..c729ae1 100644 --- a/AxibugEmuOnline.Client/Assets/Script/UI/RomItem.cs +++ b/AxibugEmuOnline.Client/Assets/Script/UI/RomItem.cs @@ -1,3 +1,4 @@ +using AxibugEmuOnline.Client.ClientCore; using AxibugEmuOnline.Client.UI; using System; using UnityEngine; @@ -17,15 +18,14 @@ namespace AxibugEmuOnline.Client public void SetData(object data) { - SetSelectState(true); - m_romfile = (RomFile)data; + m_romfile.OnInfoFilled += OnRomInfoFilled; + m_romImage.sprite = null; UpdateView(); if (!m_romfile.InfoReady) { - m_romfile.OnInfoFilled += OnRomInfoFilled; m_romlib.BeginFetchRomInfo(m_romfile); } } @@ -38,7 +38,6 @@ namespace AxibugEmuOnline.Client public void Release() { m_romfile.OnInfoFilled -= OnRomInfoFilled; - SetSelectState(false); } private void OnRomInfoFilled() @@ -50,11 +49,17 @@ namespace AxibugEmuOnline.Client { if (!m_romfile.InfoReady) { - SetBaseInfo(string.Empty, string.Empty, null); + SetBaseInfo(string.Empty, string.Empty); } else { - SetBaseInfo(m_romfile.Alias, m_romfile.Descript, null); + SetBaseInfo(m_romfile.Alias, m_romfile.Descript); + AppAxibugEmuOnline.CacheMgr.GetSpriteCache(m_romfile.ImageURL, (img, url) => + { + if (url != m_romfile.ImageURL) return; + + m_romImage.sprite = img; + }); } } }