From 670d07b39113470204b9c22b1fb8bc57acd30171 Mon Sep 17 00:00:00 2001 From: jkpete <1031139173@qq.com> Date: Tue, 24 Sep 2024 23:31:52 +0800 Subject: [PATCH] add sqlite test code --- .../ItemList/ItemList.CSV.import | 17 +++++ Example/SystemExamples/ItemList/ItemList.json | 3 + Example/SystemExamples/ItemList/ItemList.txt | 5 ++ Example/UsingTest/Scene/TestEGFramework.tscn | 6 +- Example/UsingTest/Script/EGSaveTest.cs | 30 +++++++++ ReadMe.md | 66 ++++++++++++------- addons/EGFramework/Module/SQL/EGSqlite.cs | 2 + .../EGFramework/Module/SaveTools/EGTable.cs | 15 +++++ project.godot | 2 +- 9 files changed, 119 insertions(+), 27 deletions(-) create mode 100644 Example/SystemExamples/ItemList/ItemList.CSV.import create mode 100644 Example/SystemExamples/ItemList/ItemList.json create mode 100644 Example/SystemExamples/ItemList/ItemList.txt create mode 100644 Example/UsingTest/Script/EGSaveTest.cs create mode 100644 addons/EGFramework/Module/SaveTools/EGTable.cs diff --git a/Example/SystemExamples/ItemList/ItemList.CSV.import b/Example/SystemExamples/ItemList/ItemList.CSV.import new file mode 100644 index 0000000..0e8446c --- /dev/null +++ b/Example/SystemExamples/ItemList/ItemList.CSV.import @@ -0,0 +1,17 @@ +[remap] + +importer="csv_translation" +type="Translation" +uid="uid://dhb625wmjnlwy" + +[deps] + +files=["res://Example/SystemExamples/ItemList/ItemList.en.translation", "res://Example/SystemExamples/ItemList/ItemList.es.translation", "res://Example/SystemExamples/ItemList/ItemList.ja.translation"] + +source_file="res://Example/SystemExamples/ItemList/ItemList.CSV" +dest_files=["res://Example/SystemExamples/ItemList/ItemList.en.translation", "res://Example/SystemExamples/ItemList/ItemList.es.translation", "res://Example/SystemExamples/ItemList/ItemList.ja.translation"] + +[params] + +compress=true +delimiter=0 diff --git a/Example/SystemExamples/ItemList/ItemList.json b/Example/SystemExamples/ItemList/ItemList.json new file mode 100644 index 0000000..36a3713 --- /dev/null +++ b/Example/SystemExamples/ItemList/ItemList.json @@ -0,0 +1,3 @@ +{ + +} diff --git a/Example/SystemExamples/ItemList/ItemList.txt b/Example/SystemExamples/ItemList/ItemList.txt new file mode 100644 index 0000000..f462092 --- /dev/null +++ b/Example/SystemExamples/ItemList/ItemList.txt @@ -0,0 +1,5 @@ +keys,en,es,ja +GREET,"Hello, friend!","Hola, amigo!",こんにちは +ASK,How are you?,Cómo está?,元気ですか +BYE,Goodbye,Adiós,さようなら +QUOTE,"""Hello"" said the man.","""Hola"" dijo el hombre.",「こんにちは」男は言いました \ No newline at end of file diff --git a/Example/UsingTest/Scene/TestEGFramework.tscn b/Example/UsingTest/Scene/TestEGFramework.tscn index 2599e6e..3b076e8 100644 --- a/Example/UsingTest/Scene/TestEGFramework.tscn +++ b/Example/UsingTest/Scene/TestEGFramework.tscn @@ -1,6 +1,6 @@ [gd_scene load_steps=2 format=3 uid="uid://dfa86yq8m28rk"] -[ext_resource type="Script" path="res://Example/UsingTest/Script/EGTest.cs" id="1_s8hm3"] +[ext_resource type="Script" path="res://Example/UsingTest/Script/EGSaveTest.cs" id="1_eij2h"] [node name="TestEgFramework" type="Control"] layout_mode = 3 @@ -9,7 +9,7 @@ anchor_right = 1.0 anchor_bottom = 1.0 grow_horizontal = 2 grow_vertical = 2 -script = ExtResource("1_s8hm3") +script = ExtResource("1_eij2h") [node name="Timer" type="Timer" parent="."] autostart = true @@ -23,5 +23,3 @@ offset_bottom = 164.0 layout_mode = 0 offset_right = 50.0 offset_bottom = 50.0 - -[connection signal="pressed" from="Button1" to="." method="OnButton1Click"] diff --git a/Example/UsingTest/Script/EGSaveTest.cs b/Example/UsingTest/Script/EGSaveTest.cs new file mode 100644 index 0000000..27aae3f --- /dev/null +++ b/Example/UsingTest/Script/EGSaveTest.cs @@ -0,0 +1,30 @@ +using Godot; +using System; + +namespace EGFramework.Examples.Test{ + public partial class EGSaveTest : Node,IEGFramework + { + public override void _Ready() + { + base._Ready(); + TestSqlite(); + } + + public void TestSqlite(){ + // string result = this.EGSqlite().CreateTable(); + this.EGSqlite().SaveData(new SqliteBackpackItem{ + ItemID = 10, + ItemCount = 1, + BackpackID = 1, + }); + GD.Print(this.EGSqlite().ExceptionMsg); + // var properties = typeof(SqliteBackpackItem).GetFields(); + // Godot.GD.Print(properties.Count() + " Readed "); + } + } + public struct SqliteBackpackItem{ + public int ItemID; + public int ItemCount; + public int BackpackID; + } +} \ No newline at end of file diff --git a/ReadMe.md b/ReadMe.md index aa252bd..e83e9ed 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -16,26 +16,18 @@ > > Godot 在 [宽松的 MIT 许可证](https://docs.godotengine.org/zh-cn/4.x/about/complying_with_licenses.html#doc-complying-with-licenses) 下完全自由且开源,没有附加条件、没有抽成、什么都没有。用户的游戏乃至引擎的每一行代码,都归用户自己所有。Godot 的开发完全独立且由社区驱动,允许用户为满足需求重塑引擎。它受到不以盈利为目标的 [Godot 基金会](https://godot.foundation/)支持。 - - # 一、准备工作 - - ## 1.下载开发环境 本章节所有环境均在开源IDE `VSCode`,`Godot4.2 - .NET` 的环境下进行,请确保已经下载好`VSCode`,`Godot4.2 - .NET`,如果您有购买Rider或者其他编码工具可以根据自己的喜好来替代VSCode。 - - ## 2.VSCode 插件安装 打开VSCode, 按下`Ctrl+Shift+X`,在搜索栏里面搜索`C# Tools for Godot`,安装该插件,因为该插件可能依赖于`C#`插件,同时需要安装`C#`插件。 同上,搜索并安装`NuGet Package Manager GUI`插件,用于Nuget包的安装与管理。 - - ## 3.Nuget包的安装 打开VSCode,按下`Ctrl+Shift+P`,在搜索栏里面搜索`Nuget Package ManagerGUI`,选择右侧`Install New Pakage` ,依次安装以下依赖包: @@ -50,15 +42,54 @@ 注意:这些Nuget包仅为目前框架版本所用,后续可能会有新的Nuget包导入,会在此处列出。 - - ## 4.删除不需要的Module(可选) 如果您只想使用部分功能,或者不想安装对应的Nuget包依赖,可以直接删除对应的Module脚本文件。 无需担心,除了部分Module存在相关依赖,大部分Module是支持直接删除的。 +## 5.EG插件库简介 +### 5.1 EGSave篇 + +--- + +#### Sqlite数据持久化扩展: + +注意:本功能需要依赖安装 `Microsoft.Data.Sqlite` Nuget扩展,将以下代码放入*.csproj 工程文件中,或者通过Nuget安装上述包。 + +```xml + +``` + +使用案例(保存数据=>保存单一数据,目前来看该功能仍需改进): + +```csharp + public partial class EGSaveTest : Node,IEGFramework + { + public override void _Ready() + { + TestSqlite(); + } + + public void TestSqlite(){ + // string result = this.EGSqlite().CreateTable(); + this.EGSqlite().SaveData(new SqliteBackpackItem{ + ItemID = 10, + ItemCount = 1, + BackpackID = 1, + }); + GD.Print(this.EGSqlite().ExceptionMsg); + } + } + public struct SqliteBackpackItem{ + public int ItemID; + public int ItemCount; + public int BackpackID; + } +``` + +结构类要求:保存字段不能使用 {get;set;},所有数据会自动生成自增序列的ID,字段名称不能为ID。 # 二、框架简介 @@ -68,8 +99,6 @@ 协议文件均存放在目录addons\EGFramework\License_Third_Part下面 - - ## 1.使用框架 添加using,并继承接口IEGFramework,即可使用该框架了。 @@ -96,12 +125,8 @@ public class DataTest{ public string PlayerName; public int Hp; } - - ``` - - ## 2.直接使用Module 继承接口IEGFramework时,可以直接通过this.GetModule这个扩展方法直接获取该模块。上面的代码可以通过直接调用的方式改写成如下: @@ -128,8 +153,6 @@ public class DataTest{ } ``` - - ## 3.扩展框架(编写Module) 编写框架时,要用到IModule这个接口,任何继承了该接口的类均视为Module,可以被上面的方法Get到。 @@ -150,14 +173,14 @@ namespace EGFramework { void RegisterObject(T object_); T GetObject() where T : class,new(); - + } public class EGObject : EGModule,IEGObject { private IOCContainer ObjectContainer = new IOCContainer(); public override void Init() { - + } public TObject GetObject() where TObject : class,new() @@ -178,7 +201,7 @@ namespace EGFramework return ObjectContainer.self.ContainsKey(typeof(TObject)); } } - + public static class CanGetObjectExtension { public static T EGGetObject(this IEGFramework self) where T : class,new() @@ -206,5 +229,4 @@ namespace EGFramework } } - ``` diff --git a/addons/EGFramework/Module/SQL/EGSqlite.cs b/addons/EGFramework/Module/SQL/EGSqlite.cs index 825994b..004155b 100644 --- a/addons/EGFramework/Module/SQL/EGSqlite.cs +++ b/addons/EGFramework/Module/SQL/EGSqlite.cs @@ -81,6 +81,7 @@ namespace EGFramework{ string sqlCommand = "CREATE TABLE " + typeof(TData).Name; sqlCommand += "(\"ID\" INTEGER NOT NULL UNIQUE,"; var properties = typeof(TData).GetFields(); + Godot.GD.Print(properties.Count() + " Readed "); foreach(var property in properties){ if(property.FieldType == typeof(int) || property.FieldType == typeof(bool) || property.FieldType.IsEnum){ sqlCommand += "\"" + property.Name + "\" INTEGER" + " NOT NULL,"; @@ -92,6 +93,7 @@ namespace EGFramework{ } } sqlCommand += "PRIMARY KEY(\"ID\" AUTOINCREMENT))"; + Godot.GD.Print(sqlCommand); SqliteCommand createCommand = new SqliteCommand(sqlCommand,SqliteConn); result = result + createCommand.ExecuteNonQuery().ToString(); } diff --git a/addons/EGFramework/Module/SaveTools/EGTable.cs b/addons/EGFramework/Module/SaveTools/EGTable.cs new file mode 100644 index 0000000..101f67d --- /dev/null +++ b/addons/EGFramework/Module/SaveTools/EGTable.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace EGFramework +{ + public class EGTable : EGModule + { + public override void Init() + { + + } + } +} \ No newline at end of file diff --git a/project.godot b/project.godot index 6e27992..cc00667 100644 --- a/project.godot +++ b/project.godot @@ -12,7 +12,7 @@ config_version=5 config/name="EGFramework" config/tags=PackedStringArray("official") -run/main_scene="res://Example/Action3D/MainGame.tscn" +run/main_scene="res://Example/UsingTest/Scene/TestEGFramework.tscn" config/features=PackedStringArray("4.2", "C#", "GL Compatibility") config/icon="res://icon.svg"