6

我刚刚为游戏启动了一个 AI 机器人nethack,我无法绕过源代码中的“人工检查”。我正在谈论的代码部分是nethack/sys/unix/unixunix.c

#ifdef TTY_GRAPHICS
    /* idea from rpick%ucqais@uccba.uc.edu
     * prevent automated rerolling of characters
     * test input (fd0) so that tee'ing output to get a screen dump still
     * works
     * also incidentally prevents development of any hack-o-matic programs
     */
    /* added check for window-system type -dlc */
    if (!strcmp(windowprocs.name, "tty"))
        if (!isatty(0))
        error("You must play from a terminal.");
#endif

我正在使用 JavaScript(更具体地说是 Node.js),由于上述原因,它不会让我从程序中播放,即使我正在生成一个 bash shell 子进程并告诉它 start nethack。我需要想办法绕过上述方法而不重新编译源代码。

我正在使用的当前代码是:

"use strict";

var env = { TERM: 'tty' };
for (var k in process.env) {
    env[k] = process.env[k];
}

var terminal = require('child_process').spawn('bash', [], {
    env: env,
});

terminal.stdout.on('data', function (data) {
    console.log('stdout: ' + data);
});

terminal.on('exit', function (code) {
        console.log('child process exited with code ' + code);
});

setTimeout(function() {
    terminal.stdin.write('nethack');
    terminal.stdin.end();
}, 1000);

程序的输出是:

stdout: You must play from a terminal.

child process exited with code 1

我可以使用什么 Node.js/JavaScript(而不是任何其他语言或框架,如果可能的话)黑魔法来解决这个问题?

4

1 回答 1

3

这是一种蹩脚的检查,因为 ptys 将在isatty()中返回 true 。Pty 代表Pseudo terminal,它允许程序伪装成终端。这就是 Xterm 和 Screen 的工作方式。如果该检查不允许这些程序通过,您将无法在其中玩 NetHack。

我从未使用过它,但pty.js完全绑定到您将在 C 代码中使用的内容,并且界面很有意义。

于 2013-05-17T01:48:58.400 回答