diff --git a/gui/app/src/components/commons/BodyDisplay.tsx b/gui/app/src/components/commons/BodyDisplay.tsx index 69d6bac42..ad783d871 100644 --- a/gui/app/src/components/commons/BodyDisplay.tsx +++ b/gui/app/src/components/commons/BodyDisplay.tsx @@ -1,6 +1,6 @@ import classNames from 'classnames'; import { useEffect, useMemo, useRef, useState } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { PersonFrontIcon } from './PersonFrontIcon'; import { boneRegistryAtom, diff --git a/gui/app/src/components/commons/BodyInteractions.tsx b/gui/app/src/components/commons/BodyInteractions.tsx index b72e8a0b0..cc6c8946d 100644 --- a/gui/app/src/components/commons/BodyInteractions.tsx +++ b/gui/app/src/components/commons/BodyInteractions.tsx @@ -9,7 +9,7 @@ import { useRef, useState, } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { useBreakpoint } from '@/hooks/breakpoint'; const DOT_HIT_PADDING = 12; @@ -20,10 +20,10 @@ export interface BodySideNames { } const bodyPartFromName = (name: string): BodyPart | null => { - const part = (BodyPart as unknown as Record)[ + const part = (BodyPart as unknown as Record)[ name ]; - return typeof part === 'number' ? part : null; + return typeof part === 'string' ? (part as BodyPart) : null; }; const boxOf = (el: HTMLElement, offset: { left: number; top: number }) => { diff --git a/gui/app/src/components/commons/BodyPartIcon.tsx b/gui/app/src/components/commons/BodyPartIcon.tsx index e1e3e1ca9..fa3da5303 100644 --- a/gui/app/src/components/commons/BodyPartIcon.tsx +++ b/gui/app/src/components/commons/BodyPartIcon.tsx @@ -1,4 +1,5 @@ -import { BoardType, BodyPart, DeviceDataT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { BoardType, DeviceDataT } from 'solarxr-protocol'; import { useLocaleConfig } from '@/i18n/config'; import { AnkleIcon } from './icon/AnkleIcon'; import { ButterflyIcon } from './icon/ButterflyIcon'; diff --git a/gui/app/src/components/commons/ExtremityFigure.tsx b/gui/app/src/components/commons/ExtremityFigure.tsx index 96f5cbd52..19ffd244e 100644 --- a/gui/app/src/components/commons/ExtremityFigure.tsx +++ b/gui/app/src/components/commons/ExtremityFigure.tsx @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { ExtremityFigureSpec, ExtremityParts, diff --git a/gui/app/src/components/commons/PersonFrontIcon.tsx b/gui/app/src/components/commons/PersonFrontIcon.tsx index f82583b83..a5883544f 100644 --- a/gui/app/src/components/commons/PersonFrontIcon.tsx +++ b/gui/app/src/components/commons/PersonFrontIcon.tsx @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; export const SIDES = [ { diff --git a/gui/app/src/components/firmware-update/FirmwareUpdate.tsx b/gui/app/src/components/firmware-update/FirmwareUpdate.tsx index 62c935074..10bdcf8df 100644 --- a/gui/app/src/components/firmware-update/FirmwareUpdate.tsx +++ b/gui/app/src/components/firmware-update/FirmwareUpdate.tsx @@ -2,8 +2,8 @@ import { Localized, ReactLocalization, useLocalization } from '@fluent/react'; import { Typography } from '@/components/commons/Typography'; import { getLocalizedTrackerName } from '@/hooks/tracker'; import { ComponentProps, useEffect, useMemo, useRef, useState } from 'react'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, BoardType, DeviceDataT, FirmwareDeviceIdTableT, diff --git a/gui/app/src/components/onboarding/BodyPartAssignment.tsx b/gui/app/src/components/onboarding/BodyPartAssignment.tsx index 9928cba8a..06fca7afc 100644 --- a/gui/app/src/components/onboarding/BodyPartAssignment.tsx +++ b/gui/app/src/components/onboarding/BodyPartAssignment.tsx @@ -7,7 +7,7 @@ import { useMemo, } from 'react'; import { useAtomValue } from 'jotai'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { BodyInteractions, BodyInteractionsProps, diff --git a/gui/app/src/components/onboarding/extremities/foot.tsx b/gui/app/src/components/onboarding/extremities/foot.tsx index 107da6f18..1898b5386 100644 --- a/gui/app/src/components/onboarding/extremities/foot.tsx +++ b/gui/app/src/components/onboarding/extremities/foot.tsx @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { ExtremityDescriptor } from '@/utils/extremities'; import { Digit, diff --git a/gui/app/src/components/onboarding/extremities/hand.tsx b/gui/app/src/components/onboarding/extremities/hand.tsx index 4bbd0d44c..c962b105e 100644 --- a/gui/app/src/components/onboarding/extremities/hand.tsx +++ b/gui/app/src/components/onboarding/extremities/hand.tsx @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { ExtremityDescriptor } from '@/utils/extremities'; import { Digit, diff --git a/gui/app/src/components/onboarding/pages/body-proportions/ManualProportions.tsx b/gui/app/src/components/onboarding/pages/body-proportions/ManualProportions.tsx index c5989b305..4ec76ff16 100644 --- a/gui/app/src/components/onboarding/pages/body-proportions/ManualProportions.tsx +++ b/gui/app/src/components/onboarding/pages/body-proportions/ManualProportions.tsx @@ -1,7 +1,7 @@ import { Control, Controller, useForm } from 'react-hook-form'; import { Clickable } from '@/components/commons/Clickable'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, ChangeSkeletonProportionsRequestT, ResetType, RpcMessage, diff --git a/gui/app/src/components/onboarding/pages/mounting/ManualMounting.tsx b/gui/app/src/components/onboarding/pages/mounting/ManualMounting.tsx index 80a498462..e7c757a1b 100644 --- a/gui/app/src/components/onboarding/pages/mounting/ManualMounting.tsx +++ b/gui/app/src/components/onboarding/pages/mounting/ManualMounting.tsx @@ -1,5 +1,5 @@ import { ReactNode } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { Localized } from '@fluent/react'; import { useOnboarding } from '@/hooks/onboarding'; import { useMountingSelection } from '@/hooks/tracker-mounting'; diff --git a/gui/app/src/components/onboarding/pages/mounting/MountingPartCard.tsx b/gui/app/src/components/onboarding/pages/mounting/MountingPartCard.tsx index cff030dea..0a003be83 100644 --- a/gui/app/src/components/onboarding/pages/mounting/MountingPartCard.tsx +++ b/gui/app/src/components/onboarding/pages/mounting/MountingPartCard.tsx @@ -1,5 +1,6 @@ import classNames from 'classnames'; -import { BodyPart, QuatT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { QuatT } from 'solarxr-protocol'; import { Typography } from '@/components/commons/Typography'; import { ArrowUpIcon } from '@/components/commons/icon/ArrowIcons'; import { diff --git a/gui/app/src/components/onboarding/pages/mounting/MountingSelectionMenu.tsx b/gui/app/src/components/onboarding/pages/mounting/MountingSelectionMenu.tsx index d904cc9c8..d57527e00 100644 --- a/gui/app/src/components/onboarding/pages/mounting/MountingSelectionMenu.tsx +++ b/gui/app/src/components/onboarding/pages/mounting/MountingSelectionMenu.tsx @@ -8,7 +8,7 @@ import { FootIcon } from '@/components/commons/icon/FootIcon'; import { rotationToQuatMap, similarQuaternions } from '@/maths/quaternion'; import { Quaternion } from 'three'; import { SlimeUpIcon } from '@/components/commons/icon/SlimeUpIcon'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { useLocaleConfig } from '@/i18n/config'; import { FingersIcon } from '@/components/commons/icon/FingersIcon'; import { diff --git a/gui/app/src/components/onboarding/pages/trackers-assign/BodyAssignmentPanel.tsx b/gui/app/src/components/onboarding/pages/trackers-assign/BodyAssignmentPanel.tsx index 49b757d01..706561a77 100644 --- a/gui/app/src/components/onboarding/pages/trackers-assign/BodyAssignmentPanel.tsx +++ b/gui/app/src/components/onboarding/pages/trackers-assign/BodyAssignmentPanel.tsx @@ -2,7 +2,7 @@ import classNames from 'classnames'; import { useLocalization } from '@fluent/react'; import { Clickable } from '@/components/commons/Clickable'; import { HTMLAttributes, ReactNode, useCallback, useMemo } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { MirrorLegend } from '@/components/onboarding/BodyAssignment'; import { BodyPartAssignment, diff --git a/gui/app/src/components/onboarding/pages/trackers-assign/MobileTrackerAssign.tsx b/gui/app/src/components/onboarding/pages/trackers-assign/MobileTrackerAssign.tsx index b74b06a2a..feed7a0bd 100644 --- a/gui/app/src/components/onboarding/pages/trackers-assign/MobileTrackerAssign.tsx +++ b/gui/app/src/components/onboarding/pages/trackers-assign/MobileTrackerAssign.tsx @@ -2,7 +2,7 @@ import type { FluentVariable } from '@fluent/bundle'; import { useLocalization } from '@fluent/react'; import classNames from 'classnames'; import { CSSProperties, ReactNode, useMemo } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { Button } from '@/components/commons/Button'; import { ProgressBar } from '@/components/commons/ProgressBar'; import { Typography } from '@/components/commons/Typography'; diff --git a/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignment.tsx b/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignment.tsx index feb948847..a4aa279dd 100644 --- a/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignment.tsx +++ b/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignment.tsx @@ -1,6 +1,6 @@ import classNames from 'classnames'; import { createPortal } from 'react-dom'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { BaseModal } from '@/components/commons/BaseModal'; import { Button } from '@/components/commons/Button'; import { Typography } from '@/components/commons/Typography'; diff --git a/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignmentList.tsx b/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignmentList.tsx index 30b8ec842..4e5c6c96e 100644 --- a/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignmentList.tsx +++ b/gui/app/src/components/onboarding/pages/trackers-assign/TrackerAssignmentList.tsx @@ -2,7 +2,8 @@ import classNames from 'classnames'; import { Clickable } from '@/components/commons/Clickable'; import { useLocalization } from '@fluent/react'; import { CSSProperties, ReactNode, useMemo } from 'react'; -import { BodyPart, DeviceDataT, TrackerDataT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { DeviceDataT, TrackerDataT } from 'solarxr-protocol'; import { BodyPartIcon } from '@/components/commons/BodyPartIcon'; import { Button } from '@/components/commons/Button'; import { LoaderIcon, SlimeState } from '@/components/commons/icon/LoaderIcon'; diff --git a/gui/app/src/components/onboarding/parts/PartCard.tsx b/gui/app/src/components/onboarding/parts/PartCard.tsx index 92e33d39c..3a6f46ef2 100644 --- a/gui/app/src/components/onboarding/parts/PartCard.tsx +++ b/gui/app/src/components/onboarding/parts/PartCard.tsx @@ -1,6 +1,6 @@ import classNames from 'classnames'; import { Fragment, ReactNode } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { BodyPartIcon } from '@/components/commons/BodyPartIcon'; import { Tooltip } from '@/components/commons/Tooltip'; import { Typography } from '@/components/commons/Typography'; diff --git a/gui/app/src/components/settings/pages/BoneRoutingSettings.tsx b/gui/app/src/components/settings/pages/BoneRoutingSettings.tsx index b3caea134..d92225df4 100644 --- a/gui/app/src/components/settings/pages/BoneRoutingSettings.tsx +++ b/gui/app/src/components/settings/pages/BoneRoutingSettings.tsx @@ -2,8 +2,8 @@ import { useLocalization } from '@fluent/react'; import { Clickable } from '@/components/commons/Clickable'; import classNames from 'classnames'; import { ReactNode, useEffect, useMemo, useState } from 'react'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, BoneRouteT, BoneRoutingSettingsRequestT, BoneRoutingSettingsResponseT, diff --git a/gui/app/src/components/settings/pages/components/TapDetectionSettings.tsx b/gui/app/src/components/settings/pages/components/TapDetectionSettings.tsx index 1b06c72d5..bcec43aa0 100644 --- a/gui/app/src/components/settings/pages/components/TapDetectionSettings.tsx +++ b/gui/app/src/components/settings/pages/components/TapDetectionSettings.tsx @@ -1,8 +1,8 @@ import { useLocalization } from '@fluent/react'; import { useEffect } from 'react'; import { DefaultValues, useForm } from 'react-hook-form'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, ChangeTapDetectionSettingsRequestT, RpcMessage, TapDetectionSettingsRequestT, @@ -81,7 +81,7 @@ export function TapDetectionSettings() { }); const bodyParts: { value: string; label: string }[] = Object.values(BodyPart) - .filter((v): v is BodyPart => typeof v === 'number') + .filter((v): v is BodyPart => typeof v === 'string') .filter((v) => TAP_DETECTION_BODY_PARTS.includes(v as BodyPart)) .map((value) => ({ value: String(value), @@ -104,12 +104,16 @@ export function TapDetectionSettings() { settingsReq.yawResetEnabled = values.yawResetEnabled; settingsReq.yawResetTaps = values.yawResetTaps; settingsReq.yawResetBoneId = - boneIdOfBodyPart(boneIdRegistry, Number(values.yawResetTracker)) ?? null; - settingsReq.mountingResetBoneId = - boneIdOfBodyPart(boneIdRegistry, Number(values.mountingResetTracker)) ?? + boneIdOfBodyPart(boneIdRegistry, values.yawResetTracker as BodyPart) ?? null; + settingsReq.mountingResetBoneId = + boneIdOfBodyPart( + boneIdRegistry, + values.mountingResetTracker as BodyPart + ) ?? null; settingsReq.fullResetBoneId = - boneIdOfBodyPart(boneIdRegistry, Number(values.fullResetTracker)) ?? null; + boneIdOfBodyPart(boneIdRegistry, values.fullResetTracker as BodyPart) ?? + null; settingsReq.mountingResetEnabled = values.mountingResetEnabled; settingsReq.mountingResetDelay = values.mountingResetDelay; settingsReq.mountingResetTaps = values.mountingResetTaps; diff --git a/gui/app/src/components/tracker/SingleTrackerBodyAssignmentMenu.tsx b/gui/app/src/components/tracker/SingleTrackerBodyAssignmentMenu.tsx index 440584587..82bac4a0a 100644 --- a/gui/app/src/components/tracker/SingleTrackerBodyAssignmentMenu.tsx +++ b/gui/app/src/components/tracker/SingleTrackerBodyAssignmentMenu.tsx @@ -1,6 +1,6 @@ import classNames from 'classnames'; import { useEffect, useRef } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { BaseModal } from '@/components/commons/BaseModal'; import { Button } from '@/components/commons/Button'; import { Typography } from '@/components/commons/Typography'; diff --git a/gui/app/src/components/tracker/TrackerPartCard.tsx b/gui/app/src/components/tracker/TrackerPartCard.tsx index 0e6ec2ee8..8ab2d0ebe 100644 --- a/gui/app/src/components/tracker/TrackerPartCard.tsx +++ b/gui/app/src/components/tracker/TrackerPartCard.tsx @@ -1,7 +1,7 @@ import classNames from 'classnames'; import { Clickable } from '@/components/commons/Clickable'; import { MouseEventHandler } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { getTrackerName } from '@/hooks/tracker'; import { Typography } from '@/components/commons/Typography'; import { useLocalization } from '@fluent/react'; diff --git a/gui/app/src/components/tracker/TrackerSettings.tsx b/gui/app/src/components/tracker/TrackerSettings.tsx index 38a0f1ee0..e113d1196 100644 --- a/gui/app/src/components/tracker/TrackerSettings.tsx +++ b/gui/app/src/components/tracker/TrackerSettings.tsx @@ -4,10 +4,10 @@ import { IPv4 } from 'ip-num'; import { useEffect, useMemo, useState } from 'react'; import { useForm } from 'react-hook-form'; import { useParams } from 'react-router-dom'; +import { BodyPart } from '@/utils/body-part'; import { AssignTrackerRequestT, BoardType, - BodyPart, ForgetDeviceRequestT, ImuType, MagnetometerStatus, diff --git a/gui/app/src/components/tracker/TrackersTable.tsx b/gui/app/src/components/tracker/TrackersTable.tsx index d269f9aad..43bd945be 100644 --- a/gui/app/src/components/tracker/TrackersTable.tsx +++ b/gui/app/src/components/tracker/TrackersTable.tsx @@ -40,8 +40,8 @@ import { StayAlignedInfo } from '@/components/stay-aligned/StayAlignedInfo'; import { Tooltip } from '@/components/commons/Tooltip'; import { WarningIcon } from '@/components/commons/icon/WarningIcon'; import { FirmwareIcon } from '@/components/commons/FirmwareIcon'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, DeviceDataT, TrackerDataT, TrackerStatus as TrackerStatusEnum, diff --git a/gui/app/src/components/tracking-checklist/TrackingChecklist.tsx b/gui/app/src/components/tracking-checklist/TrackingChecklist.tsx index 5fd410839..2e042fa00 100644 --- a/gui/app/src/components/tracking-checklist/TrackingChecklist.tsx +++ b/gui/app/src/components/tracking-checklist/TrackingChecklist.tsx @@ -6,6 +6,7 @@ import { } from '@/hooks/tracking-checklist'; import classNames from 'classnames'; import { Clickable } from '@/components/commons/Clickable'; +import { BodyPart } from '@/utils/body-part'; import { EnableSteamVRDriverRequestT, ResetType, @@ -13,7 +14,6 @@ import { TrackingChecklistPublicNetworksT, TrackingChecklistSteamVRDisconnectedT, TrackingChecklistStepId, - BodyPart, RoutingOutput, BoneRoutingSettingsRequestT, BoneRoutingSettingsResponseT, diff --git a/gui/app/src/components/widgets/SkeletonVisualizerWidget.tsx b/gui/app/src/components/widgets/SkeletonVisualizerWidget.tsx index 995e5192a..f32ea01f1 100644 --- a/gui/app/src/components/widgets/SkeletonVisualizerWidget.tsx +++ b/gui/app/src/components/widgets/SkeletonVisualizerWidget.tsx @@ -27,7 +27,8 @@ import { Vector3, WebGLRenderer, } from 'three'; -import { BodyPart, BoneT, MountingMethod } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { BoneT, MountingMethod } from 'solarxr-protocol'; import { QuaternionFromQuatT } from '@/maths/quaternion'; import { Vector3FromVec3fT } from '@/maths/vector3'; import classNames from 'classnames'; diff --git a/gui/app/src/hooks/body-parts.ts b/gui/app/src/hooks/body-parts.ts index 1c56caa24..672c802af 100644 --- a/gui/app/src/hooks/body-parts.ts +++ b/gui/app/src/hooks/body-parts.ts @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; export const SPINE_BODY_PARTS = [ BodyPart.HEAD, diff --git a/gui/app/src/hooks/choker-warning.ts b/gui/app/src/hooks/choker-warning.ts index 438e407eb..943392ab7 100644 --- a/gui/app/src/hooks/choker-warning.ts +++ b/gui/app/src/hooks/choker-warning.ts @@ -1,5 +1,5 @@ import { useState } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; /** * I dedicate this hook to @uriel ;) diff --git a/gui/app/src/hooks/reset.ts b/gui/app/src/hooks/reset.ts index 2014c2c55..43e0ee541 100644 --- a/gui/app/src/hooks/reset.ts +++ b/gui/app/src/hooks/reset.ts @@ -1,6 +1,6 @@ import { useEffect, useMemo, useRef, useState } from 'react'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, ResetRequestT, ResetResponseT, ResetStatus, diff --git a/gui/app/src/hooks/tracker-assignment.ts b/gui/app/src/hooks/tracker-assignment.ts index 7f52cb7a0..53651f415 100644 --- a/gui/app/src/hooks/tracker-assignment.ts +++ b/gui/app/src/hooks/tracker-assignment.ts @@ -1,7 +1,7 @@ import { createContext, useContext, useEffect, useState } from 'react'; +import { BodyPart } from '@/utils/body-part'; import { AssignTrackerRequestT, - BodyPart, QuatT, ResetTrackerAssignmentsT, RpcMessage, diff --git a/gui/app/src/hooks/tracker-drag.ts b/gui/app/src/hooks/tracker-drag.ts index 90f9249b4..24f46babd 100644 --- a/gui/app/src/hooks/tracker-drag.ts +++ b/gui/app/src/hooks/tracker-drag.ts @@ -1,6 +1,6 @@ import { useAtomValue } from 'jotai'; import { selectAtom } from 'jotai/utils'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { createPointerDrag } from './pointer-drag'; export interface TrackerDragPayload { @@ -10,10 +10,10 @@ export interface TrackerDragPayload { export const trackerDrag = createPointerDrag({ attribute: 'data-drop-body-part', - serialize: (part) => BodyPart[part], + serialize: (part) => part, parse: (raw) => { - const part = (BodyPart as unknown as Record)[raw]; - return typeof part === 'number' ? part : null; + const part = (BodyPart as unknown as Record)[raw]; + return typeof part === 'string' ? (part as BodyPart) : null; }, }); diff --git a/gui/app/src/hooks/tracker-mounting.ts b/gui/app/src/hooks/tracker-mounting.ts index d08bb1558..ef02e561e 100644 --- a/gui/app/src/hooks/tracker-mounting.ts +++ b/gui/app/src/hooks/tracker-mounting.ts @@ -1,5 +1,5 @@ import { useState } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { Quaternion } from 'three'; import * as Sentry from '@sentry/react'; import { diff --git a/gui/app/src/hooks/tracker-picker.ts b/gui/app/src/hooks/tracker-picker.ts index 5c427f1ca..56577eaa7 100644 --- a/gui/app/src/hooks/tracker-picker.ts +++ b/gui/app/src/hooks/tracker-picker.ts @@ -1,6 +1,6 @@ import { useLocalization } from '@fluent/react'; import { createContext, useContext, useMemo, useState } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { useAtomValue } from 'jotai'; import { useConfig } from './config'; import { diff --git a/gui/app/src/hooks/tracker.ts b/gui/app/src/hooks/tracker.ts index d946d3f36..dc3d8de07 100644 --- a/gui/app/src/hooks/tracker.ts +++ b/gui/app/src/hooks/tracker.ts @@ -1,5 +1,6 @@ import { CSSProperties, useEffect, useMemo, useRef, useState } from 'react'; -import { BodyPart, TrackerDataT, TrackerInfoT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { TrackerDataT, TrackerInfoT } from 'solarxr-protocol'; import { QuaternionFromQuatT, QuaternionToEulerDegrees } from '@/maths/quaternion'; import { ReactLocalization, useLocalization } from '@fluent/react'; import { useDataFeedConfig } from './datafeed-config'; diff --git a/gui/app/src/hooks/websocket-api.ts b/gui/app/src/hooks/websocket-api.ts index da3cf29ff..fa5745274 100644 --- a/gui/app/src/hooks/websocket-api.ts +++ b/gui/app/src/hooks/websocket-api.ts @@ -1,10 +1,10 @@ import { createContext, useContext, useEffect, useRef, useState } from 'react'; import { - BodyPart, + BoneRegistryRequestT, BoneRegistryT, - ClientHello, - ConfigurationAcknowledgedT, + ClientHelloT, + ConfigurationDoneT, ConnectionMessage, ConnectionMessageHeaderT, DataFeedMessage, @@ -14,7 +14,7 @@ import { MessageBundleT, RpcMessage, RpcMessageHeaderT, - ServerHello, + ServerHelloT, } from 'solarxr-protocol'; import { Builder, ByteBuffer } from 'flatbuffers'; @@ -22,9 +22,10 @@ import { useStore } from 'jotai'; import { useInterval, useTimeout } from './timeout'; import { log } from '@/utils/logging'; import { boneRegistryAtom } from '@/store/app-store'; +import { bodyPartOfKey } from '@/utils/body-part'; // Must match SOLARXR_PROTOCOL_VERSION in server/core/.../solarxr/protocol.kt -const SOLARXR_PROTOCOL_VERSION = 2; +const SOLARXR_PROTOCOL_VERSION = 3; type ConnectionPhase = 'awaiting-hello' | 'configuring' | 'ready'; @@ -66,25 +67,6 @@ export function useProvideWebsocketApi(): WebSocketApi { } }, 3000); - const onConnected = () => { - if (!webSocketRef.current) return; - setTimedOut(false); - - const fbb = new Builder(64); - ClientHello.finishClientHelloBuffer( - fbb, - ClientHello.createClientHello(fbb, SOLARXR_PROTOCOL_VERSION) - ); - webSocketRef.current.send(fbb.asUint8Array()); - }; - - const onConnectionClose = () => { - phaseRef.current = 'awaiting-hello'; - store.set(boneRegistryAtom, new Map()); - setConnected(false); - rpcPacketCounterRef.current = 0; - }; - const sendConnectionMessage = ( message: ConnectionMessageHeaderT['message'], messageType: ConnectionMessage @@ -97,28 +79,47 @@ export function useProvideWebsocketApi(): WebSocketApi { webSocketRef.current.send(fbb.asUint8Array()); }; + const onConnected = () => { + if (!webSocketRef.current) return; + setTimedOut(false); + + sendConnectionMessage( + new ClientHelloT(SOLARXR_PROTOCOL_VERSION), + ConnectionMessage.ClientHello + ); + }; + + const onConnectionClose = () => { + phaseRef.current = 'awaiting-hello'; + store.set(boneRegistryAtom, new Map()); + setConnected(false); + rpcPacketCounterRef.current = 0; + }; + const onMessage = async (event: { data: Blob }) => { if (!event.data.arrayBuffer) return; const buffer = await event.data.arrayBuffer(); const fbb = new ByteBuffer(new Uint8Array(buffer)); - if (phaseRef.current === 'awaiting-hello') { - if (!ServerHello.bufferHasIdentifier(fbb)) return; - const hello = ServerHello.getRootAsServerHello(fbb); - if (hello.status() !== HelloStatus.ACCEPTED) { - log(`SolarXR handshake rejected (status ${hello.status()}), reconnecting`); - reconnect(); - return; - } - phaseRef.current = 'configuring'; - return; - } - - if (!MessageBundle.bufferHasIdentifier(fbb)) return; const message = MessageBundle.getRootAsMessageBundle(fbb).unpack(); message.connectionMsgs.forEach((connectionHeader) => { switch (connectionHeader.messageType) { + case ConnectionMessage.ServerHello: { + const hello = connectionHeader.message as ServerHelloT; + if (hello.status !== HelloStatus.ACCEPTED) { + log(`SolarXR handshake rejected (status ${hello.status}), reconnecting`); + reconnect(); + return; + } + phaseRef.current = 'configuring'; + sendConnectionMessage( + new BoneRegistryRequestT(), + ConnectionMessage.BoneRegistryRequest + ); + sendConnectionMessage(new ConfigurationDoneT(), ConnectionMessage.ConfigurationDone); + break; + } case ConnectionMessage.BoneRegistry: { const registry = connectionHeader.message as BoneRegistryT; store.set( @@ -126,17 +127,13 @@ export function useProvideWebsocketApi(): WebSocketApi { new Map( registry.bones.map((bone) => [ bone.id, - bone.standardBodyPart ?? BodyPart.NONE, + bodyPartOfKey(bone.key?.toString() ?? ''), ]) ) ); break; } - case ConnectionMessage.FinishConfiguration: - sendConnectionMessage( - new ConfigurationAcknowledgedT(), - ConnectionMessage.ConfigurationAcknowledged - ); + case ConnectionMessage.ConfigurationDone: phaseRef.current = 'ready'; setFirstConnection(false); setConnected(true); diff --git a/gui/app/src/store/app-store.ts b/gui/app/src/store/app-store.ts index b4876dd76..04486d47b 100644 --- a/gui/app/src/store/app-store.ts +++ b/gui/app/src/store/app-store.ts @@ -1,6 +1,6 @@ import { atom } from 'jotai'; +import { BodyPart } from '@/utils/body-part'; import { - BodyPart, BoneT, DataFeedUpdateT, DeviceDataT, diff --git a/gui/app/src/utils/body-part.ts b/gui/app/src/utils/body-part.ts new file mode 100644 index 000000000..89238c696 --- /dev/null +++ b/gui/app/src/utils/body-part.ts @@ -0,0 +1,79 @@ +// SolarXR has no BodyPart type on the wire: bones are opaque registry ids and string +// keys. This mirrors the server's own definition (server/core/.../bones/BodyPart.kt) +// by name, not by number, so a bone's id can be renumbered without breaking the GUI. +export enum BodyPart { + NONE = 'NONE', + HEAD = 'HEAD', + NECK = 'NECK', + UPPER_CHEST = 'UPPER_CHEST', + LOWER_CHEST = 'LOWER_CHEST', + UPPER_WAIST = 'UPPER_WAIST', + LOWER_WAIST = 'LOWER_WAIST', + HIP = 'HIP', + LEFT_UPPER_LEG = 'LEFT_UPPER_LEG', + RIGHT_UPPER_LEG = 'RIGHT_UPPER_LEG', + LEFT_LOWER_LEG = 'LEFT_LOWER_LEG', + RIGHT_LOWER_LEG = 'RIGHT_LOWER_LEG', + LEFT_FOOT = 'LEFT_FOOT', + RIGHT_FOOT = 'RIGHT_FOOT', + LEFT_UPPER_ARM = 'LEFT_UPPER_ARM', + RIGHT_UPPER_ARM = 'RIGHT_UPPER_ARM', + LEFT_LOWER_ARM = 'LEFT_LOWER_ARM', + RIGHT_LOWER_ARM = 'RIGHT_LOWER_ARM', + LEFT_HAND = 'LEFT_HAND', + RIGHT_HAND = 'RIGHT_HAND', + LEFT_SHOULDER = 'LEFT_SHOULDER', + RIGHT_SHOULDER = 'RIGHT_SHOULDER', + LEFT_THUMB_METACARPAL = 'LEFT_THUMB_METACARPAL', + LEFT_THUMB_PROXIMAL = 'LEFT_THUMB_PROXIMAL', + LEFT_THUMB_DISTAL = 'LEFT_THUMB_DISTAL', + LEFT_INDEX_PROXIMAL = 'LEFT_INDEX_PROXIMAL', + LEFT_INDEX_INTERMEDIATE = 'LEFT_INDEX_INTERMEDIATE', + LEFT_INDEX_DISTAL = 'LEFT_INDEX_DISTAL', + LEFT_MIDDLE_PROXIMAL = 'LEFT_MIDDLE_PROXIMAL', + LEFT_MIDDLE_INTERMEDIATE = 'LEFT_MIDDLE_INTERMEDIATE', + LEFT_MIDDLE_DISTAL = 'LEFT_MIDDLE_DISTAL', + LEFT_RING_PROXIMAL = 'LEFT_RING_PROXIMAL', + LEFT_RING_INTERMEDIATE = 'LEFT_RING_INTERMEDIATE', + LEFT_RING_DISTAL = 'LEFT_RING_DISTAL', + LEFT_LITTLE_PROXIMAL = 'LEFT_LITTLE_PROXIMAL', + LEFT_LITTLE_INTERMEDIATE = 'LEFT_LITTLE_INTERMEDIATE', + LEFT_LITTLE_DISTAL = 'LEFT_LITTLE_DISTAL', + RIGHT_THUMB_METACARPAL = 'RIGHT_THUMB_METACARPAL', + RIGHT_THUMB_PROXIMAL = 'RIGHT_THUMB_PROXIMAL', + RIGHT_THUMB_DISTAL = 'RIGHT_THUMB_DISTAL', + RIGHT_INDEX_PROXIMAL = 'RIGHT_INDEX_PROXIMAL', + RIGHT_INDEX_INTERMEDIATE = 'RIGHT_INDEX_INTERMEDIATE', + RIGHT_INDEX_DISTAL = 'RIGHT_INDEX_DISTAL', + RIGHT_MIDDLE_PROXIMAL = 'RIGHT_MIDDLE_PROXIMAL', + RIGHT_MIDDLE_INTERMEDIATE = 'RIGHT_MIDDLE_INTERMEDIATE', + RIGHT_MIDDLE_DISTAL = 'RIGHT_MIDDLE_DISTAL', + RIGHT_RING_PROXIMAL = 'RIGHT_RING_PROXIMAL', + RIGHT_RING_INTERMEDIATE = 'RIGHT_RING_INTERMEDIATE', + RIGHT_RING_DISTAL = 'RIGHT_RING_DISTAL', + RIGHT_LITTLE_PROXIMAL = 'RIGHT_LITTLE_PROXIMAL', + RIGHT_LITTLE_INTERMEDIATE = 'RIGHT_LITTLE_INTERMEDIATE', + RIGHT_LITTLE_DISTAL = 'RIGHT_LITTLE_DISTAL', + LEFT_BIG_TOE = 'LEFT_BIG_TOE', + LEFT_INDEX_TOE = 'LEFT_INDEX_TOE', + LEFT_MIDDLE_TOE = 'LEFT_MIDDLE_TOE', + LEFT_RING_TOE = 'LEFT_RING_TOE', + LEFT_LITTLE_TOE = 'LEFT_LITTLE_TOE', + RIGHT_BIG_TOE = 'RIGHT_BIG_TOE', + RIGHT_INDEX_TOE = 'RIGHT_INDEX_TOE', + RIGHT_MIDDLE_TOE = 'RIGHT_MIDDLE_TOE', + RIGHT_RING_TOE = 'RIGHT_RING_TOE', + RIGHT_LITTLE_TOE = 'RIGHT_LITTLE_TOE', +} + +// Standard bones are keyed `slimevr:` (BoneRegistry.standard on the +// server); this is the only way a wire BoneDefinition resolves back to a BodyPart. +const KEY_TO_BODY_PART: ReadonlyMap = new Map( + Object.values(BodyPart) + .filter((value) => value !== BodyPart.NONE) + .map((value) => [`slimevr:${value.toLowerCase()}`, value]) +); + +export function bodyPartOfKey(key: string): BodyPart { + return KEY_TO_BODY_PART.get(key) ?? BodyPart.NONE; +} diff --git a/gui/app/src/utils/extremities.ts b/gui/app/src/utils/extremities.ts index c1076e82a..5c8cfffdf 100644 --- a/gui/app/src/utils/extremities.ts +++ b/gui/app/src/utils/extremities.ts @@ -1,5 +1,5 @@ import { ReactNode } from 'react'; -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; export type ExtremitySide = 'left' | 'right'; diff --git a/gui/app/src/utils/formatting.ts b/gui/app/src/utils/formatting.ts index b4bfff511..5decec6e7 100644 --- a/gui/app/src/utils/formatting.ts +++ b/gui/app/src/utils/formatting.ts @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; export const bodypartToString = (id: BodyPart) => BodyPart[id].replace(/_/g, ' '); diff --git a/gui/app/src/utils/skeletonHelper.ts b/gui/app/src/utils/skeletonHelper.ts index 325f7a99d..d1f04549a 100644 --- a/gui/app/src/utils/skeletonHelper.ts +++ b/gui/app/src/utils/skeletonHelper.ts @@ -10,7 +10,8 @@ import { import { LineSegments2 } from 'three/examples/jsm/lines/LineSegments2'; import { LineMaterial } from 'three/examples/jsm/lines/LineMaterial.js'; import { LineSegmentsGeometry } from 'three/examples/jsm/lines/LineSegmentsGeometry.js'; -import { BodyPart, BoneT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { BoneT } from 'solarxr-protocol'; import { QuaternionFromQuatT } from '@/maths/quaternion'; import { Vector3FromVec3fT } from '@/maths/vector3'; import { FINGER_BODY_PARTS, TOE_BODY_PARTS } from '@/hooks/body-parts'; diff --git a/gui/app/src/utils/skeletonMeshHelper.ts b/gui/app/src/utils/skeletonMeshHelper.ts index 44f80e1c2..38cf83eac 100644 --- a/gui/app/src/utils/skeletonMeshHelper.ts +++ b/gui/app/src/utils/skeletonMeshHelper.ts @@ -15,7 +15,8 @@ import { SkeletonRenderPart, TrackerPreviewData, } from './skeletonHelper'; -import { BodyPart, BoneT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { BoneT } from 'solarxr-protocol'; import { QuaternionFromQuatT } from '@/maths/quaternion'; import { Vector3FromVec3fT } from '@/maths/vector3'; import { diff --git a/gui/app/src/utils/skeletonParts.ts b/gui/app/src/utils/skeletonParts.ts index c72470588..1588d4053 100644 --- a/gui/app/src/utils/skeletonParts.ts +++ b/gui/app/src/utils/skeletonParts.ts @@ -1,4 +1,4 @@ -import { BodyPart } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; import { Color, DoubleSide, diff --git a/gui/app/src/utils/skeletonProportions.ts b/gui/app/src/utils/skeletonProportions.ts index a9e29c1e6..71ec7616f 100644 --- a/gui/app/src/utils/skeletonProportions.ts +++ b/gui/app/src/utils/skeletonProportions.ts @@ -1,4 +1,5 @@ -import { BodyPart, BoneT } from 'solarxr-protocol'; +import { BodyPart } from '@/utils/body-part'; +import { BoneT } from 'solarxr-protocol'; import { EYE_HEIGHT_TO_HEIGHT_RATIO } from '@/hooks/height'; /** diff --git a/server/android/src/main/java/dev/slimevr/android/ipc/ws-server.kt b/server/android/src/main/java/dev/slimevr/android/ipc/ws-server.kt index 75991d368..37a3f4f6b 100644 --- a/server/android/src/main/java/dev/slimevr/android/ipc/ws-server.kt +++ b/server/android/src/main/java/dev/slimevr/android/ipc/ws-server.kt @@ -3,13 +3,12 @@ package dev.slimevr.android.ipc import com.google.flatbuffers.FlatBufferBuilder import dev.slimevr.AppContextProvider import dev.slimevr.VRServerActions -import dev.slimevr.fbscodegen.runtime.JvmFlatBufferReader -import dev.slimevr.fbscodegen.runtime.JvmFlatBufferWriter import dev.slimevr.logging.AppLogger -import dev.slimevr.solarxr.SOLARXR_PROTOCOL_VERSION +import dev.slimevr.solarxr.OutboundFrame import dev.slimevr.solarxr.SolarXRBridge import dev.slimevr.solarxr.checkedSolarXRFrame import dev.slimevr.solarxr.onSolarXRMessage +import dev.slimevr.solarxr.writeSolarXRBundle import io.ktor.server.application.install import io.ktor.server.cio.CIO import io.ktor.server.engine.embeddedServer @@ -25,10 +24,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.launch -import solarxr_protocol.ClientHello -import solarxr_protocol.HelloStatus import solarxr_protocol.MessageBundle -import solarxr_protocol.ServerHello import java.nio.ByteBuffer import kotlin.time.Duration.Companion.seconds @@ -50,10 +46,14 @@ suspend fun createAndroidSolarXRWebsocketServer(appContext: AppContextProvider) AppLogger.ipc.info("SolarXR[${bridge.id}] connected (websocket)") appContext.server.context.dispatch(VRServerActions.SolarXRConnected(bridge)) - bridge.outbound.on { bundle -> + bridge.outbound.on { frame -> val fbb = FlatBufferBuilder(256) - bundle.finish(JvmFlatBufferWriter(fbb)) + writeSolarXRBundle(fbb, frame.bundle) send(Frame.Binary(fin = true, data = fbb.dataBuffer().moveToByteArray())) + if (frame.closeReason != null) { + AppLogger.ipc.warn("SolarXR[${bridge.id}] closing (websocket): ${frame.closeReason}") + this@coroutineScope.cancel() + } }.launchIn(this) val initTimeout = launch { @@ -65,7 +65,6 @@ suspend fun createAndroidSolarXRWebsocketServer(appContext: AppContextProvider) } try { - var awaitingHello = true flow { for (frame in incoming) { when (frame) { @@ -76,22 +75,9 @@ suspend fun createAndroidSolarXRWebsocketServer(appContext: AppContextProvider) } }.collect { bytes -> val buffer = ByteBuffer.wrap(bytes) - if (awaitingHello) { - val reader = checkedSolarXRFrame(buffer, ClientHello.FILE_IDENTIFIER) - val hello = ClientHello.fromByteBuffer(reader) - val accepted = hello.protocolVersion == SOLARXR_PROTOCOL_VERSION - val fbb = FlatBufferBuilder(64) - ServerHello(if (accepted) HelloStatus.ACCEPTED else HelloStatus.REJECTED_UNSUPPORTED_VERSION, SOLARXR_PROTOCOL_VERSION) - .finish(JvmFlatBufferWriter(fbb)) - send(Frame.Binary(fin = true, data = fbb.dataBuffer().moveToByteArray())) - require(accepted) { "Unsupported SolarXR protocol version ${hello.protocolVersion}" } - awaitingHello = false - bridge.beginConfiguration() - } else { - val reader = checkedSolarXRFrame(buffer, MessageBundle.FILE_IDENTIFIER) - onSolarXRMessage(MessageBundle.fromByteBuffer(reader), bridge) - if (bridge.isReady) initTimeout.cancel() - } + val reader = checkedSolarXRFrame(buffer) + onSolarXRMessage(MessageBundle.fromByteBuffer(reader), bridge) + if (bridge.isReady) initTimeout.cancel() } } finally { AppLogger.ipc.info("SolarXR[${bridge.id}] disconnected (websocket)") diff --git a/server/core/src/main/java/dev/slimevr/autobone/functions.kt b/server/core/src/main/java/dev/slimevr/autobone/functions.kt index 1f4a4522d..6c9a95a23 100644 --- a/server/core/src/main/java/dev/slimevr/autobone/functions.kt +++ b/server/core/src/main/java/dev/slimevr/autobone/functions.kt @@ -3,7 +3,7 @@ package dev.slimevr.autobone import dev.slimevr.bones.boneId import dev.slimevr.skeleton.ComputedSkeleton import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart fun slideContributionWeight(ankleA: Vector3, ankleB: Vector3, localBoneTailA: Vector3, localBoneTailB: Vector3): Float { val ankleAToB = ankleB - ankleA diff --git a/server/core/src/main/java/dev/slimevr/bones/BodyPart.kt b/server/core/src/main/java/dev/slimevr/bones/BodyPart.kt new file mode 100644 index 000000000..721c572f7 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/bones/BodyPart.kt @@ -0,0 +1,74 @@ +package dev.slimevr.bones + +/** + * The standard body parts a bone in a [BoneRegistry] may represent. Not a wire type: SolarXR + * only ever carries a bone's numeric id and string key, so nothing on the wire needs this. + */ +enum class BodyPart(val value: UByte) { + NONE(0u), + HEAD(1u), + NECK(2u), + UPPER_CHEST(3u), + LOWER_CHEST(4u), + UPPER_WAIST(5u), + LOWER_WAIST(6u), + HIP(7u), + LEFT_UPPER_LEG(8u), + RIGHT_UPPER_LEG(9u), + LEFT_LOWER_LEG(10u), + RIGHT_LOWER_LEG(11u), + LEFT_FOOT(12u), + RIGHT_FOOT(13u), + LEFT_UPPER_ARM(14u), + RIGHT_UPPER_ARM(15u), + LEFT_LOWER_ARM(16u), + RIGHT_LOWER_ARM(17u), + LEFT_HAND(18u), + RIGHT_HAND(19u), + LEFT_SHOULDER(20u), + RIGHT_SHOULDER(21u), + LEFT_THUMB_METACARPAL(22u), + LEFT_THUMB_PROXIMAL(23u), + LEFT_THUMB_DISTAL(24u), + LEFT_INDEX_PROXIMAL(25u), + LEFT_INDEX_INTERMEDIATE(26u), + LEFT_INDEX_DISTAL(27u), + LEFT_MIDDLE_PROXIMAL(28u), + LEFT_MIDDLE_INTERMEDIATE(29u), + LEFT_MIDDLE_DISTAL(30u), + LEFT_RING_PROXIMAL(31u), + LEFT_RING_INTERMEDIATE(32u), + LEFT_RING_DISTAL(33u), + LEFT_LITTLE_PROXIMAL(34u), + LEFT_LITTLE_INTERMEDIATE(35u), + LEFT_LITTLE_DISTAL(36u), + RIGHT_THUMB_METACARPAL(37u), + RIGHT_THUMB_PROXIMAL(38u), + RIGHT_THUMB_DISTAL(39u), + RIGHT_INDEX_PROXIMAL(40u), + RIGHT_INDEX_INTERMEDIATE(41u), + RIGHT_INDEX_DISTAL(42u), + RIGHT_MIDDLE_PROXIMAL(43u), + RIGHT_MIDDLE_INTERMEDIATE(44u), + RIGHT_MIDDLE_DISTAL(45u), + RIGHT_RING_PROXIMAL(46u), + RIGHT_RING_INTERMEDIATE(47u), + RIGHT_RING_DISTAL(48u), + RIGHT_LITTLE_PROXIMAL(49u), + RIGHT_LITTLE_INTERMEDIATE(50u), + RIGHT_LITTLE_DISTAL(51u), + LEFT_BIG_TOE(52u), + LEFT_INDEX_TOE(53u), + LEFT_MIDDLE_TOE(54u), + LEFT_RING_TOE(55u), + LEFT_LITTLE_TOE(56u), + RIGHT_BIG_TOE(57u), + RIGHT_INDEX_TOE(58u), + RIGHT_MIDDLE_TOE(59u), + RIGHT_RING_TOE(60u), + RIGHT_LITTLE_TOE(61u); + + companion object { + fun fromValue(value: UByte): BodyPart? = entries.firstOrNull { it.value == value } + } +} diff --git a/server/core/src/main/java/dev/slimevr/bones/bones.kt b/server/core/src/main/java/dev/slimevr/bones/bones.kt index ece23459d..5991ee327 100644 --- a/server/core/src/main/java/dev/slimevr/bones/bones.kt +++ b/server/core/src/main/java/dev/slimevr/bones/bones.kt @@ -1,6 +1,5 @@ package dev.slimevr.bones -import solarxr_protocol.datatypes.BodyPart import java.util.EnumMap /** A registry-local bone handle. `0` is reserved and never resolves to a definition. */ diff --git a/server/core/src/main/java/dev/slimevr/bones/hierarchy.kt b/server/core/src/main/java/dev/slimevr/bones/hierarchy.kt index 51c2d11a5..e12ee6a51 100644 --- a/server/core/src/main/java/dev/slimevr/bones/hierarchy.kt +++ b/server/core/src/main/java/dev/slimevr/bones/hierarchy.kt @@ -1,7 +1,5 @@ package dev.slimevr.bones -import solarxr_protocol.datatypes.BodyPart - val BODY_PART_HIERARCHY_MAP: Map> = mapOf( BodyPart.HEAD to arrayOf(BodyPart.NECK), BodyPart.NECK to arrayOf(BodyPart.UPPER_CHEST, BodyPart.LEFT_SHOULDER, BodyPart.RIGHT_SHOULDER), diff --git a/server/core/src/main/java/dev/slimevr/bones/reducer.kt b/server/core/src/main/java/dev/slimevr/bones/reducer.kt index de7162ecc..6c5e0692e 100644 --- a/server/core/src/main/java/dev/slimevr/bones/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/bones/reducer.kt @@ -5,15 +5,11 @@ import solarxr_protocol.connection.BoneRegistry as WireBoneRegistry /** * Assigns each of [incoming] a fresh ID above every ID [registry] already has, preserving order. - * A `parent` must reference a bone [registry] already defines; contributing two new bones that - * are parent and child of each other in the same batch isn't supported. A `standardBodyPart` is - * rejected here too, since only the initial [BoneRegistry.standard] registry may claim those IDs, - * which is what keeps [BodyPart.boneId][dev.slimevr.bones.boneId] safe to use with no lookup. + * A `parent` must reference a bone [registry] already defines */ private fun assignIds(registry: BoneRegistry, incoming: List): List { var nextId = registry.maxId return incoming.map { definition -> - require(definition.standardBodyPart == null) { "Extension bone \"${definition.key}\" cannot claim a standard body part" } require(definition.parent == 0.toUShort() || registry[BoneId(definition.parent)] != null) { "Extension bone \"${definition.key}\" has an unknown parent" } diff --git a/server/core/src/main/java/dev/slimevr/bones/registry.kt b/server/core/src/main/java/dev/slimevr/bones/registry.kt index b10979c5c..d7c84630f 100644 --- a/server/core/src/main/java/dev/slimevr/bones/registry.kt +++ b/server/core/src/main/java/dev/slimevr/bones/registry.kt @@ -1,20 +1,18 @@ package dev.slimevr.bones import solarxr_protocol.connection.BoneDefinition -import solarxr_protocol.datatypes.BodyPart import solarxr_protocol.connection.BoneRegistry as WireBoneRegistry +private val BODY_PART_BY_VALUE: Array = arrayOfNulls(UByte.MAX_VALUE.toInt() + 1).also { array -> + for (bodyPart in BodyPart.entries) array[bodyPart.value.toInt()] = bodyPart +} + /** * The server's bone identity and hierarchy. IDs are dense (`1..maxId`), * so [BoneMap]/[BoneSet] can use a plain array instead of a hash table. * * [BodyPart] is only used at the edges: to seed [standard], and to resolve specific bones by * name. Everything else is [BoneId]-keyed, so a bone with no standard body part still works. - * - * Invariant: a definition with a [BoneDefinition.standardBodyPart] always has - * `id == standardBodyPart.value`, checked below. That is what lets [BodyPart.boneId] convert - * without a registry lookup, so extension bones (added via `BoneRegistryManager.register`) must - * never be assigned a standard body part, and standard IDs are never reassigned. */ class BoneRegistry private constructor( val value: WireBoneRegistry, @@ -22,20 +20,26 @@ class BoneRegistry private constructor( val root: BoneId, private val byId: Array, private val byKey: Map, - private val standardIds: Array, private val childIds: Array, ) { operator fun get(id: BoneId): BoneDefinition? = byId.getOrNull(id.value.toInt()) /** Constant-time lookup with no per-call allocation. */ - operator fun get(bodyPart: BodyPart): BoneId? = standardIds[bodyPart.value.toInt()] + operator fun get(bodyPart: BodyPart): BoneId? { + if (bodyPart == BodyPart.NONE) return null + val id = BoneId(bodyPart.value.toUShort()) + return id.takeIf { this[it] != null } + } fun byKey(key: String): BoneDefinition? = byKey[key] /** The registry key is stable across a registry rebuild; a [BoneId] is not. */ operator fun get(key: String): BoneId? = byKey(key)?.let { BoneId(it.id) } - /** Inverse of `get(BodyPart)`; null when the ID is unknown or not a standardized bone. */ - fun bodyPartOf(id: BoneId): BodyPart? = this[id]?.standardBodyPart + /** Inverse of `get(BodyPart)`; null when the ID is unknown or outside the standard `BodyPart` range. */ + fun bodyPartOf(id: BoneId): BodyPart? { + if (this[id] == null) return null + return BODY_PART_BY_VALUE.getOrNull(id.value.toInt())?.takeIf { it != BodyPart.NONE } + } /** Inverse of `get(String)`. */ fun keyOf(id: BoneId): String? = this[id]?.key @@ -99,7 +103,6 @@ class BoneRegistry private constructor( key = bodyPart.key, displayName = bodyPart.name.replace('_', ' ').lowercase(), parent = parentPartOf[bodyPart]?.boneId?.value ?: 0.toUShort(), - standardBodyPart = bodyPart, ) }, ), @@ -115,9 +118,6 @@ class BoneRegistry private constructor( "Bone registry IDs must be exactly 1..$n" } require(registry.bones.all { it.parent == 0.toUShort() || it.parent in definitions }) { "Bone registry has an unknown parent" } - require(registry.bones.all { definition -> definition.standardBodyPart?.let { it.value.toUShort() == definition.id } ?: true }) { - "A standard body part's ID must equal its BodyPart value" - } val visited = mutableSetOf() fun root(id: UShort): UShort { require(visited.add(id)) { "Bone registry contains a cycle" } @@ -132,7 +132,6 @@ class BoneRegistry private constructor( val byId = arrayOfNulls(n + 1) val children = Array(n + 1) { mutableListOf() } var root: BoneId? = null - val standardIds = arrayOfNulls(UByte.MAX_VALUE.toInt() + 1) for (definition in registry.bones) { val id = definition.id.toInt() byId[id] = definition @@ -142,10 +141,6 @@ class BoneRegistry private constructor( } else { children[definition.parent.toInt()] += id } - definition.standardBodyPart?.let { bodyPart -> - require(standardIds[bodyPart.value.toInt()] == null) { "Duplicate standard bone $bodyPart" } - standardIds[bodyPart.value.toInt()] = BoneId(definition.id) - } } return BoneRegistry( value = registry, @@ -153,7 +148,6 @@ class BoneRegistry private constructor( root = requireNotNull(root) { "Bone registry has no root" }, byId = byId, byKey = registry.bones.associateBy { it.key }, - standardIds = standardIds, childIds = Array(children.size) { children[it].toIntArray() }, ) } diff --git a/server/core/src/main/java/dev/slimevr/config/settings.kt b/server/core/src/main/java/dev/slimevr/config/settings.kt index 4d1f9f354..1d03b12cf 100644 --- a/server/core/src/main/java/dev/slimevr/config/settings.kt +++ b/server/core/src/main/java/dev/slimevr/config/settings.kt @@ -14,7 +14,7 @@ import kotlinx.serialization.json.decodeFromJsonElement import kotlinx.serialization.json.intOrNull import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.MountingMethod import solarxr_protocol.rpc.ArmsResetMode import solarxr_protocol.rpc.FilteringType diff --git a/server/core/src/main/java/dev/slimevr/heightcalibration/behaviours.kt b/server/core/src/main/java/dev/slimevr/heightcalibration/behaviours.kt index f44cbbc2f..9ef6fd8e9 100644 --- a/server/core/src/main/java/dev/slimevr/heightcalibration/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/heightcalibration/behaviours.kt @@ -17,7 +17,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.sample import kotlinx.coroutines.flow.takeWhile import kotlinx.coroutines.withTimeoutOrNull -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.UserHeightCalibrationStatus import kotlin.math.PI import kotlin.math.cos diff --git a/server/core/src/main/java/dev/slimevr/heightcalibration/module.kt b/server/core/src/main/java/dev/slimevr/heightcalibration/module.kt index 2fdaa4d10..3349f8eb1 100644 --- a/server/core/src/main/java/dev/slimevr/heightcalibration/module.kt +++ b/server/core/src/main/java/dev/slimevr/heightcalibration/module.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.rpc.UserHeightCalibrationStatus diff --git a/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/sensor-info.kt b/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/sensor-info.kt index 27019a648..3e6e0653e 100644 --- a/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/sensor-info.kt +++ b/server/core/src/main/java/dev/slimevr/hid/behaviours/v3/sensor-info.kt @@ -5,7 +5,7 @@ import dev.slimevr.hid.HIDReceiver import dev.slimevr.hid.HIDReceiverBehaviour import dev.slimevr.hid.HIDSensorInfo import dev.slimevr.tracker.TrackerActions -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart class HIDSensorInfoBehaviour : HIDReceiverBehaviour { override fun observe(receiver: HIDReceiver) { diff --git a/server/core/src/main/java/dev/slimevr/keybind/module.kt b/server/core/src/main/java/dev/slimevr/keybind/module.kt index 0ebf23904..0e7a87e24 100644 --- a/server/core/src/main/java/dev/slimevr/keybind/module.kt +++ b/server/core/src/main/java/dev/slimevr/keybind/module.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.KeybindId import solarxr_protocol.rpc.ResetType diff --git a/server/core/src/main/java/dev/slimevr/resets/bodypart-sets.kt b/server/core/src/main/java/dev/slimevr/resets/bodypart-sets.kt index f3af96b11..49e142a95 100644 --- a/server/core/src/main/java/dev/slimevr/resets/bodypart-sets.kt +++ b/server/core/src/main/java/dev/slimevr/resets/bodypart-sets.kt @@ -1,6 +1,6 @@ package dev.slimevr.resets -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart object ResetBodyParts { val UPPER_LEGS = setOf( diff --git a/server/core/src/main/java/dev/slimevr/resets/module.kt b/server/core/src/main/java/dev/slimevr/resets/module.kt index 7276af79b..9fb01267a 100644 --- a/server/core/src/main/java/dev/slimevr/resets/module.kt +++ b/server/core/src/main/java/dev/slimevr/resets/module.kt @@ -22,7 +22,7 @@ import kotlinx.coroutines.Job import kotlinx.coroutines.cancelAndJoin import kotlinx.coroutines.delay import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.MountingMethod import solarxr_protocol.rpc.ArmsResetMode import solarxr_protocol.rpc.ResetResponse diff --git a/server/core/src/main/java/dev/slimevr/routing/routing.kt b/server/core/src/main/java/dev/slimevr/routing/routing.kt index 93253a97e..9522ec6cc 100644 --- a/server/core/src/main/java/dev/slimevr/routing/routing.kt +++ b/server/core/src/main/java/dev/slimevr/routing/routing.kt @@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.map -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.rpc.RoutingOutput import solarxr_protocol.rpc.RoutingOutputState diff --git a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt index 427e4a670..4a7fdba0d 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/behaviours.kt @@ -24,7 +24,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.ResetType import java.util.concurrent.Executors import kotlin.math.cos diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt index 86fb4f90a..1db87cd5c 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-constraints.kt @@ -4,7 +4,7 @@ import com.jme3.math.FastMath import dev.slimevr.bones.BodyPartMap import dev.slimevr.bones.BoneId import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart // TODO Actually figure out good values for these constraints, maybe a source would be // good? diff --git a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt index e51202c14..d9c090f6c 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/bodypart-ik-chains.kt @@ -3,7 +3,7 @@ package dev.slimevr.skeleton import dev.slimevr.bones.BodyPartMap import dev.slimevr.bones.BoneId import dev.slimevr.bones.boneId -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart typealias BodyPartChain = List diff --git a/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt b/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt index 82a7766f5..d2bae7bea 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/centre-of-mass.kt @@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneId import dev.slimevr.bones.boneId import dev.slimevr.util.inFloatingSeconds import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.time.ComparableTimeMark // TODO Where are these numbers from? diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt index 2e160a0e1..5e06723a7 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/foot-plant.kt @@ -8,7 +8,7 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonFkProcessor import dev.slimevr.tracker.eulerHeading import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart // The max height to plant feet by const val ROTATION_CORRECTION_VERTICAL = 0.1f diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt index dcf09b715..08527cf90 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/localizer.kt @@ -19,7 +19,7 @@ import dev.slimevr.util.MonotonicValueTimeMark import dev.slimevr.util.inFloatingSeconds import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.ResetType import kotlin.time.Duration import kotlin.time.Duration.Companion.milliseconds diff --git a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt index 13b56bd7b..df6ec262b 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/fkprocessors/toe-snap.kt @@ -8,7 +8,7 @@ import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonFkProcessor import dev.slimevr.tracker.eulerHeading import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.math.abs import kotlin.math.asin diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt index 9709c5372..85c9e5f06 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/bone-direct-link.kt @@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneId import dev.slimevr.bones.boneId import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Handles setting the rotation of an inactive bone with its source bone. diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt index 69cce1090..05deac7e6 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/finger-impute.kt @@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneId import dev.slimevr.bones.boneId import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart private const val INTERMEDIATE_FROM_PROXIMAL = 2.12f private const val DISTAL_FROM_PROXIMAL = 3.03f diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt index 3d9d6d4e1..ae237821a 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/head-position-fallback.kt @@ -5,7 +5,7 @@ import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Moves the head input to the skeleton height. Else, it would default to (0, 0, 0). diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt index fb65e2a34..f26fcb74a 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/hip-yaw-roll-align.kt @@ -5,7 +5,7 @@ import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Rotates the first Quaternion to match its yaw and roll to the rotation of diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/prediction.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/prediction.kt index 2707e4991..6fe01a46e 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/prediction.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/prediction.kt @@ -9,7 +9,7 @@ import dev.slimevr.skeleton.SkeletonInputProcessor import dev.slimevr.util.MonotonicValueTimeMark import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.FilteringType import solarxr_protocol.rpc.ResetType import kotlin.time.Duration.Companion.milliseconds diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt index 10f1e2cf2..172c52d7b 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/spine.kt @@ -7,7 +7,7 @@ import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.enums.enumEntries // At this default value, the user's spine should behave as we meant it to. diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/toe-active-link.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/toe-active-link.kt index ca1d0901e..9e36b1d4e 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/toe-active-link.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/toe-active-link.kt @@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneId import dev.slimevr.bones.boneId import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Handles setting the rotation of an inactive toe bone with the first active toe bone in its sources, or keeps diff --git a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt index 8a5c0602a..ef8ad17f9 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/inputprocessors/upper-legs-roll-align.kt @@ -6,7 +6,7 @@ import dev.slimevr.config.Settings import dev.slimevr.skeleton.InputSkeleton import dev.slimevr.skeleton.SkeletonInputProcessor import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Rotates the first Quaternion to match its roll to the rotation of diff --git a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt index bc1171924..929b09873 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/proportions.kt @@ -6,7 +6,7 @@ import dev.slimevr.bones.BoneRegistry import dev.slimevr.bones.boneId import io.github.axisangles.ktmath.Vector3 import io.github.axisangles.ktmath.times -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.SkeletonBone import kotlin.collections.map import kotlin.collections.plus diff --git a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt index 7c6960182..a31326876 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/reducer.kt @@ -5,7 +5,7 @@ import dev.slimevr.bones.mapValues import dev.slimevr.bones.mutateCopy import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart fun reduce(state: SkeletonState, action: SkeletonActions): SkeletonState = when (action) { is SkeletonActions.SetBoneRotation -> { diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt index 43eb32d92..a094505f5 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/floor-clip.kt @@ -7,7 +7,7 @@ import dev.slimevr.skeleton.ComputedSkeleton import dev.slimevr.skeleton.IKTargets import dev.slimevr.skeleton.SkeletonTargetProcessor import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart class FloorClipTargetProcessor( val settings: Settings, diff --git a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt index 6c06e4ecf..1d49e15d8 100644 --- a/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt +++ b/server/core/src/main/java/dev/slimevr/skeleton/targetprocessors/skating-correction.kt @@ -14,7 +14,7 @@ import dev.slimevr.skeleton.computeComState import dev.slimevr.skeleton.resolveMasses import dev.slimevr.util.timeSource import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.ResetType data class LockState( diff --git a/server/core/src/main/java/dev/slimevr/solarxr/connection.kt b/server/core/src/main/java/dev/slimevr/solarxr/connection.kt new file mode 100644 index 000000000..e13a2cde0 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/solarxr/connection.kt @@ -0,0 +1,44 @@ +package dev.slimevr.solarxr + +import solarxr_protocol.connection.BoneRegistryRequest +import solarxr_protocol.connection.ClientHello +import solarxr_protocol.connection.ConfigurationDone +import solarxr_protocol.connection.ConnectionError +import solarxr_protocol.connection.ConnectionMessage +import solarxr_protocol.connection.HelloStatus +import solarxr_protocol.connection.InitializationRequiredError +import solarxr_protocol.connection.ServerHello +import solarxr_protocol.connection.UnsupportedRequestError + +suspend fun onConnectionMessage(message: ConnectionMessage?, bridge: SolarXRBridge) { + when (bridge.context.state.value.phase) { + ConnectionPhase.HELLO -> { + val hello = message as? ClientHello + if (hello == null) { + bridge.sendConnectionMessage( + ConnectionError(message = "The first message on a connection must be ClientHello", data = InitializationRequiredError()), + closeReason = "First message was not ClientHello", + ) + return + } + val accepted = hello.protocolVersion == SOLARXR_PROTOCOL_VERSION + bridge.sendConnectionMessage( + ServerHello(if (accepted) HelloStatus.ACCEPTED else HelloStatus.REJECTED_UNSUPPORTED_VERSION, SOLARXR_PROTOCOL_VERSION), + closeReason = if (accepted) null else "Unsupported SolarXR protocol version ${hello.protocolVersion}", + ) + if (accepted) bridge.context.dispatch(SolarXRBridgeActions.SetPhase(ConnectionPhase.CONFIGURING)) + } + + ConnectionPhase.CONFIGURING -> when (message) { + is BoneRegistryRequest -> bridge.sendConnectionMessage(bridge.registry.value) + is ConfigurationDone -> { + bridge.sendConnectionMessage(ConfigurationDone()) + bridge.completeConfiguration() + } + null -> bridge.sendConnectionMessage(ConnectionError(message = "Unrecognized connection message", data = UnsupportedRequestError())) + else -> Unit + } + + ConnectionPhase.READY -> Unit + } +} diff --git a/server/core/src/main/java/dev/slimevr/solarxr/driver/outgoing.kt b/server/core/src/main/java/dev/slimevr/solarxr/driver/outgoing.kt index 3810c8ce7..1055b175a 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/driver/outgoing.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/driver/outgoing.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.driver_protocol.BoneBatteryUpdate import solarxr_protocol.driver_protocol.SkeletonUpdate diff --git a/server/core/src/main/java/dev/slimevr/solarxr/inbound.kt b/server/core/src/main/java/dev/slimevr/solarxr/inbound.kt new file mode 100644 index 000000000..5ae87f2f5 --- /dev/null +++ b/server/core/src/main/java/dev/slimevr/solarxr/inbound.kt @@ -0,0 +1,42 @@ +package dev.slimevr.solarxr + +import solarxr_protocol.MessageBundle +import solarxr_protocol.connection.ConnectionError +import solarxr_protocol.connection.InitializationRequiredError + +suspend fun onSolarXRMessage(message: MessageBundle, context: SolarXRBridge) { + val wasReady = context.isReady + message.connectionMsgs?.forEach { header -> onConnectionMessage(header.message, context) } + if (!wasReady) { + if (message.dataFeedMsgs != null || message.rpcMsgs != null || message.driverMsgs != null) { + context.sendConnectionMessage(ConnectionError(message = "Configuration has not completed", data = InitializationRequiredError())) + } + return + } + message.dataFeedMsgs?.forEach { + val msg = it.message ?: return + context.dataFeedDispatcher.emit(msg) + } + + message.rpcMsgs?.forEach { + val msg = it.message ?: return + val replyTo = it.replyTo + if (replyTo != 0u) { + context.rpcRequests.tryResolve(replyTo, msg) + return@forEach + } + context.rpcReplies.record(msg, it.txId) + context.rpcDispatcher.emit(msg) + } + + message.driverMsgs?.forEach { + val msg = it.message ?: return + val replyTo = it.replyTo + if (replyTo != 0u) { + context.driverRequests.tryResolve(replyTo, msg) + return@forEach + } + context.driverReplies.record(msg, it.txId) + context.driverDispatcher.emit(msg) + } +} diff --git a/server/core/src/main/java/dev/slimevr/solarxr/module.kt b/server/core/src/main/java/dev/slimevr/solarxr/module.kt index 83a88e179..69a088d5b 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/module.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/module.kt @@ -44,12 +44,8 @@ import dev.slimevr.util.timeSource import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Job import solarxr_protocol.MessageBundle -import solarxr_protocol.connection.ConfigurationAcknowledged -import solarxr_protocol.connection.ConnectionError -import solarxr_protocol.connection.ConnectionErrorCode import solarxr_protocol.connection.ConnectionMessage import solarxr_protocol.connection.ConnectionMessageHeader -import solarxr_protocol.connection.FinishConfiguration import solarxr_protocol.data_feed.DataFeedConfig import solarxr_protocol.data_feed.DataFeedMessage import solarxr_protocol.data_feed.DataFeedMessageHeader @@ -63,67 +59,27 @@ import solarxr_protocol.rpc.RpcMessageHeader import kotlin.time.Duration import kotlin.time.Duration.Companion.seconds +enum class ConnectionPhase { HELLO, CONFIGURING, READY } + data class SolarXRBridgeState( val dataFeedConfigs: List = emptyList(), val driverName: String? = null, val boneMask: BoneMask? = null, - val isReady: Boolean = false, + val phase: ConnectionPhase = ConnectionPhase.HELLO, val observing: Boolean = false, ) sealed interface SolarXRBridgeActions { data class SetConfig(val configs: List) : SolarXRBridgeActions data class SetDriverInfo(val name: String?, val boneMask: BoneMask?) : SolarXRBridgeActions - data object SetReady : SolarXRBridgeActions + data class SetPhase(val phase: ConnectionPhase) : SolarXRBridgeActions data object SetObserving : SolarXRBridgeActions } typealias SolarXRBridgeContext = Context typealias SolarXRBridgeBehaviour = Behaviour -suspend fun onSolarXRMessage(message: MessageBundle, context: SolarXRBridge) { - // Connection messages are a barrier: configuration must be consumed before - // any application bundle is dispatched. - val wasReady = context.isReady - message.connectionMsgs?.forEach { header -> - when (header.message) { - is ConfigurationAcknowledged -> context.completeConfiguration() - else -> Unit - } - } - if (!wasReady) { - if (message.dataFeedMsgs != null || message.rpcMsgs != null || message.driverMsgs != null) { - context.sendConnectionMessage(ConnectionError(ConnectionErrorCode.INITIALIZATION_REQUIRED, "Configuration has not completed")) - } - return - } - message.dataFeedMsgs?.forEach { - val msg = it.message ?: return - context.dataFeedDispatcher.emit(msg) - } - - message.rpcMsgs?.forEach { - val msg = it.message ?: return - val replyTo = it.replyTo - if (replyTo != 0u) { - context.rpcRequests.tryResolve(replyTo, msg) - return@forEach - } - context.rpcReplies.record(msg, it.txId) - context.rpcDispatcher.emit(msg) - } - - message.driverMsgs?.forEach { - val msg = it.message ?: return - val replyTo = it.replyTo - if (replyTo != 0u) { - context.driverRequests.tryResolve(replyTo, msg) - return@forEach - } - context.driverReplies.record(msg, it.txId) - context.driverDispatcher.emit(msg) - } -} +data class OutboundFrame(val bundle: MessageBundle, val closeReason: String? = null) class SolarXRBridge( val id: Int, @@ -132,10 +88,10 @@ class SolarXRBridge( val dataFeedDispatcher: EventDispatcher = EventDispatcher("SolarXR[$id].dataFeed", context.scope, capacity = 32), val rpcDispatcher: EventDispatcher = EventDispatcher("SolarXR[$id].rpc", context.scope, capacity = 64), val driverDispatcher: EventDispatcher = EventDispatcher("SolarXR[$id].driver", context.scope, capacity = 64), - val outbound: EventDispatcher = EventDispatcher("SolarXR[$id].outbound", context.scope, capacity = 64), + val outbound: EventDispatcher = EventDispatcher("SolarXR[$id].outbound", context.scope, capacity = 64), private val managedContext: ManagedContext? = null, ) { - val isReady: Boolean get() = context.state.value.isReady + val isReady: Boolean get() = context.state.value.phase == ConnectionPhase.READY val registry: BoneRegistry get() = appContext.bones.current // Jobs are mutable handles with no meaningful equality; storing them in state @@ -162,23 +118,18 @@ class SolarXRBridge( return driverDispatcher.on

{ msg -> action(msg, driverTxIdFor(msg)) } } - suspend fun sendRpc(message: RpcMessage, replyTo: UInt? = null, txId: UInt? = null) = outbound.emit(MessageBundle(rpcMsgs = listOf(RpcMessageHeader(txId = txId ?: 0u, replyTo = replyTo ?: 0u, message = message)))) - suspend fun sendDriverMessage(message: DriverMessage, replyTo: UInt? = null, txId: UInt? = null) = outbound.emit(MessageBundle(driverMsgs = listOf(DriverMessageHeader(txId = txId ?: 0u, replyTo = replyTo ?: 0u, message = message)))) + suspend fun sendRpc(message: RpcMessage, replyTo: UInt? = null, txId: UInt? = null) = outbound.emit(OutboundFrame(MessageBundle(rpcMsgs = listOf(RpcMessageHeader(txId = txId ?: 0u, replyTo = replyTo ?: 0u, message = message))))) + suspend fun sendDriverMessage(message: DriverMessage, replyTo: UInt? = null, txId: UInt? = null) = outbound.emit(OutboundFrame(MessageBundle(driverMsgs = listOf(DriverMessageHeader(txId = txId ?: 0u, replyTo = replyTo ?: 0u, message = message))))) suspend inline fun requestRpc(message: RpcMessage, timeout: Duration = 10.seconds): R = rpcRequests.request(timeout, message) { msg, newTxId -> sendRpc(msg, txId = newTxId) } suspend inline fun requestDriverMessage(message: DriverMessage, timeout: Duration = 10.seconds): R = driverRequests.request(timeout, message) { msg, newTxId -> sendDriverMessage(msg, txId = newTxId) } - suspend fun sendDataFeed(frame: DataFeedMessageHeader) = outbound.emit(MessageBundle(dataFeedMsgs = listOf(frame))) - suspend fun sendConnectionMessage(message: ConnectionMessage) = outbound.emit(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(message)))) - - suspend fun beginConfiguration() { - sendConnectionMessage(registry.value) - sendConnectionMessage(FinishConfiguration()) - } + suspend fun sendDataFeed(frame: DataFeedMessageHeader) = outbound.emit(OutboundFrame(MessageBundle(dataFeedMsgs = listOf(frame)))) + suspend fun sendConnectionMessage(message: ConnectionMessage, closeReason: String? = null) = outbound.emit(OutboundFrame(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(message))), closeReason)) fun completeConfiguration() { - if (context.state.value.isReady) return - context.dispatch(SolarXRBridgeActions.SetReady) + if (context.state.value.phase == ConnectionPhase.READY) return + context.dispatch(SolarXRBridgeActions.SetPhase(ConnectionPhase.READY)) if (!context.state.value.observing) { context.dispatch(SolarXRBridgeActions.SetObserving) startObserving() diff --git a/server/core/src/main/java/dev/slimevr/solarxr/protocol.kt b/server/core/src/main/java/dev/slimevr/solarxr/protocol.kt index 086219438..f7f6dd0ee 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/protocol.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/protocol.kt @@ -1,17 +1,20 @@ package dev.slimevr.solarxr +import com.google.flatbuffers.FlatBufferBuilder import dev.slimevr.fbscodegen.runtime.JvmFlatBufferReader +import dev.slimevr.fbscodegen.runtime.JvmFlatBufferWriter import java.nio.ByteBuffer +import solarxr_protocol.MessageBundle const val SOLARXR_PROTOCOL_VERSION: UInt = 2u const val SOLARXR_MAX_FRAME_SIZE = 256 * 1024 -/** Checks framing before generated code reads a FlatBuffer root. */ -fun checkedSolarXRFrame(bytes: ByteBuffer, identifier: String): JvmFlatBufferReader { +fun checkedSolarXRFrame(bytes: ByteBuffer): JvmFlatBufferReader { require(bytes.remaining() in 8..SOLARXR_MAX_FRAME_SIZE) { "Invalid SolarXR frame length" } - val position = bytes.position() - require((0 until 4).all { bytes.get(position + 4 + it).toInt().toChar() == identifier[it] }) { - "Unexpected SolarXR file identifier" - } return JvmFlatBufferReader(bytes) } + +fun writeSolarXRBundle(fbb: FlatBufferBuilder, bundle: MessageBundle) { + val writer = JvmFlatBufferWriter(fbb) + writer.finish(bundle.encode(writer)) +} diff --git a/server/core/src/main/java/dev/slimevr/solarxr/reducer.kt b/server/core/src/main/java/dev/slimevr/solarxr/reducer.kt index 19ff851fc..f0573f5e6 100644 --- a/server/core/src/main/java/dev/slimevr/solarxr/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/solarxr/reducer.kt @@ -3,6 +3,6 @@ package dev.slimevr.solarxr fun reduce(state: SolarXRBridgeState, action: SolarXRBridgeActions): SolarXRBridgeState = when (action) { is SolarXRBridgeActions.SetConfig -> state.copy(dataFeedConfigs = action.configs) is SolarXRBridgeActions.SetDriverInfo -> state.copy(driverName = action.name, boneMask = action.boneMask) - is SolarXRBridgeActions.SetReady -> state.copy(isReady = true) + is SolarXRBridgeActions.SetPhase -> state.copy(phase = action.phase) is SolarXRBridgeActions.SetObserving -> state.copy(observing = true) } diff --git a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt index 6a7a40dc8..ecfdbe43b 100644 --- a/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/tapdetection/behaviours.kt @@ -17,7 +17,7 @@ import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.merge import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.rpc.ResetType import solarxr_protocol.rpc.TapDetectionSetupNotification diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours/default-mounting-orientation.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours/default-mounting-orientation.kt index 06303b700..2dc605684 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours/default-mounting-orientation.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours/default-mounting-orientation.kt @@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.drop import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.MountingMethod class TrackerDefaultMountingOrientationBehaviour(private val registry: BoneRegistry) : TrackerBehaviour { diff --git a/server/core/src/main/java/dev/slimevr/tracker/behaviours/rest-orientation.kt b/server/core/src/main/java/dev/slimevr/tracker/behaviours/rest-orientation.kt index 1a0bc5722..1f76237b3 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/behaviours/rest-orientation.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/behaviours/rest-orientation.kt @@ -15,7 +15,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.ArmsResetMode /** diff --git a/server/core/src/main/java/dev/slimevr/tracker/reducer.kt b/server/core/src/main/java/dev/slimevr/tracker/reducer.kt index ce34cf0eb..8ac8b0399 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/reducer.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/reducer.kt @@ -2,7 +2,7 @@ package dev.slimevr.tracker import dev.slimevr.bones.boneId import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.MountingMethod import kotlin.time.Duration diff --git a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/body-parts.kt b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/body-parts.kt index aa866efe8..0ac86dabb 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/body-parts.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/body-parts.kt @@ -1,7 +1,7 @@ package dev.slimevr.tracker.stayaligned import dev.slimevr.util.Side -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Body parts used by Stay Aligned with some utils. diff --git a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/poses/relaxed-pose.kt b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/poses/relaxed-pose.kt index 5944c21bc..ac1d4802d 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/poses/relaxed-pose.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/poses/relaxed-pose.kt @@ -8,7 +8,7 @@ import dev.slimevr.tracker.getFirstActiveFor import dev.slimevr.tracker.stayaligned.StayAlignedBodyParts import dev.slimevr.tracker.stayaligned.StayAlignedDefaults import dev.slimevr.tracker.stayaligned.YawUtils.trackerYaw -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart class RelaxedPose( val upperLeg: Angle, diff --git a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/tracker-yaw-correction.kt b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/tracker-yaw-correction.kt index 60789bb2e..34ff25846 100644 --- a/server/core/src/main/java/dev/slimevr/tracker/stayaligned/tracker-yaw-correction.kt +++ b/server/core/src/main/java/dev/slimevr/tracker/stayaligned/tracker-yaw-correction.kt @@ -17,7 +17,7 @@ import dev.slimevr.tracker.stayaligned.poses.RelaxedPose import dev.slimevr.util.Side import dev.slimevr.util.side import io.github.axisangles.ktmath.Quaternion -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Entry point for Stay Aligned. More specifically, computeYawCorrection is. diff --git a/server/core/src/main/java/dev/slimevr/trackingchecklist/behaviours.kt b/server/core/src/main/java/dev/slimevr/trackingchecklist/behaviours.kt index 48ba82326..d9de2115e 100644 --- a/server/core/src/main/java/dev/slimevr/trackingchecklist/behaviours.kt +++ b/server/core/src/main/java/dev/slimevr/trackingchecklist/behaviours.kt @@ -32,7 +32,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.update -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.MountingMethod import solarxr_protocol.datatypes.TrackerStatus diff --git a/server/core/src/main/java/dev/slimevr/util/side.kt b/server/core/src/main/java/dev/slimevr/util/side.kt index 68f7d95c0..d287d24b8 100644 --- a/server/core/src/main/java/dev/slimevr/util/side.kt +++ b/server/core/src/main/java/dev/slimevr/util/side.kt @@ -1,6 +1,6 @@ package dev.slimevr.util -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart /** * Used to represent the side of the skeleton a bone is on. diff --git a/server/core/src/main/java/dev/slimevr/vmc/input.kt b/server/core/src/main/java/dev/slimevr/vmc/input.kt index 9bee6259d..e78482c97 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/input.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/input.kt @@ -19,7 +19,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.rpc.VMCOSCInputState diff --git a/server/core/src/main/java/dev/slimevr/vmc/mapping.kt b/server/core/src/main/java/dev/slimevr/vmc/mapping.kt index cc0decb68..1063aa635 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/mapping.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/mapping.kt @@ -5,7 +5,7 @@ import dev.slimevr.bones.BodyPartMap import dev.slimevr.resets.ResetBodyParts import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart // TODO how to handle UPPER_WAIST? val BODY_PART_TO_UNITY_BONE: BodyPartMap> = BodyPartMap( diff --git a/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt b/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt index 4c5dd98b2..a48233479 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/output-encoder.kt @@ -10,7 +10,7 @@ import dev.slimevr.skeleton.BoneState import dev.slimevr.skeleton.ComputedSkeleton import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.time.Duration internal fun buildOutgoingBundle( diff --git a/server/core/src/main/java/dev/slimevr/vmc/output.kt b/server/core/src/main/java/dev/slimevr/vmc/output.kt index 6ea132d55..77667d85d 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/output.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/output.kt @@ -17,7 +17,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.RoutingOutput import solarxr_protocol.rpc.VMCOSCOutputState import kotlin.time.Duration.Companion.seconds diff --git a/server/core/src/main/java/dev/slimevr/vmc/vrm.kt b/server/core/src/main/java/dev/slimevr/vmc/vrm.kt index 60c67bfa2..a8d04f4d3 100644 --- a/server/core/src/main/java/dev/slimevr/vmc/vrm.kt +++ b/server/core/src/main/java/dev/slimevr/vmc/vrm.kt @@ -12,7 +12,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.serialization.SerialName import kotlinx.serialization.Serializable import kotlinx.serialization.json.Json -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.VMCOSCVrmState private val vrmJsonParser = Json { ignoreUnknownKeys = true } diff --git a/server/core/src/main/java/dev/slimevr/vrchat/module.kt b/server/core/src/main/java/dev/slimevr/vrchat/module.kt index 3be3076b4..e6afa3a27 100644 --- a/server/core/src/main/java/dev/slimevr/vrchat/module.kt +++ b/server/core/src/main/java/dev/slimevr/vrchat/module.kt @@ -8,7 +8,7 @@ import dev.slimevr.context.Context import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.VRCAvatarMeasurementType import solarxr_protocol.rpc.VRCConfigRecommendedValues import solarxr_protocol.rpc.VRCConfigValidity diff --git a/server/core/src/main/java/dev/slimevr/vrcosc/input.kt b/server/core/src/main/java/dev/slimevr/vrcosc/input.kt index 2e9d6b329..3339c2790 100644 --- a/server/core/src/main/java/dev/slimevr/vrcosc/input.kt +++ b/server/core/src/main/java/dev/slimevr/vrcosc/input.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.rpc.VRCOSCInputState diff --git a/server/core/src/main/java/dev/slimevr/vrcosc/output-encoder.kt b/server/core/src/main/java/dev/slimevr/vrcosc/output-encoder.kt index 5e27c2b79..99d842ae4 100644 --- a/server/core/src/main/java/dev/slimevr/vrcosc/output-encoder.kt +++ b/server/core/src/main/java/dev/slimevr/vrcosc/output-encoder.kt @@ -9,7 +9,7 @@ import dev.slimevr.skeleton.ComputedSkeleton import io.github.axisangles.ktmath.EulerOrder import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.math.PI private val trackerIdsByBodyPart = mapOf( diff --git a/server/core/src/main/java/dev/slimevr/vrcosc/output.kt b/server/core/src/main/java/dev/slimevr/vrcosc/output.kt index 8121183b4..73a4968cc 100644 --- a/server/core/src/main/java/dev/slimevr/vrcosc/output.kt +++ b/server/core/src/main/java/dev/slimevr/vrcosc/output.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.onEach -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.RoutingOutput import solarxr_protocol.rpc.VRCOSCOutputState import solarxr_protocol.rpc.VRCOSCTargetSource diff --git a/server/core/src/main/java/dev/slimevr/vrcosc/toe-plugin-output-encoder.kt b/server/core/src/main/java/dev/slimevr/vrcosc/toe-plugin-output-encoder.kt index 3a4d3a1ab..fe11c5572 100644 --- a/server/core/src/main/java/dev/slimevr/vrcosc/toe-plugin-output-encoder.kt +++ b/server/core/src/main/java/dev/slimevr/vrcosc/toe-plugin-output-encoder.kt @@ -9,7 +9,7 @@ import dev.slimevr.skeleton.ComputedSkeleton import dev.slimevr.util.Side import dev.slimevr.util.opposite import io.github.axisangles.ktmath.EulerOrder -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart private const val ABSOLUTE_SPLAY_THRESHOLD_ANGLE = 7 private const val MINIMUM_TIP_TOE_PITCH = -14 diff --git a/server/core/src/test/java/dev/slimevr/TestServer.kt b/server/core/src/test/java/dev/slimevr/TestServer.kt index b587d5a7d..2727f01d8 100644 --- a/server/core/src/test/java/dev/slimevr/TestServer.kt +++ b/server/core/src/test/java/dev/slimevr/TestServer.kt @@ -55,7 +55,7 @@ import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.BufferOverflow import kotlinx.coroutines.flow.MutableSharedFlow -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.datatypes.hardware_info.ImuType diff --git a/server/core/src/test/java/dev/slimevr/bones/BoneRegistryManagerTest.kt b/server/core/src/test/java/dev/slimevr/bones/BoneRegistryManagerTest.kt index 769d416be..e3b5b63e3 100644 --- a/server/core/src/test/java/dev/slimevr/bones/BoneRegistryManagerTest.kt +++ b/server/core/src/test/java/dev/slimevr/bones/BoneRegistryManagerTest.kt @@ -2,7 +2,6 @@ package dev.slimevr.bones import kotlinx.coroutines.test.runTest import solarxr_protocol.connection.BoneDefinition -import solarxr_protocol.datatypes.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith diff --git a/server/core/src/test/java/dev/slimevr/bones/BoneRegistryTest.kt b/server/core/src/test/java/dev/slimevr/bones/BoneRegistryTest.kt index 312c1d168..fe3edd357 100644 --- a/server/core/src/test/java/dev/slimevr/bones/BoneRegistryTest.kt +++ b/server/core/src/test/java/dev/slimevr/bones/BoneRegistryTest.kt @@ -1,7 +1,6 @@ package dev.slimevr.bones import solarxr_protocol.connection.BoneDefinition -import solarxr_protocol.datatypes.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith @@ -33,9 +32,9 @@ class BoneRegistryTest { val registry = BoneRegistry.from( WireBoneRegistry( bones = listOf( - BoneDefinition(1u, "a", null, 0u, null), - BoneDefinition(2u, "b", null, 1u, null), - BoneDefinition(3u, "c", null, 2u, null), + BoneDefinition(1u, "a", null, 0u), + BoneDefinition(2u, "b", null, 1u), + BoneDefinition(3u, "c", null, 2u), ), ), ) @@ -48,8 +47,8 @@ class BoneRegistryTest { BoneRegistry.from( WireBoneRegistry( bones = listOf( - BoneDefinition(1u, "a", null, 0u, null), - BoneDefinition(1u, "b", null, 0u, null), + BoneDefinition(1u, "a", null, 0u), + BoneDefinition(1u, "b", null, 0u), ), ), ) @@ -61,7 +60,7 @@ class BoneRegistryTest { assertFailsWith { BoneRegistry.from( WireBoneRegistry( - bones = listOf(BoneDefinition(0u, "a", null, 0u, null)), + bones = listOf(BoneDefinition(0u, "a", null, 0u)), ), ) } @@ -72,7 +71,7 @@ class BoneRegistryTest { assertFailsWith { BoneRegistry.from( WireBoneRegistry( - bones = listOf(BoneDefinition(1u, "a", null, 2u, null)), + bones = listOf(BoneDefinition(1u, "a", null, 2u)), ), ) } @@ -84,8 +83,8 @@ class BoneRegistryTest { BoneRegistry.from( WireBoneRegistry( bones = listOf( - BoneDefinition(1u, "a", null, 2u, null), - BoneDefinition(2u, "b", null, 1u, null), + BoneDefinition(1u, "a", null, 2u), + BoneDefinition(2u, "b", null, 1u), ), ), ) @@ -97,21 +96,7 @@ class BoneRegistryTest { assertFailsWith { BoneRegistry.from( WireBoneRegistry( - bones = listOf(BoneDefinition(1u, "a", null, 1u, null)), - ), - ) - } - } - - @Test - fun `rejects duplicate standard body parts`() { - assertFailsWith { - BoneRegistry.from( - WireBoneRegistry( - bones = listOf( - BoneDefinition(1u, "a", null, 0u, BodyPart.HIP), - BoneDefinition(2u, "b", null, 0u, BodyPart.HIP), - ), + bones = listOf(BoneDefinition(1u, "a", null, 1u)), ), ) } @@ -123,8 +108,8 @@ class BoneRegistryTest { BoneRegistry.from( WireBoneRegistry( bones = listOf( - BoneDefinition(1u, "a", null, 0u, null), - BoneDefinition(3u, "b", null, 0u, null), + BoneDefinition(1u, "a", null, 0u), + BoneDefinition(3u, "b", null, 0u), ), ), ) diff --git a/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt b/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt index ee26ed36a..39ad05083 100644 --- a/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt +++ b/server/core/src/test/java/dev/slimevr/bvh/BvhStreamTest.kt @@ -8,7 +8,7 @@ import dev.slimevr.skeleton.buildBones import dev.slimevr.skeleton.defaultSkeletonState import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.test.runTest -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertTrue diff --git a/server/core/src/test/java/dev/slimevr/routing/AutomaticRoutesTest.kt b/server/core/src/test/java/dev/slimevr/routing/AutomaticRoutesTest.kt index 1b61302aa..b42b148bd 100644 --- a/server/core/src/test/java/dev/slimevr/routing/AutomaticRoutesTest.kt +++ b/server/core/src/test/java/dev/slimevr/routing/AutomaticRoutesTest.kt @@ -4,7 +4,7 @@ import dev.slimevr.bones.BoneRegistry import dev.slimevr.solarxr.driver.DRIVER_SUPPORTED_BONES import dev.slimevr.vmc.VMC_SUPPORTED_BONES import dev.slimevr.vrcosc.VRC_OSC_SUPPORTED_BONES -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.RoutingOutput import solarxr_protocol.rpc.RoutingOutputState import kotlin.test.Test diff --git a/server/core/src/test/java/dev/slimevr/routing/CandidateBonesTest.kt b/server/core/src/test/java/dev/slimevr/routing/CandidateBonesTest.kt index 46bb6e3d5..49bf77984 100644 --- a/server/core/src/test/java/dev/slimevr/routing/CandidateBonesTest.kt +++ b/server/core/src/test/java/dev/slimevr/routing/CandidateBonesTest.kt @@ -1,6 +1,6 @@ package dev.slimevr.routing -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.test.Test import kotlin.test.assertEquals diff --git a/server/core/src/test/java/dev/slimevr/routing/ManualRoutesTest.kt b/server/core/src/test/java/dev/slimevr/routing/ManualRoutesTest.kt index 14458dd0a..25eafb841 100644 --- a/server/core/src/test/java/dev/slimevr/routing/ManualRoutesTest.kt +++ b/server/core/src/test/java/dev/slimevr/routing/ManualRoutesTest.kt @@ -3,7 +3,7 @@ package dev.slimevr.routing import dev.slimevr.bones.BoneRegistry import dev.slimevr.bones.boneId import dev.slimevr.config.BoneRoutingConfig -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.rpc.RoutingOutput import solarxr_protocol.rpc.RoutingOutputState import kotlin.test.Test diff --git a/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt b/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt index 67e39460a..e1bfdbd2a 100644 --- a/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt +++ b/server/core/src/test/java/dev/slimevr/skeleton/IKTest.kt @@ -6,7 +6,7 @@ import dev.slimevr.bones.boneId import dev.slimevr.bones.resolveToBoneIds import io.github.axisangles.ktmath.EulerOrder import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.test.Test class IKTest { diff --git a/server/core/src/test/java/dev/slimevr/solarxr/ConnectionInitTest.kt b/server/core/src/test/java/dev/slimevr/solarxr/ConnectionInitTest.kt index 1ea763e1c..0eeb58ac1 100644 --- a/server/core/src/test/java/dev/slimevr/solarxr/ConnectionInitTest.kt +++ b/server/core/src/test/java/dev/slimevr/solarxr/ConnectionInitTest.kt @@ -6,21 +6,23 @@ import dev.slimevr.TestAppContext import dev.slimevr.bones.BoneRegistryManager import dev.slimevr.buildTestVrServer import dev.slimevr.context.Context -import dev.slimevr.fbscodegen.runtime.JvmFlatBufferWriter import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import solarxr_protocol.ClientHello -import solarxr_protocol.HelloStatus import solarxr_protocol.MessageBundle -import solarxr_protocol.ServerHello import solarxr_protocol.connection.BoneDefinition import solarxr_protocol.connection.BoneRegistry -import solarxr_protocol.connection.ConfigurationAcknowledged +import solarxr_protocol.connection.BoneRegistryRequest +import solarxr_protocol.connection.ClientHello +import solarxr_protocol.connection.ConfigurationDone import solarxr_protocol.connection.ConnectionError -import solarxr_protocol.connection.ConnectionErrorCode +import solarxr_protocol.connection.ConnectionMessage import solarxr_protocol.connection.ConnectionMessageHeader +import solarxr_protocol.connection.HelloStatus +import solarxr_protocol.connection.InitializationRequiredError +import solarxr_protocol.connection.ServerHello +import solarxr_protocol.connection.UnsupportedRequestError import solarxr_protocol.data_feed.DataFeedMessageHeader import solarxr_protocol.data_feed.StartDataFeed import java.nio.ByteBuffer @@ -28,10 +30,13 @@ import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertFailsWith import kotlin.test.assertFalse +import kotlin.test.assertIs +import kotlin.test.assertNotNull +import kotlin.test.assertNull import kotlin.test.assertTrue @OptIn(ExperimentalCoroutinesApi::class) -private fun TestScope.buildTestBridge(sent: MutableList): SolarXRBridge { +private fun TestScope.buildTestBridge(sent: MutableList): SolarXRBridge { val appContext = object : TestAppContext() { override val server = buildTestVrServer(backgroundScope) override val bones = BoneRegistryManager.create(backgroundScope) @@ -50,64 +55,114 @@ private fun TestScope.buildTestBridge(sent: MutableList): SolarXR rpcDispatcher = EventDispatcher("test.rpc", backgroundScope), driverDispatcher = EventDispatcher("test.driver", backgroundScope), ) - bridge.outbound.on { sent += it }.launchIn(backgroundScope) + bridge.outbound.on { sent += it }.launchIn(backgroundScope) runCurrent() return bridge } -private fun MessageBundle.singleConnectionError(): ConnectionError = connectionMsgs?.single()?.message as? ConnectionError ?: error("Expected a single ConnectionError") +private fun clientHello(version: UInt = SOLARXR_PROTOCOL_VERSION) = MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ClientHello(protocolVersion = version)))) +private fun OutboundFrame.connectionMessage(): ConnectionMessage? = bundle.connectionMsgs?.single()?.message @OptIn(ExperimentalCoroutinesApi::class) class ConnectionInitTest { - @Test - fun `checkedSolarXRFrame rejects a wrong identifier`() { - val fbb = FlatBufferBuilder(64) - ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(fbb)) - assertFailsWith { - checkedSolarXRFrame(fbb.dataBuffer(), ServerHello.FILE_IDENTIFIER) - } - } - @Test fun `checkedSolarXRFrame rejects a truncated frame`() { assertFailsWith { - checkedSolarXRFrame(ByteBuffer.allocate(4), ClientHello.FILE_IDENTIFIER) + checkedSolarXRFrame(ByteBuffer.allocate(4)) } } @Test fun `checkedSolarXRFrame rejects an oversized frame`() { assertFailsWith { - checkedSolarXRFrame(ByteBuffer.allocate(SOLARXR_MAX_FRAME_SIZE + 1), ClientHello.FILE_IDENTIFIER) + checkedSolarXRFrame(ByteBuffer.allocate(SOLARXR_MAX_FRAME_SIZE + 1)) } } @Test - fun `ClientHello, ServerHello and MessageBundle round-trip with their identifiers`() { - val helloFbb = FlatBufferBuilder(64) - ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(helloFbb)) - val helloReader = checkedSolarXRFrame(helloFbb.dataBuffer(), ClientHello.FILE_IDENTIFIER) - assertTrue(ClientHello.hasIdentifier(helloReader)) - assertEquals(SOLARXR_PROTOCOL_VERSION, ClientHello.fromByteBuffer(helloReader).protocolVersion) + fun `a MessageBundle round-trips through writeSolarXRBundle and checkedSolarXRFrame`() { + val fbb = FlatBufferBuilder(64) + writeSolarXRBundle(fbb, clientHello()) + val reader = checkedSolarXRFrame(fbb.dataBuffer()) + val decoded = MessageBundle.fromByteBuffer(reader).connectionMsgs?.single()?.message + assertEquals(SOLARXR_PROTOCOL_VERSION, (decoded as? ClientHello)?.protocolVersion) + } - val replyFbb = FlatBufferBuilder(64) - ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION).finish(JvmFlatBufferWriter(replyFbb)) - val replyReader = checkedSolarXRFrame(replyFbb.dataBuffer(), ServerHello.FILE_IDENTIFIER) - assertTrue(ServerHello.hasIdentifier(replyReader)) - assertEquals(HelloStatus.ACCEPTED, ServerHello.fromByteBuffer(replyReader).status) + @Test + fun `ClientHello with a matching version is accepted and moves to configuring`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) - val bundleFbb = FlatBufferBuilder(64) - MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationAcknowledged()))) - .finish(JvmFlatBufferWriter(bundleFbb)) - val bundleReader = checkedSolarXRFrame(bundleFbb.dataBuffer(), MessageBundle.FILE_IDENTIFIER) - assertTrue(MessageBundle.hasIdentifier(bundleReader)) - assertTrue(MessageBundle.fromByteBuffer(bundleReader).connectionMsgs?.single()?.message is ConfigurationAcknowledged) + onSolarXRMessage(clientHello(), bridge) + runCurrent() + + val hello = sent.single() + assertNull(hello.closeReason) + assertEquals(ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION), hello.connectionMessage()) + assertEquals(ConnectionPhase.CONFIGURING, bridge.context.state.value.phase) + } + + @Test + fun `ClientHello with a mismatched version is rejected and closes`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) + + onSolarXRMessage(clientHello(version = SOLARXR_PROTOCOL_VERSION + 1u), bridge) + runCurrent() + + val hello = sent.single() + assertNotNull(hello.closeReason) + assertEquals(ServerHello(HelloStatus.REJECTED_UNSUPPORTED_VERSION, SOLARXR_PROTOCOL_VERSION), hello.connectionMessage()) + assertEquals(ConnectionPhase.HELLO, bridge.context.state.value.phase) + } + + @Test + fun `anything but ClientHello during HELLO is rejected and closes`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) + + onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(BoneRegistryRequest()))), bridge) + runCurrent() + + val frame = sent.single() + assertNotNull(frame.closeReason) + assertIs((frame.connectionMessage() as ConnectionError).data) + } + + @Test + fun `BoneRegistryRequest during configuration replies with the registry`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) + + onSolarXRMessage(clientHello(), bridge) + runCurrent() + sent.clear() + + onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(BoneRegistryRequest()))), bridge) + runCurrent() + + assertEquals(bridge.registry.value, sent.single().connectionMessage()) + } + + @Test + fun `an undecodable connection message during configuration gets UnsupportedRequestError`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) + + onSolarXRMessage(clientHello(), bridge) + runCurrent() + sent.clear() + + onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(message = null))), bridge) + runCurrent() + + assertIs((sent.single().connectionMessage() as ConnectionError).data) } @Test fun `application bundles are rejected before configuration completes`() = runTest { - val sent = mutableListOf() + val sent = mutableListOf() val bridge = buildTestBridge(sent) var dispatched = false bridge.dataFeedDispatcher.on { dispatched = true }.launchIn(backgroundScope) @@ -120,18 +175,20 @@ class ConnectionInitTest { runCurrent() assertFalse(dispatched) - assertEquals(ConnectionErrorCode.INITIALIZATION_REQUIRED, sent.single().singleConnectionError().code) + assertIs((sent.single().connectionMessage() as ConnectionError).data) } @Test fun `application bundles dispatch once configuration completes`() = runTest { - val sent = mutableListOf() + val sent = mutableListOf() val bridge = buildTestBridge(sent) var dispatched = false bridge.dataFeedDispatcher.on { dispatched = true }.launchIn(backgroundScope) runCurrent() - onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationAcknowledged()))), bridge) + onSolarXRMessage(clientHello(), bridge) + runCurrent() + onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(ConfigurationDone()))), bridge) runCurrent() assertTrue(bridge.isReady) @@ -145,13 +202,39 @@ class ConnectionInitTest { } @Test - fun `a client-sent bone registry is silently ignored`() = runTest { - val sent = mutableListOf() + fun `a single bundle carrying ClientHello, BoneRegistryRequest and ConfigurationDone completes the handshake`() = runTest { + val sent = mutableListOf() val bridge = buildTestBridge(sent) - val clientRegistry = BoneRegistry( - bones = listOf(BoneDefinition(1u, "x", null, 0u, null)), + onSolarXRMessage( + MessageBundle( + connectionMsgs = listOf( + ConnectionMessageHeader(ClientHello(protocolVersion = SOLARXR_PROTOCOL_VERSION)), + ConnectionMessageHeader(BoneRegistryRequest()), + ConnectionMessageHeader(ConfigurationDone()), + ), + ), + bridge, ) + runCurrent() + + assertEquals(3, sent.size) + assertEquals(ServerHello(HelloStatus.ACCEPTED, SOLARXR_PROTOCOL_VERSION), sent[0].connectionMessage()) + assertEquals(bridge.registry.value, sent[1].connectionMessage()) + assertIs(sent[2].connectionMessage()) + assertTrue(bridge.isReady) + } + + @Test + fun `a client-sent bone registry is silently ignored during configuration`() = runTest { + val sent = mutableListOf() + val bridge = buildTestBridge(sent) + + onSolarXRMessage(clientHello(), bridge) + runCurrent() + sent.clear() + + val clientRegistry = BoneRegistry(bones = listOf(BoneDefinition(1u, "x", null, 0u))) onSolarXRMessage(MessageBundle(connectionMsgs = listOf(ConnectionMessageHeader(clientRegistry))), bridge) runCurrent() diff --git a/server/core/src/test/java/dev/slimevr/solarxr/DataFeedTest.kt b/server/core/src/test/java/dev/slimevr/solarxr/DataFeedTest.kt index 9cf035df2..020853849 100644 --- a/server/core/src/test/java/dev/slimevr/solarxr/DataFeedTest.kt +++ b/server/core/src/test/java/dev/slimevr/solarxr/DataFeedTest.kt @@ -22,7 +22,7 @@ import solarxr_protocol.data_feed.DataFeedConfig import solarxr_protocol.data_feed.DataFeedUpdate import solarxr_protocol.data_feed.PollDataFeed import solarxr_protocol.data_feed.StartDataFeed -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.BoneMask import kotlin.test.Test import kotlin.test.assertEquals @@ -58,7 +58,7 @@ private fun TestScope.testConn(onSend: suspend (MessageBundle) -> Unit): SolarXR rpcDispatcher = EventDispatcher("test.rpc", backgroundScope), ) bridge.startObserving() - bridge.outbound.on { onSend(it) }.launchIn(backgroundScope) + bridge.outbound.on { onSend(it.bundle) }.launchIn(backgroundScope) // launchIn registers the handler synchronously, but the dispatcher's own drain loop still has to // start before anything emitted here can reach it. diff --git a/server/core/src/test/java/dev/slimevr/stayaligned/ComputeYawCorrectionTest.kt b/server/core/src/test/java/dev/slimevr/stayaligned/ComputeYawCorrectionTest.kt index bad9dcbb8..1025122da 100644 --- a/server/core/src/test/java/dev/slimevr/stayaligned/ComputeYawCorrectionTest.kt +++ b/server/core/src/test/java/dev/slimevr/stayaligned/ComputeYawCorrectionTest.kt @@ -11,7 +11,7 @@ import dev.slimevr.tracker.stayaligned.TrackerYawCorrection import io.github.axisangles.ktmath.EulerAngles import io.github.axisangles.ktmath.EulerOrder import kotlinx.coroutines.test.runTest -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.TrackerStatus import kotlin.test.Test import kotlin.test.assertEquals diff --git a/server/core/src/test/java/dev/slimevr/tracker/YawResetSmoothingTest.kt b/server/core/src/test/java/dev/slimevr/tracker/YawResetSmoothingTest.kt index 6b811c74c..9ced5eedf 100644 --- a/server/core/src/test/java/dev/slimevr/tracker/YawResetSmoothingTest.kt +++ b/server/core/src/test/java/dev/slimevr/tracker/YawResetSmoothingTest.kt @@ -13,7 +13,7 @@ import dev.slimevr.tracker.behaviours.TrackerYawResetSmoothingBehaviour import io.github.axisangles.ktmath.Quaternion import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.test.runTest -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.TrackerStatus import kotlin.test.Test import kotlin.test.assertEquals diff --git a/server/core/src/test/java/dev/slimevr/trackingchecklist/TrackingChecklistTest.kt b/server/core/src/test/java/dev/slimevr/trackingchecklist/TrackingChecklistTest.kt index 2af92e25c..2e72b2d48 100644 --- a/server/core/src/test/java/dev/slimevr/trackingchecklist/TrackingChecklistTest.kt +++ b/server/core/src/test/java/dev/slimevr/trackingchecklist/TrackingChecklistTest.kt @@ -25,7 +25,7 @@ import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.test.TestScope import kotlinx.coroutines.test.runCurrent import kotlinx.coroutines.test.runTest -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.datatypes.TrackerStatus import solarxr_protocol.datatypes.hardware_info.ImuType diff --git a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt index 7b921cba6..ac0dd9e74 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/InputDecoderTest.kt @@ -17,7 +17,7 @@ import dev.slimevr.skeleton.Velocity import dev.slimevr.vectorAssertEquals import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull diff --git a/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt b/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt index bb5fa4a7d..7f19550ea 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/OutputEncoderTest.kt @@ -13,7 +13,7 @@ import dev.slimevr.skeleton.BoneState import dev.slimevr.skeleton.Velocity import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertNotNull diff --git a/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt b/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt index 534d48d78..d85e80390 100644 --- a/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt +++ b/server/core/src/test/java/dev/slimevr/vmc/VMCInputBehaviourTest.kt @@ -21,7 +21,7 @@ import io.github.axisangles.ktmath.Quaternion import io.github.axisangles.ktmath.Vector3 import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.test.runTest -import solarxr_protocol.datatypes.BodyPart +import dev.slimevr.bones.BodyPart import solarxr_protocol.datatypes.DeviceOrigin import solarxr_protocol.rpc.VMCOSCVrmState import kotlin.test.Test diff --git a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt index 72716d84f..45636fe31 100644 --- a/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt +++ b/server/desktop/src/main/java/dev/slimevr/desktop/ipc/solarxr.kt @@ -9,13 +9,13 @@ import dev.slimevr.SLIMEVR_IDENTIFIER import dev.slimevr.VRServerActions import dev.slimevr.desktop.startBindingsProvider import dev.slimevr.desktop.unblockSteamVRDriver -import dev.slimevr.fbscodegen.runtime.JvmFlatBufferWriter import dev.slimevr.logging.AppLogger -import dev.slimevr.solarxr.SOLARXR_PROTOCOL_VERSION +import dev.slimevr.solarxr.OutboundFrame import dev.slimevr.solarxr.SolarXRBridge import dev.slimevr.solarxr.SolarXRBridgeBehaviour import dev.slimevr.solarxr.checkedSolarXRFrame import dev.slimevr.solarxr.onSolarXRMessage +import dev.slimevr.solarxr.writeSolarXRBundle import io.ktor.client.HttpClient import io.ktor.client.engine.cio.CIO import kotlinx.coroutines.Dispatchers @@ -31,10 +31,7 @@ import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import okio.Buffer -import solarxr_protocol.ClientHello -import solarxr_protocol.HelloStatus import solarxr_protocol.MessageBundle -import solarxr_protocol.ServerHello import solarxr_protocol.rpc.EnableSteamVRDriverRequest import solarxr_protocol.rpc.OpenKeybindSettingsRequest import java.nio.ByteBuffer @@ -108,11 +105,15 @@ suspend fun handleSolarXRBridge( val fbb = FlatBufferBuilder(256) val sendBuffer = Buffer() - bridge.outbound.on { bundle -> + bridge.outbound.on { frame -> fbb.clear() - bundle.finish(JvmFlatBufferWriter(fbb)) + writeSolarXRBundle(fbb, frame.bundle) sendBuffer.write(fbb.dataBuffer()) send(sendBuffer) + if (frame.closeReason != null) { + AppLogger.solarxr.warn("SolarXR[${bridge.id}] closing ($transport): ${frame.closeReason}") + this@coroutineScope.cancel() + } }.launchIn(this) val initTimeout = launch { @@ -126,7 +127,6 @@ suspend fun handleSolarXRBridge( val receiveArray = ByteArray(MAX_FRAME_SIZE) try { - var awaitingHello = true messages.collect { frame -> val size = frame.size.toInt() // read(array, offset, count) only copies from one internal segment (up to 8k) per call @@ -134,23 +134,9 @@ suspend fun handleSolarXRBridge( var done = 0 while (done < size) done += frame.read(receiveArray, done, size - done) val bytes = ByteBuffer.wrap(receiveArray, 0, size) - if (awaitingHello) { - val reader = checkedSolarXRFrame(bytes, ClientHello.FILE_IDENTIFIER) - val hello = ClientHello.fromByteBuffer(reader) - val accepted = hello.protocolVersion == SOLARXR_PROTOCOL_VERSION - fbb.clear() - ServerHello(if (accepted) HelloStatus.ACCEPTED else HelloStatus.REJECTED_UNSUPPORTED_VERSION, SOLARXR_PROTOCOL_VERSION) - .finish(JvmFlatBufferWriter(fbb)) - sendBuffer.write(fbb.dataBuffer()) - send(sendBuffer) - require(accepted) { "Unsupported SolarXR protocol version ${hello.protocolVersion}" } - awaitingHello = false - bridge.beginConfiguration() - } else { - val reader = checkedSolarXRFrame(bytes, MessageBundle.FILE_IDENTIFIER) - onSolarXRMessage(MessageBundle.fromByteBuffer(reader), bridge) - if (bridge.isReady) initTimeout.cancel() - } + val reader = checkedSolarXRFrame(bytes) + onSolarXRMessage(MessageBundle.fromByteBuffer(reader), bridge) + if (bridge.isReady) initTimeout.cancel() } } finally { AppLogger.solarxr.info("SolarXR[${bridge.id}] disconnected ($transport)") diff --git a/solarxr-protocol b/solarxr-protocol index 185eab5b5..e93445937 160000 --- a/solarxr-protocol +++ b/solarxr-protocol @@ -1 +1 @@ -Subproject commit 185eab5b52d846dbb5ae9cd320b276fc792e6d6d +Subproject commit e93445937c7a2932aafc1420ef622f0e5e585f12