Skip to content

Commit

Permalink
Reworked IO on Android, improved logging on Android
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Oct 20, 2024
1 parent 3a28464 commit 0f28a33
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 135 deletions.
7 changes: 6 additions & 1 deletion Sources/Shared/Core/ITraceSink.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ namespace Death
class LoggerBackend;
}

/** @brief Interface for sink to be used by logger writing to it */
/**
@brief Interface for sink to be used by logger writing to it
The sink needs to be registered using @ref Trace::AttachSink() and unregistered using @ref Trace::DetachSink().
Then all registered sinks are automatically used by `LOGD`/`LOGI`/`LOGW`/`LOGE` calls and asserts.
*/
class ITraceSink
{
friend class Trace::LoggerBackend;
Expand Down
58 changes: 16 additions & 42 deletions Sources/Shared/IO/AndroidAssetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,31 +163,25 @@ namespace Death { namespace IO {
_internalDataPath = state->activity->internalDataPath;
}

const char* AndroidAssetStream::TryGetAssetPath(const char* path)
Containers::StringView AndroidAssetStream::TryGetAssetPath(const Containers::StringView path)
{
DEATH_ASSERT(path != nullptr, "path is null", nullptr);
if (strncmp(path, Prefix.data(), Prefix.size()) == 0) {
// Skip leading path separator character
return (path[7] == '/' || path[7] == '\\' ? path + 8 : path + 7);
if (path.hasPrefix(Prefix)) {
std::size_t prefixLength = Prefix.size();
return path.exceptPrefix(prefixLength < path.size() && (path[prefixLength] == '/' || path[prefixLength] == '\\')
? prefixLength + 1
: prefixLength);
}
return nullptr;
return {};
}

bool AndroidAssetStream::TryOpen(const char* path)
bool AndroidAssetStream::TryOpen(const Containers::StringView path)
{
DEATH_ASSERT(path != nullptr, "path is null", false);
return (TryOpenFile(path) || TryOpenDirectory(path));
}

bool AndroidAssetStream::TryOpenFile(const char* path)
bool AndroidAssetStream::TryOpenFile(const Containers::StringView path)
{
DEATH_ASSERT(path != nullptr, "path is null", false);
const char* strippedPath = TryGetAssetPath(path);
if (strippedPath == nullptr) {
return false;
}

AAsset* asset = AAssetManager_open(_assetManager, strippedPath, AASSET_MODE_UNKNOWN);
AAsset* asset = AAssetManager_open(_assetManager, path.data(), AASSET_MODE_UNKNOWN);
if (asset != nullptr) {
AAsset_close(asset);
return true;
Expand All @@ -196,21 +190,9 @@ namespace Death { namespace IO {
return false;
}

bool AndroidAssetStream::TryOpenDirectory(const char* path)
bool AndroidAssetStream::TryOpenDirectory(const Containers::StringView path)
{
DEATH_ASSERT(path != nullptr, "path is null", false);
const char* strippedPath = TryGetAssetPath(path);
if (strippedPath == nullptr) {
return false;
}

AAsset* asset = AAssetManager_open(_assetManager, strippedPath, AASSET_MODE_UNKNOWN);
if (asset != nullptr) {
AAsset_close(asset);
return false;
}

AAssetDir* assetDir = AAssetManager_openDir(_assetManager, strippedPath);
AAssetDir* assetDir = AAssetManager_openDir(_assetManager, path.data());
if (assetDir != nullptr) {
AAssetDir_close(assetDir);
return true;
Expand All @@ -219,17 +201,10 @@ namespace Death { namespace IO {
return false;
}

std::int64_t AndroidAssetStream::GetFileSize(const char* path)
std::int64_t AndroidAssetStream::GetFileSize(const Containers::StringView path)
{
DEATH_ASSERT(path != nullptr, "path is null", 0);

off64_t assetLength = 0;
const char* strippedPath = TryGetAssetPath(path);
if (strippedPath == nullptr) {
return assetLength;
}

AAsset* asset = AAssetManager_open(_assetManager, strippedPath, AASSET_MODE_UNKNOWN);
AAsset* asset = AAssetManager_open(_assetManager, path.data(), AASSET_MODE_UNKNOWN);
if (asset != nullptr) {
assetLength = AAsset_getLength64(asset);
AAsset_close(asset);
Expand All @@ -238,10 +213,9 @@ namespace Death { namespace IO {
return assetLength;
}

AAssetDir* AndroidAssetStream::OpenDirectory(const char* dirName)
AAssetDir* AndroidAssetStream::OpenDirectory(const Containers::StringView path)
{
DEATH_ASSERT(dirName != nullptr, "dirName is null", nullptr);
return AAssetManager_openDir(_assetManager, dirName);
return AAssetManager_openDir(_assetManager, path.data());
}

void AndroidAssetStream::CloseDirectory(AAssetDir* assetDir)
Expand Down
12 changes: 6 additions & 6 deletions Sources/Shared/IO/AndroidAssetStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,23 @@ namespace Death { namespace IO {
static void InitializeAssetManager(struct android_app* state);

/** @brief Returns the path of an Android asset without the prefix */
static const char* TryGetAssetPath(const char* path);
static Containers::StringView TryGetAssetPath(const Containers::StringView path);

/** @brief Returns the internal data path for the application */
static const char* GetInternalDataPath() {
return _internalDataPath;
}

/** @brief Checks if an asset path exists as a file or as a directory and can be opened */
static bool TryOpen(const char* path);
static bool TryOpen(const Containers::StringView path);
/** @brief Checks if an asset path exists and can be opened as a file */
static bool TryOpenFile(const char* path);
static bool TryOpenFile(const Containers::StringView path);
/** @brief Checks if an asset path exists and can be opened as a directory */
static bool TryOpenDirectory(const char* path);
static bool TryOpenDirectory(const Containers::StringView path);
/** @brief Returns the total size of the asset data */
static std::int64_t GetFileSize(const char* path);
static std::int64_t GetFileSize(const Containers::StringView path);

static AAssetDir* OpenDirectory(const char* dirName);
static AAssetDir* OpenDirectory(const Containers::StringView path);
static void CloseDirectory(AAssetDir* assetDir);
static void RewindDirectory(AAssetDir* assetDir);
static const char* GetNextFileName(AAssetDir* assetDir);
Expand Down
6 changes: 3 additions & 3 deletions Sources/Shared/IO/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,12 @@ namespace Death { namespace IO {
#else
# if defined(DEATH_USE_NOLOCK_IN_FILE)
# if defined(DEATH_TARGET_WINDOWS)
return _fflush_nolock(_handle) == 0;
return ::_fflush_nolock(_handle) == 0;
# else
return fflush_unlocked(_handle) == 0;
return ::fflush_unlocked(_handle) == 0;
# endif
# else
return fflush(_handle) == 0;
return ::fflush(_handle) == 0;
# endif
#endif
}
Expand Down
Loading

0 comments on commit 0f28a33

Please sign in to comment.