Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ wasm-bindgen = { version = "^0.2", optional = true}
pyo3 = { version = "^0.18", features = ["extension-module"], optional = true}
pyo3-built = { version = "^0.4", optional = true}
num_enum = "0.6.0"
tracing-wasm = "0.2.1"
tracing = "0.1.37"
tracing-subscriber = "0.3.17"


[lib]
Expand Down
78 changes: 47 additions & 31 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#![allow(dead_code)]
#![allow(unused_imports)]

use logging::LogLevel;
pub mod abilities;
pub mod activity;
pub mod d2_enums;
pub mod enemies;
pub mod logging;
pub mod perks;
#[cfg(test)]
mod test;
Expand All @@ -21,7 +19,6 @@ use d2_enums::StatHashes;
use enemies::Enemy;
use std::cell::RefCell;
use std::collections::HashMap;
use std::panic;

mod built_info {
include!(concat!(env!("OUT_DIR"), "/built.rs"));
Expand Down Expand Up @@ -53,43 +50,53 @@ use crate::types::py_types::{
#[cfg(feature = "python")]
use pyo3::{prelude::*, types::PyDict};

#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct PersistentData {
pub weapon: Weapon,
pub activity: Activity,
pub ability: Ability,
pub enemy: Enemy,
pub log_level: LogLevel,
pub log_level: tracing::Level,
}

impl PersistentData {
pub fn new() -> PersistentData {
Self::default()
}
}

thread_local! {
static PERS_DATA: RefCell<PersistentData> = RefCell::new(PersistentData::new());
impl Default for PersistentData {
fn default() -> Self {
Self {
weapon: Default::default(),
activity: Default::default(),
ability: Default::default(),
enemy: Default::default(),
log_level: tracing::Level::INFO,
}
}
}

#[cfg(feature = "wasm")]
#[wasm_bindgen]
extern "C" {
//foreign function interface
#[wasm_bindgen(js_namespace = console)]
pub fn log(s: &str);
thread_local! {
static PERS_DATA: RefCell<PersistentData> = RefCell::new(PersistentData::new());
}

#[cfg(feature = "wasm")]
#[macro_export]
macro_rules! console_log {
($($t:tt)*) => (crate::log(&format_args!($($t)*).to_string()))
}
#[cfg(feature = "wasm")]
#[wasm_bindgen(start)]
pub fn start() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
use tracing_subscriber::{layer::SubscriberExt, Registry};
use tracing_wasm::{WASMLayer, WASMLayerConfigBuilder};

console_error_panic_hook::set_once();
let cfg = WASMLayerConfigBuilder::new()
.set_max_level(tracing::Level::WARN)
.build();
let subscriber = Registry::default().with(WASMLayer::new(cfg));

tracing::subscriber::set_global_default(subscriber).expect("Unable to set global subscriber");

tracing::info!("D2 Calculator Loaded");
perks::map_perks();
console_log!("D2 Calculator Loaded");
}

//---------------WEAPONS---------------//
Expand Down Expand Up @@ -139,11 +146,11 @@ pub fn set_weapon(
_damage_type_id,
);
if new_weapon.is_err() {
console_log!(
"Could not find weapon data for type: {}, intrinsic: {}, Err: {:?}",
_weapon_type_id,
_intrinsic_hash,
new_weapon
tracing::error!(
kind = ?_weapon_type_id,
intrinsic = ?_intrinsic_hash,
err = ?new_weapon,
"Could not find weapon data"
);
perm_data.borrow_mut().weapon = Weapon::default();
} else {
Expand Down Expand Up @@ -394,8 +401,21 @@ pub fn set_encounter(
#[cfg(feature = "wasm")]
#[wasm_bindgen(js_name = "setLoggingLevel")]
pub fn set_logging_level(_level: usize) -> Result<(), JsValue> {
let level = match _level {
0 => tracing::Level::TRACE,
1 => tracing::Level::DEBUG,
2 => tracing::Level::INFO,
3 => tracing::Level::WARN,
4 => tracing::Level::ERROR,
Comment on lines +404 to +409
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this the opposite of what's happening inside of the (since-deleted) logging.rs—both log and tracing treat the most verbose level as the lowest usize, but logging did the opposite (0 is ERROR).

I put up this PR as-is since I'm not sure what the usage of this is over on d2foundry (and since log/tracing have one more logging level than D2_Calculation_API, starting at 0 would result in an off-by-one-error semantic error anyways).

In any case, I'm happy to change this section however you'd like me to.

_ => {
return Err(JsValue::from_str(
"provided usize is too large; must be 4 or less.",
))
}
};

PERS_DATA.with(|perm_data| {
perm_data.borrow_mut().log_level = _level.into();
perm_data.borrow_mut().log_level = level;
});
Ok(())
}
Expand Down Expand Up @@ -597,15 +617,11 @@ fn reverse_pve_calc(
_combatant_mult: Option<f64>,
_pve_mult: Option<f64>,
) -> PyResult<f64> {
use logging::extern_log;
let output = PERS_DATA.with(|perm_data| {
let combatant_mult = _combatant_mult.unwrap_or(1.0);
let pve_mult = _pve_mult.unwrap_or(1.0);
if perm_data.borrow().activity.name == "Default" {
extern_log(
"Activity is default and can return bad values",
LogLevel::Warning,
)
tracing::warn!("Activity is default and can return bad values")
}
activity::damage_calc::remove_pve_bonuses(
_damage,
Expand Down
51 changes: 0 additions & 51 deletions src/logging.rs

This file was deleted.

5 changes: 1 addition & 4 deletions src/perks/exotic_armor.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::collections::HashMap;

use crate::{
d2_enums::{AmmoType, BungieHash, DamageType, StatBump, StatHashes, WeaponType},
logging::{extern_log, LogLevel},
};
use crate::d2_enums::{AmmoType, BungieHash, DamageType, StatBump, StatHashes, WeaponType};

use super::{
add_dmr, add_epr, add_flmr, add_fmr, add_hmr, add_mmr, add_rmr, add_rsmr, add_sbr, add_vmr,
Expand Down
5 changes: 1 addition & 4 deletions src/perks/year_1_perks.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use std::collections::HashMap;

use crate::{
d2_enums::{AmmoType, BungieHash, DamageType, StatBump, StatHashes, WeaponType},
logging::extern_log,
};
use crate::d2_enums::{AmmoType, BungieHash, DamageType, StatBump, StatHashes, WeaponType};

use super::{
add_dmr, add_edr, add_epr, add_fmr, add_hmr, add_imr, add_mmr, add_rmr, add_rr, add_rsmr,
Expand Down
Loading