Skip to content

Commit 33cf6ad

Browse files
author
zeng-github01
committed
Covert data query method to dapper
1 parent 8f22879 commit 33cf6ad

File tree

8 files changed

+157
-92
lines changed

8 files changed

+157
-92
lines changed

CDK/CDK.csproj

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,26 @@
4444
<HintPath>lib\com.rlabrecque.steamworks.net.dll</HintPath>
4545
<Private>False</Private>
4646
</Reference>
47-
<Reference Include="Google.Protobuf, Version=3.21.4.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
48-
<HintPath>..\packages\Google.Protobuf.3.21.4\lib\net45\Google.Protobuf.dll</HintPath>
47+
<Reference Include="Dapper, Version=2.0.0.0, Culture=neutral, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Dapper.2.0.123\lib\net461\Dapper.dll</HintPath>
49+
</Reference>
50+
<Reference Include="Google.Protobuf, Version=3.21.9.0, Culture=neutral, PublicKeyToken=a7d26565bac4d604, processorArchitecture=MSIL">
51+
<HintPath>..\packages\Google.Protobuf.3.21.9\lib\net45\Google.Protobuf.dll</HintPath>
4952
</Reference>
5053
<Reference Include="K4os.Compression.LZ4, Version=1.2.16.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL">
5154
<HintPath>..\packages\K4os.Compression.LZ4.1.2.16\lib\net46\K4os.Compression.LZ4.dll</HintPath>
5255
</Reference>
5356
<Reference Include="K4os.Compression.LZ4.Streams, Version=1.2.16.0, Culture=neutral, PublicKeyToken=2186fa9121ef231d, processorArchitecture=MSIL">
5457
<HintPath>..\packages\K4os.Compression.LZ4.Streams.1.2.16\lib\net46\K4os.Compression.LZ4.Streams.dll</HintPath>
5558
</Reference>
56-
<Reference Include="K4os.Hash.xxHash, Version=1.0.7.0, Culture=neutral, PublicKeyToken=32cd54395057cec3, processorArchitecture=MSIL">
57-
<HintPath>..\packages\K4os.Hash.xxHash.1.0.7\lib\net46\K4os.Hash.xxHash.dll</HintPath>
59+
<Reference Include="K4os.Hash.xxHash, Version=1.0.8.0, Culture=neutral, PublicKeyToken=32cd54395057cec3, processorArchitecture=MSIL">
60+
<HintPath>..\packages\K4os.Hash.xxHash.1.0.8\lib\net462\K4os.Hash.xxHash.dll</HintPath>
5861
</Reference>
59-
<Reference Include="MySql.Data, Version=8.0.30.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
60-
<HintPath>..\packages\MySql.Data.8.0.30\lib\net48\MySql.Data.dll</HintPath>
62+
<Reference Include="MySql.Data, Version=8.0.31.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
63+
<HintPath>..\packages\MySql.Data.8.0.31\lib\net48\MySql.Data.dll</HintPath>
6164
</Reference>
6265
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
63-
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
66+
<HintPath>..\packages\Newtonsoft.Json.13.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
6467
</Reference>
6568
<Reference Include="PermissionSync, Version=1.0.1.2, Culture=neutral, processorArchitecture=MSIL">
6669
<SpecificVersion>False</SpecificVersion>
@@ -108,9 +111,6 @@
108111
<Reference Include="System.Data" />
109112
<Reference Include="System.Net.Http" />
110113
<Reference Include="System.Xml" />
111-
<Reference Include="Ubiety.Dns.Core, Version=2.2.1.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
112-
<HintPath>..\packages\MySql.Data.8.0.30\lib\net48\Ubiety.Dns.Core.dll</HintPath>
113-
</Reference>
114114
<Reference Include="Uconomy">
115115
<HintPath>lib\Uconomy.dll</HintPath>
116116
</Reference>
@@ -125,7 +125,7 @@
125125
<Private>False</Private>
126126
</Reference>
127127
<Reference Include="ZstdNet, Version=1.4.5.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d, processorArchitecture=MSIL">
128-
<HintPath>..\packages\MySql.Data.8.0.30\lib\net48\ZstdNet.dll</HintPath>
128+
<HintPath>..\packages\MySql.Data.8.0.31\lib\net48\ZstdNet.dll</HintPath>
129129
</Reference>
130130
</ItemGroup>
131131
<ItemGroup>
@@ -134,6 +134,7 @@
134134
<Compile Include="Config.cs" />
135135
<Compile Include="DatabaseManager.cs" />
136136
<Compile Include="Data\LogData.cs" />
137+
<Compile Include="Enum\EKeyReemeResult.cs" />
137138
<Compile Include="Enum\ELogQueryType.cs" />
138139
<Compile Include="Main.cs" />
139140
<Compile Include="Properties\AssemblyInfo.cs" />

CDK/DBManagerViaDapper.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using CDK.Data;
2+
using Dapper;
3+
using MySql.Data.MySqlClient;
4+
using Rocket.Core.Logging;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using CDK.Enum;
11+
12+
namespace CDK
13+
{
14+
public class DBManagerViaDapper
15+
{
16+
private MySqlConnection CreateConnection()
17+
{
18+
MySqlConnection connection = null;
19+
try
20+
{
21+
if (Main.Instance.Configuration.Instance.DatabasePort == 0)
22+
Main.Instance.Configuration.Instance.DatabasePort = 3306;
23+
connection = new MySqlConnection(
24+
$"SERVER={Main.Instance.Configuration.Instance.DatabaseAddress};DATABASE={Main.Instance.Configuration.Instance.DatabaseName};UID={Main.Instance.Configuration.Instance.DatabaseUsername};PASSWORD={Main.Instance.Configuration.Instance.DatabasePassword};PORT={Main.Instance.Configuration.Instance.DatabasePort};");
25+
}
26+
catch (Exception ex)
27+
{
28+
Logger.LogException(ex);
29+
}
30+
31+
return connection;
32+
}
33+
34+
public CDKData GetCDKData(string key)
35+
{
36+
CDKData cdkData = null;
37+
var con = CreateConnection();
38+
39+
try
40+
{
41+
cdkData = con.QuerySingle<CDKData>(
42+
$"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;",
43+
new {CDK = key});
44+
}
45+
catch (Exception ex)
46+
{
47+
Logger.LogException(ex);
48+
}
49+
finally
50+
{
51+
con.Close();
52+
}
53+
54+
return cdkData;
55+
}
56+
57+
public LogData GetLogData(string parameter, ELogQueryType type)
58+
{
59+
LogData logData = null;
60+
var con = CreateConnection();
61+
try
62+
{
63+
switch (type)
64+
{
65+
case ELogQueryType.ByCDK:
66+
logData = con.QuerySingle<LogData>(
67+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `CDK` = '@CDK'",
68+
new {CDK = parameter});
69+
break;
70+
case ELogQueryType.ByTime:
71+
logData = con.QuerySingle<LogData>(
72+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '@id' and ValidUtil < now()",
73+
new {id = parameter});
74+
break;
75+
case ELogQueryType.ByPermissionGroup:
76+
logData = con.QuerySingle<LogData>(
77+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `GrantPermissionGroup` = '@permission'",
78+
new {permission = parameter});
79+
break;
80+
}
81+
}
82+
catch (Exception e)
83+
{
84+
Logger.LogException(e);
85+
}
86+
finally
87+
{
88+
con.Close();
89+
}
90+
91+
return logData;
92+
}
93+
}
94+
}

CDK/DatabaseManager.cs

Lines changed: 29 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
using Rocket.API;
1313
using CDK.Data;
1414
using Steamworks;
15-
//using UnityEngine;
1615
using Rocket.Core.Plugins;
1716
using CDK.Enum;
1817
using PermissionSync;
18+
using Dapper;
1919

2020
namespace CDK
2121
{
@@ -75,7 +75,7 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
7575
try
7676
{
7777
var cdkdata = GetCDKData(CDK);
78-
var logdata = GetLogData(player.CSteamID, ELogQueryType.ByCDK, CDK);
78+
var logdata = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByCDK, CDK);
7979
if (cdkdata != null)
8080
{
8181
if (cdkdata.Owner != 0 && cdkdata.Owner != player.CSteamID.m_SteamID)
@@ -232,116 +232,74 @@ public RedeemCDKResult RedeemCDK(UnturnedPlayer player, string CDK)
232232

233233
internal void CheckValid(UnturnedPlayer player)
234234
{
235-
LogData logData = GetLogData(player.CSteamID, ELogQueryType.ByTime);
235+
LogData logData = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByTime);
236236
if (logData != null && logData.GrantPermissionGroup != string.Empty && !logData.UsePermissionSync)
237237
{
238238
do
239239
{
240240
CDKData cDKData = GetCDKData(logData.CDK);
241241
R.Permissions.RemovePlayerFromGroup(cDKData.GrantPermissionGroup, player);
242242
UnturnedChat.Say(player, Main.Instance.Translate("key_expired", logData.CDK));
243-
logData = GetLogData(player.CSteamID, ELogQueryType.ByTime);
243+
logData = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByTime);
244244
} while (logData == null);
245245
}
246246
}
247247

248-
private CDKData BuildCDKData(MySqlDataReader reader)
248+
249+
public CDKData GetCDKData(string key)
249250
{
250-
ulong owner = reader.IsDBNull(12) ? UInt64.MinValue : reader.GetUInt64(12);
251-
string items = reader.IsDBNull(1) ? String.Empty : reader.GetString(1);
252-
string amount = reader.IsDBNull(2) ? String.Empty : reader.GetString(2);
253-
ushort vehicle = reader.IsDBNull(2) ? UInt16.MinValue : reader.GetUInt16(2);
254-
ushort exp = reader.IsDBNull(4) ? UInt16.MinValue : reader.GetUInt16(4);
255-
decimal money = reader.IsDBNull(6) ? Decimal.Zero : reader.GetDecimal(6);
256-
int rep = reader.IsDBNull(7) ? Int32.MinValue : reader.GetInt32(7);
257-
string permission = reader.IsDBNull(7) ? String.Empty : reader.GetString(7);
258-
259-
return new CDKData(reader.GetString(0), items,
260-
amount, vehicle,
261-
exp, money,
262-
rep, permission, reader.GetInt32(9),
263-
reader.GetInt32(8), reader.GetDateTime(10), owner, reader.GetBoolean(11), reader.GetBoolean(13));
264-
}
251+
CDKData cdkData = null;
252+
var con = CreateConnection();
265253

266-
private LogData BuildLogData(MySqlDataReader reader)
267-
{
268-
return new LogData(reader.GetString(0), reader.GetUInt64(1), reader.GetDateTime(2),
269-
reader.GetDateTime(3), Convert.ToString(reader[4]), reader.GetBoolean(5));
270-
}
271-
272-
public CDKData GetCDKData(string cdk)
273-
{
274-
CDKData data = null;
275-
MySqlConnection connection = CreateConnection();
276254
try
277255
{
278-
MySqlCommand command = connection.CreateCommand();
279-
command.Parameters.AddWithValue("@CDK", cdk);
280-
command.CommandText =
281-
$"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;";
282-
connection.Open();
283-
MySqlDataReader reader = command.ExecuteReader();
284-
if (reader.Read())
285-
{
286-
data = BuildCDKData(reader);
287-
}
256+
cdkData = con.QuerySingle<CDKData>(
257+
$"SELECT * from `{Main.Instance.Configuration.Instance.DatabaseCDKTableName}` where `CDK` = @CDK;",
258+
new { CDK = key });
288259
}
289260
catch (Exception ex)
290261
{
291-
Logger.LogError("[Error] GetCDKData:");
292262
Logger.LogException(ex);
293263
}
294264
finally
295265
{
296-
connection.Close();
266+
con.Close();
297267
}
298268

299-
return data;
269+
return cdkData;
300270
}
301271

302-
public LogData GetLogData(CSteamID steamID, ELogQueryType type, string parameter = "")
272+
public LogData GetLogData(ulong steamid, ELogQueryType type, string parameter = "")
303273
{
304274
LogData logData = null;
305-
MySqlConnection connection = CreateConnection();
275+
var con = CreateConnection();
306276
try
307277
{
308-
MySqlCommand command = connection.CreateCommand();
309278
switch (type)
310279
{
311280
case ELogQueryType.ByCDK:
312-
command.Parameters.AddWithValue("@steamid", steamID);
313-
command.Parameters.AddWithValue("@cdk", parameter);
314-
command.CommandText =
315-
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = @steamid AND `CDK` = @cdk;";
316-
break;
317-
case ELogQueryType.ByPermissionGroup:
318-
command.Parameters.AddWithValue("@steamid", steamID);
319-
command.Parameters.AddWithValue("@PermissionGroup", parameter);
320-
command.CommandText =
321-
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = @steamid AND `GrantPermissionGroup` = '@PermissionGroup';";
281+
logData = con.QuerySingle<LogData>(
282+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and`CDK` = '@CDK'",
283+
new { CDK = parameter });
322284
break;
323285
case ELogQueryType.ByTime:
324-
command.Parameters.AddWithValue("@steamid", steamID);
325-
command.CommandText =
326-
$"select * from `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = @steamid and `ValidUntil` < now() LIMIT 1;";
286+
logData = con.QuerySingle<LogData>(
287+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` where `SteamID` = '{steamid}' and ValidUtil < now()");
288+
break;
289+
case ELogQueryType.ByPermissionGroup:
290+
logData = con.QuerySingle<LogData>(
291+
$"SELECT * FROM `{Main.Instance.Configuration.Instance.DatabaseRedeemLogTableName}` WHERE `SteamID` = '{steamid}' and`GrantPermissionGroup` = '@permission'",
292+
new { permission = parameter });
327293
break;
328-
}
329-
330-
connection.Open();
331-
MySqlDataReader reader = command.ExecuteReader();
332-
if (reader.Read())
333-
{
334-
logData = BuildLogData(reader);
335294
}
336295
}
337-
catch (Exception ex)
296+
catch (Exception e)
338297
{
339-
Logger.LogError("[Error] GetLogData");
340-
Logger.LogException(ex);
298+
Logger.LogException(e);
341299
}
342300
finally
343301
{
344-
connection.Close();
302+
con.Close();
345303
}
346304

347305
return logData;
@@ -397,7 +355,7 @@ internal void IncreaseRedeemedTime(string cdk)
397355
var CdkData = GetCDKData(CDK);
398356
if (CdkData != null)
399357
{
400-
var log = GetLogData(player.CSteamID, ELogQueryType.ByPermissionGroup, CdkData.GrantPermissionGroup);
358+
var log = GetLogData(player.CSteamID.m_SteamID, ELogQueryType.ByPermissionGroup, CdkData.GrantPermissionGroup);
401359
if (log != null)
402360
{
403361
result = true;

CDK/Enum/EKeyReemeResult.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace CDK.Enum
8+
{
9+
public class EKeyReemeResult
10+
{
11+
}
12+
}

CDK/Main.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ private void CheckUpdate()
8383
Rocket.Core.Logging.Logger.Log(String.Format("New Update {0} has been released",version.ToString()),ConsoleColor.Green);
8484
Rocket.Core.Logging.Logger.LogWarning($"{Name} has been unload");
8585
Rocket.Core.Logging.Logger.Log("Go to " + "https://github.com/zeng-github01/CDKey-CodeReward/releases/latest"+"to get latest update", ConsoleColor.Yellow);
86-
this.UnloadPlugin();
8786
}
8887
}
8988
}

CDK/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
3333
//通过使用 "*",如下所示:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("3.3.0.6")]
36-
[assembly: AssemblyFileVersion("3.3.0.6")]
35+
[assembly: AssemblyVersion("3.3.1.0")]
36+
[assembly: AssemblyFileVersion("3.3.1.0")]

CDK/app.config

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
55
<dependentAssembly>
66
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
7-
<bindingRedirect oldVersion="0.0.0.0-8.0.30.0" newVersion="8.0.30.0" />
7+
<bindingRedirect oldVersion="0.0.0.0-8.0.31.0" newVersion="8.0.31.0" />
88
</dependentAssembly>
99
<dependentAssembly>
1010
<assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
1111
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
1212
</dependentAssembly>
1313
<dependentAssembly>
1414
<assemblyIdentity name="Google.Protobuf" publicKeyToken="a7d26565bac4d604" culture="neutral" />
15-
<bindingRedirect oldVersion="0.0.0.0-3.21.4.0" newVersion="3.21.4.0" />
15+
<bindingRedirect oldVersion="0.0.0.0-3.21.9.0" newVersion="3.21.9.0" />
1616
</dependentAssembly>
1717
<dependentAssembly>
1818
<assemblyIdentity name="K4os.Compression.LZ4.Streams" publicKeyToken="2186fa9121ef231d" culture="neutral" />
@@ -24,7 +24,7 @@
2424
</dependentAssembly>
2525
<dependentAssembly>
2626
<assemblyIdentity name="K4os.Hash.xxHash" publicKeyToken="32cd54395057cec3" culture="neutral" />
27-
<bindingRedirect oldVersion="0.0.0.0-1.0.7.0" newVersion="1.0.7.0" />
27+
<bindingRedirect oldVersion="0.0.0.0-1.0.8.0" newVersion="1.0.8.0" />
2828
</dependentAssembly>
2929
<dependentAssembly>
3030
<assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />

0 commit comments

Comments
 (0)