DEADSOFTWARE

Console: Parsing flag should be set by default - addendum to 5c79a5c
[d2df-sdl.git] / src / game / g_console.pas
index 33bfcf7dafedefaef8e4b74a39c8d07e746f151d..edf484f1dd8177dc59170710d0c48c6f4efeffb7 100644 (file)
@@ -48,6 +48,7 @@ procedure g_Console_Clear;
 function  g_Console_CommandBlacklisted (C: AnsiString): Boolean;
 procedure g_Console_ReadConfig (filename: String);
 procedure g_Console_WriteConfig (filename: String);
+procedure g_Console_WriteGameConfig;
 
 function  g_Console_Interactive: Boolean;
 function  g_Console_Action (action: Integer): Boolean;
@@ -77,6 +78,7 @@ var
   gChatTeam: Boolean = false;
   gAllowConsoleMessages: Boolean = true;
   gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
+  gParsingBinds: Boolean = true; // íå ïåðåñîõðàíÿòü êîíôèã âî âðåìÿ ïàðñèíãà
   gPlayerAction: Array [0..1, 0..LAST_ACTION] of Boolean; // [player, action]
 
 implementation
@@ -220,6 +222,8 @@ end;
 
 procedure boolVarHandler (me: PCommand; p: SSArray);
   procedure binaryFlag (var flag: Boolean; msg: AnsiString);
+  var
+    old: Boolean;
   begin
     if (Length(p) > 2) then
     begin
@@ -227,12 +231,15 @@ procedure boolVarHandler (me: PCommand; p: SSArray);
     end
     else
     begin
+      old := flag;
       case conGetBoolArg(p, 1) of
         -1: begin end;
          0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
          1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
          666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
       end;
+      if flag <> old then
+        g_Console_WriteGameConfig();
       if (Length(msg) = 0) then msg := p[0] else msg += ':';
       if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
     end;
@@ -243,24 +250,8 @@ end;
 
 
 procedure intVarHandler (me: PCommand; p: SSArray);
-  procedure binaryFlag (var flag: Boolean; msg: AnsiString);
-  begin
-    if (Length(p) > 2) then
-    begin
-      conwritefln('too many arguments to ''%s''', [p[0]]);
-    end
-    else
-    begin
-      case conGetBoolArg(p, 1) of
-        -1: begin end;
-         0: if not me.cheat or conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
-         1: if not me.cheat or conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
-         666: if not me.cheat or conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
-      end;
-      if (Length(msg) = 0) then msg := p[0] else msg += ':';
-      if flag then conwritefln('%s tan', [msg]) else conwritefln('%s ona', [msg]);
-    end;
-  end;
+var
+  old: Integer;
 begin
   if (Length(p) <> 2) then
   begin
@@ -269,7 +260,10 @@ begin
   else
   begin
     try
+      old := PInteger(me.ptr)^;
       PInteger(me.ptr)^ := StrToInt(p[1]);
+      if PInteger(me.ptr)^ <> old then
+        g_Console_WriteGameConfig();
     except
       conwritefln('invalid integer value: "%s"', [p[1]]);
     end;
@@ -331,7 +325,7 @@ type
 procedure singleVarHandler (me: PCommand; p: SSArray);
 var
   pv: PVarSingle;
-  nv: Single;
+  nv, old: Single;
   msg: AnsiString;
 begin
   if (Length(p) > 2) then
@@ -340,6 +334,7 @@ begin
     exit;
   end;
   pv := PVarSingle(me.ptr);
+  old := pv.val^;
   if (Length(p) = 2) then
   begin
     if me.cheat and (not conIsCheatsEnabled) then begin conwriteln('not available'); exit; end;
@@ -361,6 +356,8 @@ begin
       pv.val^ := nv;
     end;
   end;
+  if pv.val^ <> old then
+    g_Console_WriteGameConfig();
   msg := me.msg;
   if (Length(msg) = 0) then msg := me.cmd else msg += ':';
   conwritefln('%s %s', [msg, pv.val^]);
@@ -648,6 +645,14 @@ begin
   end
 end;
 
+function QuoteStr(str: String): String;
+begin
+  if Pos(' ', str) > 0 then
+    Result := '"' + str + '"'
+  else
+    Result := str;
+end;
+
 procedure BindCommands (p: SSArray);
   var cmd, key: AnsiString; i: Integer;
 begin
@@ -663,7 +668,7 @@ begin
       if i < e_MaxInputKeys then
       begin
         if Length(p) = 2 then
-          g_Console_Add('"' + e_KeyNames[i] + '" = "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"')
+          g_Console_Add(QuoteStr(e_KeyNames[i]) + ' = ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)))
         else if Length(p) = 3 then
           g_Console_BindKey(i, p[2], '')
         else (* len = 4 *)
@@ -679,7 +684,7 @@ begin
   'bindlist':
     for i := 0 to e_MaxInputKeys - 1 do
       if (gInputBinds[i].down <> nil) or (gInputBinds[i].up <> nil) then
-        g_Console_Add(e_KeyNames[i] + ' "' + GetCommandString(gInputBinds[i].down) + '" "' + GetCommandString(gInputBinds[i].up) + '"');
+        g_Console_Add(e_KeyNames[i] + ' ' + QuoteStr(GetCommandString(gInputBinds[i].down)) + ' ' + QuoteStr(GetCommandString(gInputBinds[i].up)));
   'unbind':
     // unbind <key>
     if Length(p) = 2 then
@@ -719,13 +724,15 @@ begin
     end;
   'togglechat':
     begin
-      g_Console_Chat_Switch;
+      if not gConsoleShow and (g_ActiveWindow = nil) then
+        g_Console_Chat_Switch;
       gSkipFirstChar := not g_Console_Interactive()
     end;
   'toggleteamchat':
     if gGameSettings.GameMode in [GM_TDM, GM_CTF] then
     begin
-      g_Console_Chat_Switch(True);
+      if not gConsoleShow and (g_ActiveWindow = nil) then
+        g_Console_Chat_Switch(True);
       gSkipFirstChar := not g_Console_Interactive()
     end;
   end
@@ -849,7 +856,6 @@ begin
   AddCommand('p2_name', GameCVars);
   AddCommand('p1_color', GameCVars);
   AddCommand('p2_color', GameCVars);
-  AddCommand('r_showtime', GameCVars);
   AddCommand('r_showscore', GameCVars);
   AddCommand('r_showlives', GameCVars);
   AddCommand('r_showstat', GameCVars);
@@ -974,6 +980,7 @@ begin
   g_Console_ResetBinds;
   g_Console_ReadConfig(GameDir + '/dfconfig.cfg');
   g_Console_ReadConfig(GameDir + '/autoexec.cfg');
+  gParsingBinds := False;
 
   g_Console_Add(Format(_lc[I_CONSOLE_WELCOME], [GAME_VERSION]));
   g_Console_Add('');
@@ -1667,8 +1674,9 @@ begin
   if key > 0 then
   begin
     gInputBinds[key].down := ParseAlias(down);
-    gInputBinds[key].up := ParseAlias(up)
-  end
+    gInputBinds[key].up := ParseAlias(up);
+  end;
+  g_Console_WriteGameConfig();
 end;
 
 function g_Console_MatchBind (key: Integer; down: AnsiString; up: AnsiString = ''): Boolean;
@@ -1730,8 +1738,11 @@ begin
   if (not g_GUIGrabInput) and (key >= 0) and (key < e_MaxInputKeys) and ((gInputBinds[key].down <> nil) or (gInputBinds[key].up <> nil)) then
   begin
     if down then
-      for i := 0 to High(gInputBinds[key].down) do
-        g_Console_Process(gInputBinds[key].down[i], True)
+    begin
+      if not gChatShow then
+        for i := 0 to High(gInputBinds[key].down) do
+          g_Console_Process(gInputBinds[key].down[i], True);
+    end
     else
       for i := 0 to High(gInputBinds[key].up) do
         g_Console_Process(gInputBinds[key].up[i], True)
@@ -1844,10 +1855,10 @@ begin
         while (i <= len) and (s[i] <= ' ') do inc(i);
         (* skip comments *)
         if (i <= len) and ((s[i] <> '#') and ((i + 1 > len) or (s[i] <> '/') or (s[i + 1] <> '/'))) then
-          g_Console_Process(s, True)
+          g_Console_Process(s, True);
       end
     end;
-    CloseFile(f)
+    CloseFile(f);
   end
 end;
 
@@ -1860,7 +1871,14 @@ begin
   WriteLn(f, 'unbindall');
   for i := 0 to e_MaxInputKeys - 1 do
     if (Length(gInputBinds[i].down) > 0) or (Length(gInputBinds[i].up) > 0) then
-      WriteLn(f, 'bind ', e_KeyNames[i], ' "', GetCommandString(gInputBinds[i].down), '" "', GetCommandString(gInputBinds[i].up), '"');
+    begin
+      Write(f, 'bind ', e_KeyNames[i], ' ', QuoteStr(GetCommandString(gInputBinds[i].down)));
+      if Length(gInputBinds[i].down) = 0 then
+        Write(f, '""');
+      if Length(gInputBinds[i].up) > 0 then
+        Write(f, ' ', QuoteStr(GetCommandString(gInputBinds[i].up)));
+      WriteLn(f, '');
+    end;
   for i := 0 to High(commands) do
   begin
     if not commands[i].cheat then
@@ -1868,20 +1886,26 @@ begin
       if @commands[i].procEx = @boolVarHandler then
       begin
         if PBoolean(commands[i].ptr)^ then j := 1 else j := 0;
-        WriteLn(f, commands[i].cmd, ' "', j, '"')
+        WriteLn(f, commands[i].cmd, ' ', j)
       end
       else if @commands[i].procEx = @intVarHandler then
       begin
-        WriteLn(f, commands[i].cmd, ' "', PInteger(commands[i].ptr)^, '"')
+        WriteLn(f, commands[i].cmd, ' ', PInteger(commands[i].ptr)^)
       end
       else if @commands[i].procEx = @singleVarHandler then
       begin
-        WriteLn(f, commands[i].cmd, ' "', PVarSingle(commands[i].ptr).val^:0:6, '"')
+        WriteLn(f, commands[i].cmd, ' ', PVarSingle(commands[i].ptr).val^:0:6)
       end
     end
   end;
   CloseFile(f)
 end;
 
+procedure g_Console_WriteGameConfig;
+begin
+  if gParsingBinds then
+    Exit;
+  g_Console_WriteConfig(GameDir + '/dfconfig.cfg');
+end;
 
 end.