Browse Source

Initialise and shutdown sprite sheet property parser. This fixes a crash when re-initialising RmlUi after it has previously been shut down.

Michael Ragazzon 5 years ago
parent
commit
13d1c5b064
3 changed files with 26 additions and 11 deletions
  1. 3 0
      Source/Core/Core.cpp
  2. 18 11
      Source/Core/StyleSheetParser.cpp
  3. 5 0
      Source/Core/StyleSheetParser.h

+ 3 - 0
Source/Core/Core.cpp

@@ -42,6 +42,7 @@
 #include "GeometryDatabase.h"
 #include "GeometryDatabase.h"
 #include "PluginRegistry.h"
 #include "PluginRegistry.h"
 #include "StyleSheetFactory.h"
 #include "StyleSheetFactory.h"
+#include "StyleSheetParser.h"
 #include "TemplateCache.h"
 #include "TemplateCache.h"
 #include "TextureDatabase.h"
 #include "TextureDatabase.h"
 #include "EventSpecification.h"
 #include "EventSpecification.h"
@@ -116,6 +117,7 @@ bool Initialise()
 	}
 	}
 
 
 	StyleSheetSpecification::Initialise();
 	StyleSheetSpecification::Initialise();
+	StyleSheetParser::Initialise();
 	StyleSheetFactory::Initialise();
 	StyleSheetFactory::Initialise();
 
 
 	TemplateCache::Initialise();
 	TemplateCache::Initialise();
@@ -143,6 +145,7 @@ void Shutdown()
 	Factory::Shutdown();
 	Factory::Shutdown();
 	TemplateCache::Shutdown();
 	TemplateCache::Shutdown();
 	StyleSheetFactory::Shutdown();
 	StyleSheetFactory::Shutdown();
+	StyleSheetParser::Shutdown();
 	StyleSheetSpecification::Shutdown();
 	StyleSheetSpecification::Shutdown();
 
 
 	font_interface = nullptr;
 	font_interface = nullptr;

+ 18 - 11
Source/Core/StyleSheetParser.cpp

@@ -111,8 +111,7 @@ public:
 
 
 	bool Parse(const String& name, const String& value) override
 	bool Parse(const String& name, const String& value) override
 	{
 	{
-		static const String str_src = "src";
-		if (name == str_src)
+		if (name == "src")
 		{
 		{
 			image_source = value;
 			image_source = value;
 		}
 		}
@@ -139,6 +138,8 @@ public:
 };
 };
 
 
 
 
+static UniquePtr<SpritesheetPropertyParser> spritesheet_property_parser;
+
 
 
 StyleSheetParser::StyleSheetParser()
 StyleSheetParser::StyleSheetParser()
 {
 {
@@ -151,6 +152,16 @@ StyleSheetParser::~StyleSheetParser()
 {
 {
 }
 }
 
 
+void StyleSheetParser::Initialise()
+{
+	spritesheet_property_parser = MakeUnique<SpritesheetPropertyParser>();
+}
+
+void StyleSheetParser::Shutdown()
+{
+	spritesheet_property_parser.reset();
+}
+
 static bool IsValidIdentifier(const String& str)
 static bool IsValidIdentifier(const String& str)
 {
 {
 	if (str.empty())
 	if (str.empty())
@@ -406,14 +417,11 @@ int StyleSheetParser::Parse(StyleSheetNode* node, Stream* _stream, const StyleSh
 					}
 					}
 					else if (at_rule_identifier == "spritesheet")
 					else if (at_rule_identifier == "spritesheet")
 					{
 					{
-						// This is reasonably heavy to initialize, so we make it static
-						static SpritesheetPropertyParser spritesheet_property_parser;
-						spritesheet_property_parser.Clear();
+						// The spritesheet parser is reasonably heavy to initialize, so we make it a static global.
+						ReadProperties(*spritesheet_property_parser);
 
 
-						ReadProperties(spritesheet_property_parser);
-
-						const String& image_source = spritesheet_property_parser.GetImageSource();
-						const SpriteDefinitionList& sprite_definitions = spritesheet_property_parser.GetSpriteDefinitions();
+						const String& image_source = spritesheet_property_parser->GetImageSource();
+						const SpriteDefinitionList& sprite_definitions = spritesheet_property_parser->GetSpriteDefinitions();
 						
 						
 						if (at_rule_name.empty())
 						if (at_rule_name.empty())
 						{
 						{
@@ -432,7 +440,7 @@ int StyleSheetParser::Parse(StyleSheetNode* node, Stream* _stream, const StyleSh
 							spritesheet_list.AddSpriteSheet(at_rule_name, image_source, stream_file_name, (int)line_number, sprite_definitions);
 							spritesheet_list.AddSpriteSheet(at_rule_name, image_source, stream_file_name, (int)line_number, sprite_definitions);
 						}
 						}
 
 
-						spritesheet_property_parser.Clear();
+						spritesheet_property_parser->Clear();
 						at_rule_name.clear();
 						at_rule_name.clear();
 						state = State::Global;
 						state = State::Global;
 					}
 					}
@@ -527,7 +535,6 @@ StyleSheetNodeListRaw StyleSheetParser::ConstructNodes(StyleSheetNode& root_node
 	return leaf_nodes;
 	return leaf_nodes;
 }
 }
 
 
-
 bool StyleSheetParser::ReadProperties(AbstractPropertyParser& property_parser)
 bool StyleSheetParser::ReadProperties(AbstractPropertyParser& property_parser)
 {
 {
 	RMLUI_ZoneScoped;
 	RMLUI_ZoneScoped;

+ 5 - 0
Source/Core/StyleSheetParser.h

@@ -71,6 +71,11 @@ public:
 	// @return The list of leaf nodes in the constructed tree, which are all owned by the root node.
 	// @return The list of leaf nodes in the constructed tree, which are all owned by the root node.
 	static StyleSheetNodeListRaw ConstructNodes(StyleSheetNode& root_node, const String& selectors);
 	static StyleSheetNodeListRaw ConstructNodes(StyleSheetNode& root_node, const String& selectors);
 
 
+	// Initialises property parsers. Call after initialisation of StylesheetSpecification.
+	static void Initialise();
+	// Reset property parsers.
+	static void Shutdown();
+
 private:
 private:
 	// Stream we're parsing from.
 	// Stream we're parsing from.
 	Stream* stream;
 	Stream* stream;