无法与"Stockfish-9-armv7"进行通信二进制文件 [英] Not able to communicate with "Stockfish-9-armv7" binary file

查看:186
本文介绍了无法与"Stockfish-9-armv7"进行通信二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Unity3D中开发国际象棋游戏.我想为Android平台开发它.对于AI,我正在使用Stockfish Chess Engine.我下载了适用于Android的Stockfish二进制文件,名为" Stockfish-9-armv7 ".我将此二进制文件放置在StreamingAssets文件夹中,以便在构建步骤中将其正确放入目标平台.一切正常,直到此处,即当我构建Unity项目时,文件都放置在正确的位置,并且我可以很好地看到它.

I am developing a Chess Game in Unity3D. I want to develop it for the Android platform. For AI I am using the Stockfish Chess Engine. I downloaded the Stockfish binary for Android named "Stockfish-9-armv7". I placed this binary file in my StreamingAssets folder so that it correctly goes into the targeted platform during build step. Everything works fine till here i.e. when I build my Unity project the file is placed in the correct location and I can very well see it.

现在,为了使我的AI正常工作,我必须使用 UCI 协议.因此,我在Unity项目中编写了一个C#脚本,该脚本创建了一个进程来运行二进制文件并与之通信.但这是行不通的.

Now in order for my AI to work I have to communicate with this binary file using UCI protocols. And so I wrote a C# script in my Unity project that creates a process to run the binary file and communicate with it. But this is not working.

但是,当我为Windows执行完全相同的操作时,即使用Windows二进制版本的Stockfish命名为"stockfish_9_x64.exe"并将其构建为独立应用程序时,一切运行良好,并且我能够通过C#与引擎进行通信代码.

However when I do the exact same thing for Windows i.e. using the windows binary version of Stockfish named "stockfish_9_x64.exe" and building it as a standalone application, things work perfectly and I am able to communicate with the engine via my C# code.

我在线上进行了研究,但是找不到太多的资源和指导.我发现了类似的帖子,通读它,我得出的结论是,这可能与文件权限有关.提出此问题的人实际上通过编写以下两行代码解决了该问题:

I researched it online but unable to find much resources and guidance. I found a similar post and reading through it led me conclude that maybe it has something to do with file permissions. The guy who asked this question actually solved the issue by writing these two lines of code:

string[] cmd = { "chmod", "744", Path.Combine(strToFolder, fileName) };
Java.Lang.Runtime.GetRuntime().Exec(cmd);  

但是,他正在使用Xamarin并可以访问Java Runtime库.我正在使用Unity和C#,但我真的不知道如何更改此二进制文件的执行/运行权限并运行它.实际上,我什至不知道这是否是问题.

However he was using Xamarin and had access to Java Runtime library. I am using Unity and C# and I really don't know how to change the execute/run permission of this binary file and run it. In fact I don't even know whether this is the problem or not.

我只想将Stockfish以Android为目标平台集成到我的Unity项目中.如果有人有任何想法,建议或以前有人这样做过,请指导我.即使我从一开始就错了,而且我的方法是越野车,但也要与正确的方法一起让我知道这一点.

I just want to integrate stockfish into my Unity project with Android as the targeted platform. If anyone has any ideas, suggestions or if anyone has done this before, please guide me. Even if I am wrong from the start and my approach is buggy let me know that as well along with the corrected approach.

下面是我的代码:

public class CommunicateWithEngine {

    public static Process mProcess;

    public static void Communicate()
    {
        // since the apk file is archived this code retreives the stockfish binary data and
        // creates a copy of it in the persistantdatapath location.
        string filepath = Application.persistentDataPath + "/" + "Stockfish-9-armv7";
        if (!File.Exists(filepath))
        {
            WWW executable = new WWW("jar:file://" + Application.dataPath + "!/assets/" + "Stockfish-9-armv7");
            while (!executable.isDone)
            {
            }
            File.WriteAllBytes(filepath, executable.bytes);
        }

        // creating the process and communicating with the engine
        mProcess = new Process();
        ProcessStartInfo si = new ProcessStartInfo()
        {
            FileName = System.IO.Path.Combine(Application.persistentDataPath, "Stockfish-9-armv7"),
            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");

    }


    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;
        Test.PrintStringToTheConsole(text);
    }
}

推荐答案

适用于与OP和我一样遇到相同问题的任何人.经过12个小时的搜索和谷歌搜索后,这里是一个解决方案.您可以使用可执行的stockfish二进制文件,但必须将二进制文件的扩展名更改为.so,然后将其统一导入到/Assets/Plugins/Android文件夹中.构建后,您可以在设备上的/data/data//lib文件夹中找到它.在这里您可以执行它并获取输入和输出.

For anyone struggling with the same problem as OP and me. After 12 hours searching and googling here is a solution. You can use executable stockfish binary, but you have to change extension for binary to .so and import it in unity in folder /Assets/Plugins/Android. After build you can find it on device in folder /data/data//lib. Here you can execute it and get input and output.

这篇关于无法与"Stockfish-9-armv7"进行通信二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆