Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions FactorioWebInterface/Services/Discord/DiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ public DiscordService(IDiscordServiceConfiguration configuration,

public async Task Init()
{
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

using (var context = _dbContextFactory.Create<ApplicationDbContext>())
{
var servers = context.DiscordServers.AsQueryable().ToArrayAsync();
Expand Down Expand Up @@ -173,10 +172,9 @@ public async Task<Result> SetNamedChannel(string name, ulong channelId)
return Result.Failure(Constants.InvalidNameErrorKey, "Channel name can not be empty or whitespace or contain space ' ' characters.");
}

await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

using (var context = _dbContextFactory.Create<ApplicationDbContext>())
{
NamedDiscordChannel[] query = await context.NamedDiscordChannels.AsQueryable().Where(x => x.Name == name).ToArrayAsync();
Expand Down Expand Up @@ -217,10 +215,9 @@ public async Task<Result> SetNamedChannel(string name, ulong channelId)

public async Task<Result> UnSetNamedChannel(string name)
{
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

using (var context = _dbContextFactory.Create<ApplicationDbContext>())
{
NamedDiscordChannel[] query = await context.NamedDiscordChannels.AsQueryable().Where(x => x.Name == name).ToArrayAsync();
Expand Down Expand Up @@ -256,10 +253,9 @@ public async Task<Result> UnSetNamedChannel(string name)

public async Task<Result<string?>> UnSetServer(ulong channelId)
{
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

using (var context = _dbContextFactory.Create<ApplicationDbContext>())
{
DiscordServers[] query = await context.DiscordServers.AsQueryable().Where(x => x.DiscordChannelId == channelId).ToArrayAsync();
Expand Down Expand Up @@ -322,10 +318,9 @@ public async Task SendToNamedChannel(string name, string? text = null, Embed? em

public async Task<Result<(string name, ulong channel)[]>> GetNamedChannels()
{
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

var array = nameToDiscord.Select(x => (x.Key, x.Value)).ToArray();
return Result<(string name, ulong channel)[]>.OK(array);
}
Expand Down Expand Up @@ -353,10 +348,9 @@ public async Task ScheduleUpdateChannelNameAndTopic(string serverId)

private async Task<Result> SetServerInner(string serverId, ulong channelId)
{
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

using (var context = _dbContextFactory.Create<ApplicationDbContext>())
{
DiscordServers[] query = await context.DiscordServers.AsQueryable().Where(x => x.DiscordChannelId == channelId || x.ServerId == serverId).ToArrayAsync();
Expand Down Expand Up @@ -401,9 +395,9 @@ private async Task<Result> SetServerInner(string serverId, ulong channelId)
return null;
}

await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();
if (!serverdToDiscord.TryGetValue(serverId, out ulong channelId))
{
return null;
Expand All @@ -424,9 +418,9 @@ private async Task<Result> SetServerInner(string serverId, ulong channelId)
return null;
}

await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();
if (!nameToDiscord.TryGetValue(channelName, out ulong channelId))
{
return null;
Expand Down Expand Up @@ -474,9 +468,9 @@ private static void EnqueueMessage(IMessageQueue messageQueue, string? text = nu
return null;
}

await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();
if (!serverdToDiscord.TryGetValue(serverId, out ulong channelId))
{
return null;
Expand All @@ -500,10 +494,9 @@ private static void EnqueueMessage(IMessageQueue messageQueue, string? text = nu
private async void MessageReceived(IDiscordMessageHandlingService sender, MessageReceivedEventArgs eventArgs)
{
string serverId;
await discordLock.WaitAsync();
try
{
await discordLock.WaitAsync();

if (!discordToServer.TryGetValue(eventArgs.Channel.Id, out string? id))
{
return;
Expand Down
32 changes: 14 additions & 18 deletions FactorioWebInterface/Services/FactorioUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,9 @@ public async Task<List<string>> GetDownloadableVersions()

public async Task<FileInfo?> Download(string version)
{
await downloadLock.WaitAsync();
try
{
await downloadLock.WaitAsync();

var cache = new DirectoryInfo(_factorioServerDataService.UpdateCacheDirectoryPath);
if (!cache.Exists)
{
Expand Down Expand Up @@ -238,8 +237,6 @@ public async Task<Result> DoUpdate(FactorioServerData serverData, string version

string basePath = serverData.BaseDirectoryPath;
var extractDirectoryPath = Path.Combine(basePath, "factorio");
var binDirectoryPath = Path.Combine(basePath, "bin");
var dataDirectoryPath = Path.Combine(basePath, "data");

var extractDirectory = new DirectoryInfo(extractDirectoryPath);
if (extractDirectory.Exists)
Expand All @@ -248,22 +245,21 @@ public async Task<Result> DoUpdate(FactorioServerData serverData, string version
}

bool success = await ProcessHelper.RunProcessToEndAsync("/bin/tar", $"-xJf {binaries.FullName} -C {basePath}");

var binDirectory = new DirectoryInfo(binDirectoryPath);
if (binDirectory.Exists)
{
binDirectory.Delete(true);
}
var dataDirectory = new DirectoryInfo(dataDirectoryPath);
if (dataDirectory.Exists)
{
dataDirectory.Delete(true);
}

if (success)
{
Directory.Move(Path.Combine(extractDirectoryPath, "bin"), binDirectoryPath);
Directory.Move(Path.Combine(extractDirectoryPath, "data"), dataDirectoryPath);
extractDirectory.Refresh();
var subDirectories = extractDirectory.EnumerateDirectories();
foreach (var subDirectory in subDirectories)
{
string targetPath = Path.Combine(basePath, subDirectory.Name);
var targetDirectory = new DirectoryInfo(targetPath);
if (targetDirectory.Exists)
{
targetDirectory.Delete(true);
}

subDirectory.MoveTo(targetDirectory.FullName);
}

var configFile = new FileInfo(Path.Combine(basePath, "config-path.cfg"));
if (!configFile.Exists)
Expand Down
16 changes: 9 additions & 7 deletions FactorioWrapper/MainLoop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,9 @@ private async Task RestartWrapperAsync()

private async Task SendToFactorio(string data)
{
await factorioProcessLock.WaitAsync();
try
{
await factorioProcessLock.WaitAsync();

var p = factorioProcess;
if (p != null && !p.HasExited)
{
Expand Down Expand Up @@ -299,12 +298,14 @@ private void BuildConenction()

connection.On(nameof(IFactorioProcessClientMethods.Stop), async () =>
{
bool lockAquired = false;
try
{
exitClearMessageQueueCancelSource?.Cancel();
exitClearMessageQueueCancelSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));

await factorioProcessLock.WaitAsync();
lockAquired = true;

Log.Information("Stopping factorio server.");

Expand All @@ -326,7 +327,11 @@ private void BuildConenction()
// If an error is throw above the status wont be changed.
// This changes the status in case the factorio process hasn't started yet, to make sure it doesn't start.
status = FactorioServerStatus.Stopping;
factorioProcessLock.Release();

if (lockAquired)
{
factorioProcessLock.Release();
}
}
});

Expand Down Expand Up @@ -373,10 +378,9 @@ private void BuildConenction()

private async Task StartFactorioProcess()
{
await factorioProcessLock.WaitAsync();
try
{
await factorioProcessLock.WaitAsync();

// Check to see if the server has been requested to stop.
if (status != FactorioServerStatus.WrapperStarted)
{
Expand Down Expand Up @@ -432,8 +436,6 @@ private async Task StartFactorioProcess()
}
}



private async void FactorioProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
var data = e.Data;
Expand Down