Skip to content

Commit

Permalink
feat: added Download Manager #82
Browse files Browse the repository at this point in the history
  • Loading branch information
gumaciel committed Aug 20, 2023
1 parent 86f01c5 commit df08e5e
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 24 deletions.
167 changes: 153 additions & 14 deletions addons/admob/admob.gd
Original file line number Diff line number Diff line change
Expand Up @@ -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) + "%")
25 changes: 25 additions & 0 deletions addons/admob/docs/errors/ERR_001.md
Original file line number Diff line number Diff line change
@@ -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).
27 changes: 27 additions & 0 deletions addons/admob/docs/errors/ERR_002.md
Original file line number Diff line number Diff line change
@@ -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).
2 changes: 1 addition & 1 deletion addons/admob/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 0 additions & 9 deletions addons/admob/src/editor/scenes/MainScreen.tscn

This file was deleted.

0 comments on commit df08e5e

Please sign in to comment.