Пример #1
0
        private void findDuplicatedFormIDToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Plugin plugin = GetPluginFromNode(PluginTree.SelectedRecord);
            var    ids    = new Dictionary <uint, Record>();

            foreach (var record in plugin.Enumerate().OfType <Record>())
            {
                if (ids.ContainsKey(record.FormID))
                {
                    if (record.FormID == 0 && record.Name == "COBJ")
                    {
                        continue; // FormID = 0 is valid for all COBJ records
                    }
                    PluginTree.SelectedRecord = record;
                    MessageBox.Show("Record duplicates " + (ids[record.FormID]).DescriptiveName);
                    SendStatusText(string.Format("Record FormID duplicates {0}", (ids[record.FormID]).DescriptiveName), Color.OrangeRed);
                    return;
                }
                else
                {
                    ids.Add(record.FormID, record);
                }
            }
            ids.Clear();
            SendStatusText("No duplicate FormID's found", SystemColors.ControlText);
        }
Пример #2
0
        internal 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 == "TES4");
                if (tes4 != null)
                {
                    tes4.Flags1 &= ~0x00000080U;
                }
            }
            return(count);
        }
Пример #3
0
        /// <summary>
        /// Copy any strings references from master not currently in current plugin
        /// </summary>
        /// <param name="plugin"></param>
        internal 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);
        }
Пример #4
0
        internal 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);
        }
Пример #5
0
        internal 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);
        }
Пример #6
0
 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++;
                     }
                 }
             }
         }
     }
 }
Пример #7
0
        /// <summary>
        /// Extract any internalized strings and put in string table
        /// </summary>
        /// <param name="plugin"></param>
        public static int ExtractInternalStrings(Plugin plugin)
        {
            int  count       = 0;
            uint maxid       = plugin.Masters.Max(x => x.Strings.Count > 0 ? x.Strings.Keys.Max() : 0);
            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;
                                    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 == "TES4");
                if (tes4 != null)
                {
                    tes4.Flags1 |= 0x00000080U;
                }
            }
            return(count);
        }
Пример #8
0
 internal 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 == "TES4");
         if (tes4 != null) tes4.Flags1 &= ~0x00000080U;
     }
     return count;
 }
Пример #9
0
        internal 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;
        }
Пример #10
0
        internal 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;
        }
Пример #11
0
        /// <summary>
        /// Copy any strings references from master not currently in current plugin
        /// </summary>
        /// <param name="plugin"></param>
        internal 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;
        }
Пример #12
0
        /// <summary>
        /// Extract any internalized strings and put in string table
        /// </summary>
        /// <param name="plugin"></param>
        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 == "TES4");
                if (tes4 != null) tes4.Flags1 |= 0x00000080U;
            }
            return count;
        }