summaryrefslogtreecommitdiff
path: root/src/archivefiletree.cpp
blob: b90c4798fa79bef6b903143bcee9ffc19ea07403 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
Copyright (C) MO2 Team. All rights reserved.

This file is part of Mod Organizer.

Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Mod Organizer.  If not, see <http://www.gnu.org/licenses/>.
*/

// For QObject::tr:
#include <QObject>

#include "archivefiletree.h"

#include "log.h"

using namespace MOBase;

/**
 * We use custom file entries to store the index.
 */
class ArchiveFileEntry : public virtual FileTreeEntry
{
public:
  /**
   * @brief Create a new entry.
   *
   * @param parent The tree containing this entry.
   * @param name The name of this entry.
   * @param index The index of the entry in the archive.
   */
  ArchiveFileEntry(std::shared_ptr<const IFileTree> parent, QString name, int index)
      : FileTreeEntry(parent, name), m_Index(index)
  {}

  virtual std::shared_ptr<FileTreeEntry> clone() const override
  {
    return std::make_shared<ArchiveFileEntry>(nullptr, name(), m_Index);
  }

  // No private since we are in an implementation file:
  const int m_Index;
};

/**
 *
 */
class ArchiveFileTreeImpl : public virtual ArchiveFileTree,
                            public virtual ArchiveFileEntry
{
public:
  using File = std::tuple<QStringList, bool, int>;

public
    :  // Public for make_shared (but not accessible by other since not exposed in .h):
  ArchiveFileTreeImpl(std::shared_ptr<const IFileTree> parent, QString name, int index,
                      std::vector<File> files)
      : FileTreeEntry(parent, name), ArchiveFileEntry(parent, name, index), IFileTree(),
        m_Files(std::move(files))
  {}

public:  // Override to avoid VS warnings:
  virtual std::shared_ptr<IFileTree> astree() override { return IFileTree::astree(); }

  virtual std::shared_ptr<const IFileTree> astree() const override
  {
    return IFileTree::astree();
  }

protected:
  virtual std::shared_ptr<FileTreeEntry> clone() const override
  {
    return IFileTree::clone();
  }

public:  // Overrides:
  /**
   *
   */
  static void mapToArchive(IFileTree const& tree, QString path,
                           std::vector<FileData*> const& data)
  {
    if (path.length() > 0) {
      // when using a long windows path (starting with \\?\) we apparently can have
      // redundant . components in the path. This wasn't a problem with "regular" path
      // names.
      if (path == ".") {
        path.clear();
      } else {
        path.append("\\");
      }
    }

    for (auto const& entry : tree) {
      if (entry->isDir()) {
        const ArchiveFileTreeImpl& archiveEntry =
            dynamic_cast<const ArchiveFileTreeImpl&>(*entry);
        QString tmp = path + archiveEntry.name();
        if (archiveEntry.m_Index != -1) {
          data[archiveEntry.m_Index]->addOutputFilePath(tmp.toStdWString());
        }
        mapToArchive(*archiveEntry.astree(), tmp, data);
      } else {
        const ArchiveFileEntry& archiveFileEntry =
            dynamic_cast<const ArchiveFileEntry&>(*entry);
        if (archiveFileEntry.m_Index != -1) {
          data[archiveFileEntry.m_Index]->addOutputFilePath(
              (path + archiveFileEntry.name()).toStdWString());
        }
      }
    }
  }

  /**
   *
   */
  void mapToArchive(Archive& archive) const override
  {
    mapToArchive(*this, "", archive.getFileList());
  }

protected:
  /**
   * Overriding makeDirectory and makeFile to create file tree or file entry with index
   * -1.
   *
   */
  virtual std::shared_ptr<IFileTree>
  makeDirectory(std::shared_ptr<const IFileTree> parent, QString name) const override
  {
    return std::make_shared<ArchiveFileTreeImpl>(parent, name, -1, std::vector<File>{});
  }

  virtual std::shared_ptr<FileTreeEntry>
  makeFile(std::shared_ptr<const IFileTree> parent, QString name) const override
  {
    return std::make_shared<ArchiveFileEntry>(parent, name, -1);
  }

  virtual bool
  doPopulate(std::shared_ptr<const IFileTree> parent,
             std::vector<std::shared_ptr<FileTreeEntry>>& entries) const override
  {

    // Sort by name:
    std::sort(std::begin(m_Files), std::end(m_Files), [](const auto& a, const auto& b) {
      return std::get<0>(a)[0].compare(std::get<0>(b)[0], Qt::CaseInsensitive) < 0;
    });

    // We know that the files are sorted:
    QString currentName = "";
    int currentIndex    = -1;
    std::vector<File> currentFiles;
    for (auto& p : m_Files) {

      // At the start or if we have reset, just retrieve the current name:
      if (currentName == "") {
        currentName = std::get<0>(p)[0];
      }

      // If the name is different, we need to create a directory from what we have
      // accumulated:
      if (currentName != std::get<0>(p)[0]) {

        // We may or may not have an index here, it depends on the type of archive (some
        // archives list intermediate non-empty folders, some don't):
        entries.push_back(std::make_shared<ArchiveFileTreeImpl>(
            parent, currentName, currentIndex, std::move(currentFiles)));

        currentFiles.clear();  // Back to a valid state.

        // Reset the index:
        currentIndex = -1;
      }

      // We can always override the current name:
      currentName = std::get<0>(p)[0];

      // If the current path contains only one components:
      if (std::get<0>(p).size() == 1) {

        // If it is not a directory, then it is a file in directly under this tree:
        if (!std::get<1>(p)) {
          entries.push_back(
              std::make_shared<ArchiveFileEntry>(parent, currentName, std::get<2>(p)));
          currentName = "";
        } else {
          // Otherwize, it is the actual "file" corresponding to the directory we are
          // listing, so we can retrieve the index here:
          currentIndex = std::get<2>(p);
        }
      } else {
        currentFiles.push_back(
            {QStringList(std::get<0>(p).begin() + 1, std::get<0>(p).end()),
             std::get<1>(p), std::get<2>(p)});
      }
    }

    if (currentName != "") {
      entries.push_back(std::make_shared<ArchiveFileTreeImpl>(
          parent, currentName, currentIndex, std::move(currentFiles)));
    }

    // Let the parent class sort the entries:
    return false;
  }

  virtual std::shared_ptr<IFileTree> doClone() const override
  {
    return std::make_shared<ArchiveFileTreeImpl>(nullptr, name(), m_Index, m_Files);
  }

private:
  mutable std::vector<File> m_Files;
};

std::shared_ptr<ArchiveFileTree> ArchiveFileTree::makeTree(Archive const& archive)
{
  auto const& data = archive.getFileList();

  std::vector<ArchiveFileTreeImpl::File> files;
  files.reserve(data.size());

  for (size_t i = 0; i < data.size(); ++i) {
    // Ignore "." and ".." as they're useless and muck things up
    if (data[i]->getArchiveFilePath().compare(L".") == 0 ||
        data[i]->getArchiveFilePath().compare(L"..") == 0) {
      continue;
    }

    files.push_back(
        std::make_tuple(QString::fromStdWString(data[i]->getArchiveFilePath())
                            .replace("\\", "/")
                            .split("/", Qt::SkipEmptyParts),
                        data[i]->isDirectory(), (int)i));
  }

  auto tree = std::make_shared<ArchiveFileTreeImpl>(nullptr, "", -1, std::move(files));
  return tree;
}

/**
 * @brief Recursive function for the ArchiveFileTree::mapToArchive method. Need a
 * template here because iterators from a vector of entries are not exactly the same as
 * the iterators returned by a IFileTree.
 *
 */
template <class It>
void mapToArchive(std::vector<FileData*> const& data, It begin, It end)
{
  for (auto it = begin; it != end; ++it) {
    auto entry   = *it;
    auto* aentry = dynamic_cast<const ArchiveFileEntry*>(entry.get());

    if (aentry->m_Index != -1) {
      data[aentry->m_Index]->addOutputFilePath(aentry->path().toStdWString());
    }

    if (entry->isDir()) {
      auto tree = entry->astree();
      mapToArchive(data, tree->begin(), tree->end());
    }
  }
}

void ArchiveFileTree::mapToArchive(
    Archive& archive, std::vector<std::shared_ptr<const FileTreeEntry>> const& entries)
{
  ::mapToArchive(archive.getFileList(), entries.cbegin(), entries.cend());
}