-
Notifications
You must be signed in to change notification settings - Fork 137
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
Detect unsaved work #1685
Merged
Merged
Detect unsaved work #1685
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
/* | ||
* Copyright (c) 2024 - The MegaMek Team. All Rights Reserved. | ||
* | ||
* This file is part of MegaMekLab. | ||
* | ||
* MegaMek is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* MegaMek is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with MegaMek. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package megameklab.util; | ||
|
||
import megamek.common.Entity; | ||
import megamek.common.Mek; | ||
import megamek.common.MekFileParser; | ||
import megamek.common.loaders.BLKFile; | ||
import megamek.common.loaders.EntityLoadingException; | ||
import megamek.common.loaders.EntitySavingException; | ||
import megamek.logging.MMLogger; | ||
import megameklab.ui.MegaMekLabMainUI; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.File; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
|
||
/** | ||
* Helps to determine if a tab should have an "Unsaved Work" indicator and if the "would you like to save" prompt should appear. | ||
*/ | ||
public class EntityChangedUtil { | ||
private static final MMLogger logger = MMLogger.create(EntityChangedUtil.class); | ||
|
||
private static final Map<String, Entity> cache = new ConcurrentHashMap<>(); | ||
|
||
/** | ||
* Determines if the editor has unsaved changes. | ||
* @param editor The MainUI to check for changes | ||
* @return true if saving the unit would produce a different unit than the one saved already, or if there is no existing file for the unit. | ||
*/ | ||
public static boolean hasEntityChanged(MegaMekLabMainUI editor) { | ||
var filename = editor.getFileName(); | ||
if (filename == null || filename.isBlank()) { | ||
return true; | ||
} | ||
|
||
// The idea behind how this works is to store a copy of the unit based on that unit's current file. | ||
// By encoding both the original and current version of the entity to mtf/blk at the same time, | ||
// We ensure that identical units will encode to identical mtf/blk strings, which indicates no unsaved work. | ||
|
||
if (!cache.containsKey(filename)) { | ||
var f = new File(filename); | ||
if (!f.exists()) { | ||
return true; | ||
} | ||
|
||
try { | ||
var e = new MekFileParser(f).getEntity(); | ||
cache.put(filename, e); | ||
} catch (Exception ex) { | ||
logger.error("Entity loading failure:", ex); | ||
cache.put(filename, null); | ||
} | ||
} | ||
|
||
try { | ||
var o = encode(cache.get(filename)); | ||
var n = encode(editor.getEntity()); | ||
return !o.equals(n); | ||
} catch (EntitySavingException e) { | ||
logger.error("Entity encoding failure:", e); | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param editor The editor containing the unit that was just saved. | ||
*/ | ||
public static void editorSaved(MegaMekLabMainUI editor) { | ||
var filename = editor.getFileName(); | ||
if (filename == null || filename.isBlank()) { | ||
return; | ||
} | ||
|
||
try { | ||
var bis = new ByteArrayInputStream(encode(editor.getEntity()).getBytes()); | ||
cache.put(editor.getFileName(), new MekFileParser(bis, editor.getFileName()).getEntity()); | ||
} catch (EntitySavingException | EntityLoadingException e) { | ||
cache.remove(filename); | ||
logger.error("Entity encoding failure:", e); | ||
} | ||
} | ||
|
||
private static String encode(Entity e) throws EntitySavingException { | ||
if (e == null) { | ||
return ""; | ||
} | ||
|
||
if (e instanceof Mek m) { | ||
return m.getMtf(); | ||
} else { | ||
var blk = BLKFile.getBlock(e); | ||
return String.join("\n", blk.getAllDataAsString()); | ||
} | ||
} | ||
|
||
private EntityChangedUtil() {} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might be able to get by with a 'dirty' flag in the entity or something, but for now let's see how this works out.