Skip to content

Commit

Permalink
Merge pull request #84296 from bruvzg/ver_validate
Browse files Browse the repository at this point in the history
[Export] Improve app / file version validation.
  • Loading branch information
akien-mga committed Nov 1, 2023
2 parents 8a403e7 + a0bd5f8 commit 2d64e52
Showing 1 changed file with 41 additions and 17 deletions.
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

0 comments on commit 2d64e52

Please sign in to comment.