-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b62d04c
commit 8d8ecf5
Showing
8 changed files
with
234 additions
and
17 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ | |
/megameklab/docs/mml-revision.txt | ||
/megameklab/MegaMekLab.l4j.ini | ||
units.cache | ||
.mml_tmp |
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
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,172 @@ | ||
/* | ||
* 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.ui.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.common.preference.PreferenceManager; | ||
import megameklab.ui.MegaMekLabMainUI; | ||
import megameklab.ui.dialog.UiLoader; | ||
import megameklab.util.UnitUtil; | ||
import org.apache.commons.io.FileUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.util.*; | ||
import java.util.regex.Pattern; | ||
|
||
public class TabStateUtil { | ||
private final static String TAB_STATE_DIRECTORY = ".mml_tmp"; | ||
private final static String TAB_STATE_CLEAN = "clean"; | ||
private final static String FILENAME_ASSOCIATIONS = "filenames.db"; | ||
|
||
public static void saveTabState(List<MegaMekLabMainUI> editors) throws IOException { | ||
var dir = getTabStateDirectory(); | ||
|
||
var clean = new File(dir, TAB_STATE_CLEAN); | ||
if (clean.exists()) { | ||
if (!clean.delete()) { | ||
throw new IOException("Could not delete " + clean); | ||
} | ||
} | ||
|
||
FileUtils.cleanDirectory(dir); | ||
|
||
Map<File, String> filenameAssociations = new LinkedHashMap<>(); | ||
|
||
for (var editor : editors) { | ||
File unitFile; | ||
if (editor.getEntity() instanceof Mek) { | ||
unitFile = File.createTempFile("mml_unit_", ".mtf.tmp", dir); | ||
try ( | ||
var fos = new FileOutputStream(unitFile); | ||
var ps = new PrintStream(fos) | ||
) { | ||
ps.println(((Mek) editor.getEntity()).getMtf()); | ||
} catch (Exception e) { | ||
continue; | ||
} | ||
} else { | ||
unitFile = File.createTempFile("mml_unit_", ".blk.tmp", dir); | ||
try { | ||
BLKFile.encode(unitFile.getPath(), editor.getEntity()); | ||
} catch (EntitySavingException e) { | ||
continue; | ||
} | ||
} | ||
|
||
var fileName = editor.getFileName(); | ||
if (fileName == null || fileName.isBlank()) { | ||
fileName = " "; | ||
} | ||
|
||
filenameAssociations.put(unitFile, fileName); | ||
} | ||
|
||
File filenameAssociationsFile = new File(dir, FILENAME_ASSOCIATIONS); | ||
try ( | ||
var fos = new FileOutputStream(filenameAssociationsFile); | ||
var ps = new PrintStream(fos) | ||
) { | ||
for (var entry : filenameAssociations.entrySet()) { | ||
ps.print(entry.getKey().getPath() + '\0' + entry.getValue() + '\0'); | ||
} | ||
} | ||
|
||
clean.createNewFile(); | ||
Check notice Code scanning / CodeQL Ignored error status of call Note
Method saveTabState ignores exceptional return value of File.createNewFile.
|
||
} | ||
|
||
public static List<MegaMekLabMainUI> loadTabState() throws IOException { | ||
var dir = getTabStateDirectory(); | ||
List<MegaMekLabMainUI> editors = new ArrayList<>(); | ||
|
||
var clean = new File(dir, TAB_STATE_CLEAN); | ||
if (!clean.exists()) { | ||
return editors; | ||
} | ||
|
||
var db = new File(dir, FILENAME_ASSOCIATIONS); | ||
if (!db.exists()) { | ||
return editors; | ||
} | ||
|
||
var parts = Files.readString(Paths.get(db.getAbsolutePath())).split(Pattern.quote("\0")); | ||
for (int i = 0; i < parts.length; i += 2) { | ||
var entityFile = new File(parts[i]); | ||
|
||
var newFile = new File(entityFile.getAbsolutePath().replaceFirst("\\.tmp$", "")); | ||
FileUtils.copyFile(entityFile, newFile); | ||
|
||
var fileName = parts[i + 1]; | ||
Check failure Code scanning / CodeQL Array index out of bounds Error
This array access might be out of bounds, as the index might be equal to the array length.
|
||
if (fileName.isBlank()) { | ||
fileName = ""; | ||
} | ||
|
||
try { | ||
Entity loadedUnit = new MekFileParser(newFile).getEntity(); | ||
newFile.delete(); | ||
var editor = UiLoader.getUI(UnitUtil.getEditorTypeForEntity(loadedUnit), loadedUnit.isPrimitive(), loadedUnit.isIndustrialMek()); | ||
editor.setEntity(loadedUnit); | ||
editor.setFileName(fileName); | ||
editor.reloadTabs(); | ||
editor.refreshAll(); | ||
editors.add(editor); | ||
} catch (EntityLoadingException ignored) {} | ||
} | ||
|
||
clean.delete(); | ||
|
||
return editors; | ||
} | ||
|
||
private static File getTabStateDirectory() throws IOException { | ||
var userDirString = PreferenceManager.getClientPreferences().getUserDir(); | ||
if (userDirString == null || userDirString.isBlank()) { | ||
userDirString = "."; | ||
} | ||
|
||
var userDir = new File(userDirString); | ||
|
||
if (!userDir.isDirectory()) { | ||
throw new IOException("User dir is not a directory: " + userDirString); | ||
} | ||
|
||
var tabStateDir = new File(userDir, TAB_STATE_DIRECTORY); | ||
if (!tabStateDir.isDirectory()) { | ||
if (!tabStateDir.mkdir()) { | ||
throw new IOException("Could not create tab state directory: " + tabStateDir); | ||
} else { | ||
Files.setAttribute(Paths.get(tabStateDir.getAbsolutePath()), "dos:hidden", true); | ||
} | ||
} | ||
|
||
return tabStateDir; | ||
} | ||
|
||
private TabStateUtil() {} | ||
} |