2012-03-22 48 views
5

我刚刚启动了一个AI机器人游戏nethack,我无法绕过源代码中的“人工检查”。我说的是一段代码是nethack/sys/unix/unixunix.c如何从Node.js连接到nethack?

#ifdef TTY_GRAPHICS 
    /* idea from rpick%[email protected] 
    * 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的(而不是任何其他语言或框架,如果可能)我可以用黑魔法来解决这个问题吗?

+1

我不确定这一点,你可能想看看节点的[TTY模块](http://nodejs.org/api/tty.html)。此外,[此线程](http://groups.google.com/group/nodejs/browse_thread/thread/6fd25d16b250aa7d)可能会引起您的兴趣。 – 2012-03-22 11:16:04

+0

是的,我已经检出了TTY模块:似乎v0.6 +弃用了'tty.open()'方法,这可能是我想要的,但该方法使用了不推荐的'process.binding 'stdio')'调用,我找不到任何文档。我会检查线程。谢谢。 – chrisdotcode 2012-03-22 16:02:43

回答

3

这是一种跛脚检查,因为ptys将在isatty()返回true。 Pty代表Pseudo terminal,它允许一个程序伪装成一个终端。这是Xterm和Screen的工作原理。如果该检查不允许通过你的程序无法在其中播放NetHack。

我从来没有使用它,但pty.js绑定到你将在C代码中使用的接口是有意义的。

+0

这看起来很完美,谢谢:-) – chrisdotcode 2013-05-18 15:48:25