changeset 20664:66ffc94f0bd4 draft

(svn r25620) -Fix (r25342): Save/load of story books were broken
author zuu <zuu@openttd.org>
date Sun, 21 Jul 2013 13:18:45 +0000
parents 3de7c92c392b
children ca1a82e80f09
files src/saveload/afterload.cpp src/saveload/saveload.cpp src/saveload/saveload_internal.h src/saveload/story_sl.cpp src/story.cpp src/story_base.h
diffstat 6 files changed, 45 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -2818,6 +2818,7 @@
 	AfterLoadRoadStops();
 	AfterLoadLabelMaps();
 	AfterLoadCompanyStats();
+	AfterLoadStoryBook();
 
 	GamelogPrintDebug(1);
 
--- a/src/saveload/saveload.cpp
+++ b/src/saveload/saveload.cpp
@@ -250,8 +250,9 @@
  *  182   25296
  *  183   25363
  *  184   25508
+ *  185   25620
  */
-extern const uint16 SAVEGAME_VERSION = 184; ///< Current savegame version of OpenTTD.
+extern const uint16 SAVEGAME_VERSION = 185; ///< Current savegame version of OpenTTD.
 
 SavegameType _savegame_type; ///< type of savegame we are loading
 
--- a/src/saveload/saveload_internal.h
+++ b/src/saveload/saveload_internal.h
@@ -31,6 +31,7 @@
 void AfterLoadStations();
 void AfterLoadRoadStops();
 void AfterLoadLabelMaps();
+void AfterLoadStoryBook();
 void AfterLoadLinkGraphs();
 void AfterLoadCompanyStats();
 void UpdateHousesAndTowns();
--- a/src/saveload/story_sl.cpp
+++ b/src/saveload/story_sl.cpp
@@ -14,10 +14,24 @@
 
 #include "saveload.h"
 
+/** Called after load to trash broken pages. */
+void AfterLoadStoryBook()
+{
+	if (IsSavegameVersionBefore(185)) {
+		/* Trash all story pages and page elements because
+		 * they were saved with wrong data types.
+		 */
+		_story_page_element_pool.CleanPool();
+		_story_page_pool.CleanPool();
+	}
+}
+
 static const SaveLoad _story_page_elements_desc[] = {
-	    SLE_VAR(StoryPageElement, sort_value,    SLE_UINT16),
+	SLE_CONDVAR(StoryPageElement, sort_value,    SLE_FILE_U16 | SLE_VAR_U32, 0,   184),
+	SLE_CONDVAR(StoryPageElement, sort_value,    SLE_UINT32,                 185, SL_MAX_VERSION),
 	    SLE_VAR(StoryPageElement, page,          SLE_UINT16),
-	    SLE_VAR(StoryPageElement, type,          SLE_UINT16),
+	SLE_CONDVAR(StoryPageElement, type,          SLE_FILE_U16 | SLE_VAR_U8,  0,   184),
+	SLE_CONDVAR(StoryPageElement, type,          SLE_UINT8,                  185, SL_MAX_VERSION),
 	    SLE_VAR(StoryPageElement, referenced_id, SLE_UINT32),
 	    SLE_STR(StoryPageElement, text,          SLE_STR | SLF_ALLOW_CONTROL, 0),
 	    SLE_END()
@@ -50,9 +64,11 @@
 }
 
 static const SaveLoad _story_pages_desc[] = {
-	    SLE_VAR(StoryPage, sort_value, SLE_UINT16),
+	SLE_CONDVAR(StoryPage, sort_value, SLE_FILE_U16 | SLE_VAR_U32, 0,   184),
+	SLE_CONDVAR(StoryPage, sort_value, SLE_UINT32,                 185, SL_MAX_VERSION),
 	    SLE_VAR(StoryPage, date,       SLE_UINT32),
-	    SLE_VAR(StoryPage, company,    SLE_UINT16),
+	SLE_CONDVAR(StoryPage, company,    SLE_FILE_U16 | SLE_VAR_U8,  0,   184),
+	SLE_CONDVAR(StoryPage, company,    SLE_UINT8,                  185, SL_MAX_VERSION),
 	    SLE_STR(StoryPage, title,      SLE_STR | SLF_ALLOW_CONTROL, 0),
 	    SLE_END()
 };
--- a/src/story.cpp
+++ b/src/story.cpp
@@ -12,6 +12,7 @@
 #include "stdafx.h"
 #include "story_base.h"
 #include "core/pool_func.hpp"
+#include "cmd_helper.h"
 #include "command_func.h"
 #include "company_base.h"
 #include "company_func.h"
@@ -44,7 +45,7 @@
  * @param text The text parameter of the DoCommand proc
  * @return true, if and only if the given parameters are valid for the given page elment type and page id.
  */
-static bool VerifyElementContentParameters(uint32 page_id, StoryPageElementType type, TileIndex tile, uint32 reference, const char *text)
+static bool VerifyElementContentParameters(StoryPageID page_id, StoryPageElementType type, TileIndex tile, uint32 reference, const char *text)
 {
 	switch (type) {
 		case SPET_TEXT:
@@ -140,7 +141,7 @@
  * @param flags type of operation
  * @param p1 various bitstuffed elements
  * - p1 = (bit  0 -  15) - The page which the element belongs to.
- *        (bit  16 -  31) - Page element type
+ *        (bit  16 -  23) - Page element type
  * @param p2 Id of referenced object
  * @param text Text content in case it is a text or location page element
  * @return the cost of this operation or an error
@@ -150,7 +151,7 @@
 	if (!StoryPageElement::CanAllocateItem()) return CMD_ERROR;
 
 	StoryPageID page_id = (CompanyID)GB(p1, 0, 16);
-	StoryPageElementType type = (StoryPageElementType) GB(p1, 16, 16);
+	StoryPageElementType type = Extract<StoryPageElementType, 16, 8>(p1);
 
 	/* Allow at most 128 elements per page. */
 	uint16 element_count = 0;
@@ -221,7 +222,7 @@
  * Update title of a story page.
  * @param tile unused.
  * @param flags type of operation
- * @param p1 StoryPageID to update.
+ * @param p1 = (bit 0 - 15) - StoryPageID to update.
  * @param p2 unused
  * @param text title text of the story page.
  * @return the cost of this operation or an error
@@ -229,7 +230,7 @@
 CommandCost CmdSetStoryPageTitle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
 	if (_current_company != OWNER_DEITY) return CMD_ERROR;
-	StoryPageID page_id = (StoryPageID)p1;
+	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
 	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
@@ -252,7 +253,7 @@
  * view the story page.
  * @param tile unused.
  * @param flags type of operation
- * @param p1 StoryPageID to show.
+ * @param p1 = (bit 0 - 15) - StoryPageID to show.
  * @param p2 unused
  * @param text unused
  * @return the cost of this operation or an error
@@ -260,11 +261,12 @@
 CommandCost CmdShowStoryPage(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 {
 	if (_current_company != OWNER_DEITY) return CMD_ERROR;
-	if (!StoryPage::IsValidID(p1)) return CMD_ERROR;
+	StoryPageID page_id = (StoryPageID)GB(p1, 0, 16);
+	if (!StoryPage::IsValidID(page_id)) return CMD_ERROR;
 
 	if (flags & DC_EXEC) {
-		StoryPage *g = StoryPage::Get(p1);
-		if ((g->company != INVALID_COMPANY && g->company == _local_company) || (g->company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, p1);
+		StoryPage *g = StoryPage::Get(page_id);
+		if ((g->company != INVALID_COMPANY && g->company == _local_company) || (g->company == INVALID_COMPANY && Company::IsValidID(_local_company))) ShowStoryBook(_local_company, page_id);
 	}
 
 	return CommandCost();
@@ -273,7 +275,7 @@
  * Remove a story page and associated story page elements.
  * @param tile unused.
  * @param flags type of operation
- * @param p1 StoryPageID to remove.
+ * @param p1 = (bit 0 - 15) - StoryPageID to remove.
  * @param p2 unused.
  * @param text unused.
  * @return the cost of this operation or an error
--- a/src/story_base.h
+++ b/src/story_base.h
@@ -28,11 +28,17 @@
  * Each story page element is one of these types.
  */
 enum StoryPageElementType {
-	SPET_TEXT,     ///< A text element.
+	SPET_TEXT = 0, ///< A text element.
 	SPET_LOCATION, ///< An element that references a tile along with a one-line text.
 	SPET_GOAL,     ///< An element that references a goal.
+	SPET_END,
+	INVALID_SPET = 0xFF,
 };
 
+/** Define basic enum properties */
+template <> struct EnumPropsT<StoryPageElementType> : MakeEnumPropsT<StoryPageElementType, byte, SPET_TEXT, SPET_END, INVALID_SPET, 8> {};
+typedef TinyEnumT<StoryPageElementType> StoryPageElementTypeByte; ///< typedefing-enumification of Direction
+
 /**
  * Struct about story page elements.
  * Each StoryPage is composed of one or more page elements that provide
@@ -40,8 +46,8 @@
  **/
 struct StoryPageElement : StoryPageElementPool::PoolItem<&_story_page_element_pool> {
 	uint32 sort_value;   ///< A number that increases for every created story page element. Used for sorting. The id of a story page element is the pool index.
-	uint16 page; ///< Id of the page which the page element belongs to
-	StoryPageElementType type; ///< Type of page element
+	StoryPageID page; ///< Id of the page which the page element belongs to
+	StoryPageElementTypeByte type; ///< Type of page element
 
 	uint32 referenced_id; ///< Id of referenced object (location, goal etc.)
 	char *text;           ///< Static content text of page element