Skip to content

Commit 9ea1528

Browse files
authored
Fix for possibly Arabic digits and other weird player filename combos. (#832)
1 parent 6d0d4f2 commit 9ea1528

3 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/api/java/com/ldtteam/structurize/api/util/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.Network;
1010
import com.ldtteam.structurize.api.util.Shape;
11+
import com.ldtteam.structurize.api.util.Utils;
1112
import com.ldtteam.structurize.api.util.constant.Constants;
1213
import com.ldtteam.structurize.blueprints.v1.Blueprint;
1314
import com.ldtteam.structurize.blueprints.v1.BlueprintUtil;
@@ -327,13 +328,13 @@ protected void handlePlacement(final BuildToolPlacementMessage.HandlerType type,
327328
final BlueprintPreviewData previewData = RenderingCache.getOrCreateBlueprintPreviewData("shapes");
328329
if (previewData.getBlueprint() != null)
329330
{
330-
final String packName = Minecraft.getInstance().getUser().getName();
331+
final String packName = Utils.getSafePackName(Minecraft.getInstance().getUser().getName());
331332
final Path subpath = Path.of(
332333
SHAPES_FOLDER,
333334
shape.toString().toLowerCase(Locale.ROOT),
334335
mainBlock.getItem().toString().replace(':', '_'),
335336
secondaryBlock.getItem().toString().replace(':', '_'),
336-
String.format("%dx%dx%dx%d_%c.blueprint", length, width, height, frequency, hollow ? 'h' : 'f'));
337+
String.format("%sx%sx%sx%s_%c.blueprint", length, width, height, frequency, hollow ? 'h' : 'f'));
337338
final Path path = Minecraft.getInstance().gameDirectory.toPath()
338339
.resolve(BLUEPRINT_FOLDER)
339340
.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
@@ -5,6 +5,7 @@
55
import com.ldtteam.structurize.Network;
66
import com.ldtteam.structurize.Structurize;
77
import com.ldtteam.structurize.api.util.Log;
8+
import com.ldtteam.structurize.api.util.Utils;
89
import com.ldtteam.structurize.api.util.constant.Constants;
910
import com.ldtteam.structurize.network.messages.NotifyServerAboutStructurePacksMessage;
1011
import com.ldtteam.structurize.network.messages.SyncSettingsToServer;
@@ -102,7 +103,7 @@ public static void onClientLoading()
102103
Files.createDirectory(outputPath);
103104
}
104105

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

0 commit comments

Comments
 (0)