diff --git a/addons/admob/admob.gd b/addons/admob/admob.gd index 4b709d7..c7df5d3 100644 --- a/addons/admob/admob.gd +++ b/addons/admob/admob.gd @@ -23,25 +23,164 @@ @tool extends EditorPlugin -var MainScreen : Control +var http_request_downloader := HTTPRequest.new() +var timer := Timer.new() + +var android_download_path := "res://addons/admob/downloads/android/" +var ios_download_path := "res://addons/admob/downloads/ios/" +var default_download_path := "res://addons/admob/downloads/" +var current_download_path := default_download_path +var godot_version := "v" + str(Engine.get_version_info().major) + "." + str(Engine.get_version_info().minor) + "." + str(Engine.get_version_info().patch) +var plugin_version := get_plugin_version() + +var version_support := { + "android": "v3.0.0", + "ios": "v3.0.0" +} + +enum Items { + LatestVersion, + Folder, + GitHub +} func _enter_tree(): - MainScreen = load("res://addons/admob/src/editor/scenes/MainScreen.tscn").instantiate() - get_editor_interface().get_editor_main_screen().add_child(MainScreen) - MainScreen.hide() + setup_timer() + create_download_directories() + _request_version_support() + _setup_download_request_listener() + + var popup := PopupMenu.new() + + var android_popup := PopupMenu.new() + android_popup.name = "Android" + popup.add_child(android_popup) + + var ios_popup := PopupMenu.new() + ios_popup.name = "iOS" + popup.add_child(ios_popup) + + android_popup.connect("id_pressed", _on_android_popupmenu_id_pressed) + android_popup.add_item(str(Items.keys()[Items.LatestVersion]), Items.LatestVersion) + android_popup.add_item(str(Items.keys()[Items.Folder]), Items.Folder) + android_popup.add_item(str(Items.keys()[Items.GitHub]), Items.GitHub) + + ios_popup.connect("id_pressed", _on_ios_popupmenu_id_pressed) + ios_popup.add_item(str(Items.keys()[Items.LatestVersion]), Items.LatestVersion) + ios_popup.add_item(str(Items.keys()[Items.Folder]), Items.Folder) + ios_popup.add_item(str(Items.keys()[Items.GitHub]), Items.GitHub) + + popup.connect("id_pressed", _on_popupmenu_id_pressed) + + popup.add_submenu_item(android_popup.name, android_popup.name) + popup.add_submenu_item(ios_popup.name, ios_popup.name) + popup.add_item(str(Items.keys()[Items.Folder]), Items.Folder) + popup.add_item(str(Items.keys()[Items.GitHub]), Items.GitHub) + + add_tool_submenu_item("AdMob Download Manager", popup) func _exit_tree(): - get_editor_interface().get_editor_main_screen().remove_child(MainScreen) - MainScreen.queue_free() + remove_tool_menu_item("AdMob Download Manager") + + +func _request_version_support(): + var url = "https://raw.githubusercontent.com/Poing-Studios/godot-admob-versions/" + plugin_version + "/versions.json" + var http_request = HTTPRequest.new() + http_request.request_completed.connect(_on_version_support_request_completed) + add_child(http_request) + http_request.request(url) + +func _on_version_support_request_completed(result, response_code, headers, body): + if response_code == 200: + var json = JSON.new() + if json.parse(body.get_string_from_utf8()) == OK: + version_support = json.get_data() as Dictionary + return + printerr("ERR_001: Couldn't get version supported dynamic for AdMob, the latest supported version listed may be outdated. \n" \ + + "Read more about on: res://addons/admob/docs/errors/ERR_001.md") + +func _on_download_request_completed(result, response_code, headers, body): + if response_code == 200: + print("Download completed, you can check the downloaded file at: " + current_download_path) + else: + printerr("ERR_002: It is not possible to download the Android/iOS plugin. \n" \ + + "Read more about on: res://addons/admob/docs/errors/ERR_002.md") -func _has_main_screen(): - return false + timer.stop() + +func _setup_download_request_listener(): + http_request_downloader.request_completed.connect(_on_download_request_completed) + add_child(http_request_downloader) + +func setup_timer(): + timer.wait_time = 3 + timer.timeout.connect(show_download_percent) + add_child(timer) + +func create_download_directories(): + DirAccess.make_dir_recursive_absolute(android_download_path) + DirAccess.make_dir_recursive_absolute(ios_download_path) + +func start_download(platform: String, download_path: String, file_prefix: String): + if http_request_downloader.get_http_client_status() != HTTPClient.STATUS_DISCONNECTED: + printerr("http_request_downloader is processing a request, wait for completion") + return + + var file_name = file_prefix + godot_version + ".zip" + var url_download = "https://github.com/Poing-Studios/godot-admob-" + platform + "/releases/download/" + version_support[platform] + "/" + file_name + + http_request_downloader.request_ready() + http_request_downloader.download_file = download_path + file_name + http_request_downloader.request(url_download) + show_download_percent(url_download) + timer.start() -func _make_visible(visible): - MainScreen.visible = visible +func _on_android_popupmenu_id_pressed(id: int): + match id: + Items.LatestVersion: + start_download("android", android_download_path, "android-template-") + Items.Folder: + var path_directory = ProjectSettings.globalize_path(android_download_path) + OS.shell_open(str("file://", path_directory)) + Items.GitHub: + OS.shell_open("https://github.com/Poing-Studios/godot-admob-android/tree/" + version_support.android) -func _get_plugin_name(): - return "AdMob" +func _on_ios_popupmenu_id_pressed(id: int): + match id: + Items.LatestVersion: + start_download("ios", ios_download_path, "ios-template-") + Items.Folder: + var path_directory = ProjectSettings.globalize_path(ios_download_path) + OS.shell_open(str("file://", path_directory)) + Items.GitHub: + OS.shell_open("https://github.com/Poing-Studios/godot-admob-ios/tree/" + version_support.ios) -func _get_plugin_icon(): - return load("res://addons/admob/assets/icon-15.png") +func _on_popupmenu_id_pressed(id : int): + match id: + Items.Folder: + var path_directory = ProjectSettings.globalize_path(default_download_path) + OS.shell_open(str("file://", path_directory)) + Items.GitHub: + OS.shell_open("https://github.com/Poing-Studios/godot-admob-plugin/tree/" + plugin_version) + + +func get_plugin_version() -> String: + var plugin_config_file := ConfigFile.new() + var version: String = "" + + if plugin_config_file.load("res://addons/admob/plugin.cfg") == OK: + version = plugin_config_file.get_value("plugin", "version") + else: + printerr("Failed to load plugin.cfg") + + return version + +func show_download_percent(url_download: String = ""): + if not url_download.is_empty(): + print("Downloading " + url_download) + + var bodySize = http_request_downloader.get_body_size() + var downloadedBytes = http_request_downloader.get_downloaded_bytes() + + var percent = int(downloadedBytes * 100 / bodySize) + print("Download percent: " + str(percent) + "%") diff --git a/addons/admob/docs/errors/ERR_001.md b/addons/admob/docs/errors/ERR_001.md new file mode 100644 index 0000000..9617520 --- /dev/null +++ b/addons/admob/docs/errors/ERR_001.md @@ -0,0 +1,25 @@ +# Error Code ERR_001 + +## Error Description + +Error code `ERR_001` occurs when the supported dynamic version for AdMob couldn't be obtained, and the latest supported version listed may be outdated. + +## Common Causes + +Common causes of this error may include: + +1. **Connection Issues**: There may be problems with your application's internet connection, preventing the download of the plugin. + +2. **Invalid .json File**: The .json file used for version retrieval may be invalid or corrupted. + +## Solution + +Here are the steps to resolve error `ERR_001`: + +1. **Check Connection**: Verify your application's internet connection. A good address to test your connection is at [godot-admob-versions](https://github.com/Poing-Studios/godot-admob-versions/blob/master/versions.json). If the connection is functional, proceed to the next step. + +2. **Invalid .json File**: If the JSON file is invalid, try manually downloading it from the `Project->Tools->Android Download Manager->Android/iOS->LatestVersion` section. Please note that you may download an outdated version. It is recommended to create an issue on GitHub to address this issue, and if you have any questions, feel free to inquire about the latest version. + +## What to Do If the Problem Continues + +If you continue to experience issues or encounter problems, please consider opening an issue on the repository at [godot-admob-versions](https://github.com/Poing-Studios/godot-admob-versions). diff --git a/addons/admob/docs/errors/ERR_002.md b/addons/admob/docs/errors/ERR_002.md new file mode 100644 index 0000000..7b86afb --- /dev/null +++ b/addons/admob/docs/errors/ERR_002.md @@ -0,0 +1,27 @@ +# Error Code ERR_002 + +## Error Description + +Error code `ERR_002` occurs when it is not possible to download the Android/iOS plugin. + +## Common Causes + +Common causes of this error may include: + +1. **Connection Issues**: There may be problems with your application's internet connection, preventing the download of the plugin. + +2. **Plugin Unavailability**: The plugin you are trying to download may not be currently available on the specified download location. This can be due to outdated information on [godot-admob-versions](https://github.com/Poing-Studios/godot-admob-versions) or the corresponding versions not being released on [godot-admob-ios](https://github.com/Poing-Studios/godot-admob-ios) or [godot-admob-android](https://github.com/Poing-Studios/godot-admob-android). + +## Solution + +Here are the steps to resolve error `ERR_002`: + +1. **Check Network Connection**: Verify your application's internet connection. If the connection is functional, proceed to the next step. + +2. **Plugin Availability**: Confirm the availability of the plugin by checking the repositories on GitHub: [godot-admob-versions](https://github.com/Poing-Studios/godot-admob-versions), [godot-admob-ios](https://github.com/Poing-Studios/godot-admob-ios), and [godot-admob-android](https://github.com/Poing-Studios/godot-admob-android). + +3. **Retry Download**: If the plugin is available, attempt to download it again. Sometimes, network issues can cause temporary download failures. + +## What to Do If the Problem Continues + +If you continue to experience issues or encounter problems, please consider opening an issue on the repository at [godot-admob-plugin](https://github.com/Poing-Studios/godot-admob-plugin). diff --git a/addons/admob/plugin.cfg b/addons/admob/plugin.cfg index c3bd11d..453255b 100644 --- a/addons/admob/plugin.cfg +++ b/addons/admob/plugin.cfg @@ -3,5 +3,5 @@ name="AdMob" description="The AdMob Plugin for Android and iOS." author="Poing Studios" -version="v2.0.0" +version="v3.0.0" script="admob.gd" diff --git a/addons/admob/src/editor/scenes/MainScreen.tscn b/addons/admob/src/editor/scenes/MainScreen.tscn deleted file mode 100644 index 2f8a196..0000000 --- a/addons/admob/src/editor/scenes/MainScreen.tscn +++ /dev/null @@ -1,9 +0,0 @@ -[gd_scene format=3 uid="uid://bw75ekxcrvn6x"] - -[node name="MainScreen" type="Control"] -layout_mode = 3 -anchors_preset = 15 -anchor_right = 1.0 -anchor_bottom = 1.0 -grow_horizontal = 2 -grow_vertical = 2