MCPcopy Create free account
hub / github.com/PolyMC/PolyMC / createModdedJar

Method createModdedJar

launcher/MMCZip.cpp:131–229  ·  view source on GitHub ↗

ours

Source from the content-addressed store, hash-verified

129
130// ours
131bool MMCZip::createModdedJar(QString sourceJarPath, QString targetJarPath, const QList<Mod*>& mods)
132{
133 QuaZip zipOut(targetJarPath);
134 if (!zipOut.open(QuaZip::mdCreate))
135 {
136 QFile::remove(targetJarPath);
137 qCritical() << "Failed to open the minecraft.jar for modding";
138 return false;
139 }
140 // Files already added to the jar.
141 // These files will be skipped.
142 QSet<QString> addedFiles;
143
144 // Modify the jar
145 // This needs to be done in reverse-order to ensure we respect the loading order of components
146 for (auto i = mods.crbegin(); i != mods.crend(); i++)
147 {
148 const auto* mod = *i;
149 // do not merge disabled mods.
150 if (!mod->enabled())
151 continue;
152 if (mod->type() == ResourceType::ZIPFILE)
153 {
154 if (!mergeZipFiles(&zipOut, mod->fileinfo(), addedFiles))
155 {
156 zipOut.close();
157 QFile::remove(targetJarPath);
158 qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
159 return false;
160 }
161 }
162 else if (mod->type() == ResourceType::SINGLEFILE)
163 {
164 // FIXME: buggy - does not work with addedFiles
165 auto filename = mod->fileinfo();
166 if (!JlCompress::compressFile(&zipOut, filename.absoluteFilePath(), filename.fileName()))
167 {
168 zipOut.close();
169 QFile::remove(targetJarPath);
170 qCritical() << "Failed to add" << mod->fileinfo().fileName() << "to the jar.";
171 return false;
172 }
173 addedFiles.insert(filename.fileName());
174 }
175 else if (mod->type() == ResourceType::FOLDER)
176 {
177 // untested, but seems to be unused / not possible to reach
178 // FIXME: buggy - does not work with addedFiles
179 auto filename = mod->fileinfo();
180 QString what_to_zip = filename.absoluteFilePath();
181 QDir dir(what_to_zip);
182 dir.cdUp();
183 QString parent_dir = dir.absolutePath();
184 auto files = QFileInfoList();
185 MMCZip::collectFileListRecursively(what_to_zip, nullptr, &files, nullptr);
186
187 for (auto e : files) {
188 if (addedFiles.contains(e.filePath()))

Callers

nothing calls this directly

Calls 7

openMethod · 0.80
enabledMethod · 0.80
fileinfoMethod · 0.80
insertMethod · 0.80
filePathMethod · 0.80
typeMethod · 0.45
containsMethod · 0.45

Tested by

no test coverage detected