Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Android Executable | Problems implementing stockfish (UCI) for android (StreamingAssets)

Discussion in 'Scripting' started by aajaces, Dec 23, 2020.

  1. aajaces

    aajaces

    Joined:
    Dec 20, 2018
    Posts:
    2
    Hey y'all!

    I'm building a UCI (universal chess interface) compatible chess engine for VR and having trouble with the Android engine execution. It's only a few lines of code, but it has been breaking my mind for the last few days. I've tried compiling the C++ game engine code on my own (https://github.com/official-stockfish/Stockfish), but eventually found this:
    https://github.com/vjs22334/StockFishTest
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using System.IO;
    5. using UnityEngine.UI;
    6. using System.Diagnostics;
    7.  
    8. public class StockFishApi : MonoBehaviour
    9. {
    10.  
    11.     public Text OutPutText;
    12.  
    13.     string outPut = "";
    14.     public static Process mProcess;
    15.  
    16.     [SerializeField] InputField FENInput;
    17.     [SerializeField] InputField DepthInput;
    18.     [SerializeField] InputField TimeInput;
    19.  
    20.     void Start()
    21.     {
    22.         Setup();
    23.     }
    24.  
    25.     void Update()
    26.     {
    27.         OutPutText.text = outPut;
    28.     }
    29.  
    30.     public void Setup()
    31.     {
    32.         // since the apk file is archived this code retrieves the stockfish binary data and
    33.         // creates a copy of it in the persistantdatapath location.
    34.         #if UNITY_ANDROID
    35.         string filepath = Application.persistentDataPath + "/" + "stockfish-10-armv7";
    36.         if (!File.Exists(filepath))
    37.         {
    38.             WWW executable = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "stockfish-10-armv7");
    39.             while (!executable.isDone)
    40.             {
    41.             }
    42.             File.WriteAllBytes(filepath, executable.bytes);
    43.  
    44.             //change permissions via plugin
    45.          
    46.         }
    47.         var plugin = new AndroidJavaClass("com.chessbattles.jeyasurya.consoleplugin.AndroidConsole");
    48.             string command = "chmod 777 "+filepath;
    49.             outPut = plugin.CallStatic<string>("ExecuteCommand",command);
    50.            
    51.         #else
    52.         string filepath = Application.streamingAssetsPath+ "/" + "stockfish_10_x64.exe";
    53.         #endif
    54.         // creating the process and communicating with the engine
    55.         mProcess = new Process();
    56.         ProcessStartInfo si = new ProcessStartInfo()
    57.         {
    58.             FileName = filepath,
    59.             UseShellExecute = false,
    60.             CreateNoWindow = true,
    61.             RedirectStandardError = true,
    62.             RedirectStandardInput = true,
    63.             RedirectStandardOutput = true
    64.         };
    65.         mProcess.StartInfo = si;
    66.         mProcess.OutputDataReceived += new DataReceivedEventHandler(MProcess_OutputDataReceived);
    67.         mProcess.Start();
    68.         mProcess.BeginErrorReadLine();
    69.         mProcess.BeginOutputReadLine();
    70.  
    71.         SendLine("uci");
    72.         SendLine("isready");
    73.  
    74.     }
    75.  
    76.     public void GetMove(){
    77.         string Fen = FENInput.text;
    78.         string DepthValue = DepthInput.text;
    79.         string processTime = TimeInput.text;
    80.  
    81.         if(Fen==null || Fen == ""){
    82.             UnityEngine.Debug.LogError("Enter proper Fen");
    83.             outPut = "Enter proper Fen";
    84.             return;
    85.         }
    86.  
    87.         SendLine("position fen "+Fen);
    88.  
    89.         if(processTime != ""){
    90.             SendLine("go movetime "+processTime);
    91.         }
    92.         else{
    93.             SendLine("go depth "+DepthValue);
    94.         }
    95.  
    96.     }
    97.  
    98.  
    99.     public void SendLine(string command) {
    100.         mProcess.StandardInput.WriteLine(command);
    101.         mProcess.StandardInput.Flush();
    102.     }
    103.  
    104.     void MProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
    105.     {
    106.  
    107.         UnityEngine.Debug.Log("Output:"+e.Data);
    108.  
    109.         outPut = e.Data;
    110.        
    111.        
    112.     }
    113. }
    I've been fiddling around with it quite a bit, but unfortunately cannot get it to work on android (the windows runs great of course, since it doesn't involve any of that jar:file or chmod 777 permission stuff). One thing I can think of is that the WWW (now obsolete) needs to be updated to the new UnityWebRequest. Is this the issue? Is it the pathing? Or is it the permissions?

    I found this forum which might help: https://stackoverflow.com/questions...ockfish-into-an-android-app/47851924#47851924

    I've exhausted just about every online source so any help is much appreciated!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    For Android, always start by attaching the
    adb logcat
    and seeing if its throwing any exceptions.

    You can also use this to see the Debug.Log() on device and see if any of the above code is running and what the data involved might be.

    It's also possible that Java can't be just arbitrarily executed on Android; they are a signed executable environment, but my knowledge of Android Java integration ends at that point.
     
  3. aajaces

    aajaces

    Joined:
    Dec 20, 2018
    Posts:
    2
    Thanks for the tips. I had actually installed logcat and it was throwing many many lines of exceptions at a time, but it admittedly is difficult to read... so I figured it was simply because the executable wasn't loaded correctly in the first place. I'll look up some of the errors again and see if I can find anything specific.
     
  4. Ritik1999

    Ritik1999

    Joined:
    Jan 8, 2019
    Posts:
    2
    Is your problem resolved ?
     
  5. Lianyou4949

    Lianyou4949

    Joined:
    Jul 17, 2016
    Posts:
    1
    Hi i need your project source code. Please gift me. Good works.