Exemple #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;
        }
Exemple #2
0
 public void Remove(NxdObject nxdObject)
 {
     if (nxdObject is NxdDirectory)
     {
         Directories.Remove((NxdDirectory)nxdObject);
     }
     else
     {
         Contents.Remove(nxdObject);
     }
 }
Exemple #3
0
 public void Add(NxdObject nxdObject)
 {
     if (nxdObject is NxdDirectory)
     {
         Directories.Add((NxdDirectory) nxdObject);
     }
     else
     {
         Contents.Add(nxdObject);
     }            
 }
Exemple #4
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;
        }
Exemple #5
0
        public static void SerializeToStream(Stream stream, NxdObject nxdObject)
        {
            BinaryWriter writer = new BinaryWriter(stream);

            writer.Write(nxdObject.Id);

            var nameBytes = new byte[128];
            for (var i = 0; i < Math.Min(nxdObject.Name.Length, 128); i++)
            {
                nameBytes[i] = (byte)nxdObject.Name[i];
            }

            writer.Write(nameBytes);

            writer.Write(nxdObject.Type);
            writer.Write(nxdObject.Date);

            writer.Write(nxdObject.Size);

            if (nxdObject.Data != null)
            {
                writer.Write(nxdObject.Data);
            }
        }
Exemple #6
0
        private async void Window_Drop(object sender, DragEventArgs e)
        {
            await Task.Run(() =>
            {
                var filenames = (string[]) e.Data.GetData(DataFormats.FileDrop);

                foreach (var filename in filenames)
                {
                    NxdObject nxdObject = new NxdObject
                    {
                        Name = Path.GetFileName(filename),
                        Data = File.ReadAllBytes(filename)
                    };

                    nxdObject.Size = nxdObject.Data.Length;

                    selectedDirectory.Add(nxdObject);
                    currentFile.Insert(nxdObject, selectedDirectory);
                    nxdObject.Data = null;                    
                }
            });
            
            RefreshDataGrid();        
        }
Exemple #7
0
 public static byte[] Serialize(NxdObject nxdObject)
 {
     throw new NotImplementedException();
 }
Exemple #8
0
        private bool SeekTo(NxdObject nxdObject)
        {
            var lastPosition = fileStream.Position;

            fileStream.Seek(3, SeekOrigin.Begin);

            while (fileStream.Position < fileStream.Length)
            {
                var id = streamReader.ReadInt32();

                if (nxdObject.Id == id)
                {
                    fileStream.Seek(-4, SeekOrigin.Current);
                    return true;
                }

                fileStream.Seek(134, SeekOrigin.Current);
                var size = streamReader.ReadInt32();
                fileStream.Seek(size, SeekOrigin.Current);
            }

            fileStream.Seek(lastPosition, SeekOrigin.Begin);

            return false;
        }
Exemple #9
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;
        }