Skip to content
This repository has been archived by the owner on Sep 1, 2024. It is now read-only.

Commit

Permalink
refactor: Better solutions for table creation
Browse files Browse the repository at this point in the history
  • Loading branch information
K4ryuu committed Feb 6, 2024
1 parent 0858409 commit 44ca022
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 14 deletions.
8 changes: 5 additions & 3 deletions src/Plugin/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public override void Load(bool hotReload)

//** ? Initialize Database tables */

ThreadHelper.ExecuteAsync(CreateMultipleTablesAsync, () =>
ThreadHelper.ExecuteAsync(CreateMultipleTablesAsync, (_) =>
{
if (hotReload)
{
Expand Down Expand Up @@ -134,7 +134,7 @@ public override void Unload(bool hotReload)
this.Dispose();
}

public async Task CreateMultipleTablesAsync()
public async Task<bool> CreateMultipleTablesAsync()
{
string timesModuleTable = @$"CREATE TABLE IF NOT EXISTS `{this.Config.DatabaseSettings.TablePrefix}k4times` (
`id` INT AUTO_INCREMENT PRIMARY KEY,
Expand Down Expand Up @@ -211,12 +211,14 @@ public async Task CreateMultipleTablesAsync()

// Commit the transaction
await Database.Instance.CommitTransactionAsync();
return true;
}
catch (Exception ex)
{
// Roll back the transaction in case of an error
Logger.LogError("Error creating tables: {0}", ex.Message);
await Database.Instance.RollbackTransactionAsync();
Logger.LogError("Error creating tables: {0}", ex.Message);
return false;
}
}

Expand Down
11 changes: 0 additions & 11 deletions src/Plugin/PluginThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,6 @@ namespace K4System
{
public static class ThreadHelper
{
public static void ExecuteAsync(Func<Task> asyncOperation, Action mainThreadContinuation)
{
SynchronizationContext? originalContext = SynchronizationContext.Current;

Task.Run(async () =>
{
await asyncOperation();
originalContext?.Post(_ => Server.NextWorldUpdate(mainThreadContinuation), null);
});
}

public static void ExecuteAsync<TResult>(
Func<Task<TResult>> asyncOperation,
Action<TResult> mainThreadContinuation)
Expand Down

0 comments on commit 44ca022

Please sign in to comment.