Skip to content

Commit fce895e

Browse files
committed
Switch to MySqlConnector and bundle dependencies where possible
1 parent 3c9f437 commit fce895e

File tree

6 files changed

+105
-25
lines changed

6 files changed

+105
-25
lines changed

.editorconfig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# EditorConfig is awesome: http://editorconfig.org
22
root = true
33

4+
[*.*]
5+
indent_size = 2
6+
47
[*.cs]
58
end_of_line = lf
69
insert_final_newline = true

src/Connection.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
#if NET35
1+
#if NET35
22
using System.Security.Permissions;
33
#endif
4+
#if USE_MYSQLCONNECTOR
5+
using MySqlConnector;
6+
#else
47
using MySql.Data.MySqlClient;
8+
#endif
59
using Oxide.Core.Plugins;
610

711
namespace Oxide.Ext.MySql

src/ILRepack.Exclude.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
^MySqlConnector
2+
^Oxide\.MySql
3+
^Oxide\.Core\.MySql

src/Libraries/MySql.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using MySql.Data.MySqlClient;
1+
#if USE_MYSQLCONNECTOR
2+
using MySqlConnector;
3+
#else
4+
using MySql.Data.MySqlClient;
5+
#endif
26
using Oxide.Core.Database;
37
using Oxide.Core.Libraries;
48
using Oxide.Core.Plugins;
@@ -17,10 +21,10 @@ public class MySql : Library, IDatabaseProvider
1721
private readonly object _syncroot = new object();
1822
private readonly AutoResetEvent _workevent = new AutoResetEvent(false);
1923
private readonly HashSet<Connection> _runningConnections = new HashSet<Connection>();
20-
private bool _running = true;
2124
private readonly Dictionary<string, Dictionary<string, Connection>> _connections = new Dictionary<string, Dictionary<string, Connection>>();
2225
private readonly Thread _worker;
2326
private readonly Dictionary<Plugin, Event.Callback<Plugin, PluginManager>> _pluginRemovedFromManager;
27+
private bool _running = true;
2428

2529
/// <summary>
2630
/// Represents a single MySqlQuery instance
@@ -54,7 +58,6 @@ public class MySqlQuery
5458

5559
private MySqlCommand _cmd;
5660
private MySqlConnection _connection;
57-
private IAsyncResult _result;
5861

5962
private void Cleanup()
6063
{
@@ -77,8 +80,7 @@ public bool Handle()
7780
{
7881
throw new Exception("Connection is null");
7982
}
80-
//if (_result == null)
81-
//{
83+
8284
_connection = (MySqlConnection)Connection.Con;
8385
if (_connection.State == ConnectionState.Closed)
8486
{
@@ -89,17 +91,14 @@ public bool Handle()
8991
_cmd.CommandTimeout = 120;
9092
_cmd.CommandText = Sql.SQL;
9193
Sql.AddParams(_cmd, Sql.Arguments, "@");
92-
_result = NonQuery ? _cmd.BeginExecuteNonQuery() : _cmd.BeginExecuteReader();
93-
//}
94-
_result.AsyncWaitHandle.WaitOne();
95-
//if (!_result.IsCompleted) return false;
94+
9695
if (NonQuery)
9796
{
98-
nonQueryResult = _cmd.EndExecuteNonQuery(_result);
97+
nonQueryResult = _cmd.ExecuteNonQuery();
9998
}
10099
else
101100
{
102-
using (MySqlDataReader reader = _cmd.EndExecuteReader(_result))
101+
using (MySqlDataReader reader = _cmd.ExecuteReader())
103102
{
104103
list = new List<Dictionary<string, object>>();
105104
while (reader.Read())
@@ -108,15 +107,18 @@ public bool Handle()
108107
{
109108
break;
110109
}
110+
111111
var dict = new Dictionary<string, object>();
112112
for (int i = 0; i < reader.FieldCount; i++)
113113
{
114114
dict.Add(reader.GetName(i), reader.GetValue(i));
115115
}
116+
116117
list.Add(dict);
117118
}
118119
}
119120
}
121+
120122
lastInsertRowId = _cmd.LastInsertedId;
121123
Cleanup();
122124
}
@@ -131,6 +133,7 @@ public bool Handle()
131133
Interface.Oxide.LogException(message, ex);
132134
Cleanup();
133135
}
136+
134137
Interface.Oxide.NextTick(() =>
135138
{
136139
Connection?.Plugin?.TrackStart();
@@ -160,6 +163,7 @@ public bool Handle()
160163

161164
Interface.Oxide.LogException(message, ex);
162165
}
166+
163167
Connection?.Plugin?.TrackEnd();
164168
});
165169
return true;
@@ -200,6 +204,7 @@ private void Worker()
200204
_runningConnections.Clear();
201205
}
202206
}
207+
203208
if (query != null)
204209
{
205210
query.Handle();
@@ -251,6 +256,7 @@ public Connection OpenDb(string conStr, Plugin plugin, bool persistent = false)
251256
};
252257
connections[conStr] = connection;
253258
}
259+
254260
if (plugin != null && !_pluginRemovedFromManager.ContainsKey(plugin))
255261
{
256262
_pluginRemovedFromManager[plugin] = plugin.OnRemovedFromManager.Add(OnRemovedFromManager);
@@ -279,8 +285,10 @@ private void OnRemovedFromManager(Plugin sender, PluginManager manager)
279285
connection.Value.Con?.Close();
280286
connection.Value.Plugin = null;
281287
}
288+
282289
_connections.Remove(sender.Name);
283290
}
291+
284292
Event.Callback<Plugin, PluginManager> event_callback;
285293
if (_pluginRemovedFromManager.TryGetValue(sender, out event_callback))
286294
{
@@ -315,6 +323,7 @@ public void CloseDb(Connection db)
315323
}
316324
}
317325
}
326+
318327
db.Con?.Close();
319328
db.Plugin = null;
320329
}

src/Oxide.MySql.csproj

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<Project>
33
<Import Project="Sdk.props" Sdk="Microsoft.NET.Sdk" />
44
<Import Project="..\netfx.props" />
@@ -16,29 +16,43 @@
1616
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
1717
<NoWarn>NU1701;NU5128</NoWarn>
1818
</PropertyGroup>
19+
<PropertyGroup>
20+
<UseMySqlData>false</UseMySqlData>
21+
<UseMySqlData Condition="'$(TargetFramework)' == 'net35' Or '$(TargetFramework)' == 'net40' Or '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net46'">true</UseMySqlData>
22+
<UseMySqlConnector>false</UseMySqlConnector>
23+
<UseMySqlConnector Condition="'$(TargetFramework)' == 'net48' Or '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">true</UseMySqlConnector>
24+
</PropertyGroup>
25+
<PropertyGroup Condition="'$(UseMySqlConnector)' == 'true'">
26+
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
27+
<DefineConstants>$(DefineConstants);USE_MYSQLCONNECTOR</DefineConstants>
28+
</PropertyGroup>
1929
<ItemGroup>
2030
<PackageReference Include="Oxide.Core" Version="2.0.*" />
21-
<Content Include="Dependencies\*.*" Exclude="Dependencies\MySql.Data.dll" PackagePath="lib\any" />
2231
<None Include="..\resources\icon.png" Pack="true" PackagePath="\" />
2332
</ItemGroup>
2433
<ItemGroup Condition="'$(TargetFramework)' == 'net35'">
25-
<Reference Include="MySql.Data">
26-
<HintPath>Dependencies\MySql.Data.dll</HintPath>
27-
</Reference>
34+
<Reference Include="MySql.Data" HintPath="Dependencies\MySql.Data.dll" />
35+
</ItemGroup>
36+
<ItemGroup Condition="'$(UseMySqlData)' == 'true' And '$(TargetFramework)' != 'net35'">
37+
<PackageReference Include="MySql.Data" Version="6.9.12" PrivateAssets="all" />
2838
</ItemGroup>
29-
<ItemGroup Condition="'$(TargetFramework)' == 'net40' Or '$(TargetFramework)' == 'net45' Or '$(TargetFramework)' == 'net46'">
30-
<PackageReference Include="MySql.Data" Version="6.9.12" />
39+
<ItemGroup Condition="'$(UseMySqlConnector)' == 'true'">
40+
<PackageReference Include="MySqlConnector" Version="2.3.7" PrivateAssets="all" />
41+
<PackageReference Include="ILRepack.Lib.MSBuild.Task" Version="2.0.18.1" PrivateAssets="all" />
3142
</ItemGroup>
32-
<ItemGroup Condition="'$(TargetFramework)' == 'net48' Or '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">
33-
<PackageReference Include="MySql.Data" Version="9.5.0" />
43+
<ItemGroup Condition="Exists('Dependencies\MySql.Data.dll')">
44+
<None Include="Dependencies\MySql.Data.dll" Pack="true" PackagePath="lib\net35" />
45+
<None Include="$(NuGetPackageRoot)mysql.data\6.9.12\lib\net40\MySql.Data.dll" Pack="true" PackagePath="lib\net40" />
46+
<None Include="$(NuGetPackageRoot)mysql.data\6.9.12\lib\net45\MySql.Data.dll" Pack="true" PackagePath="lib\net45;lib\net46" />
47+
<None Include="Dependencies\I18N*.dll" Pack="true" PackagePath="lib\net35;lib\net40;lib\net45;lib\net46" />
48+
<None Include="Dependencies\System.Configuration.Install.dll" Pack="true" PackagePath="lib\net35;lib\net40;lib\net45;lib\net46" />
3449
</ItemGroup>
3550
<ItemGroup>
36-
<None Include="Dependencies\MySql.Data.dll" Condition="Exists('Dependencies\MySql.Data.dll')">
37-
<Pack>true</Pack>
38-
<PackagePath>lib\net35</PackagePath>
39-
</None>
51+
<None Include="$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\7.0.1\lib\net462\Microsoft.Extensions.Logging.Abstractions.dll" Pack="true" PackagePath="lib\net48" />
52+
<None Include="$(NuGetPackageRoot)microsoft.extensions.logging.abstractions\7.0.1\lib\netstandard2.0\Microsoft.Extensions.Logging.Abstractions.dll" Pack="true" PackagePath="lib\netstandard2.0;lib\netstandard2.1" />
4053
</ItemGroup>
4154
<Import Project="Sdk.targets" Sdk="Microsoft.NET.Sdk" />
55+
<Import Project="OxideILRepack.targets" Condition="Exists('OxideILRepack.targets')" />
4256
<Target Name="ChangeAliasesOfStrongNameAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
4357
<ItemGroup>
4458
<ReferencePath Condition="'%(FileName)' == 'Oxide.References'">

src/OxideILRepack.targets

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ShouldILRepack>false</ShouldILRepack>
5+
<ShouldILRepack Condition="'$(TargetFramework)' == 'net48' Or '$(TargetFramework)' == 'netstandard2.0' Or '$(TargetFramework)' == 'netstandard2.1'">true</ShouldILRepack>
6+
</PropertyGroup>
7+
<Target Name="ILRepack" AfterTargets="Build" Condition="'$(ShouldILRepack)' == 'true'">
8+
<Message Text="[ILRepack] Starting merge for $(TargetFramework)..." Importance="high" />
9+
<PropertyGroup>
10+
<PrimaryAssembly>$(OutputPath)$(TargetName)$(TargetExt)</PrimaryAssembly>
11+
<MergedOutputPath>$(OutputPath)merged\$(TargetName)$(TargetExt)</MergedOutputPath>
12+
<InternalizeExcludeFile>$(ProjectDir)ILRepack.Exclude.txt</InternalizeExcludeFile>
13+
<ILRepackExe>$(NuGetPackageRoot)ilrepack\2.0.18\tools\ILRepack.exe</ILRepackExe>
14+
<ILRepackExe Condition="!Exists('$(ILRepackExe)')">$(USERPROFILE)\.nuget\packages\ilrepack\2.0.18\tools\ILRepack.exe</ILRepackExe>
15+
</PropertyGroup>
16+
<ItemGroup>
17+
<DependencyToMerge Include="MySqlConnector" />
18+
<DependencyToMerge Include="System.Buffers" />
19+
<DependencyToMerge Include="System.Diagnostics.DiagnosticSource" />
20+
<DependencyToMerge Include="System.Memory" />
21+
<DependencyToMerge Include="System.Numerics.Vectors" />
22+
<DependencyToMerge Include="System.Runtime.CompilerServices.Unsafe" />
23+
<DependencyToMerge Include="System.Threading.Tasks.Extensions" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<MergeAssemblies Include="$(PrimaryAssembly)" />
27+
<MergeAssemblies Include="$(OutputPath)%(DependencyToMerge.Identity).dll" Condition="Exists('$(OutputPath)%(DependencyToMerge.Identity).dll')" />
28+
</ItemGroup>
29+
<PropertyGroup>
30+
<ILRepackArgs>/parallel /internalize:$(InternalizeExcludeFile) /copyattrs /allowMultiple /allowDup /union /target:library /out:$(MergedOutputPath) /lib:$(OutputPath) @(MergeAssemblies, ' ')</ILRepackArgs>
31+
</PropertyGroup>
32+
<MakeDir Directories="$(OutputPath)merged" />
33+
<Exec Command="&quot;$(ILRepackExe)&quot; $(ILRepackArgs)" WorkingDirectory="$(ProjectDir)" />
34+
<Message Text="[ILRepack] Merge completed, replacing original assembly..." Importance="high" />
35+
<Copy SourceFiles="$(MergedOutputPath)" DestinationFiles="$(PrimaryAssembly)" />
36+
<Copy SourceFiles="$(OutputPath)merged\$(TargetName).pdb" DestinationFiles="$(OutputPath)$(TargetName).pdb" Condition="Exists('$(OutputPath)merged\$(TargetName).pdb')" />
37+
<RemoveDir Directories="$(OutputPath)merged" />
38+
<ItemGroup>
39+
<FilesToDelete Include="$(OutputPath)%(DependencyToMerge.Identity).dll" />
40+
<FilesToDelete Include="$(OutputPath)%(DependencyToMerge.Identity).xml" />
41+
<FilesToDelete Include="$(OutputPath)$(TargetName).deps.json" />
42+
</ItemGroup>
43+
<Delete Files="@(FilesToDelete)" ContinueOnError="true" />
44+
<Message Text="[ILRepack] Cleanup completed successfully!" Importance="high" />
45+
</Target>
46+
47+
</Project>

0 commit comments

Comments
 (0)