Skip to content

Commit 5b779c6

Browse files
committed
refactor: update overlay config fetching to only include the overlay config
1 parent 220ff35 commit 5b779c6

File tree

11 files changed

+83
-80
lines changed

11 files changed

+83
-80
lines changed

overlay/src/main.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import "./styles/app.css";
22
import "./vtftk/events";
33
import "./vtftk/calibration";
4-
import { getAppData } from "./vtftk/appData";
54
import { RuntimeAppData } from "./vtftk/types";
65
import { subscribeEvent } from "./vtube-studio/event";
76
import { attemptAuthorization } from "./vtube-studio/auth";
87
import { VTubeStudioWebSocket } from "./vtube-studio/socket";
98
import { EventSourceData, createEventSource } from "./vtftk/events";
10-
import { updateRuntimeData, getCalibrationData } from "./vtftk/api";
9+
import {
10+
getOverlayConfig,
11+
updateRuntimeData,
12+
getCalibrationData,
13+
} from "./vtftk/api";
1114
import {
1215
requestCurrentModel,
1316
createModelParameters,
@@ -22,10 +25,10 @@ async function load() {
2225
vtube_studio_auth: false,
2326
});
2427

25-
const appData = await getAppData();
28+
const overlayConfig = await getOverlayConfig();
2629

2730
const eventSourceData: EventSourceData = {
28-
appData,
31+
overlayConfig: overlayConfig,
2932
modelCalibration: new Map(),
3033
vtSocket: undefined,
3134
modelParameters: undefined,
@@ -40,8 +43,8 @@ async function load() {
4043
const eventSource = createEventSource(eventSourceData);
4144

4245
const vtSocket = new VTubeStudioWebSocket(
43-
appData.vtube_studio_config.host,
44-
appData.vtube_studio_config.port,
46+
overlayConfig.vtube_studio_config.host,
47+
overlayConfig.vtube_studio_config.port,
4548
);
4649

4750
eventSourceData.vtSocket = vtSocket;

overlay/src/vtftk/api.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import { BACKEND_HTTP } from "../constants";
22
import { base64ArrayBuffer } from "../utils/base64";
3-
import { ModelData, RuntimeAppData } from "./types";
43
import { CalibrationStepData } from "./calibration-types";
4+
import { ModelData, OverlayConfig, RuntimeAppData } from "./types";
5+
6+
export async function getOverlayConfig(): Promise<OverlayConfig> {
7+
const response = await fetch(new URL("/overlay/config", BACKEND_HTTP));
8+
const json: OverlayConfig = await response.json();
9+
return json;
10+
}
511

612
export async function notifyProgressCalibration(body: CalibrationStepData) {
713
const response = await fetch(new URL("/calibration", BACKEND_HTTP), {

overlay/src/vtftk/appData.ts

-17
This file was deleted.

overlay/src/vtftk/events.ts

+27-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ import {
1616
executeInterval,
1717
} from "../utils";
1818
import {
19-
AppData,
2019
ModelId,
20+
OverlayConfig,
2121
ItemWithSounds,
2222
ThrowItemConfig,
2323
ModelCalibration,
@@ -27,7 +27,7 @@ import {
2727
} from "./types";
2828

2929
export type EventSourceData = {
30-
appData: AppData;
30+
overlayConfig: OverlayConfig;
3131
vtSocket: VTubeStudioWebSocket | undefined;
3232
modelCalibration: Map<ModelId, ModelCalibration>;
3333
modelParameters: ModelParameters | undefined;
@@ -58,7 +58,7 @@ async function onMessage(data: EventSourceData, event: any) {
5858
case "ThrowItem": {
5959
if (data.vtSocket && data.modelParameters) {
6060
onThrowItemEvent(
61-
data.appData,
61+
data.overlayConfig,
6262
data.vtSocket,
6363
data.modelCalibration,
6464
data.modelParameters,
@@ -92,22 +92,22 @@ async function onMessage(data: EventSourceData, event: any) {
9292

9393
case "PlaySound": {
9494
if (data.vtSocket) {
95-
onPlaySoundEvent(data.appData, event.config);
95+
onPlaySoundEvent(data.overlayConfig, event.config);
9696
}
9797

9898
break;
9999
}
100100

101101
case "PlaySoundSeq": {
102102
if (data.vtSocket) {
103-
onPlaySoundSeqEvent(data.appData, event.configs);
103+
onPlaySoundSeqEvent(data.overlayConfig, event.configs);
104104
}
105105

106106
break;
107107
}
108108

109-
case "AppDataUpdated": {
110-
onAppDataUpdatedEvent(data, event.app_data);
109+
case "ConfigUpdated": {
110+
onConfigUpdatedEvent(data, event.config);
111111
break;
112112
}
113113

@@ -135,11 +135,14 @@ async function onMessage(data: EventSourceData, event: any) {
135135
}
136136
}
137137

138-
function onAppDataUpdatedEvent(data: EventSourceData, appData: AppData) {
139-
data.appData = appData;
138+
function onConfigUpdatedEvent(
139+
data: EventSourceData,
140+
overlayConfig: OverlayConfig,
141+
) {
142+
data.overlayConfig = overlayConfig;
140143

141144
// Recreate the physics engine
142-
const { fps, gravity_multiplier } = appData.physics_config;
145+
const { fps, gravity_multiplier } = overlayConfig.physics_config;
143146
setPhysicsEngineConfig({
144147
fps: fps,
145148
gravityMultiplier: gravity_multiplier,
@@ -159,14 +162,17 @@ async function onUpdateHotkeysEvent(vtSocket: VTubeStudioWebSocket) {
159162
return hotkeys;
160163
}
161164

162-
async function onPlaySoundEvent(appData: AppData, config: PartialSoundModel) {
165+
async function onPlaySoundEvent(
166+
overlayConfig: OverlayConfig,
167+
config: PartialSoundModel,
168+
) {
163169
const audio = await loadAudio(config.src);
164-
audio.volume = config.volume * appData.sounds_config.global_volume;
170+
audio.volume = config.volume * overlayConfig.sounds_config.global_volume;
165171
audio.play();
166172
}
167173

168174
async function onPlaySoundSeqEvent(
169-
appData: AppData,
175+
appData: OverlayConfig,
170176
configs: PartialSoundModel[],
171177
) {
172178
const sounds = await loadSounds(configs);
@@ -252,7 +258,7 @@ async function onMoveModelEvent(
252258
}
253259

254260
async function onThrowItemEvent(
255-
appData: AppData,
261+
overlayConfig: OverlayConfig,
256262
vtSocket: VTubeStudioWebSocket,
257263
modelCalibration: Map<ModelId, ModelCalibration>,
258264
modelParameters: ModelParameters,
@@ -267,7 +273,7 @@ async function onThrowItemEvent(
267273
if (config.type === ThrowItemConfigType.All) {
268274
await throwItemMany(
269275
vtSocket,
270-
appData,
276+
overlayConfig,
271277
modelCalibration,
272278
modelParameters,
273279
items.items,
@@ -280,7 +286,7 @@ async function onThrowItemEvent(
280286
async () => {
281287
return throwItemMany(
282288
vtSocket,
283-
appData,
289+
overlayConfig,
284290
modelCalibration,
285291
modelParameters,
286292
items.items,
@@ -341,7 +347,7 @@ function pickRandomItem(items: ItemWithSoundIds[], images: LoadedItemMap) {
341347

342348
function throwRandomItem(
343349
socket: VTubeStudioWebSocket,
344-
appData: AppData,
350+
overlayConfig: OverlayConfig,
345351
modelCalibration: Map<ModelId, ModelCalibration>,
346352
modelParameters: ModelParameters,
347353

@@ -366,7 +372,7 @@ function throwRandomItem(
366372

367373
return throwItem(
368374
socket,
369-
appData,
375+
overlayConfig,
370376
modelCalibration,
371377

372378
modelParameters,
@@ -379,7 +385,7 @@ function throwRandomItem(
379385

380386
async function throwItemMany(
381387
socket: VTubeStudioWebSocket,
382-
appData: AppData,
388+
overlayConfig: OverlayConfig,
383389
modelCalibration: Map<ModelId, ModelCalibration>,
384390
modelParameters: ModelParameters,
385391

@@ -391,7 +397,7 @@ async function throwItemMany(
391397
if (amount === 1) {
392398
return throwRandomItem(
393399
socket,
394-
appData,
400+
overlayConfig,
395401
modelCalibration,
396402
modelParameters,
397403
items,
@@ -404,7 +410,7 @@ async function throwItemMany(
404410
Array.from(Array(amount)).map(() =>
405411
throwRandomItem(
406412
socket,
407-
appData,
413+
overlayConfig,
408414
modelCalibration,
409415
modelParameters,
410416
items,

overlay/src/vtube-studio/throw-item.ts

+14-13
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import {
1111
} from "./physics";
1212
import {
1313
MinMax,
14-
AppData,
1514
ModelId,
15+
OverlayConfig,
1616
ThrowDirection,
1717
ItemImageConfig,
1818
ModelCalibration,
@@ -35,7 +35,7 @@ export function setPhysicsEngineConfig(config: PhysicsEngineConfig) {
3535
* Throws an item
3636
*
3737
* @param socket Socket for getting model position and sending impact flinches to VTube studio
38-
* @param appData Global app data settings
38+
* @param overlayConfig Global app data settings
3939
* @param modelCalibration Calibration data for available models
4040
* @param modelParameters Parameters for the current model
4141
* @param config Configuration for the thrown item
@@ -46,7 +46,7 @@ export function setPhysicsEngineConfig(config: PhysicsEngineConfig) {
4646
*/
4747
export async function throwItem(
4848
socket: VTubeStudioWebSocket,
49-
appData: AppData,
49+
overlayConfig: OverlayConfig,
5050
modelCalibration: Map<ModelId, ModelCalibration>,
5151
modelParameters: ModelParameters,
5252
config: ItemWithSoundIds,
@@ -64,7 +64,7 @@ export async function throwItem(
6464
// Model is not available
6565
if (!modelPosition) return;
6666

67-
const { throwables_config: throwables } = appData;
67+
const { throwables_config: throwables } = overlayConfig;
6868

6969
// Determine scale of the model relative to the calibrated minimum and maximum sizes
7070
const modelScale =
@@ -96,7 +96,7 @@ export async function throwItem(
9696
if (windupAudio !== null) {
9797
try {
9898
windupAudio.sound.volume =
99-
appData.sounds_config.global_volume * windupAudio.config.volume;
99+
overlayConfig.sounds_config.global_volume * windupAudio.config.volume;
100100
windupAudio.sound.play();
101101
} catch (err) {
102102
console.error("failed to play windup audio", err);
@@ -149,7 +149,7 @@ export async function throwItem(
149149
// Handle point of impact
150150
handleThrowableImpact(
151151
socket,
152-
appData,
152+
overlayConfig,
153153
modelParameters,
154154
config,
155155
impactAudio,
@@ -158,7 +158,7 @@ export async function throwItem(
158158
);
159159

160160
// No physics to apply
161-
if (!appData.physics_config.enabled) {
161+
if (!overlayConfig.physics_config.enabled) {
162162
// Wait remaining duration before removing
163163
await sleep(throwables.duration / 2);
164164
// Remove after complete
@@ -169,7 +169,7 @@ export async function throwItem(
169169

170170
// Initialize the physics engine
171171
if (physicsEngine === null) {
172-
const { fps, gravity_multiplier } = appData.physics_config;
172+
const { fps, gravity_multiplier } = overlayConfig.physics_config;
173173

174174
physicsEngine = createPhysicsEngine({
175175
fps: fps,
@@ -182,7 +182,8 @@ export async function throwItem(
182182
pivot.style.transform = "";
183183
thrown.style.transform = "";
184184

185-
const { horizontal_multiplier, vertical_multiplier } = appData.physics_config;
185+
const { horizontal_multiplier, vertical_multiplier } =
186+
overlayConfig.physics_config;
186187

187188
const randomVelocity = Math.random();
188189

@@ -245,7 +246,7 @@ function isRandomDirectionLeft(
245246
* Handles the point of impact for a throwable hitting the model
246247
*
247248
* @param socket Socket for sending impact flinches to VTube studio
248-
* @param appData Global app data settings
249+
* @param overlayConfig Global app data settings
249250
* @param modelParameters Parameters for the current model
250251
* @param config Configuration for the thrown item
251252
* @param impactAudio Audio element to play when the item impacts the target
@@ -254,7 +255,7 @@ function isRandomDirectionLeft(
254255
*/
255256
function handleThrowableImpact(
256257
socket: VTubeStudioWebSocket,
257-
appData: AppData,
258+
overlayConfig: OverlayConfig,
258259
modelParameters: ModelParameters,
259260
config: ItemWithSoundIds,
260261
impactAudio: LoadedSoundData | null,
@@ -265,7 +266,7 @@ function handleThrowableImpact(
265266
if (impactAudio !== null) {
266267
try {
267268
impactAudio.sound.volume =
268-
appData.sounds_config.global_volume * impactAudio.config.volume;
269+
overlayConfig.sounds_config.global_volume * impactAudio.config.volume;
269270

270271
impactAudio.sound.play();
271272
} catch (err) {
@@ -278,7 +279,7 @@ function handleThrowableImpact(
278279
// Make the VTuber model flinch from the impact
279280
flinch(socket, modelParameters, {
280281
angle,
281-
eyeState: appData.model_config.eyes_on_hit,
282+
eyeState: overlayConfig.model_config.eyes_on_hit,
282283
magnitude: image.weight,
283284
leftSide,
284285
returnSpeed: 0.3,

src-tauri/src/commands/data.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ pub async fn set_app_data(
5454
let model = AppDataModel::set(db.inner(), app_data).await?;
5555

5656
// Inform the overlay of the new app data
57-
_ = event_sender.send(OverlayMessage::AppDataUpdated {
58-
app_data: Box::new(model.data),
57+
_ = event_sender.send(OverlayMessage::ConfigUpdated {
58+
config: Box::new(model.data.overlay),
5959
});
6060

6161
Ok(true)

src-tauri/src/http/routes/data.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use crate::{
2-
database::entity::{
3-
app_data::{AppData, AppDataModel},
4-
vt_access::{SetVTAccess, VTAccessModel},
5-
},
2+
database::entity::vt_access::{SetVTAccess, VTAccessModel},
63
http::{
74
error::{DynHttpError, HttpResult},
85
models::{GetAuthTokenResponse, SetAuthTokenRequest},
@@ -20,16 +17,6 @@ use reqwest::header::{CACHE_CONTROL, CONTENT_TYPE};
2017
use sea_orm::{DatabaseConnection, ModelTrait};
2118
use tauri::{path::BaseDirectory, AppHandle, Manager};
2219

23-
/// GET /app-data
24-
///
25-
/// Obtain the current app data configuration. Contains stored
26-
/// state such as calibration and throwables configuration
27-
pub async fn get_app_data(Extension(db): Extension<DatabaseConnection>) -> HttpResult<AppData> {
28-
let data = AppDataModel::get_or_default(&db).await?;
29-
30-
Ok(Json(data))
31-
}
32-
3320
/// GET /content/:folder/:name
3421
///
3522
/// Retrieve the contents of a file from one of the content folders

0 commit comments

Comments
 (0)