Skip to content

Feature/android build #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 37 additions & 18 deletions Assets/Plugins/RetroUnity/Scripts/CoreDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static void DownloadCores()
{
var coreNames = new List<string>()
{
"snes9x","blastem", "nestopia"
"snes9x","blastem", "nestopia", "mgba"
};
foreach (var coreName in coreNames)
{
Expand All @@ -51,8 +51,9 @@ string extractDirectory(BuildTarget buildTarget)
}
}

void unzipCore(BuildTarget buildTarget, string zipPath, string extractDirectory)
List<string> unzipCore(string zipPath, string extractDirectory)
{
List<string> assets = new List<string>();
try
{
using (var archive = ZipFile.OpenRead(zipPath))
Expand All @@ -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);
}
}
}
Expand All @@ -86,7 +77,9 @@ void unzipCore(BuildTarget buildTarget, string zipPath, string extractDirectory)
File.Delete(zipPath);
}

return assets;
}


static void DownloadCores(string romName)
{
Expand All @@ -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)
{
Expand All @@ -126,14 +119,40 @@ static void DownloadCores(string romName)

}

static void setNativePluginLibrary(BuildTarget buildTarget, string pluginRelativePath)

static void ImportAssets(BuildTarget buildTarget, List<string> 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);
}

}
Expand Down
49 changes: 42 additions & 7 deletions Assets/Plugins/RetroUnity/Scripts/GameManager.cs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -33,21 +36,53 @@ private void Update() {
}
}

public void LoadRom(string path) {
private IEnumerator RequestRoutine(string url, Action<byte[]> 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<byte[]> 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() {
Expand Down
2 changes: 1 addition & 1 deletion ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ PlayerSettings:
iPhoneStrippingLevel: 0
iPhoneScriptCallOptimization: 0
ForceInternetPermission: 0
ForceSDCardPermission: 0
ForceSDCardPermission: 1
CreateWallpaper: 0
APKExpansionFiles: 0
keepLoadedShadersAlive: 0
Expand Down