@@ -9,6 +9,7 @@ import { Vim } from '../vim'
99import { Scene } from '../scene'
1010import { Vimx } from './vimx'
1111
12+ import { VimSource } from '../../index'
1213import { ElementMapping , ElementMapping2 } from '../elementMapping'
1314import {
1415 BFast ,
@@ -26,71 +27,68 @@ import { DefaultLog } from 'vim-format/dist/logging'
2627
2728/**
2829 * Asynchronously opens a vim object from a given source with the provided settings.
29- * @param {string | ArrayBuffer } source - The source of the vim object, either a string or an ArrayBuffer .
30+ * @param {string | BFast } source - The source of the vim object, either a string or a BFast .
3031 * @param {VimPartialSettings } settings - The settings to configure the behavior of the vim object.
3132 * @param {(p: IProgressLogs) => void } [onProgress] - Optional callback function to track progress logs.
3233 * @returns {Promise<void> } A Promise that resolves when the vim object is successfully opened.
3334 */
3435export async function open (
35- source : string | ArrayBuffer ,
36+ source : VimSource | BFast ,
3637 settings : VimPartialSettings ,
3738 onProgress ?: ( p : IProgressLogs ) => void
3839) {
40+ const bfast = source instanceof BFast ? source : new BFast ( source )
3941 const fullSettings = getFullSettings ( settings )
40- const type = await determineFileType ( source , fullSettings ) !
42+ const type = await determineFileType ( bfast , fullSettings ) !
4143
4244 if ( type === 'vim' ) {
43- return loadFromVim ( source , fullSettings , onProgress )
45+ return loadFromVim ( bfast , fullSettings , onProgress )
4446 }
4547
4648 if ( type === 'vimx' ) {
47- return loadFromVimX ( source , fullSettings , onProgress )
49+ return loadFromVimX ( bfast , fullSettings , onProgress )
4850 }
4951
5052 throw new Error ( 'Cannot determine the appropriate loading strategy.' )
5153}
5254
5355async function determineFileType (
54- vimPath : string | ArrayBuffer ,
56+ bfast : BFast ,
5557 settings : VimSettings
5658) {
5759 if ( settings ?. fileType === 'vim' ) return 'vim'
5860 if ( settings ?. fileType === 'vimx' ) return 'vimx'
59- return requestFileType ( vimPath )
61+ return requestFileType ( bfast )
6062}
6163
62- async function requestFileType ( vimPath : string | ArrayBuffer ) {
63- if ( typeof vimPath === 'string' ) {
64- if ( vimPath . endsWith ( 'vim' ) ) return 'vim'
65- if ( vimPath . endsWith ( 'vimx' ) ) return 'vimx'
64+ async function requestFileType ( bfast : BFast ) {
65+ if ( bfast . url ) {
66+ if ( bfast . url . endsWith ( 'vim' ) ) return 'vim'
67+ if ( bfast . url . endsWith ( 'vimx' ) ) return 'vimx'
6668 }
6769
68- const bfast = new BFast ( vimPath )
6970 const header = await requestHeader ( bfast )
70-
7171 if ( header . vim !== undefined ) return 'vim'
7272 if ( header . vimx !== undefined ) return 'vimx'
7373
7474 throw new Error ( 'Cannot determine file type from header.' )
7575}
7676
7777/**
78- * Loads a Vimx file from source
79- */
78+ * Loads a Vimx file from source
79+ */
8080async function loadFromVimX (
81- source : string | ArrayBuffer ,
81+ bfast : BFast ,
8282 settings : VimSettings ,
8383 onProgress : ( p : IProgressLogs ) => void
8484) {
8585 // Fetch geometry data
86- const remoteVimx = new RemoteVimx ( source )
86+ const remoteVimx = new RemoteVimx ( bfast )
8787 if ( remoteVimx . bfast . source instanceof RemoteBuffer ) {
8888 remoteVimx . bfast . source . onProgress = onProgress
8989 }
9090
91- console . log ( 'Downloading Scene Index..' )
9291 const vimx = await Vimx . fromRemote ( remoteVimx , ! settings . progressive )
93- console . log ( 'Scene Index Downloaded.' )
9492
9593 // Create scene
9694 const scene = new Scene ( settings . matrix )
@@ -109,7 +107,7 @@ async function loadFromVimX (
109107 settings ,
110108 mapping ,
111109 builder ,
112- typeof source === 'string' ? source : undefined ,
110+ bfast . url ,
113111 'vimx'
114112 )
115113
@@ -121,15 +119,15 @@ async function loadFromVimX (
121119}
122120
123121/**
124- * Loads a Vim file from source
125- */
122+ * Loads a Vim file from source
123+ */
126124async function loadFromVim (
127- source : string | ArrayBuffer ,
125+ bfast : BFast ,
128126 settings : VimSettings ,
129127 onProgress ?: ( p : IProgressLogs ) => void
130128) {
131129 const fullSettings = getFullSettings ( settings )
132- const bfast = new BFast ( source )
130+
133131 if ( bfast . source instanceof RemoteBuffer ) {
134132 bfast . source . onProgress = onProgress
135133 if ( settings . verboseHttp ) {
@@ -147,7 +145,7 @@ async function loadFromVim (
147145 const factory = new VimMeshFactory ( g3d , materials , scene )
148146
149147 // Create legacy mapping
150- const doc = await VimDocument . createFromBfast ( bfast , true )
148+ const doc = await VimDocument . createFromBfast ( bfast )
151149 const mapping = await ElementMapping . fromG3d ( g3d , doc )
152150 const header = await requestHeader ( bfast )
153151
@@ -161,7 +159,7 @@ async function loadFromVim (
161159 fullSettings ,
162160 mapping ,
163161 builder ,
164- typeof source === 'string' ? source : undefined ,
162+ bfast . url ,
165163 'vim'
166164 )
167165
0 commit comments