/// <summary>
        /// Build the Subrecord array with groups expanded
        /// </summary>
        /// <param name="?"></param>
        /// <returns></returns>
        private static List <SubrecordStructure> BuildSubrecordStructure(IEnumerable <SubrecordBase> list)
        {
            var subrecords = new List <SubrecordStructure>();

            foreach (var sr in list)
            {
                if (sr is SubrecordStructure)
                {
                    subrecords.Add((SubrecordStructure)sr);
                }
                else if (sr is SubrecordGroup)
                {
                    var sg = sr as SubrecordGroup;
                    List <SubrecordStructure> sss = BuildSubrecordStructure(sg.elements);
                    if (sss.Count > 0)
                    {
                        if (sg.repeat > 0)
                        {
                            sss[0] = new SubrecordStructure(sss[0], sss.Count, sss.Count); // replace
                        }
                        else if (sg.optional > 0)
                        {
                            sss[0] = new SubrecordStructure(sss[0], sss.Count, 0); // optional
                        }
                    }
                    subrecords.AddRange(sss);
                }
            }
            return(subrecords);
        }
Beispiel #2
0
        public MediumLevelRecordEditor(SubRecord sr, SubrecordStructure ss, dFormIDLookupS formIDLookup,
                                       dFormIDScan formIDScan, dLStringLookup strIDLookup)
        {
            InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr           = sr;
            this.ss           = ss;
            this.formIDLookup = formIDLookup;
            this.formIDScan   = formIDScan;
            this.strIDLookup  = strIDLookup;

            int offset = 0;

            byte[] data = sr.GetReadonlyData();
            boxes      = new List <TextBox>(ss.elements.Length);
            valueTypes = new List <ElementValueType>(ss.elements.Length);
            elements   = new List <Panel>();
            int groupOffset  = 0;
            int CurrentGroup = 0;

            try
            {
                for (int i = 0; i < ss.elements.Length; i++)
                {
                    if (ss.elements[i].optional && offset == data.Length)
                    {
                        AddElement(ss.elements[i]);
                    }
                    else
                    {
                        AddElement(ss.elements[i], ref offset, data, ref groupOffset, ref CurrentGroup);
                        if (ss.elements[i].repeat > 0)
                        {
                            repeatcount++;
                            if (offset < data.Length)
                            {
                                i--;
                            }
                        }
                    }
                }
                if (ss.elements[ss.elements.Length - 1].repeat > 0 && repeatcount > 0)
                {
                    AddElement(ss.elements[ss.elements.Length - 1]);
                }
            }
            catch
            {
                MessageBox.Show("The subrecord doesn't appear to conform to the expected structure.\n" +
                                "Saving is disabled, and the formatted information may be incorrect", "Warning");
                bSave.Enabled = false;
            }
            ResumeLayout();
        }
 /// <summary>
 /// Clone structure with optional and repeat values overridden
 /// </summary>
 /// <param name="src"></param>
 /// <param name="optional"></param>
 /// <param name="repeat"></param>
 public SubrecordStructure(SubrecordStructure src, int optional, int repeat) : base(src, optional, repeat)
 {
     elements             = src.elements;
     notininfo            = src.notininfo;
     size                 = src.size;
     Condition            = src.Condition;
     CondID               = src.CondID;
     CondOperand          = src.CondOperand;
     ContainsConditionals = src.ContainsConditionals;
     UseHexEditor         = src.UseHexEditor;
 }
Beispiel #4
0
        public static void ReorderSubrecords(Record rec)
        {
            if (rec == null || RecordStructure.Records == null)
            {
                return;
            }
            if (!RecordStructure.Records.ContainsKey(rec.Name))
            {
                return;
            }

            SubrecordStructure[] sss = RecordStructure.Records[rec.Name].subrecords;

            var subs = new List <SubRecord>(rec.SubRecords);

            foreach (var sub in subs)
            {
                sub.DetachStructure();
            }

            var newsubs = new List <SubRecord>();

            for (int ssidx = 0, sslen = 0; ssidx < sss.Length; ssidx += sslen)
            {
                SubrecordStructure ss = sss[ssidx];
                bool repeat           = ss.repeat > 0;
                sslen = Math.Max(1, ss.repeat);

                bool found = false;
                do
                {
                    found = false;
                    for (int ssoff = 0; ssoff < sslen; ++ssoff)
                    {
                        ss = sss[ssidx + ssoff];
                        for (int i = 0; i < subs.Count; ++i)
                        {
                            var sr = subs[i];
                            if (sr.Name == ss.name)
                            {
                                newsubs.Add(sr);
                                subs.RemoveAt(i);
                                found = true;
                                break;
                            }
                        }
                    }
                } while (found && repeat);
            }
            newsubs.AddRange(subs);
            rec.SubRecords.Clear();
            rec.SubRecords.AddRange(newsubs);
        }
        public NewMediumLevelRecordEditor(Plugin p, Record r, SubRecord sr, SubrecordStructure ss)
        {
            InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr = sr;
            this.ss = ss;
            this.p = p;
            this.r = r;

            // walk each element in standard fashion
            int panelOffset = 0;
            try
            {
                foreach (var elem in ss.elements)
                {
                    Control c = null;
                    if (elem.options != null && elem.options.Length > 1)
                    {
                        c = new OptionsElement();
                    }
                    else if (elem.flags != null && elem.flags.Length > 1)
                    {
                        c = new FlagsElement();
                    }
                    else
                    {
                        switch (elem.type)
                        {
                            case ElementValueType.LString:
                                c = new LStringElement();
                                break;
                            case ElementValueType.FormID:
                                c = new FormIDElement();
                                break;
                            case ElementValueType.Blob:
                                c = new HexElement();
                                break;
                            default:
                                c = new TextElement();
                                break;
                        }
                    }
                    if (c is IElementControl)
                    {
                        var ec = c as IElementControl;
                        ec.formIDLookup = p.GetRecordByID;
                        ec.formIDScan = p.EnumerateRecords;
                        ec.strIDLookup = p.LookupFormStrings;
                        ec.Element = elem;

                        if (elem.repeat > 0)
                        {
                            var ge = new RepeatingElement();
                            c = ge;
                            c.Left = 8;
                            c.Width = fpanel1.Width - 16;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            ge.InnerControl = ec;
                            ge.Element = elem;
                            ec = ge;
                        }
                        else if (elem.optional)
                        {
                            var re = new OptionalElement();
                            c = re;
                            c.Left = 8;
                            c.Width = fpanel1.Width - 16;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            re.InnerControl = ec;
                            re.Element = elem;
                            ec = re;
                            c = re;
                        }
                        else
                        {
                            c.Left = 8;
                            c.Width = fpanel1.Width - 16;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                        }
                        c.MinimumSize = c.Size;

                        controlMap.Add(elem, ec);
                        fpanel1.Controls.Add(c);
                        panelOffset = c.Bottom;
                    }
                }

                foreach (Element elem in r.EnumerateElements(sr, true))
                {
                    var es = elem.Structure;

                    IElementControl c;
                    if (controlMap.TryGetValue(es, out c))
                    {
                        if (c is IGroupedElementControl)
                        {
                            var gc = c as IGroupedElementControl;
                            gc.Elements.Add(elem.Data);
                        }
                        else
                        {
                            c.Data = elem.Data;
                        }
                    }
                }
            }
            catch
            {
                strWarnOnSave =
                    "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
                Error.SetError(bSave, strWarnOnSave);
                Error.SetIconAlignment(bSave, ErrorIconAlignment.MiddleLeft);
                AcceptButton = bCancel; // remove save as default button when exception occurs
                CancelButton = bCancel;
                UpdateDefaultButton();
            }
            ResumeLayout();
        }
        public NewMediumLevelRecordEditor(Plugin p, Record r, SubRecord sr, SubrecordStructure ss)
        {
            InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr = sr;
            this.ss = ss;
            this.p  = p;
            this.r  = r;

            // walk each element in standard fashion
            int panelOffset = 0;

            try
            {
                foreach (var elem in ss.elements)
                {
                    Control c = null;
                    if (elem.options != null && elem.options.Length > 1)
                    {
                        c = new OptionsElement();
                    }
                    else if (elem.flags != null && elem.flags.Length > 1)
                    {
                        c = new FlagsElement();
                    }
                    else
                    {
                        switch (elem.type)
                        {
                        case ElementValueType.LString:
                            c = new LStringElement();
                            break;

                        case ElementValueType.FormID:
                            c = new FormIDElement();
                            break;

                        case ElementValueType.Blob:
                            c = new HexElement();
                            break;

                        default:
                            c = new TextElement();
                            break;
                        }
                    }
                    if (c is IElementControl)
                    {
                        var ec = c as IElementControl;
                        ec.formIDLookup = p.GetRecordByID;
                        ec.formIDScan   = p.EnumerateRecords;
                        ec.strIDLookup  = p.LookupFormStrings;
                        ec.Element      = elem;

                        if (elem.repeat > 0)
                        {
                            var ge = new RepeatingElement();
                            c        = ge;
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            ge.InnerControl = ec;
                            ge.Element      = elem;
                            ec = ge;
                        }
                        else if (elem.optional)
                        {
                            var re = new OptionalElement();
                            c        = re;
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            re.InnerControl = ec;
                            re.Element      = elem;
                            ec = re;
                            c  = re;
                        }
                        else
                        {
                            c.Left   = 8;
                            c.Width  = fpanel1.Width - 16;
                            c.Top    = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                        }
                        c.MinimumSize = c.Size;

                        controlMap.Add(elem, ec);
                        fpanel1.Controls.Add(c);
                        panelOffset = c.Bottom;
                    }
                }

                foreach (Element elem in r.EnumerateElements(sr, true))
                {
                    var es = elem.Structure;

                    IElementControl c;
                    if (controlMap.TryGetValue(es, out c))
                    {
                        if (c is IGroupedElementControl)
                        {
                            var gc = c as IGroupedElementControl;
                            gc.Elements.Add(elem.Data);
                        }
                        else
                        {
                            c.Data = elem.Data;
                        }
                    }
                }
            }
            catch
            {
                strWarnOnSave =
                    "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
                Error.SetError(bSave, strWarnOnSave);
                Error.SetIconAlignment(bSave, ErrorIconAlignment.MiddleLeft);
                AcceptButton = bCancel; // remove save as default button when exception occurs
                CancelButton = bCancel;
                UpdateDefaultButton();
            }
            ResumeLayout();
        }
        public MediumLevelRecordEditor(SubRecord sr, SubrecordStructure ss, dFormIDLookupS formIDLookup,
                                       dFormIDScan formIDScan, dLStringLookup strIDLookup)
        {
            InitializeComponent();
            Icon = Resources.tesv_ico;
            SuspendLayout();
            this.sr = sr;
            this.ss = ss;
            this.formIDLookup = formIDLookup;
            this.formIDScan = formIDScan;
            this.strIDLookup = strIDLookup;

            int offset = 0;
            byte[] data = sr.GetReadonlyData();
            boxes = new List<TextBox>(ss.elements.Length);
            valueTypes = new List<ElementValueType>(ss.elements.Length);
            elements = new List<Panel>();
            int groupOffset = 0;
            int CurrentGroup = 0;
            try
            {
                for (int i = 0; i < ss.elements.Length; i++)
                {
                    if (ss.elements[i].optional && offset == data.Length)
                    {
                        AddElement(ss.elements[i]);
                    }
                    else
                    {
                        AddElement(ss.elements[i], ref offset, data, ref groupOffset, ref CurrentGroup);
                        if (ss.elements[i].repeat > 0)
                        {
                            repeatcount++;
                            if (offset < data.Length) i--;
                        }
                    }
                }
                if (ss.elements[ss.elements.Length - 1].repeat > 0 && repeatcount > 0)
                {
                    AddElement(ss.elements[ss.elements.Length - 1]);
                }
            }
            catch
            {
                MessageBox.Show("The subrecord doesn't appear to conform to the expected structure.\n" +
                                "Saving is disabled, and the formatted information may be incorrect", "Warning");
                bSave.Enabled = false;
            }
            ResumeLayout();
        }
Beispiel #8
0
 /// <summary>
 /// Build the Subrecord array with groups expanded
 /// </summary>
 /// <param name="?"></param>
 /// <returns></returns>
 private static List<SubrecordStructure> BuildSubrecordStructure(IEnumerable<SubrecordBase> list)
 {
     var subrecords = new List<SubrecordStructure>();
     foreach (var sr in list)
     {
         if (sr is SubrecordStructure)
         {
             subrecords.Add((SubrecordStructure) sr);
         }
         else if (sr is SubrecordGroup)
         {
             var sg = sr as SubrecordGroup;
             List<SubrecordStructure> sss = BuildSubrecordStructure(sg.elements);
             if (sss.Count > 0)
             {
                 if (sg.repeat > 0)
                     sss[0] = new SubrecordStructure(sss[0], sss.Count, sss.Count); // replace
                 else if (sg.optional > 0)
                     sss[0] = new SubrecordStructure(sss[0], sss.Count, 0); // optional
             }
             subrecords.AddRange(sss);
         }
     }
     return subrecords;
 }
Beispiel #9
0
 private RecordStructure(RecordsRecord rec, SubrecordBase[] subrecordTree, SubrecordStructure[] subrecords)
 {
     name = rec.name;
     description = rec.desc;
     this.subrecordTree = subrecordTree;
     this.subrecords = subrecords;
 }
Beispiel #10
0
 /// <summary>
 /// Clone structure with optional and repeat values overridden
 /// </summary>
 /// <param name="src"></param>
 /// <param name="optional"></param>
 /// <param name="repeat"></param>
 public SubrecordStructure(SubrecordStructure src, int optional, int repeat) : base(src, optional, repeat)
 {
     elements = src.elements;
     notininfo = src.notininfo;
     size = src.size;
     Condition = src.Condition;
     CondID = src.CondID;
     CondOperand = src.CondOperand;
     ContainsConditionals = src.ContainsConditionals;
     UseHexEditor = src.UseHexEditor;
 }
Beispiel #11
0
        private static bool MatchRecordCheckCondition(Dictionary <int, Conditional> conditions, SubrecordStructure ss)
        {
            if (ss.Condition == CondType.Exists)
            {
                if (conditions.ContainsKey(ss.CondID))
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if (ss.Condition == CondType.Missing)
            {
                if (conditions.ContainsKey(ss.CondID))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            Conditional cond;

            if (!conditions.TryGetValue(ss.CondID, out cond))
            {
                return(false);
            }
            switch (cond.type)
            {
            case ElementValueType.SByte:
            case ElementValueType.Byte:
            case ElementValueType.UShort:
            case ElementValueType.Short:
            case ElementValueType.Int:
            case ElementValueType.UInt:
            case ElementValueType.FormID:
            {
                int i = Convert.ToInt32(cond.value), i2;
                if (!int.TryParse(ss.CondOperand, out i2))
                {
                    return(false);
                }
                switch (ss.Condition)
                {
                case CondType.Equal:
                    return(i == i2);

                case CondType.Not:
                    return(i != i2);

                case CondType.Less:
                    return(i < i2);

                case CondType.Greater:
                    return(i > i2);

                case CondType.GreaterEqual:
                    return(i >= i2);

                case CondType.LessEqual:
                    return(i <= i2);

                default:
                    return(false);
                }
            }

            case ElementValueType.Float:
            {
                float i = (float)cond.value, i2;
                if (!float.TryParse(ss.CondOperand, out i2))
                {
                    return(false);
                }
                switch (ss.Condition)
                {
                case CondType.Equal:
                    return(i == i2);

                case CondType.Not:
                    return(i != i2);

                case CondType.Less:
                    return(i < i2);

                case CondType.Greater:
                    return(i > i2);

                case CondType.GreaterEqual:
                    return(i >= i2);

                case CondType.LessEqual:
                    return(i <= i2);

                default:
                    return(false);
                }
            }

            case ElementValueType.Str4:
            case ElementValueType.BString:
            case ElementValueType.IString:
            case ElementValueType.String:
            {
                var s = (string)cond.value;
                switch (ss.Condition)
                {
                case CondType.Equal:
                    return(s == ss.CondOperand);

                case CondType.Not:
                    return(s != ss.CondOperand);

                case CondType.StartsWith:
                    return(s.StartsWith(ss.CondOperand));

                case CondType.EndsWith:
                    return(s.EndsWith(ss.CondOperand));

                case CondType.Contains:
                    return(s.Contains(ss.CondOperand));

                default:
                    return(false);
                }
            }

            case ElementValueType.LString:
            {
                int i = (int)cond.value, i2;
                if (!int.TryParse(ss.CondOperand, out i2))
                {
                    return(false);
                }
                switch (ss.Condition)
                {
                case CondType.Equal:
                    return(i == i2);

                case CondType.Not:
                    return(i != i2);

                case CondType.Less:
                    return(i < i2);

                case CondType.Greater:
                    return(i > i2);

                case CondType.GreaterEqual:
                    return(i >= i2);

                case CondType.LessEqual:
                    return(i <= i2);

                default:
                    return(false);
                }
            }

            default:
                return(false);
            }
        }
Beispiel #12
0
        private static bool MatchRecordCheckCondition(Dictionary<int, Conditional> conditions, SubrecordStructure ss)
        {
            if (ss.Condition == CondType.Exists)
            {
                if (conditions.ContainsKey(ss.CondID)) return true;
                else return false;
            }
            else if (ss.Condition == CondType.Missing)
            {
                if (conditions.ContainsKey(ss.CondID)) return false;
                else return true;
            }
            Conditional cond;
            if (!conditions.TryGetValue(ss.CondID, out cond))
                return false;
            switch (cond.type)
            {
                case ElementValueType.SByte:
                case ElementValueType.Byte:
                case ElementValueType.UShort:
                case ElementValueType.Short:
                case ElementValueType.Int:
                case ElementValueType.UInt:
                case ElementValueType.FormID:
                    {
                        int i = Convert.ToInt32(cond.value), i2;
                        if (!int.TryParse(ss.CondOperand, out i2)) return false;
                        switch (ss.Condition)
                        {
                            case CondType.Equal:
                                return i == i2;
                            case CondType.Not:
                                return i != i2;
                            case CondType.Less:
                                return i < i2;
                            case CondType.Greater:
                                return i > i2;
                            case CondType.GreaterEqual:
                                return i >= i2;
                            case CondType.LessEqual:
                                return i <= i2;
                            default:
                                return false;
                        }
                    }
                case ElementValueType.Float:
                    {
                        float i = (float) cond.value, i2;
                        if (!float.TryParse(ss.CondOperand, out i2)) return false;
                        switch (ss.Condition)
                        {
                            case CondType.Equal:
                                return i == i2;
                            case CondType.Not:
                                return i != i2;
                            case CondType.Less:
                                return i < i2;
                            case CondType.Greater:
                                return i > i2;
                            case CondType.GreaterEqual:
                                return i >= i2;
                            case CondType.LessEqual:
                                return i <= i2;
                            default:
                                return false;
                        }
                    }
                case ElementValueType.Str4:
                case ElementValueType.BString:
                case ElementValueType.IString:
                case ElementValueType.String:
                    {
                        var s = (string) cond.value;
                        switch (ss.Condition)
                        {
                            case CondType.Equal:
                                return s == ss.CondOperand;
                            case CondType.Not:
                                return s != ss.CondOperand;
                            case CondType.StartsWith:
                                return s.StartsWith(ss.CondOperand);
                            case CondType.EndsWith:
                                return s.EndsWith(ss.CondOperand);
                            case CondType.Contains:
                                return s.Contains(ss.CondOperand);
                            default:
                                return false;
                        }
                    }
                case ElementValueType.LString:
                    {
                        int i = (int) cond.value, i2;
                        if (!int.TryParse(ss.CondOperand, out i2)) return false;
                        switch (ss.Condition)
                        {
                            case CondType.Equal:
                                return i == i2;
                            case CondType.Not:
                                return i != i2;
                            case CondType.Less:
                                return i < i2;
                            case CondType.Greater:
                                return i > i2;
                            case CondType.GreaterEqual:
                                return i >= i2;
                            case CondType.LessEqual:
                                return i <= i2;
                            default:
                                return false;
                        }
                    }

                default:
                    return false;
            }
        }