Skip to content
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

Restrict user dir loading to the correct subfolders #6757

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
28 changes: 17 additions & 11 deletions megamek/docs/UserDirHelp.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,25 @@

<P>How to place files within the user data directory:</P>
<UL>
<LI><B>Fonts</B> can be placed anywhere in the user data directory (i.e., in the directory itself or in any subfolder)
<LI><B>Fonts</B> must be placed in &lt;user data directory&gt;/data/fonts/. In this subfolder, further subfolders can be used (optional).

<LI><B>Units</B> (.mtf and .blk files) can be placed anywhere in the user data directory
<LI><B>Units</B> (.mtf and .blk files) must be placed in &lt;user data directory&gt;/data/mekfiles/. In this subfolder, further subfolders can be used (optional).

<LI><B>Camo</B> images must be placed in &lt;user data directory&gt;/data/images/camo/. In this subfolder, further subfolders can be used (optional).

<LI><B>Portrait</B> images must be placed in &lt;user data directory&gt;/data/images/portraits/. In this subfolder, further subfolders can be used (optional).

<LI><B>Unit fluff</B> images must be placed in &lt;user data directory&gt;/data/images/fluff/&lt;unit type&gt;/. To find the exact subfolders to use, go to your megamek folder and open /data/images/fluff/.

<LI><B>Skin</B> definition files (.xml) can be placed anywhere in the user data directory
<LI><B>Skin</B> definition files (.xml) must be placed in &lt;user data directory&gt;/mmconf/skins/. In this subfolder, further subfolders can be used (optional).

<LI><B>Rank Systems</B> may be defined in &lt;user data directory&gt;/data/universe/ranks.xml
<LI><B>MM Scenario</B> files (.mms) must be placed in &lt;user data directory&gt;/data/scenarios/. In this subfolder, further subfolders can be used (optional).

<LI><B>Award</B> definition files (.xml) can be placed in &lt;user data directory&gt;/data/universe/awards
<LI><B>Rank Systems</B> may be defined in &lt;user data directory&gt;/data/universe/ranks.xml.

<LI><B>Award</B> definition files (.xml) can be placed in &lt;user data directory&gt;/data/universe/awards/.

<LI><B>Force Icon</B> images can be placed in &lt;user data directory&gt;/data/images/force/.
</UL>

<P>
Expand All @@ -33,13 +37,9 @@
<P>This is an example of a suitable directory structure with a few example files:
<BR><BR>
D:/myBTStuff<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Oxanium.ttf<BR>
&nbsp;&nbsp;&nbsp;&nbsp;Exo.ttf<BR>
&nbsp;&nbsp;&nbsp;&nbsp;campaign_units/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Atlas AS8-XT.mtf<BR>
&nbsp;&nbsp;&nbsp;&nbsp;data/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Jura.ttf<BR>
MyMMSkin.xml<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mekfiles/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Atlas AS8-XT.mtf<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;images/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;camo/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myForceCamo.png<BR>
Expand All @@ -53,11 +53,17 @@
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb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sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myskin.xml<BR>
&nbsp;&nbsp;&nbsp;&nbsp;.mml_tmp/<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Here be dragons!<BR>
</P>
Expand Down
8 changes: 4 additions & 4 deletions megamek/src/megamek/client/ui/swing/CommonSettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -2111,10 +2111,10 @@ public void setVisible(boolean visible) {
skinFiles.removeAllItems();
ArrayList<String> xmlFiles = new ArrayList<>(filteredFiles(Configuration.skinsDir(), ".xml"));

String userDirName = PreferenceManager.getClientPreferences().getUserDir();
File userDir = new File(userDirName);
if (!userDirName.isBlank() && userDir.isDirectory()) {
xmlFiles.addAll(filteredFilesWithSubDirs(userDir, ".xml"));
String userDir = PreferenceManager.getClientPreferences().getUserDir();
if (!userDir.isBlank()) {
File subDir = new File(userDir, Configuration.skinsDir().toString());
xmlFiles.addAll(filteredFilesWithSubDirs(subDir, ".xml"));
}

File internalUserDataDir = new File(Configuration.userdataDir(), Configuration.skinsDir().toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ private void updateSkinCombo(String selected) {
currSkinCombo.removeAllItems();
List<String> xmlFiles = new ArrayList<>(CommonSettingsDialog.filteredFiles(Configuration.skinsDir(), ".xml"));

String userDirName = PreferenceManager.getClientPreferences().getUserDir();
File userDir = new File(userDirName);
if (!userDirName.isBlank() && userDir.isDirectory()) {
xmlFiles.addAll(CommonSettingsDialog.filteredFilesWithSubDirs(userDir, ".xml"));
String userDir = PreferenceManager.getClientPreferences().getUserDir();
if (!userDir.isBlank()) {
File subDir = new File(userDir, Configuration.skinsDir().toString());
xmlFiles.addAll(CommonSettingsDialog.filteredFilesWithSubDirs(subDir, ".xml"));
}

xmlFiles.removeIf(file -> !SkinXMLHandler.validSkinSpecFile(file));
Expand Down Expand Up @@ -372,7 +372,7 @@ public void actionPerformed(ActionEvent e) {

private String saveDialog() {
String userDirName = PreferenceManager.getClientPreferences().getUserDir();
File userDir = new File(userDirName);
File userDir = new File(userDirName, Configuration.skinsDir().toString());
String path;
if (!userDirName.isBlank() && userDir.isDirectory()) {
path = userDir.getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private static void initializePortraits() {
new ImageFileFactory());

String userDir = PreferenceManager.getClientPreferences().getUserDir();
File portraitUserDir = new File(userDir + "/" + Configuration.portraitImagesDir());
File portraitUserDir = new File(userDir, Configuration.portraitImagesDir().toString());
if (!userDir.isBlank() && portraitUserDir.isDirectory()) {
DirectoryItems userDirPortraits = new DirectoryItems(portraitUserDir, new ImageFileFactory());
portraitDirectory.merge(userDirPortraits);
Expand Down Expand Up @@ -123,7 +123,7 @@ private static void initializeCamouflage() {
new ScaledImageFileFactory());

String userDir = PreferenceManager.getClientPreferences().getUserDir();
File camoUserDir = new File(userDir + "/" + Configuration.camoDir());
File camoUserDir = new File(userDir, Configuration.camoDir().toString());
if (!userDir.isBlank() && camoUserDir.isDirectory()) {
DirectoryItems userDirCamo = new DirectoryItems(camoUserDir, new ScaledImageFileFactory());
camouflageDirectory.merge(userDirCamo);
Expand Down
6 changes: 4 additions & 2 deletions megamek/src/megamek/client/ui/swing/util/FontHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import megamek.MMConstants;
import megamek.client.ui.swing.CommonSettingsDialog;
import megamek.common.Configuration;
import megamek.common.preference.PreferenceManager;
import megamek.logging.MMLogger;

Expand Down Expand Up @@ -116,8 +117,9 @@ private void initializeFonts() {

String userDir = PreferenceManager.getClientPreferences().getUserDir();
if (!userDir.isBlank()) {
logger.info("Loading fonts from {}", userDir);
parseFontsInDirectory(userDir);
File subDir = new File(userDir, Configuration.fontsDir().toString());
logger.info("Loading fonts from {}", subDir);
parseFontsInDirectory(subDir);
}

logger.info("Loading fonts from Java's GraphicsEnvironment");
Expand Down
2 changes: 1 addition & 1 deletion megamek/src/megamek/common/MekSummaryCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ private void checkForChanges(boolean ignoreUnofficial, Vector<MekSummary> vMeks,

// load units from the external user data dir
String userDir = PreferenceManager.getClientPreferences().getUserDir();
File userDataUnits2 = new File(userDir, "");
File userDataUnits2 = new File(userDir, Configuration.unitsDir().toString());
if (!userDir.isBlank() && userDataUnits2.isDirectory()) {
bNeedsUpdate |= loadMeksFromDirectory(vMeks, sKnownFiles, lLastCheck, userDataUnits2, false);
}
Expand Down
2 changes: 2 additions & 0 deletions megamek/src/megamek/common/preference/ClientPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Locale;
import java.util.Objects;

public class ClientPreferences extends PreferenceStoreProxy {
private static final MMLogger logger = MMLogger.create(ClientPreferences.class);
Expand Down Expand Up @@ -464,6 +465,7 @@ public String getUserDir() {
}

public void setUserDir(String userDir) {
userDir = Objects.requireNonNullElse(userDir, "");
// remove directory separators at the end
while (!userDir.isBlank() && (userDir.endsWith("/") || userDir.endsWith("\\"))) {
userDir = userDir.substring(0, userDir.length() - 1);
Expand Down