Add source data lock on cached values

This commit is contained in:
Matt Nadareski
2024-12-30 20:47:12 -05:00
parent 1261930fd9
commit 2384cf9f9f

View File

@@ -850,47 +850,50 @@ namespace SabreTools.Serialization.Wrappers
if (string.IsNullOrEmpty(key))
return null;
// If we have the value cached
if (_versionInfoStrings.ContainsKey(key))
return _versionInfoStrings[key];
// Ensure that we have the resource data cached
if (ResourceData == null)
return null;
// If we don't have string version info in this executable
var stringTable = _versionInfo?.StringFileInfo?.Children;
if (stringTable == null || stringTable.Length == 0)
return null;
// Try to find a key that matches
#if NET20
Models.PortableExecutable.StringData? match = null;
foreach (var st in stringTable)
lock (_sourceDataLock)
{
if (st?.Children == null)
continue;
// Return the match if found
match = Array.Find(st.Children, sd => sd != null && key.Equals(sd.Key, StringComparison.OrdinalIgnoreCase));
if (match != null)
{
_versionInfoStrings[key] = match.Value?.TrimEnd('\0');
// If we have the value cached
if (_versionInfoStrings.ContainsKey(key))
return _versionInfoStrings[key];
// Ensure that we have the resource data cached
if (ResourceData == null)
return null;
// If we don't have string version info in this executable
var stringTable = _versionInfo?.StringFileInfo?.Children;
if (stringTable == null || stringTable.Length == 0)
return null;
// Try to find a key that matches
#if NET20
Models.PortableExecutable.StringData? match = null;
foreach (var st in stringTable)
{
if (st?.Children == null)
continue;
// Return the match if found
match = Array.Find(st.Children, sd => sd != null && key.Equals(sd.Key, StringComparison.OrdinalIgnoreCase));
if (match != null)
{
_versionInfoStrings[key] = match.Value?.TrimEnd('\0');
return _versionInfoStrings[key];
}
}
}
_versionInfoStrings[key] = null;
return _versionInfoStrings[key];
_versionInfoStrings[key] = null;
return _versionInfoStrings[key];
#else
var match = stringTable
.SelectMany(st => st?.Children ?? [])
.FirstOrDefault(sd => sd != null && key.Equals(sd.Key, StringComparison.OrdinalIgnoreCase));
var match = stringTable
.SelectMany(st => st?.Children ?? [])
.FirstOrDefault(sd => sd != null && key.Equals(sd.Key, StringComparison.OrdinalIgnoreCase));
// Return either the match or null
_versionInfoStrings[key] = match?.Value?.TrimEnd('\0');
return _versionInfoStrings[key];
// Return either the match or null
_versionInfoStrings[key] = match?.Value?.TrimEnd('\0');
return _versionInfoStrings[key];
#endif
}
}
/// <summary>