Example #1
0
        /// <summary>
        /// Writes a the bytecode of a Ndf file into outStream.
        /// </summary>
        /// <param name="outStream">The Stream in wich the data has to be written in.</param>
        /// <param name="ndf">The ndf file which has to be compiled.</param>
        /// <param name="compressed">Sets wether the bytecode has to be compressed or not.</param>
        public void Write(Stream outStream, NdfBinary ndf, bool compressed)
        {
            uint compressedFlag = compressed ? 128 : 0u;

            outStream.Write(BitConverter.GetBytes(EugenMagic), 0, 4);
            outStream.Write(BitConverter.GetBytes((uint)0), 0, 4);
            outStream.Write(BitConverter.GetBytes(NdfBinMagic), 0, 4);
            outStream.Write(BitConverter.GetBytes(compressedFlag), 0, 4);

            var data = GetCompiledContent(ndf);

            outStream.Write(BitConverter.GetBytes(ndf.Footer.Offset), 0, 8);
            outStream.Write(BitConverter.GetBytes(NdfbinHeaderSize), 0, 8);
            outStream.Write(BitConverter.GetBytes(NdfbinHeaderSize + (ulong)data.Length), 0, 8);

            if (compressed)
            {
                outStream.Write(BitConverter.GetBytes(data.Length), 0, 4);
                //Compressor.Comp(data, outStream);

                var da = Compressor.Comp(data);

                outStream.Write(da,0,da.Length);
            }
            else
                outStream.Write(data, 0, data.Length);
        }
        public NdfEditorMainViewModel(NdfBinary ndf)
        {
            NdfBinary = ndf;

            InitializeNdfEditor();

            SaveNdfbinCommand = new ActionCommand(SaveNdfbinExecute, () => false);
        }
Example #3
0
        public byte[] Write(NdfBinary ndf, bool compressed)
        {
            using (var ms = new MemoryStream())
            {
                Write(ms, ndf, compressed);

                return ms.ToArray();
            }
        }
        public AddCollectionItemViewModel(NdfBinary mgr, Window view)
        {
            Manager = mgr;
            View = view;

            OkCommand = new ActionCommand(OkCommandExecute, () => Type != NdfType.Unset);
            CancelCommand = new ActionCommand(CancelCommandExecute);

            _typeSelection.AddRange(NdfTypeManager.GetTypeSelection());
        }
Example #5
0
        protected byte[] GetCompiledContent(NdfBinary ndf)
        {
            var footer = new NdfFooter();

            const long headerSize = (long)NdfbinHeaderSize;

            using (var contentStream = new MemoryStream())
            {
                byte[] buffer = RecompileObj(ndf);
                footer.AddEntry("OBJE", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileTopo(ndf);
                footer.AddEntry("TOPO", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileChnk(ndf);
                footer.AddEntry("CHNK", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileClas(ndf);
                footer.AddEntry("CLAS", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileProp(ndf);
                footer.AddEntry("PROP", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileStrTable(ndf.Strings);
                footer.AddEntry("STRG", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileStrTable(ndf.Trans);
                footer.AddEntry("TRAN", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileUIntList(ndf.Import);
                footer.AddEntry("IMPR", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = RecompileUIntList(ndf.Export);
                footer.AddEntry("EXPR", contentStream.Position + headerSize, buffer.Length);
                contentStream.Write(buffer, 0, buffer.Length);

                buffer = footer.GetBytes();

                footer.Offset = (ulong)contentStream.Position + NdfbinHeaderSize;
                contentStream.Write(buffer, 0, buffer.Length);

                ndf.Footer = footer;

                return contentStream.ToArray();
            }
        }
Example #6
0
        public NdfBinary Read(byte[] data)
        {
            var ndf = new NdfBinary();

            using (var ms = new MemoryStream(data))
            {
                ndf.Header = ReadHeader(ms);

                if (ndf.Header.IsCompressedBody)
                {
                    using (var uncompStream = new MemoryStream())
                    {
                        ms.Seek(0, SeekOrigin.Begin);
                        var headBuffer = new byte[ndf.Header.HeaderSize];
                        ms.Read(headBuffer, 0, headBuffer.Length);
                        uncompStream.Write(headBuffer, 0, headBuffer.Length);

                        ms.Seek((long)ndf.Header.HeaderSize, SeekOrigin.Begin);

                        var buffer = new byte[4];
                        ms.Read(buffer, 0, buffer.Length);
                        uint compressedblocklen = BitConverter.ToUInt32(buffer, 0);

                        var contentBuffer = new byte[(ulong)(data.Length) - ndf.Header.HeaderSize];
                        ms.Read(contentBuffer, 0, contentBuffer.Length);

                        var da = Compressor.Decomp(contentBuffer);
                        uncompStream.Write(da, 0, da.Length);

                        data = uncompStream.ToArray();
                    }
                }
            }

            using (var ms = new MemoryStream(data))
            {
                ndf.Footer = ReadFooter(ms, ndf.Header);
                ndf.Classes = ReadClasses(ms, ndf);
                ReadProperties(ms, ndf);

                ndf.Strings = ReadStrings(ms, ndf);
                ndf.Trans = ReadTrans(ms, ndf);

                ndf.TopObjects = new HashSet<uint>(ReadUIntList(ms, ndf, "TOPO"));
                ndf.Import = ReadUIntList(ms, ndf, "IMPR");
                ndf.Export = ReadUIntList(ms, ndf, "EXPR");

                ndf.Instances = ReadObjects(ms, ndf);
            }

            return ndf;
        }
        public ListEditorViewModel(NdfCollection collection, NdfBinary mgr)
        {
            if (collection == null)
                throw new ArgumentNullException("collection");
            if (mgr == null)
                throw new ArgumentNullException("mgr");

            _collection = collection;
            _ndfbinManager = mgr;
            DetailsCommand = new ActionCommand(DetailsCommandExecute);

            AddRowCommand = new ActionCommand(AddRowExecute);
            AddRowOfCommonTypeCommand = new ActionCommand(AddRowOfCommonTypeExecute, AddRowOfCommonTypeCanExecute);
            DeleteRowCommand = new ActionCommand(DeleteRowExecute, DeleteRowCanExecute);
        }
        public byte[] CreateNdfScript(NdfBinary ndf)
        {
            using (var ms = new MemoryStream())
            {
                byte[] buffer = NdfTextEncoding.GetBytes(string.Format("// Handwritten by enohka \n// For real\n\n\n"));

                ms.Write(buffer, 0, buffer.Length);

                foreach (NdfObject instance in ndf.Instances.Where(x => x.IsTopObject))
                {
                    buffer = instance.GetNdfText();
                    ms.Write(buffer, 0, buffer.Length);
                }

                return ms.ToArray();
            }
        }
Example #9
0
        protected byte[] RecompileChnk(NdfBinary ndf)
        {
            var chnk = new List<byte>();

            chnk.AddRange(BitConverter.GetBytes((uint)0));
            chnk.AddRange(BitConverter.GetBytes(ndf.Instances.Count));

            return chnk.ToArray();
        }
Example #10
0
 public NdfClass(NdfBinary mgr, uint id)
 {
     Manager = mgr;
     Id = id;
 }
Example #11
0
        /// <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.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;
        }
Example #12
0
        protected byte[] RecompileClas(NdfBinary ndf)
        {
            var clasData = new List<byte>();

            foreach (var clas in ndf.Classes.OrderBy(x => x.Id))
            {
                var nameData = Encoding.GetEncoding("ISO-8859-1").GetBytes(clas.Name);
                clasData.AddRange(BitConverter.GetBytes(nameData.Length));
                clasData.AddRange(nameData);
            }

            return clasData.ToArray();
        }
Example #13
0
        protected byte[] RecompileProp(NdfBinary ndf)
        {
            var propData = new List<byte>();

            var props = new List<NdfProperty>();

            foreach (var clas in ndf.Classes)
                props.AddRange(clas.Properties);

            foreach (var prop in props.OrderBy(x => x.Id))
            {
                var nameData = Encoding.GetEncoding("ISO-8859-1").GetBytes(prop.Name);
                propData.AddRange(BitConverter.GetBytes(nameData.Length));
                propData.AddRange(nameData);
                propData.AddRange(BitConverter.GetBytes(prop.Class.Id));
            }

            return propData.ToArray();
        }
Example #14
0
        /// <summary>
        /// Reads a list of UInt32, this is needed for the topobjects, import and export tables.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <param name="lst"></param>
        /// <returns></returns>
        protected List<uint> ReadUIntList(Stream ms, NdfBinary owner, string lst)
        {
            var uintList = new List<uint>();

            NdfFooterEntry uintEntry = owner.Footer.Entries.Single(x => x.Name == lst);
            ms.Seek(uintEntry.Offset, SeekOrigin.Begin);

            var buffer = new byte[4];
            while (ms.Position < uintEntry.Offset + uintEntry.Size)
            {
                ms.Read(buffer, 0, buffer.Length);
                uintList.Add(BitConverter.ToUInt32(buffer, 0));
            }

            return uintList;
        }
Example #15
0
        /// <summary>
        /// Reads the Properties dictionary and relates each one to its owning class.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        protected void ReadProperties(Stream ms, NdfBinary owner)
        {
            NdfFooterEntry propEntry = owner.Footer.Entries.Single(x => x.Name == "PROP");
            ms.Seek(propEntry.Offset, SeekOrigin.Begin);

            int i = 0;
            var buffer = new byte[4];
            while (ms.Position < propEntry.Offset + propEntry.Size)
            {
                var property = new NdfProperty(i);

                ms.Read(buffer, 0, buffer.Length);
                int strLen = BitConverter.ToInt32(buffer, 0);

                var strBuffer = new byte[strLen];
                ms.Read(strBuffer, 0, strBuffer.Length);

                property.Name = Encoding.GetEncoding("ISO-8859-1").GetString(strBuffer);

                ms.Read(buffer, 0, buffer.Length);

                NdfClass cls = owner.Classes.Single(x => x.Id == BitConverter.ToUInt32(buffer, 0));
                property.Class = cls;

                cls.Properties.Add(property);

                i++;
            }
        }
Example #16
0
 public MapValueHolder(NdfValueWrapper value, NdfBinary manager)
     : base(value, manager)
 {
 }
Example #17
0
 public void Write(Stream outStrea, NdfBinary ndf, bool compressed)
 {
     throw new NotImplementedException();
 }
Example #18
0
        protected byte[] RecompileObj(NdfBinary ndf)
        {
            var objectPart = new List<byte>();

            byte[] objSep = { 0xAB, 0xAB, 0xAB, 0xAB };

            foreach (NdfObject instance in ndf.Instances)
            {
                objectPart.AddRange(BitConverter.GetBytes(instance.Class.Id));

                foreach (NdfPropertyValue propertyValue in instance.PropertyValues)
                {
                    if (propertyValue.Type == NdfType.Unset)
                        continue;

                    byte[] valueBytes = propertyValue.Value.GetBytes();

                    if (propertyValue.Value.Type == NdfType.Unset)
                        continue;

                    objectPart.AddRange(BitConverter.GetBytes(propertyValue.Property.Id));

                    if (propertyValue.Value.Type == NdfType.ObjectReference ||
                        propertyValue.Value.Type == NdfType.TransTableReference)
                        objectPart.AddRange(BitConverter.GetBytes((uint)NdfType.Reference));

                    objectPart.AddRange(BitConverter.GetBytes((uint)propertyValue.Value.Type));
                    objectPart.AddRange(valueBytes);
                }

                objectPart.AddRange(objSep);
            }

            return objectPart.ToArray();
        }
Example #19
0
        /// <summary>
        /// Reads the Classes dictionary.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected ObservableCollection<NdfClass> ReadClasses(Stream ms, NdfBinary owner)
        {
            var classes = new ObservableCollection<NdfClass>();

            NdfFooterEntry classEntry = owner.Footer.Entries.Single(x => x.Name == "CLAS");

            ms.Seek(classEntry.Offset, SeekOrigin.Begin);

            uint i = 0;
            var buffer = new byte[4];

            while (ms.Position < classEntry.Offset + classEntry.Size)
            {
                var nclass = new NdfClass(owner, i);

                ms.Read(buffer, 0, buffer.Length);
                int strLen = BitConverter.ToInt32(buffer, 0);

                var strBuffer = new byte[strLen];
                ms.Read(strBuffer, 0, strBuffer.Length);

                nclass.Name = Encoding.GetEncoding("ISO-8859-1").GetString(strBuffer);

                i++;
                classes.Add(nclass);
            }

            return classes;
        }
Example #20
0
        /// <summary>
        /// Reads the amount of instances this file contains.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected uint ReadChunk(Stream ms, NdfBinary owner)
        {
            NdfFooterEntry chnk = owner.Footer.Entries.Single(x => x.Name == "CHNK");
            ms.Seek(chnk.Offset, SeekOrigin.Begin);

            var buffer = new byte[4];

            ms.Read(buffer, 0, buffer.Length);
            ms.Read(buffer, 0, buffer.Length);

            return BitConverter.ToUInt32(buffer, 0);
        }
Example #21
0
 public NdfClass(NdfBinary mgr, uint id)
 {
     Manager = mgr;
     Id      = id;
 }
Example #22
0
 public CollectionItemValueHolder(NdfValueWrapper value, NdfBinary manager)
 {
     Value   = value;
     Manager = manager;
 }
Example #23
0
        /// <summary>
        /// Reads the string list.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected ObservableCollection<NdfStringReference> ReadStrings(Stream ms, NdfBinary owner)
        {
            var strings = new ObservableCollection<NdfStringReference>();

            NdfFooterEntry stringEntry = owner.Footer.Entries.Single(x => x.Name == "STRG");
            ms.Seek(stringEntry.Offset, SeekOrigin.Begin);

            int i = 0;
            var buffer = new byte[4];
            while (ms.Position < stringEntry.Offset + stringEntry.Size)
            {
                var nstring = new NdfStringReference { Id = i };

                ms.Read(buffer, 0, buffer.Length);
                int strLen = BitConverter.ToInt32(buffer, 0);

                var strBuffer = new byte[strLen];
                ms.Read(strBuffer, 0, strBuffer.Length);

                nstring.Value = Encoding.GetEncoding("ISO-8859-1").GetString(strBuffer);

                i++;
                strings.Add(nstring);
            }

            return strings;
        }
 public MapValueHolder(NdfValueWrapper value, NdfBinary manager)
     : base(value, manager)
 {
 }
Example #25
0
        /// <summary>
        /// Reads the trans list
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected ObservableCollection<NdfTranReference> ReadTrans(Stream ms, NdfBinary owner)
        {
            var trans = new ObservableCollection<NdfTranReference>();

            NdfFooterEntry stringEntry = owner.Footer.Entries.Single(x => x.Name == "TRAN");
            ms.Seek(stringEntry.Offset, SeekOrigin.Begin);

            int i = 0;
            var buffer = new byte[4];
            while (ms.Position < stringEntry.Offset + stringEntry.Size)
            {
                var ntran = new NdfTranReference { Id = i };

                ms.Read(buffer, 0, buffer.Length);
                int strLen = BitConverter.ToInt32(buffer, 0);

                var strBuffer = new byte[strLen];
                ms.Read(strBuffer, 0, strBuffer.Length);

                ntran.Value = Encoding.GetEncoding("ISO-8859-1").GetString(strBuffer);

                i++;
                trans.Add(ntran);
            }

            // TODO: Trans is actually more a tree than a list, this is still not fully implemented/reversed.

            return trans;
        }
Example #26
0
        /// <summary>
        /// Reads one object instance.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="index"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected NdfObject ReadObject(Stream ms, uint index, NdfBinary owner)
        {
            var instance = new NdfObject { Id = index };

            if (owner.TopObjects.Contains(index))
                instance.IsTopObject = true;

            var buffer = new byte[4];
            ms.Read(buffer, 0, buffer.Length);
            int classId = BitConverter.ToInt32(buffer, 0);

            if (owner.Classes.Count < classId)
                throw new InvalidDataException("Object without class found.");

            NdfClass cls = instance.Class = owner.Classes[classId];

            cls.Instances.Add(instance);

            // Read properties
            for (; ; )
            {
                ms.Read(buffer, 0, buffer.Length);
                uint propertyId = BitConverter.ToUInt32(buffer, 0);

                if (propertyId == 0xABABABAB)
                    break;

                var propVal = new NdfPropertyValue(instance)
                    {
                        Property = cls.Properties.SingleOrDefault(x => x.Id == propertyId)
                    };

                if (propVal.Property == null)
                    // throw new InvalidDataException("Found a value for a property which doens't exist in this class.");
                    foreach (var c in owner.Classes)
                        foreach (var p in c.Properties)
                            if (p.Id == propertyId)
                            {
                                propVal.Property = p;
                                break;
                            }

                instance.PropertyValues.Add(propVal);

                NdfValueWrapper res = ReadValue(ms, owner);
                propVal.Value = res;
            }

            owner.AddEmptyProperties(instance);

            return instance;
        }
Example #27
0
        protected byte[] RecompileTopo(NdfBinary ndf)
        {
            using (var ms = new MemoryStream())
            {
                List<NdfObject> topInsts = ndf.Instances.Where(x => x.IsTopObject).ToList();

                //var writeInsts = new HashSet<NdfObject>();

                //foreach (NdfObject inst in topInsts)
                //{
                //    if (writeInsts.Contains(inst))
                //        continue;

                //    writeInsts.Add(inst);

                //    int nextItemId = topInsts.IndexOf(inst) + 1;

                //    if (topInsts.Count > nextItemId && topInsts[nextItemId].Class != inst.Class)
                //    {
                //        IEnumerable<NdfObject> othersOfSameClass =
                //            topInsts.GetRange(nextItemId, topInsts.Count - nextItemId).Where(
                //                x => x.Class == inst.Class && !writeInsts.Contains(x));

                //        foreach (NdfObject o in othersOfSameClass)
                //            writeInsts.Add(o);
                //    }
                //}

                var test = topInsts.OrderBy(x => x.Class.Id);

                foreach (NdfObject instance in test)
                {
                    byte[] buffer = BitConverter.GetBytes(instance.Id);
                    ms.Write(buffer, 0, buffer.Length);
                }

                return ms.ToArray();
            }
        }
Example #28
0
        /// <summary>
        /// Reads the object instances.
        /// </summary>
        /// <param name="ms"></param>
        /// <param name="owner"></param>
        /// <returns></returns>
        protected List<NdfObject> ReadObjects(Stream ms, NdfBinary owner)
        {
            var objects = new List<NdfObject>();

            uint instanceCount = ReadChunk(ms, owner);

            NdfFooterEntry objEntry = owner.Footer.Entries.Single(x => x.Name == "OBJE");
            ms.Seek(objEntry.Offset, SeekOrigin.Begin);

            for (uint i = 0; i < instanceCount; i++)
            {
                long objOffset = ms.Position;

                NdfObject obj = ReadObject(ms, i, owner);
                obj.Offset = objOffset;

                objects.Add(obj);
            }

            return objects;
        }
 public CollectionItemValueHolder(NdfValueWrapper value, NdfBinary manager)
 {
     Value = value;
     Manager = manager;
 }