fix potential race condition with RobloxState

This commit is contained in:
bluepilledgreat 2025-03-12 19:06:11 +00:00
parent bff40a40a7
commit 2f651e6c35
3 changed files with 38 additions and 4 deletions

View File

@ -1259,8 +1259,17 @@ namespace Bloxstrap
}
}
// make sure we're not overwriting a new update
// if we're the background update process, always overwrite
if (App.LaunchSettings.BackgroundUpdaterFlag.Active || !App.RobloxState.HasFileOnDiskChanged())
{
App.RobloxState.Prop.ModManifest = modFolderFiles;
App.RobloxState.Save();
}
else
{
App.Logger.WriteLine(LOG_IDENT, "RobloxState disk mismatch, not saving ModManifest");
}
App.Logger.WriteLine(LOG_IDENT, $"Finished checking file mods");

View File

@ -8,6 +8,11 @@ namespace Bloxstrap
public T Prop { get; set; } = new();
/// <summary>
/// The file hash when last retrieved from disk
/// </summary>
public string? LastFileHash { get; private set; }
public virtual string ClassName => typeof(T).Name;
public virtual string FileLocation => Path.Combine(Paths.Base, $"{ClassName}.json");
@ -22,12 +27,15 @@ namespace Bloxstrap
try
{
T? settings = JsonSerializer.Deserialize<T>(File.ReadAllText(FileLocation));
string contents = File.ReadAllText(FileLocation);
T? settings = JsonSerializer.Deserialize<T>(contents);
if (settings is null)
throw new ArgumentNullException("Deserialization returned null");
Prop = settings;
LastFileHash = MD5Hash.FromString(contents);
App.Logger.WriteLine(LOG_IDENT, "Loaded successfully!");
}
@ -74,7 +82,11 @@ namespace Bloxstrap
try
{
File.WriteAllText(FileLocation, JsonSerializer.Serialize(Prop, new JsonSerializerOptions { WriteIndented = true }));
string contents = JsonSerializer.Serialize(Prop, new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText(FileLocation, contents);
LastFileHash = MD5Hash.FromString(contents);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
@ -89,5 +101,13 @@ namespace Bloxstrap
App.Logger.WriteLine(LOG_IDENT, "Save complete!");
}
/// <summary>
/// Is the file on disk different to the one deserialised during this session?
/// </summary>
public bool HasFileOnDiskChanged()
{
return LastFileHash != MD5Hash.FromFile(FileLocation);
}
}
}

View File

@ -25,6 +25,11 @@ namespace Bloxstrap.Utility
return FromStream(stream);
}
public static string FromString(string str)
{
return FromBytes(Encoding.UTF8.GetBytes(str));
}
public static string Stringify(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();