opt-in command history saving with con_saveCmdHistory

This commit is contained in:
myT 2017-03-07 22:44:03 +01:00
parent 14f037f1f9
commit 82994d21f8
4 changed files with 97 additions and 0 deletions

View file

@ -288,6 +288,8 @@ void CL_ConInit()
}
Con_ClearNotify();
CL_LoadCommandHistory();
Cmd_AddCommand( "toggleconsole", Con_ToggleConsole_f );
Cmd_AddCommand( "messagemode", Con_MessageMode_f );
Cmd_AddCommand( "messagemode2", Con_MessageMode2_f );
@ -642,6 +644,7 @@ static void Con_FixPosition()
}
}
void Con_ScrollLines( int lines )
{
if (lines == 0)
@ -651,6 +654,7 @@ void Con_ScrollLines( int lines )
Con_FixPosition();
}
void Con_ScrollPages( int pages )
{
if (pages == 0)
@ -661,6 +665,7 @@ void Con_ScrollPages( int pages )
Con_FixPosition();
}
void Con_Top()
{
con.display = con.totallines;
@ -669,6 +674,7 @@ void Con_Top()
}
}
void Con_Bottom()
{
con.display = con.current;

View file

@ -1140,3 +1140,89 @@ void Key_ClearStates()
anykeydown = 0;
}
#define HISTORY_PATH "cnq3cmdhistory"
static const cvar_t* con_saveCmdHistory;
void CL_LoadCommandHistory()
{
con_saveCmdHistory = Cvar_Get( "con_saveCmdHistory", "0", CVAR_ARCHIVE );
fileHandle_t f;
FS_FOpenFileRead( HISTORY_PATH, &f, qfalse );
if ( f == NULL )
return;
int count;
if ( FS_Read( &count, sizeof(int), f ) != sizeof(int) ||
count <= 0 ||
count > COMMAND_HISTORY ) {
FS_FCloseFile( f );
return;
}
int lengths[COMMAND_HISTORY];
const int lengthBytes = sizeof(int) * count;
if ( FS_Read( lengths, lengthBytes, f ) != lengthBytes ) {
FS_FCloseFile( f );
return;
}
for ( int i = 0; i < count; ++i ) {
const int l = lengths[i];
if ( l <= 0 ||
FS_Read( historyEditLines[i].buffer, l, f ) != l ) {
FS_FCloseFile( f );
return;
}
historyEditLines[i].buffer[l] = '\0';
historyEditLines[i].cursor = l;
}
nextHistoryLine = count;
historyLine = count;
const int totalCount = ARRAY_LEN( historyEditLines );
for ( int i = count; i < totalCount; ++i ) {
historyEditLines[i].buffer[0] = '\0';
}
FS_FCloseFile(f);
}
void CL_SaveCommandHistory()
{
if ( con_saveCmdHistory->integer == 0 )
return;
const fileHandle_t f = FS_FOpenFileWrite( HISTORY_PATH );
if ( f == NULL )
return;
int count = 0;
int lengths[COMMAND_HISTORY];
const int totalCount = ARRAY_LEN( historyEditLines );
for ( int i = 0; i < totalCount; ++i ) {
const char* const s = historyEditLines[(historyLine + i) % COMMAND_HISTORY].buffer;
if ( *s == '\0' )
continue;
lengths[count++] = strlen( s );
}
FS_Write( &count, sizeof(count), f );
FS_Write( lengths, sizeof(int) * count, f );
for ( int i = 0, j = 0; i < totalCount; ++i ) {
const char* const s = historyEditLines[(historyLine + i) % COMMAND_HISTORY].buffer;
if ( *s == '\0' )
continue;
FS_Write( s, lengths[j++], f );
}
FS_FCloseFile( f );
}

View file

@ -2114,6 +2114,8 @@ void CL_Shutdown()
CL_ShutdownUI();
CL_SaveCommandHistory();
Cmd_RemoveCommand ("cmd");
Cmd_RemoveCommand ("configstrings");
Cmd_RemoveCommand ("userinfo");

View file

@ -41,3 +41,6 @@ qbool Key_GetOverstrikeMode();
void Key_SetOverstrikeMode( qbool state );
void Key_ClearStates();
int Key_GetKey( const char* binding );
void CL_LoadCommandHistory();
void CL_SaveCommandHistory();