DEADSOFTWARE

"dbg_scale" debug variable
[d2df-sdl.git] / src / game / g_console.pas
index 0669339cec3dc3ba3f2c4487db4f7acdf278dcaa..e0f401deeade15964e5c93c103ecebfa6065088c 100644 (file)
@@ -18,47 +18,72 @@ unit g_console;
 
 interface
 
-procedure g_Console_Init();
-procedure g_Console_Update();
-procedure g_Console_Draw();
-procedure g_Console_Switch();
-procedure g_Console_Char(C: Char);
-procedure g_Console_Control(K: Word);
-procedure g_Console_Process(L: String; Quiet: Boolean = False);
-procedure g_Console_Add(L: String; Show: Boolean = False);
-procedure g_Console_Clear();
-function  g_Console_CommandBlacklisted(C: String): Boolean;
+uses
+  wadreader; // for SArray
+
+procedure g_Console_Init ();
+procedure g_Console_Update ();
+procedure g_Console_Draw ();
+procedure g_Console_Switch ();
+procedure g_Console_Char (C: AnsiChar);
+procedure g_Console_Control (K: Word);
+procedure g_Console_Process (L: AnsiString; quiet: Boolean=false);
+procedure g_Console_Add (L: AnsiString; show: Boolean=false);
+procedure g_Console_Clear ();
+function  g_Console_CommandBlacklisted (C: AnsiString): Boolean;
+
+procedure conwriteln (const s: AnsiString; show: Boolean=false);
+procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
+
+// <0: no arg; 0/1: true/false
+function conGetBoolArg (p: SArray; idx: Integer): Integer;
+
+procedure g_Console_Chat_Switch (team: Boolean=false);
+
+procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
+procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
 
-procedure g_Console_Chat_Switch(Team: Boolean = False);
 
 var
-  gConsoleShow: Boolean; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
-  gChatShow: Boolean;
-  gChatTeam: Boolean = False;
-  gAllowConsoleMessages: Boolean = True;
-  gChatEnter: Boolean = True;
-  gJustChatted: Boolean = False; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
+  gConsoleShow: Boolean = false; // True - êîíñîëü îòêðûòà èëè îòêðûâàåòñÿ
+  gChatShow: Boolean = false;
+  gChatTeam: Boolean = false;
+  gAllowConsoleMessages: Boolean = true;
+  gChatEnter: Boolean = true;
+  gJustChatted: Boolean = false; // ÷òîáû àäìèí â èíòåðå ÷àòÿñü íå ïðîìàòûâàë ñòàòèñòèêó
+
 
 implementation
 
 uses
   g_textures, g_main, e_graphics, e_input, g_game,
-  SysUtils, g_basic, g_options, wadreader, Math,
-  g_menu, g_language, g_net, g_netmsg, e_log, conbuf;
+  SysUtils, g_basic, g_options, Math,
+  g_menu, g_language, g_net, g_netmsg, e_log, conbuf, utils;
+
 
 type
-  TCmdProc = procedure (P: SArray);
+  PCommand = ^TCommand;
+
+  TCmdProc = procedure (p: SArray);
+  TCmdProcEx = procedure (me: PCommand; p: SArray);
 
   TCommand = record
-    Cmd: String;
-    Proc: TCmdProc;
+    cmd: AnsiString;
+    proc: TCmdProc;
+    procEx: TCmdProcEx;
+    help: AnsiString;
+    hidden: Boolean;
+    ptr: Pointer; // various data
+    msg: AnsiString; // message for var changes
+    cheat: Boolean;
   end;
 
   TAlias = record
-    Name: String;
-    Commands: SArray;
+    name: AnsiString;
+    commands: SArray;
   end;
 
+
 const
   Step = 32;
   Alpha = 25;
@@ -73,21 +98,188 @@ var
   RecursionLimitHit: Boolean = False;
   Cons_Y: SmallInt;
   Cons_Shown: Boolean; // Ðèñîâàòü ëè êîíñîëü?
-  Line: String;
+  Line: AnsiString;
   CPos: Word;
   //ConsoleHistory: SArray;
   CommandHistory: SArray;
   Whitelist: SArray;
-  Commands: Array of TCommand;
-  Aliases: Array of TAlias;
+  commands: Array of TCommand = nil;
+  Aliases: Array of TAlias = nil;
   CmdIndex: Word;
   conSkipLines: Integer = 0;
   MsgArray: Array [0..4] of record
-                              Msg: String;
+                              Msg: AnsiString;
                               Time: Word;
                             end;
 
-function GetStrACmd(var Str: String): String;
+
+// ////////////////////////////////////////////////////////////////////////// //
+// <0: no arg; 0/1: true/false; 666: toggle
+function conGetBoolArg (p: SArray; idx: Integer): Integer;
+begin
+  if (idx < 0) or (idx > High(p)) then begin result := -1; exit; end;
+  result := 0;
+  if (p[idx] = '1') or (CompareText(p[idx], 'on') = 0) or (CompareText(p[idx], 'true') = 0) or
+     (CompareText(p[idx], 'tan') = 0) or (CompareText(p[idx], 'yes') = 0) then result := 1
+  else if (CompareText(p[idx], 'toggle') = 0) or (CompareText(p[idx], 'switch') = 0) or
+          (CompareText(p[idx], 't') = 0) then result := 666;
+end;
+
+
+procedure boolVarHandler (me: PCommand; p: SArray);
+  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 conIsCheatsEnabled then flag := false else begin conwriteln('not available'); exit; end;
+         1: if conIsCheatsEnabled then flag := true else begin conwriteln('not available'); exit; end;
+         666: if conIsCheatsEnabled then flag := not flag else begin conwriteln('not available'); exit; end;
+      end;
+      if flag then conwritefln('%s: tan', [msg]) else conwritefln('%s: ona', [msg]);
+    end;
+  end;
+begin
+  binaryFlag(PBoolean(me.ptr)^, me.msg);
+end;
+
+
+procedure conRegVar (const conname: AnsiString; pvar: PBoolean; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
+var
+  f: Integer;
+  cp: PCommand;
+begin
+  f := Length(commands);
+  SetLength(commands, f+1);
+  cp := @commands[f];
+  cp.cmd := LowerCase(conname);
+  cp.proc := nil;
+  cp.procEx := boolVarHandler;
+  cp.help := ahelp;
+  cp.hidden := false;
+  cp.ptr := pvar;
+  cp.msg := amsg;
+  cp.cheat := acheat;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+type
+  PVarSingle = ^TVarSingle;
+  TVarSingle = record
+    val: PSingle;
+    min, max, def: Single; // default will be starting value
+  end;
+
+
+procedure singleVarHandler (me: PCommand; p: SArray);
+  // poor man's floating literal parser; i'm sorry, but `StrToFloat()` sux cocks
+  function parseFloat (var res: Single; const s: AnsiString): Boolean;
+  var
+    pos: Integer = 1;
+    frac: Single = 1;
+    slen: Integer;
+  begin
+    result := false;
+    res := 0;
+    slen := Length(s);
+    while (slen > 0) and (s[slen] <= ' ') do Dec(slen);
+    while (pos <= slen) and (s[pos] <= ' ') do Inc(pos);
+    if (pos > slen) then exit;
+    if (slen-pos = 1) and (s[pos] = '.') then exit; // single dot
+    // integral part
+    while (pos <= slen) do
+    begin
+      if (s[pos] < '0') or (s[pos] > '9') then break;
+      res := res*10+Byte(s[pos])-48;
+      Inc(pos);
+    end;
+    if (pos <= slen) then
+    begin
+      // must be a dot
+      if (s[pos] <> '.') then exit;
+      Inc(pos);
+      while (pos <= slen) do
+      begin
+        if (s[pos] < '0') or (s[pos] > '9') then break;
+        frac := frac/10;
+        res += frac*(Byte(s[pos])-48);
+        Inc(pos);
+      end;
+    end;
+    if (pos <= slen) then exit; // oops
+    result := true;
+  end;
+var
+  pv: PVarSingle;
+  nv: Single;
+  msg: AnsiString;
+begin
+  if (Length(p) > 2) then
+  begin
+    conwritefln('too many arguments to ''%s''', [p[0]]);
+    exit;
+  end;
+  pv := PVarSingle(me.ptr);
+  if (Length(p) = 2) then
+  begin
+    if not conIsCheatsEnabled then begin conwriteln('not available'); exit; end;
+    if (CompareText(p[1], 'default') = 0) or (CompareText(p[1], 'def') = 0) or
+       (CompareText(p[1], 'd') = 0) or (CompareText(p[1], 'off') = 0) or
+       (CompareText(p[1], 'ona') = 0) then
+    begin
+      pv.val^ := pv.def;
+    end
+    else
+    begin
+      if not parseFloat(nv, p[1]) then
+      begin
+        conwritefln('%s: ''%s'' doesn''t look like a floating number', [p[0], p[1]]);
+        exit;
+      end;
+      if (nv < pv.min) then nv := pv.min;
+      if (nv > pv.max) then nv := pv.max;
+      pv.val^ := nv;
+    end;
+  end;
+  msg := me.msg;
+  if (Length(msg) = 0) then msg := p[0] else msg += ':';
+  conwritefln('%s %s', [msg, pv.val^]);
+end;
+
+
+procedure conRegVar (const conname: AnsiString; pvar: PSingle; amin, amax: Single; const ahelp: AnsiString; const amsg: AnsiString; acheat: Boolean=false); overload;
+var
+  f: Integer;
+  cp: PCommand;
+  pv: PVarSingle;
+begin
+  GetMem(pv, sizeof(pv^));
+  pv.val := pvar;
+  pv.min := amin;
+  pv.max := amax;
+  pv.def := pvar^;
+  f := Length(commands);
+  SetLength(commands, f+1);
+  cp := @commands[f];
+  cp.cmd := LowerCase(conname);
+  cp.proc := nil;
+  cp.procEx := singleVarHandler;
+  cp.help := ahelp;
+  cp.hidden := false;
+  cp.ptr := pv;
+  cp.msg := amsg;
+  cp.cheat := acheat;
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
+function GetStrACmd(var Str: AnsiString): AnsiString;
 var
   a: Integer;
 begin
@@ -102,7 +294,7 @@ begin
     end;
 end;
 
-function ParseAlias(Str: String): SArray;
+function ParseAlias(Str: AnsiString): SArray;
 begin
   Result := nil;
 
@@ -118,16 +310,16 @@ begin
   end;
 end;
 
-procedure ConsoleCommands(P: SArray);
+procedure ConsoleCommands(p: SArray);
 var
-  Cmd, s: String;
+  cmd, s: AnsiString;
   a, b: Integer;
   F: TextFile;
 begin
-  Cmd := LowerCase(P[0]);
+  cmd := LowerCase(p[0]);
   s := '';
 
-  if Cmd = 'clear' then
+  if cmd = 'clear' then
   begin
     //ConsoleHistory := nil;
     cbufClear();
@@ -141,10 +333,10 @@ begin
       end;
   end;
 
-  if Cmd = 'clearhistory' then
+  if cmd = 'clearhistory' then
     CommandHistory := nil;
 
-  if Cmd = 'showhistory' then
+  if cmd = 'showhistory' then
     if CommandHistory <> nil then
     begin
       g_Console_Add('');
@@ -152,37 +344,46 @@ begin
         g_Console_Add('  '+CommandHistory[a]);
     end;
 
-  if Cmd = 'commands' then
+  if cmd = 'commands' then
   begin
     g_Console_Add('');
-    g_Console_Add('Commands list:');
-    for a := High(Commands) downto 0 do
-      g_Console_Add('  '+Commands[a].Cmd);
+    g_Console_Add('commands list:');
+    for a := High(commands) downto 0 do
+    begin
+      if (Length(commands[a].help) > 0) then
+      begin
+        g_Console_Add('  '+commands[a].cmd+' -- '+commands[a].help);
+      end
+      else
+      begin
+        g_Console_Add('  '+commands[a].cmd);
+      end;
+    end;
   end;
 
-  if Cmd = 'time' then
+  if cmd = 'time' then
     g_Console_Add(TimeToStr(Now), True);
 
-  if Cmd = 'date' then
+  if cmd = 'date' then
     g_Console_Add(DateToStr(Now), True);
 
-  if Cmd = 'echo' then
-    if Length(P) > 1 then
+  if cmd = 'echo' then
+    if Length(p) > 1 then
       begin
-        if P[1] = 'ololo' then
+        if p[1] = 'ololo' then
           gCheats := True
         else
         begin
           s := '';
-          for a := 1 to High(P) do
-            s := s + P[a] + ' ';
+          for a := 1 to High(p) do
+            s := s + p[a] + ' ';
           g_Console_Add(b_Text_Format(s), True);
         end;
       end
     else
       g_Console_Add('');
 
-  if Cmd = 'dump' then
+  if cmd = 'dump' then
   begin
     (*
     if ConsoleHistory <> nil then
@@ -212,12 +413,12 @@ begin
     *)
   end;
 
-  if Cmd = 'exec' then
+  if cmd = 'exec' then
   begin
     // exec <filename>
-    if Length(P) > 1 then
+    if Length(p) > 1 then
     begin
-      s := GameDir+'/'+P[1];
+      s := GameDir+'/'+p[1];
 
       {$I-}
       AssignFile(F, s);
@@ -262,56 +463,56 @@ begin
       g_Console_Add('exec <script file>');
   end;
 
-  if Cmd = 'alias' then
+  if cmd = 'alias' then
   begin
     // alias [alias_name] [commands]
-    if Length(P) > 1 then
+    if Length(p) > 1 then
     begin
       for a := 0 to High(Aliases) do
-        if Aliases[a].Name = P[1] then
+        if Aliases[a].name = p[1] then
         begin
-          if Length(P) > 2 then
-            Aliases[a].Commands := ParseAlias(P[2])
+          if Length(p) > 2 then
+            Aliases[a].commands := ParseAlias(p[2])
           else
-            for b := 0 to High(Aliases[a].Commands) do
-              g_Console_Add(Aliases[a].Commands[b]);
+            for b := 0 to High(Aliases[a].commands) do
+              g_Console_Add(Aliases[a].commands[b]);
           Exit;
         end;
       SetLength(Aliases, Length(Aliases)+1);
       a := High(Aliases);
-      Aliases[a].Name := P[1];
-      if Length(P) > 2 then
-        Aliases[a].Commands := ParseAlias(P[2])
+      Aliases[a].name := p[1];
+      if Length(p) > 2 then
+        Aliases[a].commands := ParseAlias(p[2])
       else
-        for b := 0 to High(Aliases[a].Commands) do
-          g_Console_Add(Aliases[a].Commands[b]);
+        for b := 0 to High(Aliases[a].commands) do
+          g_Console_Add(Aliases[a].commands[b]);
     end else
       for a := 0 to High(Aliases) do
-        if Aliases[a].Commands <> nil then
-          g_Console_Add(Aliases[a].Name);
+        if Aliases[a].commands <> nil then
+          g_Console_Add(Aliases[a].name);
   end;
 
-  if Cmd = 'call' then
+  if cmd = 'call' then
   begin
     // call <alias_name>
-    if Length(P) > 1 then
+    if Length(p) > 1 then
     begin
       if Aliases = nil then
         Exit;
       for a := 0 to High(Aliases) do
-        if Aliases[a].Name = P[1] then
+        if Aliases[a].name = p[1] then
         begin
-          if Aliases[a].Commands <> nil then
+          if Aliases[a].commands <> nil then
           begin
             // with this system proper endless loop detection seems either impossible
             // or very dirty to implement, so let's have this instead
             // prevents endless loops
-            for b := 0 to High(Aliases[a].Commands) do
+            for b := 0 to High(Aliases[a].commands) do
             begin
               Inc(RecursionDepth);
               RecursionLimitHit := (RecursionDepth > MaxScriptRecursion) or RecursionLimitHit;
               if not RecursionLimitHit then
-                g_Console_Process(Aliases[a].Commands[b], True);
+                g_Console_Process(Aliases[a].commands[b], True);
               Dec(RecursionDepth);
             end;
             if (RecursionDepth = 0) and RecursionLimitHit then
@@ -328,23 +529,31 @@ begin
   end;
 end;
 
-procedure WhitelistCommand(Cmd: string);
+procedure WhitelistCommand(cmd: AnsiString);
 var
   a: Integer;
 begin
   SetLength(Whitelist, Length(Whitelist)+1);
   a := High(Whitelist);
-  Whitelist[a] := Cmd;
+  Whitelist[a] := LowerCase(cmd);
 end;
 
-procedure AddCommand(Cmd: String; Proc: TCmdProc);
+procedure AddCommand(cmd: AnsiString; proc: TCmdProc; ahelp: AnsiString=''; ahidden: Boolean=false; acheat: Boolean=false);
 var
   a: Integer;
+  cp: PCommand;
 begin
-  SetLength(Commands, Length(Commands)+1);
-  a := High(Commands);
-  Commands[a].Cmd := Cmd;
-  Commands[a].Proc := Proc;
+  SetLength(commands, Length(commands)+1);
+  a := High(commands);
+  cp := @commands[a];
+  cp.cmd := LowerCase(cmd);
+  cp.proc := proc;
+  cp.procEx := nil;
+  cp.help := ahelp;
+  cp.hidden := ahidden;
+  cp.ptr := nil;
+  cp.msg := '';
+  cp.cheat := acheat;
 end;
 
 procedure g_Console_Init();
@@ -365,7 +574,7 @@ begin
       Time := 0;
     end;
 
-  AddCommand('clear', ConsoleCommands);
+  AddCommand('clear', ConsoleCommands, 'clear console');
   AddCommand('clearhistory', ConsoleCommands);
   AddCommand('showhistory', ConsoleCommands);
   AddCommand('commands', ConsoleCommands);
@@ -388,12 +597,6 @@ begin
   AddCommand('d_player', DebugCommands);
   AddCommand('d_joy', DebugCommands);
 
-  AddCommand('dpp', ProfilerCommands);
-  AddCommand('dpu', ProfilerCommands);
-  AddCommand('dpc', ProfilerCommands);
-  AddCommand('r_draw_grid', ProfilerCommands);
-  AddCommand('dbg_coldet_grid', ProfilerCommands);
-
   AddCommand('p1_name', GameCVars);
   AddCommand('p2_name', GameCVars);
   AddCommand('p1_color', GameCVars);
@@ -710,7 +913,7 @@ begin
   CPos := 1;
 end;
 
-procedure g_Console_Char(C: Char);
+procedure g_Console_Char(C: AnsiChar);
 begin
   if gChatShow and (not gChatEnter) then
     Exit;
@@ -718,39 +921,122 @@ begin
   CPos := CPos + 1;
 end;
 
-procedure Complete();
+
 var
-  i: Integer;
-  t: Array of String;
+  tcomplist: array of AnsiString = nil;
+  tcompidx: array of Integer = nil;
+
+procedure Complete ();
+var
+  i, c: Integer;
+  tused: Integer;
+  ll, lpfx, cmd: AnsiString;
 begin
-  if Line = '' then
-    Exit;
+  if (Length(Line) = 0) then
+  begin
+    g_Console_Add('');
+    for i := 0 to High(commands) do
+    begin
+      if not commands[i].hidden then
+      begin
+        if (Length(commands[i].help) > 0) then
+        begin
+          g_Console_Add('  '+commands[i].cmd+' -- '+commands[i].help);
+        end
+        else
+        begin
+          g_Console_Add('  '+commands[i].cmd);
+        end;
+      end;
+    end;
+    exit;
+  end;
 
-  t := nil;
+  ll := LowerCase(Line);
+  lpfx := '';
 
-  for i := 0 to High(Commands) do
-    if LowerCase(Line) = LowerCase(Copy(Commands[i].Cmd, 0, Length(Line))) then
+  if (Length(ll) > 1) and (ll[Length(ll)] = ' ') then
+  begin
+    ll := Copy(ll, 0, Length(ll)-1);
+    for i := 0 to High(commands) do
     begin
-      SetLength(t, Length(t) + 1);
-      t[Length(t)-1] := Commands[i].Cmd;
+      if commands[i].hidden then continue;
+      if (commands[i].cmd = ll) then
+      begin
+        if (Length(commands[i].help) > 0) then
+        begin
+          g_Console_Add('  '+commands[i].cmd+' -- '+commands[i].help);
+        end;
+      end;
     end;
+    exit;
+  end;
 
-  if t = nil then
-    Exit;
+  // build completion list
+  tused := 0;
+  for i := 0 to High(commands) do
+  begin
+    if commands[i].hidden then continue;
+    cmd := commands[i].cmd;
+    if (Length(cmd) >= Length(ll)) and (ll = Copy(cmd, 0, Length(ll))) then
+    begin
+      if (tused = Length(tcomplist)) then
+      begin
+        SetLength(tcomplist, Length(tcomplist)+128);
+        SetLength(tcompidx, Length(tcompidx)+128);
+      end;
+      tcomplist[tused] := cmd;
+      tcompidx[tused] := i;
+      Inc(tused);
+      if (Length(cmd) > Length(lpfx)) then lpfx := cmd;
+    end;
+  end;
 
-  if Length(t) = 1 then
+  // get longest prefix
+  for i := 0 to tused-1 do
+  begin
+    cmd := tcomplist[i];
+    for c := 1 to Length(lpfx) do
     begin
-      Line := t[0]+' ';
-      CPos := Length(Line)+1;
-    end
+      if (c > Length(cmd)) then break;
+      if (cmd[c] <> lpfx[c]) then begin lpfx := Copy(lpfx, 0, c-1); break; end;
+    end;
+  end;
+
+  if (tused = 0) then exit;
+
+  if (tused = 1) then
+  begin
+    Line := tcomplist[0]+' ';
+    CPos := Length(Line)+1;
+  end
   else
+  begin
+    // has longest prefix?
+    if (Length(lpfx) > Length(ll)) then
+    begin
+      Line := lpfx;
+      CPos:= Length(Line)+1;
+    end
+    else
     begin
       g_Console_Add('');
-      for i := 0 to High(t) do
-        g_Console_Add('  '+t[i]);
+      for i := 0 to tused-1 do
+      begin
+        if (Length(commands[tcompidx[i]].help) > 0) then
+        begin
+          g_Console_Add('  '+tcomplist[i]+' -- '+commands[tcompidx[i]].help);
+        end
+        else
+        begin
+          g_Console_Add('  '+tcomplist[i]);
+        end;
+      end;
     end;
+  end;
 end;
 
+
 procedure g_Console_Control(K: Word);
 begin
   case K of
@@ -783,7 +1069,7 @@ begin
               if g_Game_IsClient then
                 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_TEAM)
               else
-                MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
+                MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
                   NET_CHAT_TEAM, gPlayer1Settings.Team);
             end
             else
@@ -791,7 +1077,7 @@ begin
               if g_Game_IsClient then
                 MC_SEND_Chat(b_Text_Format(Line), NET_CHAT_PLAYER)
               else
-                MH_SEND_Chat('[' + gPlayer1Settings.Name + ']: ' + b_Text_Format(Line),
+                MH_SEND_Chat('[' + gPlayer1Settings.name + ']: ' + b_Text_Format(Line),
                 NET_CHAT_PLAYER);
             end;
           end;
@@ -836,7 +1122,7 @@ begin
   end;
 end;
 
-function GetStr(var Str: String): String;
+function GetStr(var Str: AnsiString): AnsiString;
 var
   a, b: Integer;
 begin
@@ -863,7 +1149,7 @@ begin
     end;
 end;
 
-function ParseString(Str: String): SArray;
+function ParseString(Str: AnsiString): SArray;
 begin
   Result := nil;
 
@@ -879,7 +1165,7 @@ begin
   end;
 end;
 
-procedure g_Console_Add (L: string; Show: Boolean=false);
+procedure g_Console_Add (L: AnsiString; show: Boolean=false);
 
   procedure conmsg (s: AnsiString);
   var
@@ -914,8 +1200,8 @@ begin
   if (length(L) = 0) or ((L[length(L)] <> #10) and (L[length(L)] <> #13)) then cbufPut(#10);
 
   // now show 'em out of console too
-  Show := Show and gAllowConsoleMessages;
-  if Show and gShowMessages then
+  show := show and gAllowConsoleMessages;
+  if show and gShowMessages then
   begin
     // Âûâîä ñòðîê ñ ïåðåíîñàìè ïî î÷åðåäè
     while length(L) > 0 do
@@ -937,6 +1223,58 @@ begin
   *)
 end;
 
+
+var
+  consolewriterLastWasEOL: Boolean = false;
+
+procedure consolewriter (constref buf; len: SizeUInt);
+var
+  b: PByte;
+begin
+  if (len < 1) then exit;
+  b := PByte(@buf);
+  consolewriterLastWasEOL := (b[len-1] = 13) or (b[len-1] = 10);
+  while (len > 0) do
+  begin
+    if (b[0] <> 13) and (b[0] <> 10) then
+    begin
+      cbufPut(AnsiChar(b[0]));
+    end
+    else
+    begin
+      if (len > 1) and (b[0] = 13) then begin len -= 1; b += 1; end;
+      cbufPut(#10);
+    end;
+    len -= 1;
+    b += 1;
+  end;
+end;
+
+
+// returns formatted string if `writerCB` is `nil`, empty string otherwise
+//function formatstrf (const fmt: AnsiString; args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
+//TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
+procedure conwriteln (const s: AnsiString; show: Boolean=false);
+begin
+  g_Console_Add(s, show);
+end;
+
+
+procedure conwritefln (const s: AnsiString; args: array of const; show: Boolean=false);
+begin
+  if show then
+  begin
+    g_Console_Add(formatstrf(s, args), true);
+  end
+  else
+  begin
+    consolewriterLastWasEOL := false;
+    formatstrf(s, args, consolewriter);
+    if not consolewriterLastWasEOL then cbufPut(#10);
+  end;
+end;
+
+
 procedure g_Console_Clear();
 begin
   //ConsoleHistory := nil;
@@ -944,7 +1282,7 @@ begin
   conSkipLines := 0;
 end;
 
-procedure AddToHistory(L: String);
+procedure AddToHistory(L: AnsiString);
 var
   len: Integer;
 begin
@@ -960,7 +1298,7 @@ begin
   CmdIndex := Length(CommandHistory);
 end;
 
-function g_Console_CommandBlacklisted(C: String): Boolean;
+function g_Console_CommandBlacklisted(C: AnsiString): Boolean;
 var
   Arr: SArray;
   i: Integer;
@@ -981,7 +1319,7 @@ begin
       Result := False;
 end;
 
-procedure g_Console_Process(L: String; Quiet: Boolean = False);
+procedure g_Console_Process(L: AnsiString; quiet: Boolean = False);
 var
   Arr: SArray;
   i: Integer;
@@ -1002,7 +1340,7 @@ begin
     exit;
   end;
 
-  if not Quiet then
+  if not quiet then
   begin
     g_Console_Add('> '+L);
     Line := '';
@@ -1013,19 +1351,28 @@ begin
   if Arr = nil then
     Exit;
 
-  if Commands = nil then
+  if commands = nil then
     Exit;
 
-  if not Quiet then
+  if not quiet then
     AddToHistory(L);
 
-  for i := 0 to High(Commands) do
-    if Commands[i].Cmd = LowerCase(Arr[0]) then
-      if @Commands[i].Proc <> nil then
+  for i := 0 to High(commands) do
+  begin
+    if commands[i].cmd = LowerCase(Arr[0]) then
+    begin
+      if assigned(commands[i].procEx) then
       begin
-        Commands[i].Proc(Arr);
-        Exit;
+        commands[i].procEx(@commands[i], Arr);
+        exit;
+      end;
+      if assigned(commands[i].proc) then
+      begin
+        commands[i].proc(Arr);
+        exit;
       end;
+    end;
+  end;
 
   g_Console_Add(Format(_lc[I_CONSOLE_UNKNOWN], [Arr[0]]));
 end;