public static void UpdateRecordCount(Plugin plugin) { int reccount = -1 + plugin.Records.Cast<Rec>().Sum(r => sanitizeCountRecords(r)); var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name == "TES4"); if (tes4 != null) { if (tes4.SubRecords.Count > 0 && tes4.SubRecords[0].Name == "HEDR" && tes4.SubRecords[0].Size >= 8) { byte[] data = tes4.SubRecords[0].GetData(); byte[] reccountbytes = TypeConverter.si2h(reccount); for (int i = 0; i < 4; i++) data[4 + i] = reccountbytes[i]; tes4.SubRecords[0].SetData(data); } } }
private void bCInt_Click(object sender, EventArgs e) { int i; if (!int.TryParse(tbInt.Text, out i)) { MessageBox.Show("Invalid int"); return; } byte[] b = TypeConverter.si2h(i); var pos = (int)hexBox1.SelectionStart; bytes[pos + 0] = b[0]; bytes[pos + 1] = b[1]; bytes[pos + 2] = b[2]; bytes[pos + 3] = b[3]; hexBox1.Refresh(); }
public static void SanitizePlugin(Plugin plugin) { // performance update to prevent lists from updating currently selected record bool oldHoldUpdates = BaseRecord.HoldUpdates; try { BaseRecord.HoldUpdates = true; if (plugin == null) { throw new ApplicationException("Cannot select plugin"); } var hdr = plugin.Records.OfType <Rec>().FirstOrDefault(x => x.Name == "TES4"); if (hdr == null) { throw new ApplicationException(Resources.PluginLacksAValidTes4RecordCannotContinue); } var toParse = new Queue <BaseRecord>(plugin.Records.OfType <BaseRecord>().Where(x => !x.Equals(hdr))); plugin.Clear(); plugin.AddRecord(hdr); var groups = new Dictionary <string, GroupRecord>(); foreach (string s in SanitizeOrder) { var gr = new GroupRecord(s); plugin.AddRecord(gr); groups[s] = gr; } bool looseGroupsWarning = false; bool unknownRecordsWarning = false; while (toParse.Count > 0) { var r = toParse.Dequeue(); if (r is GroupRecord) { var gr = (GroupRecord)r; if (gr.ContentsType == "CELL" || gr.ContentsType == "WRLD" || gr.ContentsType == "DIAL") { var gr2 = groups[gr.ContentsType]; foreach (BaseRecord r2 in gr.Records) { gr2.AddRecord(r2); } gr.Clear(); } else { foreach (BaseRecord r2 in gr.Records) { toParse.Enqueue(r2); } gr.Clear(); } } else if (r is Record) { var r2 = (Record)r; if (LooseGroups.Contains(r2.Name)) { looseGroupsWarning = true; plugin.AddRecord(r2); } else { if (groups.ContainsKey(r2.Name)) { groups[r2.Name].AddRecord(r2); } else { unknownRecordsWarning = true; plugin.AddRecord(r2); } } } } foreach (GroupRecord gr2 in groups.Values) { if (gr2.Records.Count == 0) { plugin.DeleteRecord(gr2); } } if (looseGroupsWarning) { MessageBox.Show(Resources.CannotSanitizeLooseGroups, Resources.WarningText); } if (unknownRecordsWarning) { MessageBox.Show(Resources.CannotSanitizeUnknownRecords, Resources.WarningText); } plugin.InvalidateCache(); int reccount = -1 + plugin.Records.Cast <Rec>().Sum(r => sanitizeCountRecords(r)); var tes4 = plugin.Records.OfType <Record>().FirstOrDefault(x => x.Name == "TES4"); if (tes4 != null) { if (tes4.SubRecords.Count > 0 && tes4.SubRecords[0].Name == "HEDR" && tes4.SubRecords[0].Size >= 8) { byte[] data = tes4.SubRecords[0].GetData(); byte[] reccountbytes = TypeConverter.si2h(reccount); for (int i = 0; i < 4; i++) { data[4 + i] = reccountbytes[i]; } tes4.SubRecords[0].SetData(data); } } } finally { BaseRecord.HoldUpdates = oldHoldUpdates; } }
private void bSave_Click(object sender, EventArgs e) { var bytes = new List <byte>(); for (int j = 0; j < boxes.Count; j++) { var vt = valueTypes[j]; string tbText = boxes[j].Text; NumberStyles numStyle = NumberStyles.Any; if (tbText.StartsWith("0x")) { numStyle = NumberStyles.HexNumber; tbText = tbText.Substring(2); } if (!boxes[j].Enabled) { continue; } switch (vt) { case ElementValueType.Byte: { byte b; if (!byte.TryParse(tbText, numStyle, null, out b)) { MessageBox.Show("Invalid byte: " + tbText, "Error"); return; } bytes.Add(b); break; } case ElementValueType.Short: { short s; if (!short.TryParse(tbText, numStyle, null, out s)) { MessageBox.Show("Invalid short: " + tbText, "Error"); return; } byte[] conv = TypeConverter.ss2h(s); bytes.Add(conv[0]); bytes.Add(conv[1]); break; } case ElementValueType.UShort: { ushort s; if (!ushort.TryParse(tbText, numStyle, null, out s)) { MessageBox.Show("Invalid ushort: " + tbText, "Error"); return; } byte[] conv = TypeConverter.s2h(s); bytes.Add(conv[0]); bytes.Add(conv[1]); break; } case ElementValueType.Int: { int i; if (!int.TryParse(tbText, numStyle, null, out i)) { MessageBox.Show("Invalid int: " + tbText, "Error"); return; } byte[] conv = TypeConverter.si2h(i); bytes.AddRange(conv); break; } case ElementValueType.UInt: { uint i; if (!uint.TryParse(tbText, numStyle, null, out i)) { MessageBox.Show("Invalid uint: " + tbText, "Error"); return; } byte[] conv = TypeConverter.i2h(i); bytes.AddRange(conv); break; } case ElementValueType.Float: { float f; if (!float.TryParse(tbText, numStyle, null, out f)) { MessageBox.Show("Invalid float: " + tbText, "Error"); return; } byte[] conv = TypeConverter.f2h(f); bytes.AddRange(conv); break; } case ElementValueType.FormID: { uint i; if (!uint.TryParse(tbText, NumberStyles.AllowHexSpecifier, null, out i)) { MessageBox.Show("Invalid formID: " + tbText, "Error"); return; } byte[] conv = TypeConverter.i2h(i); bytes.AddRange(conv); break; } case ElementValueType.String: { byte[] conv = System.Text.Encoding.Default.GetBytes(tbText); bytes.AddRange(conv); bytes.Add(0); break; } case ElementValueType.BString: { bytes.AddRange(TypeConverter.s2h((ushort)tbText.Length)); bytes.AddRange(System.Text.Encoding.Default.GetBytes(tbText)); break; } case ElementValueType.IString: { bytes.AddRange(TypeConverter.si2h(tbText.Length)); bytes.AddRange(System.Text.Encoding.Default.GetBytes(tbText)); break; } case ElementValueType.LString: { uint i; var ltag = boxes[j].Tag as lTag; if (ltag != null) { if (!ltag.cb.Checked) { if (!uint.TryParse(ltag.id.Text, NumberStyles.AllowHexSpecifier, null, out i)) { MessageBox.Show("Invalid string id: " + ltag.id.Text, "Error"); return; } byte[] conv = TypeConverter.i2h(i); bytes.AddRange(conv); } else { byte[] conv = System.Text.Encoding.Default.GetBytes(ltag.str.Text); bytes.AddRange(conv); bytes.Add(0); } } break; } case ElementValueType.Str4: { var txtbytes = new byte[] { 0x32, 0x32, 0x32, 0x32 }; System.Text.Encoding.Default.GetBytes(tbText, 0, Math.Min(4, tbText.Length), txtbytes, 0); bytes.AddRange(txtbytes); } break; default: throw new ApplicationException(); } } sr.SetData(bytes.ToArray()); Close(); }