Skip to content

Commit

Permalink
allow to read files virtually from memory stream
Browse files Browse the repository at this point in the history
  • Loading branch information
jpcima committed Oct 16, 2019
1 parent 302aab6 commit e31f280
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 1 deletion.
20 changes: 20 additions & 0 deletions sources/dunelegacy/FileClasses/FileManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,17 @@ std::vector<std::string> FileManager::getMissingFiles() {
sdl2::RWops_ptr FileManager::openFile(const std::string& filename) {
sdl2::RWops_ptr ret;

// try loading memory file
{
auto it = memoryFiles.find(strToUpper(filename));
if (it != memoryFiles.end()) {
ret = sdl2::RWops_ptr{SDL_RWFromConstMem(it->second.first, it->second.second)};
if(ret) {
return ret;
}
}
}

// try loading external file
for(const auto& searchPath : getSearchPath()) {
auto externalFilename = searchPath + "/";
Expand All @@ -153,6 +164,10 @@ sdl2::RWops_ptr FileManager::openFile(const std::string& filename) {

bool FileManager::exists(const std::string& filename) const {

if (memoryFiles.find(strToUpper(filename)) != memoryFiles.end()) {
return true;
}

// try finding external file
for(const std::string& searchPath : getSearchPath()) {
auto externalFilename = searchPath + "/";
Expand All @@ -172,6 +187,11 @@ bool FileManager::exists(const std::string& filename) const {
return false;
}

void FileManager::insertMemoryFile(const std::string &filename, const void *data, size_t length)
{
memoryFiles[strToUpper(filename)] = {data, length};
}


std::string FileManager::md5FromFilename(const std::string& filename) const {
unsigned char md5sum[16];
Expand Down
5 changes: 5 additions & 0 deletions sources/dunelegacy/FileClasses/FileManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <string>
#include <vector>
#include <map>
#include <memory>

/// A class for loading all the PAK-Files.
Expand Down Expand Up @@ -59,10 +60,14 @@ class FileManager final {
sdl2::RWops_ptr openFile(const std::string& filename);

bool exists(const std::string& filename) const;

void insertMemoryFile(const std::string &filename, const void *data, size_t length);
private:
std::string md5FromFilename(const std::string& filename) const;

std::vector<std::unique_ptr<Pakfile>> pakFiles;

std::map<std::string, std::pair<const void *, size_t>> memoryFiles;
};

#endif // FILEMANAGER_H
13 changes: 12 additions & 1 deletion sources/dunemusic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ DUNEMUSIC_EXPORT
void DuneMusic_Init(int sampleRate, const char *dataDirectory, DuneMusicOplEmu oplEmu)
{
Mix_OpenAudio(sampleRate, MIX_DEFAULT_FORMAT, 2, 1024);
duneLegacyDataDir.assign(dataDirectory);

if (dataDirectory && dataDirectory[0])
duneLegacyDataDir.assign(dataDirectory);
else
duneLegacyDataDir.assign(".");

pFileManager.reset(new FileManager);
SoundAdlibPC::s_oplEmu = oplEmu;
sPlayer.reset(new ADLPlayer);
Expand All @@ -35,6 +40,12 @@ void DuneMusic_Quit()
Mix_CloseAudio();
}

DUNEMUSIC_EXPORT
void DuneMusic_InsertMemoryFile(const char *filename, const void *data, size_t length)
{
pFileManager->insertMemoryFile(filename, data, length);
}

DUNEMUSIC_EXPORT
void DuneMusic_GetSamples(int16_t *buf, unsigned count)
{
Expand Down
3 changes: 3 additions & 0 deletions sources/dunemusic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
Expand All @@ -17,6 +18,8 @@ void DuneMusic_Quit();

void DuneMusic_GetSamples(int16_t *buf, unsigned count);

void DuneMusic_InsertMemoryFile(const char *filename, const void *data, size_t length);

typedef enum DuneMusicType {
kMusicAttack = 0, /*!< Played when at least one of player's units was hit. */
kMusicPeace, /*!< Played most of the time when the enemy is not attacking. */
Expand Down

0 comments on commit e31f280

Please sign in to comment.