Skip to content

Commit

Permalink
🐛 fix languages related issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Roms1383 committed Nov 28, 2024
1 parent aee825c commit 0971b8e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 107 deletions.
14 changes: 12 additions & 2 deletions crates/audioware/reds/Codeware.reds
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ public class LocalizationProvider extends ModLocalizationProvider {
let system = LocalizationSystem.GetInstance(this.GetGameInstance());
let spoken = system.GetVoiceLanguage();
let written = system.GetSubtitleLanguage();
FTLog(s"update locales: spoken: \(NameToString(spoken)), written: \(NameToString(written))");
SetGameLocales(spoken, written);
}
public func OnGenderChange() {
let system = LocalizationSystem.GetInstance(this.GetGameInstance());
let gender = system.GetPlayerGender();
FTLog(s"update player gender: \(ToString(gender))");
SetPlayerGender(gender);
}
public func GetPackage(language: CName) -> ref<ModLocalizationPackage> {
Expand All @@ -33,6 +31,18 @@ public class LocalizationProvider extends ModLocalizationProvider {
public func GetFallback() -> CName = n"";
}

class LocalizationService extends ScriptableService {
private cb func OnLoad() {
GameInstance.GetCallbackSystem()
.RegisterCallback(n"Session/Ready", this, n"OnSessionReady")
.SetRunMode(CallbackRunMode.Once);
}
private cb func OnSessionReady(event: ref<GameSessionEvent>) {
let provider = GameInstance.GetScriptableSystemsContainer(GetGameInstance()).Get(n"Audioware.LocalizationProvider") as LocalizationProvider;
provider.OnLocaleChange();
}
}

private func PropagateSubtitle(reaction: CName, entityID: EntityID, emitterName: CName, lineType: scnDialogLineType, duration: Float) -> Void {
if !IsNameValid(reaction) || !EntityID.IsDefined(entityID) { return; }
let target = GameInstance.FindEntityByID(GetGameInstance(), entityID);
Expand Down
4 changes: 0 additions & 4 deletions crates/audioware/src/abi/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use kira::spatial::emitter::EmitterSettings;
use red4ext_rs::types::{CName, EntityId};

mod board;
mod codeware;
mod session;
mod system;
pub use board::Board;
pub use codeware::Codeware;
pub use session::Session;
pub use system::System;

Expand Down Expand Up @@ -60,7 +58,6 @@ pub enum Lifecycle {
Session(Session),
System(System),
Board(Board),
Codeware(Codeware),
ReportInitialization,
#[cfg(feature = "hot-reload")]
HotReload,
Expand Down Expand Up @@ -91,7 +88,6 @@ impl std::fmt::Display for Lifecycle {
Lifecycle::SetVolume { setting, value } => {
write!(f, "set volume {} {value}", setting.as_str())
}
Lifecycle::Codeware(x) => write!(f, "{x}"),
Lifecycle::SetListenerDilation { value: dilation, reason, ease_in_curve } => {
write!(f, "set listener dilation {dilation}, reason: {reason}, curve: {ease_in_curve}")
}
Expand Down
21 changes: 0 additions & 21 deletions crates/audioware/src/abi/lifecycle/codeware.rs

This file was deleted.

35 changes: 25 additions & 10 deletions crates/audioware/src/abi/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use audioware_manifest::{PlayerGender, ScnDialogLineType};
use audioware_manifest::{Locale, PlayerGender, ScnDialogLineType};
use command::Command;
use crossbeam::channel::bounded;
use kira::manager::backend::cpal::CpalBackend;
use lifecycle::{Board, Codeware, Lifecycle, Session, System};
use lifecycle::{Board, Lifecycle, Session, System};
use red4ext_rs::{
exports, methods,
types::{CName, EntityId, IScriptable, Opt, Ref},
Expand All @@ -11,7 +11,7 @@ use red4ext_rs::{
};

use crate::{
engine::{eq::Preset, Engine},
engine::{eq::Preset, state, Engine},
queue,
utils::{fails, lifecycle, warns},
Audioware, EmitterSettings, LocalizationPackage, ToTween, Tween,
Expand Down Expand Up @@ -225,22 +225,37 @@ impl ListenerLifecycle for Audioware {

impl CodewareLifecycle for Audioware {
fn set_player_gender(gender: PlayerGender) {
queue::notify(Lifecycle::Codeware(Codeware::SetPlayerGender { gender }));
state::PlayerGender::set(gender);
}

fn unset_player_gender() {
queue::notify(Lifecycle::Codeware(Codeware::UnsetPlayerGender));
state::PlayerGender::unset();
}

fn set_game_locales(spoken: CName, written: CName) {
queue::notify(Lifecycle::Codeware(Codeware::SetGameLocales {
spoken,
written,
}));
match Locale::try_from(spoken) {
Ok(spoken) => state::SpokenLocale::set(spoken),
Err(e) => fails!("failed to set spoken locale: {e}"),
};
match Locale::try_from(written) {
Ok(written) => state::WrittenLocale::set(written),
Err(e) => fails!("failed to set written locale: {e}"),
};
}

fn define_subtitles(package: Ref<LocalizationPackage>) {
Engine::<CpalBackend>::define_subtitles(package);
use crate::types::Subtitle;
use audioware_bank::BankSubtitles;
let written = state::WrittenLocale::get();
lifecycle!("define localization package subtitles for {written}");
if let Some(banks) = Engine::<CpalBackend>::banks().as_ref() {
let subtitles = banks.subtitles(written);
for (key, (value_f, value_m)) in subtitles.iter() {
package.subtitle(key.as_str(), value_f.as_str(), value_m.as_str());
}
} else {
warns!("banks aren't initialized yet, skipping subtitles definition");
}
}

fn supported_languages() -> Vec<CName> {
Expand Down
44 changes: 4 additions & 40 deletions crates/audioware/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use kira::{
OutputDestination,
};
use modulators::{Modulators, Parameter};
use red4ext_rs::types::{CName, EntityId, GameInstance, Opt, Ref};
use red4ext_rs::types::{CName, EntityId, GameInstance, Opt};
use scene::Scene;
use state::SpokenLocale;
use state::{SpokenLocale, ToGender};
use tracks::Tracks;
use tweens::{
DEFAULT, DILATION_EASE_IN, DILATION_EASE_OUT, DILATION_LINEAR, IMMEDIATELY, LAST_BREATH,
Expand All @@ -25,20 +25,18 @@ use crate::{
error::{EngineError, Error},
propagate_subtitles,
utils::{fails, lifecycle, success, warns},
AsAudioSystem, AsGameInstance, LocalizationPackage,
AsAudioSystem, AsGameInstance,
};

pub mod eq;
pub mod queue;
pub mod state;

mod modulators;
mod scene;
mod state;
mod tracks;
mod tweens;

pub use state::ToGender;

#[cfg(not(feature = "hot-reload"))]
static BANKS: std::sync::OnceLock<Banks> = std::sync::OnceLock::new();
#[cfg(feature = "hot-reload")]
Expand Down Expand Up @@ -575,25 +573,6 @@ where
}
}

pub fn set_gender(&mut self, gender: audioware_manifest::PlayerGender) {
state::PlayerGender::set(gender);
}

pub fn unset_gender(&mut self) {
state::PlayerGender::unset();
}

pub fn set_locales(&mut self, spoken: CName, written: CName) {
match Locale::try_from(spoken) {
Ok(spoken) => state::SpokenLocale::set(spoken),
Err(e) => fails!("failed to set spoken locale: {e}"),
};
match Locale::try_from(written) {
Ok(written) => state::WrittenLocale::set(written),
Err(e) => fails!("failed to set written locale: {e}"),
};
}

pub fn duration(
event_name: CName,
locale: Locale,
Expand All @@ -615,21 +594,6 @@ where
vec![]
}

pub fn define_subtitles(package: Ref<LocalizationPackage>) {
use crate::types::Subtitle;
use audioware_bank::BankSubtitles;
lifecycle!("Codeware request to define subtitles");
let written = state::WrittenLocale::get();
if let Some(banks) = Self::banks().as_ref() {
let subtitles = banks.subtitles(written);
for (key, (value_f, value_m)) in subtitles.iter() {
package.subtitle(key.as_str(), value_f.as_str(), value_m.as_str());
}
} else {
warns!("banks aren't initialized yet, skipping subtitles definition");
}
}

#[cfg(not(feature = "hot-reload"))]
pub fn banks<'a>() -> Option<&'a Banks> {
BANKS.get()
Expand Down
9 changes: 1 addition & 8 deletions crates/audioware/src/engine/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use std::sync::{Mutex, RwLock};
use crate::{
abi::{
command::Command,
lifecycle::{Board, Codeware, Lifecycle, Session, System},
lifecycle::{Board, Lifecycle, Session, System},
},
config::BufferSize,
engine::DilationUpdate,
Expand Down Expand Up @@ -142,13 +142,6 @@ pub fn run(rl: Receiver<Lifecycle>, rc: Receiver<Command>, mut engine: Engine<Cp
ease_out_curve,
},
),
Lifecycle::Codeware(Codeware::SetPlayerGender { gender }) => {
engine.set_gender(gender)
}
Lifecycle::Codeware(Codeware::UnsetPlayerGender) => engine.unset_gender(),
Lifecycle::Codeware(Codeware::SetGameLocales { spoken, written }) => {
engine.set_locales(spoken, written)
}
Lifecycle::RegisterEmitter {
entity_id,
emitter_name,
Expand Down
22 changes: 17 additions & 5 deletions crates/audioware/src/engine/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicU32, AtomicU8};
use audioware_manifest::{Locale, LocaleExt};
use red4ext_rs::types::{EntityId, GameInstance};

use crate::{AsGameInstance, PlayerPuppet};
use crate::{utils::warns, AsGameInstance, PlayerPuppet};

use super::scene::AsEntityExt;

Expand All @@ -27,11 +27,17 @@ impl SpokenLocale {
.store(value.into(), std::sync::atomic::Ordering::Release);
}
pub fn get() -> audioware_manifest::SpokenLocale {
SPOKEN_LOCALE
match SPOKEN_LOCALE
.0
.load(std::sync::atomic::Ordering::Acquire)
.try_into()
.expect("checked on set")
{
Ok(x) => x,
Err(e) => {
warns!("invalid spoken locale in state: {}", e);
Locale::English.into()
}
}
}
}

Expand All @@ -42,11 +48,17 @@ impl WrittenLocale {
.store(value.into(), std::sync::atomic::Ordering::Release);
}
pub fn get() -> audioware_manifest::WrittenLocale {
WRITTEN_LOCALE
match WRITTEN_LOCALE
.0
.load(std::sync::atomic::Ordering::Acquire)
.try_into()
.expect("checked on set")
{
Ok(x) => x,
Err(e) => {
warns!("invalid written locale in state: {}", e);
Locale::English.into()
}
}
}
}

Expand Down
35 changes: 18 additions & 17 deletions crates/manifest/src/types/locale.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,23 +397,24 @@ impl TryFrom<u32> for Locale {

fn try_from(value: u32) -> Result<Self, Self::Error> {
match value {
0 => Ok(Self::English),
1 => Ok(Self::Spanish),
2 => Ok(Self::French),
3 => Ok(Self::Italian),
4 => Ok(Self::German),
5 => Ok(Self::LatinAmericanSpanish),
6 => Ok(Self::Korean),
7 => Ok(Self::SimplifiedChinese),
8 => Ok(Self::Russian),
9 => Ok(Self::BrazilianPortuguese),
10 => Ok(Self::Japanese),
11 => Ok(Self::TraditionalChinese),
12 => Ok(Self::Arabic),
13 => Ok(Self::Czech),
14 => Ok(Self::Hungarian),
15 => Ok(Self::Turkish),
16 => Ok(Self::Thai),
x if x == Self::Polish as u32 => Ok(Self::Polish),
x if x == Self::English as u32 => Ok(Self::English),
x if x == Self::Spanish as u32 => Ok(Self::Spanish),
x if x == Self::French as u32 => Ok(Self::French),
x if x == Self::Italian as u32 => Ok(Self::Italian),
x if x == Self::German as u32 => Ok(Self::German),
x if x == Self::LatinAmericanSpanish as u32 => Ok(Self::LatinAmericanSpanish),
x if x == Self::Korean as u32 => Ok(Self::Korean),
x if x == Self::SimplifiedChinese as u32 => Ok(Self::SimplifiedChinese),
x if x == Self::Russian as u32 => Ok(Self::Russian),
x if x == Self::BrazilianPortuguese as u32 => Ok(Self::BrazilianPortuguese),
x if x == Self::Japanese as u32 => Ok(Self::Japanese),
x if x == Self::TraditionalChinese as u32 => Ok(Self::TraditionalChinese),
x if x == Self::Arabic as u32 => Ok(Self::Arabic),
x if x == Self::Czech as u32 => Ok(Self::Czech),
x if x == Self::Hungarian as u32 => Ok(Self::Hungarian),
x if x == Self::Turkish as u32 => Ok(Self::Turkish),
x if x == Self::Thai as u32 => Ok(Self::Thai),
_ => Err(Self::Error::InvalidLocale {
value: value.to_string(),
}),
Expand Down

0 comments on commit 0971b8e

Please sign in to comment.