Skip to content

Commit e909e77

Browse files
authored
Fix for possibly Arabic digits and other weird player filename combos. (#836)
1 parent 8a38f26 commit e909e77

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/main/java/com/ldtteam/structurize/api/Utils.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.jetbrains.annotations.NotNull;
99

1010
import java.io.File;
11+
import java.text.Normalizer;
12+
import java.util.Locale;
1113
import java.util.Objects;
1214

1315
/**
@@ -73,4 +75,21 @@ public static boolean nbtContains(final CompoundTag originTag, final CompoundTag
7375
}
7476
return true;
7577
}
78+
79+
/**
80+
* Get a filename that's probably safe from a player name that might contain problematic characters.
81+
* @param input a player name or other possibly unsafe text.
82+
* @return the safe filename.
83+
*
84+
* This doesn't protect against Windows reserved filenames. Most servers are Linux anyway
85+
* so this only hurts SP players who will have a lot of Windows things break on them too.
86+
*/
87+
public static String getSafePackName(String input)
88+
{
89+
String s = Normalizer.normalize(input, Normalizer.Form.NFC);
90+
s = s.replaceAll("[\\\\/:*?\"<>|]", "_");
91+
s = s.replaceAll("\\p{Cntrl}", "");
92+
s = s.trim();
93+
return s;
94+
}
7695
}

src/main/java/com/ldtteam/structurize/client/gui/WindowShapeTool.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.ldtteam.blockui.views.View;
99
import com.ldtteam.structurize.api.RotationMirror;
1010
import com.ldtteam.structurize.api.Shape;
11+
import com.ldtteam.structurize.api.Utils;
1112
import com.ldtteam.structurize.api.constants.Constants;
1213
import com.ldtteam.structurize.blueprints.v1.Blueprint;
1314
import com.ldtteam.structurize.blueprints.v1.BlueprintUtil;
@@ -332,13 +333,13 @@ protected void handlePlacement(final BuildToolPlacementMessage.HandlerType type,
332333
final BlueprintPreviewData previewData = RenderingCache.getOrCreateBlueprintPreviewData("shapes");
333334
if (previewData.getBlueprint() != null)
334335
{
335-
final String packName = Minecraft.getInstance().getUser().getName();
336+
final String packName = Utils.getSafePackName(Minecraft.getInstance().getUser().getName());
336337
final Path subpath = Path.of(
337338
SHAPES_FOLDER,
338339
shape.toString().toLowerCase(Locale.ROOT),
339340
mainBlock.getItem().toString().replace(':', '_'),
340341
secondaryBlock.getItem().toString().replace(':', '_'),
341-
String.format("%dx%dx%dx%d_%c.blueprint", length, width, height, frequency, hollow ? 'h' : 'f'));
342+
String.format("%sx%sx%sx%s_%c.blueprint", length, width, height, frequency, hollow ? 'h' : 'f'));
342343
final Path path = Minecraft.getInstance().gameDirectory.toPath()
343344
.resolve(BLUEPRINT_FOLDER)
344345
.resolve(packName.toLowerCase(Locale.US))

src/main/java/com/ldtteam/structurize/storage/ClientStructurePackLoader.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.google.gson.JsonObject;
55
import com.ldtteam.structurize.Structurize;
66
import com.ldtteam.structurize.api.Log;
7+
import com.ldtteam.structurize.api.Utils;
78
import com.ldtteam.structurize.api.constants.Constants;
89
import com.ldtteam.structurize.network.messages.NotifyServerAboutStructurePacksMessage;
910
import com.ldtteam.structurize.network.messages.SyncSettingsToServer;
@@ -101,7 +102,7 @@ public static void onClientLoading()
101102
Files.createDirectory(outputPath);
102103
}
103104

104-
final Path clientPackPath = outputPath.resolve(Minecraft.getInstance().getUser().getName().toLowerCase(Locale.US));
105+
final Path clientPackPath = outputPath.resolve(Utils.getSafePackName(Minecraft.getInstance().getUser().getName()).toLowerCase(Locale.US));
105106
if (!Files.exists(clientPackPath))
106107
{
107108
Files.createDirectory(clientPackPath);
@@ -345,12 +346,12 @@ public static Path zipSlipProtect(ZipEntry zipEntry, Path targetDir) throws IOEx
345346
*/
346347
public static void handleSaveScanMessage(final CompoundTag compound, final String fileName, final HolderLookup.Provider provider)
347348
{
348-
final String packName = Minecraft.getInstance().getUser().getName().toLowerCase(Locale.US);
349-
StructurePacks.switchSelectedPack(StructurePacks.getStructurePack(Minecraft.getInstance().getUser().getName()));
349+
final String packName = Utils.getSafePackName(Minecraft.getInstance().getUser().getName());
350+
StructurePacks.switchSelectedPack(StructurePacks.getStructurePack(Utils.getSafePackName(Minecraft.getInstance().getUser().getName())));
350351
RenderingCache.getOrCreateBlueprintPreviewData("blueprint").setBlueprintFuture(
351352
StructurePacks.storeBlueprint(packName, compound, Minecraft.getInstance().gameDirectory.toPath()
352353
.resolve(BLUEPRINT_FOLDER)
353-
.resolve(Minecraft.getInstance().getUser().getName().toLowerCase(Locale.US))
354+
.resolve(packName.toLowerCase(Locale.US))
354355
.resolve(SCANS_FOLDER).resolve(fileName), provider));
355356
RenderingCache.getOrCreateBlueprintPreviewData("blueprint").setPos(null);
356357
Minecraft.getInstance().player.displayClientMessage(Component.translatable("Scan successfully saved as %s", fileName), false);

0 commit comments

Comments
 (0)