📄 cmd.java
字号:
/* * Cmd.java * Copyright (C) 2003 * * $Id: Cmd.java,v 1.18 2006/01/21 21:53:32 salomo Exp $ *//* Copyright (C) 1997-2001 Id Software, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package jake2.game;import jake2.Defines;import jake2.Globals;import jake2.game.monsters.M_Player;import jake2.qcommon.*;import jake2.server.SV_GAME;import jake2.util.Lib;import java.util.*;import java.util.Arrays;import java.util.Vector;/** * Cmd */public final class Cmd { static xcommand_t List_f = new xcommand_t() { public void execute() { cmd_function_t cmd = Cmd.cmd_functions; int i = 0; while (cmd != null) { Com.Printf(cmd.name + '\n'); i++; cmd = cmd.next; } Com.Printf(i + " commands\n"); } }; static xcommand_t Exec_f = new xcommand_t() { public void execute() { if (Cmd.Argc() != 2) { Com.Printf("exec <filename> : execute a script file\n"); return; } byte[] f = null; f = FS.LoadFile(Cmd.Argv(1)); if (f == null) { Com.Printf("couldn't exec " + Cmd.Argv(1) + "\n"); return; } Com.Printf("execing " + Cmd.Argv(1) + "\n"); Cbuf.InsertText(new String(f)); FS.FreeFile(f); } }; static xcommand_t Echo_f = new xcommand_t() { public void execute() { for (int i = 1; i < Cmd.Argc(); i++) { Com.Printf(Cmd.Argv(i) + " "); } Com.Printf("'\n"); } }; static xcommand_t Alias_f = new xcommand_t() { public void execute() { cmdalias_t a = null; if (Cmd.Argc() == 1) { Com.Printf("Current alias commands:\n"); for (a = Globals.cmd_alias; a != null; a = a.next) { Com.Printf(a.name + " : " + a.value); } return; } String s = Cmd.Argv(1); if (s.length() > Defines.MAX_ALIAS_NAME) { Com.Printf("Alias name is too long\n"); return; } // if the alias already exists, reuse it for (a = Globals.cmd_alias; a != null; a = a.next) { if (s.equalsIgnoreCase(a.name)) { a.value = null; break; } } if (a == null) { a = new cmdalias_t(); a.next = Globals.cmd_alias; Globals.cmd_alias = a; } a.name = s; // copy the rest of the command line String cmd = ""; int c = Cmd.Argc(); for (int i = 2; i < c; i++) { cmd = cmd + Cmd.Argv(i); if (i != (c - 1)) cmd = cmd + " "; } cmd = cmd + "\n"; a.value = cmd; } }; public static xcommand_t Wait_f = new xcommand_t() { public void execute() { Globals.cmd_wait = true; } }; public static cmd_function_t cmd_functions = null; public static int cmd_argc; public static String[] cmd_argv = new String[Defines.MAX_STRING_TOKENS]; public static String cmd_args; public static final int ALIAS_LOOP_COUNT = 16; /** * Register our commands. */ public static void Init() { Cmd.AddCommand("exec", Exec_f); Cmd.AddCommand("echo", Echo_f); Cmd.AddCommand("cmdlist", List_f); Cmd.AddCommand("alias", Alias_f); Cmd.AddCommand("wait", Wait_f); } private static char expanded[] = new char[Defines.MAX_STRING_CHARS]; private static char temporary[] = new char[Defines.MAX_STRING_CHARS]; public static Comparator PlayerSort = new Comparator() { public int compare(Object o1, Object o2) { int anum = ((Integer) o1).intValue(); int bnum = ((Integer) o2).intValue(); int anum1 = GameBase.game.clients[anum].ps.stats[Defines.STAT_FRAGS]; int bnum1 = GameBase.game.clients[bnum].ps.stats[Defines.STAT_FRAGS]; if (anum1 < bnum1) return -1; if (anum1 > bnum1) return 1; return 0; } }; /** * Cmd_MacroExpandString. */ public static char[] MacroExpandString(char text[], int len) { int i, j, count; boolean inquote; char scan[]; String token; inquote = false; scan = text; if (len >= Defines.MAX_STRING_CHARS) { Com.Printf("Line exceeded " + Defines.MAX_STRING_CHARS + " chars, discarded.\n"); return null; } count = 0; for (i = 0; i < len; i++) { if (scan[i] == '"') inquote = !inquote; if (inquote) continue; // don't expand inside quotes if (scan[i] != '$') continue; // scan out the complete macro, without $ Com.ParseHelp ph = new Com.ParseHelp(text, i + 1); token = Com.Parse(ph); if (ph.data == null) continue; token = Cvar.VariableString(token); j = token.length(); len += j; if (len >= Defines.MAX_STRING_CHARS) { Com.Printf("Expanded line exceeded " + Defines.MAX_STRING_CHARS + " chars, discarded.\n"); return null; } System.arraycopy(scan, 0, temporary, 0, i); System.arraycopy(token.toCharArray(), 0, temporary, i, token.length()); System.arraycopy(ph.data, ph.index, temporary, i + j, len - ph.index - j); System.arraycopy(temporary, 0, expanded, 0, 0); scan = expanded; i--; if (++count == 100) { Com.Printf("Macro expansion loop, discarded.\n"); return null; } } if (inquote) { Com.Printf("Line has unmatched quote, discarded.\n"); return null; } return scan; } /** * Cmd_TokenizeString * * Parses the given string into command line tokens. $Cvars will be expanded * unless they are in a quoted token. */ public static void TokenizeString(char text[], boolean macroExpand) { String com_token; cmd_argc = 0; cmd_args = ""; int len = Lib.strlen(text); // macro expand the text if (macroExpand) text = MacroExpandString(text, len); if (text == null) return; len = Lib.strlen(text); Com.ParseHelp ph = new Com.ParseHelp(text); while (true) { // skip whitespace up to a /n char c = ph.skipwhitestoeol(); if (c == '\n') { // a newline seperates commands in the buffer c = ph.nextchar(); break; } if (c == 0) return; // set cmd_args to everything after the first arg if (cmd_argc == 1) { cmd_args = new String(text, ph.index, len - ph.index); cmd_args.trim(); } com_token = Com.Parse(ph); if (ph.data == null) return; if (cmd_argc < Defines.MAX_STRING_TOKENS) { cmd_argv[cmd_argc] = com_token; cmd_argc++; } } } public static void AddCommand(String cmd_name, xcommand_t function) { cmd_function_t cmd; //Com.DPrintf("Cmd_AddCommand: " + cmd_name + "\n"); // fail if the command is a variable name if ((Cvar.VariableString(cmd_name)).length() > 0) { Com.Printf("Cmd_AddCommand: " + cmd_name + " already defined as a var\n"); return; } // fail if the command already exists for (cmd = cmd_functions; cmd != null; cmd = cmd.next) { if (cmd_name.equals(cmd.name)) { Com .Printf("Cmd_AddCommand: " + cmd_name + " already defined\n"); return; } } cmd = new cmd_function_t(); cmd.name = cmd_name; cmd.function = function; cmd.next = cmd_functions; cmd_functions = cmd; } /** * Cmd_RemoveCommand */ public static void RemoveCommand(String cmd_name) { cmd_function_t cmd, back = null; back = cmd = cmd_functions; while (true) { if (cmd == null) { Com.Printf("Cmd_RemoveCommand: " + cmd_name + " not added\n"); return; } if (0 == Lib.strcmp(cmd_name, cmd.name)) { if (cmd == cmd_functions) cmd_functions = cmd.next; else back.next = cmd.next; return; } back = cmd; cmd = cmd.next; } } /** * Cmd_Exists */ public static boolean Exists(String cmd_name) { cmd_function_t cmd; for (cmd = cmd_functions; cmd != null; cmd = cmd.next) { if (cmd.name.equals(cmd_name)) return true; } return false; } public static int Argc() { return cmd_argc; } public static String Argv(int i) { if (i < 0 || i >= cmd_argc) return ""; return cmd_argv[i]; } public static String Args() { return new String(cmd_args); } /** * Cmd_ExecuteString * * A complete command line has been parsed, so try to execute it * FIXME: lookupnoadd the token to speed search? */ public static void ExecuteString(String text) { cmd_function_t cmd; cmdalias_t a; TokenizeString(text.toCharArray(), true); // execute the command line if (Argc() == 0) return; // no tokens // check functions for (cmd = cmd_functions; cmd != null; cmd = cmd.next) { if (cmd_argv[0].equalsIgnoreCase(cmd.name)) { if (null == cmd.function) { // forward to server command Cmd.ExecuteString("cmd " + text); } else { cmd.function.execute(); } return; } } // check alias for (a = Globals.cmd_alias; a != null; a = a.next) { if (cmd_argv[0].equalsIgnoreCase(a.name)) { if (++Globals.alias_count == ALIAS_LOOP_COUNT) { Com.Printf("ALIAS_LOOP_COUNT\n"); return; } Cbuf.InsertText(a.value); return; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -