示例#1
0
        protected virtual void UpdateText()
        {
            var data = GetCurrentData();

            if (element == null || data == null || data.Array == null)
            {
                this.textBox.Text = "<error>";
            }
            else
            {
                bool fitTextBoxToWidth = false;
                var  es       = element;
                var  tb       = this.textBox;
                bool hasFlags = es.options.Length == 0 && es.flags.Length > 1;
                switch (element.type)
                {
                case ElementValueType.UInt:
                {
                    var value = TypeConverter.h2i(data);
                    if (_pyInterpreterCalc)
                    {
                        value = PyInterpreter.ExecuteFunction <uint>(element, value, FunctionOperation.ForReading);
                    }
                    _pyInterpreterCalc = false;
                    this.textBox.Text  = hasFlags || es.hexview ? "0x" + value.ToString("X8") : value.ToString(CultureInfo.InvariantCulture);
                    //var v = TypeConverter.h2i(data);
                    //this.textBox.Text = element.hexview ? "0x" + v.ToString("X8") : v.ToString();
                }

                break;

                case ElementValueType.Int:
                {
                    var value = TypeConverter.h2si(data);
                    if (_pyInterpreterCalc)
                    {
                        value = PyInterpreter.ExecuteFunction <int>(element, value, FunctionOperation.ForReading);
                    }
                    _pyInterpreterCalc = false;
                    this.textBox.Text  = hasFlags || es.hexview ? "0x" + value.ToString("X8") : value.ToString(CultureInfo.InvariantCulture);
                    //var v = TypeConverter.h2si(data);
                    //this.textBox.Text = hasFlags || es.hexview ? "0x" + v.ToString("X8") : v.ToString();
                }

                break;

                case ElementValueType.FormID:
                    this.textBox.Text = TypeConverter.h2i(data).ToString("X8");
                    break;

                case ElementValueType.Float:
                {
                    var value = TypeConverter.h2f(data);
                    if (_pyInterpreterCalc)
                    {
                        value = PyInterpreter.ExecuteFunction <float>(element, value, FunctionOperation.ForReading);
                    }
                    _pyInterpreterCalc = false;
                    this.textBox.Text  = value.ToString(CultureInfo.InvariantCulture);
                    //this.textBox.Text = TypeConverter.h2f(data).ToString();
                }
                break;

                case ElementValueType.UShort:
                {
                    var value = TypeConverter.h2s(data);
                    if (_pyInterpreterCalc)
                    {
                        value = PyInterpreter.ExecuteFunction <ushort>(element, value, FunctionOperation.ForReading);
                    }
                    _pyInterpreterCalc = false;
                    this.textBox.Text  = hasFlags || es.hexview ? "0x" + value.ToString("X4") : value.ToString(CultureInfo.InvariantCulture);
                    //var v = TypeConverter.h2s(data);
                    //this.textBox.Text = hasFlags || es.hexview ? "0x" + v.ToString("X4") : v.ToString();
                }

                break;

                case ElementValueType.Short:
                {
                    var value = TypeConverter.h2ss(data);
                    if (_pyInterpreterCalc)
                    {
                        value = PyInterpreter.ExecuteFunction <short>(element, value, FunctionOperation.ForReading);
                    }
                    _pyInterpreterCalc = false;
                    this.textBox.Text  = hasFlags || es.hexview ? "0x" + value.ToString("X4") : value.ToString(CultureInfo.InvariantCulture);
                    //var v = TypeConverter.h2ss(data);
                    //tb.Text = hasFlags || es.hexview ? "0x" + v.ToString("X4") : v.ToString();
                }

                break;

                case ElementValueType.Byte:
                {
                    var v = TypeConverter.h2b(data);
                    tb.Text = hasFlags || es.hexview ? "0x" + v.ToString("X2") : v.ToString();
                }

                break;

                case ElementValueType.SByte:
                {
                    var v = TypeConverter.h2sb(data);
                    tb.Text = hasFlags || es.hexview ? "0x" + v.ToString("X2") : v.ToString();
                }

                break;

                case ElementValueType.String:
                    tb.Text           = TypeConverter.GetZString(data);
                    fitTextBoxToWidth = true;
                    break;

                case ElementValueType.BString:
                    tb.Text           = TypeConverter.GetBString(data);
                    fitTextBoxToWidth = true;
                    break;

                case ElementValueType.IString:
                    tb.Text           = TypeConverter.GetIString(data);
                    fitTextBoxToWidth = true;
                    break;

                case ElementValueType.LString:
                {
                    uint id = TypeConverter.IsLikelyString(data) ? 0 : TypeConverter.h2i(data);
                    tb.Text = id.ToString("X8");
                }

                break;

                case ElementValueType.Str4:
                {
                    tb.Text      = (data.Count >= 4) ? Encoding.Instance.GetString(data.Array, data.Offset, 4) : string.Empty;
                    tb.MaxLength = 4;
                }

                break;

                default:
                {
                    tb.Text    = "<Error>";
                    tb.Enabled = false;
                }

                break;
                }

                if (fitTextBoxToWidth)
                {
                    this.lblText.Left   = ((Width - this.lblText.Width - 50) / 50) * 50;
                    this.lblText.Anchor = AnchorStyles.Right | AnchorStyles.Top;
                    this.textBox.Width  = this.lblText.Left - 20 - this.textBox.Left;
                    this.textBox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
                }
            }
        }
示例#2
0
        private void bSave_Click(object sender, EventArgs e)
        {
            // warn user about data corruption.  But this may be case of fixing using tesvsnip to fix corruption so still allow
            if (this.strWarnOnSave != null)
            {
                if (DialogResult.Yes
                    !=
                    MessageBox.Show(
                        this,
                        this.strWarnOnSave + "\n\nData maybe lost if saved. Do you want to continue saving?",
                        "Warning",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Warning,
                        MessageBoxDefaultButton.Button2))
                {
                    return;
                }
            }

            using (var str = new MemoryStream())
            {
                foreach (var kvp in this.controlMap)
                {
                    var c = kvp.Value;
                    if (c is IGroupedElementControl)
                    {
                        var gc = c as IGroupedElementControl;
                        foreach (var elem in gc.Elements)
                        {
                            str.Write(elem.Array, elem.Offset, elem.Count);
                        }
                    }
                    else
                    {
                        var elem = c.Data;
                        if (elem.Count > 0 && elem.Array != null)
                        {
                            switch (kvp.Key.type)
                            {
                            case ElementValueType.UInt:
                            {
                                var sf    = new ArraySegment <byte>(elem.Array, elem.Offset, elem.Count);
                                var value = TypeConverter.h2i(sf);
                                if (!string.IsNullOrWhiteSpace(kvp.Key.funcw))
                                {
                                    bool valueIsChanged = (kvp.Value).Changed;
                                    if (kvp.Value is OptionalElement)
                                    {
                                        valueIsChanged = (((OptionalElement)(kvp.Value)).InnerControl).Changed;
                                    }
                                    if (valueIsChanged)
                                    {
                                        value = PyInterpreter.ExecuteFunction <uint>(kvp.Key, value, FunctionOperation.ForWriting);
                                        var b = TypeConverter.i2h(value);
                                        Buffer.BlockCopy(b, 0, elem.Array, elem.Offset, elem.Count);
                                    }
                                }
                            }
                            break;

                            case ElementValueType.FormID:
                            {
                            }
                            break;

                            case ElementValueType.Int:
                            {
                                var sf    = new ArraySegment <byte>(elem.Array, elem.Offset, elem.Count);
                                var value = TypeConverter.h2si(sf);
                                if (!string.IsNullOrWhiteSpace(kvp.Key.funcw))
                                {
                                    bool valueIsChanged = (kvp.Value).Changed;
                                    if (kvp.Value is OptionalElement)
                                    {
                                        valueIsChanged = (((OptionalElement)(kvp.Value)).InnerControl).Changed;
                                    }
                                    if (valueIsChanged)
                                    {
                                        value = PyInterpreter.ExecuteFunction <int>(kvp.Key, value, FunctionOperation.ForWriting);
                                        var b = TypeConverter.si2h(value);
                                        Buffer.BlockCopy(b, 0, elem.Array, elem.Offset, elem.Count);
                                    }
                                }
                            }
                            break;

                            case ElementValueType.Float:
                            {
                                var sf    = new ArraySegment <byte>(elem.Array, elem.Offset, elem.Count);
                                var value = TypeConverter.h2f(sf);
                                if (!string.IsNullOrWhiteSpace(kvp.Key.funcw))
                                {
                                    bool valueIsChanged = (kvp.Value).Changed;
                                    if (kvp.Value is OptionalElement)
                                    {
                                        valueIsChanged = (((OptionalElement)(kvp.Value)).InnerControl).Changed;
                                    }
                                    if (valueIsChanged)
                                    {
                                        value = PyInterpreter.ExecuteFunction <float>(kvp.Key, value, FunctionOperation.ForWriting);
                                        var b = TypeConverter.f2h(value);
                                        Buffer.BlockCopy(b, 0, elem.Array, elem.Offset, elem.Count);
                                    }
                                }
                            }
                            break;

                            case ElementValueType.UShort:
                            {
                                var sf    = new ArraySegment <byte>(elem.Array, elem.Offset, elem.Count);
                                var value = TypeConverter.h2s(sf);
                                if (!string.IsNullOrWhiteSpace(kvp.Key.funcw))
                                {
                                    bool valueIsChanged = (kvp.Value).Changed;
                                    if (kvp.Value is OptionalElement)
                                    {
                                        valueIsChanged = (((OptionalElement)(kvp.Value)).InnerControl).Changed;
                                    }
                                    if (valueIsChanged)
                                    {
                                        value = PyInterpreter.ExecuteFunction <ushort>(kvp.Key, value, FunctionOperation.ForWriting);
                                        var b = TypeConverter.s2h(value);
                                        Buffer.BlockCopy(b, 0, elem.Array, elem.Offset, elem.Count);
                                    }
                                }
                            }
                            break;

                            case ElementValueType.Short:
                            {
                                var sf    = new ArraySegment <byte>(elem.Array, elem.Offset, elem.Count);
                                var value = TypeConverter.h2ss(sf);
                                if (!string.IsNullOrWhiteSpace(kvp.Key.funcw))
                                {
                                    bool valueIsChanged = (kvp.Value).Changed;
                                    if (kvp.Value is OptionalElement)
                                    {
                                        valueIsChanged = (((OptionalElement)(kvp.Value)).InnerControl).Changed;
                                    }
                                    if (valueIsChanged)
                                    {
                                        value = PyInterpreter.ExecuteFunction <short>(kvp.Key, value, FunctionOperation.ForWriting);
                                        var b = TypeConverter.ss2h(value);
                                        Buffer.BlockCopy(b, 0, elem.Array, elem.Offset, elem.Count);
                                    }
                                }
                            }
                            break;

                            case ElementValueType.Byte:
                            {
                            }
                            break;

                            case ElementValueType.SByte:
                            {
                            }
                            break;

                            case ElementValueType.String:
                                break;

                            case ElementValueType.BString:
                                break;

                            case ElementValueType.IString:
                                break;

                            case ElementValueType.LString:
                            {
                                // not handled
                            }
                            break;

                            case ElementValueType.Str4:
                            {
                            }
                            break;
                            }
                            str.Write(elem.Array, elem.Offset, elem.Count);
                        }
                    }
                }

                byte[] newData = str.ToArray();
                this.sr.SetData(newData);
            }
        }