Skip to content

Commit d3a86d8

Browse files
authored
Clean up profile code (#6503)
1 parent 6b7a109 commit d3a86d8

3 files changed

Lines changed: 32 additions & 28 deletions

File tree

src/main/java/meteordevelopment/meteorclient/gui/tabs/builtin/ProfilesTab.java

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.minecraft.client.gui.screens.Screen;
2929
import net.minecraft.nbt.CompoundTag;
3030
import net.minecraft.nbt.NbtIo;
31+
import org.apache.commons.io.FilenameUtils;
3132
import org.lwjgl.BufferUtils;
3233
import org.lwjgl.PointerBuffer;
3334
import org.lwjgl.system.MemoryUtil;
@@ -41,6 +42,7 @@
4142
import java.nio.file.Path;
4243
import java.util.ArrayList;
4344
import java.util.List;
45+
import java.util.Optional;
4446

4547
import static meteordevelopment.meteorclient.MeteorClient.mc;
4648

@@ -98,12 +100,12 @@ public void initWidgets() {
98100
if (imported != null)
99101
MeteorClient.LOG.info("Successfully imported profile '{}'.", imported.name.get());
100102
reload();
101-
} catch (IOException e) {
103+
} catch (Exception e) {
102104
MeteorClient.LOG.error("Error importing profile", e);
103105
OkPrompt.create()
104106
.title("Failure importing profile")
105107
.message("There was an error importing the profile.")
106-
.message("Error: %d", e.getMessage())
108+
.message("Error: %s", e.getMessage())
107109
.dontShowAgainCheckboxVisible(false)
108110
.show();
109111
}
@@ -146,15 +148,25 @@ private Profile importProfile() throws IOException {
146148
File profileFile = new File(file);
147149

148150
CompoundTag nbt = NbtIo.read(profileFile.toPath());
151+
if (nbt == null) return null;
149152

150153
Profile p = new Profile();
151-
if (!p.name.set(nbt.getStringOr("name", profileFile.getName()))) return null;
152-
File profileFolder = p.getSafeFile();
153-
if (profileFolder == null) return null;
154+
Optional<String> parsedName = nbt.getString("name").filter(n -> !n.isEmpty());
155+
156+
if (parsedName.filter(p.name::set).isEmpty() && !p.name.set(FilenameUtils.removeExtension(profileFile.getName()))) {
157+
throw new IllegalStateException("Imported profile does not have a valid name.");
158+
}
159+
160+
File profileFolder = p.getFile().getCanonicalFile();
161+
if (!profileFolder.getParentFile().equals(Profiles.FOLDER.getCanonicalFile())) {
162+
throw new IllegalStateException("Imported profile does not have a valid location.");
163+
}
164+
154165
//noinspection ResultOfMethodCallIgnored
155166
profileFolder.mkdirs();
156-
157167
nbt.remove("name");
168+
169+
boolean valid = false;
158170
for (var entry : nbt.entrySet()) {
159171
String filename = entry.getKey();
160172
if (!filename.endsWith(".nbt")) continue;
@@ -172,9 +184,14 @@ private Profile importProfile() throws IOException {
172184
File f = new File(profileFolder, filename).getCanonicalFile();
173185
if (!f.toPath().startsWith(profileFolder.toPath())) continue;
174186

187+
valid = true;
175188
NbtIo.writeUnnamedTagWithFallback(entry.getValue(), new DataOutputStream(new FileOutputStream(f)));
176189
}
177190

191+
if (!valid) {
192+
throw new IllegalStateException("Imported file is not a profile.");
193+
}
194+
178195
Profiles.get().getAll().add(p);
179196
Profiles.get().save();
180197

@@ -215,7 +232,7 @@ public void initWidgets() {
215232

216233
WButton save = add(theme.button(isNew ? "Create" : "Save")).expandX().widget();
217234
save.action = () -> {
218-
if (profile.getSafeFile() == null) return;
235+
if (profile.name.get().isEmpty()) return;
219236

220237
if (isNew) {
221238
for (Profile p : Profiles.get()) {

src/main/java/meteordevelopment/meteorclient/systems/profiles/Profile.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Profile implements ISerializable<Profile> {
3131
public Setting<String> name = sgGeneral.add(new StringSetting.Builder()
3232
.name("name")
3333
.description("The name of the profile.")
34-
.filter(Utils::nameFilter)
34+
.filter(Utils::fileNameFilter)
3535
.build()
3636
);
3737

@@ -78,8 +78,7 @@ public Profile(Tag tag) {
7878
}
7979

8080
public void load() {
81-
File folder = getSafeFile();
82-
if (folder == null) return;
81+
File folder = getFile();
8382

8483
if (hud.get()) Hud.get().load(folder);
8584
if (macros.get()) Macros.get().load(folder);
@@ -88,8 +87,7 @@ public void load() {
8887
}
8988

9089
public void save() {
91-
File folder = getSafeFile();
92-
if (folder == null) return;
90+
File folder = getFile();
9391

9492
if (hud.get()) Hud.get().save(folder);
9593
if (macros.get()) Macros.get().save(folder);
@@ -99,8 +97,7 @@ public void save() {
9997

10098
public void delete() {
10199
try {
102-
File folder = getSafeFile();
103-
if (folder != null) FileUtils.deleteDirectory(folder);
100+
FileUtils.deleteDirectory(getFile());
104101
} catch (IOException e) {
105102
MeteorClient.LOG.error("Error deleting profile {}", name.get(), e);
106103
}
@@ -110,20 +107,6 @@ public File getFile() {
110107
return new File(Profiles.FOLDER, name.get());
111108
}
112109

113-
public File getSafeFile() {
114-
try {
115-
File folder = getFile().getCanonicalFile();
116-
File profilesFolder = Profiles.FOLDER.getCanonicalFile();
117-
118-
if (name.get().isEmpty() || !profilesFolder.equals(folder.getParentFile())) return null;
119-
120-
return folder;
121-
} catch (IOException e) {
122-
MeteorClient.LOG.error("Error resolving profile {}", name.get(), e);
123-
return null;
124-
}
125-
}
126-
127110
@Override
128111
public CompoundTag toTag() {
129112
CompoundTag tag = new CompoundTag();

src/main/java/meteordevelopment/meteorclient/utils/Utils.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,6 +672,10 @@ public static boolean nameFilter(String text, char character) {
672672
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '_' || character == '-' || character == '.' || character == ' ';
673673
}
674674

675+
public static boolean fileNameFilter(String text, char character) {
676+
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '_' || character == '-' || character == ' ';
677+
}
678+
675679
public static boolean ipFilter(String text, char character) {
676680
if (text.contains(":") && character == ':') return false;
677681
return (character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9') || character == '.' || character == '-' || character == ':';

0 commit comments

Comments
 (0)