Skip to content

Commit

Permalink
handle export file written when fileWatcher triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
cadon committed Nov 24, 2023
1 parent 1e1dd7e commit 5a00196
Showing 1 changed file with 76 additions and 50 deletions.
126 changes: 76 additions & 50 deletions ARKBreedingStats/importExportGun/ImportExportGun.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading;
using ARKBreedingStats.Library;
using ARKBreedingStats.values;
using Newtonsoft.Json;
Expand All @@ -21,35 +22,47 @@ public static Creature ImportCreature(string filePath, out string resultText, ou
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
return null;

try
const int tryLoadCount = 3;
const int waitAfterFailedLoadMs = 200;

for (int tryIndex = 0; tryIndex < tryLoadCount; tryIndex++)
{
string jsonText = null;
switch (Path.GetExtension(filePath))
try
{
case ".sav":
jsonText = ReadExportFile.ReadFile(filePath, "DinoExportGunSave_C", out resultText);
break;
case ".json":
jsonText = File.ReadAllText(filePath);
break;
string jsonText = null;
switch (Path.GetExtension(filePath))
{
case ".sav":
jsonText = ReadExportFile.ReadFile(filePath, "DinoExportGunSave_C", out resultText);
break;
case ".json":
jsonText = File.ReadAllText(filePath);
break;
}

if (string.IsNullOrEmpty(jsonText))
{
resultText = $"Error when importing file {filePath}: {resultText}";
return null;
}

var exportedCreature = JsonConvert.DeserializeObject<ExportGunCreatureFile>(jsonText);
if (exportedCreature == null) return null;

serverMultipliersHash = exportedCreature.ServerMultipliersHash;

return ConvertExportGunToCreature(exportedCreature, out resultText);
}

if (string.IsNullOrEmpty(jsonText))
catch (IOException) when (tryIndex < tryLoadCount - 1)
{
// file is probably still being written. Try up to 3 times again after some time.
Thread.Sleep(waitAfterFailedLoadMs * (1 << tryIndex));
}
catch (Exception ex)
{
resultText = $"Error when importing file {filePath}: {resultText}";
resultText = $"Error when importing file {filePath}: {ex.Message}";
return null;
}

var exportedCreature = JsonConvert.DeserializeObject<ExportGunCreatureFile>(jsonText);
if (exportedCreature == null) return null;

serverMultipliersHash = exportedCreature.ServerMultipliersHash;

return ConvertExportGunToCreature(exportedCreature, out resultText);
}
catch (Exception ex)
{
resultText = $"Error when importing file {filePath}: {ex.Message}";
}

return null;
Expand Down Expand Up @@ -142,41 +155,54 @@ internal static ExportGunServerFile ReadServerMultipliers(string filePath, out s
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
return null;

try
const int tryLoadCount = 3;
const int waitAfterFailedLoadMs = 200;

for (int tryIndex = 0; tryIndex < tryLoadCount; tryIndex++)
{
string jsonText = null;
string game = null;
switch (Path.GetExtension(filePath))
try
{
case ".sav":
jsonText = ReadExportFile.ReadFile(filePath, "DinoExportGunServerSave_C", out resultText);
game = "ASE";
break;
case ".json":
jsonText = File.ReadAllText(filePath);
game = "ASA";
break;
string jsonText = null;
string game = null;
switch (Path.GetExtension(filePath))
{
case ".sav":
jsonText = ReadExportFile.ReadFile(filePath, "DinoExportGunServerSave_C", out resultText);
game = "ASE";
break;
case ".json":
jsonText = File.ReadAllText(filePath);
game = "ASA";
break;
}

if (jsonText == null)
{
resultText = $"Error when importing file {filePath}: {resultText}";
return null;
}

var exportedServerMultipliers = JsonConvert.DeserializeObject<ExportGunServerFile>(jsonText);
if (exportedServerMultipliers?.WildLevel == null)
{
resultText = $"Unknown error when importing file {filePath}";
return null;
}

exportedServerMultipliers.Game = game;
resultText = $"Server multipliers imported from {filePath}";
return exportedServerMultipliers;
}
if (jsonText == null)
catch (IOException) when (tryIndex < tryLoadCount - 1)
{
resultText = $"Error when importing file {filePath}: {resultText}";
return null;
// file is probably still being written. Try up to 3 times again after some time.
Thread.Sleep(waitAfterFailedLoadMs * (1 << tryIndex));
}

var exportedServerMultipliers = JsonConvert.DeserializeObject<ExportGunServerFile>(jsonText);
if (exportedServerMultipliers?.WildLevel == null)
catch (Exception ex)
{
resultText = $"Unknown error when importing file {filePath}";
resultText = $"Error when importing file {filePath}: {ex.Message}";
return null;
}

exportedServerMultipliers.Game = game;
resultText = $"Server multipliers imported from {filePath}";
return exportedServerMultipliers;
}
catch (Exception ex)
{
resultText = $"Error when importing file {filePath}: {ex.Message}";
}

return null;
Expand Down

0 comments on commit 5a00196

Please sign in to comment.