matty just provided you with two possible implementations of how you could read a single line from a file (what's in a function name?). Basically, you copy one of those functions in your script and call them:
js code:
var line = ReadLineFromFile("C:\\Program Files\\AssaultCube_v1.1.0.3\\config\\saved.cfg", 255);
// line now contains the 255th line from saved.cfg
Of course, the most important part is when you call the function and what you do with it. If I understand correctly, you want to create a new chat command "/acserv" which will read the line, do some modifications with it and then output it as a chat message.
- In order to make your script do something when a chat message is sent by the current user, you'll need to create an OnEvent_ChatWndSendMessage function. This function will be called whenever a message is sent, allowing you to modify it before it gets sent. Have a look at the scripting documentation on how to define this event in your script.
- Inside this function, you'll want to check whether the message starts with your command "/acserv". There are a lot of cases which you need to take into account when parsing script commands, for example when there is more than one slash before a command, that command should be escaped and thus not parsed at all. Also, there are some limitations on which characters are allowed in the command name and the parameters. Luckily, CookieRevised wrote a regular expression which deals with all these cases.
js code:
var m;
if ( m = /^\/([^ \n\r\v\xA0\/][^ \n\r\v\xA0]*)[\s\xA0]?([\s\S]*)/.exec(sMessage) ) {
var command = m[1].toLowerCase();
var parameter = m[2];
if ( command == 'acserv' ) {
// do something and return a modified message
}
}
return sMessage;
Source: CookieRevised's reply to Gettin data from "/" commands (small modifications by myself)
- Inside the command parser check if ( command == 'acserv' ) { ... }, you place your actual code. That is, call ReadLineFromFile, remove something from the returned string and output it by returning it back to the event function. So you start off with the first code block in this post, then do some replace() on the string, such as:
js code:
line = line.replace('alias "serverinfo [', '');
line = line.replace(']', '');
and finally return line;.
If you're planning to use the second function matty posted, you'll want to fix a few things in his code:
js code:
function ReadLineFromFile(sFile, nLine) {
var f = new ActiveXObject("Scripting.FileSystemObject").OpenTextFile(sFile, 1 /* ForReading */);
var s = f.ReadAll();
f.Close(); // added semicolon
var FileContents = s.split(/\r?\n/); // matches both \r\n and \n
return FileContents[nLine-1]; // fixed variable name and needs [ ] instead of ( ) since it's an array accessor.
}
I can't tell which of those implementations is the best, they all look pretty good. In most cases, the first method will be faster since it won't have to read the whole file, yet the second method may be easier to understand.