From 9a926d524eaea85441f10cc8807b9ebb031bbcb2 Mon Sep 17 00:00:00 2001 From: Kodi Craft Date: Fri, 11 Oct 2024 19:56:06 +0200 Subject: [PATCH] Implement generating data files --- apps/amethyst/lib/datagen.ex | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/apps/amethyst/lib/datagen.ex b/apps/amethyst/lib/datagen.ex index 2452af9..fcfdacf 100644 --- a/apps/amethyst/lib/datagen.ex +++ b/apps/amethyst/lib/datagen.ex @@ -51,7 +51,7 @@ defmodule Amethyst.DataGenerator do Logger.debug("Checking if server jar exists at #{path} with hash #{sha1}") case File.read(path) do {:ok, data} -> - if :crypto.hash(:sha, data) == sha1 do + if :crypto.hash(:sha, data) |> Base.encode16() |> String.downcase() == sha1 do Logger.debug("Using cached server jar at #{path}") {:ok, path} else @@ -64,6 +64,17 @@ defmodule Amethyst.DataGenerator do end end + @spec generate_data_files(String.t(), out_path, nil | String.t()) :: {:error, pos_integer()} | {:ok, out_path} when out_path: String.t() + def generate_data_files(jar_path, out_path, java_bin \\ "java") do + Logger.debug("Generating data files from server jar at #{jar_path}") + # mkdir the output directory since we cd into it + File.mkdir_p!(out_path) + case System.cmd(java_bin, ["-DbundlerMainClass=net.minecraft.data.Main", "-jar", jar_path, "--all", "--output", out_path], cd: out_path) do + {_, 0} -> {:ok, out_path} + {_, code} -> {:error, code} + end + end + defp download_jar(url, path) do Logger.debug("Downloading server jar from #{url}") case Req.get(url, into: File.stream!(path)) do