public static void SanitizePlugin(Plugin plugin)
{
// performance update to prevent lists from updating currently selected record
bool oldHoldUpdates = BaseRecord.HoldUpdates;
try
{
BaseRecord.HoldUpdates = true;
if (plugin == null)
{
throw new ApplicationException("Cannot select plugin");
}
var hdr = plugin.Records.OfType<Rec>().FirstOrDefault(x => x.Name .StartsWith("TES"));
if (hdr == null)
{
throw new ApplicationException(Resources.PluginLacksAValidTes4RecordCannotContinue);
}
var toParse = new Queue<BaseRecord>(plugin.Records.OfType<BaseRecord>().Where(x => !x.Equals(hdr)));
plugin.Clear();
plugin.AddRecord(hdr);
var groups = new Dictionary<string, GroupRecord>();
foreach (string s in SanitizeOrder)
{
var gr = new GroupRecord(s);
plugin.AddRecord(gr);
groups[s] = gr;
}
bool looseGroupsWarning = false;
bool unknownRecordsWarning = false;
while (toParse.Count > 0)
{
var r = toParse.Dequeue();
if (r is GroupRecord)
{
var gr = (GroupRecord)r;
if (gr.ContentsType == "CELL" || gr.ContentsType == "WRLD" || gr.ContentsType == "DIAL")
{
var gr2 = groups[gr.ContentsType];
foreach (BaseRecord r2 in gr.Records)
{
gr2.AddRecord(r2);
}
gr.Clear();
}
else
{
foreach (BaseRecord r2 in gr.Records)
{
toParse.Enqueue(r2);
}
gr.Clear();
}
}
else if (r is Record)
{
var r2 = (Record)r;
if (LooseGroups.Contains(r2.Name))
{
looseGroupsWarning = true;
plugin.AddRecord(r2);
}
else
{
if (groups.ContainsKey(r2.Name))
{
groups[r2.Name].AddRecord(r2);
}
else
{
unknownRecordsWarning = true;
plugin.AddRecord(r2);
}
}
}
}
foreach (GroupRecord gr2 in groups.Values)
{
if (gr2.Records.Count == 0)
{
plugin.DeleteRecord(gr2);
}
}
if (looseGroupsWarning)
{
Alerts.Show(Resources.CannotSanitizeLooseGroups);
}
if (unknownRecordsWarning)
{
Alerts.Show(Resources.CannotSanitizeUnknownRecords);
}
plugin.InvalidateCache();
plugin.UpdateRecordCount();
/* int reccount = -1 + plugin.Records.Cast<Rec>().Sum(r => sanitizeCountRecords(r));
var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name .StartsWith("TES"));
if (tes4 != null)
{
if (tes4.SubRecords.Count > 0 && tes4.SubRecords[0].Name == "HEDR" && tes4.SubRecords[0].Size >= 8)
{
byte[] data = tes4.SubRecords[0].GetData();
byte[] reccountbytes = TypeConverter.si2h(reccount);
for (int i = 0; i < 4; i++) data[4 + i] = reccountbytes[i];
tes4.SubRecords[0].SetData(data);
}
} */
}
finally
{
BaseRecord.HoldUpdates = oldHoldUpdates;
}
}