From 2db90408fe082a38c276c4bacb0b66fc86ae238a Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 16 Apr 2023 12:07:57 -0400 Subject: [PATCH] Use schematic/map names for filenames --- core/src/mindustry/game/Schematics.java | 15 ++++++++++++++- core/src/mindustry/maps/Maps.java | 22 ++++++++++++++-------- gradle.properties | 2 +- 3 files changed, 29 insertions(+), 10 deletions(-) diff --git a/core/src/mindustry/game/Schematics.java b/core/src/mindustry/game/Schematics.java index ca12b2668b..118fdaf14f 100644 --- a/core/src/mindustry/game/Schematics.java +++ b/core/src/mindustry/game/Schematics.java @@ -320,11 +320,24 @@ public class Schematics implements Loadable{ return block.size + maxLoadoutSchematicPad*2; } + Fi findFile(String schematicName){ + if(schematicName.isEmpty()) schematicName = "empty"; + Fi result = null; + int index = 0; + + while(result == null || result.exists()){ + result = schematicDirectory.child(schematicName + (index == 0 ? "" : "_" + index) + "." + schematicExtension); + index ++; + } + + return result; + } + /** Adds a schematic to the list, also copying it into the files.*/ public void add(Schematic schematic){ all.add(schematic); try{ - Fi file = schematicDirectory.child(Time.millis() + "." + schematicExtension); + Fi file = findFile(Strings.sanitizeFilename(schematic.name())); write(schematic, file); schematic.file = file; }catch(Exception e){ diff --git a/core/src/mindustry/maps/Maps.java b/core/src/mindustry/maps/Maps.java index 6b23e518f9..631ee13e6b 100644 --- a/core/src/mindustry/maps/Maps.java +++ b/core/src/mindustry/maps/Maps.java @@ -190,7 +190,7 @@ public class Maps{ maps.remove(other); file = other.file; }else{ - file = findFile(); + file = findFile(name); } //create map, write it, etc etc etc @@ -239,7 +239,7 @@ public class Maps{ /** Import a map, then save it. This updates all values and stored data necessary. */ public void importMap(Fi file) throws IOException{ - Fi dest = findFile(); + Fi dest = findFile(file.name()); file.copyTo(dest); Map map = loadMap(dest, true); @@ -429,13 +429,19 @@ public class Maps{ } /** Find a new filename to put a map to. */ - private Fi findFile(){ - //find a map name that isn't used. - int i = maps.size; - while(customMapDirectory.child("map_" + i + "." + mapExtension).exists()){ - i++; + private Fi findFile(String unsanitizedName){ + String name = Strings.sanitizeFilename(unsanitizedName); + if(name.isEmpty()) name = "blank"; + + Fi result = null; + int index = 0; + + while(result == null || result.exists()){ + result = customMapDirectory.child(name + (index == 0 ? "" : "_" + index) + "." + mapExtension); + index ++; } - return customMapDirectory.child("map_" + i + "." + mapExtension); + + return result; } private Map loadMap(Fi file, boolean custom) throws IOException{ diff --git a/gradle.properties b/gradle.properties index 2fad05d87a..3db1bc9531 100644 --- a/gradle.properties +++ b/gradle.properties @@ -25,4 +25,4 @@ org.gradle.caching=true #used for slow jitpack builds; TODO see if this actually works org.gradle.internal.http.socketTimeout=100000 org.gradle.internal.http.connectionTimeout=100000 -archash=2ef12ecfa1 +archash=56c0bbf95f