From 7ad64207ed666bf3f1a7a6cb90e87f19926283cd Mon Sep 17 00:00:00 2001
From: Ivan Savenko <saven.ivan@gmail.com>
Date: Fri, 3 May 2024 20:08:29 +0300
Subject: [PATCH] Prefer high-quality bik's to smk's

---
 client/media/CVideoHandler.cpp  | 23 ++++++++++++++++++-----
 lib/filesystem/ResourcePath.cpp |  5 +----
 lib/filesystem/ResourcePath.h   |  1 +
 lib/json/JsonValidator.cpp      |  1 +
 4 files changed, 21 insertions(+), 9 deletions(-)

diff --git a/client/media/CVideoHandler.cpp b/client/media/CVideoHandler.cpp
index 8bc819c29..1c7337360 100644
--- a/client/media/CVideoHandler.cpp
+++ b/client/media/CVideoHandler.cpp
@@ -61,14 +61,27 @@ static si64 lodSeek(void * opaque, si64 pos, int whence)
 	throw std::runtime_error(errorMessage.data());
 }
 
-void CVideoInstance::open(const VideoPath & videoToOpen)
+static std::unique_ptr<CInputStream> findVideoData(const VideoPath & videoToOpen)
 {
 	if(CResourceHandler::get()->existsResource(videoToOpen))
-		state.actualPath = videoToOpen;
-	else
-		state.actualPath = videoToOpen.addPrefix("VIDEO/");
+		return CResourceHandler::get()->load(videoToOpen);
 
-	state.videoData = CResourceHandler::get()->load(state.actualPath);
+	auto highQualityVideoToOpenWithDir = videoToOpen.addPrefix("VIDEO/");
+	auto lowQualityVideo = videoToOpen.toType<EResType::VIDEO_LOW_QUALITY>();
+	auto lowQualityVideoWithDir = highQualityVideoToOpenWithDir.toType<EResType::VIDEO_LOW_QUALITY>();
+
+	if(CResourceHandler::get()->existsResource(highQualityVideoToOpenWithDir))
+		return CResourceHandler::get()->load(highQualityVideoToOpenWithDir);
+
+	if(CResourceHandler::get()->existsResource(lowQualityVideo))
+		return CResourceHandler::get()->load(lowQualityVideo);
+
+	return CResourceHandler::get()->load(lowQualityVideoWithDir);
+}
+
+void CVideoInstance::open(const VideoPath & videoToOpen)
+{
+	state.videoData = findVideoData(videoToOpen);
 
 	static const int BUFFER_SIZE = 4096;
 
diff --git a/lib/filesystem/ResourcePath.cpp b/lib/filesystem/ResourcePath.cpp
index 15efb5cb7..d6bc81a2d 100644
--- a/lib/filesystem/ResourcePath.cpp
+++ b/lib/filesystem/ResourcePath.cpp
@@ -113,11 +113,8 @@ EResType EResTypeHelper::getTypeFromExtension(std::string extension)
 		{".MP3",   EResType::SOUND},
 		{".OGG",   EResType::SOUND},
 		{".FLAC",  EResType::SOUND},
-		{".SMK",   EResType::VIDEO},
+		{".SMK",   EResType::VIDEO_LOW_QUALITY},
 		{".BIK",   EResType::VIDEO},
-		{".MJPG",  EResType::VIDEO},
-		{".MPG",   EResType::VIDEO},
-		{".AVI",   EResType::VIDEO},
 		{".WEBM",  EResType::VIDEO},
 		{".ZIP",   EResType::ARCHIVE_ZIP},
 		{".LOD",   EResType::ARCHIVE_LOD},
diff --git a/lib/filesystem/ResourcePath.h b/lib/filesystem/ResourcePath.h
index 250e76bfc..fde6bec9f 100644
--- a/lib/filesystem/ResourcePath.h
+++ b/lib/filesystem/ResourcePath.h
@@ -46,6 +46,7 @@ enum class EResType
 	TTF_FONT,
 	IMAGE,
 	VIDEO,
+	VIDEO_LOW_QUALITY,
 	SOUND,
 	ARCHIVE_VID,
 	ARCHIVE_ZIP,
diff --git a/lib/json/JsonValidator.cpp b/lib/json/JsonValidator.cpp
index fd4b67d49..e2f6b1e06 100644
--- a/lib/json/JsonValidator.cpp
+++ b/lib/json/JsonValidator.cpp
@@ -493,6 +493,7 @@ static std::string imageFile(const JsonNode & node)
 static std::string videoFile(const JsonNode & node)
 {
 	TEST_FILE(node.getModScope(), "Video/", node.String(), EResType::VIDEO);
+	TEST_FILE(node.getModScope(), "Video/", node.String(), EResType::VIDEO_LOW_QUALITY);
 	return "Video file \"" + node.String() + "\" was not found";
 }
 #undef TEST_FILE