Skip to content

Commit

Permalink
Added timeBeginPeriod, minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
deathkiller committed Mar 18, 2024
1 parent d8aa7e5 commit af63597
Show file tree
Hide file tree
Showing 11 changed files with 340 additions and 218 deletions.
6 changes: 3 additions & 3 deletions Sources/Jazz2/Multiplayer/Backends/enet.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
#ifndef ENET_NO_PRAGMA_LINK
#ifndef __GNUC__
#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "winmm.lib")
//#pragma comment(lib, "winmm.lib")
#endif
#endif

Expand Down Expand Up @@ -5744,12 +5744,12 @@ extern "C" {
return -1;
}

timeBeginPeriod(1);
//timeBeginPeriod(1);
return 0;
}

void enet_deinitialize(void) {
timeEndPeriod(1);
//timeEndPeriod(1);
WSACleanup();
}

Expand Down
12 changes: 4 additions & 8 deletions Sources/Shared/IO/AndroidAssetStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,13 +231,14 @@ namespace Death { namespace IO {

void AndroidAssetStream::Open(FileAccessMode mode)
{
#if defined(DEATH_USE_FILE_DESCRIPTORS)
// An asset file can only be read
if (mode != FileAccessMode::Read) {
FileAccessMode maskedMode = mode & ~FileAccessMode::Exclusive;
if (maskedMode != FileAccessMode::Read) {
LOGE("Cannot open file \"%s\" - wrong open mode", _path.data());
return;
}

#if defined(DEATH_USE_FILE_DESCRIPTORS)
// An asset file can only be read
AAsset* asset = AAssetManager_open(_assetManager, _path.data(), AASSET_MODE_UNKNOWN);
if (asset == nullptr) {
LOGE("Cannot open file \"%s\"", _path.data());
Expand All @@ -262,11 +263,6 @@ namespace Death { namespace IO {
LOGI("File \"%s\" opened", _path.data());
#else
// An asset file can only be read
if (mode != FileAccessMode::Read) {
LOGE("Cannot open file \"%s\" - wrong open mode", _path.data());
return;
}

_asset = AAssetManager_open(_assetManager, _path.data(), AASSET_MODE_UNKNOWN);
if (_asset == nullptr) {
LOGE("Cannot open file \"%s\"", _path.data());
Expand Down
24 changes: 14 additions & 10 deletions Sources/Shared/IO/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ namespace Death { namespace IO {
{
#if defined(DEATH_USE_FILE_DESCRIPTORS)
std::int32_t openFlag;
switch (mode) {
switch (mode & ~FileAccessMode::Exclusive) {
case FileAccessMode::Read:
openFlag = O_RDONLY;
break;
Expand All @@ -162,7 +162,7 @@ namespace Death { namespace IO {
return;
}

switch (mode) {
switch (mode & ~FileAccessMode::Exclusive) {
default: LOGI("File \"%s\" opened", _path.data()); break;
case FileAccessMode::Write: LOGI("File \"%s\" opened for write", _path.data()); break;
case FileAccessMode::Read | FileAccessMode::Write: LOGI("File \"%s\" opened for read+write", _path.data()); break;
Expand All @@ -176,31 +176,35 @@ namespace Death { namespace IO {
DWORD desireAccess, creationDisposition;
std::int32_t openFlag;
const char* modeInternal;
switch (mode) {
DWORD shareMode;
switch (mode & ~FileAccessMode::Exclusive) {
case FileAccessMode::Read:
desireAccess = GENERIC_READ;
creationDisposition = OPEN_EXISTING;
openFlag = _O_RDONLY | _O_BINARY;
modeInternal = "rb";
shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? 0 : FILE_SHARE_READ | FILE_SHARE_WRITE);
break;
case FileAccessMode::Write:
desireAccess = GENERIC_WRITE;
creationDisposition = CREATE_ALWAYS;
openFlag = _O_WRONLY | _O_BINARY;
modeInternal = "wb";
shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? 0 : FILE_SHARE_READ);
break;
case FileAccessMode::Read | FileAccessMode::Write:
desireAccess = GENERIC_READ | GENERIC_WRITE;
creationDisposition = OPEN_ALWAYS;
openFlag = _O_RDWR | _O_BINARY;
modeInternal = "r+b";
shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? 0 : FILE_SHARE_READ);
break;
default:
LOGE("Cannot open file \"%s\" - wrong open mode", _path.data());
return;
}

HANDLE hFile = ::CreateFile2FromAppW(Utf8::ToUtf16(_path), desireAccess, FILE_SHARE_READ, creationDisposition, nullptr);
HANDLE hFile = ::CreateFile2FromAppW(Utf8::ToUtf16(_path), desireAccess, shareMode, creationDisposition, nullptr);
if (hFile == nullptr || hFile == INVALID_HANDLE_VALUE) {
DWORD error = ::GetLastError();
LOGE("Cannot open file \"%s\" - failed with error 0x%08X", _path.data(), error);
Expand All @@ -216,10 +220,10 @@ namespace Death { namespace IO {
# elif defined(DEATH_TARGET_WINDOWS) && !defined(DEATH_TARGET_MINGW)
const wchar_t* modeInternal;
std::int32_t shareMode;
switch (mode) {
case FileAccessMode::Read: modeInternal = L"rb"; shareMode = _SH_DENYNO; break;
case FileAccessMode::Write: modeInternal = L"wb"; shareMode = _SH_DENYWR; break;
case FileAccessMode::Read | FileAccessMode::Write: modeInternal = L"r+b"; shareMode = _SH_DENYWR; break;
switch (mode & ~FileAccessMode::Exclusive) {
case FileAccessMode::Read: modeInternal = L"rb"; shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? _SH_DENYRW : _SH_DENYNO); break;
case FileAccessMode::Write: modeInternal = L"wb"; shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? _SH_DENYRW : _SH_DENYWR); break;
case FileAccessMode::Read | FileAccessMode::Write: modeInternal = L"r+b"; shareMode = ((mode & FileAccessMode::Exclusive) == FileAccessMode::Exclusive ? _SH_DENYRW : _SH_DENYWR); break;
default:
LOGE("Cannot open file \"%s\" - wrong open mode", _path.data());
return;
Expand All @@ -233,7 +237,7 @@ namespace Death { namespace IO {
}
# else
const char* modeInternal;
switch (mode) {
switch (mode & ~FileAccessMode::Exclusive) {
case FileAccessMode::Read: modeInternal = "rb"; break;
case FileAccessMode::Write: modeInternal = "wb"; break;
case FileAccessMode::Read | FileAccessMode::Write: modeInternal = "r+b"; break;
Expand All @@ -249,7 +253,7 @@ namespace Death { namespace IO {
}
# endif

switch (mode) {
switch (mode & ~FileAccessMode::Exclusive) {
default: LOGI("File \"%s\" opened", _path.data()); break;
case FileAccessMode::Write: LOGI("File \"%s\" opened for write", _path.data()); break;
case FileAccessMode::Read | FileAccessMode::Write: LOGI("File \"%s\" opened for read+write", _path.data()); break;
Expand Down
164 changes: 133 additions & 31 deletions Sources/Shared/IO/FileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,20 @@ namespace Death { namespace IO {
}
#endif
}

bool FileSystem::Directory::IsValid() const
{
#if defined(DEATH_TARGET_WINDOWS)
return (_hFindFile != NULL && _hFindFile != INVALID_HANDLE_VALUE);
#else
# if defined(DEATH_TARGET_ANDROID)
if (_assetDir != nullptr) {
return true;
}
# endif
return (_dirStream != nullptr);
#endif
}

#if !defined(DEATH_TARGET_WINDOWS) && !defined(DEATH_TARGET_SWITCH)
String FileSystem::FindPathCaseInsensitive(const StringView path)
Expand Down Expand Up @@ -1007,6 +1021,11 @@ namespace Death { namespace IO {
#endif
}

bool FileSystem::IsAbsolutePath(const StringView path)
{
return GetPathRootLength(path) > 0;
}

String FileSystem::GetExecutablePath()
{
#if defined(DEATH_TARGET_EMSCRIPTEN)
Expand Down Expand Up @@ -1113,23 +1132,23 @@ namespace Death { namespace IO {
#if defined(DEATH_TARGET_ANDROID)
String FileSystem::GetExternalStorage()
{
const char* extStorage = ::getenv("EXTERNAL_STORAGE");
if (extStorage == nullptr || extStorage[0] == '\0') {
return "/sdcard"_s;
StringView extStorage = ::getenv("EXTERNAL_STORAGE");
if (!extStorage.empty()) {
return extStorage;
}
return extStorage;
return "/sdcard"_s;
}
#elif defined(DEATH_TARGET_UNIX)
String FileSystem::GetLocalStorage()
{
const char* localStorage = ::getenv("XDG_DATA_HOME");
if (localStorage != nullptr && localStorage[0] != '\0') {
StringView localStorage = ::getenv("XDG_DATA_HOME");
if (IsAbsolutePath(localStorage)) {
return localStorage;
}

// Not delegating into GetHomeDirectory() as the (admittedly rare) error message would have a confusing source
const char* home = ::getenv("HOME");
if (home != nullptr && home[0] != '\0') {
StringView home = ::getenv("HOME");
if (!home.empty()) {
return CombinePath(home, ".local/share/"_s);
}

Expand Down Expand Up @@ -1382,6 +1401,10 @@ namespace Death { namespace IO {
#elif defined(DEATH_TARGET_WINDOWS)
const DWORD attrs = ::GetFileAttributesW(Utf8::ToUtf16(path));
return (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN);
#elif defined(DEATH_TARGET_APPLE) || defined(__FreeBSD__)
auto nullTerminatedPath = String::nullTerminatedView(path);
struct stat sb;
return (::stat(nullTerminatedPath.data(), &st) == 0 && (sb.st_flags & UF_HIDDEN) == UF_HIDDEN);
#else
auto nullTerminatedPath = String::nullTerminatedView(path);
# if defined(DEATH_TARGET_ANDROID)
Expand All @@ -1406,17 +1429,15 @@ namespace Death { namespace IO {
Array<wchar_t> nullTerminatedPath = Utf8::ToUtf16(path);
WIN32_FILE_ATTRIBUTE_DATA lpFileInfo;
if (!::GetFileAttributesExFromAppW(nullTerminatedPath, GetFileExInfoStandard, &lpFileInfo)) {
return true;
return false;
}

if (hidden && (lpFileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN) {
// Adding the hidden flag
lpFileInfo.dwFileAttributes |= FILE_ATTRIBUTE_HIDDEN;
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes);
} else if (!hidden && (lpFileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) {
// Removing the hidden flag
lpFileInfo.dwFileAttributes &= ~FILE_ATTRIBUTE_HIDDEN;
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes);
if (hidden == ((lpFileInfo.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN)) {
return true;
} else if (hidden) {
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes | FILE_ATTRIBUTE_HIDDEN);
} else {
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes & ~FILE_ATTRIBUTE_HIDDEN);
}
#elif defined(DEATH_TARGET_WINDOWS)
Array<wchar_t> nullTerminatedPath = Utf8::ToUtf16(path);
Expand All @@ -1425,14 +1446,26 @@ namespace Death { namespace IO {
return false;
}

if (hidden && (attrs & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN) {
// Adding the hidden flag
attrs |= FILE_ATTRIBUTE_HIDDEN;
return ::SetFileAttributesW(nullTerminatedPath, attrs);
} else if (!hidden && (attrs & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN) {
// Removing the hidden flag
attrs &= ~FILE_ATTRIBUTE_HIDDEN;
return ::SetFileAttributesW(nullTerminatedPath, attrs);
if (hidden == ((attrs & FILE_ATTRIBUTE_HIDDEN) == FILE_ATTRIBUTE_HIDDEN)) {
return true;
} else if (hidden) {
return ::SetFileAttributesW(nullTerminatedPath, attrs | FILE_ATTRIBUTE_HIDDEN);
} else {
return ::SetFileAttributesW(nullTerminatedPath, attrs & ~FILE_ATTRIBUTE_HIDDEN);
}
#elif defined(DEATH_TARGET_APPLE) || defined(__FreeBSD__)
auto nullTerminatedPath = String::nullTerminatedView(path);
struct stat sb;
if (::stat(nullTerminatedPath.data(), &sb) != 0) {
return false;
}

if (hidden == ((sb.st_flags & UF_HIDDEN) == UF_HIDDEN)) {
return true;
} else if (hidden) {
return ::chflags(nullTerminatedPath.data(), sb.st_flags | UF_HIDDEN) == 0;
} else {
return ::chflags(nullTerminatedPath.data(), sb.st_flags & ~UF_HIDDEN) == 0;
}
#else
auto nullTerminatedPath = String::nullTerminatedView(path);
Expand Down Expand Up @@ -1461,6 +1494,75 @@ namespace Death { namespace IO {
return false;
}

bool FileSystem::IsReadOnly(const StringView path)
{
if (path.empty()) return false;

#if defined(DEATH_TARGET_WINDOWS_RT)
WIN32_FILE_ATTRIBUTE_DATA lpFileInfo;
return (::GetFileAttributesExFromAppW(Utf8::ToUtf16(path), GetFileExInfoStandard, &lpFileInfo) && (lpFileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY);
#elif defined(DEATH_TARGET_WINDOWS)
const DWORD attrs = ::GetFileAttributesW(Utf8::ToUtf16(path));
return (attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY);
#elif defined(DEATH_TARGET_APPLE) || defined(__FreeBSD__)
auto nullTerminatedPath = String::nullTerminatedView(path);
struct stat sb;
return (::stat(nullTerminatedPath.data(), &st) == 0 && (sb.st_flags & UF_IMMUTABLE) == UF_IMMUTABLE);
#else
return false;
#endif
}

bool FileSystem::SetReadOnly(const StringView path, bool readonly)
{
if (path.empty()) return false;

#if defined(DEATH_TARGET_WINDOWS_RT)
Array<wchar_t> nullTerminatedPath = Utf8::ToUtf16(path);
WIN32_FILE_ATTRIBUTE_DATA lpFileInfo;
if (!::GetFileAttributesExFromAppW(nullTerminatedPath, GetFileExInfoStandard, &lpFileInfo)) {
return false;
}

if (readonly == ((lpFileInfo.dwFileAttributes & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)) {
return true;
} else if (readonly) {
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes | FILE_ATTRIBUTE_READONLY);
} else {
return ::SetFileAttributes(nullTerminatedPath, lpFileInfo.dwFileAttributes & ~FILE_ATTRIBUTE_READONLY);
}
#elif defined(DEATH_TARGET_WINDOWS)
Array<wchar_t> nullTerminatedPath = Utf8::ToUtf16(path);
DWORD attrs = ::GetFileAttributesW(nullTerminatedPath);
if (attrs == INVALID_FILE_ATTRIBUTES) {
return false;
}

if (readonly == ((attrs & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY)) {
return true;
} else if (readonly) {
return ::SetFileAttributesW(nullTerminatedPath, attrs | FILE_ATTRIBUTE_READONLY);
} else {
return ::SetFileAttributesW(nullTerminatedPath, attrs & ~FILE_ATTRIBUTE_READONLY);
}
#elif defined(DEATH_TARGET_APPLE) || defined(__FreeBSD__)
auto nullTerminatedPath = String::nullTerminatedView(path);
struct stat sb;
if (::stat(nullTerminatedPath.data(), &sb) != 0) {
return false;
}

if (readonly == ((sb.st_flags & UF_IMMUTABLE) == UF_IMMUTABLE)) {
return true;
} else if (readonly) {
return ::chflags(nullTerminatedPath.data(), sb.st_flags | UF_IMMUTABLE) == 0;
} else {
return ::chflags(nullTerminatedPath.data(), sb.st_flags & ~UF_IMMUTABLE) == 0;
}
#endif
return false;
}

bool FileSystem::CreateDirectories(const StringView path)
{
if (path.empty()) return false;
Expand Down Expand Up @@ -2252,22 +2354,22 @@ namespace Death { namespace IO {
}
#elif defined(DEATH_TARGET_APPLE)
// Not delegating into GetHomeDirectory() as the (admittedly rare) error message would have a confusing source
const char* home = ::getenv("HOME");
if (home == nullptr) {
StringView home = ::getenv("HOME");
if (home.empty()) {
return;
}

_savePath = CombinePath({ home, "Library/Application Support"_s, applicationName });
#elif defined(DEATH_TARGET_UNIX) || defined(DEATH_TARGET_EMSCRIPTEN)
const char* config = ::getenv("XDG_CONFIG_HOME");
if (config != nullptr && config[0] != '\0') {
StringView config = ::getenv("XDG_CONFIG_HOME");
if (IsAbsolutePath(config.empty())) {
_savePath = CombinePath(config, applicationName);
return;
}

// Not delegating into GetHomeDirectory() as the (admittedly rare) error message would have a confusing source
const char* home = ::getenv("HOME");
if (home == nullptr) {
StringView home = ::getenv("HOME");
if (home.empty()) {
return;
}

Expand Down
Loading

0 comments on commit af63597

Please sign in to comment.