A bunch of fixes and improvements
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
This commit is contained in:
parent
7bcfd92d45
commit
a4fd112a07
|
@ -51,12 +51,12 @@ rand_pcg = "0.3.1"
|
||||||
hashbrown = "0.12.1"
|
hashbrown = "0.12.1"
|
||||||
indexmap = "1.8.2"
|
indexmap = "1.8.2"
|
||||||
parking_lot = "0.12.1"
|
parking_lot = "0.12.1"
|
||||||
conquer-once = "0.3.2"
|
|
||||||
serde = { version = "1.0.137", optional = true, features = ["derive"] }
|
serde = { version = "1.0.137", optional = true, features = ["derive"] }
|
||||||
wasmer = { version = "3.0.0-beta", optional = true, default-features = true }
|
wasmer = { version = "3.0.0-beta", optional = true, default-features = true }
|
||||||
unique-type-id = { version = "1.0.0", optional = true }
|
unique-type-id = { version = "1.0.0", optional = true }
|
||||||
unique-type-id-derive = { version = "1.0.0", optional = true }
|
unique-type-id-derive = { version = "1.0.0", optional = true }
|
||||||
paste = { version = "1.0.8" }
|
paste = { version = "1.0.8" }
|
||||||
|
arcstr = "1.1.4"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
csv = "1.1.6"
|
csv = "1.1.6"
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
Main lifetimes:
|
|
||||||
- Library: the static data underlying everything. This has the longest lifetime.
|
|
||||||
- Pokemon: The lifetime of a Pokemon.
|
|
||||||
- Party: The lifetime of a party, as a Pokemon can be added or taken from a party, this is shorter than the lifetime of a pokemon
|
|
||||||
- Battle: The lifetime of a battle.
|
|
|
@ -239,7 +239,7 @@ mod tests {
|
||||||
impl TestScript {
|
impl TestScript {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: StringKey::new("test"),
|
name: StringKey::new("test".into()),
|
||||||
is_marked_for_deletion: Default::default(),
|
is_marked_for_deletion: Default::default(),
|
||||||
suppressed_count: AtomicUsize::new(0),
|
suppressed_count: AtomicUsize::new(0),
|
||||||
test_count: AtomicUsize::new(0),
|
test_count: AtomicUsize::new(0),
|
||||||
|
@ -247,7 +247,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
fn new_with_name(name: &str) -> Self {
|
fn new_with_name(name: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: StringKey::new(name),
|
name: StringKey::new(name.into()),
|
||||||
is_marked_for_deletion: Default::default(),
|
is_marked_for_deletion: Default::default(),
|
||||||
suppressed_count: AtomicUsize::new(0),
|
suppressed_count: AtomicUsize::new(0),
|
||||||
test_count: AtomicUsize::new(0),
|
test_count: AtomicUsize::new(0),
|
||||||
|
@ -424,7 +424,7 @@ mod tests {
|
||||||
"test_b"
|
"test_b"
|
||||||
);
|
);
|
||||||
|
|
||||||
set.remove(&StringKey::new("test_c"));
|
set.remove(&StringKey::new("test_c".into()));
|
||||||
assert!(aggregator.get_next().is_none());
|
assert!(aggregator.get_next().is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -451,7 +451,7 @@ mod tests {
|
||||||
"test_a"
|
"test_a"
|
||||||
);
|
);
|
||||||
|
|
||||||
set.remove(&StringKey::new("test_b"));
|
set.remove(&StringKey::new("test_b".into()));
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
aggregator
|
aggregator
|
||||||
|
|
|
@ -398,7 +398,7 @@ mod tests {
|
||||||
impl TestScript {
|
impl TestScript {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
name: StringKey::new("test"),
|
name: StringKey::new("test".into()),
|
||||||
container: AtomicPtr::<ScriptContainer>::default(),
|
container: AtomicPtr::<ScriptContainer>::default(),
|
||||||
suppressed_count: AtomicUsize::new(0),
|
suppressed_count: AtomicUsize::new(0),
|
||||||
marked_for_deletion: Default::default(),
|
marked_for_deletion: Default::default(),
|
||||||
|
@ -539,7 +539,7 @@ mod tests {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
container.script.read().as_ref().unwrap().name(),
|
container.script.read().as_ref().unwrap().name(),
|
||||||
&StringKey::new("script2")
|
&StringKey::new("script2".into())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#![feature(is_some_with)]
|
#![feature(is_some_with)]
|
||||||
#![feature(new_uninit)]
|
#![feature(new_uninit)]
|
||||||
#![feature(get_mut_unchecked)]
|
#![feature(get_mut_unchecked)]
|
||||||
|
#![feature(strict_provenance)]
|
||||||
|
|
||||||
//! PkmnLib
|
//! PkmnLib
|
||||||
//! PkmnLib is a full featured implementation of Pokemon. while currently focused on implementing
|
//! PkmnLib is a full featured implementation of Pokemon. while currently focused on implementing
|
||||||
|
|
|
@ -194,7 +194,7 @@ register! {
|
||||||
) -> u8 {
|
) -> u8 {
|
||||||
let name : *mut c_char = env.data().data().get_raw_pointer(name);
|
let name : *mut c_char = env.data().data().get_raw_pointer(name);
|
||||||
let name = unsafe { CStr::from_ptr(name) };
|
let name = unsafe { CStr::from_ptr(name) };
|
||||||
let key = StringKey::new(&name.to_str().unwrap().clone());
|
let key = StringKey::new(name.to_str().unwrap().into());
|
||||||
if pokemon.value_func(&env).unwrap().has_held_item(&key) { 1 } else { 0 }
|
if pokemon.value_func(&env).unwrap().has_held_item(&key) { 1 } else { 0 }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,7 +41,7 @@ pub mod tests {
|
||||||
pub fn build() -> AbilityLibrary {
|
pub fn build() -> AbilityLibrary {
|
||||||
let mut lib = AbilityLibrary::new(1);
|
let mut lib = AbilityLibrary::new(1);
|
||||||
lib.add(
|
lib.add(
|
||||||
&StringKey::new("test_ability"),
|
&StringKey::new("test_ability".into()),
|
||||||
Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new()),
|
Ability::new(&"test_ability".into(), &"test_ability".into(), Vec::new()),
|
||||||
);
|
);
|
||||||
// Drops borrow as mut
|
// Drops borrow as mut
|
||||||
|
|
|
@ -61,7 +61,7 @@ pub mod tests {
|
||||||
let m = build_move();
|
let m = build_move();
|
||||||
// Borrow as mut so we can insert
|
// Borrow as mut so we can insert
|
||||||
let w = &mut lib;
|
let w = &mut lib;
|
||||||
w.add(&StringKey::new("foo"), m);
|
w.add(&StringKey::new("foo".into()), m);
|
||||||
// Drops borrow as mut
|
// Drops borrow as mut
|
||||||
|
|
||||||
lib
|
lib
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, LazyLock};
|
||||||
|
|
||||||
use hashbrown::{HashMap, HashSet};
|
use hashbrown::{HashMap, HashSet};
|
||||||
|
|
||||||
|
@ -29,11 +29,11 @@ pub struct Species {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A cached String Key to get the default form.
|
/// A cached String Key to get the default form.
|
||||||
static DEFAULT_KEY: conquer_once::OnceCell<StringKey> = conquer_once::OnceCell::uninit();
|
static DEFAULT_KEY: LazyLock<StringKey> = LazyLock::new(|| StringKey::new("default"));
|
||||||
|
|
||||||
/// Gets the StringKey for "default". Initialises it if it does not exist.
|
/// Gets the StringKey for "default". Initialises it if it does not exist.
|
||||||
fn get_default_key() -> StringKey {
|
fn get_default_key() -> StringKey {
|
||||||
DEFAULT_KEY.get_or_init(|| StringKey::new("default")).clone()
|
DEFAULT_KEY.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Species {
|
impl Species {
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
|
use arcstr::ArcStr;
|
||||||
|
use hashbrown::HashMap;
|
||||||
|
use parking_lot::RwLock;
|
||||||
use std::borrow::Borrow;
|
use std::borrow::Borrow;
|
||||||
use std::ffi::CStr;
|
use std::ffi::CStr;
|
||||||
use std::fmt::{Display, Formatter};
|
use std::fmt::{Display, Formatter};
|
||||||
use std::hash::{Hash, Hasher};
|
use std::hash::{Hash, Hasher};
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::sync::{Arc, Mutex, Weak};
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use conquer_once::OnceCell;
|
|
||||||
use hashbrown::HashMap;
|
|
||||||
|
|
||||||
/// StringKey is an immutable string that is used for indexing of hashmaps or equality a lot.
|
/// StringKey is an immutable string that is used for indexing of hashmaps or equality a lot.
|
||||||
/// By reference counting the string instead of copying, and caching the hash, we can get some
|
/// By reference counting the string instead of copying, and caching the hash, we can get some
|
||||||
|
@ -16,16 +16,16 @@ use hashbrown::HashMap;
|
||||||
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
#[cfg_attr(feature = "wasm", derive(unique_type_id_derive::UniqueTypeId))]
|
||||||
pub struct StringKey {
|
pub struct StringKey {
|
||||||
/// The underlying reference counted string.
|
/// The underlying reference counted string.
|
||||||
str: Arc<str>,
|
str: ArcStr,
|
||||||
/// The unique hash of the string.
|
/// The unique hash of the string.
|
||||||
hash: u32,
|
hash: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A cache of all allocated strings. This allows us to re-use strings that are often used without
|
/// A cache of all allocated strings. This allows us to re-use strings that are often used without
|
||||||
/// allocation.
|
/// allocation.
|
||||||
static STRING_CACHE: OnceCell<Mutex<HashMap<u32, Weak<str>>>> = OnceCell::uninit();
|
static STRING_CACHE: LazyLock<RwLock<HashMap<u32, ArcStr>>> = LazyLock::new(|| RwLock::new(HashMap::new()));
|
||||||
/// An empty StringKey
|
/// An empty StringKey
|
||||||
static EMPTY: OnceCell<StringKey> = OnceCell::uninit();
|
static EMPTY: LazyLock<StringKey> = LazyLock::new(|| StringKey::new(""));
|
||||||
|
|
||||||
impl StringKey {
|
impl StringKey {
|
||||||
/// Gets the hash of a string.
|
/// Gets the hash of a string.
|
||||||
|
@ -42,25 +42,29 @@ impl StringKey {
|
||||||
/// Creates a new StringKey. If we can find a value for this StringKey in the cache, we re-use
|
/// Creates a new StringKey. If we can find a value for this StringKey in the cache, we re-use
|
||||||
/// that value.
|
/// that value.
|
||||||
pub fn new(s: &str) -> Self {
|
pub fn new(s: &str) -> Self {
|
||||||
let hash = StringKey::get_hash(s);
|
let s: ArcStr = s.into();
|
||||||
let mut cache = STRING_CACHE.get_or_init(|| Mutex::new(HashMap::new())).lock().unwrap();
|
let hash = StringKey::get_hash(s.as_str());
|
||||||
let cached_value = cache.get(&hash);
|
{
|
||||||
if let Some(cached_value) = cached_value {
|
let cache_read = STRING_CACHE.read();
|
||||||
if let Some(cached_value) = cached_value.upgrade() {
|
let cached_value = cache_read.get(&hash);
|
||||||
|
if let Some(cached_value) = cached_value {
|
||||||
return Self {
|
return Self {
|
||||||
str: cached_value,
|
str: cached_value.clone(),
|
||||||
hash,
|
hash,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let v = Self { str: s.into(), hash };
|
{
|
||||||
cache.insert(hash, Arc::downgrade(&v.str));
|
let v = Self { str: s.clone(), hash };
|
||||||
v
|
let mut cache_write = STRING_CACHE.write();
|
||||||
|
cache_write.insert(hash, s);
|
||||||
|
v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the empty StringKey.
|
/// Gets the empty StringKey.
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
EMPTY.get_or_init(|| StringKey::new("")).clone()
|
EMPTY.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the underlying string for the StringKey.
|
/// Gets the underlying string for the StringKey.
|
||||||
|
@ -108,7 +112,7 @@ impl Display for StringKey {
|
||||||
|
|
||||||
impl Into<StringKey> for &CStr {
|
impl Into<StringKey> for &CStr {
|
||||||
fn into(self) -> StringKey {
|
fn into(self) -> StringKey {
|
||||||
StringKey::new(self.to_str().unwrap())
|
StringKey::new(self.to_str().unwrap().into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// Converts a character to lowercased in a const safe way.
|
/// Converts a character to lowercased in a const safe way.
|
||||||
|
@ -160,7 +164,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_empty_stringkey() {
|
fn create_empty_stringkey() {
|
||||||
let sk = StringKey::new("");
|
let sk = StringKey::new("".into());
|
||||||
assert_eq!(sk.str(), "");
|
assert_eq!(sk.str(), "");
|
||||||
assert_eq!(sk.hash(), 0);
|
assert_eq!(sk.hash(), 0);
|
||||||
assert_eq!(sk.hash(), StringKey::get_hash(""));
|
assert_eq!(sk.hash(), StringKey::get_hash(""));
|
||||||
|
@ -168,7 +172,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_stringkey_foo() {
|
fn create_stringkey_foo() {
|
||||||
let sk = StringKey::new("foo");
|
let sk = StringKey::new("foo".into());
|
||||||
assert_eq!(sk.str(), "foo");
|
assert_eq!(sk.str(), "foo");
|
||||||
assert_eq!(sk.hash(), 2356372769);
|
assert_eq!(sk.hash(), 2356372769);
|
||||||
assert_eq!(sk.hash(), StringKey::get_hash("foo"));
|
assert_eq!(sk.hash(), StringKey::get_hash("foo"));
|
||||||
|
@ -177,7 +181,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn create_stringkey_bar() {
|
fn create_stringkey_bar() {
|
||||||
let sk = StringKey::new("bar");
|
let sk = StringKey::new("bar".into());
|
||||||
assert_eq!(sk.str(), "bar");
|
assert_eq!(sk.str(), "bar");
|
||||||
assert_eq!(sk.hash(), 1996459178);
|
assert_eq!(sk.hash(), 1996459178);
|
||||||
assert_eq!(sk.hash(), StringKey::get_hash("bar"));
|
assert_eq!(sk.hash(), StringKey::get_hash("bar"));
|
||||||
|
|
|
@ -52,13 +52,13 @@ pub fn load_types(path: &String, type_library: &mut TypeLibrary) {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let headers = reader.headers().unwrap();
|
let headers = reader.headers().unwrap();
|
||||||
for header in headers.iter().skip(1) {
|
for header in headers.iter().skip(1) {
|
||||||
type_library.register_type(&StringKey::new(header));
|
type_library.register_type(&StringKey::new(header.into()));
|
||||||
}
|
}
|
||||||
|
|
||||||
for record in reader.records() {
|
for record in reader.records() {
|
||||||
let record = record.unwrap();
|
let record = record.unwrap();
|
||||||
let offensive_type = record.get(0).unwrap();
|
let offensive_type = record.get(0).unwrap();
|
||||||
let offensive_type_id = type_library.get_type_id(&StringKey::new(offensive_type));
|
let offensive_type_id = type_library.get_type_id(&StringKey::new(offensive_type.into()));
|
||||||
|
|
||||||
for (i, v) in record.iter().skip(1).enumerate() {
|
for (i, v) in record.iter().skip(1).enumerate() {
|
||||||
let effectiveness = v.parse::<f32>().unwrap();
|
let effectiveness = v.parse::<f32>().unwrap();
|
||||||
|
@ -75,7 +75,7 @@ pub fn load_natures(path: &String, nature_library: &mut NatureLibrary) {
|
||||||
|
|
||||||
for record in reader.records() {
|
for record in reader.records() {
|
||||||
let record = record.unwrap();
|
let record = record.unwrap();
|
||||||
let nature_name = StringKey::new(record.get(0).unwrap());
|
let nature_name = StringKey::new(record.get(0).unwrap().into());
|
||||||
let increased_statistic_str = record.get(1).unwrap();
|
let increased_statistic_str = record.get(1).unwrap();
|
||||||
let decreased_statistic_str = record.get(2).unwrap();
|
let decreased_statistic_str = record.get(2).unwrap();
|
||||||
if increased_statistic_str.is_empty() || decreased_statistic_str.is_empty() {
|
if increased_statistic_str.is_empty() || decreased_statistic_str.is_empty() {
|
||||||
|
@ -99,18 +99,18 @@ pub fn load_items(path: &String, lib: &mut ItemLibrary) {
|
||||||
let json_array = json.as_array().unwrap();
|
let json_array = json.as_array().unwrap();
|
||||||
|
|
||||||
for v in json_array {
|
for v in json_array {
|
||||||
let name = StringKey::new(v["name"].as_str().unwrap());
|
let name = StringKey::new(v.get("name").unwrap().as_str().unwrap().into());
|
||||||
let category = serde_json::from_value(v["itemType"].clone()).unwrap();
|
let category = serde_json::from_value(v.get("itemType").unwrap().clone()).unwrap();
|
||||||
let mut battle_category = BattleItemCategory::None;
|
let mut battle_category = BattleItemCategory::None;
|
||||||
if let Some(c) = v.get("battleType") {
|
if let Some(c) = v.get("battleType") {
|
||||||
battle_category = serde_json::from_value(c.clone()).unwrap();
|
battle_category = serde_json::from_value(c.clone()).unwrap();
|
||||||
}
|
}
|
||||||
let price = v["price"].as_i64().unwrap();
|
let price = v.get("price").unwrap().as_i64().unwrap();
|
||||||
let mut flags = HashSet::new();
|
let mut flags = HashSet::new();
|
||||||
if let Some(f) = v.get("flags") {
|
if let Some(f) = v.get("flags") {
|
||||||
let a = f.as_array().unwrap();
|
let a = f.as_array().unwrap();
|
||||||
for flag in a {
|
for flag in a {
|
||||||
flags.insert(StringKey::new(flag.as_str().unwrap()));
|
flags.insert(StringKey::new(flag.as_str().unwrap().into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -148,7 +148,7 @@ pub fn load_abilities(path: &String, ability_library: &mut AbilityLibrary) {
|
||||||
let name = StringKey::new(key);
|
let name = StringKey::new(key);
|
||||||
let mut effect = StringKey::empty();
|
let mut effect = StringKey::empty();
|
||||||
if let Some(e) = value.get("effect") {
|
if let Some(e) = value.get("effect") {
|
||||||
effect = StringKey::new(e.as_str().unwrap());
|
effect = StringKey::new(e.as_str().unwrap().into());
|
||||||
}
|
}
|
||||||
let mut parameters = Vec::new();
|
let mut parameters = Vec::new();
|
||||||
if let Some(p) = value.get("parameters") {
|
if let Some(p) = value.get("parameters") {
|
||||||
|
@ -169,15 +169,15 @@ pub fn load_moves(path: &String, lib: &mut StaticData) {
|
||||||
let data = json.as_object().unwrap().get("data").unwrap().as_array().unwrap();
|
let data = json.as_object().unwrap().get("data").unwrap().as_array().unwrap();
|
||||||
for move_data in data {
|
for move_data in data {
|
||||||
let move_data = move_data.as_object().unwrap();
|
let move_data = move_data.as_object().unwrap();
|
||||||
let move_name = StringKey::new(move_data["name"].as_str().unwrap());
|
let move_name = StringKey::new(move_data.get("name").unwrap().as_str().unwrap().into());
|
||||||
let move_type = StringKey::new(move_data["type"].as_str().unwrap());
|
let move_type = StringKey::new(move_data.get("type").unwrap().as_str().unwrap().into());
|
||||||
let move_type_id = lib.types().get_type_id(&move_type);
|
let move_type_id = lib.types().get_type_id(&move_type);
|
||||||
let move_category = serde_json::from_value(move_data["category"].clone()).unwrap();
|
let move_category = serde_json::from_value(move_data.get("category").unwrap().clone()).unwrap();
|
||||||
let base_power = move_data["power"].as_i64().unwrap() as u8;
|
let base_power = move_data.get("power").unwrap().as_i64().unwrap() as u8;
|
||||||
let accuracy = move_data["accuracy"].as_i64().unwrap() as u8;
|
let accuracy = move_data.get("accuracy").unwrap().as_i64().unwrap() as u8;
|
||||||
let pp = move_data["pp"].as_i64().unwrap() as u8;
|
let pp = move_data.get("pp").unwrap().as_i64().unwrap() as u8;
|
||||||
let target = serde_json::from_value(move_data["target"].clone()).unwrap();
|
let target = serde_json::from_value(move_data.get("target").unwrap().clone()).unwrap();
|
||||||
let priority = move_data["priority"].as_i64().unwrap() as i8;
|
let priority = move_data.get("priority").unwrap().as_i64().unwrap() as i8;
|
||||||
let secondary_effect = if let Some(v) = move_data.get("effect") {
|
let secondary_effect = if let Some(v) = move_data.get("effect") {
|
||||||
let mut chance = -1.0;
|
let mut chance = -1.0;
|
||||||
if let Some(chance_value) = v.get("chance") {
|
if let Some(chance_value) = v.get("chance") {
|
||||||
|
@ -193,7 +193,7 @@ pub fn load_moves(path: &String, lib: &mut StaticData) {
|
||||||
|
|
||||||
Some(SecondaryEffect::new(
|
Some(SecondaryEffect::new(
|
||||||
chance,
|
chance,
|
||||||
StringKey::new(v["name"].as_str().unwrap()),
|
StringKey::new(v.get("name").unwrap().as_str().unwrap().into()),
|
||||||
parameters,
|
parameters,
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
|
@ -204,7 +204,7 @@ pub fn load_moves(path: &String, lib: &mut StaticData) {
|
||||||
if let Some(f) = move_data.get("flags") {
|
if let Some(f) = move_data.get("flags") {
|
||||||
let f = f.as_array().unwrap();
|
let f = f.as_array().unwrap();
|
||||||
for flag in f {
|
for flag in f {
|
||||||
flags.insert(StringKey::new(flag.as_str().unwrap()));
|
flags.insert(StringKey::new(flag.as_str().unwrap().into()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,29 +233,29 @@ pub fn load_species(path: &String, library: &mut StaticData) {
|
||||||
let json: Value = serde_json::from_str(&data).unwrap();
|
let json: Value = serde_json::from_str(&data).unwrap();
|
||||||
let o = json.as_object().unwrap();
|
let o = json.as_object().unwrap();
|
||||||
|
|
||||||
for (key, value) in o {
|
for (key, value) in o.iter() {
|
||||||
if key.starts_with('$') {
|
if key.starts_with('$') {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
let name = StringKey::new(key);
|
let name = StringKey::new(key);
|
||||||
let id = value["id"].as_i64().unwrap();
|
let id = value.get("id").unwrap().as_i64().unwrap();
|
||||||
let gender_rate = value["genderRatio"].as_f64().unwrap();
|
let gender_rate = value.get("genderRatio").unwrap().as_f64().unwrap();
|
||||||
let growth_rate_name = StringKey::new(value["growthRate"].as_str().unwrap());
|
let growth_rate_name = StringKey::new(value.get("growthRate").unwrap().as_str().unwrap().into());
|
||||||
let _base_happiness = value["baseHappiness"].as_i64().unwrap();
|
let _base_happiness = value.get("baseHappiness").unwrap().as_i64().unwrap();
|
||||||
let catch_rate = value["catchRate"].as_i64().unwrap();
|
let catch_rate = value.get("catchRate").unwrap().as_i64().unwrap();
|
||||||
let _color = value["color"].as_str().unwrap();
|
let _color = value.get("color").unwrap().as_str().unwrap();
|
||||||
// let egg_groups = value["eggGroups"]
|
// let egg_groups = value.get("eggGroups").unwrap()
|
||||||
// .as_array()
|
// .as_array()
|
||||||
// .unwrap()
|
// .unwrap()
|
||||||
// .iter()
|
// .iter()
|
||||||
// .map(|&a| a.as_str().unwrap())
|
// .map(|&a| a.as_str().unwrap())
|
||||||
// .collect();
|
// .collect();
|
||||||
let _egg_cycle = value["eggCycles"].as_i64().unwrap();
|
let _egg_cycle = value.get("eggCycles").unwrap().as_i64().unwrap();
|
||||||
// TODO: tags
|
// TODO: tags
|
||||||
// TODO: evolutions
|
// TODO: evolutions
|
||||||
|
|
||||||
let forms = value["formes"].as_object().unwrap();
|
let forms = value.get("formes").unwrap().as_object().unwrap();
|
||||||
let default_form_value = &forms["default"];
|
let default_form_value = forms.get("default").unwrap();
|
||||||
let default_form = parse_form("default".into(), default_form_value, library);
|
let default_form = parse_form("default".into(), default_form_value, library);
|
||||||
|
|
||||||
let species = Species::new(
|
let species = Species::new(
|
||||||
|
@ -282,26 +282,28 @@ fn load_wasm(path: &String, library: &mut WebAssemblyScriptResolver) {
|
||||||
|
|
||||||
fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Form {
|
fn parse_form(name: StringKey, value: &Value, library: &mut StaticData) -> Form {
|
||||||
let mut abilities = Vec::new();
|
let mut abilities = Vec::new();
|
||||||
for a in value["abilities"].as_array().unwrap() {
|
for a in value.get("abilities").unwrap().as_array().unwrap() {
|
||||||
abilities.push(StringKey::new(a.as_str().unwrap()));
|
abilities.push(StringKey::new(a.as_str().unwrap().into()));
|
||||||
}
|
}
|
||||||
let mut hidden_abilities = Vec::new();
|
let mut hidden_abilities = Vec::new();
|
||||||
for a in value["hiddenAbilities"].as_array().unwrap() {
|
for a in value.get("hiddenAbilities").unwrap().as_array().unwrap() {
|
||||||
hidden_abilities.push(StringKey::new(a.as_str().unwrap()));
|
hidden_abilities.push(StringKey::new(a.as_str().unwrap().into()));
|
||||||
}
|
}
|
||||||
let base_stats = parse_statistics(&value["baseStats"]);
|
let base_stats = parse_statistics(&value.get("baseStats").unwrap());
|
||||||
// TODO: ev reward
|
// TODO: ev reward
|
||||||
let height = value["height"].as_f64().unwrap();
|
let height = value.get("height").unwrap().as_f64().unwrap();
|
||||||
let weight = value["weight"].as_f64().unwrap();
|
let weight = value.get("weight").unwrap().as_f64().unwrap();
|
||||||
let base_experience = value["baseExp"].as_u64().unwrap();
|
let base_experience = value.get("baseExp").unwrap().as_u64().unwrap();
|
||||||
let types = value["types"]
|
let types = value
|
||||||
|
.get("types")
|
||||||
|
.unwrap()
|
||||||
.as_array()
|
.as_array()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|a| library.types().get_type_id(&StringKey::new(a.as_str().unwrap())))
|
.map(|a| library.types().get_type_id(&StringKey::new(a.as_str().unwrap().into())))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let moves = parse_moves(&value["moves"], library.moves());
|
let moves = parse_moves(&value.get("moves").unwrap(), library.moves());
|
||||||
|
|
||||||
Form::new(
|
Form::new(
|
||||||
&name,
|
&name,
|
||||||
|
@ -364,10 +366,10 @@ where
|
||||||
fn parse_moves(value: &Value, move_library: &MoveLibrary) -> LearnableMoves {
|
fn parse_moves(value: &Value, move_library: &MoveLibrary) -> LearnableMoves {
|
||||||
let mut moves = LearnableMoves::default();
|
let mut moves = LearnableMoves::default();
|
||||||
|
|
||||||
let level_moves = value["levelMoves"].as_array().unwrap();
|
let level_moves = value.get("levelMoves").unwrap().as_array().unwrap();
|
||||||
for level_move in level_moves {
|
for level_move in level_moves {
|
||||||
let name = StringKey::new(level_move["name"].as_str().unwrap());
|
let name = StringKey::new(level_move.get("name").unwrap().as_str().unwrap().into());
|
||||||
let level = level_move["level"].as_u64().unwrap() as LevelInt;
|
let level = level_move.get("level").unwrap().as_u64().unwrap() as LevelInt;
|
||||||
assert!(move_library.get(&name).is_some());
|
assert!(move_library.get(&name).is_some());
|
||||||
moves.add_level_move(level, &name);
|
moves.add_level_move(level, &name);
|
||||||
}
|
}
|
||||||
|
@ -388,7 +390,7 @@ fn parse_effect_parameter(value: &Value) -> EffectParameter {
|
||||||
EffectParameter::Int(n.as_i64().unwrap())
|
EffectParameter::Int(n.as_i64().unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Value::String(s) => EffectParameter::String(StringKey::new(s.as_str())),
|
Value::String(s) => EffectParameter::String(StringKey::new(s.as_str().into())),
|
||||||
Value::Array(_) => {
|
Value::Array(_) => {
|
||||||
panic!("Unexpected type")
|
panic!("Unexpected type")
|
||||||
}
|
}
|
||||||
|
@ -408,8 +410,8 @@ fn test_type_library_loaded() {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
lib.get_effectiveness(
|
lib.get_effectiveness(
|
||||||
lib.get_type_id(&StringKey::new("fire")),
|
lib.get_type_id(&StringKey::new("fire".into())),
|
||||||
&[lib.get_type_id(&StringKey::new("grass"))],
|
&[lib.get_type_id(&StringKey::new("grass".into()))],
|
||||||
),
|
),
|
||||||
2.0
|
2.0
|
||||||
);
|
);
|
||||||
|
|
|
@ -74,7 +74,7 @@ impl TestCase {
|
||||||
|
|
||||||
impl TestPokemon {
|
impl TestPokemon {
|
||||||
fn to_pokemon(&self, library: Arc<DynamicLibrary>) -> Pokemon {
|
fn to_pokemon(&self, library: Arc<DynamicLibrary>) -> Pokemon {
|
||||||
let mut builder = PokemonBuilder::new(library, StringKey::new(self.species.as_str()), self.level);
|
let mut builder = PokemonBuilder::new(library, StringKey::new(self.species.as_str().into()), self.level);
|
||||||
for move_name in &self.moves {
|
for move_name in &self.moves {
|
||||||
builder = builder.learn_move(StringKey::new(move_name));
|
builder = builder.learn_move(StringKey::new(move_name));
|
||||||
}
|
}
|
||||||
|
|
Binary file not shown.
|
@ -7,8 +7,6 @@ use std::io::Read;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use conquer_once::OnceCell;
|
|
||||||
|
|
||||||
use pkmn_lib::dynamic_data::{
|
use pkmn_lib::dynamic_data::{
|
||||||
Battle, BattleParty, DamageSource, DynamicLibrary, ExecutingMove, MoveChoice, PokemonBuilder, PokemonParty, Script,
|
Battle, BattleParty, DamageSource, DynamicLibrary, ExecutingMove, MoveChoice, PokemonBuilder, PokemonParty, Script,
|
||||||
ScriptCategory, ScriptContainer, ScriptOwnerData, TurnChoice, VolatileScriptsOwner,
|
ScriptCategory, ScriptContainer, ScriptOwnerData, TurnChoice, VolatileScriptsOwner,
|
||||||
|
@ -19,22 +17,11 @@ use crate::common::{library_loader, TestCase};
|
||||||
|
|
||||||
pub mod common;
|
pub mod common;
|
||||||
|
|
||||||
static LIBRARY: OnceCell<Arc<DynamicLibrary>> = OnceCell::uninit();
|
|
||||||
|
|
||||||
fn get_library() -> Arc<DynamicLibrary> {
|
fn get_library() -> Arc<DynamicLibrary> {
|
||||||
LIBRARY
|
Arc::new(library_loader::load_library())
|
||||||
.get_or_init(|| {
|
|
||||||
let start_time = chrono::Utc::now();
|
|
||||||
let lib = library_loader::load_library();
|
|
||||||
let end_time = chrono::Utc::now();
|
|
||||||
println!("Built library in {} ms", (end_time - start_time).num_milliseconds());
|
|
||||||
Arc::new(lib)
|
|
||||||
})
|
|
||||||
.clone()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg_attr(miri, ignore)]
|
|
||||||
fn validate_library_load() {
|
fn validate_library_load() {
|
||||||
let start_time = chrono::Utc::now();
|
let start_time = chrono::Utc::now();
|
||||||
library_loader::load_library();
|
library_loader::load_library();
|
||||||
|
|
|
@ -0,0 +1,90 @@
|
||||||
|
{
|
||||||
|
<static_hashmap>
|
||||||
|
Memcheck:Leak
|
||||||
|
fun:malloc
|
||||||
|
fun:alloc
|
||||||
|
fun:__rdl_alloc
|
||||||
|
fun:_ZN5alloc5alloc5alloc17hc42e909f81160460E
|
||||||
|
fun:_ZN96_$LT$hashbrown..raw..alloc..inner..Global$u20$as$u20$hashbrown..raw..alloc..inner..Allocator$GT$8allocate17h2c481e4442c55c5dE
|
||||||
|
fun:_ZN9hashbrown3raw5alloc5inner8do_alloc17h1f83bdd1222cbbb7E
|
||||||
|
fun:_ZN9hashbrown3raw22RawTableInner$LT$A$GT$17new_uninitialized17h12dbb4fa91b4b3afE
|
||||||
|
fun:_ZN9hashbrown3raw22RawTableInner$LT$A$GT$22fallible_with_capacity17h6df80e2764604857E
|
||||||
|
fun:_ZN9hashbrown3raw22RawTableInner$LT$A$GT$14prepare_resize17h71d20d4d23776238E
|
||||||
|
fun:_ZN9hashbrown3raw21RawTable$LT$T$C$A$GT$14reserve_rehash17h28974fc9c4c277e6E.llvm.15230268944078492848
|
||||||
|
fun:_ZN9hashbrown3raw21RawTable$LT$T$C$A$GT$7reserve17h7eee95820260d1e7E.llvm.15230268944078492848
|
||||||
|
fun:_ZN9hashbrown3raw21RawTable$LT$T$C$A$GT$6insert17h35ff9588f93ce06cE
|
||||||
|
fun:_ZN9hashbrown3map28HashMap$LT$K$C$V$C$S$C$A$GT$6insert17h996ff1a9e535e422E
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
<static_hashmap_2>
|
||||||
|
Memcheck:Leak
|
||||||
|
fun:malloc
|
||||||
|
fun:alloc
|
||||||
|
fun:__rdl_alloc
|
||||||
|
fun:_ZN5alloc5alloc6Global10alloc_impl17hd6fbb6e351b4be39E.llvm.18227935767307273530
|
||||||
|
fun:allocate
|
||||||
|
fun:_ZN5alloc5alloc15exchange_malloc17h9e4cc8ad48821a81E
|
||||||
|
fun:new<[[u64; 4]; 2]>
|
||||||
|
fun:_ZN94_$LT$ahash..random_state..DefaultRandomSource$u20$as$u20$ahash..random_state..RandomSource$GT$15get_fixed_seeds28_$u7b$$u7b$closure$u7d$$u7d$17h1797bff870f50682E
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init28_$u7b$$u7b$closure$u7d$$u7d$17h744caffedf4c7067E
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$15get_or_try_init17hcd2c841e12e37bc0E
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init17h7d3ca5ccc71b3021E
|
||||||
|
fun:_ZN94_$LT$ahash..random_state..DefaultRandomSource$u20$as$u20$ahash..random_state..RandomSource$GT$15get_fixed_seeds17h2a6c6672bd2f8f7bE
|
||||||
|
fun:_ZN5ahash12random_state11RandomState3new17he4bd97026fbf5adcE.llvm.11960766389962301245
|
||||||
|
fun:_ZN75_$LT$ahash..random_state..RandomState$u20$as$u20$core..default..Default$GT$7default17h65efd61ef64a7f0dE
|
||||||
|
fun:_ZN87_$LT$hashbrown..map..HashMap$LT$K$C$V$C$S$C$A$GT$$u20$as$u20$core..default..Default$GT$7default17h40e688f79e703e93E.llvm.35124437821632695
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
<static_hashmap_3>
|
||||||
|
Memcheck:Leak
|
||||||
|
match-leak-kinds: reachable
|
||||||
|
fun:malloc
|
||||||
|
fun:alloc
|
||||||
|
fun:__rdl_alloc
|
||||||
|
fun:_ZN5alloc5alloc6Global10alloc_impl17h0263a87402431af1E.llvm.1186731428384934186
|
||||||
|
fun:allocate
|
||||||
|
fun:_ZN5alloc5alloc15exchange_malloc17h2285591b899cc9f4E
|
||||||
|
fun:new<alloc::boxed::Box<(dyn ahash::random_state::RandomSource + core::marker::Send + core::marker::Sync), alloc::alloc::Global>>
|
||||||
|
fun:_ZN5ahash12random_state11RandomState7get_src28_$u7b$$u7b$closure$u7d$$u7d$17h5cf982d28005fdddE
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init28_$u7b$$u7b$closure$u7d$$u7d$17h72179437b77bfeebE
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$15get_or_try_init17h4373eb8bae30d42dE.llvm.10117491539980278462
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init17hec6279877245cea1E
|
||||||
|
fun:_ZN5ahash12random_state11RandomState7get_src17haf7fc09e2535a0a7E.llvm.11960766389962301245
|
||||||
|
fun:_ZN5ahash12random_state11RandomState3new17he4bd97026fbf5adcE.llvm.11960766389962301245
|
||||||
|
fun:_ZN75_$LT$ahash..random_state..RandomState$u20$as$u20$core..default..Default$GT$7default17h65efd61ef64a7f0dE
|
||||||
|
fun:_ZN87_$LT$hashbrown..map..HashMap$LT$K$C$V$C$S$C$A$GT$$u20$as$u20$core..default..Default$GT$7default17h40e688f79e703e93E.llvm.35124437821632695
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
<static_hashmap_4>
|
||||||
|
Memcheck:Leak
|
||||||
|
match-leak-kinds: reachable
|
||||||
|
fun:malloc
|
||||||
|
fun:alloc
|
||||||
|
fun:__rdl_alloc
|
||||||
|
fun:_ZN5alloc5alloc6Global10alloc_impl17h0263a87402431af1E.llvm.1186731428384934186
|
||||||
|
fun:allocate
|
||||||
|
fun:_ZN5alloc5alloc15exchange_malloc17h2285591b899cc9f4E
|
||||||
|
fun:new<ahash::random_state::DefaultRandomSource>
|
||||||
|
fun:_ZN5ahash12random_state11RandomState7get_src28_$u7b$$u7b$closure$u7d$$u7d$17h5cf982d28005fdddE
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init28_$u7b$$u7b$closure$u7d$$u7d$17h72179437b77bfeebE
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$15get_or_try_init17h4373eb8bae30d42dE.llvm.10117491539980278462
|
||||||
|
fun:_ZN9once_cell4race8once_box16OnceBox$LT$T$GT$11get_or_init17hec6279877245cea1E
|
||||||
|
fun:_ZN5ahash12random_state11RandomState7get_src17haf7fc09e2535a0a7E.llvm.11960766389962301245
|
||||||
|
fun:_ZN5ahash12random_state11RandomState3new17he4bd97026fbf5adcE.llvm.11960766389962301245
|
||||||
|
fun:_ZN75_$LT$ahash..random_state..RandomState$u20$as$u20$core..default..Default$GT$7default17h65efd61ef64a7f0dE
|
||||||
|
fun:_ZN9hashbrown3map20HashMap$LT$K$C$V$GT$13with_capacity17h8042103d2e19cc61E
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
<stringkey_cache>
|
||||||
|
Memcheck:Leak
|
||||||
|
fun:malloc
|
||||||
|
fun:alloc
|
||||||
|
fun:__rdl_alloc
|
||||||
|
fun:_ZN5alloc5alloc5alloc17hdc225bf5a9332839E
|
||||||
|
fun:_ZN6arcstr7arc_str9ThinInner12try_allocate17h5ec0d22c686b0aa1E
|
||||||
|
fun:_ZN6arcstr7arc_str9ThinInner8allocate17hb1fd6d38fd1e7864E
|
||||||
|
}
|
Loading…
Reference in New Issue