Beispiel #1
0
        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();
        }
Beispiel #2
0
        /// <summary>
        /// Extract any internalized strings and put in string table
        /// </summary>
        /// <param name="plugin"></param>
        public static int ExtractInternalStrings(Plugin plugin)
        {
            int count = 0;
            // uint maxid = plugin.Masters.Max(x=>x.Strings.Count > 0 ? x.Strings.Keys.Max() : 0); // No need to check the masters string since the numbers are unique for every plugin

            bool anyModified = false;
            foreach (var record in plugin.Enumerate().OfType<Record>())
            {
                record.MatchRecordStructureToRecord();
                foreach (var sr in record.SubRecords)
                {
                    var elements = record.EnumerateElements(sr, rawData: true).ToList();
                    foreach (var elem in elements)
                    {
                        if (elem.Structure != null && elem.Structure.type == ElementValueType.LString)
                        {
                            var data = elem.Data;
                            uint id = TypeConverter.h2i(data);
                            if (id == 0) continue;
                            if (data.Count == 4 && TypeConverter.IsLikelyString(data))
                            {
                                string str;
                                if (plugin.Strings.TryGetValue(id, out str))
                                    continue;
                            }
                            if (data.Count != 4 || TypeConverter.IsLikelyString(data))
                            {
                                string value = TypeConverter.GetString(data);
                                if (!String.IsNullOrEmpty(value))
                                {
                                    // uint nextid = Math.Max(maxid, plugin.Strings.Count == 0 ? 0 : plugin.Strings.Keys.Max()) + 1; // No need to check the masters strings since the numbers are unique for every plugin
                                    uint nextid = (plugin.Strings.Count == 0 ? 0 : plugin.Strings.Keys.Max()) + 1;
                                    int idx = plugin.Strings.FindValue(value);
                                    if (idx >= 0)
                                    {
                                        nextid = plugin.Strings.ElementAt(idx).Key;
                                    }
                                    else
                                    {
                                        plugin.Strings[nextid] = value;                                        
                                    }
                                    elem.AssignValue<ArraySegment<byte>>(
                                        new ArraySegment<byte>((byte[])TypeConverter.i2h(nextid).Clone()));
                                    ++count;
                                }
                            }
                        }
                    }
                    if (elements.Any(x => x.Changed))
                    {
                        // need to repack the structure
                        using (var ms = new MemoryStream(sr.GetReadonlyData().Length))
                        {
                            foreach (var seg in elements.Select(elem => elem.Data))
                                ms.Write(seg.Array, seg.Offset, seg.Count);
                            sr.SetData(ms.ToArray());
                        }
                        anyModified = true;
                    }
                }
            }
            if (anyModified)
            {
                var tes4 = plugin.Records.OfType<Record>().FirstOrDefault(x => x.Name == "TES4");
                if (tes4 != null) tes4.Flags1 |= 0x00000080U;
            }
            return count;
        }
Beispiel #3
0
        private void bSave_Click(object sender, EventArgs e)
        {
            bSave.Focus();
            uint flags, datestamp;

            if (!uint.TryParse(tbDateStamp.Text, NumberStyles.AllowHexSpecifier, null, out datestamp))
            {
                tbDateStamp.Focus();
                MessageBox.Show("Invalid value specified for datestamp");
                return;
            }
            if (!uint.TryParse(tbFlags.Text, NumberStyles.AllowHexSpecifier, null, out flags))
            {
                tbFlags.Focus();
                MessageBox.Show("Invalid value specified for flags");
                return;
            }
            var grouptype = (uint)cmbGroupType.SelectedIndex;

            byte[] data;
            switch (cmbGroupType.SelectedIndex)
            {
            case 0:
                if (tbRecType.TextLength != 4)
                {
                    tbRecType.Focus();
                    MessageBox.Show("Invalid parent record type. Needs to be 4 characters!");
                    return;
                }
                data = new byte[4];
                Encoding.CP1252.GetBytes(tbRecType.Text, 0, 4, data, 0);
                break;

            case 2:
            case 3:
                uint block;
                if (!uint.TryParse(tbBlock.Text, out block))
                {
                    tbBlock.Focus();
                    MessageBox.Show("Invalid value specified for block id");
                    return;
                }
                data = TypeConverter.i2h(block);
                break;

            case 4:
            case 5:
                short x, y;
                if (!short.TryParse(tbX.Text, out x))
                {
                    tbX.Focus();
                    MessageBox.Show("Invalid value specified for x coord");
                    return;
                }
                if (!short.TryParse(tbY.Text, out y))
                {
                    tbY.Focus();
                    MessageBox.Show("Invalid value specified for y coord");
                    return;
                }
                data = new byte[4];
                TypeConverter.ss2h(x, data, 2);
                TypeConverter.ss2h(y, data, 0);
                break;

            case 1:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                uint parent;
                if (!uint.TryParse(tbParent.Text, NumberStyles.AllowHexSpecifier, null, out parent))
                {
                    tbParent.Focus();
                    MessageBox.Show("Invalid value specified for parent");
                    return;
                }
                data = TypeConverter.i2h(parent);
                break;

            default:
                cmbGroupType.Focus();
                MessageBox.Show("Sanity check failed; invalid group type");
                return;
            }
            gr.flags     = flags;
            gr.dateStamp = datestamp;
            gr.groupType = grouptype;
            gr.SetData(data);
            gr.UpdateShortDescription();
            this.DialogResult = DialogResult.OK;
        }
Beispiel #4
0
        private void bSave_Click(object sender, EventArgs e)
        {
            uint flags, datestamp;

            if (!uint.TryParse(tbDateStamp.Text, NumberStyles.AllowHexSpecifier, null, out datestamp))
            {
                MessageBox.Show("Invalid value specified for datestamp");
                return;
            }
            if (!uint.TryParse(tbFlags.Text, NumberStyles.AllowHexSpecifier, null, out flags))
            {
                MessageBox.Show("Invalid value specified for flags");
                return;
            }
            var grouptype = (uint)cmbGroupType.SelectedIndex;

            byte[] data;
            switch (cmbGroupType.SelectedIndex)
            {
            case 0:
                data = Encoding.CP1252.GetBytes(tbRecType.Text);
                break;

            case 2:
            case 3:
                uint block;
                if (!uint.TryParse(tbBlock.Text, out block))
                {
                    MessageBox.Show("Invalid value specified for block id");
                    return;
                }
                data = TypeConverter.i2h(block);
                break;

            case 4:
            case 5:
                short x, y;
                if (!short.TryParse(tbX.Text, out x))
                {
                    MessageBox.Show("Invalid value specified for x coord");
                    return;
                }
                if (!short.TryParse(tbY.Text, out y))
                {
                    MessageBox.Show("Invalid value specified for y coord");
                    return;
                }
                data = new byte[4];
                TypeConverter.ss2h(x, data, 2);
                TypeConverter.ss2h(y, data, 0);
                break;

            case 1:
            case 6:
            case 7:
            case 8:
            case 9:
            case 10:
                uint parent;
                if (!uint.TryParse(tbParent.Text, NumberStyles.AllowHexSpecifier, null, out parent))
                {
                    MessageBox.Show("Invalid value specified for parent");
                    return;
                }
                data = TypeConverter.i2h(parent);
                break;

            default:
                MessageBox.Show("Sanity check failed; invalid group type");
                return;
            }
            gr.flags     = flags;
            gr.dateStamp = datestamp;
            gr.groupType = grouptype;
            gr.SetData(data);
        }