initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
58
modules/mono/utils/macos_utils.cpp
Normal file
58
modules/mono/utils/macos_utils.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
/**************************************************************************/
|
||||
/* macos_utils.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifdef MACOS_ENABLED
|
||||
|
||||
#import "macos_utils.h"
|
||||
|
||||
#include "core/string/print_string.h"
|
||||
|
||||
#import <CoreFoundation/CoreFoundation.h>
|
||||
#import <CoreServices/CoreServices.h>
|
||||
|
||||
bool macos_is_app_bundle_installed(const String &p_bundle_id) {
|
||||
CFStringRef bundle_id = CFStringCreateWithCString(nullptr, p_bundle_id.utf8().get_data(), kCFStringEncodingUTF8);
|
||||
CFArrayRef result = LSCopyApplicationURLsForBundleIdentifier(bundle_id, nullptr);
|
||||
CFRelease(bundle_id);
|
||||
|
||||
if (result) {
|
||||
if (CFArrayGetCount(result) > 0) {
|
||||
CFRelease(result);
|
||||
return true;
|
||||
} else {
|
||||
CFRelease(result);
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
39
modules/mono/utils/macos_utils.h
Normal file
39
modules/mono/utils/macos_utils.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**************************************************************************/
|
||||
/* macos_utils.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef MACOS_ENABLED
|
||||
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
bool macos_is_app_bundle_installed(const String &p_bundle_id);
|
||||
|
||||
#endif
|
69
modules/mono/utils/macros.h
Normal file
69
modules/mono/utils/macros.h
Normal file
@@ -0,0 +1,69 @@
|
||||
/**************************************************************************/
|
||||
/* macros.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#define _GD_VARNAME_CONCAT_B_(m_ignore, m_name) m_name
|
||||
#define _GD_VARNAME_CONCAT_A_(m_a, m_b, m_c) _GD_VARNAME_CONCAT_B_(hello there, m_a##m_b##m_c)
|
||||
#define _GD_VARNAME_CONCAT_(m_a, m_b, m_c) _GD_VARNAME_CONCAT_A_(m_a, m_b, m_c)
|
||||
#define GD_UNIQUE_NAME(m_name) _GD_VARNAME_CONCAT_(m_name, _, __COUNTER__)
|
||||
|
||||
// unreachable
|
||||
|
||||
#if defined(_MSC_VER)
|
||||
#define GD_UNREACHABLE() __assume(0)
|
||||
#elif defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 405
|
||||
#define GD_UNREACHABLE() __builtin_unreachable()
|
||||
#else
|
||||
#define GD_UNREACHABLE() \
|
||||
CRASH_NOW(); \
|
||||
do { \
|
||||
} while (true)
|
||||
#endif
|
||||
|
||||
namespace gdmono {
|
||||
|
||||
template <typename F>
|
||||
struct ScopeExit {
|
||||
ScopeExit(F p_exit_func) :
|
||||
exit_func(p_exit_func) {}
|
||||
~ScopeExit() { exit_func(); }
|
||||
F exit_func;
|
||||
};
|
||||
|
||||
class ScopeExitAux {
|
||||
public:
|
||||
template <typename F>
|
||||
ScopeExit<F> operator+(F p_exit_func) { return ScopeExit<F>(p_exit_func); }
|
||||
};
|
||||
} // namespace gdmono
|
||||
|
||||
#define SCOPE_EXIT \
|
||||
auto GD_UNIQUE_NAME(gd_scope_exit) = gdmono::ScopeExitAux() + [=]() -> void
|
293
modules/mono/utils/naming_utils.cpp
Normal file
293
modules/mono/utils/naming_utils.cpp
Normal file
@@ -0,0 +1,293 @@
|
||||
/**************************************************************************/
|
||||
/* naming_utils.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "naming_utils.h"
|
||||
|
||||
#include "core/string/ucaps.h"
|
||||
#include "core/templates/hash_map.h"
|
||||
|
||||
HashMap<String, String> _create_hashmap_from_vector(Vector<Pair<String, String>> vector) {
|
||||
HashMap<String, String> hashmap = HashMap<String, String>(vector.size());
|
||||
for (const Pair<String, String> &pair : vector) {
|
||||
hashmap.insert(pair.first, pair.second);
|
||||
}
|
||||
return hashmap;
|
||||
}
|
||||
|
||||
// Hardcoded collection of PascalCase name conversions.
|
||||
const HashMap<String, String> pascal_case_name_overrides = _create_hashmap_from_vector({
|
||||
{ "BitMap", "Bitmap" },
|
||||
{ "JSONRPC", "JsonRpc" },
|
||||
{ "Object", "GodotObject" },
|
||||
{ "OpenXRIPBinding", "OpenXRIPBinding" },
|
||||
{ "SkeletonModification2DCCDIK", "SkeletonModification2DCcdik" },
|
||||
{ "SkeletonModification2DFABRIK", "SkeletonModification2DFabrik" },
|
||||
{ "SkeletonModification3DCCDIK", "SkeletonModification3DCcdik" },
|
||||
{ "SkeletonModification3DFABRIK", "SkeletonModification3DFabrik" },
|
||||
{ "System", "System_" },
|
||||
{ "Thread", "GodotThread" },
|
||||
});
|
||||
|
||||
// Hardcoded collection of PascalCase part conversions.
|
||||
const HashMap<String, String> pascal_case_part_overrides = _create_hashmap_from_vector({
|
||||
{ "AA", "AA" }, // Anti Aliasing
|
||||
{ "AO", "AO" }, // Ambient Occlusion
|
||||
{ "FILENAME", "FileName" },
|
||||
{ "FADEIN", "FadeIn" },
|
||||
{ "FADEOUT", "FadeOut" },
|
||||
{ "FX", "FX" },
|
||||
{ "GI", "GI" }, // Global Illumination
|
||||
{ "GZIP", "GZip" },
|
||||
{ "HBOX", "HBox" }, // Horizontal Box
|
||||
{ "ID", "Id" },
|
||||
{ "IO", "IO" }, // Input/Output
|
||||
{ "IP", "IP" }, // Internet Protocol
|
||||
{ "IV", "IV" }, // Initialization Vector
|
||||
{ "MACOS", "MacOS" },
|
||||
{ "NODEPATH", "NodePath" },
|
||||
{ "SPIRV", "SpirV" },
|
||||
{ "STDIN", "StdIn" },
|
||||
{ "STDOUT", "StdOut" },
|
||||
{ "USERNAME", "UserName" },
|
||||
{ "UV", "UV" },
|
||||
{ "UV2", "UV2" },
|
||||
{ "VBOX", "VBox" }, // Vertical Box
|
||||
{ "WHITESPACE", "WhiteSpace" },
|
||||
{ "WM", "WM" },
|
||||
{ "XR", "XR" },
|
||||
{ "XRAPI", "XRApi" },
|
||||
});
|
||||
|
||||
String _get_pascal_case_part_override(String p_part, bool p_input_is_upper = true) {
|
||||
if (!p_input_is_upper) {
|
||||
for (int i = 0; i < p_part.length(); i++) {
|
||||
p_part[i] = _find_upper(p_part[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (pascal_case_part_overrides.has(p_part)) {
|
||||
return pascal_case_part_overrides.get(p_part);
|
||||
}
|
||||
|
||||
return String();
|
||||
}
|
||||
|
||||
Vector<String> _split_pascal_case(const String &p_identifier) {
|
||||
Vector<String> parts;
|
||||
int current_part_start = 0;
|
||||
bool prev_was_upper = is_ascii_upper_case(p_identifier[0]);
|
||||
for (int i = 1; i < p_identifier.length(); i++) {
|
||||
if (prev_was_upper) {
|
||||
if (is_digit(p_identifier[i]) || is_ascii_lower_case(p_identifier[i])) {
|
||||
if (!is_digit(p_identifier[i])) {
|
||||
// These conditions only apply when the separator is not a digit.
|
||||
if (i - current_part_start == 1) {
|
||||
// Upper character was only the beginning of a word.
|
||||
prev_was_upper = false;
|
||||
continue;
|
||||
}
|
||||
if (i != p_identifier.length()) {
|
||||
// If this is not the last character, the last uppercase
|
||||
// character is the start of the next word.
|
||||
i--;
|
||||
}
|
||||
}
|
||||
if (i - current_part_start > 0) {
|
||||
parts.append(p_identifier.substr(current_part_start, i - current_part_start));
|
||||
current_part_start = i;
|
||||
prev_was_upper = false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (is_digit(p_identifier[i]) || is_ascii_upper_case(p_identifier[i])) {
|
||||
parts.append(p_identifier.substr(current_part_start, i - current_part_start));
|
||||
current_part_start = i;
|
||||
prev_was_upper = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the rest of the identifier as the last part.
|
||||
if (current_part_start != p_identifier.length()) {
|
||||
parts.append(p_identifier.substr(current_part_start));
|
||||
}
|
||||
|
||||
return parts;
|
||||
}
|
||||
|
||||
String pascal_to_pascal_case(const String &p_identifier) {
|
||||
if (p_identifier.length() == 0) {
|
||||
return p_identifier;
|
||||
}
|
||||
|
||||
if (p_identifier.length() <= 2) {
|
||||
return p_identifier.to_upper();
|
||||
}
|
||||
|
||||
if (pascal_case_name_overrides.has(p_identifier)) {
|
||||
// Use hardcoded value for the identifier.
|
||||
return pascal_case_name_overrides.get(p_identifier);
|
||||
}
|
||||
|
||||
Vector<String> parts = _split_pascal_case(p_identifier);
|
||||
|
||||
String ret;
|
||||
|
||||
for (String &part : parts) {
|
||||
String part_override = _get_pascal_case_part_override(part);
|
||||
if (!part_override.is_empty()) {
|
||||
// Use hardcoded value for part.
|
||||
ret += part_override;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (part.length() <= 2 && part.to_upper() == part) {
|
||||
// Acronym of length 1 or 2.
|
||||
for (int j = 0; j < part.length(); j++) {
|
||||
part[j] = _find_upper(part[j]);
|
||||
}
|
||||
ret += part;
|
||||
continue;
|
||||
}
|
||||
|
||||
part[0] = _find_upper(part[0]);
|
||||
for (int i = 1; i < part.length(); i++) {
|
||||
if (is_digit(part[i - 1])) {
|
||||
// Use uppercase after digits.
|
||||
part[i] = _find_upper(part[i]);
|
||||
continue;
|
||||
}
|
||||
|
||||
part[i] = _find_lower(part[i]);
|
||||
}
|
||||
ret += part;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
String snake_to_pascal_case(const String &p_identifier, bool p_input_is_upper) {
|
||||
String ret;
|
||||
Vector<String> parts = p_identifier.split("_", true);
|
||||
|
||||
for (int i = 0; i < parts.size(); i++) {
|
||||
String part = parts[i];
|
||||
|
||||
String part_override = _get_pascal_case_part_override(part, p_input_is_upper);
|
||||
if (!part_override.is_empty()) {
|
||||
// Use hardcoded value for part.
|
||||
ret += part_override;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!part.is_empty()) {
|
||||
part[0] = _find_upper(part[0]);
|
||||
for (int j = 1; j < part.length(); j++) {
|
||||
if (is_digit(part[j - 1])) {
|
||||
// Use uppercase after digits.
|
||||
part[j] = _find_upper(part[j]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p_input_is_upper) {
|
||||
part[j] = _find_lower(part[j]);
|
||||
}
|
||||
}
|
||||
ret += part;
|
||||
} else {
|
||||
if (i == 0 || i == (parts.size() - 1)) {
|
||||
// Preserve underscores at the beginning and end
|
||||
ret += "_";
|
||||
} else {
|
||||
// Preserve contiguous underscores
|
||||
if (parts[i - 1].length()) {
|
||||
ret += "__";
|
||||
} else {
|
||||
ret += "_";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
String snake_to_camel_case(const String &p_identifier, bool p_input_is_upper) {
|
||||
String ret;
|
||||
Vector<String> parts = p_identifier.split("_", true);
|
||||
|
||||
for (int i = 0; i < parts.size(); i++) {
|
||||
String part = parts[i];
|
||||
|
||||
String part_override = _get_pascal_case_part_override(part, p_input_is_upper);
|
||||
if (!part_override.is_empty()) {
|
||||
// Use hardcoded value for part.
|
||||
if (i == 0) {
|
||||
part_override[0] = _find_lower(part_override[0]);
|
||||
}
|
||||
ret += part_override;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!part.is_empty()) {
|
||||
if (i == 0) {
|
||||
part[0] = _find_lower(part[0]);
|
||||
} else {
|
||||
part[0] = _find_upper(part[0]);
|
||||
}
|
||||
for (int j = 1; j < part.length(); j++) {
|
||||
if (is_digit(part[j - 1])) {
|
||||
// Use uppercase after digits.
|
||||
part[j] = _find_upper(part[j]);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (p_input_is_upper) {
|
||||
part[j] = _find_lower(part[j]);
|
||||
}
|
||||
}
|
||||
ret += part;
|
||||
} else {
|
||||
if (i == 0 || i == (parts.size() - 1)) {
|
||||
// Preserve underscores at the beginning and end
|
||||
ret += "_";
|
||||
} else {
|
||||
// Preserve contiguous underscores
|
||||
if (parts[i - 1].length()) {
|
||||
ret += "__";
|
||||
} else {
|
||||
ret += "_";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
39
modules/mono/utils/naming_utils.h
Normal file
39
modules/mono/utils/naming_utils.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/**************************************************************************/
|
||||
/* naming_utils.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
String pascal_to_pascal_case(const String &p_identifier);
|
||||
|
||||
String snake_to_pascal_case(const String &p_identifier, bool p_input_is_upper = false);
|
||||
|
||||
String snake_to_camel_case(const String &p_identifier, bool p_input_is_upper = false);
|
264
modules/mono/utils/path_utils.cpp
Normal file
264
modules/mono/utils/path_utils.cpp
Normal file
@@ -0,0 +1,264 @@
|
||||
/**************************************************************************/
|
||||
/* path_utils.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "path_utils.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "core/io/dir_access.h"
|
||||
#include "core/io/file_access.h"
|
||||
#include "core/os/os.h"
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#ifdef WINDOWS_ENABLED
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
#define ENV_PATH_SEP ";"
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <climits>
|
||||
|
||||
#define ENV_PATH_SEP ":"
|
||||
#endif
|
||||
|
||||
namespace Path {
|
||||
|
||||
String find_executable(const String &p_name) {
|
||||
#ifdef WINDOWS_ENABLED
|
||||
Vector<String> exts = OS::get_singleton()->get_environment("PATHEXT").split(ENV_PATH_SEP, false);
|
||||
#endif
|
||||
Vector<String> env_path = OS::get_singleton()->get_environment("PATH").split(ENV_PATH_SEP, false);
|
||||
|
||||
if (env_path.is_empty()) {
|
||||
return String();
|
||||
}
|
||||
|
||||
for (int i = 0; i < env_path.size(); i++) {
|
||||
String p = Path::join(env_path[i], p_name);
|
||||
|
||||
#ifdef WINDOWS_ENABLED
|
||||
for (int j = 0; j < exts.size(); j++) {
|
||||
String p2 = p + exts[j].to_lower(); // lowercase to reduce risk of case mismatch warning
|
||||
|
||||
if (FileAccess::exists(p2)) {
|
||||
return p2;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (FileAccess::exists(p)) {
|
||||
return p;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
return String();
|
||||
}
|
||||
|
||||
String cwd() {
|
||||
#ifdef WINDOWS_ENABLED
|
||||
const DWORD expected_size = ::GetCurrentDirectoryW(0, nullptr);
|
||||
|
||||
Char16String buffer;
|
||||
buffer.resize_uninitialized((int)expected_size);
|
||||
if (::GetCurrentDirectoryW(expected_size, (wchar_t *)buffer.ptrw()) == 0) {
|
||||
return ".";
|
||||
}
|
||||
|
||||
String result = String::utf16(buffer.ptr());
|
||||
if (result.is_empty()) {
|
||||
return ".";
|
||||
}
|
||||
return result.simplify_path();
|
||||
#else
|
||||
char buffer[PATH_MAX];
|
||||
if (::getcwd(buffer, sizeof(buffer)) == nullptr) {
|
||||
return ".";
|
||||
}
|
||||
|
||||
String result;
|
||||
if (result.append_utf8(buffer) != OK) {
|
||||
return ".";
|
||||
}
|
||||
|
||||
return result.simplify_path();
|
||||
#endif
|
||||
}
|
||||
|
||||
String abspath(const String &p_path) {
|
||||
if (p_path.is_absolute_path()) {
|
||||
return p_path.simplify_path();
|
||||
} else {
|
||||
return Path::join(Path::cwd(), p_path).simplify_path();
|
||||
}
|
||||
}
|
||||
|
||||
String realpath(const String &p_path) {
|
||||
#ifdef WINDOWS_ENABLED
|
||||
// Open file without read/write access
|
||||
HANDLE hFile = ::CreateFileW((LPCWSTR)(p_path.utf16().get_data()), 0,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
|
||||
nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
|
||||
|
||||
if (hFile == INVALID_HANDLE_VALUE) {
|
||||
return p_path;
|
||||
}
|
||||
|
||||
const DWORD expected_size = ::GetFinalPathNameByHandleW(hFile, nullptr, 0, FILE_NAME_NORMALIZED);
|
||||
|
||||
if (expected_size == 0) {
|
||||
::CloseHandle(hFile);
|
||||
return p_path;
|
||||
}
|
||||
|
||||
Char16String buffer;
|
||||
buffer.resize_uninitialized((int)expected_size);
|
||||
::GetFinalPathNameByHandleW(hFile, (wchar_t *)buffer.ptrw(), expected_size, FILE_NAME_NORMALIZED);
|
||||
|
||||
::CloseHandle(hFile);
|
||||
|
||||
String result = String::utf16(buffer.ptr());
|
||||
if (result.is_empty()) {
|
||||
return p_path;
|
||||
}
|
||||
|
||||
return result.simplify_path();
|
||||
#elif defined(UNIX_ENABLED)
|
||||
char *resolved_path = ::realpath(p_path.utf8().get_data(), nullptr);
|
||||
|
||||
if (!resolved_path) {
|
||||
return p_path;
|
||||
}
|
||||
|
||||
String result;
|
||||
Error parse_ok = result.append_utf8(resolved_path);
|
||||
::free(resolved_path);
|
||||
|
||||
if (parse_ok != OK) {
|
||||
return p_path;
|
||||
}
|
||||
|
||||
return result.simplify_path();
|
||||
#endif
|
||||
}
|
||||
|
||||
String join(const String &p_a, const String &p_b) {
|
||||
if (p_a.is_empty()) {
|
||||
return p_b;
|
||||
}
|
||||
|
||||
const char32_t a_last = p_a[p_a.length() - 1];
|
||||
if ((a_last == '/' || a_last == '\\') ||
|
||||
(p_b.size() > 0 && (p_b[0] == '/' || p_b[0] == '\\'))) {
|
||||
return p_a + p_b;
|
||||
}
|
||||
|
||||
return p_a + "/" + p_b;
|
||||
}
|
||||
|
||||
String join(const String &p_a, const String &p_b, const String &p_c) {
|
||||
return Path::join(Path::join(p_a, p_b), p_c);
|
||||
}
|
||||
|
||||
String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d) {
|
||||
return Path::join(Path::join(Path::join(p_a, p_b), p_c), p_d);
|
||||
}
|
||||
|
||||
String relative_to_impl(const String &p_path, const String &p_relative_to) {
|
||||
// This function assumes arguments are normalized and absolute paths
|
||||
|
||||
if (p_path.begins_with(p_relative_to)) {
|
||||
return p_path.substr(p_relative_to.length() + 1);
|
||||
} else {
|
||||
String base_dir = p_relative_to.get_base_dir();
|
||||
|
||||
if (base_dir.length() <= 2 && (base_dir.is_empty() || base_dir.ends_with(":"))) {
|
||||
return p_path;
|
||||
}
|
||||
|
||||
return String("..").path_join(relative_to_impl(p_path, base_dir));
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef WINDOWS_ENABLED
|
||||
String get_drive_letter(const String &p_norm_path) {
|
||||
int idx = p_norm_path.find(":/");
|
||||
if (idx != -1 && idx < p_norm_path.find_char('/')) {
|
||||
return p_norm_path.substr(0, idx + 1);
|
||||
}
|
||||
return String();
|
||||
}
|
||||
#endif
|
||||
|
||||
String relative_to(const String &p_path, const String &p_relative_to) {
|
||||
String relative_to_abs_norm = abspath(p_relative_to);
|
||||
String path_abs_norm = abspath(p_path);
|
||||
|
||||
#ifdef WINDOWS_ENABLED
|
||||
if (get_drive_letter(relative_to_abs_norm) != get_drive_letter(path_abs_norm)) {
|
||||
return path_abs_norm;
|
||||
}
|
||||
#endif
|
||||
|
||||
return relative_to_impl(path_abs_norm, relative_to_abs_norm);
|
||||
}
|
||||
|
||||
const Vector<String> reserved_assembly_names = { "GodotSharp", "GodotSharpEditor", "Godot.SourceGenerators" };
|
||||
|
||||
String get_csharp_project_name() {
|
||||
String name = GLOBAL_GET("dotnet/project/assembly_name");
|
||||
if (name.is_empty()) {
|
||||
name = GLOBAL_GET("application/config/name");
|
||||
Vector<String> invalid_chars = Vector<String>({ //
|
||||
// Windows reserved filename chars.
|
||||
":", "*", "?", "\"", "<", ">", "|",
|
||||
// Directory separators.
|
||||
"/", "\\",
|
||||
// Other chars that have been found to break assembly loading.
|
||||
";", "'", "=", "," });
|
||||
name = name.strip_edges();
|
||||
for (int i = 0; i < invalid_chars.size(); i++) {
|
||||
name = name.replace(invalid_chars[i], "-");
|
||||
}
|
||||
}
|
||||
|
||||
if (name.is_empty()) {
|
||||
name = "UnnamedProject";
|
||||
}
|
||||
|
||||
// Avoid reserved names that conflict with Godot assemblies.
|
||||
if (reserved_assembly_names.has(name)) {
|
||||
name += "_";
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace Path
|
61
modules/mono/utils/path_utils.h
Normal file
61
modules/mono/utils/path_utils.h
Normal file
@@ -0,0 +1,61 @@
|
||||
/**************************************************************************/
|
||||
/* path_utils.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/string/ustring.h"
|
||||
|
||||
namespace Path {
|
||||
|
||||
String find_executable(const String &p_name);
|
||||
|
||||
String join(const String &p_a, const String &p_b);
|
||||
String join(const String &p_a, const String &p_b, const String &p_c);
|
||||
String join(const String &p_a, const String &p_b, const String &p_c, const String &p_d);
|
||||
|
||||
/// Returns a normalized absolute path to the current working directory
|
||||
String cwd();
|
||||
|
||||
/**
|
||||
* Obtains a normalized absolute path to p_path. Symbolic links are
|
||||
* not resolved. The path p_path might not exist in the file system.
|
||||
*/
|
||||
String abspath(const String &p_path);
|
||||
|
||||
/**
|
||||
* Obtains a normalized path to p_path with symbolic links resolved.
|
||||
* The resulting path might be either a relative or an absolute path.
|
||||
*/
|
||||
String realpath(const String &p_path);
|
||||
|
||||
String relative_to(const String &p_path, const String &p_relative_to);
|
||||
|
||||
String get_csharp_project_name();
|
||||
} // namespace Path
|
225
modules/mono/utils/string_utils.cpp
Normal file
225
modules/mono/utils/string_utils.cpp
Normal file
@@ -0,0 +1,225 @@
|
||||
/**************************************************************************/
|
||||
/* string_utils.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "string_utils.h"
|
||||
|
||||
#include "core/io/file_access.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
|
||||
namespace {
|
||||
|
||||
int sfind(const String &p_text, int p_from) {
|
||||
if (p_from < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int src_len = 2;
|
||||
int len = p_text.length();
|
||||
|
||||
if (len == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char32_t *src = p_text.get_data();
|
||||
|
||||
for (int i = p_from; i <= (len - src_len); i++) {
|
||||
bool found = true;
|
||||
|
||||
for (int j = 0; j < src_len; j++) {
|
||||
int read_pos = i + j;
|
||||
|
||||
ERR_FAIL_COND_V(read_pos >= len, -1);
|
||||
|
||||
switch (j) {
|
||||
case 0:
|
||||
found = src[read_pos] == '%';
|
||||
break;
|
||||
case 1: {
|
||||
char32_t c = src[read_pos];
|
||||
found = src[read_pos] == 's' || (c >= '0' && c <= '5');
|
||||
break;
|
||||
}
|
||||
default:
|
||||
found = false;
|
||||
}
|
||||
|
||||
if (!found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (found) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
String sformat(const String &p_text, const String &p1, const String &p2,
|
||||
const String &p3, const String &p4, const String &p5, const String &p6) {
|
||||
if (p_text.length() < 2) {
|
||||
return p_text;
|
||||
}
|
||||
|
||||
String args[6] = { p1, p2, p3, p4, p5, p6 };
|
||||
|
||||
String new_string;
|
||||
|
||||
int findex = 0;
|
||||
int search_from = 0;
|
||||
int result = 0;
|
||||
|
||||
while ((result = sfind(p_text, search_from)) >= 0) {
|
||||
char32_t c = p_text[result + 1];
|
||||
|
||||
int req_index = (c == 's' ? findex++ : c - '0');
|
||||
|
||||
new_string += p_text.substr(search_from, result - search_from);
|
||||
new_string += args[req_index];
|
||||
search_from = result + 2;
|
||||
}
|
||||
|
||||
new_string += p_text.substr(search_from);
|
||||
|
||||
return new_string;
|
||||
}
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
bool is_csharp_keyword(const String &p_name) {
|
||||
// Reserved keywords
|
||||
|
||||
return p_name == "abstract" || p_name == "as" || p_name == "base" || p_name == "bool" ||
|
||||
p_name == "break" || p_name == "byte" || p_name == "case" || p_name == "catch" ||
|
||||
p_name == "char" || p_name == "checked" || p_name == "class" || p_name == "const" ||
|
||||
p_name == "continue" || p_name == "decimal" || p_name == "default" || p_name == "delegate" ||
|
||||
p_name == "do" || p_name == "double" || p_name == "else" || p_name == "enum" ||
|
||||
p_name == "event" || p_name == "explicit" || p_name == "extern" || p_name == "false" ||
|
||||
p_name == "finally" || p_name == "fixed" || p_name == "float" || p_name == "for" ||
|
||||
p_name == "foreach" || p_name == "goto" || p_name == "if" || p_name == "implicit" ||
|
||||
p_name == "in" || p_name == "int" || p_name == "interface" || p_name == "internal" ||
|
||||
p_name == "is" || p_name == "lock" || p_name == "long" || p_name == "namespace" ||
|
||||
p_name == "new" || p_name == "null" || p_name == "object" || p_name == "operator" ||
|
||||
p_name == "out" || p_name == "override" || p_name == "params" || p_name == "private" ||
|
||||
p_name == "protected" || p_name == "public" || p_name == "readonly" || p_name == "ref" ||
|
||||
p_name == "return" || p_name == "sbyte" || p_name == "sealed" || p_name == "short" ||
|
||||
p_name == "sizeof" || p_name == "stackalloc" || p_name == "static" || p_name == "string" ||
|
||||
p_name == "struct" || p_name == "switch" || p_name == "this" || p_name == "throw" ||
|
||||
p_name == "true" || p_name == "try" || p_name == "typeof" || p_name == "uint" || p_name == "ulong" ||
|
||||
p_name == "unchecked" || p_name == "unsafe" || p_name == "ushort" || p_name == "using" ||
|
||||
p_name == "virtual" || p_name == "volatile" || p_name == "void" || p_name == "while";
|
||||
}
|
||||
|
||||
String escape_csharp_keyword(const String &p_name) {
|
||||
return is_csharp_keyword(p_name) ? "@" + p_name : p_name;
|
||||
}
|
||||
#endif
|
||||
|
||||
Error read_all_file_utf8(const String &p_path, String &r_content) {
|
||||
Vector<uint8_t> sourcef;
|
||||
Error err;
|
||||
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ, &err);
|
||||
ERR_FAIL_COND_V_MSG(err != OK, err, "Cannot open file '" + p_path + "'.");
|
||||
|
||||
uint64_t len = f->get_length();
|
||||
sourcef.resize(len + 1);
|
||||
uint8_t *w = sourcef.ptrw();
|
||||
uint64_t r = f->get_buffer(w, len);
|
||||
ERR_FAIL_COND_V(r != len, ERR_CANT_OPEN);
|
||||
w[len] = 0;
|
||||
|
||||
String source;
|
||||
if (source.append_utf8((const char *)w, len) != OK) {
|
||||
ERR_FAIL_V(ERR_INVALID_DATA);
|
||||
}
|
||||
|
||||
r_content = source;
|
||||
return OK;
|
||||
}
|
||||
|
||||
// TODO: Move to variadic templates once we upgrade to C++11
|
||||
|
||||
String str_format(const char *p_format, ...) {
|
||||
va_list list;
|
||||
|
||||
va_start(list, p_format);
|
||||
String res = str_format(p_format, list);
|
||||
va_end(list);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
#if defined(MINGW_ENABLED)
|
||||
#define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf_s(m_buffer, m_count, _TRUNCATE, m_format, m_args_copy)
|
||||
#define RScprintf(m_format, m_args_copy) _vscprintf(m_format, m_args_copy)
|
||||
#else
|
||||
#define RSnprintf(m_buffer, m_count, m_format, m_args_copy) vsnprintf(m_buffer, m_count, m_format, m_args_copy)
|
||||
#define RScprintf(m_format, m_args_copy) vsnprintf(nullptr, 0, p_format, m_args_copy)
|
||||
#endif
|
||||
|
||||
String str_format(const char *p_format, va_list p_list) {
|
||||
char *buffer = str_format_new(p_format, p_list);
|
||||
|
||||
String res(buffer);
|
||||
memdelete_arr(buffer);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
char *str_format_new(const char *p_format, ...) {
|
||||
va_list list;
|
||||
|
||||
va_start(list, p_format);
|
||||
char *res = str_format_new(p_format, list);
|
||||
va_end(list);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
char *str_format_new(const char *p_format, va_list p_list) {
|
||||
va_list list;
|
||||
|
||||
va_copy(list, p_list);
|
||||
int len = RScprintf(p_format, list);
|
||||
va_end(list);
|
||||
|
||||
len += 1; // for the trailing '/0'
|
||||
|
||||
char *buffer(memnew_arr(char, len));
|
||||
|
||||
va_copy(list, p_list);
|
||||
RSnprintf(buffer, len, p_format, list);
|
||||
va_end(list);
|
||||
|
||||
return buffer;
|
||||
}
|
60
modules/mono/utils/string_utils.h
Normal file
60
modules/mono/utils/string_utils.h
Normal file
@@ -0,0 +1,60 @@
|
||||
/**************************************************************************/
|
||||
/* string_utils.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "core/string/ustring.h"
|
||||
#include "core/variant/variant.h"
|
||||
|
||||
#include <cstdarg>
|
||||
|
||||
String sformat(const String &p_text, const String &p1 = String(), const String &p2 = String(),
|
||||
const String &p3 = String(), const String &p4 = String(), const String &p5 = String(), const String &p6 = String());
|
||||
|
||||
#ifdef TOOLS_ENABLED
|
||||
bool is_csharp_keyword(const String &p_name);
|
||||
|
||||
String escape_csharp_keyword(const String &p_name);
|
||||
#endif
|
||||
|
||||
Error read_all_file_utf8(const String &p_path, String &r_content);
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#define _PRINTF_FORMAT_ATTRIBUTE_1_0 __attribute__((format(printf, 1, 0)))
|
||||
#define _PRINTF_FORMAT_ATTRIBUTE_1_2 __attribute__((format(printf, 1, 2)))
|
||||
#else
|
||||
#define _PRINTF_FORMAT_ATTRIBUTE_1_0
|
||||
#define _PRINTF_FORMAT_ATTRIBUTE_1_2
|
||||
#endif
|
||||
|
||||
String str_format(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_1_2;
|
||||
String str_format(const char *p_format, va_list p_list) _PRINTF_FORMAT_ATTRIBUTE_1_0;
|
||||
char *str_format_new(const char *p_format, ...) _PRINTF_FORMAT_ATTRIBUTE_1_2;
|
||||
char *str_format_new(const char *p_format, va_list p_list) _PRINTF_FORMAT_ATTRIBUTE_1_0;
|
Reference in New Issue
Block a user