Skip to content

Commit 10314ce

Browse files
authored
Merge pull request #130 from Refactorio/Updater
Change FactorioUpdater to copy all extracted directories not just data and bin
2 parents f409a0b + 4c2d68e commit 10314ce

3 files changed

Lines changed: 33 additions & 42 deletions

File tree

FactorioWebInterface/Services/Discord/DiscordService.cs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,9 @@ public DiscordService(IDiscordServiceConfiguration configuration,
8888

8989
public async Task Init()
9090
{
91+
await discordLock.WaitAsync();
9192
try
9293
{
93-
await discordLock.WaitAsync();
94-
9594
using (var context = _dbContextFactory.Create<ApplicationDbContext>())
9695
{
9796
var servers = context.DiscordServers.AsQueryable().ToArrayAsync();
@@ -173,10 +172,9 @@ public async Task<Result> SetNamedChannel(string name, ulong channelId)
173172
return Result.Failure(Constants.InvalidNameErrorKey, "Channel name can not be empty or whitespace or contain space ' ' characters.");
174173
}
175174

175+
await discordLock.WaitAsync();
176176
try
177177
{
178-
await discordLock.WaitAsync();
179-
180178
using (var context = _dbContextFactory.Create<ApplicationDbContext>())
181179
{
182180
NamedDiscordChannel[] query = await context.NamedDiscordChannels.AsQueryable().Where(x => x.Name == name).ToArrayAsync();
@@ -217,10 +215,9 @@ public async Task<Result> SetNamedChannel(string name, ulong channelId)
217215

218216
public async Task<Result> UnSetNamedChannel(string name)
219217
{
218+
await discordLock.WaitAsync();
220219
try
221220
{
222-
await discordLock.WaitAsync();
223-
224221
using (var context = _dbContextFactory.Create<ApplicationDbContext>())
225222
{
226223
NamedDiscordChannel[] query = await context.NamedDiscordChannels.AsQueryable().Where(x => x.Name == name).ToArrayAsync();
@@ -256,10 +253,9 @@ public async Task<Result> UnSetNamedChannel(string name)
256253

257254
public async Task<Result<string?>> UnSetServer(ulong channelId)
258255
{
256+
await discordLock.WaitAsync();
259257
try
260258
{
261-
await discordLock.WaitAsync();
262-
263259
using (var context = _dbContextFactory.Create<ApplicationDbContext>())
264260
{
265261
DiscordServers[] query = await context.DiscordServers.AsQueryable().Where(x => x.DiscordChannelId == channelId).ToArrayAsync();
@@ -322,10 +318,9 @@ public async Task SendToNamedChannel(string name, string? text = null, Embed? em
322318

323319
public async Task<Result<(string name, ulong channel)[]>> GetNamedChannels()
324320
{
321+
await discordLock.WaitAsync();
325322
try
326323
{
327-
await discordLock.WaitAsync();
328-
329324
var array = nameToDiscord.Select(x => (x.Key, x.Value)).ToArray();
330325
return Result<(string name, ulong channel)[]>.OK(array);
331326
}
@@ -353,10 +348,9 @@ public async Task ScheduleUpdateChannelNameAndTopic(string serverId)
353348

354349
private async Task<Result> SetServerInner(string serverId, ulong channelId)
355350
{
351+
await discordLock.WaitAsync();
356352
try
357353
{
358-
await discordLock.WaitAsync();
359-
360354
using (var context = _dbContextFactory.Create<ApplicationDbContext>())
361355
{
362356
DiscordServers[] query = await context.DiscordServers.AsQueryable().Where(x => x.DiscordChannelId == channelId || x.ServerId == serverId).ToArrayAsync();
@@ -401,9 +395,9 @@ private async Task<Result> SetServerInner(string serverId, ulong channelId)
401395
return null;
402396
}
403397

398+
await discordLock.WaitAsync();
404399
try
405400
{
406-
await discordLock.WaitAsync();
407401
if (!serverdToDiscord.TryGetValue(serverId, out ulong channelId))
408402
{
409403
return null;
@@ -424,9 +418,9 @@ private async Task<Result> SetServerInner(string serverId, ulong channelId)
424418
return null;
425419
}
426420

421+
await discordLock.WaitAsync();
427422
try
428423
{
429-
await discordLock.WaitAsync();
430424
if (!nameToDiscord.TryGetValue(channelName, out ulong channelId))
431425
{
432426
return null;
@@ -474,9 +468,9 @@ private static void EnqueueMessage(IMessageQueue messageQueue, string? text = nu
474468
return null;
475469
}
476470

471+
await discordLock.WaitAsync();
477472
try
478473
{
479-
await discordLock.WaitAsync();
480474
if (!serverdToDiscord.TryGetValue(serverId, out ulong channelId))
481475
{
482476
return null;
@@ -500,10 +494,9 @@ private static void EnqueueMessage(IMessageQueue messageQueue, string? text = nu
500494
private async void MessageReceived(IDiscordMessageHandlingService sender, MessageReceivedEventArgs eventArgs)
501495
{
502496
string serverId;
497+
await discordLock.WaitAsync();
503498
try
504499
{
505-
await discordLock.WaitAsync();
506-
507500
if (!discordToServer.TryGetValue(eventArgs.Channel.Id, out string? id))
508501
{
509502
return;

FactorioWebInterface/Services/FactorioUpdater.cs

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,9 @@ public async Task<List<string>> GetDownloadableVersions()
175175

176176
public async Task<FileInfo?> Download(string version)
177177
{
178+
await downloadLock.WaitAsync();
178179
try
179180
{
180-
await downloadLock.WaitAsync();
181-
182181
var cache = new DirectoryInfo(_factorioServerDataService.UpdateCacheDirectoryPath);
183182
if (!cache.Exists)
184183
{
@@ -238,8 +237,6 @@ public async Task<Result> DoUpdate(FactorioServerData serverData, string version
238237

239238
string basePath = serverData.BaseDirectoryPath;
240239
var extractDirectoryPath = Path.Combine(basePath, "factorio");
241-
var binDirectoryPath = Path.Combine(basePath, "bin");
242-
var dataDirectoryPath = Path.Combine(basePath, "data");
243240

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

250247
bool success = await ProcessHelper.RunProcessToEndAsync("/bin/tar", $"-xJf {binaries.FullName} -C {basePath}");
251-
252-
var binDirectory = new DirectoryInfo(binDirectoryPath);
253-
if (binDirectory.Exists)
254-
{
255-
binDirectory.Delete(true);
256-
}
257-
var dataDirectory = new DirectoryInfo(dataDirectoryPath);
258-
if (dataDirectory.Exists)
259-
{
260-
dataDirectory.Delete(true);
261-
}
262-
263248
if (success)
264249
{
265-
Directory.Move(Path.Combine(extractDirectoryPath, "bin"), binDirectoryPath);
266-
Directory.Move(Path.Combine(extractDirectoryPath, "data"), dataDirectoryPath);
250+
extractDirectory.Refresh();
251+
var subDirectories = extractDirectory.EnumerateDirectories();
252+
foreach (var subDirectory in subDirectories)
253+
{
254+
string targetPath = Path.Combine(basePath, subDirectory.Name);
255+
var targetDirectory = new DirectoryInfo(targetPath);
256+
if (targetDirectory.Exists)
257+
{
258+
targetDirectory.Delete(true);
259+
}
260+
261+
subDirectory.MoveTo(targetDirectory.FullName);
262+
}
267263

268264
var configFile = new FileInfo(Path.Combine(basePath, "config-path.cfg"));
269265
if (!configFile.Exists)

FactorioWrapper/MainLoop.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,9 @@ private async Task RestartWrapperAsync()
243243

244244
private async Task SendToFactorio(string data)
245245
{
246+
await factorioProcessLock.WaitAsync();
246247
try
247248
{
248-
await factorioProcessLock.WaitAsync();
249-
250249
var p = factorioProcess;
251250
if (p != null && !p.HasExited)
252251
{
@@ -299,12 +298,14 @@ private void BuildConenction()
299298

300299
connection.On(nameof(IFactorioProcessClientMethods.Stop), async () =>
301300
{
301+
bool lockAquired = false;
302302
try
303303
{
304304
exitClearMessageQueueCancelSource?.Cancel();
305305
exitClearMessageQueueCancelSource = new CancellationTokenSource(TimeSpan.FromSeconds(30));
306306

307307
await factorioProcessLock.WaitAsync();
308+
lockAquired = true;
308309

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

@@ -326,7 +327,11 @@ private void BuildConenction()
326327
// If an error is throw above the status wont be changed.
327328
// This changes the status in case the factorio process hasn't started yet, to make sure it doesn't start.
328329
status = FactorioServerStatus.Stopping;
329-
factorioProcessLock.Release();
330+
331+
if (lockAquired)
332+
{
333+
factorioProcessLock.Release();
334+
}
330335
}
331336
});
332337

@@ -373,10 +378,9 @@ private void BuildConenction()
373378

374379
private async Task StartFactorioProcess()
375380
{
381+
await factorioProcessLock.WaitAsync();
376382
try
377383
{
378-
await factorioProcessLock.WaitAsync();
379-
380384
// Check to see if the server has been requested to stop.
381385
if (status != FactorioServerStatus.WrapperStarted)
382386
{
@@ -432,8 +436,6 @@ private async Task StartFactorioProcess()
432436
}
433437
}
434438

435-
436-
437439
private async void FactorioProcess_OutputDataReceived(object sender, DataReceivedEventArgs e)
438440
{
439441
var data = e.Data;

0 commit comments

Comments
 (0)