Exemplo n.º 1
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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
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);
        }
Exemplo n.º 4
0
 public GroupEditor(GroupRecord gr)
 {
     this.gr = gr;
     InitializeComponent();
     Icon = Resources.tesv_ico;
     cmbGroupType.ContextMenu   = new ContextMenu();
     cmbGroupType.SelectedIndex = (int)gr.groupType;
     tbRecType.Text             = gr.ContentsType;
     byte[] data = gr.GetData();
     tbX.Text         = TypeConverter.h2ss(data[2], data[3]).ToString();
     tbY.Text         = TypeConverter.h2ss(data[0], data[1]).ToString();
     tbBlock.Text     = TypeConverter.h2i(data[0], data[1], data[2], data[3]).ToString();
     tbParent.Text    = TypeConverter.h2i(data[0], data[1], data[2], data[3]).ToString("X8");
     tbDateStamp.Text = gr.dateStamp.ToString("X8");
     tbFlags.Text     = gr.flags.ToString("X8");
 }
Exemplo n.º 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);
        }
Exemplo n.º 6
0
        private void hexBox1_SelectionStartChanged(object sender, EventArgs e)
        {
            var pos = (int)hexBox1.SelectionStart;

            if (bytes.Count >= pos + 4)
            {
                tbFloat.Text    = TypeConverter.h2f(bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]).ToString();
                tbFloat.Enabled = true;
                tbInt.Text      = TypeConverter.h2si(bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]).ToString();
                tbInt.Enabled   = true;
                bCFloat.Enabled = true;
                bCInt.Enabled   = true;
                tbFormID.Text   =
                    TypeConverter.h2i(bytes[pos], bytes[pos + 1], bytes[pos + 2], bytes[pos + 3]).ToString("X8");
                tbFormID.Enabled = true;
                bCFormID.Enabled = true;
                bLookup.Enabled  = true;
            }
            else
            {
                tbFloat.Enabled  = false;
                tbInt.Enabled    = false;
                bCFloat.Enabled  = false;
                bCInt.Enabled    = false;
                tbFormID.Enabled = false;
                bCFormID.Enabled = false;
                bLookup.Enabled  = false;
            }
            if (bytes.Count >= pos + 2)
            {
                tbWord.Text    = TypeConverter.h2ss(bytes[pos], bytes[pos + 1]).ToString();
                tbWord.Enabled = true;
                bCWord.Enabled = true;
            }
            else
            {
                tbWord.Enabled = false;
                bCWord.Enabled = false;
            }
        }
Exemplo n.º 7
0
        private void generateLLXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FixMasters();
            var plugin = GetPluginFromNode(PluginTree.SelectedRecord);

            if (plugin == null)
            {
                return;
            }
            var p = plugin;

            {
                Record r;
                if (p.Records.Count > 0)
                {
                    r = p.Records[0] as Record;
                }
                else
                {
                    r = null;
                }
                bool firstwasfallout = false;
                if (r != null && r.Name == "TES4")
                {
                    foreach (SubRecord sr in r.SubRecords)
                    {
                        if (sr.Name == "MAST")
                        {
                            if (sr.GetStrData().ToLowerInvariant() == "skyrim.esm")
                            {
                                firstwasfallout = true;
                            }
                            break;
                        }
                    }
                }
                if (!firstwasfallout)
                {
                    MessageBox.Show("Only works on plugin's whose first master is Skyrim.esm", "Error");
                    return;
                }
            }

            uint mask = (uint)(plugin.Masters.Length - 1) << 24;
            var  recs = new Queue <Rec>(p.Records.OfType <Rec>());

            var sb2 = new StringBuilder();
            var sb3 = new StringBuilder();

            while (recs.Count > 0)
            {
                Rec rec = recs.Dequeue();
                if (rec is GroupRecord)
                {
                    var gr = (GroupRecord)rec;
                    if (gr.ContentsType == "LVLI" || gr.ContentsType == "LVLN" || gr.ContentsType == "LVLC")
                    {
                        for (int i = 0; i < gr.Records.Count; i++)
                        {
                            recs.Enqueue(gr.Records[i] as Rec);
                        }
                    }
                }
                else
                {
                    var r = (Record)rec;
                    if ((r.FormID & 0xff000000) != 0)
                    {
                        continue;
                    }
                    switch (r.Name)
                    {
                    case "LVLI":
                        for (int i = 0; i < r.SubRecords.Count; i++)
                        {
                            if (r.SubRecords[i].Name == "LVLO")
                            {
                                if (r.SubRecords[i].Size != 12)
                                {
                                    continue;
                                }
                                byte[] data   = r.SubRecords[i].GetReadonlyData();
                                uint   formid = TypeConverter.h2i(data[4], data[5], data[6], data[7]);
                                if ((formid & 0xff000000) != mask)
                                {
                                    continue;
                                }
                                sb3.Append("      <Element level=\"" + TypeConverter.h2ss(data[0], data[1]) +
                                           "\" formid=\"" +
                                           (formid & 0xffffff).ToString("X6") + "\" count=\"" +
                                           TypeConverter.h2ss(data[8], data[9]) + "\" ");
                                if (i < r.SubRecords.Count - 1 && r.SubRecords[i + 1].Name == "COED" &&
                                    r.SubRecords[i + 1].Size == 12)
                                {
                                    i++;
                                    data = r.SubRecords[i].GetReadonlyData();
                                    sb3.Append(" coed1=\"" + TypeConverter.h2i(data[0], data[1], data[2], data[3]) +
                                               "\" coed2=\"" +
                                               TypeConverter.h2i(data[4], data[5], data[6], data[7]) + "\" coed3=\"" +
                                               TypeConverter.h2i(data[8], data[9], data[10], data[11]) + "\" ");
                                }
                                sb3.AppendLine("/>");
                            }
                        }
                        if (sb3.Length > 0)
                        {
                            sb2.AppendLine("    <LVLI formid=\"" + r.FormID.ToString("X6") + "\">");
                            sb2.Append(sb3.ToString());
                            sb2.AppendLine("    </LVLI>");
                        }
                        sb3.Length = 0;
                        break;

                    case "LVLN":
                        for (int i = 0; i < r.SubRecords.Count; i++)
                        {
                            if (r.SubRecords[i].Name == "LVLO")
                            {
                                if (r.SubRecords[i].Size != 12)
                                {
                                    continue;
                                }
                                byte[] data   = r.SubRecords[i].GetReadonlyData();
                                uint   formid = TypeConverter.h2i(data[4], data[5], data[6], data[7]);
                                if ((formid & 0xff000000) != mask)
                                {
                                    continue;
                                }
                                sb3.AppendLine("      <Element level=\"" + TypeConverter.h2ss(data[0], data[1]) +
                                               "\" formid=\"" +
                                               (formid & 0xffffff).ToString("X6") + "\" count=\"" +
                                               TypeConverter.h2ss(data[8], data[9]) + "\" />");
                            }
                        }
                        if (sb3.Length > 0)
                        {
                            sb2.AppendLine("    <LVLN formid=\"" + r.FormID.ToString("X6") + "\">");
                            sb2.Append(sb3.ToString());
                            sb2.AppendLine("    </LVLN>");
                        }
                        sb3.Length = 0;
                        break;

                    case "LVLC":
                        for (int i = 0; i < r.SubRecords.Count; i++)
                        {
                            if (r.SubRecords[i].Name == "LVLO")
                            {
                                if (r.SubRecords[i].Size != 12)
                                {
                                    continue;
                                }
                                byte[] data   = r.SubRecords[i].GetReadonlyData();
                                uint   formid = TypeConverter.h2i(data[4], data[5], data[6], data[7]);
                                if ((formid & 0xff000000) != mask)
                                {
                                    continue;
                                }
                                sb3.AppendLine("      <Element level=\"" + TypeConverter.h2ss(data[0], data[1]) +
                                               "\" formid=\"" +
                                               (formid & 0xffffff).ToString("X6") + "\" count=\"" +
                                               TypeConverter.h2ss(data[8], data[9]) + "\" />");
                            }
                        }
                        if (sb3.Length > 0)
                        {
                            sb2.AppendLine("    <LVLC formid=\"" + r.FormID.ToString("X6") + "\">");
                            sb2.Append(sb3.ToString());
                            sb2.AppendLine("    </LVLC>");
                        }
                        sb3.Length = 0;
                        break;
                    }
                }
            }
            if (sb2.Length > 0)
            {
                var sb1 = new StringBuilder();
                sb1.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                sb1.AppendLine("<Plugin>");
                sb1.AppendLine("  <MergedLists>");
                sb1.Append(sb2);
                sb1.AppendLine("  </MergedLists>");
                sb1.AppendLine("</Plugin>");
                File.WriteAllText(Path.ChangeExtension("data\\" + p.Name, ".xml"), sb1.ToString());
            }
            else
            {
                MessageBox.Show("No compatible leveled lists found");
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        private void AddElement(ElementStructure es, ref int offset, byte[] data, ref int groupOffset,
                                ref int CurrentGroup)
        {
            var panel1 = new Panel();

            panel1.AutoSize = true;
            panel1.Width    = fpanel1.Width - 10;
            panel1.Height   = 1;
            panel1.Anchor   = AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left | AnchorStyles.Bottom;
            int  ypos      = 0;
            uint flagValue = 0; // value if flags is set
            byte flagSize  = 4;
            bool hasFlags  = (es.options.Length == 0 && es.flags.Length > 1);

            var tb = new TextBox();

            boxes.Add(tb);
            if (es.group != 0)
            {
                var cb = new CheckBox();
                cb.Text = "Use this value?";
                panel1.Controls.Add(cb);
                cb.Location = new Point(10, ypos);
                ypos       += 24;
                cb.Tag      = new cbTag(es.group, tb);
                if (CurrentGroup != es.group)
                {
                    cb.Checked = true;
                }
                else
                {
                    tb.Enabled = false;
                }
                cb.CheckedChanged += CheckBox_CheckedChanged;
            }
            if (es.optional || es.repeat > 0 && repeatcount > 0)
            {
                var cb = new CheckBox();
                cb.Text = "Use this value?";
                panel1.Controls.Add(cb);
                cb.Location = new Point(10, ypos);
                ypos       += 24;
                cb.Tag      = new repeatCbTag(tb, elements.Count);
                if (data == null)
                {
                    tb.Enabled = false;
                }
                else
                {
                    cb.Checked = true;
                }
                cb.CheckedChanged += RepeatCheckBox_CheckedChanged;
            }
            if ((CurrentGroup == 0 && es.group != 0) || (CurrentGroup != 0 && es.group != 0 && CurrentGroup != es.group))
            {
                CurrentGroup = es.group;
                groupOffset  = offset;
            }
            else if (CurrentGroup != 0 && es.group == 0)
            {
                CurrentGroup = 0;
            }
            else if (CurrentGroup != 0 && CurrentGroup == es.group)
            {
                offset = groupOffset;
            }

            valueTypes.Add(es.type);
            if (data != null)
            {
                switch (es.type)
                {
                case ElementValueType.UInt:
                {
                    var v = TypeConverter.h2i(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                    flagValue = v;
                    flagSize  = 4;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X8") : v.ToString();
                    offset   += 4;
                }
                break;

                case ElementValueType.Int:
                {
                    var v = TypeConverter.h2si(data[offset], data[offset + 1], data[offset + 2],
                                               data[offset + 3]);
                    flagValue = (uint)v;
                    flagSize  = 4;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X8") : v.ToString();
                    offset   += 4;
                }
                break;

                case ElementValueType.FormID:
                    tb.Text =
                        TypeConverter.h2i(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]).
                        ToString("X8");
                    offset += 4;
                    break;

                case ElementValueType.Float:
                    tb.Text =
                        TypeConverter.h2f(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]).
                        ToString();
                    offset += 4;
                    break;

                case ElementValueType.UShort:
                {
                    var v = TypeConverter.h2s(data[offset], data[offset + 1]);
                    flagValue = v;
                    flagSize  = 2;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X4") : v.ToString();
                    offset   += 2;
                }
                break;

                case ElementValueType.Short:
                {
                    var v = TypeConverter.h2ss(data[offset], data[offset + 1]);
                    flagValue = (uint)v;
                    flagSize  = 2;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X4") : v.ToString();
                    offset   += 2;
                }
                break;

                case ElementValueType.Byte:
                {
                    var v = data[offset];
                    flagValue = v;
                    flagSize  = 1;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X2") : v.ToString();
                    offset++;
                }
                break;

                case ElementValueType.SByte:
                {
                    var v = (sbyte)data[offset];
                    flagValue = (uint)v;
                    flagSize  = 1;
                    tb.Text   = hasFlags || es.hexview ? "0x" + v.ToString("X2") : v.ToString();
                    offset++;
                }
                break;

                case ElementValueType.String:
                {
                    string s = "";
                    while (data[offset] != 0)
                    {
                        s += (char)data[offset++];
                    }
                    offset++;
                    tb.Text   = s;
                    tb.Width += 200;
                }
                break;

                case ElementValueType.BString:
                {
                    int    len = TypeConverter.h2s(data[offset], data[offset + 1]);
                    string s   = Encoding.CP1252.GetString(data, offset + 2, len);
                    offset    = offset + (2 + len);
                    tb.Text   = s;
                    tb.Width += 200;
                }
                break;

                case ElementValueType.IString:
                {
                    int    len = TypeConverter.h2si(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                    string s   = Encoding.CP1252.GetString(data, offset + 4, len);
                    offset    = offset + (4 + len);
                    tb.Text   = s;
                    tb.Width += 200;
                }
                break;

                case ElementValueType.LString:
                {
                    int  left = data.Length - offset;
                    uint id   = (left < 4)
                                          ? 0
                                          : TypeConverter.h2i(data[offset], data[offset + 1], data[offset + 2],
                                                              data[offset + 3]);
                    bool   isString  = TypeConverter.IsLikelyString(new ArraySegment <byte>(data, offset, left));
                    int    strOffset = offset;
                    string s         = null;
                    if (isString)
                    {
                        s       = TypeConverter.GetString(new ArraySegment <byte>(data, offset, data.Length - offset));
                        tb.Text = 0.ToString("X8");
                        offset += s.Length;
                    }
                    else
                    {
                        offset += 4;
                        tb.Text = id.ToString("X8");
                        if (strIDLookup != null)
                        {
                            s = strIDLookup(id);
                        }
                    }
                    tb.Tag = new lTag(tb, s, data, strOffset, isString);
                }
                break;

                case ElementValueType.Str4:
                {
                    string s = Encoding.CP1252.GetString(data, offset, 4);
                    offset      += 4;
                    tb.MaxLength = 4;
                    tb.Text      = s;
                }
                break;

                default:
                    throw new ApplicationException();
                }
            }
            else
            {
                if (es.type == ElementValueType.String || es.type == ElementValueType.BString ||
                    es.type == ElementValueType.LString || es.type == ElementValueType.IString)
                {
                    tb.Width += 200;
                }
                if (removedStrings.ContainsKey(boxes.Count - 1))
                {
                    tb.Text = removedStrings[boxes.Count - 1];
                }
            }
            var l = new Label();

            l.AutoSize = true;
            string tmp = es.type.ToString();

            l.Text = tmp + ": " + es.name + (!string.IsNullOrEmpty(es.desc) ? (" (" + es.desc + ")") : "");
            panel1.Controls.Add(tb);
            tb.Location = new Point(10, ypos);
            if (es.multiline)
            {
                tb.Multiline = true;
                ypos        += tb.Height * 5;
                tb.Height   *= 6;
            }
            panel1.Controls.Add(l);
            l.Location = new Point(tb.Right + 10, ypos + 3);
            string[] options = null;
            if (es.type == ElementValueType.FormID)
            {
                ypos += 28;
                var b = new Button();
                b.Text   = "FormID lookup";
                b.Click += LookupFormID_Click;
                panel1.Controls.Add(b);
                b.Location = new Point(20, ypos);
                var tb2 = new TextBox();
                tb2.Width   += 200;
                tb2.ReadOnly = true;
                panel1.Controls.Add(tb2);
                tb2.Location = new Point(b.Right + 10, ypos);
                b.Tag        = new bTag(tb, tb2);
                if (es.FormIDType != null)
                {
                    if (cachedFormIDs.ContainsKey(es.FormIDType))
                    {
                        options = cachedFormIDs[es.FormIDType];
                    }
                    else
                    {
                        options = formIDScan(es.FormIDType);
                        cachedFormIDs[es.FormIDType] = options;
                    }
                }
            }
            else if (es.type == ElementValueType.LString)
            {
                ypos += 24;
                var ltag = tb.Tag as lTag;

                ltag.cb         = new CheckBox();
                ltag.cb.Width   = ltag.cb.Height;
                ltag.cb.Checked = ltag.isString;
                panel1.Controls.Add(ltag.cb);
                ltag.cb.Location = new Point(8, ypos);

                ltag.str = new TextBox();
                //ltag.str.Font = this.baseFont;
                ltag.str.Width += (200 - ltag.cb.Width + 8);
                panel1.Controls.Add(ltag.str);
                ltag.str.Location = new Point(ltag.cb.Location.X + ltag.cb.Width + 8, ypos);
                ltag.str.Text     = string.IsNullOrEmpty(ltag.disp) ? "" : ltag.disp;

                ypos += 24;
            }
            else if (es.options != null)
            {
                options = es.options;
            }
            if (options != null && options.Length > 0)
            {
                ypos += 28;
                var cmb = new ComboBox();
                cmb.Tag    = tb;
                cmb.Width += 200;
                for (int j = 0; j < options.Length; j += 2)
                {
                    cmb.Items.Add(new comboBoxItem(options[j], options[j + 1]));
                }
                cmb.KeyPress             += cb_KeyPress;
                cmb.ContextMenu           = new ContextMenu();
                cmb.SelectedIndexChanged += cb_SelectedIndexChanged;
                panel1.Controls.Add(cmb);
                cmb.Location = new Point(20, ypos);
            }
            if (hasFlags) // add flags combo box to the side
            {
                var ccb = new FlagComboBox();
                ccb.Tag = tb;
                ccb.SetItems(es.flags, flagSize);
                ccb.SetState(flagValue);
                ccb.TextChanged += delegate
                {
                    uint value = ccb.GetState();
                    var  text  = ccb.Tag as TextBox;
                    text.Text = "0x" + value.ToString("X");
                };
                ccb.Location = new Point(l.Location.X + l.Width + 10, tb.Top);
                ccb.Width    = Math.Max(ccb.Width, Width - 50 - (ccb.Location.X));
                ccb.Anchor   = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
                panel1.Controls.Add(ccb);
            }
            fpanel1.Controls.Add(panel1);
            elements.Add(panel1);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Helper routine for doing an actual search
        /// </summary>
        /// <param name="type">Type of search to perform</param>
        /// <param name="tn">Starting node to search with</param>
        /// <param name="text">Text to search for</param>
        /// <param name="first">Whether this is the first search (if not current node can be matched)</param>
        /// <param name="partial">Allow for partial text matches</param>
        /// <param name="forward">Search forward or backward</param>
        /// <param name="wrapAround">Whether to wrap around when reach top or bottom</param>
        /// <param name="updateFunc">Function to call to update the UI when doing select</param>
        /// <returns></returns>
        private BaseRecord PerformSearch(SearchSettings ctx)
        {
            Predicate <BaseRecord> searchFunction = null;

            if (ctx.type == SearchType.FormID)
            {
                if (string.IsNullOrEmpty(ctx.text))
                {
                    return(null);
                }

                uint searchID;
                if (!uint.TryParse(ctx.text, NumberStyles.AllowHexSpecifier, null, out searchID))
                {
                    MainView.PostStatusWarning("Invalid FormID");
                    return(null);
                }
                searchFunction = (BaseRecord node) =>
                {
                    var rec = node as Record;
                    if (ctx.updateFunc != null && ctx.updateFunc(node))
                    {
                        return(true);
                    }
                    return((rec != null) ? rec.FormID == searchID : false);
                };
            }
            else if (ctx.type == SearchType.EditorID || ctx.type == SearchType.TypeEditorIdSearch)
            {
                if (ctx.type == SearchType.TypeEditorIdSearch && string.IsNullOrEmpty(ctx.rectype))
                {
                    return(null);
                }
                if (ctx.type == SearchType.EditorID && string.IsNullOrEmpty(ctx.text))
                {
                    return(null);
                }

                string searchString = string.IsNullOrEmpty(ctx.text) ? null : ctx.text.ToLowerInvariant();
                searchFunction = (BaseRecord node) =>
                {
                    var rec = node as Record;
                    if (rec != null)
                    {
                        bool typeOk = true;
                        if (ctx.type == SearchType.TypeEditorIdSearch)
                        {
                            typeOk = !string.IsNullOrEmpty(rec.Name) && string.Compare(rec.Name, ctx.rectype, true) == 0;
                        }
                        if (typeOk)
                        {
                            if (string.IsNullOrEmpty(searchString))
                            {
                                return(true);
                            }
                            else if (ctx.partial)
                            {
                                var val = rec.DescriptiveName.ToLowerInvariant();
                                if (val.Contains(searchString))
                                {
                                    return(true);
                                }
                            }
                            else
                            {
                                var val = rec.DescriptiveName.ToLowerInvariant().Substring(2, rec.DescriptiveName.Length - 3);
                                if (val == searchString)
                                {
                                    return(true);
                                }
                            }
                        }
                    }
                    if (ctx.updateFunc != null && ctx.updateFunc(node))
                    {
                        return(true);
                    }
                    return(false);
                };
            }
            else if (ctx.type == SearchType.FullSearch || ctx.type == SearchType.TypeFullSearch)
            {
                if (ctx.type == SearchType.TypeFullSearch && string.IsNullOrEmpty(ctx.rectype))
                {
                    return(null);
                }
                if (ctx.type == SearchType.FullSearch && string.IsNullOrEmpty(ctx.text))
                {
                    return(null);
                }
                string searchString = ctx.text.ToLowerInvariant();
                searchFunction = node =>
                {
                    var rec = node as Record;
                    if (rec != null)
                    {
                        bool typeOk = true;
                        if (ctx.type == SearchType.TypeFullSearch)
                        {
                            typeOk = !string.IsNullOrEmpty(rec.Name) &&
                                     string.Compare(rec.Name, ctx.rectype, true) == 0;
                        }
                        if (typeOk)
                        {
                            foreach (SubRecord sr in rec.SubRecords)
                            {
                                var val = sr.GetStrData();
                                if (!string.IsNullOrEmpty(val))
                                {
                                    val = val.ToLowerInvariant();
                                    if ((ctx.partial && val.Contains(searchString)) ||
                                        (val == searchString))
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                    if (ctx.updateFunc != null && ctx.updateFunc(node))
                    {
                        return(true);
                    }
                    return(false);
                };
            }
            else if (ctx.type == SearchType.FormIDRef) // Back reference form id search
            {
                if (string.IsNullOrEmpty(ctx.text))
                {
                    return(null);
                }

                uint searchID;
                if (!uint.TryParse(ctx.text, NumberStyles.AllowHexSpecifier, null, out searchID))
                {
                    MainView.PostStatusWarning("Invalid FormID");
                    return(null);
                }
                searchFunction = node =>
                {
                    var rec = node as Record;
                    if (rec != null)
                    {
                        rec.MatchRecordStructureToRecord();
                        if ((from sr in rec.SubRecords from elem in rec.EnumerateElements(sr)
                             let es = elem.Structure
                                      where es != null && es.type == ElementValueType.FormID select elem)
                            .Any(elem => searchID == TypeConverter.h2i(elem.Data)))
                        {
                            return(true);
                        }
                    }
                    if (ctx.updateFunc != null && ctx.updateFunc(node))
                    {
                        return(true);
                    }
                    return(false);
                };
            }
            return(IncrementalSearch(ctx.startNode, ctx.first, ctx.forward, ctx.wrapAround, searchFunction));
        }