Implement downloading server jar
All checks were successful
Build & Test / nix-build (push) Successful in 1m48s

This commit is contained in:
Kodi Craft 2024-10-10 15:46:28 +02:00
parent 827004e145
commit 02c05122d7
Signed by: kodi
GPG Key ID: 69D9EED60B242822

View File

@ -15,6 +15,8 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>.
defmodule Amethyst.DataGenerator do
require Logger
@moduledoc """
This module allows Amethyst to download the vanilla Minecraft server and generate
necessary data files.
@ -28,7 +30,12 @@ defmodule Amethyst.DataGenerator do
{:ok, %{body: %{"versions" => versions}}} ->
case Enum.find(versions, fn %{"id" => id} -> id == version end) do
nil -> {:error, :version_not_found}
version_meta -> {:ok, version_meta}
version_header ->
# Get the version manifest
case Req.get(version_header["url"]) do
{:ok, %{body: version_meta}} -> {:ok, version_meta}
{:error, err} -> {:error, err}
end
end
{:error, err} -> {:error, err}
end
@ -36,6 +43,22 @@ defmodule Amethyst.DataGenerator do
end
end
@spec download_server_jar(:latest | String.t(), path) :: {:ok, path} | {:error, term} when path: String.t()
def download_server_jar(version, path) do
case get_version_meta(version) do
{:ok, %{"downloads" => %{"server" => %{"url" => url, "sha1" => sha1}}}} ->
Logger.debug("Downloading server jar from #{url}, sha1: #{sha1}")
case Req.get(url, into: File.stream!(path)) do
{:ok, _} -> {:ok, path}
{:error, err} -> {:error, err}
end
{:error, err} -> {:error, err}
end
end
@spec get_latest_version() ::
{:error, %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
| {:ok, any()}
def get_latest_version() do
case Req.get(Application.fetch_env!(:amethyst, :mojang_game_meta)) do
{:ok, %{body: %{"latest" => %{"release" => release}}}} -> {:ok, release}