/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

#include "BundledResoureLoaderIos.h"
#include "Base/Logger.hpp"
#include "Base/Utils.hpp"

namespace OpenVulkano
{
	namespace
	{
		void* LOADER_HANDLE = ResourceLoader::RegisterResourceLoader(std::move(std::unique_ptr<ResourceLoader>(new BundledResourceLoaderIOS())));
	}


	Array<char> BundledResourceLoaderIOS::GetResource(const std::string& resourceName)
	{
		@autoreleasepool
		{
			auto [fileName, fileType] = Utils::SplitAtLastOccurrence(resourceName, '.');
			NSString* filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:fileName.c_str()] ofType:[NSString stringWithUTF8String:fileType.c_str()]];
			if (filePath)
				return Utils::ReadFile(std::string([filePath UTF8String]));
		}
		Logger::FILESYS->warn("Failed to find resource: '{}' in bundle", resourceName);
		return {};
	}
}
