Model *BinaryConverter::Load(const std::string &shortname, const std::string &basepath)
{
	FileSystem::FileSource &fileSource = FileSystem::gameDataFiles;
	for (FileSystem::FileEnumerator files(fileSource, basepath, FileSystem::FileEnumerator::Recurse); !files.Finished(); files.Next()) {
		const FileSystem::FileInfo &info = files.Current();
		const std::string &fpath = info.GetPath();

		//check it's the expected type
		if (info.IsFile() && ends_with_ci(fpath, SGM_EXTENSION)) {
			//check it's the wanted name & load it
			const std::string name = info.GetName();

			if (shortname == name.substr(0, name.length() - SGM_EXTENSION.length())) {
				//curPath is used to find textures, patterns,
				//possibly other data files for this model.
				//Strip trailing slash
				m_curPath = info.GetDir();
				if (m_curPath[m_curPath.length()-1] == '/')
					m_curPath = m_curPath.substr(0, m_curPath.length()-1);

				RefCountedPtr<FileSystem::FileData> binfile = info.Read();
				if (binfile.Valid()) {
					Model* model(nullptr);
					size_t outSize(0);
					// decompress the loaded ByteRange in memory
					const ByteRange bin = binfile->AsByteRange();
					void *pDecompressedData = tinfl_decompress_mem_to_heap(&bin[0], bin.Size(), &outSize, 0);
					if (pDecompressedData) {
						// now parse in-memory representation as new ByteRange.
						Serializer::Reader rd(ByteRange(static_cast<char*>(pDecompressedData), outSize));
						model = CreateModel(rd);
						mz_free(pDecompressedData);
					}
					return model;
				}
			}
		}
	}

	throw (LoadingError("File not found"));
	return nullptr;
}