Skip to content

Commit

Permalink
refactor tts to use middleware and constant data (#902)
Browse files Browse the repository at this point in the history
## Что этот PR делает

Рефактор префов ТТСа, перенос константной информации в json asset

## Почему это хорошо для игры

Возможно будет быстрее подгружаться меню префов
  • Loading branch information
Gaxeer authored Dec 23, 2024
1 parent 92096c7 commit 3fc9271
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 105 deletions.
3 changes: 2 additions & 1 deletion modular_bandastation/tts/_tts.dme
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include "code/tts_component.dm"
#include "code/tts_configuration.dm"
#include "code/tts_hear.dm"
#include "code/tts_preferences.dm"
#include "code/tts_provider.dm"
#include "code/tts_seed.dm"
#include "code/tts_subsystem.dm"
Expand All @@ -25,6 +24,8 @@
#include "code/base_seeds/mobs/pets.dm"
#include "code/base_seeds/mobs/spiders.dm"
#include "code/base_seeds/objs/objs.dm"
#include "code/preferences/tts_preference_middleware.dm"
#include "code/preferences/tts_preferences.dm"
#include "code/providers/silero.dm"
#include "code/seeds/base.dm"
#include "code/seeds/silero.dm"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
GLOBAL_LIST_EMPTY(human_to_tts)

/datum/preference_middleware/text_to_speech
action_delegations = list(
"listen" = PROC_REF(listen_voice),
"select_voice" = PROC_REF(select_voice),
)

/datum/preference_middleware/text_to_speech/get_ui_data(mob/user)
var/list/data = list()
data["tts_seed"] = preferences.read_preference(/datum/preference/text/tts_seed)
data["tts_enabled"] = CONFIG_GET(flag/tts_enabled)
return data

/datum/preference_middleware/text_to_speech/get_constant_data()
var/list/data = list()
data["providers"] = get_tts_providers_ui_data()
data["seeds"] = get_tts_seeds_ui_data()
data["phrases"] = TTS_PHRASES
return data

/datum/preference_middleware/text_to_speech/proc/get_tts_providers_ui_data()
var/list/providers = list()
for(var/_provider in SStts220.tts_providers)
var/datum/tts_provider/provider = SStts220.tts_providers[_provider]
providers += list(list(
"name" = provider.name,
"is_enabled" = provider.is_enabled,
))
return providers

/datum/preference_middleware/text_to_speech/proc/get_tts_seeds_ui_data()
var/list/seeds = list()
for(var/_seed in SStts220.tts_seeds)
var/datum/tts_seed/seed = SStts220.tts_seeds[_seed]
seeds += list(list(
"name" = seed.name,
"value" = seed.value,
"category" = seed.category,
"gender" = seed.gender,
"provider" = initial(seed.provider.name),
"donator_level" = seed.required_donator_level,
))
return seeds

/datum/preference_middleware/text_to_speech/proc/listen_voice(list/params, mob/user)
var/seed_name = params["seed"]
if(!seed_name)
return FALSE

var/datum/tts_seed/seed = SStts220.tts_seeds[seed_name]
if(!seed)
return FALSE

var/phrase = params["phrase"]
if(!phrase || !(phrase in TTS_PHRASES))
phrase = pick(TTS_PHRASES)

INVOKE_ASYNC(SStts220, TYPE_PROC_REF(/datum/controller/subsystem/tts220, get_tts), null, usr, phrase, seed, FALSE)

return FALSE

/datum/preference_middleware/text_to_speech/proc/select_voice(list/params, mob/user)
var/seed_name = params["seed"]
if(!seed_name || !SStts220.tts_seeds[seed_name])
return FALSE

preferences.update_preference(GLOB.preference_entries[/datum/preference/text/tts_seed], seed_name)
return TRUE
28 changes: 28 additions & 0 deletions modular_bandastation/tts/code/preferences/tts_preferences.dm
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/datum/preference/text/tts_seed
savefile_key = "tts_seed"
savefile_identifier = PREFERENCE_CHARACTER

/datum/preference/text/tts_seed/apply_to_human(mob/living/carbon/human/target, value)
var/datum/tts_seed/seed = SStts220.tts_seeds[value]
if(!seed)
seed = SStts220.tts_seeds[SStts220.get_random_seed(target)]

target.AddComponent(/datum/component/tts_component, seed)
target.dna.tts_seed_dna = seed
GLOB.human_to_tts["[target.real_name]"] = seed

/datum/preference/numeric/sound_tts_volume_radio
category = PREFERENCE_CATEGORY_GAME_PREFERENCES
savefile_key = "sound_tts_volume_radio"
savefile_identifier = PREFERENCE_PLAYER

minimum = 0
maximum = 200

/datum/preference/numeric/sound_tts_volume_radio/create_default_value()
return maximum / 2

/mob/living/carbon/human/randomize_human_appearance(randomize_flags)
. = ..()
var/datum/component/tts_component/tts_component = GetComponent(/datum/component/tts_component)
tts_component.tts_seed = tts_component.get_random_tts_seed_by_gender()
81 changes: 0 additions & 81 deletions modular_bandastation/tts/code/tts_preferences.dm

This file was deleted.

37 changes: 24 additions & 13 deletions tgui/packages/tgui/interfaces/PreferencesMenu/VoicePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
Icon,
Input,
LabeledList,
NoticeBox,
Section,
Stack,
Table,
VirtualList,
} from '../../components';
import { PreferencesMenuData, Seed } from './data';
import { PreferencesMenuData, Seed, ServerData, TtsData } from './data';
import { ServerPreferencesFetcher } from './ServerPreferencesFetcher';

const donatorTiers = {
0: 'Free',
Expand Down Expand Up @@ -51,7 +53,6 @@ const getCheckboxGroup = (
<Button.Checkbox
key={title}
checked={selectedList.includes(item)}
content={title}
onClick={() => {
if (selectedList.includes(item)) {
setSelected(
Expand All @@ -63,31 +64,41 @@ const getCheckboxGroup = (
setSelected([item, ...selectedList]);
}
}}
/>
>
{title}
</Button.Checkbox>
);
});
};

export const VoicePage = (props) => {
const { data } = useBackend<PreferencesMenuData>();
export const VoicePage = () => {
return (
<ServerPreferencesFetcher
render={(serverData: ServerData) => {
if (!serverData) {
return <NoticeBox>Loading...</NoticeBox>;
}
return <VoicePageInner text_to_speech={serverData.text_to_speech} />;
}}
/>
);
};

const {
providers,
seeds,
tts_seed,
phrases,
// donator_level,
// character_gender,
} = data;
const VoicePageInner = (props: { text_to_speech: TtsData }) => {
const { data } = useBackend<PreferencesMenuData>();
const { tts_seed } = data;
const { providers, seeds, phrases } = props.text_to_speech;

const donator_level = 5; // Remove after tiers implementation

const categories = seeds
.map((seed) => seed.category)
.filter((category, i, a) => a.indexOf(category) === i);

const genders = seeds
.map((seed) => seed.gender)
.filter((gender, i, a) => a.indexOf(gender) === i);

const donatorLevels = seeds
.map((seed) => seed.donator_level)
.filter((level, i, a) => a.indexOf(level) === i)
Expand Down
26 changes: 16 additions & 10 deletions tgui/packages/tgui/interfaces/PreferencesMenu/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,11 @@ export type PreferencesMenuData = {

window: Window;

// BANDASTATION EDIT START - TTS
tts_seed: string;
tts_enabled: BooleanLike;
providers: Array<{
name: string;
is_enabled: BooleanLike;
}>;
seeds: Array<Seed>;
phrases: string[];
tts_seed: string; // BANDASTATION ADD
tts_enabled: BooleanLike; // BANDASTATION ADD
};

// BANDASTATION EDIT START
export type Seed = {
name: string;
value: string;
Expand All @@ -201,9 +195,21 @@ export type Seed = {
donator_level: number;
};

// BANDASTATION EDIT END - TTS
export type TtsProvider = {
name: string;
is_enabled: BooleanLike;
};

export type TtsData = {
providers: Array<TtsProvider>;
seeds: Array<Seed>;
phrases: string[];
};

// BANDASTATION EDIT END

export type ServerData = {
text_to_speech: TtsData; // BANDASTATION ADD
jobs: {
departments: Record<string, Department>;
jobs: Record<string, Job>;
Expand Down

0 comments on commit 3fc9271

Please sign in to comment.