Inheritance: TESVSnip.Domain.Model.Rec, ISerializable, IDeserializationCallback
示例#1
0
        private HeaderEditor(Record r)
        {
            this.InitializeComponent();
            Icon = Resources.tesv_ico;
            this.R = r;
            this.tbName.Text = r.Name;
            this.Flags1 = r.Flags1;
            this.Flags2 = r.Flags2;
            this.Flags3 = r.Flags3;
            this.FormID = r.FormID;
            this.tbFormID.Text = this.FormID.ToString("x8").ToUpperInvariant();
            this.tbFlags2.Text = this.Flags2.ToString("x8").ToUpperInvariant();
            this.tbFlags3.Text = this.Flags3.ToString("x8").ToUpperInvariant();
            foreach (Control c in Controls)
            {
                if (c is CheckBox)
                {
                    c.Tag = int.Parse((string)c.Tag);
                    if ((this.Flags1 & GetInt(c.Tag)) > 0)
                    {
                        ((CheckBox)c).Checked = true;
                    }
                }
            }

            // cb18.Checked=false; // Allow compression
        }
示例#2
0
 /// <summary>
 /// Clear any state.
 /// </summary>
 public void ClearControl()
 {
     this.r = null;
     this.sr = null;
     this.ss = null;
     this.controlMap.Clear();
     this.fpanel1.Controls.Clear();
     Enabled = false;
 }
示例#3
0
        public override bool Match(Record r)
        {
            bool any = false;
            foreach (bool value in r.SubRecords.Where(x => x.Name == this.Parent.Record.name).Select(x => this.Match(r, x)))
            {
                if (!value)
                {
                    return false;
                }

                any = true;
            }

            return any;
        }
示例#4
0
        public override bool Evaluate(Record r, SubRecord sr)
        {
            if (this.Type == BatchCondRecordType.Create)
            {
                if (sr == null)
                {
                    // guess the best insert location
                    int idx = -1;
                    var records = r.GetStructures();
                    RecordStructure rs;
                    if (records.TryGetValue(r.Name, out rs))
                    {
                        for (int i = Array.FindIndex(rs.subrecords, structure => structure.name == this.Record.name) - 1; i >= 0; --i)
                        {
                            var srsname = rs.subrecords[i].name;
                            idx = r.SubRecords.IndexOf(r.SubRecords.FirstOrDefault(x => x.Name == srsname));
                        }
                    }

                    sr = new SubRecord(this.Record);
                    if (idx < 0)
                    {
                        r.SubRecords.Add(sr);
                    }
                    else
                    {
                        r.SubRecords.Insert(idx + 1, sr);
                    }
                }
            }
            else if (this.Type == BatchCondRecordType.Delete)
            {
                while (sr != null)
                {
                    r.SubRecords.Remove(sr);
                    sr = r.SubRecords.FirstOrDefault(x => x.Name == this.Record.name);
                }
            }

            return true;
        }
示例#5
0
 public static void giveRecordNewFormID(Record rec, bool updateReference)
 {
     uint formCount = 0, refCount = 0;
     giveRecordNewFormID(rec, updateReference, ref formCount, ref refCount);
 }
示例#6
0
 public FullRecordEditor(Record rec)
     : this()
 {
     this.Record = rec;
 }
示例#7
0
        public bool Process(string name, Record[] records)
        {
            RecordsRecord rr;
            if (!this.rdict.TryGetValue(name, out rr))
            {
                rr = new RecordsRecord();
                rr.name = name;
                rr.desc = name;
                this.rdict.Add(name, rr);
            }

            foreach (var r in records)
            {
                this.UpdateProgress();
                if (!this.CreateSubrecords(rr, r))
                {
                    return false;
                }
            }

            // now that we have subrecords, go back through all records
            foreach (var sr in rr.Items.OfType<Subrecord>())
            {
                // list of all subrecords matching the given name
                var ss = (from r in records from s in r.SubRecords where s.Name == sr.name select s).ToArray();

                this.ProcessSubRecord(sr, ss);
            }

            // Post Process
            IEnumerable<Subrecord> srs = rr.Subrecords;
            var itr = srs.GetEnumerator();
            for (bool atEnd = itr.MoveNext(); !atEnd;)
            {
                var sr = itr.Current;
                if (sr.repeat > 1)
                {
                    if (sr.size < 0)
                    {
                        sr.size = 0;
                        continue;
                    }

                    int count = sr.repeat;
                    for (int j = 1; j < count; ++j)
                    {
                        atEnd = itr.MoveNext();
                        if (atEnd)
                        {
                            sr = itr.Current;
                            if (sr.repeat == count)
                            {
                                sr.repeat = sr.optional = 0;
                            }
                            else
                            {
                                // Should be a group??
                            }
                        }
                        else
                        {
                            sr.repeat = sr.optional = 1;
                        }
                    }
                }
                else
                {
                    atEnd = itr.MoveNext();
                }
            }

            // sub records have been updated
            long minSize = records.Min(a => a.Size);
            long maxSize = records.Max(a => a.Size);
            if (maxSize == minSize)
            {
                // Uniform record size
            }
            else
            {
            }

            return true;
        }
示例#8
0
 public TESVSnip.Data.RecordsRecord ProcessBase(Record r)
 {
     TESVSnip.Data.RecordsRecord rr;
     if (!rdict.TryGetValue(r.Name, out rr))
     {
         rr = new TESVSnip.Data.RecordsRecord();
         rr.name = r.Name;
         rr.desc = r.Name;
         rdict.Add(r.Name, rr);
     }
     return rr;
 }
示例#9
0
        public override bool Match(Record r, SubRecord sr, Element se)
        {
            if (this.Type == SearchCondElementType.Exists && se != null)
            {
                return true;
            }

            if (this.Type == SearchCondElementType.Missing && se == null)
            {
                return true;
            }

            if (se == null)
            {
                return false;
            }

            var value = sr.GetCompareValue(se);
            int diff = ValueComparer.Compare(value, this.Value);
            switch (this.Type)
            {
                case SearchCondElementType.Equal:
                    return diff == 0;
                case SearchCondElementType.Not:
                    return diff != 0;
                case SearchCondElementType.Greater:
                    return diff > 0;
                case SearchCondElementType.Less:
                    return diff < 0;
                case SearchCondElementType.GreaterEqual:
                    return diff >= 0;
                case SearchCondElementType.LessEqual:
                    return diff <= 0;
                case SearchCondElementType.StartsWith:
                    if (diff == 0)
                    {
                        return true;
                    }

                    if (value != null && this.Value != null)
                    {
                        return value.ToString().StartsWith(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase);
                    }

                    break;
                case SearchCondElementType.EndsWith:
                    if (diff == 0)
                    {
                        return true;
                    }

                    if (value != null && this.Value != null)
                    {
                        return value.ToString().EndsWith(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase);
                    }

                    break;
                case SearchCondElementType.Contains:
                    if (diff == 0)
                    {
                        return true;
                    }

                    if (value != null && this.Value != null)
                    {
                        return value.ToString().IndexOf(this.Value.ToString(), StringComparison.CurrentCultureIgnoreCase) >= 0;
                    }

                    break;
            }

            return false;
        }
示例#10
0
 public override bool Match(Record r, SubRecord sr, Element se)
 {
     return false;
 }
示例#11
0
 public override bool Match(Record r)
 {
     var sr = r.SubRecords.FirstOrDefault(x => x.Name == this.Record.name);
     return this.Match(r, sr);
 }
示例#12
0
        public static bool CompileResultScript(SubRecord sr, out Record r2, out string msg)
        {
            msg = null;
            r2 = null;
            r = new Record();
            string script = sr.GetStrData();
            locals.Clear();
            localList.Clear();
            edidRefs.Clear();
            refcount = 0;
            errors.Clear();

            ts = new TokenStream(script, errors);
            if (errors.Count > 0)
            {
                return OutputErrors(out msg);
            }

            schr = new SubRecord();
            schr.Name = "SCHR";
            r.AddRecord(schr);
            scda = new SubRecord();
            scda.Name = "SCDA";
            r.AddRecord(scda);
            sr = (SubRecord)sr.Clone();
            r.AddRecord(sr);

            bw = new BinaryWriter(new MemoryStream());

            while (ts.PeekNextStatement().Length > 0)
            {
                try
                {
                    HandleResultsBlock();
                }
                catch (Exception ex)
                {
                    return ReturnError(ex.Message, out msg);
                }
            }

            if (errors.Count > 0)
            {
                return OutputErrors(out msg);
            }

            var header = new byte[20];
            TypeConverter.si2h(refcount, header, 4);
            TypeConverter.i2h((uint)bw.BaseStream.Length, header, 8);
            TypeConverter.si2h(localList.Count, header, 12);
            TypeConverter.si2h(0x10000, header, 16);
            schr.SetData(header);
            byte[] compileddata = ((MemoryStream)bw.BaseStream).GetBuffer();
            if (compileddata.Length != bw.BaseStream.Length)
            {
                Array.Resize(ref compileddata, (int)bw.BaseStream.Length);
            }

            scda.SetData(compileddata);
            bw.Close();
            r2 = r;
            return true;
        }
示例#13
0
 private static bool ReturnError(string msg, out string error)
 {
     error = ts.Line.ToString() + ": " + msg;
     ts = null;
     r = null;
     return false;
 }
示例#14
0
        private static bool OutputErrors(out string msg)
        {
            msg = string.Empty;
            foreach (string s in errors)
            {
                msg += s + Environment.NewLine;
            }

            ts = null;
            r = null;
            return false;
        }
示例#15
0
        public static bool Compile(Record r2, out string msg)
        {
            msg = null;
            r = new Record();
            string script = null;
            int scptype = 0;
            foreach (SubRecord sr2 in r2.SubRecords)
            {
                if (sr2.Name == "SCTX")
                {
                    script = sr2.GetStrData();
                }

                if (sr2.Name == "SCHR")
                {
                    byte[] tmp = sr2.GetReadonlyData();
                    scptype = TypeConverter.h2si(tmp[16], tmp[17], tmp[18], tmp[19]);
                }
            }

            if (script == null)
            {
                msg = "Script had no SCTX record to compile";
                return false;
            }

            locals.Clear();
            localList.Clear();
            edidRefs.Clear();
            refcount = 0;
            errors.Clear();

            ts = new TokenStream(script, errors);
            if (errors.Count > 0)
            {
                return OutputErrors(out msg);
            }

            Token[] smt = ts.PopNextStatement();
            if (smt.Length != 2 || !smt[0].IsKeyword(Keywords.ScriptName) || smt[1].token == null)
            {
                return ReturnError("Expected 'ScriptName <edid>'", out msg);
            }

            var sr = new SubRecord();
            sr.Name = "EDID";
            sr.SetStrData(smt[1].utoken, true);
            r.AddRecord(sr);
            r.UpdateShortDescription();
            schr = new SubRecord();
            schr.Name = "SCHR";
            r.AddRecord(schr);
            scda = new SubRecord();
            scda.Name = "SCDA";
            r.AddRecord(scda);
            sr = new SubRecord();
            sr.Name = "SCTX";
            sr.SetStrData(script, false);
            r.AddRecord(sr);

            bw = new BinaryWriter(new MemoryStream());
            Emit(0x001d);
            Emit(0x0000);
            try
            {
                HandleVariables();
            }
            catch (Exception ex)
            {
                return ReturnError(ex.Message, out msg);
            }

            for (int i = 0; i < localList.Count; i++)
            {
                if (localList[i].type == VarType.Ref)
                {
                    sr = new SubRecord();
                    sr.Name = "SCRV";
                    sr.SetData(TypeConverter.si2h(i + 1));
                    r.AddRecord(sr);
                    refcount++;
                    localList[i].refid = refcount;
                }
            }

            while (ts.PeekNextStatement().Length > 0)
            {
                try
                {
                    HandleBlock();
                }
                catch (Exception ex)
                {
                    return ReturnError(ex.Message, out msg);
                }
            }

            if (errors.Count > 0)
            {
                return OutputErrors(out msg);
            }

            var header = new byte[20];
            TypeConverter.si2h(refcount, header, 4);
            TypeConverter.i2h((uint)bw.BaseStream.Length, header, 8);
            TypeConverter.si2h(localList.Count, header, 12);
            TypeConverter.si2h(scptype, header, 16);
            schr.SetData(header);
            byte[] compileddata = ((MemoryStream)bw.BaseStream).GetBuffer();
            if (compileddata.Length != bw.BaseStream.Length)
            {
                Array.Resize(ref compileddata, (int)bw.BaseStream.Length);
            }

            scda.SetData(compileddata);
            r2.SubRecords.Clear();
            r2.SubRecords.AddRange(r.SubRecords);
            bw.Close();
            return true;
        }
示例#16
0
 public bool TryGetRecordByID(uint key, out Record value)
 {
     this.RebuildCache();
     return this.FormIDLookup.TryGetValue(key, out value);
 }
示例#17
0
        public bool Process(Record p)
        {
            TESVSnip.Data.RecordsRecord rr = ProcessBase(p);

            // int srIdx = 0;
            var groups = from psr in p.SubRecords
                         group psr by psr.Name into g
                         select new { Name = g.Key, Records = g.ToArray() };

            int lastIndex = 0;
            Dictionary<string, TESVSnip.Data.Subrecord> dict = new Dictionary<string, TESVSnip.Data.Subrecord>();
            foreach (var kvp in groups)
            {
                if (IsCanceled) return false;

                TESVSnip.Data.Subrecord sr = rr.Subrecords.FirstOrDefault(x => x.name == kvp.Name);
                if (sr == null)
                {
                    sr = new TESVSnip.Data.Subrecord();
                    sr.name = sr.desc = kvp.Name;
                    sr.optional = 1;
                    if (lastIndex + 1 <= rr.Items.Count)
                    {
                        rr.Items.Insert(++lastIndex, sr);
                    }
                    else
                    {
                        rr.Items.Add(sr);
                    }
                }
                else
                {
                    lastIndex = rr.Items.IndexOf(sr, lastIndex);
                    if (lastIndex < 0)  // out of order
                        lastIndex = rr.Items.IndexOf(sr);
                }
                int n = kvp.Records.Length;
                if (n > 0)
                {
                    if (sr.repeat == 0 && n > 1)
                        sr.optional = sr.repeat = 1;
                    int idx1 = p.SubRecords.IndexOf(kvp.Records[0]);
                    int idx2 = idx1;
                    for (int i = 1; i < n; ++i, idx1 = idx2)
                    {
                        idx2 = p.SubRecords.IndexOf(kvp.Records[i]);
                        int diff = (idx2 - idx1);
                        if (diff > sr.repeat)
                        {
                            if (diff < 10)
                            {
                                sr.optional = sr.repeat = diff;
                            }
                            else
                            {
                                sr.optional = sr.repeat;
                            }
                        }
                    }
                }
                foreach (var subrec in kvp.Records)
                {
                    if (IsCanceled) break;
                    Process(sr, subrec);
                }
            }
            return true;
        }
示例#18
0
        public static void giveRecordNewFormID(Record rec, bool updateReference, ref uint formCount, ref uint refCount)
        {
            var plugin = GetPluginFromNode(rec);
            if (plugin == null)
            {
                throw new ApplicationException("Cannot select plugin");
            }

            uint newFormID = getNextFormID(plugin);
            uint oldFormID = rec.FormID;
            rec.FormID = newFormID;
            formCount++;
            if (oldFormID != 0 && updateReference)
            {
                updateFormIDReference(plugin, oldFormID, newFormID, ref refCount);
            }

            plugin.InvalidateCache();
        }
示例#19
0
        public static void ReorderSubrecords(Record rec)
        {
            var records = rec.GetStructures();
            if (rec == null || records == null)
                return;

            RecordStructure recStruct;
            if (!records.TryGetValue(rec.Name, out recStruct))
                return;

            SubrecordStructure[] sss = recStruct.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);
        }
示例#20
0
        public bool Evaluate(Record r, SubRecord sr, Element se)
        {
            if (se == null)
            {
                return false;
            }

            var value = sr.GetCompareValue(se);
            int diff = ValueComparer.Compare(value, this.Value);
            switch (this.Type)
            {
                case BatchCondElementType.Set:
                    break;
                case BatchCondElementType.Add:
                    break;
                case BatchCondElementType.Subtract:
                    break;
                case BatchCondElementType.Multiply:
                    break;
                case BatchCondElementType.Divide:
                    break;
                case BatchCondElementType.Clear:
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return false;
        }
示例#21
0
 public override bool Match(Record r, SubRecord sr)
 {
     return this.Type == SearchCondRecordType.Exists ^ sr == null;
 }
示例#22
0
        public override bool Match(Record r, SubRecord sr)
        {
            bool any = false;
            foreach (bool value in sr.EnumerateElements().Where(x => x.Structure.name == this.Record.name).Select(x => this.Match(r, sr, x)))
            {
                if (!value)
                {
                    return false;
                }

                any = true;
            }

            return any;
        }
示例#23
0
        private Record(Record r)
        {
            this.SubRecords = new AdvancedList<SubRecord>(r.SubRecords.Count);
            this.SubRecords.AllowSorting = false;
            foreach (var sr in r.SubRecords.OfType<SubRecord>())
            {
                this.SubRecords.Add((SubRecord) sr.Clone());
            }

            this.Flags1 = r.Flags1;
            this.Flags2 = r.Flags2;
            this.Flags3 = r.Flags3;
            this.FormID = r.FormID;
            Name = r.Name;
            this.descNameOverride = this.DefaultDescriptiveName;
            this.UpdateShortDescription();
            this.FixSubrecordOwner();
        }
示例#24
0
        public object GetDisplayValue(Element elem)
        {
            object value = elem.Value;

            var    sselem     = elem.Structure;
            Record rec        = null;
            string strValue   = null; // value to display
            bool   hasOptions = sselem.options != null && sselem.options.Length > 0;
            bool   hasFlags   = sselem.flags != null && sselem.flags.Length > 1;
            var    p          = this.GetPlugin();

            switch (elem.Structure.type)
            {
            case ElementValueType.FormID:
            {
                var id = (uint)value;
                strValue = id.ToString("X8");
                if (id != 0)
                {
                    rec = p.GetRecordByID(id);
                    if (rec != null)
                    {
                        strValue = string.Format("{0}: {1}", strValue, rec.DescriptiveName);
                    }
                }

                value = strValue;
            }

            break;

            case ElementValueType.LString:
                if (value is uint)
                {
                    if (p != null)
                    {
                        value = p.LookupFormStrings((uint)value) ?? value;
                    }
                }

                break;

            case ElementValueType.Blob:
                value = TypeConverter.GetHexData(elem.Data);
                break;

            case ElementValueType.SByte:
            case ElementValueType.Int:
            case ElementValueType.Short:
            {
                if (sselem.hexview || hasFlags)
                {
                    value = string.Format(string.Format("{{0:X{0}}}", elem.Data.Count * 2), value);
                }
                else
                {
                    value = value ?? string.Empty;
                }

                if (hasOptions)
                {
                    int intVal;
                    if (sselem.hexview || hasFlags)
                    {
                        intVal = int.Parse(value.ToString(), NumberStyles.HexNumber);
                    }
                    else
                    {
                        intVal = Convert.ToInt32(value);
                    }

                    for (int k = 0; k < sselem.options.Length; k += 2)
                    {
                        if (intVal == int.Parse(sselem.options[k + 1]))
                        {
                            value = sselem.options[k];
                        }
                    }
                }
                else if (hasFlags)
                {
                    int intVal = int.Parse(value.ToString(), NumberStyles.HexNumber);
                    var tmp2   = new StringBuilder();
                    for (int k = 0; k < sselem.flags.Length; k++)
                    {
                        if ((intVal & (1 << k)) != 0)
                        {
                            if (tmp2.Length > 0)
                            {
                                tmp2.Append(", ");
                            }

                            tmp2.Append(sselem.flags[k]);
                        }
                    }

                    tmp2.Insert(0, ": ");
                    tmp2.Insert(0, value.ToString());
                    value = tmp2.ToString();
                }
            }

            break;

            case ElementValueType.UInt:
            case ElementValueType.Byte:
            case ElementValueType.UShort:
            {
                if (sselem.hexview || hasFlags)
                {
                    value = string.Format(string.Format("{{0:X{0}}}", elem.Data.Count * 2), value);
                }
                else
                {
                    value = value ?? string.Empty;
                }

                if (hasOptions)
                {
                    uint intVal;
                    if (sselem.hexview || hasFlags)
                    {
                        intVal = uint.Parse(value.ToString(), NumberStyles.HexNumber);
                    }
                    else
                    {
                        intVal = Convert.ToUInt32(value);
                    }

                    for (int k = 0; k < sselem.options.Length; k += 2)
                    {
                        if (intVal == uint.Parse(sselem.options[k + 1]))
                        {
                            value = sselem.options[k];
                        }
                    }
                }
                else if (hasFlags)
                {
                    uint intVal = uint.Parse(value.ToString(), NumberStyles.HexNumber);
                    var  tmp2   = new StringBuilder();
                    for (int k = 0; k < sselem.flags.Length; k++)
                    {
                        if ((intVal & (1 << k)) != 0)
                        {
                            if (tmp2.Length > 0)
                            {
                                tmp2.Append(", ");
                            }

                            tmp2.Append(sselem.flags[k]);
                        }
                    }

                    tmp2.Insert(0, ": ");
                    tmp2.Insert(0, value.ToString());
                    value = tmp2.ToString();
                }
            }

            break;

            case ElementValueType.Str4:
                strValue = TypeConverter.GetString(elem.Data);
                break;

            case ElementValueType.BString:
                strValue = TypeConverter.GetBString(elem.Data);
                break;

            case ElementValueType.IString:
                strValue = TypeConverter.GetIString(elem.Data);
                break;

            default:
                strValue = value == null ? string.Empty : value.ToString();
                break;
            }
            return(value);
        }
示例#25
0
        public void SetContext(Record r, SubRecord sr, bool hexView)
        {
            if (this.r == r && this.sr == sr && this.hexView == hexView)
            {
                return;
            }

            if (r == null || sr == null)
            {
                this.ClearControl();
                return;
            }

            // walk each element in standard fashion
            int panelOffset = 0;
            try
            {
                this.BeginUpdate();

                this.ClearControl();
                SuspendLayout();
                this.fpanel1.SuspendLayout();
                this.fpanel1.Width = Parent.Width;
                this.controlMap.Clear();

                this.hexView = hexView;
                this.r = r;
                this.sr = sr;
                var p = this.GetPluginFromNode(r);
                this.ss = sr.Structure;

                // default to blob if no elements
                if (this.ss == null || this.ss.elements == null || hexView)
                {
                    var c = new HexElement();
                    c.Left = 8;
                    c.Width = this.fpanel1.Width - 16;
                    c.Top = panelOffset;
                    c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                    var elem = r.EnumerateElements(sr, true).FirstOrDefault();
                    if (elem != null)
                    {
                        this.controlMap.Add(elem.Structure, c);
                        this.fpanel1.Controls.Add(c);
                        c.Data = elem.Data;
                    }
                }
                else
                {
                    foreach (var elem in this.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 = this.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 > 0)
                            {
                                var re = new OptionalElement();
                                c = re;
                                c.Left = 8;
                                c.Width = this.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 = this.fpanel1.Width - 16;
                                c.Top = panelOffset;
                                c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                            }

                            c.MinimumSize = c.Size;

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

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

                        IElementControl c;
                        if (this.controlMap.TryGetValue(es, out c))
                        {
                            if (c is IGroupedElementControl)
                            {
                                var gc = c as IGroupedElementControl;
                                gc.Elements.Add(elem.Data);
                            }
                            else
                            {
                                c.Data = elem.Data;
                            }
                        }
                    }
                }

                Enabled = true;
            }
            catch
            {
                this.strWarnOnSave = "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
            }
            finally
            {
                this.fpanel1.ResumeLayout();
                ResumeLayout();
                this.EndUpdate();
                Refresh();
            }
        }
示例#26
0
        private bool CreateSubrecords(RecordsRecord rr, Record r)
        {
            // int srIdx = 0;
            var groups = from psr in r.SubRecords group psr by psr.Name into g select new { Name = g.Key, Records = g.ToArray() };

            int lastIndex = 0;
            var dict = new Dictionary<string, Subrecord>();
            foreach (var kvp in groups)
            {
                if (this.IsCanceled)
                {
                    return false;
                }

                // if (kvp.Name.Count(a => !Char.IsLetterOrDigit(a)) > 0) continue;
                int n = kvp.Records.Length;

                Subrecord sr = rr.Subrecords.FirstOrDefault(x => x.name == kvp.Name);
                if (sr == null)
                {
                    sr = new Subrecord();
                    sr.name = sr.desc = kvp.Name;
                    sr.optional = 1;
                    if (lastIndex + 1 <= rr.Items.Count)
                    {
                        rr.Items.Insert(++lastIndex, sr);
                    }
                    else
                    {
                        rr.Items.Add(sr);
                    }
                }
                else
                {
                    lastIndex = rr.Items.IndexOf(sr, (lastIndex < 0) ? lastIndex : 0);
                    if (lastIndex < 0)
                    {
                        // out of order
                        lastIndex = rr.Items.IndexOf(sr);
                    }
                }

                // Group Detection
                if (n > 0)
                {
                    int idx1 = r.SubRecords.IndexOf(kvp.Records[0]);
                    int idx2 = idx1;
                    for (int i = 1; i < n; ++i, idx1 = idx2)
                    {
                        idx2 = r.SubRecords.IndexOf(kvp.Records[i]);
                        int diff = r.SubRecords.Skip(idx1).Take(idx2 - idx1).Select((a) => a.Name).Distinct().Count();
                        if (diff > sr.repeat)
                        {
                            sr.optional = sr.repeat = diff;
                        }
                    }

                    if (sr.repeat == 0 && n > 1)
                    {
                        sr.optional = sr.repeat = 1;
                    }
                }
            }

            return true;
        }
示例#27
0
 public SubRecord()
 {
     Name       = "NEW_";
     this.Data  = new byte[0];
     this.Owner = null;
 }
        public NewMediumLevelRecordEditor(Plugin p, Record r, SubRecord sr, SubrecordStructure ss)
        {
            this.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
            {
                this.fpanel1.ColumnStyles[0] = new ColumnStyle(SizeType.Percent, 100.0f);
                int maxWidth = this.fpanel1.Width - SystemInformation.VerticalScrollBarWidth - 8;
                int leftOffset = 0; // 8;
                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 = leftOffset;
                            c.Width = maxWidth;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            ge.InnerControl = ec;
                            ge.Element = elem;
                            ec = ge;
                        }
                        else if (elem.optional > 0)
                        {
                            var re = new OptionalElement();
                            c = re;
                            c.Left = leftOffset;
                            c.Width = maxWidth;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;

                            re.InnerControl = ec;
                            re.Element = elem;
                            ec = re;
                            c = re;
                        }
                        else
                        {
                            c.Left = leftOffset;
                            c.Width = maxWidth;
                            c.Top = panelOffset;
                            c.Anchor = c.Anchor | AnchorStyles.Left | AnchorStyles.Right;
                        }

                        this.controlMap.Add(elem, ec);
                        int idx = this.fpanel1.RowCount - 1;
                        this.fpanel1.Controls.Add(c, 0, idx);
                        var info = new RowStyle(SizeType.Absolute, c.Size.Height+2);
                        if (idx == 0)
                            this.fpanel1.RowStyles[0] = info;
                        else
                            this.fpanel1.RowStyles.Add(info);
                        panelOffset = 0;
                        ++this.fpanel1.RowCount;
                    }
                }

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

                    IElementControl c;
                    if (this.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
            {
                this.strWarnOnSave = "The subrecord doesn't appear to conform to the expected structure.\nThe formatted information may be incorrect.";
                this.Error.SetError(this.bSave, this.strWarnOnSave);
                this.Error.SetIconAlignment(this.bSave, ErrorIconAlignment.MiddleLeft);
                AcceptButton = this.bCancel; // remove save as default button when exception occurs
                CancelButton = this.bCancel;
                UpdateDefaultButton();
            }

            ResumeLayout();
        }
示例#29
0
 private SubRecord(SubRecord sr)
 {
     this.Owner = null;
     Name       = sr.Name;
     this.Data  = (byte[])sr.Data.Clone();
 }
示例#30
0
        internal GroupRecord(uint Size, BinaryReader br, bool Oblivion, string[] recFilter, bool filterAll)
        {
            Name = "GRUP";
            this.data = br.ReadBytes(4);
            this.groupType = br.ReadUInt32();
            this.dateStamp = br.ReadUInt32();
            string contentType = this.groupType == 0 ? Encoding.Instance.GetString(this.data) : string.Empty;
            if (!Oblivion)
            {
                this.flags = br.ReadUInt32();
            }

            uint amountRead = 0;
            while (amountRead < Size - (Oblivion ? 20 : 24))
            {
                string s = ReadRecName(br);
                uint recsize = br.ReadUInt32();
                if (s == "GRUP")
                {
                    try
                    {
                        bool skip = filterAll || (recFilter != null && Array.IndexOf(recFilter, contentType) >= 0);
                        var gr = new GroupRecord(recsize, br, Oblivion, recFilter, skip);
                        if (!filterAll)
                        {
                            this.AddRecord(gr);
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show(e.Message);
                    }
                    finally
                    {
                        amountRead += recsize;
                    }
                }
                else
                {
                    bool skip = filterAll || (recFilter != null && Array.IndexOf(recFilter, s) >= 0);
                    if (skip)
                    {
                        long size = recsize + (Oblivion ? 12 : 16);

                        // if ((br.ReadUInt32() & 0x00040000) > 0) size += 4;
                        br.BaseStream.Position += size; // just read past the data
                        amountRead += (uint)(recsize + (Oblivion ? 20 : 24));
                    }
                    else
                    {
                        try
                        {
                            var r = new Record(s, recsize, br, Oblivion);
                            this.AddRecord(r);
                        }
                        catch (Exception e)
                        {
                            MessageBox.Show(e.Message);
                        }
                        finally
                        {
                            amountRead += (uint)(recsize + (Oblivion ? 20 : 24));
                        }
                    }
                }
            }

            this.UpdateShortDescription();
            if (amountRead != (Size - (Oblivion ? 20 : 24)))
            {
                throw new TESParserException(
                    string.Format("Record block did not match the size specified in the group header! Header Size={0:D} Group Size={1:D}", Size - (Oblivion ? 20 : 24), amountRead));
            }
        }
示例#31
0
 public static DialogResult Display(Record r)
 {
     var hr = new HeaderEditor(r);
     return hr.ShowDialog();
 }
示例#32
0
        internal GroupRecord(uint Size, BinaryReader br, TESVSnip.Domain.Data.DomainDefinition define, Func<string, bool> recFilter, bool filterAll)
        {
            Name = "GRUP";
            this.data = br.ReadBytes(4);
            this.groupType = br.ReadUInt32();
            this.dateStamp = br.ReadUInt32();
            string contentType = this.groupType == 0 ? Encoding.Instance.GetString(this.data) : string.Empty;
            if (define.RecSize >= 16)
            {
                this.flags = br.ReadUInt32();
            }

            uint amountRead = 0;
            while (amountRead < Size - (define.RecSize+8))
            {
                string s = ReadRecName(br);
                uint recsize = br.ReadUInt32();
                if (s == "GRUP")
                {
                    try
                    {
                        bool skip = filterAll || (recFilter != null && !recFilter(contentType));
                        var gr = new GroupRecord(recsize, br, define, recFilter, skip);
                        if (!filterAll)
                        {
                            this.AddRecord(gr);
                        }
                    }
                    catch (Exception e)
                    {
                        Alerts.Show(e.Message);
                    }
                    finally
                    {
                        amountRead += recsize;
                    }
                }
                else
                {
                    bool skip = filterAll || (recFilter != null && !recFilter(contentType));
                    if (skip)
                    {
                        long size = recsize + define.RecSize;

                        // if ((br.ReadUInt32() & 0x00040000) > 0) size += 4;
                        br.BaseStream.Position += size; // just read past the data
                        amountRead += (uint)(recsize + (define.RecSize+8));
                    }
                    else
                    {
                        try
                        {
                            var r = new Record(s, recsize, br, define);
                            this.AddRecord(r);
                        }
                        catch (Exception e)
                        {
                            Alerts.Show(e.Message);
                        }
                        finally
                        {
                            amountRead += (uint)(recsize + (define.RecSize+8));
                        }
                    }
                }
            }

            this.UpdateShortDescription();
            if (amountRead != (Size - (define.RecSize+8)))
            {
                throw new TESParserException(
                    string.Format("Record block did not match the size specified in the group header! Header Size={0:D} Group Size={1:D}", Size - (define.RecSize+8), amountRead));
            }
        }