升级 .NET 9;测试 SQLite 初始数据库

This commit is contained in:
milimoe 2024-11-14 22:40:48 +08:00
parent 3f27123c03
commit 562658bc62
Signed by: milimoe
GPG Key ID: 05D280912DA6C69E
8 changed files with 51 additions and 17 deletions

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<BaseOutputPath>..\bin</BaseOutputPath> <BaseOutputPath>..\bin</BaseOutputPath>

View File

@ -2,7 +2,7 @@
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ApplicationIcon>logo.ico</ApplicationIcon> <ApplicationIcon>logo.ico</ApplicationIcon>

View File

@ -204,6 +204,11 @@ namespace Milimoe.FunGame.Server.Others
public static void AfterCreateSQLService(SQLHelper sqlHelper) public static void AfterCreateSQLService(SQLHelper sqlHelper)
{ {
Config.SQLMode = sqlHelper.Mode; Config.SQLMode = sqlHelper.Mode;
if (sqlHelper is SQLiteHelper sqliteHelper && !sqliteHelper.DatabaseExists())
{
ServerHelper.WriteLine("正在初始化数据库 . . .", InvokeMessageType.Core);
sqliteHelper.ExecuteSqlFile(AppDomain.CurrentDomain.BaseDirectory + "fungame_sqlite.sql");
}
ServerLogin(sqlHelper); ServerLogin(sqlHelper);
ClearRoomList(sqlHelper); ClearRoomList(sqlHelper);
sqlHelper.Dispose(); sqlHelper.Dispose();

View File

@ -46,7 +46,7 @@ namespace Milimoe.FunGame.Server.Utility
} }
else ServerHelper.Error(new SQLServiceException()); else ServerHelper.Error(new SQLServiceException());
} }
return "data source=" + DataSource; return $"data source={AppDomain.CurrentDomain.BaseDirectory}" + DataSource;
} }
} }
} }

View File

@ -80,7 +80,6 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
/// </summary> /// </summary>
/// <param name="script"></param> /// <param name="script"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="Exception"></exception>
public override int Execute(string script) public override int Execute(string script)
{ {
bool localTransaction = _transaction == null; bool localTransaction = _transaction == null;
@ -92,6 +91,7 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
NewTransaction(); NewTransaction();
} }
OpenConnection(); OpenConnection();
Script = script;
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api); ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
using MySqlCommand command = new(script, _connection); using MySqlCommand command = new(script, _connection);
command.CommandType = CommandType; command.CommandType = CommandType;
@ -139,6 +139,7 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
NewTransaction(); NewTransaction();
} }
OpenConnection(); OpenConnection();
Script = script;
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api); ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
using MySqlCommand command = new(script, _connection) using MySqlCommand command = new(script, _connection)
@ -150,6 +151,7 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
{ {
SelectCommand = command SelectCommand = command
}; };
_dataSet = new();
adapter.Fill(_dataSet); adapter.Fill(_dataSet);
if (localTransaction) Commit(); if (localTransaction) Commit();

View File

@ -78,7 +78,6 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
/// </summary> /// </summary>
/// <param name="script"></param> /// <param name="script"></param>
/// <returns></returns> /// <returns></returns>
/// <exception cref="Exception"></exception>
public override int Execute(string script) public override int Execute(string script)
{ {
bool localTransaction = _transaction == null; bool localTransaction = _transaction == null;
@ -90,6 +89,7 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
NewTransaction(); NewTransaction();
} }
OpenConnection(); OpenConnection();
Script = script;
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api); ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
using SqliteCommand command = new(script, _connection); using SqliteCommand command = new(script, _connection);
command.CommandType = CommandType; command.CommandType = CommandType;
@ -138,6 +138,7 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
NewTransaction(); NewTransaction();
} }
OpenConnection(); OpenConnection();
Script = script;
ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api); ServerHelper.WriteLine("SQLQuery -> " + script, InvokeMessageType.Api);
using SqliteCommand command = new(script, _connection) using SqliteCommand command = new(script, _connection)
{ {
@ -211,6 +212,39 @@ namespace Milimoe.FunGame.Server.Utility.DataUtility
} }
} }
/// <summary>
/// 检查数据库是否存在
/// </summary>
/// <returns></returns>
public bool DatabaseExists()
{
return File.Exists(ServerInfo.SQLServerDataBase);
}
/// <summary>
/// 执行SQL文件中的所有SQL语句来初始化数据库
/// </summary>
/// <param name="sqlFilePath">SQL文件路径</param>
public void ExecuteSqlFile(string sqlFilePath)
{
if (!File.Exists(sqlFilePath))
{
throw new FileNotFoundException("SQL文件不存在", sqlFilePath);
}
string sqlContent = File.ReadAllText(sqlFilePath);
string[] sqlCommands = sqlContent.Split([";"], StringSplitOptions.RemoveEmptyEntries);
foreach (string commandText in sqlCommands)
{
string sql = commandText.Trim();
if (!string.IsNullOrEmpty(sql))
{
Execute(sql);
}
}
}
private bool _isDisposed = false; private bool _isDisposed = false;
/// <summary> /// <summary>

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web"> <Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup> <PropertyGroup>
<TargetFramework>net8.0</TargetFramework> <TargetFramework>net9.0</TargetFramework>
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
<RootNamespace>Milimoe.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace> <RootNamespace>Milimoe.$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>

View File

@ -103,17 +103,10 @@ try
options.JsonSerializerOptions.WriteIndented = true; options.JsonSerializerOptions.WriteIndented = true;
options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All); options.JsonSerializerOptions.Encoder = JavaScriptEncoder.Create(UnicodeRanges.All);
options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles; options.JsonSerializerOptions.ReferenceHandler = ReferenceHandler.IgnoreCycles;
options.JsonSerializerOptions.Converters.Add(new DateTimeConverter()); foreach (JsonConverter converter in JsonTool.JsonSerializerOptions.Converters)
options.JsonSerializerOptions.Converters.Add(new DataTableConverter()); {
options.JsonSerializerOptions.Converters.Add(new DataSetConverter()); options.JsonSerializerOptions.Converters.Add(converter);
options.JsonSerializerOptions.Converters.Add(new UserConverter()); }
options.JsonSerializerOptions.Converters.Add(new RoomConverter());
options.JsonSerializerOptions.Converters.Add(new CharacterConverter());
options.JsonSerializerOptions.Converters.Add(new MagicResistanceConverter());
options.JsonSerializerOptions.Converters.Add(new EquipSlotConverter());
options.JsonSerializerOptions.Converters.Add(new SkillConverter());
options.JsonSerializerOptions.Converters.Add(new EffectConverter());
options.JsonSerializerOptions.Converters.Add(new ItemConverter());
}); });
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer(); builder.Services.AddEndpointsApiExplorer();