diff --git a/apps/amethyst/lib/datagen.ex b/apps/amethyst/lib/datagen.ex new file mode 100644 index 0000000..3ecf060 --- /dev/null +++ b/apps/amethyst/lib/datagen.ex @@ -0,0 +1,45 @@ +# Amethyst - An experimental Minecraft server written in Elixir. +# Copyright (C) 2024 KodiCraft +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +defmodule Amethyst.DataGenerator do + @moduledoc """ + This module allows Amethyst to download the vanilla Minecraft server and generate + necessary data files. + """ + @spec get_version_meta(String.t() | :latest) :: {:ok, map()} | {:error, term} + def get_version_meta(version) do + version = if version == :latest, do: get_latest_version(), else: {:ok, version} + case version do + {:ok, version} -> + case Req.get(Application.fetch_env!(:amethyst, :mojang_game_meta)) 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} + end + {:error, err} -> {:error, err} + end + {:error, err} -> {:error, err} + end + end + + def get_latest_version() do + case Req.get(Application.fetch_env!(:amethyst, :mojang_game_meta)) do + {:ok, %{body: %{"latest" => %{"release" => release}}}} -> {:ok, release} + {:error, err} -> {:error, err} + end + end +end diff --git a/config/config.exs b/config/config.exs index 27de4af..fb1393a 100644 --- a/config/config.exs +++ b/config/config.exs @@ -12,3 +12,7 @@ import Config level: :debug, format: "$date $time [$level] $metadata$message\n", metadata: [] + + config :amethyst, + mojang_game_meta: "https://piston-meta.mojang.com/mc/game/version_manifest_v2.json", + session_server: "https://sessionserver.mojang.com" diff --git a/config/runtime.exs b/config/runtime.exs index 9ad42e1..640bae0 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -4,7 +4,6 @@ config :amethyst, port: 25599, # Bogus port for testing, avoids unexpected conflicts encryption: true, # Whether or not to request encryption from clients. auth: true, # Whether or not users should be authenticated with Mojang. - session_server: "https://sessionserver.mojang.com", # Base URL to the Mojang session server compression: 256, # Packets larger than this amount are sent compressed. Set to nil to disable compression. default_game: Example.Game, # Which game new players should be sent to release: config_env() == :prod # If this is set to false, Amethyst will perform additional checks at runtime and will handle errors more loosely