Skip to content

Commit 1cfcfc0

Browse files
committed
数据库服务更改为单例;增加启动时的连接数据库窗口
1 parent f83cfda commit 1cfcfc0

File tree

260 files changed

+5161
-6527
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

260 files changed

+5161
-6527
lines changed

src/DofGMTool/App.xaml

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
x:Class="DofGMTool.App"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:converters="using:DofGMTool.Helpers"
56
xmlns:winuicommunity="using:WinUICommunity">
67
<Application.Resources>
78
<ResourceDictionary>
@@ -11,7 +12,9 @@
1112
<ResourceDictionary Source="/Styles/Thickness.xaml" />
1213
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
1314
<ResourceDictionary Source="ms-appx:///WinUICommunity.Components/Themes/Generic.xaml" />
15+
1416
</ResourceDictionary.MergedDictionaries>
17+
<converters:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter" />
1518
</ResourceDictionary>
1619
</Application.Resources>
1720
</Application>

src/DofGMTool/App.xaml.cs

+16-44
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using DofGMTool.Contracts.Services;
33
using DofGMTool.Core.Contracts.Services;
44
using DofGMTool.Core.Services;
5+
using DofGMTool.Helpers;
56
using DofGMTool.Models;
67
using DofGMTool.Services;
78
using DofGMTool.ViewModels;
@@ -11,7 +12,6 @@
1112
using Microsoft.Extensions.Hosting;
1213
using Microsoft.UI.Xaml;
1314
using Microsoft.UI.Xaml.Controls;
14-
using MySqlConnector;
1515
using System.Diagnostics;
1616
using Windows.Storage;
1717

@@ -90,42 +90,7 @@ public App()
9090
services.AddTransient<MainPage>();
9191
services.AddTransient<ShellPage>();
9292
services.AddTransient<ShellViewModel>();
93-
services.AddSingleton<IFreeSql<MySqlFlag>>(provider =>
94-
{
95-
return new FreeSqlBuilder()
96-
.UseConnectionFactory(DataType.MySql, () =>
97-
{
98-
var conn = new MySqlConnection("data source=192.168.200.131;port=3306;user id=game;password=uu5!^%jg;initial catalog=taiwan_cain;sslmode=none;max pool size=50;Charset=latin1;ConvertZeroDateTime=True;");
99-
conn.Open();
100-
MySqlCommand cmd = conn.CreateCommand();
101-
cmd.CommandText = "SET Charset latin1;";
102-
cmd.ExecuteNonQuery();
103-
return conn;
104-
})
105-
.UseMonitorCommand(cmd => Debug.WriteLine($"Sql:{cmd.CommandText}"))
106-
.Build<MySqlFlag>();
107-
});
108-
//Func<IServiceProvider, IFreeSql<MySqlFlag>> fsqlMysql = r =>
109-
//{
110-
// IFreeSql<MySqlFlag> fsql1 = new FreeSqlBuilder().UseConnectionFactory(DataType.MySql, () =>
111-
// {
112-
113-
114-
115-
// var conn = new MySqlConnection("data source=192.168.200.131;port=3306;user id=game;password=uu5!^%jg;initial catalog=taiwan_cain;sslmode=none;max pool size=12;Charset=latin1;");
116-
// conn.Open();
117-
// MySqlCommand cmd = conn.CreateCommand();
118-
// cmd.CommandText = "SET Charset latin1;";
119-
// cmd.ExecuteNonQuery();
120-
// //conn.Close();
121-
// return conn;
122-
// })
123-
// .UseMonitorCommand(cmd => Debug.WriteLine($"Sql:{cmd.CommandText}"))
124-
// .Build<MySqlFlag>();
125-
126-
// return fsql1;
127-
//};
128-
93+
services.AddTransient<LoginWindowViewModel>();
12994

13095
Func<IServiceProvider, IFreeSql<SqliteFlag>> fsqlSqlite = r =>
13196
{
@@ -139,9 +104,7 @@ public App()
139104
return fsql2;
140105
};
141106

142-
//services.AddSingleton<IFreeSql<MySqlFlag>>(fsqlMysql);
143107
services.AddSingleton<IFreeSql<SqliteFlag>>(fsqlSqlite);
144-
services.AddSingleton<IDatabaseService, DatabaseService>();// 注册动态数据库服务
145108
// Configuration
146109
services.Configure<LocalSettingsOptions>(context.Configuration.GetSection(nameof(LocalSettingsOptions)));
147110
}).
@@ -161,20 +124,29 @@ private async void App_UnhandledException(object sender, Microsoft.UI.Xaml.Unhan
161124

162125
var dialog = new ContentDialog
163126
{
164-
Title = "Unhandled Exception",
165-
Content = $"An unhandled exception occurred: {e.Exception.Message}",
127+
Title = "错误!",
128+
Content = $" {e.Exception.Message}",
166129
CloseButtonText = "我知道了",
167-
XamlRoot = MainWindow.Content.XamlRoot
130+
XamlRoot = App.CurrentWindow.Content.XamlRoot//MainWindow.Content.XamlRoot
168131
};
169132

170133
await dialog.ShowAsync();
171134
}
172135

136+
public static Window CurrentWindow = Window.Current;
173137

174-
protected override async void OnLaunched(LaunchActivatedEventArgs args)
138+
protected override void OnLaunched(LaunchActivatedEventArgs args)
175139
{
140+
CurrentWindow = new LoginWindow();
141+
142+
App.MainWindow.Closed += CurrentWindow_Closed;
143+
CurrentWindow.Activate();
176144
base.OnLaunched(args);
177145

178-
await App.GetService<IActivationService>().ActivateAsync(args);
146+
//await App.GetService<IActivationService>().ActivateAsync(args);
147+
}
148+
private void CurrentWindow_Closed(object sender, WindowEventArgs e)
149+
{
150+
DatabaseHelper.Instance.Dispose();
179151
}
180152
}

src/DofGMTool/Assets/windowbg.jpg

1.44 MB
Loading

src/DofGMTool/Contracts/Services/ICharacterManagerService.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public interface ICharacterManagerService
2424
/// <param name="mid"></param>
2525
/// <param name="payValue"></param>
2626
/// <param name="pay"></param>
27-
void AccountRecharge( int payValue, Pay pay);
27+
void AccountRecharge(int payValue, Pay pay);
2828

2929
/// <summary>
3030
/// 转职

src/DofGMTool/Contracts/Services/IDatabaseService.cs

-31
This file was deleted.

src/DofGMTool/DofGMTool.csproj

+7
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
<None Remove="Assets\inventory.png" />
3535
<None Remove="Views\CharacterInfoDialog\ChangeEquipDialog.xaml" />
3636
<None Remove="Views\CharacterInfoDialog\PowerupEquip.xaml" />
37+
<None Remove="Views\LoginWindow.xaml" />
3738
</ItemGroup>
3839
<ItemGroup>
3940
<Content Include="Assets\ImagePacks\**\*.*">
@@ -108,11 +109,17 @@
108109
<Content Update="Assets\icons8\icons8-3ds-36.png">
109110
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
110111
</Content>
112+
<Content Update="Assets\windowbg.jpg">
113+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
114+
</Content>
111115
</ItemGroup>
112116
<ItemGroup>
113117
<None Update="appsettings.json">
114118
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
115119
</None>
120+
<Page Update="Views\LoginWindow.xaml">
121+
<Generator>MSBuild:Compile</Generator>
122+
</Page>
116123
<Page Update="Views\CharacterInfoDialog\ChangeEquipDialog.xaml">
117124
<Generator>MSBuild:Compile</Generator>
118125
</Page>
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using DofGMTool.Models;
2+
using FreeSql;
3+
using MySqlConnector;
4+
using System.Data.Common;
5+
using System.Diagnostics;
6+
7+
namespace DofGMTool.Helpers;
8+
9+
public class DatabaseHelper : IDisposable
10+
{
11+
12+
public string? Ip { get; set; }
13+
public string? Port { get; set; }
14+
public string? User { get; set; }
15+
public string? Password { get; set; }
16+
17+
// 单例实例
18+
public static DatabaseHelper Instance { get; } = new DatabaseHelper();
19+
20+
// 私有构造函数,防止外部实例化
21+
private DatabaseHelper() { }
22+
23+
private MySqlConnection? _connection;
24+
// 其他方法
25+
public IFreeSql<MySqlFlag> GetMySqlConnection(string databaseName)
26+
{
27+
// 使用实例属性构建连接字符串
28+
IFreeSql<MySqlFlag> fsql = new FreeSqlBuilder()
29+
.UseConnectionFactory(DataType.MySql, () =>
30+
{
31+
_connection = new MySqlConnection($"data source={Ip};port={Port};user id={User};password={Password};initial catalog={databaseName};sslmode=none;max pool size=50;Charset=latin1;ConvertZeroDateTime=True;");
32+
_connection.Open();
33+
MySqlCommand cmd = _connection.CreateCommand();
34+
cmd.CommandText = "SET Charset latin1;";
35+
cmd.ExecuteNonQuery();
36+
return _connection;
37+
})
38+
.UseMonitorCommand(cmd => Debug.WriteLine($"Sql:{cmd.CommandText}"))
39+
.Build<MySqlFlag>();
40+
return fsql;
41+
}
42+
43+
// 实现IDisposable接口
44+
public void Dispose()
45+
{
46+
_connection?.Close();
47+
_connection?.Dispose();
48+
}
49+
}

src/DofGMTool/Helpers/EnumHelper.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
using System;
21
using System.ComponentModel;
32
using System.Reflection;
43

5-
namespace DofGMTool.Helpers
4+
namespace DofGMTool.Helpers;
5+
6+
public static class EnumHelper
67
{
7-
public static class EnumHelper
8+
public static string GetEnumDescription(Enum value)
89
{
9-
public static string GetEnumDescription(Enum value)
10-
{
11-
FieldInfo field = value.GetType().GetField(value.ToString());
12-
DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>();
13-
return attribute == null ? value.ToString() : attribute.Description;
14-
}
10+
FieldInfo field = value.GetType().GetField(value.ToString());
11+
DescriptionAttribute attribute = field.GetCustomAttribute<DescriptionAttribute>();
12+
return attribute == null ? value.ToString() : attribute.Description;
1513
}
1614
}

src/DofGMTool/Helpers/StringToVisibilityConverter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class StringToVisibilityConverter : IValueConverter
77
{
88
public object Convert(object value, Type targetType, object parameter, string language)
99
{
10-
if (value is int intValue && intValue == 0 || value == null)
10+
if ((value is int intValue && intValue == 0) || value == null)
1111
{
1212
value = string.Empty;//return string.Empty;
1313
}
+7-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
using FreeSql.DatabaseModel;using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Reflection;
6-
using System.Threading.Tasks;
1+
using FreeSql.DataAnnotations;
72
using Newtonsoft.Json;
8-
using FreeSql.DataAnnotations;
93

10-
namespace DofGMTool.Models {
4+
namespace DofGMTool.Models;
115

12-
[JsonObject(MemberSerialization.OptIn), Table(Name = "allow_proxy_user", DisableSyncStructure = true)]
13-
public partial class AllowProxyUser {
6+
[JsonObject(MemberSerialization.OptIn), Table(Name = "allow_proxy_user", DisableSyncStructure = true)]
7+
public partial class AllowProxyUser
8+
{
149

15-
[JsonProperty, Column(Name = "m_id", IsPrimary = true)]
16-
public uint MId { get; set; } = 0;
17-
18-
}
10+
[JsonProperty, Column(Name = "m_id", IsPrimary = true)]
11+
public uint MId { get; set; } = 0;
1912

2013
}
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
using FreeSql.DatabaseModel;using System;
2-
using System.Collections;
3-
using System.Collections.Generic;
4-
using System.Linq;
5-
using System.Reflection;
6-
using System.Threading.Tasks;
1+
using FreeSql.DataAnnotations;
72
using Newtonsoft.Json;
8-
using FreeSql.DataAnnotations;
93

10-
namespace DofGMTool.Models {
4+
namespace DofGMTool.Models;
115

12-
[JsonObject(MemberSerialization.OptIn), Table(Name = "auto_punish_blackip_info", DisableSyncStructure = true)]
13-
public partial class AutoPunishBlackipInfo {
6+
[JsonObject(MemberSerialization.OptIn), Table(Name = "auto_punish_blackip_info", DisableSyncStructure = true)]
7+
public partial class AutoPunishBlackipInfo
8+
{
149

15-
[JsonProperty, Column(Name = "end_ip", IsPrimary = true)]
16-
public byte EndIp { get; set; } = 0;
10+
[JsonProperty, Column(Name = "end_ip", IsPrimary = true)]
11+
public byte EndIp { get; set; } = 0;
1712

18-
[JsonProperty, Column(Name = "ip", StringLength = 11, IsPrimary = true, IsNullable = false)]
19-
public string Ip { get; set; }
13+
[JsonProperty, Column(Name = "ip", StringLength = 11, IsPrimary = true, IsNullable = false)]
14+
public string Ip { get; set; }
2015

21-
[JsonProperty, Column(Name = "start_ip", IsPrimary = true)]
22-
public byte StartIp { get; set; } = 0;
16+
[JsonProperty, Column(Name = "start_ip", IsPrimary = true)]
17+
public byte StartIp { get; set; } = 0;
2318

24-
[JsonProperty, Column(Name = "apply_flag", DbType = "tinyint(4)")]
25-
public sbyte ApplyFlag { get; set; } = 0;
19+
[JsonProperty, Column(Name = "apply_flag", DbType = "tinyint(4)")]
20+
public sbyte ApplyFlag { get; set; } = 0;
2621

27-
[JsonProperty, Column(Name = "reg_date", DbType = "datetime")]
28-
public DateTime RegDate { get; set; }
29-
30-
}
22+
[JsonProperty, Column(Name = "reg_date", DbType = "datetime")]
23+
public DateTime RegDate { get; set; }
3124

3225
}

0 commit comments

Comments
 (0)