1
\$\begingroup\$

I am making the chess interface I designed for a game. I am using the process to get answers from a stock fish binary:

stockfish = new Process {
                StartInfo = new ProcessStartInfo {
                    FileName = System.IO.Directory.GetCurrentDirectory () + "\\Assets\\Scripts\\Stockfish\\stockfish.exe",
                    Arguments = "",
                    UseShellExecute = false,
                    RedirectStandardOutput = true,
                    RedirectStandardInput = true,
                    RedirectStandardError = true,
                    CreateNoWindow = true
                }

            };

The problem I face is it worked for my Windows build but for mobile package build it won't work with the executable. Is there a way around it?

\$\endgroup\$
2
\$\begingroup\$

This line should only ever work in Windows version (or for versions that your VS supports):

FileName = System.IO.Directory.GetCurrentDirectory () + "\\Assets\\Scripts\\Stockfish\\stockfish.exe",

For Unity's builder, you need to use Application.dataPath or Application.persistentDataPath. I've had success with using both, the former for accessing the game folder and the latter for saving or loading files.

\$\endgroup\$
3
  • 2
    \$\begingroup\$ You'd also need a version of the external binary compiled for your target platform, as the exe is a Windows executable. \$\endgroup\$
    – DMGregory
    Dec 22 '16 at 14:42
  • \$\begingroup\$ What is the stranded binary for Android and how can i access it ? Can you guide me through on it. \$\endgroup\$ Dec 26 '16 at 10:18
  • \$\begingroup\$ @AhmadIftikhar apk, which you get by changing the build target in Unity. \$\endgroup\$ Jan 8 '19 at 15:27
1
\$\begingroup\$

I have managed to make this work like this. You need to update the package name in the file path.

using System.Diagnostics;
using System.IO;
using UnityEngine;

namespace Assets.Scripts
{
    public class EngineCommunicator
    {
        public static Process mProcess;
        public static string fileName = "stockfish.android.armv7";
        public static void Communicate()
        {
#if UNITY_EDITOR
            string filepath = "E:\\Personal\\Unity\\Chess2d\\Assets\\Plugins\\Windows\\stockfish_13_win_x64";
#elif UNITY_ANDROID
            string filepath = "/data/data/com.tiringbring.Chess2d/lib/stockfish.android.armv7.so";
            UI.Instance.AddJob(() =>
            {
                UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text = filepath;
            });
            

            if (!File.Exists(filepath))
            {
                UI.Instance.AddJob(() =>
                {
                    UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text =UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text +" "+ "file not found";
                });
            }else{
                UI.Instance.AddJob(() =>
                {
                    UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text =UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text +" "+ "file found";
                });
            }
#else
        string filepath = Application.streamingAssetsPath+ "/" + "stockfish_13_x64.exe";
#endif
            // creating the process and communicating with the engine
            mProcess = new Process();
            ProcessStartInfo si = new ProcessStartInfo()
            {
                FileName = filepath,
                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();

            SendLine("uci");
            SendLine("isready");
            SendLine("ucinewgame");
            SendLine("position startpos");
            SendLine("go infinite searchmoves e2e4 d2d4");


        }


        private static void SendLine(string command)
        {
            mProcess.StandardInput.WriteLine(command);
            mProcess.StandardInput.Flush();
        }

        private static void MProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            string text = e.Data;
            UI.Instance.AddJob(() =>
            {
                UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text = UI.Instance.TestText.GetComponent<TMPro.TextMeshProUGUI>().text +" "+ text;
            });

            UnityEngine.Debug.Log(text);
        }
    }
}

Put the file in Assets/Android directory with .so extension.

enter image description here

It will be copied automatically in lib folder on android mobile

enter image description here

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.