Skip to content

Commit 34c297c

Browse files
committed
update missing things. Update sample to use resource.
1 parent 2aab80f commit 34c297c

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

demo/Main.tscn

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
[gd_scene load_steps=4 format=3 uid="uid://dsba4ukrk2ymt"]
1+
[gd_scene load_steps=5 format=3 uid="uid://dsba4ukrk2ymt"]
22

33
[ext_resource type="Script" path="res://database.gd" id="1"]
44
[ext_resource type="Script" path="res://Main.gd" id="2"]
5+
[ext_resource type="SQLiteResource" path="res://data/test.db" id="3_onnij"]
56

67
[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_45gfi"]
78
content_margin_left = 24.0
@@ -21,6 +22,7 @@ script = ExtResource("2")
2122

2223
[node name="Database" type="Node" parent="."]
2324
script = ExtResource("1")
25+
db_resource = ExtResource("3_onnij")
2426

2527
[node name="MarginContainer" type="MarginContainer" parent="."]
2628
layout_mode = 1

demo/database.gd

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var db : SQLite = null
44

55
const verbosity_level : int = SQLite.VERBOSE
66

7-
var db_name := "res://data/test"
7+
@export var db_resource : SQLiteResource
88
var packaged_db_name := "res://data_to_be_packaged"
99
var peristent_db_name := "user://my_database"
1010
var json_name := "res://data/test_backup"
@@ -29,7 +29,6 @@ signal texture_received(texture)
2929
func _ready():
3030
if OS.get_name() in ["Android", "iOS", "Web"]:
3131
copy_data_to_user()
32-
db_name = "user://data/test"
3332
json_name = "user://data/test_backup"
3433

3534
# Enable/disable examples here:
@@ -77,7 +76,7 @@ func example_of_basic_database_querying():
7776
table_dict["salary"] = {"data_type":"real"}
7877

7978
db = SQLite.new()
80-
db.path = db_name
79+
db.path = db_resource.resource_name
8180
db.verbosity_level = verbosity_level
8281
# Open the database using the db_name found in the path variable
8382
db.open_db()
@@ -249,7 +248,7 @@ func example_of_call_external_functions():
249248
table_dict["salary"] = {"data_type":"real"}
250249

251250
db = SQLite.new()
252-
db.path = db_name
251+
db.path = db_resource.resource_path
253252
db.verbosity_level = verbosity_level
254253
# Open the database using the db_name found in the path variable
255254
db.open_db()
@@ -298,7 +297,7 @@ func example_of_blob_io():
298297
var tex_data : PackedByteArray = texture.get_image().save_png_to_buffer()
299298

300299
db = SQLite.new()
301-
db.path = db_name
300+
db.path = db_resource.resource_path
302301
db.verbosity_level = verbosity_level
303302
# Open the database using the db_name found in the path variable
304303
db.open_db()

demo/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ settings/stdout/verbose_stdout=true
2121

2222
[editor_plugins]
2323

24-
enabled=PackedStringArray()
24+
enabled=PackedStringArray("res://addons/godot-sqlite/plugin.cfg")
2525

2626
[rendering]
2727

src/register_types.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,32 @@
77
#include <godot_cpp/godot.hpp>
88

99
#include "gdsqlite.h"
10+
#include "resource_loader_sqlite.h"
11+
#include "resource_sqlite.h"
1012

1113
using namespace godot;
1214

15+
static Ref<ResourceFormatLoaderSQLite> sqlite_loader;
16+
1317
void initialize_sqlite_module(ModuleInitializationLevel p_level) {
1418
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
1519
return;
1620
}
1721

1822
ClassDB::register_class<SQLite>();
23+
GDREGISTER_CLASS(ResourceFormatLoaderSQLite);
24+
GDREGISTER_CLASS(SQLiteResource);
25+
26+
sqlite_loader.instantiate();
27+
ResourceLoader::get_singleton()->add_resource_format_loader(sqlite_loader);
1928
}
2029

2130
void uninitialize_sqlite_module(ModuleInitializationLevel p_level) {
2231
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
2332
return;
2433
}
34+
ResourceLoader::get_singleton()->remove_resource_format_loader(sqlite_loader);
35+
sqlite_loader.unref();
2536
}
2637

2738
extern "C" {

src/resource_loader_sqlite.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
#include "resource_loader_sqlite.h"
22
#include "resource_sqlite.h"
33

4-
Variant ResourceFormatLoaderSqlite::_load(const String &p_path, const String &original_path, bool use_sub_threads, int32_t cache_mode) const {
5-
Ref<SqliteResource> sqlite_model = memnew(SqliteResource);
4+
Variant ResourceFormatLoaderSQLite::_load(const String &p_path, const String &original_path, bool use_sub_threads, int32_t cache_mode) const {
5+
Ref<SQLiteResource> sqlite_model = memnew(SQLiteResource);
66
sqlite_model->set_file(p_path);
77
return sqlite_model;
88
}
9-
PackedStringArray ResourceFormatLoaderSqlite::_get_recognized_extensions() const {
9+
PackedStringArray ResourceFormatLoaderSQLite::_get_recognized_extensions() const {
1010
PackedStringArray array;
11-
array.push_back("bin");
11+
array.push_back("db");
1212
return array;
1313
}
14-
bool ResourceFormatLoaderSqlite::_handles_type(const StringName &type) const {
15-
return ClassDB::is_parent_class(type, "SqliteResource");
14+
bool ResourceFormatLoaderSQLite::_handles_type(const StringName &type) const {
15+
return ClassDB::is_parent_class(type, "SQLiteResource");
1616
}
17-
String ResourceFormatLoaderSqlite::_get_resource_type(const String &p_path) const {
17+
String ResourceFormatLoaderSQLite::_get_resource_type(const String &p_path) const {
1818
String el = p_path.get_extension().to_lower();
19-
if (el == "sqlite") {
20-
return "SqliteResource";
19+
if (el == "db") {
20+
return "SQLiteResource";
2121
}
2222
return "";
2323
}

src/resource_loader_sqlite.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
using namespace godot;
88

9-
class ResourceFormatLoaderSqlite : public ResourceFormatLoader {
10-
GDCLASS(ResourceFormatLoaderSqlite, ResourceFormatLoader);
9+
class ResourceFormatLoaderSQLite : public ResourceFormatLoader {
10+
GDCLASS(ResourceFormatLoaderSQLite, ResourceFormatLoader);
1111

1212
protected:
1313
static void _bind_methods() {}

src/resource_sqlite.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#include <godot_cpp/classes/file_access.hpp>
55

6-
PackedByteArray SqliteResource::get_content() {
6+
PackedByteArray SQLiteResource::get_content() {
77
PackedByteArray content;
88
String p_path = get_file();
99
content = FileAccess::get_file_as_bytes(p_path);

src/resource_sqlite.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
using namespace godot;
77

8-
class SqliteResource : public Resource {
9-
GDCLASS(SqliteResource, Resource);
8+
class SQLiteResource : public Resource {
9+
GDCLASS(SQLiteResource, Resource);
1010

1111
protected:
1212
static void _bind_methods() {}
@@ -23,7 +23,7 @@ class SqliteResource : public Resource {
2323
}
2424

2525
PackedByteArray get_content();
26-
SqliteResource() {}
27-
~SqliteResource() {}
26+
SQLiteResource() {}
27+
~SQLiteResource() {}
2828
};
2929
#endif // SQLITE_RESOURCE_H

0 commit comments

Comments
 (0)