Advertisement
Guest User

Untitled

a guest
Nov 25th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.60 KB | None | 0 0
  1. void ResourceManager::LoadArchive(Pack& pack, ResourceType type, const std::filesystem::path& path)
  2. {
  3.     using namespace util;
  4.     HND_LOG_DEBUG("ARCHIVE: " + path.filename().string());
  5.  
  6.     std::string spath = path.string();
  7.  
  8.     rresCentralDir dir = rresLoadCentralDirectory(spath.c_str());
  9.     if (dir.count == 0) HND_LOG_DEBUG("ARCHIVE: No central directory");
  10.  
  11.     unsigned int chunkCount = 0;
  12.     rresResourceChunkInfo* infos = rresLoadResourceChunkInfoAll(spath.c_str(), &chunkCount);
  13.     unsigned int prevId = 0;
  14.  
  15.     for (unsigned int i = 0; i < chunkCount; i++)
  16.     {
  17.         // skips fonts in tex/obj dirs and tex/obj in font dir
  18.         if ((SameFourCC(infos[i].type, FOURCC_IMAGE) && type == ResourceType::Font) ||
  19.             SameFourCC(infos[i].type, FOURCC_FONT) && (type == ResourceType::Texture|| type==ResourceType::Object))
  20.             continue;
  21.  
  22.         std::string name;
  23.         for (unsigned int j = 0; j < dir.count; j++)
  24.         {
  25.             if ((infos[i].id == dir.entries[j].id) && (infos[i].id != prevId))
  26.             {
  27.                 name = std::string(dir.entries[j].fileName);
  28.                 prevId = dir.entries[j].id;
  29.                 break;
  30.             }
  31.         }
  32.  
  33.         if(SameFourCC(infos[i].type, FOURCC_IMAGE))
  34.         {
  35.             rresResourceChunk chunk = rresLoadResourceChunk(spath.c_str(), infos[i].id);
  36.             int result = UnpackResourceChunk(&chunk);
  37.             if (result == 0)
  38.             {
  39.                 Image img = LoadImageFromResource(chunk);
  40.                 if (type == ResourceType::Texture)
  41.                 {
  42.                     pack.textures[GenerateHandle(pack.name, type, name)] =
  43.                         TextureData(name,std::make_shared<Texture2D>(LoadTextureFromImage(img)));
  44.  
  45.                     HND_LOG_DEBUG("ARCHIVE: loaded texture " + name);
  46.                 }
  47.                 else
  48.                 {
  49.                     pack.objects[GenerateHandle(pack.name, type, name)] =
  50.                         TextureData(name, std::make_shared<Texture2D>(LoadTextureFromImage(img)));
  51.  
  52.                     HND_LOG_DEBUG("ARCHIVE: loaded object " + name);
  53.                 }
  54.                 UnloadImage(img);
  55.             }
  56.             else HND_LOG_ERROR(
  57.                 std::format("Failed to unpack resource chunk (id:{})", infos[i].id));
  58.  
  59.             rresUnloadResourceChunk(chunk);
  60.         }
  61.         else if (SameFourCC(infos[i].type, FOURCC_FONT))
  62.         {
  63.             rresResourceMulti multi = rresLoadResourceMulti(spath.c_str(), infos[i].id);
  64.             int result = -1;
  65.             for (unsigned int i = 0; i < multi.count; i++)
  66.             {
  67.                 result = UnpackResourceChunk(&multi.chunks[i]);
  68.                 if (result != 0) HND_LOG_ERROR(
  69.                     std::format("Failed to unpack multi resource chunk (id:{}, i:{})", infos[i].id, i));
  70.             }
  71.             if (result == 0)
  72.             {
  73.                 pack.fonts[GenerateHandle(pack.name, type, name)]=
  74.                     FontData(name, std::make_shared<Font>(LoadFontFromResource(multi)));
  75.  
  76.                 HND_LOG_DEBUG("ARCHIVE: loaded font " + name);
  77.             }
  78.             rresUnloadResourceMulti(multi);
  79.         }
  80.     }
  81. }
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement