Skip to content

Commit

Permalink
updated update checker
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriySalnikov committed Jul 31, 2023
1 parent 00ebe2a commit 2134d0d
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 17 deletions.
79 changes: 63 additions & 16 deletions src/asset_library_update_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,66 @@ void AssetLibraryUpdateChecker::request_completed(int result, int response_code,
Dictionary dict = JSON::parse_string(body.get_string_from_utf8());
String ver = dict["version_string"];
String download_url = dict["download_url"];
Array split = ver.split(".");
String addon_godot_version = dict["godot_version"];

int ver_code = 0;
{
Array split = ver.split(".");
if (split.size() >= 1 && split.size() <= 4) {
for (int i = 0; i < split.size(); i++) {
String str = (String)split[i];
if (str.is_valid_int()) {
char num = (char)str.to_int();
ver_code += num << (3 - i) * 8;
} else {
break;
}
}
}
}

int ver_num = 0;
if (split.size() >= 1 && split.size() <= 4) {
for (int i = 0; i < split.size(); i++) {
char num = (char)((String)split[i]).to_int();
ver_num += num << (3 - i) * 8;
int addon_ver_code = 0;
int godot_ver_code = 0;
String godot_ver_str = "";
{
Dictionary godot_ver = Engine::get_singleton()->get_version_info();
godot_ver_code = (((char)(int)godot_ver["major"]) << 3 * 8) + (((char)(int)godot_ver["minor"]) << 2 * 8) + (((char)(int)godot_ver["patch"]) << 1 * 8);
godot_ver_str = godot_ver["major"].stringify() + "." + godot_ver["minor"].stringify() + "." + godot_ver["patch"].stringify();

Array godot_split = addon_godot_version.split(".");
if (godot_split.size() >= 1 && godot_split.size() <= 4) {
for (int i = 0; i < godot_split.size(); i++) {
String str = (String)godot_split[i];
if (str.is_valid_int()) {
char num = (char)str.to_int();
addon_ver_code += num << (3 - i) * 8;
} else {
break;
}
}
}
}

if (ver_num > DD3D_VERSION) {
PRINT(String("=====\n") +
"An update for " + addon_name + " is available. Current: " + DD3D_VERSION_STR + " -> New: " + ver + "\n" +
"It will not be possible to update the addon through the editor, since its files are currently locked.\n" +
"You need to download the archive from this link (select the URL below, click copy and paste this address into your browser):\n" + download_url + "\n" +
"Then close the editor and replace the files in your project with new ones from the archive\n" +
"(In the archive, first go inside the " + repository_name + "-XXXXX... folder and extract its contents).\n" +
"To disable this alert, go to the Project Settings in the \"" + root_settings_section + "\" section\n"+
"=====");
bool is_godot_version_incompatible = addon_ver_code > godot_ver_code;

if (ver_code > DD3D_VERSION) {
String warning = String("=====\n") +
"An update for [b]" + addon_name + "[/b] is available. Current: [i]" + DD3D_VERSION_STR + "[/i] -> New: [i]" + ver + "[/i]\n";

if (is_godot_version_incompatible) {
warning += "This version of Godot ([i]" + godot_ver_str + "[/i]) is not compatible with the new version of the addon (requires [i]" + addon_godot_version + "+[/i])!\n";
} else {
warning += "It will not be possible to update the addon through the editor, since its files are currently locked.\n";
warning += "You need to download the archive from this link:\n[url]" + download_url + "[/url]\n";
warning += "Then close the editor and replace the files in your project with new ones from the archive ";
warning += "(extract the content of the [i]" + repository_name + "-*****[/i] folder).\n";
}

warning += "To disable this alert, go to the [b]Project Settings[/b] in the \"[u]" + root_settings_section + "[/u]\" section.\n";
warning += "Addon page: [url]" + godot_asset_page + String::num_int64(addon_id) + "[/url]\n";
warning += "Changes: [url]" + changes_page + "[/url]\n";
warning += "=====";
godot::UtilityFunctions::print_rich(godot::Variant(warning));
}
}

Expand All @@ -65,15 +106,21 @@ void AssetLibraryUpdateChecker::init() {
SCENE_ROOT()->add_child(request);

request->connect("request_completed", Callable(this, NAMEOF(request_completed)));
request->request("https://godotengine.org/asset-library/api/asset/1766");
request->request(godot_asset_api + String::num_int64(addon_id));
}

AssetLibraryUpdateChecker::AssetLibraryUpdateChecker() {
addon_id = 1766;
addon_name = "Debug Draw 3D";
repository_name = "godot_debug_draw_3d";
root_settings_section = "debug_draw_3d/settings/";
changes_page = "https://github.com/DmitriySalnikov/godot_debug_draw_3d/releases";

godot_asset_api = "https://godotengine.org/asset-library/api/asset/";
godot_asset_page = "https://godotengine.org/asset-library/asset/";

DEFINE_SETTING_READ_ONLY(root_settings_section + "addon_version", DD3D_VERSION_STR, Variant::STRING);
DEFINE_SETTING_READ_ONLY(root_settings_section + "addon_page", godot_asset_page + String::num_int64(addon_id), Variant::STRING);
DEFINE_SETTING_AND_GET(bool check_updates, root_settings_section + "check_for_updates", true, Variant::BOOL);

if (check_updates)
Expand Down
5 changes: 5 additions & 0 deletions src/asset_library_update_checker.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ class AssetLibraryUpdateChecker : public RefCounted {

private:
HTTPRequest *request = nullptr;
int addon_id;
String addon_name;
String repository_name;
String root_settings_section;
String changes_page;

String godot_asset_api;
String godot_asset_page;

protected:
static void _bind_methods();
Expand Down
2 changes: 1 addition & 1 deletion src/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#define DD3D_MAJOR 1
#define DD3D_MINOR 1
#define DD3D_PATCH 2
#define DD3D_PATCH 3
#define DD3D_VERSION ((DD3D_MAJOR << (8 * 3)) + (DD3D_MINOR << (8 * 2)) + (DD3D_PATCH << (8 * 1)))

#define _DD3D_VERSION_STR_TEXT(text) #text
Expand Down

0 comments on commit 2134d0d

Please sign in to comment.