new Process Access Denied
Hi,
I have been trying to run the chess engine binary stockfish from Android (stockfish-10-armv7). For PC in editor and .exe it works fine but I have an access denied error when I try to run it through new Process() on Android.
The binary is in the steamingAssets and at run time i can find it using unitywebrequest and write it into the Application.persistentDataPath. I have checked to see if i have the write /read privilege for external storage.
Anyone have any ideas?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Android;
// Example FEN
//"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
public static class StockFish
{
public static Process mProcess;
public static string engineText="";
public static void StartEngine()
{
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageWrite))
{
Permission.RequestUserPermission(Permission.ExternalStorageWrite);
}
if (!Permission.HasUserAuthorizedPermission(Permission.ExternalStorageRead))
{
Permission.RequestUserPermission(Permission.ExternalStorageRead);
}
string sfBin = "stockfish-10-armv7";
//string perFilePath = GetAndroidFriendlyFilesPath();
//string sfPathFile = Path.Combine(perFilePath, sfBin);
string sfPathFile = Path.Combine(Application.persistentDataPath, sfBin);
string sfPathAsset = Path.Combine(Application.streamingAssetsPath, sfBin);
loadWeb(sfPathAsset, sfPathFile);
if (!File.Exists(sfPathFile))
{
engineLog("-------FILE NOT FOUND--------------------");
return;
}
else
{
engineLog("FILE EXISTS -------------------------------");
}
try
{
mProcess = new Process();
ProcessStartInfo si = new ProcessStartInfo()
{
FileName = sfPathFile,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true
};
mProcess.StartInfo = si;
mProcess.OutputDataReceived += new DataReceivedEventHandler(MProcess_OutputDataReceived);
mProcess.Start();
mProcess.BeginErrorReadLine();
mProcess.BeginOutputReadLine();
}
catch (Exception e)
{
engineLog("ERROR Process:" + e.Message);
return;
}
engineLog("------RUN OK----------------");
UciCmd("uci");
UciCmd("isready");
//SendLine("position fen " + forsythEdwardsNotationString);
//SendLine("go movetime 5000");
}
public static void loadWeb(string inFile, string outFile)
{
var loadingRequest = UnityWebRequest.Get(inFile);
loadingRequest.SendWebRequest();
while (!loadingRequest.isDone)
{
if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
{
engineLog("While network error");
break;
}
}
if (loadingRequest.isNetworkError || loadingRequest.isHttpError)
{
engineLog("network error");
}
else
{
try
{
File.WriteAllBytes(outFile, loadingRequest.downloadHandler.data);
}
catch (Exception e)
{
engineLog("ERROR File Write:" + e.Message);
return;
}
}
}
public static void engineLog(string engString)
{
engineText = engineText + engString + "\n";
}
public static void UciCmd(string command)
{
mProcess.StandardInput.WriteLine(command);
mProcess.StandardInput.Flush();
}
private static void MProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
engineLog(e.Data.ToString());
}
private static string GetAndroidFriendlyFilesPath()
{
#if UNITY_ANDROID
AndroidJavaClass up = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = up.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject applicationContext = currentActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaObject path = applicationContext.Call<AndroidJavaObject>("getFilesDir");
string filesPath = path.Call<string>("getCanonicalPath");
return filesPath;
#endif
return Application.persistentDataPath;
}
}
Your answer
Follow this Question
Related Questions
Access to the Application.persistentDataPath is denied in Android 1 Answer
Android Logcat error : "Access to "/Desktop" is denied." 0 Answers
How can I port this Windows chess project to Android and iOS Devices? 0 Answers
Unity How to limit rotation of canon with touch for Android 0 Answers
Unity UI - Button Slow On Some Devices? 0 Answers