Merge pull request #293 from Mighty-S0715: reassign colliding dialog IDs instead of discarding

This commit is contained in:
KAMKEEL committed 2026-08-11 05:54:14 -04:00
commit 3535e7b819
2 files changed
+81 -2

No files matched your search

@@ -76,6 +76,7 @@ public class DialogController implements IDialogHandler {
if (!file.isDirectory())
continue;
DialogCategory category = loadCategoryDir(file);
List<Dialog> reassigned = new ArrayList<Dialog>();
Iterator<Integer> ite = category.dialogs.keySet().iterator();
while (ite.hasNext()) {
int id = ite.next();
@@ -83,12 +84,48 @@ public class DialogController implements IDialogHandler {
lastUsedDialogID = id;
Dialog dialog = category.dialogs.get(id);
if (dialogs.containsKey(id)) {
LogWriter.error("Duplicate id " + dialog.id + " from category " + category.title);
// Dialog IDs live in the filename, not inside the JSON body,
// so a simple rename permanently resolves the conflict.
File catDir = new File(getDir(), category.title);
// Find the next free ID. It has to clear three things: the ids already
// registered globally, the ids this category still holds but has not
// reached yet, and any file already on disk - renameTo overwrites
// silently on POSIX, so landing on an existing file would destroy it.
int newId = lastUsedDialogID;
do {
newId++;
} while (dialogs.containsKey(newId)
|| category.dialogs.containsKey(newId)
|| new File(catDir, newId + ".json").exists());
lastUsedDialogID = newId;
File oldFile = new File(catDir, id + ".json");
File newFile = new File(catDir, newId + ".json");
boolean renamed = oldFile.renameTo(newFile);
dialog.id = newId;
ite.remove();
reassigned.add(dialog);
if (renamed) {
LogWriter.info("Dialog ID conflict: reassigned \"" + dialog.title
+ "\" from id " + id + " to " + newId
+ " in category \"" + category.title + "\"");
} else {
LogWriter.error("Dialog ID conflict: could not rename file for \""
+ dialog.title + "\" (id " + id
+ " in category \"" + category.title + "\") to " + newId);
}
} else {
dialogs.put(id, dialog);
}
}
// Re-register reassigned dialogs under their new IDs.
for (Dialog dialog : reassigned) {
dialogs.put(dialog.id, dialog);
category.dialogs.put(dialog.id, dialog);
}
lastUsedCatID++;
category.id = lastUsedCatID;
categories.put(category.id, category);
@@ -70,6 +70,7 @@ public class QuestController implements IQuestHandler {
if (!file.isDirectory())
continue;
QuestCategory category = loadCategoryDir(file);
List<Quest> reassigned = new ArrayList<Quest>();
Iterator<Integer> ite = category.quests.keySet().iterator();
while (ite.hasNext()) {
int id = ite.next();
@@ -77,8 +78,39 @@ public class QuestController implements IQuestHandler {
lastUsedQuestID = id;
Quest quest = category.quests.get(id);
if (quests.containsKey(id)) {
LogWriter.error("Duplicate id " + quest.id + " from category " + category.title);
// Quest IDs live in the filename, not inside the JSON body,
// so a simple rename permanently resolves the conflict.
File catDir = new File(getDir(), category.title);
// Find the next free ID. It has to clear three things: the ids already
// registered globally, the ids this category still holds but has not
// reached yet, and any file already on disk - renameTo overwrites
// silently on POSIX, so landing on an existing file would destroy it.
int newId = lastUsedQuestID;
do {
newId++;
} while (quests.containsKey(newId)
|| category.quests.containsKey(newId)
|| new File(catDir, newId + ".json").exists());
lastUsedQuestID = newId;
File oldFile = new File(catDir, id + ".json");
File newFile = new File(catDir, newId + ".json");
boolean renamed = oldFile.renameTo(newFile);
quest.id = newId;
ite.remove();
reassigned.add(quest);
if (renamed) {
LogWriter.info("Quest ID conflict: reassigned \"" + quest.title
+ "\" from id " + id + " to " + newId
+ " in category \"" + category.title + "\"");
} else {
LogWriter.error("Quest ID conflict: could not rename file for \""
+ quest.title + "\" (id " + id
+ " in category \"" + category.title + "\") to " + newId);
}
} else {
quests.put(id, quest);
if (quest.profileOptions.enableOptions
@@ -88,6 +120,16 @@ public class QuestController implements IQuestHandler {
}
}
}
// Re-register reassigned quests under their new IDs.
for (Quest quest : reassigned) {
quests.put(quest.id, quest);
category.quests.put(quest.id, quest);
if (quest.profileOptions.enableOptions
&& (quest.profileOptions.completeControl == EnumProfileSync.Shared
|| quest.profileOptions.cooldownControl == EnumProfileSync.Shared)) {
sharedQuests.put(quest.id, quest);
}
}
lastUsedCatID++;
category.id = lastUsedCatID;
categories.put(category.id, category);