This commit is contained in:
LordMZTE 2022-10-31 10:48:35 +01:00
commit 3725cffb90
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
14 changed files with 9596 additions and 0 deletions

4
.formatter.exs Normal file
View File

@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]

29
.gitignore vendored Normal file
View File

@ -0,0 +1,29 @@
# The directory Mix will write compiled artifacts to.
_build/
# If you run "mix test --cover", coverage assets end up here.
cover/
# The directory Mix downloads your dependencies sources to.
deps/
# Where third-party dependencies like ExDoc output generated docs.
doc/
# Ignore .fetch files in case you like to edit your project deps locally.
.fetch
# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
# Also ignore archive artifacts (built via "mix archive.build").
*.ez
# Ignore package tarball (built via "mix hex.build").
portingtools-*.tar
# Temporary files, for example, from tests.
tmp/
# Language Server
.elixir_ls/

12
README.md Normal file
View File

@ -0,0 +1,12 @@
# Portingtools
This tool is meant to make porting mods easier for MineteckReloaded.
Also, it helped me learn some elixir, so don't expect good code.
## Installation
- `MIX_ENV=prod mix release`
- start the server with `_build/prod/rel/portingtools/bin/portingtools start`
- map stuff with `./client.sh map`
- resolve ResourceLocations with `./client.sh resourceloc`

2
client.sh Executable file
View File

@ -0,0 +1,2 @@
#!/usr/bin/sh
exec "$(dirname "$(realpath "$0")")"/_build/prod/rel/portingtools/bin/portingtools rpc "Cli.call(~s<$1>)"

31
lib/cli.ex Normal file
View File

@ -0,0 +1,31 @@
defmodule Cli do
def call(command) do
case command do
"map" ->
input()
|> PortingTools.Mappings.map()
|> output(:map)
"resourceloc" ->
input()
|> PortingTools.ResourceLoc.resolve()
|> output(:map)
_ ->
raise "Invalid command!"
end
end
def input() do
IO.gets(nil) |> String.trim()
end
def output(response, type) do
case {response, type} do
{s, _} when is_binary(s) -> s
{nil, :map} -> "<unknown>"
{nil, :resourceloc} -> "<invalid>"
end
|> IO.puts()
end
end

View File

@ -0,0 +1,18 @@
defmodule PortingTools.Application do
use Application
@impl true
def start(_type, _args) do
#{:ok, _} = Node.start(:"portingtools@localhost", :shortnames)
#Node.set_cookie(Node.self(), :ptools)
children = [
PortingTools.ResourceLoc,
PortingTools.Mappings,
PortingTools.Mappings.Agent,
]
opts = [strategy: :one_for_one, name: PortingTools.Supervisor]
Supervisor.start_link(children, opts)
end
end

View File

@ -0,0 +1,22 @@
defmodule PortingTools.Mappings do
use GenServer, restart: :transient
require Logger
def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
def init(_init_arg) do
Logger.info("Initilizing mappings server @ #{__MODULE__}")
{:ok, nil}
end
def map(str) do
GenServer.call(__MODULE__, {:map, str})
end
def handle_call({:map, str}, _from, state) do
Logger.info("Got map request to map #{str}")
{:reply, PortingTools.Mappings.Agent.map(str), state}
end
end

View File

@ -0,0 +1,27 @@
defmodule PortingTools.Mappings.Agent do
use Agent, restart: :transient
require Logger
def start_link(_) do
Agent.start_link(&init/0, name: __MODULE__)
end
def init() do
Logger.info("Reading mappings")
mappings =
File.ls!("mappings")
|> Stream.flat_map(&File.stream!("mappings/#{&1}"))
|> Stream.map(&String.split(&1, ","))
|> Enum.reduce(%{}, fn [remapped, orig | _], map ->
Map.put(map, remapped, orig)
end)
Logger.info("Done reading mappings")
mappings
end
def map(key) do
Agent.get(__MODULE__, &Map.get(&1, key))
end
end

View File

@ -0,0 +1,32 @@
defmodule PortingTools.ResourceLoc do
use GenServer, restart: :transient
require Logger
def start_link(_) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end
def init(_init_arg) do
Logger.info("Initializing Resourceloc server @ #{__MODULE__}")
{:ok, nil}
end
def resolve(str) do
GenServer.call(__MODULE__, {:resolve, str})
end
def handle_call({:resolve, str}, _from, state) do
Logger.info("Got resolve request")
try do
["mods", mod | rest] =
String.trim(str)
|> String.trim(<<?">>)
|> String.split("/", trim: true)
{:reply, ~s<new ResourceLocation("#{mod}", "#{Enum.join(rest, "/")}")>, state}
rescue
MatchError -> {:reply, nil, state}
end
end
end

4926
mappings/fields.csv Normal file

File diff suppressed because it is too large Load Diff

4455
mappings/methods.csv Normal file

File diff suppressed because it is too large Load Diff

29
mix.exs Normal file
View File

@ -0,0 +1,29 @@
defmodule PortingTools.MixProject do
use Mix.Project
def project do
[
app: :portingtools,
version: "0.1.0",
elixir: "~> 1.14",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end
# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger],
mod: {PortingTools.Application, []}
]
end
# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end

View File

@ -0,0 +1,8 @@
defmodule PortingtoolsTest do
use ExUnit.Case
doctest Portingtools
test "greets the world" do
assert Portingtools.hello() == :world
end
end

1
test/test_helper.exs Normal file
View File

@ -0,0 +1 @@
ExUnit.start()