feat: add renames

This commit is contained in:
LordMZTE 2022-10-31 22:08:43 +01:00
parent 3725cffb90
commit 42b1e8e358
Signed by: LordMZTE
GPG Key ID: B64802DC33A64FF6
3 changed files with 62 additions and 6 deletions

View File

@ -1,4 +1,9 @@
defmodule PortingTools.Mappings.Agent do
@moduledoc """
This module manages and loads the mappings
and renames.
"""
use Agent, restart: :transient
require Logger
@ -6,22 +11,64 @@ defmodule PortingTools.Mappings.Agent do
Agent.start_link(&init/0, name: __MODULE__)
end
defmodule PortingTools.Mappings.Agent.State do
defstruct [:mappings, :renames]
end
def init() do
Logger.info("Reading mappings")
%PortingTools.Mappings.Agent.State{
mappings: load_mappings(),
renames: load_renames()
}
end
def load_mappings() 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)
Map.put(map, String.trim(remapped), String.trim(orig))
end)
Logger.info("Done reading mappings")
Logger.info("read #{map_size(mappings)} mappings")
mappings
end
def load_renames() do
Logger.info("reading renames")
renames =
File.stream!("renames.csv")
|> Stream.map(&String.split(&1, ","))
|> Enum.reduce(%{}, fn [old, new | _], map ->
Map.put(map, String.trim(old), String.trim(new))
end)
Logger.info("read #{map_size(renames)} renames")
renames
end
def map(key) do
Agent.get(__MODULE__, &Map.get(&1, key))
mapping = Agent.get(__MODULE__, &Map.get(&1.mappings, key))
case mapping do
nil ->
Logger.warn("tried to map unknown symbol")
nil
m ->
case Agent.get(__MODULE__, &Map.get(&1.renames, m)) do
nil ->
m
r ->
Logger.info("found rename for #{m} -> #{r}")
r
end
end
end
end

View File

@ -7,7 +7,7 @@ defmodule PortingTools.ResourceLoc do
end
def init(_init_arg) do
Logger.info("Initializing Resourceloc server @ #{__MODULE__}")
Logger.info("initializing Resourceloc server @ #{__MODULE__}")
{:ok, nil}
end
@ -16,7 +16,7 @@ defmodule PortingTools.ResourceLoc do
end
def handle_call({:resolve, str}, _from, state) do
Logger.info("Got resolve request")
Logger.info("got ResourceLoc resolve request")
try do
["mods", mod | rest] =

9
renames.csv Normal file
View File

@ -0,0 +1,9 @@
getBlockTileEntity,getTileEntity
fontRenderer,fontRendererObj
getInvName,getInventoryName
tagAt,getCompoundTagAt
isInvNameLocalized,hasCustomInventoryName
getTexture,bindTexture
bindTextureByName,bindTexture
username,getDisplayName()
getLastAttackingEntity,getLastAttacker
1 getBlockTileEntity getTileEntity
2 fontRenderer fontRendererObj
3 getInvName getInventoryName
4 tagAt getCompoundTagAt
5 isInvNameLocalized hasCustomInventoryName
6 getTexture bindTexture
7 bindTextureByName bindTexture
8 username getDisplayName()
9 getLastAttackingEntity getLastAttacker