Skip to content

Commit c8e0525

Browse files
committed
engine: embed media files
1 parent 87cc29d commit c8e0525

File tree

8 files changed

+53
-3
lines changed

8 files changed

+53
-3
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,9 @@ if (NOT NACL)
10101010
Tests ${ENGINETESTLIST}
10111011
)
10121012
endif()
1013+
1014+
# Generate media include files.
1015+
daemon_embed_files("EngineMedia" "MEDIA" "engine-lib")
10131016
endif()
10141017

10151018
if (BUILD_CLIENT)

src.cmake

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ else()
8686
include (${ENGINE_DIR}/renderer/src.cmake)
8787
endif()
8888

89+
set(MEDIA_EMBED_DIR ${ENGINE_DIR}/media)
90+
set(MEDIA_EMBED_LIST
91+
gfx/2d/bigchars.png
92+
scripts/engine.shader
93+
sound/null.wav
94+
)
95+
8996
set(GLSL_EMBED_DIR "${ENGINE_DIR}/renderer/glsl_source")
9097
set(GLSL_EMBED_LIST
9198
# Common shader libraries
@@ -167,6 +174,8 @@ set(SERVERLIST
167174
)
168175

169176
set(ENGINELIST
177+
${DAEMON_EMBEDDED_DIR}/EngineMedia.cpp
178+
${DAEMON_EMBEDDED_DIR}/EngineMedia.h
170179
${ENGINE_DIR}/framework/Application.cpp
171180
${ENGINE_DIR}/framework/Application.h
172181
${ENGINE_DIR}/framework/ApplicationInternals.h

src/common/FileSystem.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3030

3131
#if defined(BUILD_ENGINE)
3232
#include "minizip/unzip.h"
33+
#include "DaemonEmbedded/EngineMedia.h"
3334
#endif
3435

3536
#ifdef BUILD_VM
@@ -129,6 +130,15 @@ static Cvar::Cvar<bool> fs_legacypaks("fs_legacypaks", "also load pk3s, ignoring
129130
static Cvar::Cvar<int> fs_maxSymlinkDepth("fs_maxSymlinkDepth", "max depth of symlinks in zip paks (0 means disabled)", Cvar::NONE, 1);
130131
static Cvar::Cvar<std::string> fs_pakprefixes("fs_pakprefixes", "prefixes to look for paks to load", 0, "");
131132

133+
const PakInfo builtinPak =
134+
{
135+
"*builtin",
136+
ENGINE_VERSION,
137+
0,
138+
pakType_t::PAK_BUILTIN,
139+
"*builtin",
140+
};
141+
132142
bool UseLegacyPaks()
133143
{
134144
return fs_legacypaks.Get();
@@ -1258,6 +1268,7 @@ static void InternalLoadPak(
12581268
offset_t depsOffset = 0;
12591269
ZipArchive zipFile;
12601270
bool isLegacy = pak.version.empty();
1271+
bool isBuiltin = pak.type == pakType_t::PAK_BUILTIN;
12611272

12621273
// Check if this pak has already been loaded to avoid recursive dependencies
12631274
for (auto& x: loadedPaks) {
@@ -1279,6 +1290,8 @@ static void InternalLoadPak(
12791290
} else {
12801291
fsLogs.WithoutSuppression().Notice("Loading legacy pakdir '%s'...", pak.path.c_str());
12811292
}
1293+
} else if (pak.type == pakType_t::PAK_BUILTIN ) {
1294+
fsLogs.WithoutSuppression().Notice("Loading builtin pak '%s'...", pak.path.c_str());
12821295
} else {
12831296
ASSERT_UNREACHABLE();
12841297
}
@@ -1369,6 +1382,11 @@ static void InternalLoadPak(
13691382
}, err);
13701383
if (err)
13711384
return;
1385+
} else if (pak.type == pakType_t::PAK_BUILTIN) {
1386+
for ( auto it : EngineMedia::FileMap )
1387+
{
1388+
fileMap.emplace(it.first, std::pair<uint32_t, offset_t>(it.second.size(), 0));
1389+
}
13721390
} else {
13731391
ASSERT_UNREACHABLE();
13741392
}
@@ -1388,8 +1406,8 @@ static void InternalLoadPak(
13881406

13891407
loadedPak.pathPrefix = pathPrefix;
13901408

1391-
// Legacy paks don't have version neither checksum
1392-
if (!isLegacy) {
1409+
// Legacy and builtin paks don't have version neither checksum
1410+
if (!isLegacy && !isBuiltin) {
13931411
// If an explicit checksum was requested, verify that the pak we loaded is the one we are expecting
13941412
if (expectedChecksum && realChecksum != *expectedChecksum) {
13951413
SetErrorCodeFilesystem(err, filesystem_error::wrong_pak_checksum, pak.path);
@@ -1542,6 +1560,12 @@ std::string ReadFile(Str::StringRef path, std::error_code& err) {
15421560
#ifdef BUILD_ENGINE
15431561
std::string ReadFile(Str::StringRef path, std::error_code& err)
15441562
{
1563+
if (EngineMedia::HasFile(path))
1564+
{
1565+
Log::Debug("Loading internal file: %s", path);
1566+
return EngineMedia::ReadFile(path);
1567+
}
1568+
15451569
auto it = fileMap.find(path);
15461570
if (it == fileMap.end()) {
15471571
SetErrorCodeFilesystem(err, filesystem_error::no_such_file, path);
@@ -2660,6 +2684,13 @@ static const PakInfo* FindPakNoPrefix(Str::StringRef name)
26602684

26612685
const PakInfo* FindPak(Str::StringRef name)
26622686
{
2687+
#if defined(BUILD_ENGINE)
2688+
if (name == builtinPak.name)
2689+
{
2690+
return &builtinPak;
2691+
}
2692+
#endif
2693+
26632694
Cmd::Args pakprefixes(Cvar::GetValue("fs_pakprefixes"));
26642695
for (const std::string &pakprefix: pakprefixes)
26652696
{

src/common/FileSystem.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ template<typename State> class DirectoryIterator {
265265
// Type of pak
266266
enum class pakType_t {
267267
PAK_ZIP, // Zip archive
268-
PAK_DIR // Directory
268+
PAK_DIR, // Directory
269+
PAK_BUILTIN, // Embedded in the engine
269270
};
270271

271272
// Information about a package
@@ -288,6 +289,8 @@ struct PakInfo {
288289
std::string path;
289290
};
290291

292+
extern const PakInfo builtinPak;
293+
291294
// Information about a package that has been loaded
292295
struct LoadedPakInfo: public PakInfo {
293296
// Actual CRC32 checksum of the pak, derived from the pak contents. This is
File renamed without changes.
File renamed without changes.

src/engine/qcommon/files.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,10 @@ bool FS_LoadPak(const Str::StringRef name)
655655

656656
void FS_LoadBasePak()
657657
{
658+
#if defined(BUILD_GRAPHICAL_CLIENT)
659+
FS_LoadPak(FS::builtinPak.name);
660+
#endif
661+
658662
Cmd::Args extrapaks(fs_extrapaks.Get());
659663
for (auto& x: extrapaks) {
660664
if (!FS_LoadPak(x)) {

0 commit comments

Comments
 (0)