Fix build tool crash when includes cache file is corrupted

This commit is contained in:
Wojtek Figat
2023-04-03 23:42:32 +02:00
parent 820c18968a
commit eda07ac57b

View File

@@ -26,63 +26,74 @@ namespace Flax.Build.NativeCpp
if (!File.Exists(CachePath))
return;
using (var stream = new FileStream(CachePath, FileMode.Open))
using (var reader = new BinaryReader(stream))
try
{
int version = reader.ReadInt32();
if (version != 1)
return;
// DirectIncludesCache
using (var stream = new FileStream(CachePath, FileMode.Open))
using (var reader = new BinaryReader(stream))
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string[] values = new string[reader.ReadInt32()];
for (int j = 0; j < values.Length; j++)
values[j] = reader.ReadString();
int version = reader.ReadInt32();
if (version != 1)
return;
DirectIncludesCache.Add(key, values);
}
}
// AllIncludesCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string[] values = new string[reader.ReadInt32()];
for (int j = 0; j < values.Length; j++)
values[j] = reader.ReadString();
AllIncludesCache.Add(key, values);
}
}
// DirectIncludesTimestampCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
DateTime value = new DateTime(reader.ReadInt64());
DirectIncludesTimestampCache.Add(key, value);
}
}
// AllIncludesTimestampCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
DateTime value = new DateTime(reader.ReadInt64());
AllIncludesTimestampCache.Add(key, value);
// DirectIncludesCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string[] values = new string[reader.ReadInt32()];
for (int j = 0; j < values.Length; j++)
values[j] = reader.ReadString();
DirectIncludesCache.Add(key, values);
}
}
// AllIncludesCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
string[] values = new string[reader.ReadInt32()];
for (int j = 0; j < values.Length; j++)
values[j] = reader.ReadString();
AllIncludesCache.Add(key, values);
}
}
// DirectIncludesTimestampCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
DateTime value = new DateTime(reader.ReadInt64());
DirectIncludesTimestampCache.Add(key, value);
}
}
// AllIncludesTimestampCache
{
int count = reader.ReadInt32();
for (int i = 0; i < count; i++)
{
string key = reader.ReadString();
DateTime value = new DateTime(reader.ReadInt64());
AllIncludesTimestampCache.Add(key, value);
}
}
}
}
catch (Exception)
{
// Clear cache in case of error when loading data (eg. corrupted file)
DirectIncludesCache.Clear();
AllIncludesCache.Clear();
DirectIncludesTimestampCache.Clear();
AllIncludesTimestampCache.Clear();
}
}
public static void SaveCache()
@@ -305,7 +316,7 @@ namespace Flax.Build.NativeCpp
if (includedFile.EndsWith('\\'))
{
int j = includeStart;
while (j-- > 1 && fileContents[j-1] != '\n')
while (j-- > 1 && fileContents[j - 1] != '\n')
{
}
var injectCode = fileContents.Substring(j, "API_INJECT_CODE".Length);