Exemple #1
0
 private HeaderEditor(Record r)
 {
     InitializeComponent();
     Icon = Resources.tesv_ico;
     R = r;
     tbName.Text = r.Name;
     Flags1 = r.Flags1;
     Flags2 = r.Flags2;
     Flags3 = r.Flags3;
     FormID = r.FormID;
     tbFormID.Text = FormID.ToString("x8");
     tbFlags2.Text = Flags2.ToString("x8");
     tbFlags3.Text = Flags3.ToString("x8");
     foreach (Control c in Controls)
     {
         if (c is CheckBox)
         {
             c.Tag = int.Parse((string) c.Tag);
             if ((Flags1 & GetInt(c.Tag)) > 0) ((CheckBox) c).Checked = true;
         }
     }
     //cb18.Checked=false; // Allow compression
 }
        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();
        }
Exemple #3
0
 public bool TryGetRecordByID(uint key, out Record value)
 {
     RebuildCache();
     return FormIDLookup.TryGetValue(key, out value);
 }
Exemple #4
0
 internal GroupRecord(uint Size, BinaryReader br, bool Oblivion, string[] recFilter, bool filterAll)
 {
     Name = "GRUP";
     data = br.ReadBytes(4);
     groupType = br.ReadUInt32();
     dateStamp = br.ReadUInt32();
     string contentType = groupType == 0 ? Encoding.CP1252.GetString(data) : "";
     if (!Oblivion) 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) AddRecord(gr);
             }
             catch (Exception e)
             {
                 System.Windows.Forms.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);
                     AddRecord(r);
                 }
                 catch (Exception e)
                 {
                     System.Windows.Forms.MessageBox.Show(e.Message);
                 }
                 finally
                 {
                     amountRead += (uint)(recsize + (Oblivion ? 20 : 24));
                 }
             }
         }
     }
     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));
     }
 }
Exemple #5
0
 public static DialogResult Display(Record r)
 {
     var hr = new HeaderEditor(r);
     return hr.ShowDialog();
 }
Exemple #6
0
 private Record(Record r)
 {
     SubRecords = new AdvancedList<SubRecord>(r.SubRecords.Count);
     SubRecords.AllowSorting = false;
     foreach (var sr in r.SubRecords.OfType<SubRecord>())
         SubRecords.Add((SubRecord) sr.Clone());
     Flags1 = r.Flags1;
     Flags2 = r.Flags2;
     Flags3 = r.Flags3;
     FormID = r.FormID;
     Name = r.Name;
     descNameOverride = DefaultDescriptiveName;
     UpdateShortDescription();
     FixSubrecordOwner();
 }
Exemple #7
0
 public static void giveRecordNewFormID(Record rec, bool updateReference)
 {
     uint formCount = 0, refCount = 0;
     giveRecordNewFormID(rec, updateReference, ref formCount, ref refCount);
 }
Exemple #8
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();
 }
Exemple #9
0
 private static bool OutputErrors(out string msg)
 {
     msg = "";
     foreach (string s in errors) msg += s + Environment.NewLine;
     ts = null;
     r = null;
     return false;
 }
Exemple #10
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);
        }
Exemple #11
0
 private static bool ReturnError(string msg, out string error)
 {
     error = ts.Line.ToString() + ": " + msg;
     ts = null;
     r = null;
     return false;
 }
Exemple #12
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;
        }
Exemple #13
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;
        }
Exemple #14
0
        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();
        }
Exemple #15
0
 public bool TryGetRecordByID(uint key, out Record value)
 {
     RebuildCache();
     return FormIDLookup.TryGetValue(key, out value);
 }