Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.systems.modules.misc.Notebot;
import meteordevelopment.meteorclient.utils.notebot.decoder.SongDecoders;
import net.minecraft.commands.SharedSuggestionProvider;

Expand All @@ -34,12 +34,12 @@ private NotebotSongArgumentType() {
public Path parse(StringReader reader) throws CommandSyntaxException {
final String text = reader.getRemaining();
reader.setCursor(reader.getTotalLength());
return MeteorClient.FOLDER.toPath().resolve("notebot/" + text);
return Notebot.PATH.toPath().resolve(text);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be a good idea to throw a CommandSyntaxException on path traversal here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

e.g. text contains ..?

}

@Override
public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
try (var suggestions = Files.list(MeteorClient.FOLDER.toPath().resolve("notebot"))) {
try (var suggestions = Files.list(Notebot.PATH.toPath())) {
return SharedSuggestionProvider.suggest(suggestions
.filter(SongDecoders::hasDecoder)
.map(path -> path.getFileName().toString()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public void build(LiteralArgumentBuilder<ClientSuggestionProvider> builder) {
if (name == null || name.isEmpty()) {
throw INVALID_PATH.create(name);
}
Path notebotFolder = MeteorClient.FOLDER.toPath().resolve("notebot");
Path notebotFolder = Notebot.PATH.toPath();
Path path = notebotFolder.resolve(String.format("%s.txt", name)).normalize();
if (!path.startsWith(notebotFolder)) {
throw INVALID_PATH.create(path);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package meteordevelopment.meteorclient.gui.screens;

import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.gui.GuiTheme;
import meteordevelopment.meteorclient.gui.WindowScreen;
import meteordevelopment.meteorclient.gui.widgets.containers.WTable;
Expand Down Expand Up @@ -57,8 +56,8 @@ public void initWidgets() {

private void initSongsTable() {
AtomicBoolean noSongsFound = new AtomicBoolean(true);
try {
Files.list(MeteorClient.FOLDER.toPath().resolve("notebot")).forEach(path -> {
try (var stream = Files.list(Notebot.PATH.toPath())) {
stream.forEach(path -> {
if (SongDecoders.hasDecoder(path)) {
String name = path.getFileName().toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -166,7 +167,9 @@ private Profile importProfile() throws IOException {
}

File f = new File(p.getFile(), filename);
NbtIo.writeUnnamedTagWithFallback(entry.getValue(), new DataOutputStream(new FileOutputStream(f)));
try (DataOutputStream output = new DataOutputStream(new FileOutputStream(f))) {
NbtIo.writeUnnamedTagWithFallback(entry.getValue(), output);
}
}

Profiles.get().getAll().add(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
import net.minecraft.network.protocol.game.ServerboundUseItemOnPacket;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.util.Util;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.NoteBlock;
Expand All @@ -60,6 +61,8 @@

public class Notebot extends Module {

public static final File PATH = new File(MeteorClient.FOLDER, "notebot");

private final SettingGroup sgGeneral = settings.getDefaultGroup();
private final SettingGroup sgNoteMap = settings.createGroup("Note Map", false);
private final SettingGroup sgRender = settings.createGroup("Render", true);
Expand Down Expand Up @@ -650,7 +653,7 @@ public void onSongEnd() {
}

public void playRandomSong() {
File[] files = MeteorClient.FOLDER.toPath().resolve("notebot").toFile().listFiles();
File[] files = PATH.listFiles();
if (files == null) return;

File randomSong = files[ThreadLocalRandom.current().nextInt(files.length)];
Expand Down Expand Up @@ -718,7 +721,8 @@ public boolean loadFileToMap(File file, Runnable callback) {
return false;
}

info("Loading song \"%s\".", FilenameUtils.getBaseName(file.getName()));
String baseName = FilenameUtils.getBaseName(file.getName());
info("Loading song \"%s\".", baseName);

// Start loading song
loadingSongFuture = CompletableFuture.supplyAsync(() -> {
Expand All @@ -727,16 +731,16 @@ public boolean loadFileToMap(File file, Runnable callback) {
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}, Util.nonCriticalIoPool());
loadingSongFuture.completeOnTimeout(null, 60, TimeUnit.SECONDS);

stage = Stage.LoadingSong;
long time1 = System.currentTimeMillis();
loadingSongFuture.whenComplete((song, ex) -> {
loadingSongFuture.whenCompleteAsync((song, ex) -> {
if (ex == null) {
// Song is null only when it times out
if (song == null) {
error("Loading song '" + FilenameUtils.getBaseName(file.getName()) + "' timed out.");
error("Loading song '" + baseName + "' timed out.");
onSongEnd();
return;
}
Expand All @@ -745,18 +749,18 @@ public boolean loadFileToMap(File file, Runnable callback) {
long time2 = System.currentTimeMillis();
long diff = time2 - time1;

info("Song '" + FilenameUtils.getBaseName(file.getName()) + "' has been loaded to the memory! Took " + diff + "ms");
info("Song '" + baseName + "' has been loaded to the memory! Took " + diff + "ms");
callback.run();
} else {
if (ex instanceof CancellationException) {
error("Loading song '" + FilenameUtils.getBaseName(file.getName()) + "' was cancelled.");
error("Loading song '" + baseName + "' was cancelled.");
} else {
error("An error occurred while loading song '" + FilenameUtils.getBaseName(file.getName()) + "'. See the logs for more details");
MeteorClient.LOG.error("An error occurred while loading song '{}'", FilenameUtils.getBaseName(file.getName()), ex);
error("An error occurred while loading song '" + baseName + "'. See the logs for more details");
MeteorClient.LOG.error("An error occurred while loading song '{}'", baseName, ex);
onSongEnd();
}
}
});
}, mc);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,8 +346,7 @@ private void load() {
// Try to load csv
file = getCsvFile();
if (!loaded && file.exists()) {
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
reader.readLine();

String line;
Expand All @@ -364,8 +363,6 @@ private void load() {

chunks.add(chunk);
}

reader.close();
} catch (Exception _) {
if (chunks == null) chunks = new ArrayList<>();
}
Expand Down