diff options
| author | qwasdrizzel <145519042+qwasdrizzel@users.noreply.github.com> | 2026-03-16 21:44:26 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-03-16 21:44:26 -0500 |
| commit | ce739f6045ec72127491286ea3f3f21e537c1b55 (patch) | |
| tree | f33bd42a47c1b4a7b2153a7fb77127ee3b407db9 /Minecraft.Server/Common/AccessStorageUtils.h | |
| parent | 255a18fe8e9b57377975f82e2b227afe2a12eda0 (diff) | |
| parent | 5a59f5d146b43811dde6a5a0245ee9875d7b5cd1 (diff) | |
Merge branch 'smartcmd:main' into main
Diffstat (limited to 'Minecraft.Server/Common/AccessStorageUtils.h')
| -rw-r--r-- | Minecraft.Server/Common/AccessStorageUtils.h | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/Minecraft.Server/Common/AccessStorageUtils.h b/Minecraft.Server/Common/AccessStorageUtils.h new file mode 100644 index 00000000..c5d3477c --- /dev/null +++ b/Minecraft.Server/Common/AccessStorageUtils.h @@ -0,0 +1,105 @@ +#pragma once + +#include "FileUtils.h" +#include "StringUtils.h" + +#include "..\vendor\nlohmann\json.hpp" + +#include <stdio.h> + +namespace ServerRuntime +{ + namespace AccessStorageUtils + { + inline bool IsRegularFile(const std::string &path) + { + const std::wstring widePath = StringUtils::Utf8ToWide(path); + if (widePath.empty()) + { + return false; + } + + const DWORD attributes = GetFileAttributesW(widePath.c_str()); + return (attributes != INVALID_FILE_ATTRIBUTES) && ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0); + } + + inline bool EnsureJsonListFileExists(const std::string &path) + { + return IsRegularFile(path) || FileUtils::WriteTextFileAtomic(path, "[]\n"); + } + + inline bool TryGetStringField(const nlohmann::ordered_json &object, const char *key, std::string *outValue) + { + if (key == nullptr || outValue == nullptr || !object.is_object()) + { + return false; + } + + const auto it = object.find(key); + if (it == object.end() || !it->is_string()) + { + return false; + } + + *outValue = it->get<std::string>(); + return true; + } + + inline std::string NormalizeXuid(const std::string &xuid) + { + const std::string trimmed = StringUtils::TrimAscii(xuid); + if (trimmed.empty()) + { + return ""; + } + + unsigned long long numericXuid = 0; + if (StringUtils::TryParseUnsignedLongLong(trimmed, &numericXuid)) + { + if (numericXuid == 0ULL) + { + return ""; + } + + char buffer[32] = {}; + sprintf_s(buffer, sizeof(buffer), "0x%016llx", numericXuid); + return buffer; + } + + return StringUtils::ToLowerAscii(trimmed); + } + + inline std::string BuildPathFromBaseDirectory(const std::string &baseDirectory, const char *fileName) + { + if (fileName == nullptr || fileName[0] == 0) + { + return ""; + } + + const std::wstring wideFileName = StringUtils::Utf8ToWide(fileName); + if (wideFileName.empty()) + { + return ""; + } + + if (baseDirectory.empty() || baseDirectory == ".") + { + return StringUtils::WideToUtf8(wideFileName); + } + + const std::wstring wideBaseDirectory = StringUtils::Utf8ToWide(baseDirectory); + if (wideBaseDirectory.empty()) + { + return StringUtils::WideToUtf8(wideFileName); + } + + const wchar_t last = wideBaseDirectory[wideBaseDirectory.size() - 1]; + if (last == L'\\' || last == L'/') + { + return StringUtils::WideToUtf8(wideBaseDirectory + wideFileName); + } + + return StringUtils::WideToUtf8(wideBaseDirectory + L"\\" + wideFileName); + } + } +} |
