|
1 | 1 | @tool |
2 | | -extends EditorPlugin |
| 2 | +class_name FirebaseEditorPlugin extends EditorPlugin |
3 | 3 |
|
4 | 4 | var export_plugin : AndroidExportPlugin |
5 | 5 |
|
| 6 | +# Firebase plugin lines to inject into Gradle files |
| 7 | +const BUILD_GRADLE_PLUGIN_LINE := "id 'com.google.gms.google-services'" |
| 8 | +const SETTINGS_GRADLE_PLUGIN_LINE := "id 'com.google.gms.google-services' version '4.4.2' apply false" |
| 9 | + |
6 | 10 | func _enter_tree(): |
7 | 11 | export_plugin = AndroidExportPlugin.new() |
8 | 12 | add_export_plugin(export_plugin) |
9 | 13 |
|
10 | | - |
11 | 14 | func _exit_tree(): |
12 | 15 | remove_export_plugin(export_plugin) |
13 | 16 | export_plugin = null |
14 | 17 |
|
| 18 | +func _enable_plugin() -> void: |
| 19 | + add_autoload_singleton("Firebase", "res://addons/GodotFirebaseAndroid/Firebase.gd") |
| 20 | + |
| 21 | +func _disable_plugin() -> void: |
| 22 | + remove_autoload_singleton("Firebase") |
| 23 | + _cleanup_gradle_files() |
| 24 | + |
| 25 | +static func _cleanup_gradle_files() -> void: |
| 26 | + _clean_line_from_file("res://android/build/build.gradle", BUILD_GRADLE_PLUGIN_LINE) |
| 27 | + _clean_line_from_file("res://android/build/settings.gradle", SETTINGS_GRADLE_PLUGIN_LINE) |
| 28 | + |
| 29 | +static func _clean_line_from_file(file_path: String, line_to_remove: String) -> void: |
| 30 | + if FileAccess.file_exists(file_path): |
| 31 | + var text := FileAccess.open(file_path, FileAccess.READ).get_as_text() |
| 32 | + text = text.replace(line_to_remove, "") |
| 33 | + var file := FileAccess.open(file_path, FileAccess.WRITE) |
| 34 | + file.store_string(text) |
| 35 | + file.close() |
| 36 | + |
15 | 37 |
|
16 | 38 | class AndroidExportPlugin extends EditorExportPlugin: |
17 | 39 | var _plugin_name = "GodotFirebaseAndroid" |
| 40 | + |
| 41 | + func _export_begin(features: PackedStringArray, is_debug: bool, path: String, flags: int) -> void: |
| 42 | + if not features.has("android"): |
| 43 | + return |
| 44 | + |
| 45 | + if (not get_option("gradle_build/use_gradle_build")) or (not FileAccess.file_exists("res://android/build/google-services.json")): |
| 46 | + FirebaseEditorPlugin._cleanup_gradle_files() |
| 47 | + return |
| 48 | + |
| 49 | + # Modify build.gradle |
| 50 | + _insert_line_if_missing("res://android/build/build.gradle", "id 'org.jetbrains.kotlin.android'", BUILD_GRADLE_PLUGIN_LINE) |
| 51 | + # Modify settings.gradle |
| 52 | + _insert_line_if_missing("res://android/build/settings.gradle", "id 'org.jetbrains.kotlin.android' version versions.kotlinVersion", SETTINGS_GRADLE_PLUGIN_LINE) |
| 53 | + |
| 54 | + func _insert_line_if_missing(file_path: String, after_line: String, insert_line: String) -> void: |
| 55 | + var text := FileAccess.open(file_path, FileAccess.READ).get_as_text() |
| 56 | + if text.contains(insert_line): |
| 57 | + return |
| 58 | + |
| 59 | + var lines := text.split("\n") |
| 60 | + var result := PackedStringArray() |
| 61 | + var inserted := false |
| 62 | + |
| 63 | + for line in lines: |
| 64 | + result.append(line) |
| 65 | + if not inserted and line.strip_edges() == after_line.strip_edges(): |
| 66 | + var indent := "" |
| 67 | + for c in line: |
| 68 | + if c == " " or c == "\t": |
| 69 | + indent += c |
| 70 | + else: |
| 71 | + break |
| 72 | + result.append(indent + insert_line) |
| 73 | + inserted = true |
| 74 | + |
| 75 | + var file := FileAccess.open(file_path, FileAccess.WRITE) |
| 76 | + file.store_string("\n".join(result)) |
| 77 | + file.close() |
18 | 78 |
|
19 | 79 | func _supports_platform(platform): |
20 | 80 | if platform is EditorExportPlatformAndroid: |
|
0 commit comments