[Scummvm-git-logs] scummvm master -> 4b13314e23e68377de2c85df537c84a903407f3d

mgerhardy martin.gerhardy at gmail.com
Wed Sep 1 14:30:06 UTC 2021


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
4b13314e23 GUI: persist the debug console history


Commit: 4b13314e23e68377de2c85df537c84a903407f3d
    https://github.com/scummvm/scummvm/commit/4b13314e23e68377de2c85df537c84a903407f3d
Author: Martin Gerhardy (martin.gerhardy at gmail.com)
Date: 2021-09-01T16:30:02+02:00

Commit Message:
GUI: persist the debug console history

Changed paths:
    gui/console.cpp
    gui/console.h


diff --git a/gui/console.cpp b/gui/console.cpp
index 48540f27fd..9a1c0143a7 100644
--- a/gui/console.cpp
+++ b/gui/console.cpp
@@ -21,6 +21,7 @@
  */
 
 #include "gui/console.h"
+#include "common/savefile.h"
 #include "gui/widgets/scrollbar.h"
 #include "gui/ThemeEval.h"
 #include "gui/gui-manager.h"
@@ -36,6 +37,8 @@ namespace GUI {
 #define kConsoleCharWidth  (_font->getCharWidth('M'))
 #define kConsoleLineHeight (_font->getFontHeight())
 
+#define HISTORY_FILENAME "scummvm-history.txt"
+
 enum {
 	kConsoleSlideDownDuration = 200	// Time in milliseconds
 };
@@ -89,6 +92,10 @@ ConsoleDialog::ConsoleDialog(float widthPercent, float heightPercent)
 	print("\nConsole is ready\n");
 }
 
+ConsoleDialog::~ConsoleDialog() {
+	saveHistory();
+}
+
 void ConsoleDialog::init() {
 	const int screenW = g_system->getOverlayWidth();
 	const int screenH = g_system->getOverlayHeight();
@@ -156,6 +163,10 @@ void ConsoleDialog::open() {
 		print(PROMPT);
 		_promptStartPos = _promptEndPos = _currentPos;
 	}
+
+	if (_historySize == 0) {
+		loadHistory();
+	}
 }
 
 void ConsoleDialog::close() {
@@ -582,6 +593,43 @@ void ConsoleDialog::killLastWord() {
 	}
 }
 
+void ConsoleDialog::loadHistory() {
+	Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
+	Common::InSaveFile *loadFile = saveFileMan->openRawFile(HISTORY_FILENAME);
+	if (!loadFile) {
+		return;
+	}
+	for (int i = 0; i < kHistorySize; ++i) {
+		const Common::String &line = loadFile->readLine();
+		if (line.empty()) {
+			break;
+		}
+		addToHistory(line);
+	}
+	delete loadFile;
+	debug("Read %i history entries", _historySize);
+}
+
+void ConsoleDialog::saveHistory() {
+	if (_historySize == 0) {
+		return;
+	}
+	Common::SaveFileManager *saveFileMan = g_system->getSavefileManager();
+	Common::WriteStream *saveFile = saveFileMan->openForSaving(HISTORY_FILENAME, false);
+	if (!saveFile) {
+		warning("Failed to open " HISTORY_FILENAME " for writing");
+		return;
+	}
+
+	for (int i = 0; i < _historySize; ++i) {
+		saveFile->writeString(_history[i]);
+		saveFile->writeByte('\n');
+	}
+	saveFile->finalize();
+	delete saveFile;
+	debug("Wrote %i history entries", _historySize);
+}
+
 void ConsoleDialog::addToHistory(const Common::String &str) {
 	_history[_historyIndex] = str;
 	_historyIndex = (_historyIndex + 1) % kHistorySize;
diff --git a/gui/console.h b/gui/console.h
index 8a9659c1e9..6d9a19db91 100644
--- a/gui/console.h
+++ b/gui/console.h
@@ -118,6 +118,9 @@ protected:
 	int _historyIndex;
 	int _historyLine;
 
+	void loadHistory();
+	void saveHistory();
+
 	float _widthPercent, _heightPercent;
 
 	int _leftPadding;
@@ -129,6 +132,7 @@ protected:
 
 public:
 	ConsoleDialog(float widthPercent, float heightPercent);
+	virtual ~ConsoleDialog();
 
 	void open() override;
 	void close() override;




More information about the Scummvm-git-logs mailing list