rom: extract pokemon list

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2021-11-24 22:18:34 +01:00
parent e70a19ec0e
commit efc5128890
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2
2 changed files with 48 additions and 1 deletions

View file

@ -7,3 +7,4 @@ license = "AGPL-3.0-or-later"
[build-dependencies] [build-dependencies]
quote = "1.0" quote = "1.0"
pkstrings = "0.1.0"

View file

@ -21,10 +21,12 @@ use std::error::Error as StdError;
use std::path::Path; use std::path::Path;
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use pkstrings::{self, PKString};
#[derive(Debug, Eq, PartialEq)] #[derive(Debug, Eq, PartialEq)]
pub enum Error { pub enum Error {
EnvVariableError(env::VarError), EnvVariableError(env::VarError),
PKStringError(pkstrings::Error),
ROMDoesntExist, ROMDoesntExist,
UnableToOpenROM, UnableToOpenROM,
} }
@ -35,6 +37,7 @@ impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
match self { match self {
Error::EnvVariableError(e) => write!(f, "POKEMON_ROM env var error: {}", e), Error::EnvVariableError(e) => write!(f, "POKEMON_ROM env var error: {}", e),
Error::PKStringError(e) => write!(f, "PKString error: {}", e),
Error::ROMDoesntExist => write!(f, "ROM doesn't exist"), Error::ROMDoesntExist => write!(f, "ROM doesn't exist"),
Error::UnableToOpenROM => write!(f, "Unable to open ROM"), Error::UnableToOpenROM => write!(f, "Unable to open ROM"),
} }
@ -47,6 +50,12 @@ impl From<env::VarError> for Error {
} }
} }
impl From<pkstrings::Error> for Error {
fn from(err: pkstrings::Error) -> Error {
Error::PKStringError(err)
}
}
fn get_rom() -> Result<Vec<u8>, Error> { fn get_rom() -> Result<Vec<u8>, Error> {
let env_romp = env::var("POKEMON_ROM")?; let env_romp = env::var("POKEMON_ROM")?;
let romp = Path::new(&env_romp); let romp = Path::new(&env_romp);
@ -61,10 +70,45 @@ fn get_rom() -> Result<Vec<u8>, Error> {
}) })
} }
/// Get pokemon names from ROM slice.
/// The data that gets in is a list of encoded strings of 10 chars each, over 255 pokemons.
fn get_pokemon_list(data: &[u8]) -> Result<Vec<String>, Error> {
let mut buffer: String = String::with_capacity(10);
let mut res: Vec<String> = Vec::with_capacity(255);
// TODO: Ensure we have the right length? a multiple of 10
for (offset, ord) in data.iter().enumerate() {
let pkstr = match PKString::try_from(&[*ord][..]) {
Ok(pkstr) => pkstr,
Err(e) => {
println!("BUFFER: {:?}", buffer);
return Err(Error::PKStringError(e));
},
};
if offset != 0 && (offset % 10) == 0 {
res.push(buffer.clone());
buffer.clear()
}
buffer.push_str(String::from(&pkstr).as_str());
}
res.push(buffer.clone());
Ok(res)
}
fn main() -> Result<(), Error> { fn main() -> Result<(), Error> {
let output = Path::new(&env::var("OUT_DIR").unwrap()).join("output.rs"); let output = Path::new(&env::var("OUT_DIR").unwrap()).join("output.rs");
let mut output = BufWriter::new(File::create(&output).unwrap()); let mut output = BufWriter::new(File::create(&output).unwrap());
let _rom: Vec<u8> = get_rom()?; let rom: Vec<u8> = get_rom()?;
let pkmn: Vec<_> = {
let start = 0x1c21e;
let end = start + 10 * 190; // Only the first 190 are valid
get_pokemon_list(&rom[start..end])?
};
let pkmn_names: Vec<_> = [ let pkmn_names: Vec<_> = [
"Rhydon", "Rhydon",
@ -80,6 +124,8 @@ fn main() -> Result<(), Error> {
pub enum PokemonNames { pub enum PokemonNames {
#(#pkmn_names),* #(#pkmn_names),*
} }
let foo = vec![#(#pkmn),*];
}; };
write!(output, "{}", tokens).unwrap(); write!(output, "{}", tokens).unwrap();