aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_bsplugins/src/BSPluginInfo/PluginRecordModel.cpp
blob: 9bc554ebfcebe41381a2bb2a4d213c49a987b965 (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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "PluginRecordModel.h"
#include "TESData/BranchConflictParser.h"
#include "TESData/TypeStringNames.h"
#include "TESFile/Reader.h"

#include <log.h>

#include <algorithm>
#include <iterator>
#include <ranges>

using namespace Qt::Literals::StringLiterals;

namespace BSPluginInfo
{

PluginRecordModel::PluginRecordModel(MOBase::IOrganizer* organizer,
                                     TESData::PluginList* pluginList,
                                     const QString& pluginName)
    : m_PluginName{pluginName}, m_Organizer{organizer}, m_PluginList{pluginList},
      m_FileEntry{pluginList->findEntryByName(pluginName.toStdString())}
{
  if (m_FileEntry) {
    m_DataRoot = m_FileEntry->dataRoot();
  }
}

TESData::RecordPath PluginRecordModel::getPath(const QModelIndex& index) const
{
  TESData::RecordPath path;

  std::vector<const Item*> parents;
  const auto last = static_cast<const Item*>(index.internalPointer());
  for (auto item = last; item->parent; item = item->parent) {
    parents.push_back(item);
  }

  for (const auto& item : std::ranges::reverse_view(parents)) {
    if (item->group.has_value()) {
      path.push(item->group.value(), m_FileEntry->files(), m_PluginName.toStdString());
    }
  }

  if (last->record) {
    if (last->group.has_value()) {
      path.pop();
    }
    path.setIdentifier(last->record->identifier(), {&last->record->file(), 1});
  }

  return path;
}

QModelIndex PluginRecordModel::index(int row, int column,
                                     const QModelIndex& parent) const
{
  if (row < 0 || column < 0)
    return QModelIndex();

  const auto parentItem = parent.isValid()
                              ? static_cast<const Item*>(parent.internalPointer())
                              : m_DataRoot;

  if (!parentItem || row >= parentItem->children.size())
    return QModelIndex();

  const auto item = parentItem->children.nth(row)->second.get();
  return createIndex(row, column, item);
}

QModelIndex PluginRecordModel::parent(const QModelIndex& index) const
{
  if (!index.isValid())
    return QModelIndex();

  const auto item = static_cast<const Item*>(index.internalPointer());
  if (!item->parent || !item->parent->parent) {
    return QModelIndex();
  }

  const auto& siblings = item->parent->parent->children;
  const int row        = static_cast<int>(
      siblings.index_of(std::ranges::find(siblings, item->parent, [&](auto&& pair) {
        return pair.second.get();
      })));

  return createIndex(row, 0, item->parent);
}

bool PluginRecordModel::hasChildren(const QModelIndex& parent) const
{
  if (!parent.isValid()) {
    return m_DataRoot && !m_DataRoot->children.empty();
  }

  const auto parentItem = static_cast<const Item*>(parent.internalPointer());
  return parentItem ? parentItem->group.has_value() : false;
}

int PluginRecordModel::rowCount(const QModelIndex& parent) const
{
  const auto parentItem =
      parent.isValid() ? static_cast<Item*>(parent.internalPointer()) : m_DataRoot;

  return parentItem ? static_cast<int>(parentItem->children.size()) : 0;
}

int PluginRecordModel::columnCount([[maybe_unused]] const QModelIndex& parent) const
{
  return COL_COUNT;
}

bool PluginRecordModel::canFetchMore(const QModelIndex& parent) const
{
  if (!parent.isValid()) {
    return false;
  }

  const auto parentItem = static_cast<const Item*>(parent.internalPointer());
  return parentItem && parentItem->group.has_value() && parentItem->children.empty();
}

void PluginRecordModel::fetchMore(const QModelIndex& parent)
{
  const auto parentItem =
      parent.isValid() ? static_cast<Item*>(parent.internalPointer()) : nullptr;

  if (!parentItem || !parentItem->children.empty() || !parentItem->group.has_value()) {
    return;
  }

  QList<QString> names;
  if (parentItem->record) {
    for (const auto handle : parentItem->record->alternatives()) {
      const auto entry = m_PluginList->findEntryByHandle(handle);
      names.append(QString::fromStdString(entry->name()));
    }
  } else {
    names.append(m_PluginName);
  }

  for (const auto& name : names) {
    const auto vfsEntry =
        m_Organizer->virtualFileTree()->find(name, MOBase::FileTreeEntry::FILE);

    const auto filePath = m_Organizer->resolvePath(name);

    try {
      TESData::BranchConflictParser handler{m_PluginList, name.toStdString(),
                                            getPath(parent)};
      TESFile::Reader<TESData::BranchConflictParser> reader{};
      reader.parse(std::filesystem::path(filePath.toStdString()), handler);
    } catch (const std::exception& e) {
      MOBase::log::error("Error parsing \"{}\": {}", filePath, e.what());
    }
  }

  if (parentItem->children.empty()) {
    beginRemoveRows(parent, 0, 0);
    parentItem->group = std::nullopt;
    endRemoveRows();
  }
}

QVariant PluginRecordModel::data(const QModelIndex& index, int role) const
{
  const auto item = static_cast<const Item*>(index.internalPointer());

  switch (role) {
  case Qt::DisplayRole:
  case Qt::EditRole: {
    switch (index.column()) {
    case COL_ID: {
      if (item->record) {
        if (item->record->hasFormId()) {
          std::uint32_t formId = item->record->formId() & 0xFFFFFFU;

          const auto plugin =
              m_PluginList ? m_PluginList->getPluginByName(m_PluginName) : nullptr;

          if (plugin) {
            const auto file       = item->record->file();
            const auto localIndex = std::distance(
                std::begin(plugin->masters()),
                TESFile::find(plugin->masters(), file, &QString::toStdString));
            formId |= ((localIndex & 0xFF) << 24U);
          }

          QString str = u"%1"_s.arg(formId, 8, 16, QChar(u'0')).toUpper();

          for (const Item* p = item->parent; p; p = p->parent) {
            if (p->group && p->group->hasFormType()) {
              if (item->formType != p->group->formType()) {
                QString suffix = TESData::getFormName(item->formType).toString();
                if (suffix.isEmpty()) {
                  suffix = QString::fromLocal8Bit(item->formType.data(),
                                                  item->formType.size());
                }
                str = u"%1 %2"_s.arg(str).arg(suffix);
              }
              break;
            }
          }

          return str;

        } else if (item->record->hasEditorId()) {
          return QString::fromStdString(item->record->editorId());

        } else if (item->record->hasTypeId()) {
          const auto type    = item->record->typeId();
          const auto typestr = QString::fromLocal8Bit(type.data(), type.size());
          const auto name    = TESData::getDefaultObjectName(type);
          if (!name.isEmpty()) {
            return u"%1 - %2"_s.arg(typestr).arg(name);
          } else {
            return typestr;
          }
        }
      }

      if (item->group.has_value()) {
        return makeGroupName(item->group.value());
      }

      return QVariant();
    }

    case COL_OWNER: {
      if (item->record && item->record->hasFormId()) {
        const auto file = item->record->file();
        return QString::fromStdString(file);
      }
      return QVariant();
    }

    case COL_NAME: {
      if (item->record && item->record->hasFormId()) {
        return QString::fromStdString(item->name);
      }
      return QVariant();
    }
    }

    return QVariant();
  }

  case Qt::BackgroundRole: {
    if (!m_PluginList || !m_FileEntry || !item->record) {
      return QVariant();
    }

    const auto info = m_PluginList->getPluginByName(m_PluginName);
    if (!info) {
      return QVariant();
    }

    bool isConflicted = false;
    for (const auto alternative : item->record->alternatives()) {
      const auto altEntry = m_PluginList->findEntryByHandle(alternative);
      if (!altEntry || altEntry == m_FileEntry)
        continue;

      const auto altInfo =
          m_PluginList->getPluginByName(QString::fromStdString(altEntry->name()));
      if (!altInfo || !altInfo->enabled())
        continue;

      isConflicted = true;
      if (altInfo->priority() > info->priority()) {
        return QColor(255, 0, 0, 64);
      }
    }

    if (isConflicted) {
      return QColor(0, 255, 0, 64);
    } else {
      return QVariant();
    }
  }

  case Qt::UserRole: {
    return QVariant::fromValue(item);
  }
  }

  return QVariant();
}

QString PluginRecordModel::makeGroupName(TESFile::GroupData group)
{
  using GroupType = TESFile::GroupType;

  switch (group.type()) {
  case GroupType::Top: {
    const auto type    = group.formType();
    const auto typestr = QString::fromLocal8Bit(type.data(), type.size());
    const auto name    = TESData::getFormName(type);
    if (!name.isEmpty()) {
      return u"%1 - %2"_s.arg(typestr).arg(name);
    } else {
      return typestr;
    }
  }
  case GroupType::WorldChildren:
    return tr("Children");
  case GroupType::InteriorCellBlock:
    return tr("Block %1").arg(group.block());
  case GroupType::InteriorCellSubBlock:
    return tr("Sub-Block %1").arg(group.block());
  case GroupType::ExteriorCellBlock: {
    const auto [x, y] = group.gridCell();
    return tr("Block %1, %2").arg(x).arg(y);
  }
  case GroupType::ExteriorCellSubBlock: {
    const auto [x, y] = group.gridCell();
    return tr("Sub-Block %1, %2").arg(x).arg(y);
  }
  case GroupType::CellChildren:
    return tr("Children");
  case GroupType::TopicChildren:
    return tr("Children");
  case GroupType::CellPersistentChildren:
    return tr("Persistent");
  case GroupType::CellTemporaryChildren:
    return tr("Temporary");
  case GroupType::CellVisibleDistantChildren:
    return tr("Visible Distant");
  default:
    return tr("Children");
  }
}

QVariant PluginRecordModel::headerData(int section, Qt::Orientation orientation,
                                       int role) const
{
  if (orientation != Qt::Horizontal || role != Qt::DisplayRole)
    return QVariant();

  switch (section) {
  case COL_ID:
    return tr("Form");
  case COL_OWNER:
    return tr("Origin");
  case COL_NAME:
    return tr("Editor ID");
  }

  return QVariant();
}

}  // namespace BSPluginInfo