(svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
authorrubidium
Wed, 17 Oct 2007 20:09:16 +0000
changeset 7750 7afbef1235e0
parent 7749 3ed7d92bd966
child 7751 d4042cd04555
(svn r11287) -Codechange: add support for pushing paramaters into NewIndustries' error messages. Patch by Csaboka.
src/industry_gui.cpp
src/misc_gui.cpp
src/newgrf_industries.cpp
src/newgrf_industrytiles.cpp
src/newgrf_text.cpp
src/newgrf_text.h
--- a/src/industry_gui.cpp	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/industry_gui.cpp	Wed Oct 17 20:09:16 2007 +0000
@@ -503,7 +503,7 @@
 				if (message != STR_NULL && message != STR_UNDEFINED) {
 					y += 10;
 
-					PrepareTextRefStackUsage();
+					PrepareTextRefStackUsage(6);
 					DrawString(2, y, message, 0);
 					StopTextRefStackUsage();
 				}
--- a/src/misc_gui.cpp	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/misc_gui.cpp	Wed Oct 17 20:09:16 2007 +0000
@@ -9,6 +9,7 @@
 #include "functions.h"
 #include "landscape.h"
 #include "newgrf.h"
+#include "newgrf_text.h"
 #include "saveload.h"
 #include "strings.h"
 #include "table/sprites.h"
@@ -505,6 +506,13 @@
 		CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
 		DrawWindowWidgets(w);
 		CopyInDParam(0, _errmsg_decode_params, lengthof(_errmsg_decode_params));
+
+		/* If the error message comes from a NewGRF, we must use the text ref. stack reserved for error messages.
+		 * If the message doesn't come from a NewGRF, it won't use the TTDP-style text ref. stack, so we won't hurt anything
+		 */
+		SwitchToErrorRefStack();
+		RewindTextRefStack();
+
 		if (!IsWindowOfPrototype(w, _errmsg_face_widgets)) {
 			DrawStringMultiCenter(
 				120,
@@ -533,6 +541,9 @@
 					_errmsg_message_1,
 					w->width - 2);
 		}
+
+		/* Switch back to the normal text ref. stack for NewGRF texts */
+		SwitchToNormalRefStack();
 		break;
 
 	case WE_MOUSELOOP:
--- a/src/newgrf_industries.cpp	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/newgrf_industries.cpp	Wed Oct 17 20:09:16 2007 +0000
@@ -458,6 +458,11 @@
 	 * the building of the industry, as that's how it's done in TTDP. */
 	if (group == NULL || group->type != SGT_CALLBACK) return true;
 
+	/* Copy some parameters from the registers to the error message text ref. stack */
+	SwitchToErrorRefStack();
+	PrepareTextRefStackUsage(4);
+	SwitchToNormalRefStack();
+
 	switch (group->g.callback.result) {
 		case 0x400: return true;
 		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE; break;
--- a/src/newgrf_industrytiles.cpp	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/newgrf_industrytiles.cpp	Wed Oct 17 20:09:16 2007 +0000
@@ -265,6 +265,11 @@
 		return callback_res != 0;
 	}
 
+	/* Copy some parameters from the registers to the error message text ref. stack */
+	SwitchToErrorRefStack();
+	PrepareTextRefStackUsage(4);
+	SwitchToNormalRefStack();
+
 	switch (callback_res) {
 		case 0x400: return true;
 		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE;                 return false;
--- a/src/newgrf_text.cpp	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/newgrf_text.cpp	Wed Oct 17 20:09:16 2007 +0000
@@ -538,21 +538,28 @@
 		this->stack[this->position + 1] = GB(word, 8, 8);
 	}
 
-	void ResetStack() { this->used = true; this->position = 0; }
+	void ResetStack()  { this->position = 0; this->used = true; }
+	void RewindStack() { this->position = 0; }
 };
 
+static TextRefStack _newgrf_normal_textrefstack;
+static TextRefStack _newgrf_error_textrefstack;
+
 /** The stack that is used for TTDP compatible string code parsing */
-static TextRefStack _newgrf_textrefstack;
+static TextRefStack *_newgrf_textrefstack = &_newgrf_normal_textrefstack;
 
-/** Prepare the TTDP compatible string code parsing */
-void PrepareTextRefStackUsage()
+/**
+ * Prepare the TTDP compatible string code parsing
+ * @param numEntries number of entries to copy from the registers
+ */
+void PrepareTextRefStackUsage(byte numEntries)
 {
 	extern TemporaryStorageArray<uint32, 0x110> _temp_store;
 
-	_newgrf_textrefstack.ResetStack();
+	_newgrf_textrefstack->ResetStack();
 
-	byte *p = _newgrf_textrefstack.stack;
-	for (uint i = 0; i < 6; i++) {
+	byte *p = _newgrf_textrefstack->stack;
+	for (uint i = 0; i < numEntries; i++) {
 		for (uint j = 0; j < 32; j += 8) {
 			*p = GB(_temp_store.Get(0x100 + i), j, 8);
 			p++;
@@ -561,7 +568,22 @@
 }
 
 /** Stop using the TTDP compatible string code parsing */
-void StopTextRefStackUsage() { _newgrf_textrefstack.used = false; }
+void StopTextRefStackUsage() { _newgrf_textrefstack->used = false; }
+
+void SwitchToNormalRefStack()
+{
+	_newgrf_textrefstack = &_newgrf_normal_textrefstack;
+}
+
+void SwitchToErrorRefStack()
+{
+	_newgrf_textrefstack = &_newgrf_error_textrefstack;
+}
+
+void RewindTextRefStack()
+{
+	_newgrf_textrefstack->RewindStack();
+}
 
 /**
  * FormatString for NewGRF specific "magic" string control codes
@@ -571,31 +593,31 @@
  */
 uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv)
 {
-	if (_newgrf_textrefstack.used) {
+	if (_newgrf_textrefstack->used) {
 		switch (scc) {
 			default: NOT_REACHED();
-			case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack.PopSignedByte();    break;
-			case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack.PopSignedWord();    break;
-			case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack.PopUnsignedQWord(); break;
+			case SCC_NEWGRF_PRINT_SIGNED_BYTE:    *argv = _newgrf_textrefstack->PopSignedByte();    break;
+			case SCC_NEWGRF_PRINT_SIGNED_WORD:    *argv = _newgrf_textrefstack->PopSignedWord();    break;
+			case SCC_NEWGRF_PRINT_QWORD_CURRENCY: *argv = _newgrf_textrefstack->PopUnsignedQWord(); break;
 
 			case SCC_NEWGRF_PRINT_DWORD_CURRENCY:
-			case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack.PopSignedDWord();   break;
+			case SCC_NEWGRF_PRINT_DWORD:          *argv = _newgrf_textrefstack->PopSignedDWord();   break;
 
 			case SCC_NEWGRF_PRINT_WORD_SPEED:
 			case SCC_NEWGRF_PRINT_WORD_LITRES:
-			case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack.PopUnsignedWord(); break;
+			case SCC_NEWGRF_PRINT_UNSIGNED_WORD:  *argv = _newgrf_textrefstack->PopUnsignedWord();  break;
 
 			case SCC_NEWGRF_PRINT_DATE:
-			case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack.PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
+			case SCC_NEWGRF_PRINT_MONTH_YEAR:     *argv = _newgrf_textrefstack->PopSignedWord() + DAYS_TILL_ORIGINAL_BASE_YEAR; break;
 
-			case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack.PopUnsignedWord(); break;
+			case SCC_NEWGRF_DISCARD_WORD:         _newgrf_textrefstack->PopUnsignedWord(); break;
 
-			case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack.RotateTop4Words(); break;
-			case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack.PushWord(Utf8Consume(str)); break;
+			case SCC_NEWGRF_ROTATE_TOP_4_WORDS:   _newgrf_textrefstack->RotateTop4Words(); break;
+			case SCC_NEWGRF_PUSH_WORD:            _newgrf_textrefstack->PushWord(Utf8Consume(str)); break;
 			case SCC_NEWGRF_UNPRINT:              *buff -= Utf8Consume(str); break;
 
 			case SCC_NEWGRF_PRINT_STRING_ID:
-				*argv = _newgrf_textrefstack.PopUnsignedWord();
+				*argv = _newgrf_textrefstack->PopUnsignedWord();
 				if (*argv == STR_NULL) *argv = STR_EMPTY;
 				break;
 		}
--- a/src/newgrf_text.h	Wed Oct 17 16:09:04 2007 +0000
+++ b/src/newgrf_text.h	Wed Oct 17 20:09:16 2007 +0000
@@ -15,8 +15,11 @@
 
 bool CheckGrfLangID(byte lang_id, byte grf_version);
 
-void PrepareTextRefStackUsage();
+void PrepareTextRefStackUsage(byte numEntries);
 void StopTextRefStackUsage();
+void SwitchToNormalRefStack();
+void SwitchToErrorRefStack();
+void RewindTextRefStack();
 uint RemapNewGRFStringControlCode(uint scc, char **buff, const char **str, int64 *argv);
 
 #endif /* NEWGRF_TEXT_H */