From 31ba2f48c77a161ce8d8e26920514b0534885851 Mon Sep 17 00:00:00 2001 From: Aeronica Date: Sun, 17 Dec 2023 19:20:40 -0600 Subject: [PATCH] Continue work to remove the H2 MVStore library. --- .../resources/assets/mxtune/lang/en_us.json | 1 + .../mods/mxtune/caches/ModDataStore.java | 41 ++++++++++++++++++- .../mods/mxtune/command/CommandMusic.java | 20 ++++++++- .../datagen/MXTuneLanguageProvider.java | 1 + 4 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/generated/resources/assets/mxtune/lang/en_us.json b/src/generated/resources/assets/mxtune/lang/en_us.json index eb9339809..cea394c7b 100644 --- a/src/generated/resources/assets/mxtune/lang/en_us.json +++ b/src/generated/resources/assets/mxtune/lang/en_us.json @@ -7,6 +7,7 @@ "chat.mxtune.groupManager.player_cannot_join_too_many": "%s cannot join since group is full", "chat.mxtune.groupManager.player_joined_the_group": "%s joined the group.", "chat.mxtune.groupManager.you_joined_players_group": "You joined %s's group.", + "commands.mxtune.music.convert": "Dump converted to %s files", "commands.mxtune.music.dump": "Wrote %s records", "commands.mxtune.music.load": "Read %s records", "config.mxtune.client.double_click_time_ms": "Double-click time in milliseconds for GUI widgets", diff --git a/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java b/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java index 62ec642e9..28606b600 100644 --- a/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java +++ b/src/main/java/aeronicamc/mods/mxtune/caches/ModDataStore.java @@ -14,12 +14,14 @@ import java.io.IOException; import java.io.PrintWriter; import java.nio.file.Files; +import java.nio.file.Path; import java.time.Duration; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeParseException; import java.util.ArrayList; import java.util.Map; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; import static aeronicamc.mods.mxtune.caches.FileHelper.*; @@ -165,6 +167,43 @@ public static int loadDumpFile() return size; } + public static int convertDumpToFiles() + { + AtomicInteger size = new AtomicInteger(0); + if (fileExists(SERVER_MUSIC_FOLDER_DUMP_FOLDER, SERVER_DATA_STORE_DUMP_FILENAME, LogicalSide.SERVER)) + { + try (Stream stream = Files.lines(getCacheFile(SERVER_MUSIC_FOLDER_DUMP_FOLDER, SERVER_DATA_STORE_DUMP_FILENAME, LogicalSide.SERVER))) + { + LOGGER.info("Convert to Dump using: {}", SERVER_FOLDER ); + stream.forEach(line -> + { + String[] pair = line.split("="); + LocalDateTime dateTime = (LocalDateTime.parse(pair[0])); + String filename = "--error--"; + try + { + filename = toSafeFileNameKey(dateTime.toString())+".txt"; + Path path = FileHelper.getCacheFile(SERVER_FOLDER, filename, LogicalSide.SERVER); + PrintWriter printWriter = new PrintWriter(path.toString(), "UTF-8"); + printWriter.println(pair[1]); + printWriter.close(); + LOGGER.info(" Created: {}", path.toString()); + size.getAndIncrement(); + } catch (IOException e) + { + LOGGER.error(" failed to create file: {}", filename, e); + } + + }); + } catch (IOException | SecurityException | DateTimeParseException e) + { + LOGGER.error(e); + } + + } else return size.get(); + return size.get(); + } + private static LocalDateTime nextKey() { LocalDateTime now; @@ -257,7 +296,7 @@ private static long reapSheetMusic(boolean whatIf) } /** - * Add add a MML format sting to the store. Returns a unique date-time string as the key to the MML. + * Add a MML format sting to the store. Returns a unique date-time string as the key to the MML. * (At least providing there are no unexpected time shifts due to incorrect time on the server/pc etc.) * @param musicText - the MML music text string to be stored. * @return a unique date-time string (GMT0) as the key to the entry, or null if the add failed. diff --git a/src/main/java/aeronicamc/mods/mxtune/command/CommandMusic.java b/src/main/java/aeronicamc/mods/mxtune/command/CommandMusic.java index 15b75310d..c19d07162 100644 --- a/src/main/java/aeronicamc/mods/mxtune/command/CommandMusic.java +++ b/src/main/java/aeronicamc/mods/mxtune/command/CommandMusic.java @@ -12,7 +12,8 @@ public class CommandMusic { return Commands.literal("music") .then(CommandMusicDump.register()) - .then(CommandMusicLoad.register()); + .then(CommandMusicLoad.register()) + .then(CommandConvert.register()); } private static class CommandMusicDump @@ -48,4 +49,21 @@ private static class CommandMusicLoad ); } } + + private static class CommandConvert + { + static ArgumentBuilder register() + { + return Commands.literal("convert") + .requires(cs->cs.hasPermission(4)) //permission + .executes(ctx -> + { + ctx.getSource().sendSuccess( + new TranslationTextComponent("commands.mxtune.music.convert", + String.format("%d", ModDataStore.convertDumpToFiles())), true); + return 0; + } + ); + } + } } diff --git a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java index f92dac8d3..d498f8ec8 100644 --- a/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java +++ b/src/main/java/aeronicamc/mods/mxtune/datagen/MXTuneLanguageProvider.java @@ -232,6 +232,7 @@ private void addCommands() { addCommand("music.dump", "Wrote %s records"); addCommand("music.load", "Read %s records"); + addCommand("music.convert", "Dump converted to %s files"); } private void addConfigs()