diff --git a/Assets/Plugins/RetroUnity/Scripts/CoreDownloader.cs b/Assets/Plugins/RetroUnity/Scripts/CoreDownloader.cs index b3a4fdf..712b4e1 100644 --- a/Assets/Plugins/RetroUnity/Scripts/CoreDownloader.cs +++ b/Assets/Plugins/RetroUnity/Scripts/CoreDownloader.cs @@ -31,7 +31,7 @@ static void DownloadCores() { var coreNames = new List() { - "snes9x","blastem", "nestopia" + "snes9x","blastem", "nestopia", "mgba" }; foreach (var coreName in coreNames) { @@ -51,8 +51,9 @@ string extractDirectory(BuildTarget buildTarget) } } - void unzipCore(BuildTarget buildTarget, string zipPath, string extractDirectory) + List unzipCore(string zipPath, string extractDirectory) { + List assets = new List(); try { using (var archive = ZipFile.OpenRead(zipPath)) @@ -67,17 +68,7 @@ void unzipCore(BuildTarget buildTarget, string zipPath, string extractDirectory) } entry.ExtractToFile(destinationPath); - - var relativePath = destinationPath.Substring(destinationPath.IndexOf("Assets")); - AssetDatabase.ImportAsset(relativePath); - - - // Only for plugins - if (relativePath.Contains("Plugins")) - { - setNativePluginLibrary(buildTarget, relativePath); - } - + assets.Add(destinationPath); } } } @@ -86,7 +77,9 @@ void unzipCore(BuildTarget buildTarget, string zipPath, string extractDirectory) File.Delete(zipPath); } + return assets; } + static void DownloadCores(string romName) { @@ -113,9 +106,9 @@ static void DownloadCores(string romName) { var zipPath = coreDownloader.DownloadFile(url, extractDirectory); Debug.Log($"File successfully downloaded and saved to {zipPath}"); - coreDownloader.unzipCore(buildTarget, zipPath, extractDirectory); + var unzippedAssets = coreDownloader.unzipCore( zipPath, extractDirectory); Debug.Log($"Unzipping successfully downloaded and saved to {item.Value}"); - + ImportAssets(buildTarget, unzippedAssets); } catch (Exception e) { @@ -126,14 +119,40 @@ static void DownloadCores(string romName) } - static void setNativePluginLibrary(BuildTarget buildTarget, string pluginRelativePath) + + static void ImportAssets(BuildTarget buildTarget, List assetPaths) + { + foreach (var assetPath in assetPaths) + { + var relativePath = assetPath.Substring(assetPath.IndexOf("Assets")); + AssetDatabase.ImportAsset(relativePath); + + // Only for plugins + if (assetPath.Contains("Plugins")) + { + SetNativePluginLibrary(buildTarget, "ARMv7", relativePath); + } + + } + + } + + static void SetNativePluginLibrary(BuildTarget buildTarget, string cpu, string relativePath) { // native library, avaiable only for mobile - var nativePlugin = AssetImporter.GetAtPath(pluginRelativePath) as PluginImporter; + var nativePlugin = AssetImporter.GetAtPath(relativePath) as PluginImporter; + // Exclude + nativePlugin.SetExcludeEditorFromAnyPlatform(true); + nativePlugin.SetExcludeFromAnyPlatform(buildTarget, false); + // Include nativePlugin.SetCompatibleWithEditor(false); nativePlugin.SetCompatibleWithAnyPlatform(false); nativePlugin.SetCompatibleWithPlatform(buildTarget, true); - nativePlugin.SetPlatformData(buildTarget, "CompileFlags", "-fno-objc-arc"); + // Specific + nativePlugin.SetPlatformData(buildTarget, "CPU", cpu); + // Forcing flush + EditorUtility.SetDirty(nativePlugin); + AssetDatabase.WriteImportSettingsIfDirty(relativePath); } } diff --git a/Assets/Plugins/RetroUnity/Scripts/GameManager.cs b/Assets/Plugins/RetroUnity/Scripts/GameManager.cs index 5687e01..d681bf0 100644 --- a/Assets/Plugins/RetroUnity/Scripts/GameManager.cs +++ b/Assets/Plugins/RetroUnity/Scripts/GameManager.cs @@ -1,6 +1,9 @@ -using System.IO; -using RetroUnity.Utility; +using System; +using System.Collections; +using System.IO; +using System.Net; using UnityEngine; +using UnityEngine.Networking; namespace RetroUnity { public class GameManager : MonoBehaviour { @@ -33,21 +36,53 @@ private void Update() { } } - public void LoadRom(string path) { + private IEnumerator RequestRoutine(string url, Action callback = null) + { + // Using the static constructor + var request = UnityWebRequest.Get(url); + + // Wait for the response and then get our data + yield return request.SendWebRequest(); + var data = request.downloadHandler.data; + + // This isn't required, but I prefer to pass in a callback so that I can + // act on the response data outside of this function + if (callback != null) + callback(data); + } + + public void LoadRom(string romPath) { #if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX || UNITY_STANDALONE_LINUX // If the file doesn't exist the application gets stuck in a loop. - if (!File.Exists(path)) + if (!File.Exists(romPath)) { - Debug.LogError(path + " not found."); + Debug.LogError(romPath + " not found."); return; } #endif + Display.material.color = Color.white; wrapper = new LibretroWrapper.Wrapper(CoreName); - wrapper.Init(); - wrapper.LoadGame(path); + +#if UNITY_ANDROID + Action AfterReadingRomFromAPK = bytes => + { + // Using persistentDataPath + romPath = Path.Combine(Application.persistentDataPath, RomName); + // Write + File.WriteAllBytes(romPath, bytes); + Debug.Log($"Copied to {romPath}"); + // Load Rom + wrapper.LoadGame(romPath); + }; + // Async + StartCoroutine(RequestRoutine(romPath, AfterReadingRomFromAPK)); +#else + wrapper.LoadGame(romPath); +#endif + } private void OnDestroy() { diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index 615c103..bc96571 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -164,7 +164,7 @@ PlayerSettings: iPhoneStrippingLevel: 0 iPhoneScriptCallOptimization: 0 ForceInternetPermission: 0 - ForceSDCardPermission: 0 + ForceSDCardPermission: 1 CreateWallpaper: 0 APKExpansionFiles: 0 keepLoadedShadersAlive: 0