Ejemplo n.º 1
0
        public bool Insert(NxdObject nxdObject, NxdDirectory directory)
        {
            if (!UpdateDirectoryMetaData(directory))
            {
                return false;
            }

            SeekTo(directory);

            fileStream.Seek(142 + directory.Size - nxdObject.SizeTotal, SeekOrigin.Current);

            var currentPosition = fileStream.Position;
            if (fileStream.Position < fileStream.Length)
            {
                var afterBytes = streamReader.ReadBytes((int)(fileStream.Length - fileStream.Position));

                fileStream.Seek(currentPosition + nxdObject.SizeTotal, SeekOrigin.Begin);

                streamWriter.Write(afterBytes);
            }
            fileStream.Seek(currentPosition, SeekOrigin.Begin);

            NxdConvert.SerializeToStream(fileStream, nxdObject);

            return true;
        }
Ejemplo n.º 2
0
        public MainWindow()
        {
            InitializeComponent();

            currentFile = new NxdFile("test.nxd");
            selectedDirectory = currentFile.RootDirectory;
            //selectedDirectory.Directories.Add(new NxdDirectory() { Name = "Images" });

            dgContents.ItemsSource = selectedDirectory.Contents;           
            
            twHierarchy.ItemsSource = new NxdDirectory[] { currentFile.RootDirectory };                      
        }
Ejemplo n.º 3
0
        public static NxdObject Deserialize(byte[] bytes)
        {
            Stream stream = new MemoryStream(bytes);
            BinaryReader reader = new BinaryReader(stream);
            
            
            var id = reader.ReadInt32();            
            var name = Encoding.Default.GetString(reader.ReadBytes(128).ToArray()).TrimEnd('\0');
            var type = reader.ReadInt16();
            var date = reader.ReadInt32();
            var size = reader.ReadInt32();

            NxdObject nxdObject = null;
            switch (type)
            {
                case NxdTypes.Directory:
                    NxdDirectory nxdDirectory = new NxdDirectory();

                    var pos = stream.Position;
                    while (stream.Position - pos < size)
                    {
                        stream.Seek(138, SeekOrigin.Current);
                        var objSize = reader.ReadInt32();
                        stream.Seek(-142, SeekOrigin.Current);

                        NxdObject obj = NxdConvert.Deserialize(reader.ReadBytes(objSize + 142));

                        nxdDirectory.Contents.Add(obj);
                    }

                    nxdObject = nxdDirectory;                    
                    break;
                default:
                    nxdObject = new NxdObject();
                    break;
            }

            nxdObject.Id = id;
            nxdObject.Name = name;
            nxdObject.Type = type;
            nxdObject.Date = date;
            nxdObject.Size = size;

            NxdObject.NextId = id + 1;

            return nxdObject;
        }
Ejemplo n.º 4
0
        public NxdFile(string filename)
        {
            NxdObject.NextId = 0;

            Filename = filename;

            fileStream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            streamReader = new BinaryReader(fileStream);
            streamWriter = new BinaryWriter(fileStream);

            if (fileStream.Length == 0)
            {
                // NXD File Signature
                streamWriter.Write((byte) 0x5E);
                // Version number            
                streamWriter.Write(DefaultVersion);

                RootDirectory = new NxdDirectory() {Name = "Root"};

                NxdConvert.SerializeToStream(fileStream, RootDirectory);
            }
            else
            {
                if (streamReader.ReadByte() != 0x5E)
                {
                    throw new InvalidNxdFileException();
                }

                /*
                    Get file version
                */
                Version = streamReader.ReadInt16();

                RootDirectory =
                    (NxdDirectory)
                        NxdConvert.Deserialize(streamReader.ReadBytes((int) (fileStream.Length - fileStream.Position)));
            }
        }
Ejemplo n.º 5
0
 private void TwHierarchy_OnSelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
 {
     if (twHierarchy.SelectedItem != null)
     {
         selectedDirectory = (NxdDirectory) twHierarchy.SelectedItem;
         RefreshDataGrid();
     }
 }
Ejemplo n.º 6
0
        public bool Remove(NxdObject nxdObject, NxdDirectory directory)
        {
            if (!UpdateDirectoryMetaData(directory))
            {
                return false;
            }

            if (!SeekTo(nxdObject))
            {
                return false;
            }

            var lastPosition = fileStream.Position;

            fileStream.Seek(138, SeekOrigin.Current);
            fileStream.Seek(streamReader.ReadInt32(), SeekOrigin.Current);

            var afterBytes = streamReader.ReadBytes((int) (fileStream.Length - fileStream.Position));

            fileStream.Seek(lastPosition, SeekOrigin.Begin);
            streamWriter.Write(afterBytes);
            fileStream.SetLength(fileStream.Position);

            return true;
        }
Ejemplo n.º 7
0
        private bool UpdateDirectoryMetaData(NxdDirectory directory)
        {
            if (!SeekTo(directory))
            {
                return false;
            }

            NxdConvert.SerializeToStream(fileStream, directory);

            return true;
        }