Example #1
0
        public static uint SizeofType(NdfType type)
        {
            switch (type)
            {
            case NdfType.Boolean:
            case NdfType.Int8:
                return(1);

            case NdfType.Int16:
            case NdfType.UInt16:
                return(2);

            case NdfType.Int32:
            case NdfType.UInt32:
            case NdfType.Float32:
            case NdfType.TableStringFile:
            case NdfType.TableString:
            case NdfType.Color32:
            case NdfType.WideString:
                return(4);

            case NdfType.Float64:
            case NdfType.LocalisationHash:
            case NdfType.ObjectReference:
            case NdfType.EugInt2:
            case NdfType.EugFloat2:
            case NdfType.Time64:
                return(8);

            case NdfType.TrippleInt:
            case NdfType.Vector:
                return(12);

            case NdfType.Color128:
            case NdfType.Guid:
            case NdfType.Hash:
                return(16);

            case NdfType.Map:
                return(0);

            case NdfType.List:
            case NdfType.MapList:
            case NdfType.TransTableReference:
                return(4);

            case NdfType.Bob:
                return(8);

            default:
                return(0);
            }
        }
Example #2
0
        private void AddRowOfCommonTypeExecute(object obj)
        {
            var cv = CollectionViewSource.GetDefaultView(Value);

            if (cv == null)
            {
                return;
            }

            NdfType type =
                Value.GroupBy(x => x.Value.Type).OrderByDescending(gp => gp.Count()).Select(x => x.First().Value.Type).
                Single();

            var wrapper =
                new CollectionItemValueHolder(NdfTypeManager.GetValue(new byte[NdfTypeManager.SizeofType(type)], type, NdfbinManager), NdfbinManager);

            if (IsInsertMode)
            {
                if (cv.CurrentItem == null)
                {
                    return;
                }

                var val = cv.CurrentItem as CollectionItemValueHolder;

                if (val == null)
                {
                    return;
                }

                Value.Insert(cv.CurrentPosition + 1, wrapper);
            }
            else
            {
                Value.Add(wrapper);
            }

            cv.MoveCurrentTo(wrapper);
        }
 protected NdfValueWrapper(NdfType type)
 {
     Type = type;
 }
Example #4
0
        public static NdfValueWrapper GetValue(byte[] data, NdfType type, NdfBinary mgr)
        {
            //if (data.Length != SizeofType(type))
            //    return null;

            switch (type)
            {
            case NdfType.Boolean:
                return(new NdfBoolean(BitConverter.ToBoolean(data, 0)));

            case NdfType.Int8:
                return(new NdfInt8(data[0]));

            case NdfType.Int16:
                return(new NdfInt16(BitConverter.ToInt16(data, 0)));

            case NdfType.UInt16:
                return(new NdfUInt16(BitConverter.ToUInt16(data, 0)));

            case NdfType.Int32:
                return(new NdfInt32(BitConverter.ToInt32(data, 0)));

            case NdfType.UInt32:
                return(new NdfUInt32(BitConverter.ToUInt32(data, 0)));

            case NdfType.Float32:
                return(new NdfSingle(BitConverter.ToSingle(data, 0)));

            case NdfType.Float64:
                return(new NdfDouble(BitConverter.ToDouble(data, 0)));

            case NdfType.TableStringFile:
                int id = BitConverter.ToInt32(data, 0);
                return(new NdfFileNameString(mgr.Strings[id]));

            case NdfType.TableString:
                int id2 = BitConverter.ToInt32(data, 0);
                return(new NdfString(mgr.Strings[id2]));

            case NdfType.Color32:
                return(new NdfColor32(Color.FromArgb(data[3], data[0], data[1], data[2])));

            case NdfType.Color128:
                return(new NdfColor128(data));

            case NdfType.Vector:
                byte[] px = data.Take(4).ToArray();
                byte[] py = data.Skip(4).Take(4).ToArray();
                byte[] pz = data.Skip(8).ToArray();
                return(new NdfVector(new Point3D(BitConverter.ToSingle(px, 0),
                                                 BitConverter.ToSingle(py, 0),
                                                 BitConverter.ToSingle(pz, 0))));

            case NdfType.ObjectReference:
                uint     instId = BitConverter.ToUInt32(data.Take(4).ToArray(), 0);
                uint     clsId  = BitConverter.ToUInt32(data.Skip(4).ToArray(), 0);
                NdfClass cls    = null;

                // possible deadrefs here
                if (clsId <= mgr.Classes.Count)
                {
                    cls = mgr.Classes[(int)clsId];
                }

                return(new NdfObjectReference(cls, instId, cls == null));

            case NdfType.Map:
                return(new NdfMap(new MapValueHolder(null, mgr), new MapValueHolder(null, mgr), mgr));

            case NdfType.Guid:
                return(new NdfGuid(new Guid(data)));

            case NdfType.WideString:
                return(new NdfWideString(Encoding.Unicode.GetString(data)));

            case NdfType.TransTableReference:
                int id3 = BitConverter.ToInt32(data, 0);
                return(new NdfTrans(mgr.Trans[id3]));

            case NdfType.LocalisationHash:
                return(new NdfLocalisationHash(data));

            case NdfType.List:
                return(new NdfCollection());

            case NdfType.MapList:
                return(new NdfMapList());

            case NdfType.Blob:
                return(new NdfBlob(data));

            case NdfType.ZipBlob:
                return(new NdfZipBlob(data));

            case NdfType.EugInt2:
                return(new NdfEugInt2(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4)));

            case NdfType.EugFloat2:
                return(new NdfEugFloat2(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4)));

            case NdfType.TrippleInt:
                return(new NdfTrippleInt(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4), BitConverter.ToInt32(data, 8)));

            case NdfType.Hash:
                return(new NdfHash(data));

            case NdfType.Time64:
                return(new NdfTime64(new DateTime(1970, 1, 1).AddSeconds(BitConverter.ToUInt32(data, 0))));

            case NdfType.Unset:
                return(new NdfNull());

            case NdfType.Bob:
                return(new NdfTime64(new DateTime(1970, 1, 1).AddSeconds(BitConverter.ToUInt32(data, 0))));

            default:
                return(null);
            }
        }
Example #5
0
 protected NdfValueWrapper(NdfType type)
 {
     Type = type;
 }
        public static NdfValueWrapper GetValue(byte[] data, NdfType type, NdfbinManager mgr, long pos)
        {
            //if (data.Length != SizeofType(type))
            //    return null;

            switch (type)
            {
            case NdfType.Boolean:
                return(new NdfBoolean(BitConverter.ToBoolean(data, 0), pos));

            case NdfType.Int8:
                return(new NdfInt8(data[0], pos));

            case NdfType.Int16:
                return(new NdfInt16(BitConverter.ToInt16(data, 0), pos));

            case NdfType.Int32:
                return(new NdfInt32(BitConverter.ToInt32(data, 0), pos));

            case NdfType.UInt32:
                return(new NdfUInt32(BitConverter.ToUInt32(data, 0), pos));

            case NdfType.Float32:
                return(new NdfSingle(BitConverter.ToSingle(data, 0), pos));

            case NdfType.Float64:
                return(new NdfDouble(BitConverter.ToDouble(data, 0), pos));

            case NdfType.Float64_2:
                return(new NdfDouble_2(BitConverter.ToDouble(data, 0), pos));

            case NdfType.TableStringFile:
                var id = BitConverter.ToInt32(data, 0);
                return(new NdfFileNameString(mgr.Strings[id], pos));

            case NdfType.TableString:
                var id2 = BitConverter.ToInt32(data, 0);
                return(new NdfString(mgr.Strings[id2], pos));

            case NdfType.Color32:
                return(new NdfColor32(Color.FromArgb(data[0], data[1], data[2], data[3]), pos));

            case NdfType.Color128:
                return(new NdfColor128(data, pos));

            case NdfType.Vector:
                var px = data.Take(4).ToArray();
                var py = data.Skip(4).Take(4).ToArray();
                var pz = data.Skip(8).ToArray();
                return(new NdfVector(new Point3D(BitConverter.ToSingle(px, 0),
                                                 BitConverter.ToSingle(py, 0),
                                                 BitConverter.ToSingle(pz, 0)), pos));

            case NdfType.ObjectReference:
                var instId = BitConverter.ToUInt32(data.Take(4).ToArray(), 0);
                var clsId  = BitConverter.ToUInt32(data.Skip(4).ToArray(), 0);
                var cls    = mgr.Classes.SingleOrDefault(x => x.Id == clsId);  // mgr.Classes[(int)clsId]; due to deadrefs...
                return(new NdfObjectReference(cls, instId, pos, cls == null));

            case NdfType.Map:
                return(new NdfMap(new MapValueHolder(null, mgr, 0), new MapValueHolder(null, mgr, 0), pos));

            case NdfType.Guid:
                return(new NdfGuid(new Guid(data), pos));

            case NdfType.WideString:
                return(new NdfWideString(Encoding.Unicode.GetString(data), 0));

            case NdfType.TransTableReference:
                var id3 = BitConverter.ToInt32(data, 0);
                return(new NdfTrans(mgr.Trans[id3], pos));

            case NdfType.LocalisationHash:
                return(new NdfLocalisationHash(data, 0));

            case NdfType.Unset:
                return(new NdfNull(pos));

            default:
                return(null);
            }
        }
Example #7
0
 protected NdfFlatValueWrapper(NdfType type, object value, long offset)
     : base(type, offset)
 {
     Value = value;
 }
        /// <summary>
        /// Reads the value of a Property inside a object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="binary"></param>
        /// <returns>A NdfValueWrapper Instance.</returns>
        protected NdfValueWrapper ReadValue(Stream ms, NdfBinary binary)
        {
            uint            contBufferlen;
            NdfValueWrapper value;
            var             buffer = new byte[4];

            ms.Read(buffer, 0, buffer.Length);
            NdfType type = NdfTypeManager.GetType(buffer);

            if (type == NdfType.Unknown)
            {
                throw new InvalidDataException("Unknown datatypes are not supported!");
            }



            if (type == NdfType.Reference)
            {
                ms.Read(buffer, 0, buffer.Length);
                type = NdfTypeManager.GetType(buffer);
            }

            switch (type)
            {
            case NdfType.Unknown:
            case NdfType.WideString:
            case NdfType.List:
            case NdfType.MapList:
            case NdfType.Blob:
            case NdfType.ZipBlob:
                ms.Read(buffer, 0, buffer.Length);
                contBufferlen = BitConverter.ToUInt32(buffer, 0);

                if (type == NdfType.ZipBlob)
                {
                    if (ms.ReadByte() != 1)
                    {
                        throw new InvalidDataException("has to be checked.");
                    }
                }
                break;

            default:
                contBufferlen = NdfTypeManager.SizeofType(type);
                break;
            }

            switch (type)
            {
            case NdfType.MapList:
            case NdfType.List:
                NdfCollection lstValue = type == NdfType.List ? new NdfCollection() : new NdfMapList();

                for (int i = 0; i < contBufferlen; i++)
                {
                    CollectionItemValueHolder res;
                    if (type == NdfType.List)
                    {
                        res = new CollectionItemValueHolder(ReadValue(ms, binary), binary);
                    }
                    else
                    {
                        res = new CollectionItemValueHolder(
                            new NdfMap(
                                new MapValueHolder(ReadValue(ms, binary), binary),
                                new MapValueHolder(ReadValue(ms, binary), binary),
                                binary), binary);
                    }

                    lstValue.Add(res);
                }

                value = lstValue;
                break;

            case NdfType.Map:
                value = new NdfMap(
                    new MapValueHolder(ReadValue(ms, binary), binary),
                    new MapValueHolder(ReadValue(ms, binary), binary),
                    binary);
                break;

            default:
                var contBuffer = new byte[contBufferlen];
                ms.Read(contBuffer, 0, contBuffer.Length);

                value = NdfTypeManager.GetValue(contBuffer, type, binary);
                break;
            }

            return(value);
        }
 protected NdfFlatValueWrapper(NdfType type, object value)
     : base(type)
 {
     Value = value;
 }
 protected NdfFlatValueWrapper(NdfType type, object value)
     : base(type)
 {
     Value = value;
 }
Example #11
0
 protected NdfValueWrapper(NdfType type, long offset)
 {
     Type   = type;
     OffSet = offset;
 }
        public static NdfValueWrapper GetValue(byte[] data, NdfType type, NdfBinary mgr)
        {
            //if (data.Length != SizeofType(type))
            //    return null;

            switch (type)
            {
                case NdfType.Boolean:
                    return new NdfBoolean(BitConverter.ToBoolean(data, 0));
                case NdfType.Int8:
                    return new NdfInt8(data[0]);
                case NdfType.Int16:
                    return new NdfInt16(BitConverter.ToInt16(data, 0));
                case NdfType.UInt16:
                    return new NdfUInt16(BitConverter.ToUInt16(data, 0));
                case NdfType.Int32:
                    return new NdfInt32(BitConverter.ToInt32(data, 0));
                case NdfType.UInt32:
                    return new NdfUInt32(BitConverter.ToUInt32(data, 0));
                case NdfType.Float32:
                    return new NdfSingle(BitConverter.ToSingle(data, 0));
                case NdfType.Float64:
                    return new NdfDouble(BitConverter.ToDouble(data, 0));

                case NdfType.TableStringFile:
                    int id = BitConverter.ToInt32(data, 0);
                    return new NdfFileNameString(mgr.Strings[id]);
                case NdfType.TableString:
                    int id2 = BitConverter.ToInt32(data, 0);
                    return new NdfString(mgr.Strings[id2]);
                case NdfType.Color32:
                    return new NdfColor32(Color.FromArgb(data[3], data[0], data[1], data[2]));
                case NdfType.Color128:
                    return new NdfColor128(data);
                case NdfType.Vector:
                    byte[] px = data.Take(4).ToArray();
                    byte[] py = data.Skip(4).Take(4).ToArray();
                    byte[] pz = data.Skip(8).ToArray();
                    return new NdfVector(new Point3D(BitConverter.ToSingle(px, 0),
                                                     BitConverter.ToSingle(py, 0),
                                                     BitConverter.ToSingle(pz, 0)));

                case NdfType.ObjectReference:
                    uint instId = BitConverter.ToUInt32(data.Take(4).ToArray(), 0);
                    uint clsId = BitConverter.ToUInt32(data.Skip(4).ToArray(), 0);
                    NdfClass cls = null;

                    // possible deadrefs here
                    if (clsId <= mgr.Classes.Count)
                        cls = mgr.Classes[(int)clsId];

                    return new NdfObjectReference(cls, instId, cls == null);

                case NdfType.Map:
                    return new NdfMap(new MapValueHolder(null, mgr), new MapValueHolder(null, mgr), mgr);

                case NdfType.Guid:
                    return new NdfGuid(new Guid(data));

                case NdfType.WideString:
                    return new NdfWideString(Encoding.Unicode.GetString(data));

                case NdfType.TransTableReference:
                    int id3 = BitConverter.ToInt32(data, 0);
                    return new NdfTrans(mgr.Trans[id3]);

                case NdfType.LocalisationHash:
                    return new NdfLocalisationHash(data);

                case NdfType.List:
                    return new NdfCollection();
                case NdfType.MapList:
                    return new NdfMapList();

                case NdfType.Blob:
                    return new NdfBlob(data);

                case NdfType.ZipBlob:
                    return new NdfZipBlob(data);

                case NdfType.EugInt2:
                    return new NdfEugInt2(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4));
                case NdfType.EugFloat2:
                    return new NdfEugFloat2(BitConverter.ToSingle(data, 0), BitConverter.ToSingle(data, 4));

                case NdfType.TrippleInt:
                    return new NdfTrippleInt(BitConverter.ToInt32(data, 0), BitConverter.ToInt32(data, 4), BitConverter.ToInt32(data, 8));

                case NdfType.Hash:
                    return new NdfHash(data);

                case NdfType.Time64:
                    return new NdfTime64(new DateTime(1970, 1, 1).AddSeconds(BitConverter.ToUInt32(data, 0)));

                case NdfType.Unset:
                    return new NdfNull();

                default:
                    return null;
            }
        }
        public static uint SizeofType(NdfType type)
        {
            switch (type)
            {
                case NdfType.Boolean:
                case NdfType.Int8:
                    return 1;
                case NdfType.Int16:
                case NdfType.UInt16:
                    return 2;
                case NdfType.Int32:
                case NdfType.UInt32:
                case NdfType.Float32:
                case NdfType.TableStringFile:
                case NdfType.TableString:
                case NdfType.Color32:
                case NdfType.WideString:
                    return 4;
                case NdfType.Float64:
                case NdfType.LocalisationHash:
                case NdfType.ObjectReference:
                case NdfType.EugInt2:
                case NdfType.EugFloat2:
                case NdfType.Time64:
                    return 8;
                case NdfType.TrippleInt:
                case NdfType.Vector:
                    return 12;
                case NdfType.Color128:
                case NdfType.Guid:
                case NdfType.Hash:
                    return 16;

                case NdfType.Map:
                    return 0;

                case NdfType.List:
                case NdfType.MapList:
                case NdfType.TransTableReference:
                    return 4;

                default:
                    return 0;
            }
        }