Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Export] Improve app / file version validation. #84296

Merged
merged 1 commit into from
Nov 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 41 additions & 17 deletions editor/export/editor_export_preset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,31 +334,55 @@ Variant EditorExportPreset::get_or_env(const StringName &p_name, const String &p
return get(p_name, r_valid);
}

_FORCE_INLINE_ bool _check_digits(const String &p_str) {
for (int i = 0; i < p_str.length(); i++) {
char32_t c = p_str.operator[](i);
if (!is_digit(c)) {
return false;
}
}
return true;
}

String EditorExportPreset::get_version(const StringName &p_preset_string, bool p_windows_version) const {
String result = get(p_preset_string);
if (result.is_empty()) {
result = GLOBAL_GET("application/config/version");

if (p_windows_version) {
// Modify version number to match Windows constraints (version numbers must have 4 components).
const PackedStringArray result_split = result.split(".");
String windows_version;
if (result_split.is_empty()) {
// Use a valid fallback if the version string is empty, as a version number must be specified.
// Split and validate version number components.
const PackedStringArray result_split = result.split(".", false);
bool valid_version = !result_split.is_empty();
for (const String &E : result_split) {
if (!_check_digits(E)) {
valid_version = false;
break;
}
}

if (valid_version) {
if (p_windows_version) {
// Modify version number to match Windows constraints (version numbers must have 4 components).
if (result_split.size() == 1) {
result = result + ".0.0.0";
} else if (result_split.size() == 2) {
result = result + ".0.0";
} else if (result_split.size() == 3) {
result = result + ".0";
} else {
result = vformat("%s.%s.%s.%s", result_split[0], result_split[1], result_split[2], result_split[3]);
}
} else {
result = String(".").join(result_split);
}
} else {
if (!result.is_empty()) {
WARN_PRINT(vformat("Invalid version number \"%s\". The version number can only contain numeric characters (0-9) and non-consecutive periods (.).", result));
}
if (p_windows_version) {
result = "1.0.0.0";
} else if (result_split.size() == 1) {
result = result + ".0.0.0";
} else if (result_split.size() == 2) {
result = result + ".0.0";
} else if (result_split.size() == 3) {
result = result + ".0";
} else {
// 4 components or more in the version string. Trim to contain only the first 4 components.
result = vformat("%s.%s.%s.%s", result_split[0] + result_split[1] + result_split[2] + result_split[3]);
result = "1.0.0";
}
} else if (result.is_empty()) {
// Use a valid fallback if the version string is empty, as a version number must be specified.
result = "1.0.0";
}
}

Expand Down