我刚刚开始为游戏 nethack
开发一个 AI 机器人,我无法绕过源代码中的“人工检查”。我正在谈论的代码部分是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 子进程并告诉它启动 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(如果可能,不要使用任何其他语言或框架)黑魔法来解决这个问题?
最佳答案
这是一种蹩脚的检查,因为 ptys 将在 isatty() 中返回 true。 Pty代表Pseudo terminal这允许一个程序假装是一个终端。这就是 Xterm 和 Screen 的工作方式。如果该检查不允许这些程序通过,您将无法在其中玩 NetHack。
我从未使用过它,但 pty.js绑定(bind)到您将在 C 代码中使用的内容,并且接口(interface)很有意义。
关于javascript - 如何从 Node.js 连接到 nethack?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9815728/