Skip to content

Commit

Permalink
Android file system: use output files directory
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMostDiligent committed Dec 5, 2023
1 parent ffb26de commit 1897655
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 25 deletions.
8 changes: 5 additions & 3 deletions Graphics/GraphicsEngine/interface/EngineFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ DILIGENT_BEGIN_INTERFACE(IEngineFactory, IObject)
/// On Android platform, it is necessary to initialize the file system before
/// CreateDefaultShaderSourceStreamFactory() method can be called.

/// \param [in] ExternalFilesDir - External files directory.
/// \param [in] AssetManager - A pointer to the asset manager (AAssetManager).
/// \param [in] ExternalFilesDir - External files directory.
/// \param [in] OutputFilesDir - Output files directory.
///
/// \remarks See AndroidFileSystem::Init.
VIRTUAL void METHOD(InitAndroidFileSystem)(THIS_
const char* ExternalFilesDir,
struct AAssetManager* AssetManager) CONST PURE;
struct AAssetManager* AssetManager,
const char* ExternalFilesDir DEFAULT_VALUE(nullptr),
const char* OutputFilesDir DEFAULT_VALUE(nullptr)) CONST PURE;
#endif
};
DILIGENT_END_INTERFACE
Expand Down
12 changes: 7 additions & 5 deletions Graphics/GraphicsEngineOpenGL/src/EngineFactoryOpenGL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ class EngineFactoryOpenGLImpl : public EngineFactoryBase<IEngineFactoryOpenGL>
}

#if PLATFORM_ANDROID
virtual void InitAndroidFileSystem(const char* ExternalFilesDir,
struct AAssetManager* AssetManager) const override final;
virtual void InitAndroidFileSystem(struct AAssetManager* AssetManager,
const char* ExternalFilesDir,
const char* OutputFilesDir) const override final;
#endif
};

Expand Down Expand Up @@ -376,10 +377,11 @@ void EngineFactoryOpenGLImpl::CreateHLSL2GLSLConverter(IHLSL2GLSLConverter** ppC
}

#if PLATFORM_ANDROID
void EngineFactoryOpenGLImpl::InitAndroidFileSystem(const char* ExternalFilesDir,
struct AAssetManager* AssetManager) const
void EngineFactoryOpenGLImpl::InitAndroidFileSystem(struct AAssetManager* AssetManager,
const char* ExternalFilesDir,
const char* OutputFilesDir) const
{
AndroidFileSystem::Init(ExternalFilesDir, AssetManager);
AndroidFileSystem::Init(AssetManager, ExternalFilesDir, OutputFilesDir);
}
#endif

Expand Down
12 changes: 7 additions & 5 deletions Graphics/GraphicsEngineVulkan/src/EngineFactoryVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ class EngineFactoryVkImpl final : public EngineFactoryBase<IEngineFactoryVk>
}

#if PLATFORM_ANDROID
virtual void InitAndroidFileSystem(const char* ExternalFilesDir,
struct AAssetManager* AssetManager) const override final;
virtual void InitAndroidFileSystem(struct AAssetManager* AssetManager,
const char* ExternalFilesDir,
const char* OutputFilesDir) const override final;
#endif

private:
Expand Down Expand Up @@ -1383,10 +1384,11 @@ void EngineFactoryVkImpl::CreateSwapChainVk(IRenderDevice* pDevice,


#if PLATFORM_ANDROID
void EngineFactoryVkImpl::InitAndroidFileSystem(const char* ExternalFilesDir,
struct AAssetManager* AssetManager) const
void EngineFactoryVkImpl::InitAndroidFileSystem(struct AAssetManager* AssetManager,
const char* ExternalFilesDir,
const char* OutputFilesDir) const
{
AndroidFileSystem::Init(ExternalFilesDir, AssetManager);
AndroidFileSystem::Init(AssetManager, ExternalFilesDir, OutputFilesDir);
}
#endif

Expand Down
11 changes: 7 additions & 4 deletions Platforms/Android/interface/AndroidFileSystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,19 @@ struct AndroidFileSystem : public BasicFileSystem
public:
/// Initializes the file system.

/// \param [in] ExternalFilesDir - External files directory.
/// \param [in] AssetManager - A pointer to the asset manager (AAssetManager).
/// \param [in] ExternalFilesDir - External files directory.
/// \param [in] OutputFilesDir - Output files directory.
///
/// \remarks The file system can be initialized to use either the external assets path or asset manager, or both.
/// When ExternalFilesDir is not null, the file system will try to use it first when opening files.
/// It will then resort to using the asset manager.
/// If ExternalFilesDir is null, the file system will only use the asset manager.
static void Init(const char* ExternalFilesDir,
struct AAssetManager* AssetManager);

// clang-format off
static void Init(struct AAssetManager* AssetManager,
const char* ExternalFilesDir DEFAULT_INITIALIZER(nullptr),
const char* OutputFilesDir DEFAULT_INITIALIZER(nullptr));
// clang-format on

static AndroidFile* OpenFile(const FileOpenAttribs& OpenAttribs);

Expand Down
33 changes: 25 additions & 8 deletions Platforms/Android/src/AndroidFileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,11 @@ struct AndroidFileSystemHelper
return Instance;
}

void Init(const char* ExternalFilesDir, AAssetManager* AssetManager)
void Init(AAssetManager* AssetManager, const char* ExternalFilesDir, const char* OutputFilesDir)
{
m_ExternalFilesDir = ExternalFilesDir != nullptr ? ExternalFilesDir : "";
m_AssetManager = AssetManager;
m_ExternalFilesDir = ExternalFilesDir != nullptr ? ExternalFilesDir : "";
m_OutputFilesDir = OutputFilesDir != nullptr ? OutputFilesDir : "";
}

bool OpenFile(const char* fileName, std::ifstream& IFS, AAsset*& AssetFile, size_t& FileSize)
Expand Down Expand Up @@ -109,12 +110,23 @@ struct AndroidFileSystemHelper
}
}

const std::string& GetExternalFilesDir() const
{
return m_ExternalFilesDir;
}

const std::string& GetOutputFilesDir() const
{
return m_OutputFilesDir;
}

private:
AndroidFileSystemHelper() {}

private:
std::string m_ExternalFilesDir;
AAssetManager* m_AssetManager = nullptr;
std::string m_ExternalFilesDir;
std::string m_OutputFilesDir;
};

} // namespace
Expand Down Expand Up @@ -197,10 +209,11 @@ bool AndroidFile::SetPos(size_t Offset, FilePosOrigin Origin)
}


void AndroidFileSystem::Init(const char* ExternalFilesPath,
struct AAssetManager* AssetManager)
void AndroidFileSystem::Init(struct AAssetManager* AssetManager,
const char* ExternalFilesDir,
const char* OutputFilesDir)
{
AndroidFileSystemHelper::GetInstance().Init(ExternalFilesPath, AssetManager);
AndroidFileSystemHelper::GetInstance().Init(AssetManager, ExternalFilesDir, OutputFilesDir);
}

AndroidFile* AndroidFileSystem::OpenFile(const FileOpenAttribs& OpenAttribs)
Expand Down Expand Up @@ -272,8 +285,12 @@ std::vector<std::unique_ptr<FindFileData>> AndroidFileSystem::Search(const Char*

std::string AndroidFileSystem::GetLocalAppDataDirectory(const char* AppName /*= nullptr*/, bool Create /*= true*/)
{
UNSUPPORTED("GetLocalAppDataDirectory() is not supported on Android");
return "";
const std::string& OutputFilesDir = AndroidFileSystemHelper::GetInstance().GetOutputFilesDir();
if (OutputFilesDir.empty())
{
LOG_ERROR_MESSAGE("Output files directory has not been initialized. Call AndroidFileSystem::Init().");
}
return OutputFilesDir;
}

} // namespace Diligent

0 comments on commit 1897655

Please sign in to comment.