Permalink
Browse files

Make console history persistent

The console history is saved to a file on quit and restored after
starting the game.
So the commands entered in the console the last times the game was run
are still available in the console-history, just like commands that
were just entered.
  • Loading branch information...
DanielGibson committed Sep 16, 2012
1 parent 8c6e4eb commit 04266eee5e8341085f819584660b5110f02c1af0
Showing with 44 additions and 0 deletions.
  1. +6 −0 neo/framework/Common.cpp
  2. +35 −0 neo/framework/Console.cpp
  3. +3 −0 neo/framework/Console.h
View
@@ -2853,6 +2853,9 @@ void idCommonLocal::Init( int argc, char **argv ) {
ClearCommandLine();
+ // load the persistent console history
+ console->LoadHistory();
+
com_fullyInitialized = true;
}
@@ -2881,6 +2884,9 @@ void idCommonLocal::Shutdown( void ) {
idAsyncNetwork::server.Kill();
idAsyncNetwork::client.Shutdown();
+ // save persistent console history
+ console->SaveHistory();
+
// game specific shut down
ShutdownGame( false );
View
@@ -70,6 +70,9 @@ class idConsoleLocal : public idConsole {
void Dump( const char *toFile );
void Clear();
+ virtual void SaveHistory();
+ virtual void LoadHistory();
+
//============================
const idMaterial * charSetShader;
@@ -516,6 +519,38 @@ void idConsoleLocal::Dump( const char *fileName ) {
fileSystem->CloseFile( f );
}
+void idConsoleLocal::SaveHistory() {
+ idFile *f = fileSystem->OpenFileWrite( "consolehistory.dat" );
+ for ( int i=0; i < COMMAND_HISTORY; ++i ) {
+ // make sure the history is in the right order
+ int line = (nextHistoryLine + i) % COMMAND_HISTORY;
+ const char *s = historyEditLines[line].GetBuffer();
+ if ( s && s[0] ) {
+ f->WriteString(s);
+ }
+ }
+ fileSystem->CloseFile(f);
+}
+
+void idConsoleLocal::LoadHistory() {
+ idFile *f = fileSystem->OpenFileRead( "consolehistory.dat" );
+ if ( f == NULL ) // file doesn't exist
+ return;
+
+ historyLine = 0;
+ idStr tmp;
+ for ( int i=0; i < COMMAND_HISTORY; ++i ) {
+ if ( f->Tell() >= f->Length() ) {
+ break; // EOF is reached
+ }
+ f->ReadString(tmp);
+ historyEditLines[i].SetBuffer(tmp.c_str());
+ ++historyLine;
+ }
+ nextHistoryLine = historyLine;
+ fileSystem->CloseFile(f);
+}
+
/*
================
idConsoleLocal::PageUp
View
@@ -67,6 +67,9 @@ class idConsole {
virtual void Draw( bool forceFullScreen ) = 0;
virtual void Print( const char *text ) = 0;
+
+ virtual void SaveHistory() = 0;
+ virtual void LoadHistory() = 0;
};
extern idConsole * console; // statically initialized to an idConsoleLocal

0 comments on commit 04266ee

Please sign in to comment.