-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDK2AssetLoader.cpp
More file actions
182 lines (158 loc) · 5.27 KB
/
Copy pathDK2AssetLoader.cpp
File metadata and controls
182 lines (158 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "stdafx.h"
#include "DK2AssetLoader.h"
#include "DK2WADArchive.h"
#include "DK2EngineTextures.h"
//////////////////////////////////////////////////////////////////////////
DK2AssetLoader gDK2AssetLoader;
//////////////////////////////////////////////////////////////////////////
bool DK2AssetLoader::Initialize()
{
std::string texturesCacheLocation;
if (!gFiles.LocateEngineTexturesCache(texturesCacheLocation))
{
gConsole.LogMessage(eLogLevel_Warning, "Cannot locate engine textures cache");
return false;
}
gConsole.LogMessage(eLogLevel_Info, "Engine textures cache location '%s'", texturesCacheLocation.c_str());
if (!mEngineTexturesCache.ScanDungeonKeeperTexturesCache(texturesCacheLocation))
return false;
if (!LoadWADs())
return false;
gConsole.LogMessage(eLogLevel_Info, "Resource data provider is initialized");
return true;
}
void DK2AssetLoader::Shutdown()
{
FreeWADs();
mEngineTexturesCache.Shutdown();
}
bool DK2AssetLoader::LoadWADs()
{
const static std::string archiveNamesList[] =
{
"Meshes.WAD",
"EngineTextures.WAD",
"FrontEnd.WAD",
"Paths.WAD",
"Sprite.WAD"
};
cxx_assert(mArchives.empty());
for (const std::string& rollerArchiveName: archiveNamesList)
{
DK2WADArchive* archiveInstance = new DK2WADArchive (rollerArchiveName);
std::string archiveFilePath;
if (!gFiles.LocateWAD(rollerArchiveName, archiveFilePath) || !archiveInstance->OpenArchive(archiveFilePath))
{
gConsole.LogMessage(eLogLevel_Warning, "Cannot open WAD archive '%s'", rollerArchiveName.c_str());
}
mArchives.push_back(archiveInstance);
}
return !mArchives.empty();
}
void DK2AssetLoader::FreeWADs()
{
for (DK2WADArchive* rollerArchive: mArchives)
{
SafeDelete(rollerArchive);
}
mArchives.clear();
}
bool DK2AssetLoader::LoadImageData(const std::string& theTextureName, BitmapImage& outputBitmap)
{
// 1. search in engine textures cache
DK2EngineTextureID textureID;
if (mEngineTexturesCache.FindTextureByName(theTextureName, textureID))
{
if (!mEngineTexturesCache.ExtractTexture(textureID, outputBitmap))
{
gConsole.LogMessage(eLogLevel_Warning, "Cannot extract texture '%s'", theTextureName.c_str());
return false;
}
return true;
}
// 2. search in wads
if (LoadFromWADs(theTextureName, mReadBuffer))
{
bool isLoaded = false;
if (cxx::ends_with_icase(theTextureName, ".444"))
{
isLoaded = mEngineTexturesCache.Decompress444(mReadBuffer, outputBitmap);
}
else
{
isLoaded = outputBitmap.LoadFromMemory(mReadBuffer.data(), mReadBuffer.size());
}
if (!isLoaded)
{
gConsole.LogMessage(eLogLevel_Warning, "Cannot extract texture '%s'", theTextureName.c_str());
return false;
}
return true;
}
gConsole.LogMessage(eLogLevel_Warning, "Cannot load texture '%s'", theTextureName.c_str());
return false;
}
bool DK2AssetLoader::LoadKMFModelData(const std::string& theMeshName, DK2KMFModel& outputModel)
{
if (!LoadFromWADs(theMeshName, mReadBuffer))
return false;
cxx::memory_istream memorystream((char*)&mReadBuffer[0], (char*) &mReadBuffer[0] + mReadBuffer.size());
std::istream instream(&memorystream);
return DK2_KMF_LoadFromStream(instream, outputModel);
}
bool DK2AssetLoader::LoadBF4FontData(const std::string& theFontName, DK2FontDesc& outputFontData)
{
std::string resourcePath;
if (!gFiles.LocateFont(theFontName, resourcePath))
{
gConsole.LogMessage(eLogLevel_Warning, "Font '%s' is not found", theFontName.c_str());
return false;
}
std::ifstream fileStream(resourcePath, std::ios::in | std::ios::binary);
if (fileStream.is_open())
return DK2_BF4_LoadFromStream(fileStream, outputFontData);
return false;
}
bool DK2AssetLoader::GetArchiveNames(std::vector<std::string>& namesList) const
{
namesList.clear();
namesList.reserve(mArchives.size());
for (DK2WADArchive* rollerArchive: mArchives)
{
namesList.push_back(rollerArchive->GetArchiveName());
}
return !namesList.empty();
}
bool DK2AssetLoader::GetArchiveEntryNames(const std::string& archiveName, std::vector<std::string>& entryNames) const
{
entryNames.clear();
if (DK2WADArchive* archive = GetArchiveByName(archiveName))
{
return archive->GetEntryNames(entryNames);
}
return false;
}
bool DK2AssetLoader::LoadFromWADs(const std::string& theResourceName, ByteArray& theOutputData)
{
theOutputData.clear();
for (DK2WADArchive* wad_archive: mArchives)
{
DK2WADArchiveEntryID entry_id;
if (!wad_archive->FindEntryByName(theResourceName, entry_id))
{
continue;
}
// try to load found entry
return wad_archive->ExtractEntryData(entry_id, theOutputData);
}
return false;
}
DK2WADArchive* DK2AssetLoader::GetArchiveByName(const std::string& archiveName) const
{
for (DK2WADArchive* rollerArchive: mArchives)
{
if (rollerArchive->GetArchiveName() == archiveName)
return rollerArchive;
}
return nullptr;
}