public Enumerate ( Predicate |
||
match | Predicate |
|
리턴 | IEnumerable |
/// <summary> /// Change all Form Value greater than 40 to 40 /// </summary> /// <param name="plugin"></param> /// <returns></returns> public static int ChangeFormIdGreater40To40(Plugin plugin) { if (plugin == null) { return -1; } var masters = plugin.Masters; if (masters == null || masters.Length == 0) { return -1; } int countGreate40 = 0; foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); if (record.Flags3 > 40) { var data = new byte[4]; TypeConverter.i2h(record.Flags3, data, 0); if (data[0] > 40) { data[0] = 40; record.Flags3 = TypeConverter.h2i(new ArraySegment<byte>(data, 0, 4)); countGreate40++; } } } return countGreate40; }
public static int CleanUnusedStrings(Plugin plugin) { if (plugin == null) { return -1; } var masters = plugin.Masters; if (masters == null || masters.Length == 0) { return -1; } LocalizedStringDict oldStrings = plugin.Strings; plugin.Strings = new LocalizedStringDict(); foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (var sr in record.SubRecords) { var elements = record.EnumerateElements(sr, rawData: true).ToList(); foreach (var elem in elements) { if (elem.Structure != null && elem.Structure.type == ElementValueType.LString) { var data = elem.Data; if (data.Count == 4) { string value; uint id = TypeConverter.h2i(data); if (id == 0) { continue; } if (oldStrings.TryGetValue(id, out value)) { oldStrings.Remove(id); plugin.Strings[id] = value; } } } } } } return oldStrings.Count; }
public static void updateFormIDReference(Plugin plugin, uint oldFormID, uint newFormID, ref uint refCount) { foreach (Record record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (SubRecord sr in record.SubRecords) { var elements = sr.EnumerateElements(true).ToList(); foreach (Element e in elements) { if (e.Structure != null && e.Structure.type == ElementValueType.FormID) { if ((uint)e.Value == oldFormID) { e.AssignValue<uint>((object)newFormID); refCount++; } } } } } }
public static int InternalizeStrings(Plugin plugin) { bool anyModified = false; int count = 0; foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (var sr in record.SubRecords) { var elements = record.EnumerateElements(sr, rawData: true).ToList(); foreach (var elem in elements) { if (elem.Structure != null && elem.Structure.type == ElementValueType.LString) { var data = elem.Data; uint id = TypeConverter.h2i(data); if (id == 0) { continue; } if (data.Count == 4) { var str = plugin.LookupFormStrings(id); if (!string.IsNullOrEmpty(str)) { elem.AssignValue<ArraySegment<byte>>(new ArraySegment<byte>(TypeConverter.str2h(str))); ++count; } } } } if (elements.Any(x => x.Changed)) { // need to repack the structure using (var ms = new MemoryStream(sr.GetReadonlyData().Length)) { foreach (var seg in elements.Select(elem => elem.Data)) { ms.Write(seg.Array, seg.Offset, seg.Count); } sr.SetData(ms.ToArray()); } anyModified = true; } } } if (anyModified) { var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name .StartsWith("TES")); if (tes4 != null) { tes4.Flags1 &= ~0x00000080U; } } return count; }
/// <summary> /// Extract any internalized strings and put in string table. /// </summary> /// <param name="plugin"> /// </param> /// <returns> /// The System.Int32. /// </returns> public static int ExtractInternalStrings(Plugin plugin) { int count = 0; // uint maxid = plugin.Masters.Max(x=>x.Strings.Count > 0 ? x.Strings.Keys.Max() : 0); // No need to check the masters string since the numbers are unique for every plugin bool anyModified = false; foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (var sr in record.SubRecords) { var elements = record.EnumerateElements(sr, rawData: true).ToList(); foreach (var elem in elements) { if (elem.Structure != null && elem.Structure.type == ElementValueType.LString) { var data = elem.Data; uint id = TypeConverter.h2i(data); if (id == 0) { continue; } if (data.Count == 4 && TypeConverter.IsLikelyString(data)) { string str; if (plugin.Strings.TryGetValue(id, out str)) { continue; } } if (data.Count != 4 || TypeConverter.IsLikelyString(data)) { string value = TypeConverter.GetString(data); if (!string.IsNullOrEmpty(value)) { // uint nextid = Math.Max(maxid, plugin.Strings.Count == 0 ? 0 : plugin.Strings.Keys.Max()) + 1; // No need to check the masters strings since the numbers are unique for every plugin uint nextid = (plugin.Strings.Count == 0 ? 0 : plugin.Strings.Keys.Max()) + 1; int idx = plugin.Strings.FindValue(value); if (idx >= 0) { nextid = plugin.Strings.ElementAt(idx).Key; } else { plugin.Strings[nextid] = value; } elem.AssignValue<ArraySegment<byte>>( new ArraySegment<byte>((byte[])TypeConverter.i2h(nextid).Clone())); ++count; } } } } if (elements.Any(x => x.Changed)) { // need to repack the structure using (var ms = new MemoryStream(sr.GetReadonlyData().Length)) { foreach (var seg in elements.Select(elem => elem.Data)) { ms.Write(seg.Array, seg.Offset, seg.Count); } sr.SetData(ms.ToArray()); } anyModified = true; } } } if (anyModified) { var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name .StartsWith("TES")); if (tes4 != null) { tes4.Flags1 |= 0x00000080U; } } return count; }
public static int CreateStringStubs(Plugin plugin) { if (plugin == null) { return -1; } var masters = plugin.Masters; if (masters == null || masters.Length == 0) { return -1; } int count = 0; foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (var sr in record.SubRecords) { var elements = record.EnumerateElements(sr, rawData: true).ToList(); foreach (var elem in elements) { if (elem.Structure != null && elem.Structure.type == ElementValueType.LString) { var data = elem.Data; if (data.Count == 4) { string value; uint id = TypeConverter.h2i(data); if (id == 0) { continue; } if (!plugin.Strings.TryGetValue(id, out value)) { value = string.Format("STUB: {0} {1}", record.DescriptiveName, sr.DescriptiveName); plugin.Strings[id] = value; ++count; } } } } } } return count; }
/// <summary> /// Copy any strings references from master not currently in current plugin. /// </summary> /// <param name="plugin"> /// </param> /// <returns> /// The System.Int32. /// </returns> public static int CopyMasterStringReferences(Plugin plugin) { if (plugin == null) { return -1; } var masters = plugin.Masters; if (masters == null || masters.Length == 0) { return -1; } int count = 0; foreach (var record in plugin.Enumerate().OfType<Record>()) { record.MatchRecordStructureToRecord(); foreach (var sr in record.SubRecords) { var elements = record.EnumerateElements(sr, rawData: true).ToList(); foreach (var elem in elements) { if (elem.Structure != null && elem.Structure.type == ElementValueType.LString) { var data = elem.Data; if (data.Count == 4) { string value; uint id = TypeConverter.h2i(data); if (id == 0) { continue; } if (!plugin.Strings.TryGetValue(id, out value)) { foreach (var master in masters.Reverse()) { if (master.Strings.TryGetValue(id, out value)) { ++count; plugin.Strings[id] = value; break; } } } } } } } } return count; }