Skip to content

Commit

Permalink
Fix crashes when some mod from modpack gets deleted before duplicated…
Browse files Browse the repository at this point in the history
… mods gets checked
  • Loading branch information
Skidamek committed Jun 29, 2024
1 parent 964c272 commit bc2d3d9
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 198 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
public class GlobalVariables {
public static final Logger LOGGER = LogManager.getLogger("AutoModpack");
public static final String MOD_ID = "automodpack";
public static Boolean DEBUG;
public static Boolean DEBUG = false;
public static Boolean preload;
public static String MC_VERSION;
public static String AM_VERSION;
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mixin_extras_version=0.3.6
# Mod Properties
mod_id=automodpack
mod_name=AutoModpack
mod_version=4.0.0-beta4
mod_version=4.0.0-beta5
maven_group=pl.skidam.automodpack
archives_base_name=automodpack

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -439,9 +439,12 @@ private boolean finishModpackUpdate(Path modpackDir, Path modpackContentFile) th

var dupeMods = ModpackUtils.getDupeMods(modpackDir);

return ModpackUtils.removeDupeMods(dupeMods);
boolean requiresRestart = ModpackUtils.removeDupeMods(dupeMods);

return requiresRestart;
}

// returns true if requires restart
private void deleteDeletedFiles(Path modpackDir, Path modpackContentFile, List<String> modpackFiles, List<Path> pathList) {
for (Path path : pathList) {
if (Files.isDirectory(path)) continue;
Expand All @@ -454,7 +457,11 @@ private void deleteDeletedFiles(Path modpackDir, Path modpackContentFile, List<S
Path runPath = Path.of("." + file);
if (Files.exists(runPath) && CustomFileUtils.compareFileHashes(path, runPath, "SHA-1")) {
LOGGER.info("Deleting {} and {}", path, runPath);
CustomFileUtils.forceDelete(runPath);
// TODO: confirm with content.json if its a mod or not
if (!runPath.getParent().getFileName().toString().equals("mods")) {
// we dont delete mods, as we dont ever add mods there
CustomFileUtils.forceDelete(runPath);
}
} else {
LOGGER.info("Deleting {}", path);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ public Collection<Mod> getModList() {
Collection<Mod> modList = new ArrayList<>();

for (var info : mods) {
String modID = info.getMetadata().getId();
Path path = getModPath(modID);
// If we cant get the path, we skip the mod, its probably JiJed, we dont need it in the list
if (path == null || path.toString().isEmpty()) {
continue;
}
List<String> dependencies = info.getMetadata().getDependencies().stream().filter(d -> d.getKind().equals(ModDependency.Kind.DEPENDS)).map(ModDependency::getModId).toList();
Mod mod = new Mod(modID,
info.getMetadata().getProvides(),
info.getMetadata().getVersion().getFriendlyString(),
path,
getModEnvironment(modID),
dependencies
);
modList.add(mod);
try {
String modID = info.getMetadata().getId();
Path path = getModPath(modID);
// If we cant get the path, we skip the mod, its probably JiJed, we dont need it in the list
if (path == null || path.toString().isEmpty()) {
continue;
}
List<String> dependencies = info.getMetadata().getDependencies().stream().filter(d -> d.getKind().equals(ModDependency.Kind.DEPENDS)).map(ModDependency::getModId).toList();
Mod mod = new Mod(modID,
info.getMetadata().getProvides(),
info.getMetadata().getVersion().getFriendlyString(),
path,
getModEnvironment(modID),
dependencies
);
modList.add(mod);
} catch (Exception ignored) {}
}

return this.modList = modList;
Expand Down Expand Up @@ -123,12 +125,14 @@ public String getLoaderVersion() {
public Path getModPath(String modId) {
if (!isModLoaded(modId)) return null;

for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
if (modContainer.getMetadata().getId().equals(modId)) {
FileSystem fileSys = modContainer.getRootPaths().get(0).getFileSystem();
return Path.of(fileSys.toString());
try {
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
if (modContainer.getMetadata().getId().equals(modId)) {
FileSystem fileSys = modContainer.getRootPaths().get(0).getFileSystem();
return Path.of(fileSys.toString());
}
}
}
} catch (Exception ignored) {}

LOGGER.error("Could not find jar file for " + modId);
return null;
Expand Down Expand Up @@ -247,13 +251,15 @@ public EnvironmentType getModEnvironment(String modId) {
}

public String getModId(Path file, boolean checkAlsoOutOfContainer) {
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
FileSystem fileSys = modContainer.getRootPaths().get(0).getFileSystem();
Path modFile = Paths.get(fileSys.toString());
if (modFile.toAbsolutePath().equals(file.toAbsolutePath())) {
return modContainer.getMetadata().getId();
try {
for (ModContainer modContainer : FabricLoader.getInstance().getAllMods()) {
FileSystem fileSys = modContainer.getRootPaths().get(0).getFileSystem();
Path modFile = Paths.get(fileSys.toString());
if (modFile.toAbsolutePath().equals(file.toAbsolutePath())) {
return modContainer.getMetadata().getId();
}
}
}
} catch (Exception ignored) {}

if (checkAlsoOutOfContainer) {
return getModIdFromNotLoadedJar(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,24 @@ public Collection<Mod> getModList() {
Collection<Mod> modList = new ArrayList<>();

for (ModInfo info: modInfo) {
String modID = info.getModId();
Path path = getModPath(modID);
// If we cant get the path, we skip the mod, its probably JiJed, we dont need it in the list
if (path == null || path.toString().isEmpty()) {
continue;
}
List<String> dependencies = info.getDependencies().stream().filter(IModInfo.ModVersion::isMandatory).map(IModInfo.ModVersion::getModId).toList();
Mod mod = new Mod(modID,
List.of(),
info.getOwningFile().versionString(),
path,
getModEnvironment(modID),
dependencies
);
modList.add(mod);
try {
String modID = info.getModId();
Path path = getModPath(modID);
// If we cant get the path, we skip the mod, its probably JiJed, we dont need it in the list
if (path == null || path.toString().isEmpty()) {
continue;
}
List<String> dependencies = info.getDependencies().stream().filter(IModInfo.ModVersion::isMandatory).map(IModInfo.ModVersion::getModId).toList();
Mod mod = new Mod(modID,
List.of(),
info.getOwningFile().versionString(),
path,
getModEnvironment(modID),
dependencies
);

modList.add(mod);
} catch (Exception ignored) {}
}

return this.modList = modList;
Expand Down Expand Up @@ -140,25 +143,27 @@ public Path getModPath(String modId) {
return null;
}

if (preload) {
Collection<ModFile> modFiles = LoadedMods.INSTANCE.candidateMods;
for (var modFile: modFiles) {
if (modFile.getModFileInfo() == null || modFile.getModInfos().isEmpty()) {
continue;
}
if (modFile.getModInfos().get(0).getModId().equals(modId)) {
return modFile.getModInfos().get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
try {
if (preload) {
Collection<ModFile> modFiles = LoadedMods.INSTANCE.candidateMods;
for (var modFile : modFiles) {
if (modFile.getModFileInfo() == null || modFile.getModInfos().isEmpty()) {
continue;
}
if (modFile.getModInfos().get(0).getModId().equals(modId)) {
return modFile.getModInfos().get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
}
}
}

} else if (isModLoaded(modId)) {
ModFileInfo modInfo = FMLLoader.getLoadingModList().getModFileById(modId);
} else if (isModLoaded(modId)) {
ModFileInfo modInfo = FMLLoader.getLoadingModList().getModFileById(modId);

List<IModInfo> mods = modInfo.getMods();
if (!mods.isEmpty()) {
return mods.get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
List<IModInfo> mods = modInfo.getMods();
if (!mods.isEmpty()) {
return mods.get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
}
}
}
} catch (Exception ignored) {}

return null;
}
Expand Down Expand Up @@ -224,14 +229,16 @@ public EnvironmentType getModEnvironment(String modId) {
}

public String getModId(Path file, boolean checkAlsoOutOfContainer) {
if (FMLLoader.getLoadingModList() != null) {
List<ModInfo> modInfos = FMLLoader.getLoadingModList().getMods();
for (ModInfo modInfo : modInfos) {
if (modInfo.getOwningFile().getFile().getFilePath().toAbsolutePath().normalize().equals(file.toAbsolutePath().normalize())) {
return modInfo.getModId();
try {
if (FMLLoader.getLoadingModList() != null) {
List<ModInfo> modInfos = FMLLoader.getLoadingModList().getMods();
for (ModInfo modInfo : modInfos) {
if (modInfo.getOwningFile().getFile().getFilePath().toAbsolutePath().normalize().equals(file.toAbsolutePath().normalize())) {
return modInfo.getModId();
}
}
}
}
} catch (Exception ignored) {}

if (checkAlsoOutOfContainer) {
return getModIdFromNotLoadedJar(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,18 @@ public Collection<Mod> getModList() {
Collection<Mod> modList = new ArrayList<>();

for (ModInfo info: modInfo) {
String modID = info.getModId();
List<String> dependencies = info.getDependencies().stream().filter(IModInfo.ModVersion::isMandatory).map(IModInfo.ModVersion::getModId).toList();
Mod mod = new Mod(modID,
List.of(),
info.getOwningFile().versionString(),
getModPath(modID),
getModEnvironment(modID),
dependencies
);
modList.add(mod);
try {
String modID = info.getModId();
List<String> dependencies = info.getDependencies().stream().filter(IModInfo.ModVersion::isMandatory).map(IModInfo.ModVersion::getModId).toList();
Mod mod = new Mod(modID,
List.of(),
info.getOwningFile().versionString(),
getModPath(modID),
getModEnvironment(modID),
dependencies
);
modList.add(mod);
} catch (Exception ignored) {}
}

return this.modList = modList;
Expand Down Expand Up @@ -136,25 +138,27 @@ public Path getModPath(String modId) {
return null;
}

if (preload) {
Collection<ModFile> modFiles = LoadedMods.INSTANCE.candidateMods;
for (var modFile: modFiles) {
if (modFile.getModFileInfo() == null || modFile.getModInfos().isEmpty()) {
continue;
try {
if (preload) {
Collection<ModFile> modFiles = LoadedMods.INSTANCE.candidateMods;
for (var modFile: modFiles) {
if (modFile.getModFileInfo() == null || modFile.getModInfos().isEmpty()) {
continue;
}
if (modFile.getModInfos().get(0).getModId().equals(modId)) {
return modFile.getModInfos().get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
}
}
if (modFile.getModInfos().get(0).getModId().equals(modId)) {
return modFile.getModInfos().get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
}
}

} else if (isModLoaded(modId)) {
ModFileInfo modInfo = FMLLoader.getLoadingModList().getModFileById(modId);
} else if (isModLoaded(modId)) {
ModFileInfo modInfo = FMLLoader.getLoadingModList().getModFileById(modId);

List<IModInfo> mods = modInfo.getMods();
if (!mods.isEmpty()) {
return mods.get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
List<IModInfo> mods = modInfo.getMods();
if (!mods.isEmpty()) {
return mods.get(0).getOwningFile().getFile().getFilePath().toAbsolutePath();
}
}
}
} catch (Exception ignored) {}

return null;
}
Expand Down Expand Up @@ -220,14 +224,16 @@ public EnvironmentType getModEnvironment(String modId) {
}

public String getModId(Path file, boolean checkAlsoOutOfContainer) {
if (FMLLoader.getLoadingModList() != null) {
List<ModInfo> modInfos = FMLLoader.getLoadingModList().getMods();
for (ModInfo modInfo : modInfos) {
if (modInfo.getOwningFile().getFile().getFilePath().toAbsolutePath().normalize().equals(file.toAbsolutePath().normalize())) {
return modInfo.getModId();
try {
if (FMLLoader.getLoadingModList() != null) {
List<ModInfo> modInfos = FMLLoader.getLoadingModList().getMods();
for (ModInfo modInfo : modInfos) {
if (modInfo.getOwningFile().getFile().getFilePath().toAbsolutePath().normalize().equals(file.toAbsolutePath().normalize())) {
return modInfo.getModId();
}
}
}
}
} catch (Exception ignored) {}

if (checkAlsoOutOfContainer) {
return getModIdFromNotLoadedJar(file);
Expand Down
Loading

0 comments on commit bc2d3d9

Please sign in to comment.