diff options
Diffstat (limited to 'src/main/java/de')
6 files changed, 117 insertions, 6 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 53c3fd3..7ab2448 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -64,6 +64,8 @@ public class CoflSky if (config == null) { config = LocalConfig.createDefaultConfig(); } + // Cache all the mods on load + WSCommandHandler.cacheMods(); } @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index ef15381..5427364 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -1,16 +1,16 @@ package de.torui.coflsky; +import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; -import de.torui.coflsky.commands.models.ChatMessageData; -import de.torui.coflsky.commands.models.FlipData; -import de.torui.coflsky.commands.models.SoundData; +import de.torui.coflsky.commands.RawCommand; +import de.torui.coflsky.commands.models.*; import de.torui.coflsky.configuration.ConfigurationManager; -import de.torui.coflsky.commands.models.TimerData; import de.torui.coflsky.handlers.EventRegistry; +import de.torui.coflsky.utils.FileUtils; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.audio.SoundHandler; @@ -23,11 +23,18 @@ import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.common.ForgeModContainer; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.ModContainer; + +import java.io.File; public class WSCommandHandler { public static transient String lastOnClickEvent; public static FlipHandler flipHandler = new FlipHandler(); + private static final ModListData modListData = new ModListData(); + private static final Gson gson = new Gson(); public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { // Entity sender = Minecraft.getMinecraft().thePlayer; @@ -54,6 +61,9 @@ public class WSCommandHandler { case Countdown: StartTimer(cmd.GetAs(new TypeToken<TimerData>() {})); break; + case GetMods: + getMods(); + break; default: break; } @@ -61,6 +71,30 @@ public class WSCommandHandler { return true; } + public static void cacheMods(){ + File modFolder = new File(Minecraft.getMinecraft().mcDataDir, "mods"); + for(File mods : modFolder.listFiles()){ + modListData.addFilename(mods.getName()); + try { + modListData.addFileHashes(FileUtils.getMD5Checksum(mods)); + } catch (Exception exception){ + // Highly less likely to happen unless something goes wrong + exception.printStackTrace(); + } + } + + for(ModContainer mod : Loader.instance().getModList()){ + modListData.addModname(mod.getName()); + modListData.addModname(mod.getModId()); + } + } + + private static void getMods(){ + // the Cofl server has asked for an mod list now let's respond with all the info + CoflSky.Wrapper.SendMessage(new RawCommand("foundMods",gson.toJson(modListData))); + } + + private static void Flip(Command<FlipData> cmd) { //handle chat message ChatMessageData[] messages = cmd.getData().Messages; diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index 7f196d4..b7dbc37 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -53,7 +53,10 @@ public enum CommandType { chatBatch, @SerializedName("uploadTab") uploadTab, - ; + @SerializedName("getMods") + GetMods; + + public static Map<CommandType,String> data; static { data = new HashMap<>(); diff --git a/src/main/java/de/torui/coflsky/commands/models/ModListData.java b/src/main/java/de/torui/coflsky/commands/models/ModListData.java new file mode 100644 index 0000000..f654ecd --- /dev/null +++ b/src/main/java/de/torui/coflsky/commands/models/ModListData.java @@ -0,0 +1,33 @@ +package de.torui.coflsky.commands.models; + +import com.google.gson.annotations.SerializedName; + +import java.util.LinkedList; +import java.util.List; + +public class ModListData { + + @SerializedName("fileNames") + private final List<String> fileNames = new LinkedList<>(); + + @SerializedName("modNames") + private final List<String> modNames = new LinkedList<>(); + + @SerializedName("fileHashes") + private final List<String> fileHashes = new LinkedList<>(); + + + public void addFilename(String name){ + this.fileNames.add(name); + } + + public void addModname(String modname){ + this.modNames.add(modname); + } + + public void addFileHashes(String hash){ + this.fileHashes.add(hash); + } + + +} diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java index f306751..12543e0 100644 --- a/src/main/java/de/torui/coflsky/network/WSClient.java +++ b/src/main/java/de/torui/coflsky/network/WSClient.java @@ -22,7 +22,6 @@ import de.torui.coflsky.commands.RawCommand; public class WSClient extends WebSocketAdapter { - public static Gson gson; diff --git a/src/main/java/de/torui/coflsky/utils/FileUtils.java b/src/main/java/de/torui/coflsky/utils/FileUtils.java new file mode 100644 index 0000000..1541a75 --- /dev/null +++ b/src/main/java/de/torui/coflsky/utils/FileUtils.java @@ -0,0 +1,40 @@ +package de.torui.coflsky.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.security.MessageDigest; + +public class FileUtils { + + public static byte[] createChecksum(File file) throws Exception { + InputStream fis = new FileInputStream(file); + + byte[] buffer = new byte[1024]; + MessageDigest complete = MessageDigest.getInstance("MD5"); + int numRead; + + do { + numRead = fis.read(buffer); + if (numRead > 0) { + complete.update(buffer, 0, numRead); + } + } while (numRead != -1); + + fis.close(); + return complete.digest(); + } + + + public static String getMD5Checksum(File file) throws Exception { + byte[] b = createChecksum(file); + String result = ""; + + for (int i=0; i < b.length; i++) { + result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); + } + return result; + } + + +} |