Skip to content

Autosave mappings #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,28 @@ public void openMappings(EntryTree<EntryMapping> mappings) {
}

public CompletableFuture<Void> saveMappings(Path path) {
return this.saveMappings(path, this.readWriteService);
return this.saveMappings(path, false);
}

public CompletableFuture<Void> saveMappings(Path path, boolean background) {
return this.saveMappings(path, this.readWriteService, background);
}

/**
* Saves the mappings, with a dialog popping up, showing the progress.
* Saves the mappings. If {@code background} is false, a dialog will pop up
* showing the progress. Otherwise, the progress will be shown in the
* status bar.
*
* <p>Notice the returned completable future has to be completed by
* {@link SwingUtilities#invokeLater(Runnable)}. Hence, do not try to
* join on the future in gui, but rather call {@code thenXxx} methods.
*
* @param path the path of the save
* @param service the writer for the mapping type
* @param background whether the progress should be shown in the status bar
* @return the future of saving
*/
public CompletableFuture<Void> saveMappings(Path path, ReadWriteService service) {
public CompletableFuture<Void> saveMappings(Path path, ReadWriteService service, boolean background) {
if (this.project == null) {
return CompletableFuture.completedFuture(null);
} else if (!service.supportsWriting()) {
Expand All @@ -231,22 +238,35 @@ public CompletableFuture<Void> saveMappings(Path path, ReadWriteService service)
return CompletableFuture.completedFuture(null);
}

return ProgressDialog.runOffThread(this.gui, progress -> {
EntryRemapper mapper = this.project.getRemapper();
MappingSaveParameters saveParameters = this.enigma.getProfile().getMappingSaveParameters();
if (background) {
return CompletableFuture.supplyAsync(() -> {
ProgressListener progress = ProgressListener.createEmpty();
this.gui.getMainWindow().getStatusBar().syncWith(progress);
this.doSave(path, service, progress);
return null;
});
} else {
return ProgressDialog.runOffThread(this.gui, progress -> {
this.doSave(path, service, progress);
});
}
}

MappingDelta<EntryMapping> delta = mapper.takeMappingDelta();
boolean saveAll = !path.equals(this.loadedMappingPath);
private void doSave(Path path, ReadWriteService service, ProgressListener progress) {
EntryRemapper mapper = this.project.getRemapper();
MappingSaveParameters saveParameters = this.enigma.getProfile().getMappingSaveParameters();

this.readWriteService = service;
this.loadedMappingPath = path;
MappingDelta<EntryMapping> delta = mapper.takeMappingDelta();
boolean saveAll = !path.equals(this.loadedMappingPath);

if (saveAll) {
service.write(mapper.getMappings(), path, progress, saveParameters);
} else {
service.write(mapper.getMappings(), delta, path, progress, saveParameters);
}
});
this.readWriteService = service;
this.loadedMappingPath = path;

if (saveAll) {
service.write(mapper.getMappings(), path, progress, saveParameters);
} else {
service.write(mapper.getMappings(), delta, path, progress, saveParameters);
}
}

public void closeMappings() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@
public class FeaturesSection extends ReflectiveConfig.Section {
@Comment("Enables statistic icons in the class tree. This has a major performance impact on JAR files with lots of classes.")
public final TrackedValue<Boolean> enableClassTreeStatIcons = this.value(true);
@Comment("Enables auto save functionality, which will automatically save mappings when a change is made.")
public final TrackedValue<Boolean> autoSaveMappings = this.value(true);
}
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,7 @@ private static void prepareSaveMappingsAsMenu(JMenu saveMappingsAsMenu, JMenuIte

if (fileChooser.showSaveDialog(gui.getFrame()) == JFileChooser.APPROVE_OPTION) {
Path savePath = ExtensionFileFilter.getSavePath(fileChooser);
gui.getController().saveMappings(savePath, format);
gui.getController().saveMappings(savePath, format, false);
saveMappingsItem.setEnabled(true);
Config.main().stats.lastSelectedDir.setValue(fileChooser.getCurrentDirectory().toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public void onStopEditing(ConvertingTextField field, boolean abort) {
IdentifierPanel.this.vc.setNotifier(IdentifierPanel.this.gui.getNotificationManager());
IdentifierPanel.this.vc.reset();
IdentifierPanel.this.doRename(field.getText());
if (Config.main().features.autoSaveMappings.value() && IdentifierPanel.this.gui.mappingsFileChooser.getSelectedFile() != null) {
IdentifierPanel.this.gui.getController().saveMappings(IdentifierPanel.this.gui.mappingsFileChooser.getSelectedFile().toPath(), true);
}
}

EditorPanel e = IdentifierPanel.this.gui.getActiveEditor();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public void write(EntryTree<EntryMapping> mappings, MappingDelta<EntryMapping> d
progress.step(steps++, classEntry.getFullName());
this.writeRoot(writer, writtenMappings, classEntry);
}

progress.step(steps, I18n.translate("progress.mappings.enigma_file.done"));
} catch (IOException e) {
Logger.error(e, "Error while writing mappings to file {}", path);
}
Expand Down Expand Up @@ -102,6 +104,7 @@ public void write(EntryTree<EntryMapping> mappings, MappingDelta<EntryMapping> d
Logger.error(e, "Failed to write class '{}'", classEntry.getFullName());
}
});
progress.step(steps.get(), I18n.translate("progress.mappings.enigma_directory.done"));
}

private void applyDeletions(Path root, Collection<ClassEntry> changedClasses, EntryTree<EntryMapping> mappings, EntryTree<EntryMapping> oldMappings, MappingFileNameFormat fileNameFormat) {
Expand Down
1 change: 1 addition & 0 deletions enigma/src/main/resources/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@
"progress.mappings.enigma_file.writing": "Writing classes",
"progress.mappings.enigma_directory.loading": "Loading mapping files",
"progress.mappings.enigma_directory.writing": "Writing classes",
"progress.mappings.enigma_directory.done": "Done!",
"progress.mappings.tiny_file.loading": "Loading mapping file",
"progress.mappings.tiny_v2.loading": "Loading mapping file",
"progress.mappings.srg_file.generating": "Generating mappings",
Expand Down