mirror of
https://github.com/mat-1/azalea.git
synced 2025-08-02 06:16:04 +00:00
Replace lazy_static with once_cell::sync::Lazy (#43)
* Remove lazy_static in azalea-chat * replace lazy_static with once_cell everywhere * fix * fix import * ignore a clippy warning in shape codegen
This commit is contained in:
parent
befa22c1f3
commit
619984fa33
12 changed files with 1745 additions and 1755 deletions
8
Cargo.lock
generated
8
Cargo.lock
generated
|
@ -194,7 +194,7 @@ version = "0.3.0"
|
|||
dependencies = [
|
||||
"azalea-buf",
|
||||
"azalea-language",
|
||||
"lazy_static",
|
||||
"once_cell",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
@ -213,9 +213,9 @@ dependencies = [
|
|||
"azalea-physics",
|
||||
"azalea-protocol",
|
||||
"azalea-world",
|
||||
"lazy_static",
|
||||
"log",
|
||||
"nohash-hasher",
|
||||
"once_cell",
|
||||
"parking_lot",
|
||||
"regex",
|
||||
"thiserror",
|
||||
|
@ -253,7 +253,7 @@ dependencies = [
|
|||
name = "azalea-language"
|
||||
version = "0.3.0"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"once_cell",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
@ -279,7 +279,7 @@ dependencies = [
|
|||
"azalea-block",
|
||||
"azalea-core",
|
||||
"azalea-world",
|
||||
"lazy_static",
|
||||
"once_cell",
|
||||
"parking_lot",
|
||||
"uuid",
|
||||
]
|
||||
|
|
|
@ -11,6 +11,6 @@ repository = "https://github.com/mat-1/azalea/tree/main/azalea-chat"
|
|||
[dependencies]
|
||||
azalea-buf = {path = "../azalea-buf", features = ["serde_json"], version = "^0.3.0" }
|
||||
azalea-language = {path = "../azalea-language", version = "^0.3.0" }
|
||||
lazy_static = "^1.4.0"
|
||||
once_cell = "1.16.0"
|
||||
serde = "^1.0.130"
|
||||
serde_json = "^1.0.72"
|
||||
|
|
|
@ -1,17 +1,16 @@
|
|||
use std::{
|
||||
fmt::Display,
|
||||
io::{Cursor, Write},
|
||||
};
|
||||
|
||||
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
|
||||
use crate::{
|
||||
base_component::BaseComponent,
|
||||
style::{ChatFormatting, Style},
|
||||
text_component::TextComponent,
|
||||
translatable_component::{StringOrComponent, TranslatableComponent},
|
||||
};
|
||||
use azalea_buf::{BufReadError, McBufReadable, McBufWritable};
|
||||
use once_cell::sync::Lazy;
|
||||
use serde::{de, Deserialize, Deserializer};
|
||||
use std::{
|
||||
fmt::Display,
|
||||
io::{Cursor, Write},
|
||||
};
|
||||
|
||||
/// A chat component, basically anything you can see in chat.
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -20,12 +19,10 @@ pub enum Component {
|
|||
Translatable(TranslatableComponent),
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref DEFAULT_STYLE: Style = Style {
|
||||
color: Some(ChatFormatting::White.try_into().unwrap()),
|
||||
..Style::default()
|
||||
};
|
||||
}
|
||||
pub static DEFAULT_STYLE: Lazy<Style> = Lazy::new(|| Style {
|
||||
color: Some(ChatFormatting::White.try_into().unwrap()),
|
||||
..Style::default()
|
||||
});
|
||||
|
||||
/// A chat component
|
||||
impl Component {
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
//! Things for working with Minecraft chat messages.
|
||||
//! This was inspired by Minecraft and prismarine-chat.
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
pub mod base_component;
|
||||
mod component;
|
||||
pub mod style;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::{collections::HashMap, fmt};
|
||||
|
||||
use azalea_buf::McBuf;
|
||||
use once_cell::sync::Lazy;
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug)]
|
||||
|
@ -28,8 +29,8 @@ impl TextColor {
|
|||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
static ref LEGACY_FORMAT_TO_COLOR: HashMap<&'static ChatFormatting, TextColor> = {
|
||||
static LEGACY_FORMAT_TO_COLOR: Lazy<HashMap<&'static ChatFormatting, TextColor>> =
|
||||
Lazy::new(|| {
|
||||
let mut legacy_format_to_color = HashMap::new();
|
||||
for formatter in &ChatFormatting::FORMATTERS {
|
||||
if !formatter.is_format() && *formatter != ChatFormatting::Reset {
|
||||
|
@ -43,15 +44,14 @@ lazy_static! {
|
|||
}
|
||||
}
|
||||
legacy_format_to_color
|
||||
};
|
||||
static ref NAMED_COLORS: HashMap<String, TextColor> = {
|
||||
let mut named_colors = HashMap::new();
|
||||
for color in LEGACY_FORMAT_TO_COLOR.values() {
|
||||
named_colors.insert(color.name.clone().unwrap(), color.clone());
|
||||
}
|
||||
named_colors
|
||||
};
|
||||
}
|
||||
});
|
||||
static NAMED_COLORS: Lazy<HashMap<String, TextColor>> = Lazy::new(|| {
|
||||
let mut named_colors = HashMap::new();
|
||||
for color in LEGACY_FORMAT_TO_COLOR.values() {
|
||||
named_colors.insert(color.name.clone().unwrap(), color.clone());
|
||||
}
|
||||
named_colors
|
||||
});
|
||||
|
||||
pub struct Ansi {}
|
||||
impl Ansi {
|
||||
|
|
|
@ -19,9 +19,9 @@ azalea-crypto = {path = "../azalea-crypto", version = "0.3.0"}
|
|||
azalea-physics = {path = "../azalea-physics", version = "0.3.0"}
|
||||
azalea-protocol = {path = "../azalea-protocol", version = "0.3.0"}
|
||||
azalea-world = {path = "../azalea-world", version = "0.3.0"}
|
||||
lazy_static = "1.4.0"
|
||||
log = "0.4.17"
|
||||
nohash-hasher = "0.2.0"
|
||||
once_cell = "1.16.0"
|
||||
parking_lot = {version = "^0.12.1", features = ["deadlock_detection"]}
|
||||
regex = "1.7.0"
|
||||
thiserror = "^1.0.34"
|
||||
|
|
|
@ -9,8 +9,6 @@ use azalea_protocol::packets::game::{
|
|||
serverbound_chat_command_packet::ServerboundChatCommandPacket,
|
||||
serverbound_chat_packet::ServerboundChatPacket,
|
||||
};
|
||||
use lazy_static::lazy_static;
|
||||
use regex::Regex;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
/// A chat packet, either a system message or a chat message.
|
||||
|
@ -20,6 +18,13 @@ pub enum ChatPacket {
|
|||
Player(Box<ClientboundPlayerChatPacket>),
|
||||
}
|
||||
|
||||
macro_rules! regex {
|
||||
($re:literal $(,)?) => {{
|
||||
static RE: once_cell::sync::OnceCell<regex::Regex> = once_cell::sync::OnceCell::new();
|
||||
RE.get_or_init(|| regex::Regex::new($re).unwrap())
|
||||
}};
|
||||
}
|
||||
|
||||
impl ChatPacket {
|
||||
/// Get the message shown in chat for this packet.
|
||||
pub fn message(&self) -> Component {
|
||||
|
@ -49,11 +54,7 @@ impl ChatPacket {
|
|||
}
|
||||
// It's a system message, so we'll have to match the content
|
||||
// with regex
|
||||
lazy_static! {
|
||||
static ref ANGLE_BRACKETS_RE: Regex =
|
||||
Regex::new("^<([a-zA-Z_0-9]{1,16})> (.+)$").unwrap();
|
||||
}
|
||||
if let Some(m) = ANGLE_BRACKETS_RE.captures(&message) {
|
||||
if let Some(m) = regex!("^<([a-zA-Z_0-9]{1,16})> (.+)$").captures(&message) {
|
||||
return (Some(m[1].to_string()), m[2].to_string());
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ version = "0.3.0"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
lazy_static = "1.4.0"
|
||||
once_cell = "1.16.0"
|
||||
serde = "1.0.137"
|
||||
serde_json = "1.0.81"
|
||||
# tokio = {version = "^1.21.2", features = ["fs"]}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//! Translate Minecraft strings from their id.
|
||||
|
||||
use lazy_static::lazy_static;
|
||||
use once_cell::sync::Lazy;
|
||||
use std::io::Read;
|
||||
use std::path::Path;
|
||||
use std::{collections::HashMap, fs::File};
|
||||
|
@ -31,16 +31,16 @@ use std::{collections::HashMap, fs::File};
|
|||
// Language object that we passed around everywhere which is not convenient
|
||||
// The code above is kept in case I come up with a better solution
|
||||
|
||||
lazy_static! {
|
||||
pub static ref STORAGE: HashMap<String, String> = serde_json::from_str(&{
|
||||
pub static STORAGE: Lazy<HashMap<String, String>> = Lazy::new(|| {
|
||||
serde_json::from_str(&{
|
||||
let src_dir = Path::new(concat!(env!("CARGO_MANIFEST_DIR"), "/src/en_us.json"));
|
||||
let mut file = File::open(src_dir).unwrap();
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
contents
|
||||
})
|
||||
.unwrap();
|
||||
}
|
||||
.unwrap()
|
||||
});
|
||||
|
||||
pub fn get(key: &str) -> Option<&str> {
|
||||
STORAGE.get(key).map(|s| s.as_str())
|
||||
|
|
|
@ -12,7 +12,7 @@ version = "0.3.0"
|
|||
azalea-block = {path = "../azalea-block", version = "^0.3.0"}
|
||||
azalea-core = {path = "../azalea-core", version = "^0.3.0"}
|
||||
azalea-world = {path = "../azalea-world", version = "^0.3.0"}
|
||||
lazy_static = "1.4.0"
|
||||
once_cell = "1.16.0"
|
||||
parking_lot = "^0.12.1"
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -49,14 +49,8 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report,
|
|||
# look at downloads/generator-mod-*/blockCollisionShapes.json for format of blocks and shapes
|
||||
|
||||
generated_shape_code = ''
|
||||
# we make several lazy_static! blocks so it doesn't complain about
|
||||
# recursion and hopefully the compiler can paralleize it?
|
||||
generated_shape_code += 'lazy_static! {'
|
||||
for i, (shape_id, shape) in enumerate(sorted(shapes.items(), key=lambda shape: int(shape[0]))):
|
||||
if i > 0 and i % 10 == 0:
|
||||
generated_shape_code += '}\nlazy_static! {'
|
||||
for (shape_id, shape) in sorted(shapes.items(), key=lambda shape: int(shape[0])):
|
||||
generated_shape_code += generate_code_for_shape(shape_id, shape)
|
||||
generated_shape_code += '}'
|
||||
|
||||
# BlockState::PurpurStairs_NorthTopStraightTrue => &SHAPE24,
|
||||
generated_match_inner_code = ''
|
||||
|
@ -93,11 +87,12 @@ def generate_block_shapes_code(blocks: dict, shapes: dict, block_states_report,
|
|||
// modify it, change that file.
|
||||
|
||||
#![allow(clippy::explicit_auto_deref)]
|
||||
#![allow(clippy::redundant_closure)]
|
||||
|
||||
use super::VoxelShape;
|
||||
use crate::collision::{{self, Shapes}};
|
||||
use azalea_block::*;
|
||||
use lazy_static::lazy_static;
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
pub trait BlockWithShape {{
|
||||
fn shape(&self) -> &'static VoxelShape;
|
||||
|
@ -119,7 +114,7 @@ def generate_code_for_shape(shape_id: str, parts: list[list[float]]):
|
|||
def make_arguments(part: list[float]):
|
||||
return ', '.join(map(lambda n: str(n).rstrip('0'), part))
|
||||
code = ''
|
||||
code += f'static ref SHAPE{shape_id}: VoxelShape = '
|
||||
code += f'static SHAPE{shape_id}: Lazy<VoxelShape> = Lazy::new(|| {{'
|
||||
steps = []
|
||||
if parts == []:
|
||||
steps.append('collision::empty_shape()')
|
||||
|
@ -136,5 +131,6 @@ def generate_code_for_shape(shape_id: str, parts: list[list[float]]):
|
|||
for step in steps[:-1]:
|
||||
code += f' let s = {step};\n'
|
||||
code += f' {steps[-1]}\n'
|
||||
code += '};\n'
|
||||
code += '}\n'
|
||||
code += '});\n'
|
||||
return code
|
||||
|
|
Loading…
Add table
Reference in a new issue