31 lines
850 B
Elixir
31 lines
850 B
Elixir
defmodule CobblemonUi.CobblemonFS.PartyStore do
|
|
@moduledoc """
|
|
Parses a player's party storage `.dat` file.
|
|
|
|
Party layout:
|
|
Root -> party -> slot0..slot5
|
|
"""
|
|
|
|
alias CobblemonUi.CobblemonFS.{NBT, Pokemon}
|
|
|
|
@party_slots 6
|
|
|
|
@doc """
|
|
Reads and parses a party `.dat` file at the given path.
|
|
|
|
Returns `{:ok, [pokemon | nil]}` or `{:error, reason}`.
|
|
"""
|
|
@spec parse(String.t()) :: {:ok, list(map() | nil)} | {:error, term()}
|
|
def parse(path) do
|
|
with {:ok, data} <- File.read(path),
|
|
{:ok, {_name, root}} <- NBT.decode(data) do
|
|
party = Map.get(root, "party", %{})
|
|
slots = for i <- 0..(@party_slots - 1), do: Pokemon.normalize(Map.get(party, "slot#{i}"))
|
|
{:ok, slots}
|
|
else
|
|
{:error, :enoent} -> {:error, :not_found}
|
|
{:error, reason} -> {:error, {:corrupt_data, reason}}
|
|
end
|
|
end
|
|
end
|