Browse Source

Merge pull request #69963 from lawnjelly/variant_parser_optional_readahead_3

VariantParser make readahead optional [3.x]
Rémi Verschelde 2 years ago
parent
commit
5d5f0a3958
3 changed files with 31 additions and 5 deletions
  1. 16 1
      core/variant_parser.cpp
  2. 13 3
      core/variant_parser.h
  3. 2 1
      scene/resources/resource_format_text.cpp

+ 16 - 1
core/variant_parser.cpp

@@ -42,7 +42,7 @@ CharType VariantParser::Stream::get_char() {
 	}
 
 	// attempt to readahead
-	readahead_filled = _read_buffer(readahead_buffer, READAHEAD_SIZE);
+	readahead_filled = _read_buffer(readahead_buffer, readahead_enabled ? READAHEAD_SIZE : 1);
 	if (readahead_filled) {
 		readahead_pointer = 0;
 	} else {
@@ -54,6 +54,13 @@ CharType VariantParser::Stream::get_char() {
 	return get_char();
 }
 
+bool VariantParser::Stream::is_eof() const {
+	if (readahead_enabled) {
+		return eof;
+	}
+	return _is_eof();
+}
+
 uint32_t VariantParser::StreamFile::_read_buffer(CharType *p_buffer, uint32_t p_num_chars) {
 	// The buffer is assumed to include at least one character (for null terminator)
 	ERR_FAIL_COND_V(!p_num_chars, 0);
@@ -75,6 +82,10 @@ bool VariantParser::StreamFile::is_utf8() const {
 	return true;
 }
 
+bool VariantParser::StreamFile::_is_eof() const {
+	return f->eof_reached();
+}
+
 uint32_t VariantParser::StreamString::_read_buffer(CharType *p_buffer, uint32_t p_num_chars) {
 	// The buffer is assumed to include at least one character (for null terminator)
 	ERR_FAIL_COND_V(!p_num_chars, 0);
@@ -107,6 +118,10 @@ bool VariantParser::StreamString::is_utf8() const {
 	return false;
 }
 
+bool VariantParser::StreamString::_is_eof() const {
+	return pos > s.length();
+}
+
 /////////////////////////////////////////////////////////////////////////////////////////////////
 
 const char *VariantParser::tk_name[TK_MAX] = {

+ 13 - 3
core/variant_parser.h

@@ -46,14 +46,16 @@ public:
 		bool eof = false;
 
 	protected:
+		bool readahead_enabled = true;
 		virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars) = 0;
+		virtual bool _is_eof() const = 0;
 
 	public:
 		CharType saved;
 
 		CharType get_char();
 		virtual bool is_utf8() const = 0;
-		bool is_eof() const { return eof; }
+		bool is_eof() const;
 
 		Stream() :
 				saved(0) {}
@@ -63,12 +65,16 @@ public:
 	struct StreamFile : public Stream {
 	protected:
 		virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
+		virtual bool _is_eof() const;
 
 	public:
 		FileAccess *f;
 
 		virtual bool is_utf8() const;
-		StreamFile() { f = nullptr; }
+		StreamFile(bool p_readahead_enabled = true) {
+			f = nullptr;
+			readahead_enabled = p_readahead_enabled;
+		}
 	};
 
 	struct StreamString : public Stream {
@@ -77,12 +83,16 @@ public:
 
 	protected:
 		virtual uint32_t _read_buffer(CharType *p_buffer, uint32_t p_num_chars);
+		virtual bool _is_eof() const;
 
 	public:
 		String s;
 
 		virtual bool is_utf8() const;
-		StreamString() { pos = 0; }
+		StreamString(bool p_readahead_enabled = true) {
+			pos = 0;
+			readahead_enabled = p_readahead_enabled;
+		}
 	};
 
 	typedef Error (*ParseResourceFunc)(void *p_self, Stream *p_stream, Ref<Resource> &r_res, int &line, String &r_err_str);

+ 2 - 1
scene/resources/resource_format_text.cpp

@@ -629,7 +629,8 @@ void ResourceInteractiveLoaderText::set_translation_remapped(bool p_remapped) {
 	translation_remapped = p_remapped;
 }
 
-ResourceInteractiveLoaderText::ResourceInteractiveLoaderText() {
+ResourceInteractiveLoaderText::ResourceInteractiveLoaderText() :
+		stream(false) {
 	translation_remapped = false;
 }