refactor store

This commit is contained in:
pa
2026-03-10 13:55:03 +09:00
parent 2fffadfbcf
commit d7220baaf6
47 changed files with 1993 additions and 1750 deletions
+41 -211
View File
@@ -1,38 +1,9 @@
import { reactive, ref } from 'vue';
import { defineStore } from 'pinia';
import { toast } from 'vue-sonner';
import {
deleteVRChatCache as _deleteVRChatCache,
isRealInstance
} from '../shared/utils';
import { database } from '../service/database';
import { runGameRunningChangedFlow } from '../coordinators/gameCoordinator';
import { useAdvancedSettingsStore } from './settings/advanced';
import { useAvatarStore } from './avatar';
import { useGameLogStore } from './gameLog';
import { useLaunchStore } from './launch';
import { useLocationStore } from './location';
import { useModalStore } from './modal';
import { useNotificationStore } from './notification';
import { useVrStore } from './vr';
import { useWorldStore } from './world';
import configRepository from '../service/config.js';
import * as workerTimers from 'worker-timers';
export const useGameStore = defineStore('Game', () => {
const advancedSettingsStore = useAdvancedSettingsStore();
const locationStore = useLocationStore();
const notificationStore = useNotificationStore();
const avatarStore = useAvatarStore();
const launchStore = useLaunchStore();
const worldStore = useWorldStore();
const gameLogStore = useGameLogStore();
const vrStore = useVrStore();
const modalStore = useModalStore();
const state = reactive({
lastCrashedTime: null
});
@@ -60,139 +31,13 @@ export const useGameStore = defineStore('Game', () => {
init();
/**
* @param {object} ref Avatar or world reference payload.
*/
async function deleteVRChatCache(ref) {
await _deleteVRChatCache(ref);
getVRChatCacheSize();
worldStore.updateVRChatWorldCache();
avatarStore.updateVRChatAvatarCache();
}
// --- Atomic setters ---
/**
*
* @param {boolean} value Game running flag.
*/
function autoVRChatCacheManagement() {
if (advancedSettingsStore.autoSweepVRChatCache) {
sweepVRChatCache();
}
}
/**
*
*/
async function sweepVRChatCache() {
try {
const output = await AssetBundleManager.SweepCache();
console.log('SweepCache', output);
} catch (e) {
console.error('SweepCache failed', e);
}
if (advancedSettingsStore.isVRChatConfigDialogVisible) {
getVRChatCacheSize();
}
}
/**
*
*/
function checkIfGameCrashed() {
if (!advancedSettingsStore.relaunchVRChatAfterCrash) {
return;
}
const { location } = locationStore.lastLocation;
AppApi.VrcClosedGracefully().then((result) => {
if (result || !isRealInstance(location)) {
return;
}
// check if relaunched less than 2mins ago (prvent crash loop)
if (
state.lastCrashedTime &&
new Date().getTime() - state.lastCrashedTime.getTime() < 120_000
) {
console.log('VRChat was recently crashed, not relaunching');
return;
}
state.lastCrashedTime = new Date();
// wait a bit for SteamVR to potentially close before deciding to relaunch
let restartDelay = 8000;
if (isGameNoVR.value) {
// wait for game to close before relaunching
restartDelay = 2000;
}
workerTimers.setTimeout(
() => restartCrashedGame(location),
restartDelay
);
});
}
/**
* @param {string} location Last known location to relaunch.
*/
function restartCrashedGame(location) {
if (!isGameNoVR.value && !isSteamVRRunning.value) {
console.log("SteamVR isn't running, not relaunching VRChat");
return;
}
AppApi.FocusWindow();
const message = 'VRChat crashed, attempting to rejoin last instance';
toast(message);
const entry = {
created_at: new Date().toJSON(),
type: 'Event',
data: message
};
database.addGamelogEventToDatabase(entry);
notificationStore.queueGameLogNoty(entry);
gameLogStore.addGameLog(entry);
launchStore.launchGame(location, '', isGameNoVR.value);
}
/**
*
*/
async function getVRChatCacheSize() {
VRChatCacheSizeLoading.value = true;
const totalCacheSize = 30;
VRChatTotalCacheSize.value = totalCacheSize;
const usedCacheSize = await AssetBundleManager.GetCacheSize();
VRChatUsedCacheSize.value = (usedCacheSize / 1073741824).toFixed(2);
VRChatCacheSizeLoading.value = false;
}
// use in C#
/**
* @param {boolean} isGameRunningArg Game running flag from IPC.
* @param {boolean} isSteamVRRunningArg SteamVR running flag from IPC.
*/
async function updateIsGameRunning(isGameRunningArg, isSteamVRRunningArg) {
if (advancedSettingsStore.gameLogDisabled) {
return;
}
if (isGameRunningArg !== isGameRunning.value) {
isGameRunning.value = isGameRunningArg;
await runGameRunningChangedFlow(isGameRunningArg);
console.log(new Date(), 'isGameRunning', isGameRunningArg);
}
if (isSteamVRRunningArg !== isSteamVRRunning.value) {
isSteamVRRunning.value = isSteamVRRunningArg;
console.log('isSteamVRRunning:', isSteamVRRunningArg);
}
vrStore.updateOpenVR();
}
// use in C#
/**
* @param {boolean} isHmdAfkArg HMD AFK flag from VR polling.
*/
function updateIsHmdAfk(isHmdAfkArg) {
if (isHmdAfkArg !== isHmdAfk.value) {
isHmdAfk.value = isHmdAfkArg;
console.log('isHmdAfk', isHmdAfkArg);
}
function setIsGameRunning(value) {
isGameRunning.value = value;
}
/**
@@ -203,50 +48,38 @@ export const useGameStore = defineStore('Game', () => {
}
/**
*
* @param {boolean} value SteamVR running flag.
*/
async function checkVRChatDebugLogging() {
if (advancedSettingsStore.gameLogDisabled) {
return;
}
try {
const loggingEnabled =
await getVRChatRegistryKey('LOGGING_ENABLED');
if (
loggingEnabled === null ||
typeof loggingEnabled === 'undefined'
) {
// key not found
return;
}
if (parseInt(loggingEnabled, 10) === 1) {
// already enabled
return;
}
const result = await AppApi.SetVRChatRegistryKey(
'LOGGING_ENABLED',
'1',
4
);
if (!result) {
// failed to set key
modalStore.alert({
description:
'VRCX has noticed VRChat debug logging is disabled. VRCX requires debug logging in order to function correctly. Please enable debug logging in VRChat quick menu settings > debug > enable debug logging, then rejoin the instance or restart VRChat.',
title: 'Enable debug logging'
});
console.error('Failed to enable debug logging', result);
return;
}
modalStore.alert({
description:
'VRCX has noticed VRChat debug logging is disabled and automatically re-enabled it. VRCX requires debug logging in order to function correctly.',
title: 'Enabled debug logging'
});
console.log('Enabled debug logging');
} catch (e) {
console.error(e);
}
function setIsSteamVRRunning(value) {
isSteamVRRunning.value = value;
}
/**
* @param {boolean} value HMD AFK flag.
*/
function setIsHmdAfk(value) {
isHmdAfk.value = value;
}
/**
* @param {Date | null} value Last crashed time.
*/
function setLastCrashedTime(value) {
state.lastCrashedTime = value;
}
// --- Self-contained operations (no cross-store deps) ---
/**
* Fetches VRChat cache size from AssetBundleManager.
*/
async function getVRChatCacheSize() {
VRChatCacheSizeLoading.value = true;
const totalCacheSize = 30;
VRChatTotalCacheSize.value = totalCacheSize;
const usedCacheSize = await AssetBundleManager.GetCacheSize();
VRChatUsedCacheSize.value = (usedCacheSize / 1073741824).toFixed(2);
VRChatCacheSizeLoading.value = false;
}
/**
@@ -271,15 +104,12 @@ export const useGameStore = defineStore('Game', () => {
isSteamVRRunning,
isHmdAfk,
deleteVRChatCache,
sweepVRChatCache,
getVRChatCacheSize,
updateIsGameRunning,
setIsGameRunning,
setIsGameNoVR,
getVRChatRegistryKey,
checkVRChatDebugLogging,
autoVRChatCacheManagement,
checkIfGameCrashed,
updateIsHmdAfk
setIsSteamVRRunning,
setIsHmdAfk,
setLastCrashedTime,
getVRChatCacheSize,
getVRChatRegistryKey
};
});