Пример #1
0
        public bool RemoveEntry(D2pEntry entry)
        {
            if (entry.Container != this)
            {
                if (!entry.Container.RemoveEntry(entry))
                {
                    return(false);
                }

                if (!m_linksToSave.Contains(entry.Container))
                {
                    m_linksToSave.Enqueue(entry.Container);
                }
            }

            if (m_entries.Remove(entry.FullFileName))
            {
                entry.State = D2pEntryState.Removed;
                InternalRemoveDirectories(entry);
                OnPropertyChanged("Entries");

                if (entry.Container == this)
                {
                    IndexTable.EntriesCount--;
                }

                return(true);
            }

            return(false);
        }
Пример #2
0
 public void AddEntry(D2pEntry entry)
 {
     entry.State = D2pEntryState.Added;
     InternalAddEntry(entry);
     IndexTable.EntriesCount++;
     OnPropertyChanged("Entries");
 }
Пример #3
0
        public static D2pEntry CreateEntryDefinition(D2pFile container, IDataReader reader)
        {
            var entry = new D2pEntry(container);

            entry.ReadEntryDefinition(reader);

            return(entry);
        }
Пример #4
0
        public D2pEntry AddFile(string fileName, byte[] data)
        {
            var entry = new D2pEntry(this, fileName, data);

            AddEntry(entry);

            return(entry);
        }
Пример #5
0
        private void ReadEntriesDefinitions()
        {
            m_reader.Seek(IndexTable.EntriesDefinitionOffset, SeekOrigin.Begin);
            for (int i = 0; i < IndexTable.EntriesCount; i++)
            {
                D2pEntry entry = D2pEntry.CreateEntryDefinition(this, m_reader);

                InternalAddEntry(entry);
            }
        }
Пример #6
0
        public byte[] ReadFile(string fileName)
        {
            if (!Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            D2pEntry entry = GetEntry(fileName);

            return(ReadFile(entry));
        }
Пример #7
0
        private void InternalAddDirectories(D2pEntry entry)
        {
            string[] directories = entry.GetDirectoriesName();

            if (directories.Length == 0)
            {
                return;
            }

            D2pDirectory current = null;

            if (!m_rootDirectories.ContainsKey(directories[0]))
            {
                m_rootDirectories.Add(directories[0], current = new D2pDirectory(this, directories[0]));
            }
            else
            {
                current = m_rootDirectories[directories[0]];
            }

            if (directories.Length == 1)
            {
                current.Entries.Add(entry);
            }

            for (int i = 1; i < directories.Length; i++)
            {
                string directory = directories[i];
                var    next      = current.TryGetDirectory(directory);
                if (next == null)
                {
                    var dir = new D2pDirectory(this, directory)
                    {
                        Parent = current
                    };
                    current.Directories.Add(directory, dir);

                    current = dir;
                }
                else
                {
                    current = next;
                }

                if (i == directories.Length - 1)
                {
                    current.Entries.Add(entry);
                }
            }

            entry.Directory = current;
        }
Пример #8
0
        /*
         * public void Rename(D2pEntry entry, string name)
         * {
         *  if (name.Contains("/") || name.Contains("\\"))
         *      throw new Exception("File name cannot contains \\ or /. Use Move method");
         *
         *  var fullname = Path.Combine(Path.GetDirectoryName(entry.FullFileName), name);
         *
         *  if (Exists(fullname))
         *      throw new Exception(string.Format("Cannot rename {0} to {1} because a file named {1} already exists", entry.FullFileName, name));
         *
         *  if (!m_entries.Remove(entry.FullFileName))
         *      throw new Exception(string.Format("File {0} not found", entry.FullFileName));
         *
         *  m_entries.Add(fullname, entry);
         *  entry.FullFileName = fullname;
         *
         *  if (!m_linksToSave.Contains(entry.Container))
         *      m_linksToSave.Enqueue(entry.Container);
         * }
         *
         * public void CheckRename(D2pDirectory directory, string name)
         * {
         *  if (name.Contains("/") || name.Contains("\\"))
         *      throw new Exception("Directory name cannot contains \\ or /. Use Move method");
         *
         *  var fullname = Path.Combine(Path.GetDirectoryName(directory.FullName), name);
         *
         *  if (HasDirectory(fullname))
         *      throw new Exception(string.Format("Cannot rename {0} to {1} because a directory named {1} already exists", directory.FullName, name));
         *
         *  if (directory.Entries.Any(x => Exists(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(x.FullFileName)), name, x.FileName))))
         *  {
         *      throw new Exception(string.Format("Following files already exists : {0}",
         *          string.Join(", ", directory.Entries.Where(x => Exists(Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(x.FullFileName)), name, x.FileName))).
         *                          Select(x => Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(x.FullFileName)), name, x.FileName)))));
         *  }
         * }
         *
         * public void Rename(D2pDirectory directory, string name)
         * {
         *  CheckRename(directory, name);
         *
         *  foreach (var link in m_links)
         *  {
         *      var dir = link.TryGetDirectory(directory.FullName);
         *      if (dir != null)
         *          link.CheckRename(dir, name);
         *  }
         *
         *  InternalRename(directory, name);
         *
         *  foreach (var link in m_links)
         *  {
         *      var dir = link.TryGetDirectory(directory.FullName);
         *      if (dir != null)
         *          link.InternalRename(dir, name);
         *  }
         * }
         *
         * private void InternalRename(D2pDirectory directory, string name)
         * {
         *  if (directory.IsRoot)
         *  {
         *      if (!m_rootDirectories.Remove(directory.Name))
         *          throw new Exception(string.Format("Directory {0} not found", directory.Name));
         *
         *      m_rootDirectories.Add(name, directory);
         *      directory.Name = name;
         *  }
         *  else
         *  {
         *      if (!directory.Parent.Directories.Remove(directory.Name))
         *          throw new Exception(string.Format("Directory {0} not found", directory.FullName));
         *
         *      directory.Parent.Directories.Add(name, directory);
         *      directory.Name = name;
         *  }
         *
         *  foreach (var entry in directory.Entries)
         *  {
         *      entry.FullFileName = Path.Combine(Path.GetDirectoryName(Path.GetDirectoryName(entry.FullFileName)), name, entry.FileName);
         *  }
         *
         * }
         *
         */
        public void ExtractFile(string fileName, bool overwrite = false)
        {
            if (!Exists(fileName))
            {
                throw new FileNotFoundException(fileName);
            }

            D2pEntry entry = GetEntry(fileName);

            string dest = Path.Combine("./", entry.FullFileName);

            if (!Directory.Exists(Path.GetDirectoryName(dest)))
            {
                Directory.CreateDirectory(dest);
            }

            ExtractFile(fileName, dest, overwrite);
        }
Пример #9
0
        private void InternalAddEntry(D2pEntry entry)
        {
            D2pEntry registerdEntry = TryGetEntry(entry.FullFileName);

            // shouldn't be possible but dofus don't care about that
            if (registerdEntry != null)
            {
                logger.Warn("Entry '{0}'({1}) already added and will be override ({2})", registerdEntry.FullFileName,
                            registerdEntry.Container.FileName, FileName);
                m_entries[registerdEntry.FullFileName] = entry;
            }
            else
            {
                m_entries.Add(entry.FullFileName, entry);
            }

            InternalAddDirectories(entry);
        }
Пример #10
0
        public byte[] ReadFile(D2pEntry entry)
        {
            if (entry.Container != this)
            {
                return(entry.Container.ReadFile(entry));
            }

            lock (m_reader)
            {
                if (entry.Index >= 0 && IndexTable.OffsetBase + entry.Index >= 0)
                {
                    m_reader.Seek(IndexTable.OffsetBase + entry.Index, SeekOrigin.Begin);
                }

                byte[] data = entry.ReadEntry(m_reader);

                return(data);
            }
        }
Пример #11
0
        private void InternalRemoveDirectories(D2pEntry entry)
        {
            D2pDirectory current = entry.Directory;

            while (current != null)
            {
                current.Entries.Remove(entry);

                if (current.Parent != null && current.Entries.Count == 0)
                {
                    current.Parent.Directories.Remove(current.Name);
                }
                else if (current.IsRoot && current.Entries.Count == 0)
                {
                    m_rootDirectories.Remove(current.Name);
                }

                current = current.Parent;
            }
        }
Пример #12
0
        public bool ModifyFile(string file, byte[] data)
        {
            D2pEntry entry = TryGetEntry(file);

            if (entry == null)
            {
                return(false);
            }

            entry.ModifyEntry(data);

            lock (m_linksToSave)
            {
                if (entry.Container != this &&
                    !m_linksToSave.Contains(entry.Container))
                {
                    m_linksToSave.Enqueue(entry.Container);
                }
            }

            return(true);
        }
Пример #13
0
        public bool RemoveFile(string file)
        {
            D2pEntry entry = TryGetEntry(file);

            return(entry != null && RemoveEntry(entry));
        }