From 56e0bea8d9ef2c63588bcb929747290955bec17d Mon Sep 17 00:00:00 2001
From: Rodrigo Braz Monteiro <zeratul@cellosoft.com>
Date: Sat, 22 Nov 2008 00:35:10 +0000
Subject: [PATCH] Improved detection of ASS-family formats.

Originally committed to SVN as r2474.
---
 athenasub/src/formats/format_ass.cpp | 44 ++++++++++++++++++++++++++--
 athenasub/src/formats/format_ass.h   |  6 ++++
 2 files changed, 47 insertions(+), 3 deletions(-)

diff --git a/athenasub/src/formats/format_ass.cpp b/athenasub/src/formats/format_ass.cpp
index 4f0e133ab..c713d18c9 100644
--- a/athenasub/src/formats/format_ass.cpp
+++ b/athenasub/src/formats/format_ass.cpp
@@ -96,10 +96,48 @@ StringArray FormatASS2::GetWriteExtensions() const
 float FormatASSFamily::CanReadFile(Reader &reader) const
 {
 	shared_ptr<TextReader> file = reader.GetTextReader();
-	if (!file->HasMoreLines()) return 0;
+
+	// Check header
+	if (!file->HasMoreLines()) return 0.0f;
 	String line = file->ReadLineFromFile();
-	if (line == "[Script Info]") return 1;
-	return 0;
+	line.AsciiMakeLower();
+	if (line != "[script info]") return 0.0f;
+
+	float version = 0.0f;
+	float sections = 0.25f;
+	String section = line;
+	while (file->HasMoreLines()) {
+		// Get line
+		line = file->ReadLineFromFile();
+		line.AsciiMakeLower();
+
+		// Set section
+		if (line[0] == '[' && line[line.Length()-1] == ']') section = line;
+
+		// Check version
+		if (section == "[script info]") {
+			if (line.StartsWith("ScriptType")) {
+				int formatVersion = GetVersion();
+				String expected = "";
+				switch (formatVersion) {
+					case 0: expected = "v4.00"; break;
+					case 1: expected = "v4.00+"; break;
+					case 2: expected = "v4.00++"; break;
+				}
+				if (line.EndsWith(expected)) version = 0.5f;
+				else version = 0.25f;
+			}
+		}
+
+		// Done when events begin
+		if (section == "[events]") {
+			sections += 0.25f;
+			break;
+		}
+	}
+
+	// OK
+	return sections+version;
 }
 
 
diff --git a/athenasub/src/formats/format_ass.h b/athenasub/src/formats/format_ass.h
index dcbc9099a..bdfe93488 100644
--- a/athenasub/src/formats/format_ass.h
+++ b/athenasub/src/formats/format_ass.h
@@ -68,6 +68,9 @@ namespace Athenasub {
 
 	// Advanced Substation Alpha format base class
 	class FormatASSFamily : public IFormat {
+	protected:
+		virtual int GetVersion() const = 0;
+
 	public:
 		virtual ~FormatASSFamily() {}
 
@@ -98,6 +101,7 @@ namespace Athenasub {
 		String GetName() const { return "Substation Alpha"; }
 		StringArray GetReadExtensions() const;
 		StringArray GetWriteExtensions() const;
+		int GetVersion() const { return 0; }
 	};
 
 	// Advanced Substation Alpha
@@ -107,6 +111,7 @@ namespace Athenasub {
 		String GetName() const { return "Advanced Substation Alpha"; }
 		StringArray GetReadExtensions() const;
 		StringArray GetWriteExtensions() const;
+		int GetVersion() const { return 1; }
 	};
 
 	// Advanced Substation Alpha 2
@@ -116,6 +121,7 @@ namespace Athenasub {
 		String GetName() const { return "Advanced Substation Alpha 2"; }
 		StringArray GetReadExtensions() const;
 		StringArray GetWriteExtensions() const;
+		int GetVersion() const { return 2; }
 	};
 
 }
-- 
GitLab