Skip to content

Commit

Permalink
update missing things. Update sample to use resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ughuuu committed Mar 13, 2024
1 parent 2aab80f commit 34c297c
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 23 deletions.
4 changes: 3 additions & 1 deletion demo/Main.tscn
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[gd_scene load_steps=4 format=3 uid="uid://dsba4ukrk2ymt"]
[gd_scene load_steps=5 format=3 uid="uid://dsba4ukrk2ymt"]

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

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

[node name="Database" type="Node" parent="."]
script = ExtResource("1")
db_resource = ExtResource("3_onnij")

[node name="MarginContainer" type="MarginContainer" parent="."]
layout_mode = 1
Expand Down
9 changes: 4 additions & 5 deletions demo/database.gd
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var db : SQLite = null

const verbosity_level : int = SQLite.VERBOSE

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

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

db = SQLite.new()
db.path = db_name
db.path = db_resource.resource_name
db.verbosity_level = verbosity_level
# Open the database using the db_name found in the path variable
db.open_db()
Expand Down Expand Up @@ -249,7 +248,7 @@ func example_of_call_external_functions():
table_dict["salary"] = {"data_type":"real"}

db = SQLite.new()
db.path = db_name
db.path = db_resource.resource_path
db.verbosity_level = verbosity_level
# Open the database using the db_name found in the path variable
db.open_db()
Expand Down Expand Up @@ -298,7 +297,7 @@ func example_of_blob_io():
var tex_data : PackedByteArray = texture.get_image().save_png_to_buffer()

db = SQLite.new()
db.path = db_name
db.path = db_resource.resource_path
db.verbosity_level = verbosity_level
# Open the database using the db_name found in the path variable
db.open_db()
Expand Down
2 changes: 1 addition & 1 deletion demo/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ settings/stdout/verbose_stdout=true

[editor_plugins]

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

[rendering]

Expand Down
11 changes: 11 additions & 0 deletions src/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,32 @@
#include <godot_cpp/godot.hpp>

#include "gdsqlite.h"
#include "resource_loader_sqlite.h"
#include "resource_sqlite.h"

using namespace godot;

static Ref<ResourceFormatLoaderSQLite> sqlite_loader;

void initialize_sqlite_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}

ClassDB::register_class<SQLite>();
GDREGISTER_CLASS(ResourceFormatLoaderSQLite);
GDREGISTER_CLASS(SQLiteResource);

sqlite_loader.instantiate();
ResourceLoader::get_singleton()->add_resource_format_loader(sqlite_loader);
}

void uninitialize_sqlite_module(ModuleInitializationLevel p_level) {
if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
return;
}
ResourceLoader::get_singleton()->remove_resource_format_loader(sqlite_loader);
sqlite_loader.unref();
}

extern "C" {
Expand Down
18 changes: 9 additions & 9 deletions src/resource_loader_sqlite.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
#include "resource_loader_sqlite.h"
#include "resource_sqlite.h"

Variant ResourceFormatLoaderSqlite::_load(const String &p_path, const String &original_path, bool use_sub_threads, int32_t cache_mode) const {
Ref<SqliteResource> sqlite_model = memnew(SqliteResource);
Variant ResourceFormatLoaderSQLite::_load(const String &p_path, const String &original_path, bool use_sub_threads, int32_t cache_mode) const {
Ref<SQLiteResource> sqlite_model = memnew(SQLiteResource);
sqlite_model->set_file(p_path);
return sqlite_model;
}
PackedStringArray ResourceFormatLoaderSqlite::_get_recognized_extensions() const {
PackedStringArray ResourceFormatLoaderSQLite::_get_recognized_extensions() const {
PackedStringArray array;
array.push_back("bin");
array.push_back("db");
return array;
}
bool ResourceFormatLoaderSqlite::_handles_type(const StringName &type) const {
return ClassDB::is_parent_class(type, "SqliteResource");
bool ResourceFormatLoaderSQLite::_handles_type(const StringName &type) const {
return ClassDB::is_parent_class(type, "SQLiteResource");
}
String ResourceFormatLoaderSqlite::_get_resource_type(const String &p_path) const {
String ResourceFormatLoaderSQLite::_get_resource_type(const String &p_path) const {
String el = p_path.get_extension().to_lower();
if (el == "sqlite") {
return "SqliteResource";
if (el == "db") {
return "SQLiteResource";
}
return "";
}
4 changes: 2 additions & 2 deletions src/resource_loader_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

using namespace godot;

class ResourceFormatLoaderSqlite : public ResourceFormatLoader {
GDCLASS(ResourceFormatLoaderSqlite, ResourceFormatLoader);
class ResourceFormatLoaderSQLite : public ResourceFormatLoader {
GDCLASS(ResourceFormatLoaderSQLite, ResourceFormatLoader);

protected:
static void _bind_methods() {}
Expand Down
2 changes: 1 addition & 1 deletion src/resource_sqlite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

#include <godot_cpp/classes/file_access.hpp>

PackedByteArray SqliteResource::get_content() {
PackedByteArray SQLiteResource::get_content() {
PackedByteArray content;
String p_path = get_file();
content = FileAccess::get_file_as_bytes(p_path);
Expand Down
8 changes: 4 additions & 4 deletions src/resource_sqlite.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

using namespace godot;

class SqliteResource : public Resource {
GDCLASS(SqliteResource, Resource);
class SQLiteResource : public Resource {
GDCLASS(SQLiteResource, Resource);

protected:
static void _bind_methods() {}
Expand All @@ -23,7 +23,7 @@ class SqliteResource : public Resource {
}

PackedByteArray get_content();
SqliteResource() {}
~SqliteResource() {}
SQLiteResource() {}
~SQLiteResource() {}
};
#endif // SQLITE_RESOURCE_H

0 comments on commit 34c297c

Please sign in to comment.