aboutsummaryrefslogtreecommitdiff
path: root/Minecraft.Server/Common/AccessStorageUtils.h
blob: c5d3477c7003447fb013fa19a29713a92eba2ebf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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);
		}
	}
}