Exemplo n.º 1
0
        public void Process(TESVSnip.Data.Subrecord sr, SubRecord subrec)
        {
            TESVSnip.Data.SubrecordElement expectedElement = (sr.Elements.Count >= 1) ? sr.Elements[0] : null;
            if (expectedElement != null && expectedElement.type == "blob")
            {
                return;
            }

            byte[] data = subrec.GetReadonlyData();
            if (data.Length == 0)
            {
                if (sr.Elements.Count == 0)
                {
                    sr.Elements.Add(CreateBlob());
                }
                return;
            }
            if (data.Length == 2)
            {
// common scenarios
                if (expectedElement != null && expectedElement.size == 2)
                {
                    return;
                }

                TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                elem.name = "Unknown";
                elem.type = "short";
                elem.size = 2;
                sr.Elements.Add(elem);
                UpdateSize(sr);
                return;
            }

            if (data.Length > 0)
            {
                bool isAscii = true;
                for (int i = 0; i < data.Length - 1 && isAscii; ++i)
                {
                    isAscii = !Char.IsControl((char)data[i]);
                }
                isAscii = (isAscii && data[data.Length - 1] == 0);
                if (isAscii)
                {
                    // test if its a string
                    if (expectedElement == null)
                    {
                        TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                        elem.name = "Unknown";
                        elem.type = "string";
                        sr.Elements.Add(elem);
                    }
                    else if (expectedElement.type != "string")
                    {
                        sr.Elements.Clear();
                        sr.Elements.Add(CreateBlob());
                    }
                    return;
                }
            }
            for (int offset = 0, elemSize = 4; offset < data.Length; offset += elemSize)
            {
                if (IsCanceled)
                {
                    return;
                }

                int left = data.Length - offset;
                if (left >= 4)
                {
// common scenarios
                    if (expectedElement != null && expectedElement.size == 4)
                    {
                        continue;
                    }

                    ushort lhs = TypeConverter.h2s(data[offset], data[offset + 1]);
                    ushort uhs = TypeConverter.h2s(data[offset + 2], data[offset + 3]);
                    uint   ui4 = TypeConverter.h2i(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                    if (ui4 == 0)
                    {
                        if (expectedElement == null)
                        {
                            TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                            elem.name = "Unknown";
                            elem.type = "int";
                            elem.size = 4;
                            sr.Elements.Add(elem);
                            UpdateSize(sr);
                            continue;
                        }
                    }
                    else
                    {
                        float flt = TypeConverter.h2f(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                        if (IsLikelyFloat(flt))
                        {
// replace element which is int with float
                            if (expectedElement == null)
                            {
                                TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                elem.name = "Unknown";
                                elem.type = "float";
                                elem.size = 4;
                                sr.Elements.Add(elem);
                            }
                            else
                            {
                                if (expectedElement.type == "int")
                                {
                                    expectedElement.type = "float";
                                }
                            }
                            continue;
                        }
                        else
                        {
                            Record r = this.FormLookup(ui4);
                            if (r != null)
                            {
                                string reftype = r.DescriptiveName.Substring(0, 4); // ???
                                if (expectedElement == null)
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name    = "ID";
                                    elem.type    = "formid";
                                    elem.reftype = reftype;
                                    elem.size    = 4;
                                    sr.Elements.Add(elem);
                                }
                                else
                                {
                                    if (expectedElement.type == "formid")
                                    {
                                        if (expectedElement.reftype != reftype)
                                        {
                                            expectedElement.reftype = string.Empty;
                                        }
                                    }
                                }
                                continue;
                            }
                            else
                            {
                                string s = this.StringLookup(ui4);
                                if (!string.IsNullOrEmpty(s))
                                {
                                    if (expectedElement == null)
                                    {
                                        TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                        elem.name = "Unknown";
                                        elem.type = "string";
                                        sr.Elements.Add(elem);
                                    }
                                    continue;
                                }
                            }
                            if (expectedElement == null)
                            {
                                if (lhs > 0 && lhs < 255 && uhs > 0 && uhs < 255)
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name = "Unknown";
                                    elem.type = "short";
                                    elem.size = 2;
                                    sr.Elements.Add(elem);
                                    sr.Elements.Add(elem);
                                }
                                else
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name = "Unknown";
                                    elem.type = "int";
                                    elem.size = 4;
                                    sr.Elements.Add(elem);
                                    UpdateSize(sr);
                                }
                            }
                        }
                    }
                }
            }


            if (sr.Elements.Count == 0)
            {
                sr.Elements.Add(CreateBlob());
            }
            UpdateSize(sr);
        }
Exemplo n.º 2
0
        public void Process(TESVSnip.Data.Subrecord sr, SubRecord subrec)
        {
            TESVSnip.Data.SubrecordElement expectedElement = (sr.Elements.Count >= 1) ? sr.Elements[0] : null;
            if (expectedElement != null && expectedElement.type == "blob")
                return;

            byte[] data = subrec.GetReadonlyData();
            if (data.Length == 0)
            {
                if (sr.Elements.Count == 0)
                    sr.Elements.Add(CreateBlob());
                return;
            }
            if (data.Length == 2)            {

// common scenarios
                if (expectedElement != null && expectedElement.size == 2)
                    return;

                TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                elem.name = "Unknown";
                elem.type = "short";
                elem.size = 2;
                sr.Elements.Add(elem);
                UpdateSize(sr);
                return;
            }

            if (data.Length > 0)
            {
                bool isAscii = true;
                for (int i = 0; i < data.Length - 1 && isAscii; ++i)
                    isAscii = !Char.IsControl((char)data[i]);
                isAscii = (isAscii && data[data.Length - 1] == 0);
                if (isAscii)
                {
                    // test if its a string
                    if (expectedElement == null)
                    {
                        TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                        elem.name = "Unknown";
                        elem.type = "string";
                        sr.Elements.Add(elem);
                    }
                    else if (expectedElement.type != "string")
                    {
                        sr.Elements.Clear();
                        sr.Elements.Add(CreateBlob());
                    }
                    return;
                }
            }
            for (int offset = 0, elemSize = 4; offset < data.Length; offset += elemSize)
            {
                if (IsCanceled) return;

                int left = data.Length - offset;
                if (left >= 4)                {

// common scenarios
                    if (expectedElement != null && expectedElement.size == 4)
                        continue;

                    ushort lhs = TypeConverter.h2s(data[offset], data[offset + 1]);
                    ushort uhs = TypeConverter.h2s(data[offset + 2], data[offset + 3]);
                    uint ui4 = TypeConverter.h2i(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                    if (ui4 == 0)
                    {
                        if (expectedElement == null)
                        {
                            TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                            elem.name = "Unknown";
                            elem.type = "int";
                            elem.size = 4;
                            sr.Elements.Add(elem);
                            UpdateSize(sr);
                            continue;
                        }
                    }
                    else
                    {
                        float flt = TypeConverter.h2f(data[offset], data[offset + 1], data[offset + 2], data[offset + 3]);
                        if (IsLikelyFloat(flt))                        {

// replace element which is int with float
                            if (expectedElement == null)
                            {
                                TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                elem.name = "Unknown";
                                elem.type = "float";
                                elem.size = 4;
                                sr.Elements.Add(elem);
                            }
                            else
                            {
                                if (expectedElement.type == "int")
                                    expectedElement.type = "float";
                            }
                            continue;
                        }
                        else
                        {
                            Record r = this.FormLookup(ui4);
                            if (r != null)
                            {
                                string reftype = r.DescriptiveName.Substring(0, 4); // ???
                                if (expectedElement == null)
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name = "ID";
                                    elem.type = "formid";
                                    elem.reftype = reftype;
                                    elem.size = 4;
                                    sr.Elements.Add(elem);
                                }
                                else
                                {
                                    if (expectedElement.type == "formid")
                                    {
                                        if (expectedElement.reftype != reftype)
                                            expectedElement.reftype = string.Empty;
                                    }
                                }
                                continue;
                            }
                            else
                            {
                                string s = this.StringLookup(ui4);
                                if (!string.IsNullOrEmpty(s))
                                {
                                    if (expectedElement == null)
                                    {
                                        TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                        elem.name = "Unknown";
                                        elem.type = "string";
                                        sr.Elements.Add(elem);
                                    }
                                    continue;
                                }
                            }
                            if (expectedElement == null)
                            {
                                if (lhs > 0 && lhs < 255 && uhs > 0 && uhs < 255)
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name = "Unknown";
                                    elem.type = "short";
                                    elem.size = 2;
                                    sr.Elements.Add(elem);
                                    sr.Elements.Add(elem);
                                }
                                else
                                {
                                    TESVSnip.Data.SubrecordElement elem = new TESVSnip.Data.SubrecordElement();
                                    elem.name = "Unknown";
                                    elem.type = "int";
                                    elem.size = 4;
                                    sr.Elements.Add(elem);
                                    UpdateSize(sr);
                                }
                            }
                        }
                    }
                }
            }


            if (sr.Elements.Count == 0)
            {
                sr.Elements.Add(CreateBlob());
            }
            UpdateSize(sr);
        }