aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_bsplugins/src/TESData/FileInfo.cpp
blob: 8429a11a078c69026ab0991a66eee072c7bc7709 (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
#include "FileInfo.h"
#include "FileEntry.h"
#include "MOPlugin/Settings.h"
#include "PluginList.h"

#include <algorithm>

using namespace Qt::Literals::StringLiterals;

namespace TESData
{

FileInfo::FileInfo(PluginList* pluginList, const QString& name, bool forceLoaded,
                   bool forceEnabled, bool forceDisabled, bool lightSupported)
    : m_PluginList{pluginList},
      m_FileSystemData{
          .name               = name,
          .hasMasterExtension = name.endsWith(u".esm"_s, Qt::CaseInsensitive),
          .hasLightExtension =
              lightSupported && name.endsWith(u".esl"_s, Qt::CaseInsensitive),
          .forceLoaded   = forceLoaded,
          .forceEnabled  = forceEnabled,
          .forceDisabled = forceDisabled,
      },
      m_Conflicts{[this]() {
        return doConflictCheck();
      }}
{}

bool FileInfo::isMasterFile() const
{
  return m_Metadata.isMasterFlagged || m_FileSystemData.hasMasterExtension ||
         m_FileSystemData.hasLightExtension;
}

bool FileInfo::isMediumFile() const
{
  return m_Metadata.isMediumFlagged;
}

bool FileInfo::isSmallFile() const
{
  return m_Metadata.isLightFlagged || m_FileSystemData.hasLightExtension;
}

bool FileInfo::isBlueprintFile() const
{
  return isMasterFile() && m_Metadata.isBlueprintFlagged;
}

bool FileInfo::isAlwaysEnabled() const
{
  return m_FileSystemData.forceLoaded || m_FileSystemData.forceEnabled;
}

bool FileInfo::canBeToggled() const
{
  return !m_FileSystemData.forceLoaded && !m_FileSystemData.forceEnabled &&
         !m_FileSystemData.forceDisabled;
}

bool FileInfo::mustLoadAfter(const FileInfo& other) const
{
  if (this->isBlueprintFile() && !other.isBlueprintFile()) {
    return true;
  } else if (other.isBlueprintFile() && !this->isBlueprintFile()) {
    return false;
  }

  const bool hasMaster = this->masters().contains(other.name(), Qt::CaseInsensitive);
  const bool isMaster  = other.masters().contains(this->name(), Qt::CaseInsensitive);

  if (hasMaster && !isMaster) {
    return true;
  } else if (isMaster) {
    return false;
  }

  if (other.forceLoaded() && !this->forceLoaded()) {
    return true;
  }

  if (other.isMasterFile() && !this->isMasterFile()) {
    return true;
  }

  return false;
}

static void checkConflict(QSet<int>& winning, QSet<int>& losing, const FileInfo& file,
                          const TESData::PluginList* pluginList,
                          TESFileHandle alternative, bool ignoreMasters)
{
  const auto entry      = pluginList->findEntryByName(file.name().toStdString());
  const auto otherEntry = pluginList->findEntryByHandle(alternative);
  if (otherEntry == nullptr || otherEntry == entry) {
    return;
  }

  const auto otherFile =
      pluginList->getPluginByName(QString::fromStdString(otherEntry->name()));
  if (otherFile == nullptr) {
    return;
  }

  const QString otherName = QString::fromStdString(otherEntry->name());
  const int otherIndex    = pluginList->getIndex(otherName);

  if (file.priority() > otherFile->priority()) {
    if (!ignoreMasters ||
        !file.masters().contains(otherFile->name(), Qt::CaseInsensitive)) {
      winning.insert(otherIndex);
    }
  } else {
    if (!ignoreMasters ||
        !otherFile->masters().contains(file.name(), Qt::CaseInsensitive)) {
      losing.insert(otherIndex);
    }
  }
}

FileInfo::Conflicts FileInfo::doConflictCheck() const
{
  Conflicts conflicts;

  const auto entry = m_PluginList->findEntryByName(name().toStdString());
  if (entry == nullptr) {
    return conflicts;
  }

  const bool ignoreMasters =
      Settings::instance()->get<bool>("ignore_master_conflicts", false);

  entry->forEachRecord([&](auto&& record) {
    if (record->ignored())
      return;

    for (const auto alternative : record->alternatives()) {
      checkConflict(conflicts.m_OverridingList, conflicts.m_OverriddenList, *this,
                    m_PluginList, alternative, ignoreMasters);
    }
  });

  for (const auto& archive : m_FileSystemData.archives) {
    const auto archiveEntry = m_PluginList->findArchive(archive);
    if (!archiveEntry) {
      continue;
    }

    archiveEntry->forEachMember([&](auto&& item) {
      for (const auto alternative : item->alternatives) {
        checkConflict(conflicts.m_OverwritingArchiveList,
                      conflicts.m_OverwrittenArchiveList, *this, m_PluginList,
                      alternative, ignoreMasters);
      }
    });
  }

  uint conflictState = CONFLICT_NONE;
  if (!conflicts.m_OverridingList.empty()) {
    conflictState |= CONFLICT_OVERRIDE;
  }
  if (!conflicts.m_OverriddenList.empty()) {
    conflictState |= CONFLICT_OVERRIDDEN;
  }
  if (!conflicts.m_OverwritingArchiveList.empty()) {
    conflictState |= CONFLICT_ARCHIVE_OVERWRITE;
  }
  if (!conflicts.m_OverwrittenArchiveList.empty()) {
    conflictState |= CONFLICT_ARCHIVE_OVERWRITTEN;
  }
  conflicts.m_CurrentConflictState = static_cast<EConflictFlag>(conflictState);

  return conflicts;
}

}  // namespace TESData