From 001a1d83dfcb19c75e897199072c7300651841c5 Mon Sep 17 00:00:00 2001 From: yyc <395976266@qq.com> Date: Thu, 18 Apr 2019 10:25:34 +0800 Subject: [PATCH] feat(asset-bundle): add "assemble sab" logic --- src/asset/assemble/AssembleWholeWDBSystem.re | 39 +- .../BatchOperateWholeGeometrySystem.re | 74 +- src/asset/assemble/BatchOperateWholeSystem.re | 98 +-- .../assemble/utils/AssembleWholeWDBUtils.re | 22 + src/asset_bundle/AssetBundleUsageTest.re | 152 ++-- .../dependency/FindDependencyDataSystem.re | 6 +- .../dependency/RemoveDependencyDataSystem.re | 8 +- .../dependency/utils/DependencyDataUtils.re | 4 +- .../all/utils/ABArrayBufferUtils.re | 35 +- .../all/utils/GenerateManifestABUtils.re | 8 +- src/asset_bundle/import/ImportABSystem.re | 247 ++++--- .../import/assemble/AssembleABSystem.re | 662 ++++++++++++++++-- src/service/atom/OptionService.re | 23 + .../main/data/assetBundle/AssetBundleType.re | 17 +- .../OperateRABAssetBundleMainService.re | 174 +++-- .../OperateSABAssetBundleMainService.re | 61 +- .../OperateWABAssetBundleMainService.re | 27 + .../RecordAssetBundleMainService.re | 9 +- .../state/main/data/StateDataMainType.re | 16 +- 19 files changed, 1242 insertions(+), 440 deletions(-) create mode 100644 src/asset/assemble/utils/AssembleWholeWDBUtils.re create mode 100644 src/service/state/main/assetBundle/OperateWABAssetBundleMainService.re diff --git a/src/asset/assemble/AssembleWholeWDBSystem.re b/src/asset/assemble/AssembleWholeWDBSystem.re index 3d2d69bda1..c11d1484fe 100755 --- a/src/asset/assemble/AssembleWholeWDBSystem.re +++ b/src/asset/assemble/AssembleWholeWDBSystem.re @@ -22,9 +22,10 @@ let _getArrayBuffer = (binBuffer, bufferView, bufferViews: array(bufferView)) => let _buildImageArray = (isLoadImage, {images, bufferViews}: wd, binBuffer) => { let blobObjectUrlImageArr = [||]; - let imageUint8ArrayDataMap = WonderCommonlib.MutableSparseMapService.createEmpty(); + let imageUint8ArrayDataMap = + WonderCommonlib.MutableSparseMapService.createEmpty(); - ! isLoadImage ? + !isLoadImage ? resolve((blobObjectUrlImageArr, imageUint8ArrayDataMap)) : ( images |> OptionService.isJsonSerializedValueNone ? @@ -89,28 +90,6 @@ let _buildImageArray = (isLoadImage, {images, bufferViews}: wd, binBuffer) => { typeArr |> Uint8Array.buffer; }; */ -let _buildBufferArray = (buffers: array(int), binBuffer) => { - WonderLog.Contract.requireCheck( - () => { - open WonderLog; - open Contract; - open Operators; - - let bufferLen = buffers |> Js.Array.length; - test( - Log.buildAssertMessage( - ~expect={j|has only one buffer|j}, - ~actual={j|has $bufferLen|j}, - ), - () => - bufferLen == 1 - ); - }, - IsDebugMainService.getIsDebug(StateDataMain.stateData), - ); - - [|binBuffer|]; -}; let checkWDB = dataView => { WonderLog.Contract.requireCheck( @@ -160,11 +139,16 @@ let assembleWDBData = isLoadImage, ), state, - ) => + ) => { + StateDataMainService.setState(StateDataMain.stateData, state) |> ignore; + _buildImageArray(isLoadImage, wd, binBuffer) |> then_(imageDataTuple => { + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); + let hasIMGUIFunc = - ! OptionService.isJsonSerializedValueNone(wd.scene.imgui); + !OptionService.isJsonSerializedValueNone(wd.scene.imgui); let state = isSetIMGUIFunc && hasIMGUIFunc ? state |> SetIMGUIFuncSystem.setIMGUIFunc(wd) : state; @@ -175,7 +159,7 @@ let assembleWDBData = |> BatchOperateWholeSystem.batchOperate( wd, imageDataTuple, - _buildBufferArray(buffers, binBuffer), + AssembleWholeWDBUtils. buildBufferArray(buffers, binBuffer), (isBindEvent, isActiveCamera), ); @@ -186,6 +170,7 @@ let assembleWDBData = |> resolve; }) |> WonderBsMost.Most.fromPromise; +}; let assemble = (wdb, configTuple, state) => { let (wdFileContent, streamChunk, binBuffer) = diff --git a/src/asset/assemble/BatchOperateWholeGeometrySystem.re b/src/asset/assemble/BatchOperateWholeGeometrySystem.re index 1aa69e6324..604ad7cdc0 100644 --- a/src/asset/assemble/BatchOperateWholeGeometrySystem.re +++ b/src/asset/assemble/BatchOperateWholeGeometrySystem.re @@ -104,4 +104,76 @@ let getBufferIndex32Data = (componentType, accessorIndex, dataViewArr, wd) => ) ->Some | _ => None - }; \ No newline at end of file + }; + +let setGeometryData = + ( + geometry, + wd, + dataViewArr, + {position, normal, texCoord, index}: WDType.geometry, + state, + ) => { + let state = + VerticesGeometryMainService.setVerticesByTypeArray( + geometry, + getBufferAttributeData(position, dataViewArr, wd), + state, + ); + let state = + normal |> OptionService.isJsonSerializedValueNone ? + state : + NormalsGeometryMainService.setNormalsByTypeArray( + geometry, + getBufferAttributeData( + normal |> OptionService.unsafeGetJsonSerializedValue, + dataViewArr, + wd, + ), + state, + ); + let state = + texCoord |> OptionService.isJsonSerializedValueNone ? + state : + TexCoordsGeometryMainService.setTexCoordsByTypeArray( + geometry, + getBufferAttributeData( + texCoord |> OptionService.unsafeGetJsonSerializedValue, + dataViewArr, + wd, + ), + state, + ); + + let componentType = getAccessorComponentType(wd, index); + let state = + switch (getBufferIndex16Data(componentType, index, dataViewArr, wd)) { + | Some(data) => + IndicesGeometryMainService.setIndicesByUint16Array( + geometry, + data, + state, + ) + | None => + switch (getBufferIndex32Data(componentType, index, dataViewArr, wd)) { + | Some(data) => + IndicesGeometryMainService.setIndicesByUint32Array( + geometry, + data, + state, + ) + | None => + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="_batchSetGeometryData", + ~description={j|unknown componentType: $componentType|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ) + } + }; + + state; +}; \ No newline at end of file diff --git a/src/asset/assemble/BatchOperateWholeSystem.re b/src/asset/assemble/BatchOperateWholeSystem.re index 3857ed7004..ac5482ac5a 100755 --- a/src/asset/assemble/BatchOperateWholeSystem.re +++ b/src/asset/assemble/BatchOperateWholeSystem.re @@ -16,96 +16,18 @@ let _batchSetGeometryData = geometryData |> OptionService.isJsonSerializedValueNone ? state : { - let {position, normal, texCoord, index}: WDType.geometry = + let ({position, normal, texCoord, index}: WDType.geometry) as geometryData = geometryData |> OptionService.unsafeGetJsonSerializedValue; let geometry = Array.unsafe_get(geometryArr, geometryIndex); - let state = - VerticesGeometryMainService.setVerticesByTypeArray( - geometry, - BatchOperateWholeGeometrySystem.getBufferAttributeData( - position, - dataViewArr, - wd, - ), - state, - ); - let state = - normal |> OptionService.isJsonSerializedValueNone ? - state : - NormalsGeometryMainService.setNormalsByTypeArray( - geometry, - BatchOperateWholeGeometrySystem.getBufferAttributeData( - normal |> OptionService.unsafeGetJsonSerializedValue, - dataViewArr, - wd, - ), - state, - ); - let state = - texCoord |> OptionService.isJsonSerializedValueNone ? - state : - TexCoordsGeometryMainService.setTexCoordsByTypeArray( - geometry, - BatchOperateWholeGeometrySystem.getBufferAttributeData( - texCoord |> OptionService.unsafeGetJsonSerializedValue, - dataViewArr, - wd, - ), - state, - ); - - let componentType = - BatchOperateWholeGeometrySystem.getAccessorComponentType( - wd, - index, - ); - let state = - switch ( - BatchOperateWholeGeometrySystem.getBufferIndex16Data( - componentType, - index, - dataViewArr, - wd, - ) - ) { - | Some(data) => - IndicesGeometryMainService.setIndicesByUint16Array( - geometry, - data, - state, - ) - | None => - switch ( - BatchOperateWholeGeometrySystem.getBufferIndex32Data( - componentType, - index, - dataViewArr, - wd, - ) - ) { - | Some(data) => - IndicesGeometryMainService.setIndicesByUint32Array( - geometry, - data, - state, - ) - | None => - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="_batchSetGeometryData", - ~description= - {j|unknown componentType: $componentType|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ) - } - }; - - state; + BatchOperateWholeGeometrySystem.setGeometryData( + geometry, + wd, + dataViewArr, + geometryData, + state, + ); }, state, ); @@ -133,7 +55,7 @@ let _getBatchTextureData = ), ); -let _getBatchAllTypeTextureData = +let getBatchAllTypeTextureData = (lightMaterialArr, basicSourceTextureArr, blobObjectUrlImageArr, wd) => _getBatchTextureData( lightMaterialArr, @@ -234,7 +156,7 @@ let batchOperate = ); let basicSourceTextureData = - _getBatchAllTypeTextureData( + getBatchAllTypeTextureData( lightMaterialArr, basicSourceTextureArr, blobObjectUrlImageArr, diff --git a/src/asset/assemble/utils/AssembleWholeWDBUtils.re b/src/asset/assemble/utils/AssembleWholeWDBUtils.re new file mode 100644 index 0000000000..df66160f15 --- /dev/null +++ b/src/asset/assemble/utils/AssembleWholeWDBUtils.re @@ -0,0 +1,22 @@ +let buildBufferArray = (buffers: array(int), binBuffer) => { + WonderLog.Contract.requireCheck( + () => { + open WonderLog; + open Contract; + open Operators; + + let bufferLen = buffers |> Js.Array.length; + test( + Log.buildAssertMessage( + ~expect={j|has only one buffer|j}, + ~actual={j|has $bufferLen|j}, + ), + () => + bufferLen == 1 + ); + }, + IsDebugMainService.getIsDebug(StateDataMain.stateData), + ); + + [|binBuffer|]; +}; \ No newline at end of file diff --git a/src/asset_bundle/AssetBundleUsageTest.re b/src/asset_bundle/AssetBundleUsageTest.re index f893c1667d..d36265e82f 100644 --- a/src/asset_bundle/AssetBundleUsageTest.re +++ b/src/asset_bundle/AssetBundleUsageTest.re @@ -32,30 +32,30 @@ let dynamicLoadAB = needRewriteAPI => { let wholeDependencyRelationMap = ParseABSystem.WAB.getWholeDependencyRelationMap(manifest); - FindDependencyDataSystem.findAllDependencyAbRelativePath( + FindDependencyDataSystem.findAllDependencyRAbRelativePath( "sab/a.sab", wholeDependencyRelationMap, ) |> WonderCommonlib.ArrayService.reduceOneParam( - (. stream, abRelativePath) => + (. stream, abPath) => stream |> Most.flatMap(state => LoadABSystem.load( - PathABSystem.getAssetBundlePath() ++ abRelativePath, + PathABSystem.getAssetBundlePath() ++ abPath, ) |> Most.map(ab => - AssembleABSystem.assemble(abRelativePath, ab, state) + AssembleABSystem.assemble(abPath, ab, state) ) ), Most.just(state), ); - /* |> Js.Array.map(abRelativePath => + /* |> Js.Array.map(abPath => LoadABSystem.load( - PathABSystem.getAssetBundlePath() ++ abRelativePath, + PathABSystem.getAssetBundlePath() ++ abPath, ) |> Most.map(ab => { - AssembleABSystem.assemble(abRelativePath, ab) + AssembleABSystem.assemble(abPath, ab) }) @@ -73,41 +73,58 @@ let dynamicLoadAB = needRewriteAPI => { let abRelativePath = "sab/a.sab"; + let wabRelativePath = "whole.wab"; + + let abPath = getAssetBundlePath() ++ abRelativePath; + + let wabPath = getAssetBundlePath() ++ wabRelativePath; + let _ = - LoadABSystem.load(getAssetBundlePath() ++ "whole.wab") + LoadABSystem.load(wabPath) |> Most.flatMap(wab => { let manifest = ParseABSystem.WAB.parseManifest(wab); /* let state = StateAPI.unsafeGetState(); */ + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); + let wholeDependencyRelationMap = ParseABSystem.WAB.getWholeDependencyRelationMap(manifest); - ImportABSystem.loadAndAssembleAllDependencies( - abRelativePath, - manifest, - ( - getAssetBundlePath, - isAssetBundleArrayBufferCached, - getAssetBundleArrayBufferCache, - cacheAssetBundleArrayBuffer, + state + |> OperateWABAssetBundleMainService.setWholeDependencyRelationMap( + wabRelativePath, + wholeDependencyRelationMap, + ) + |> StateDataMainService.setState(StateDataMain.stateData) + |> ignore; + + MostUtils.concatArray([| + ImportABSystem.RAB.loadAndAssembleAllDependencyRAB( + abRelativePath, + manifest, + ( + getAssetBundlePath, + isAssetBundleArrayBufferCached, + getAssetBundleArrayBufferCache, + cacheAssetBundleArrayBuffer, + ), + /* state, */ ), - /* state, */ - ) - |> Most.flatMap(() => - ImportABSystem.loadAndAssembleAB( - abRelativePath, - manifest, - wholeDependencyRelationMap, - ( - getAssetBundlePath, - isAssetBundleArrayBufferCached, - getAssetBundleArrayBufferCache, - cacheAssetBundleArrayBuffer, - ), - /* state, */ - ) - ) /* |> Most. */; + ImportABSystem.SAB.loadSABAndSetToState( + abRelativePath, + manifest, + wholeDependencyRelationMap, + ( + getAssetBundlePath, + isAssetBundleArrayBufferCached, + getAssetBundleArrayBufferCache, + cacheAssetBundleArrayBuffer, + ), + /* state, */ + ), + |]); }) |> Most.subscribe({ "next": _ => (), @@ -118,35 +135,60 @@ let dynamicLoadAB = needRewriteAPI => { }; /* let addSabAllGameObjectsToScene = (needRewriteAPI, state) => { */ -let goToNextScene = (sabRelativePath, needRewriteAPI, state) => - /* let sabRelativePath = "sab/a.sab"; */ - AssembleABSystem.isAssembled(sabRelativePath, state) ? - { - let sabAllGameObjects = - OperateSABAssetBundleMainService.unsafeFindAllGameObjects( - sabRelativePath, - state, - ); - /* |> OptionService.unsafeGet; */ - - let state = - state - |> ImportABSystem.disposeSceneAllChildren - |> GameObjectSceneMainService.addChildren(sabAllGameObjects); - - sabAllGameObjects - |> WonderCommonlib.ArrayService.reduceOneParam( - (. state, gameObject) => - GameObjectAPI.initGameObject(gameObject, state), +let goToNextScene = (wabRelativePath, sabRelativePath, needRewriteAPI) => { + let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); + + OperateSABAssetBundleMainService.isLoaded(sabRelativePath, state) ? + AssembleABSystem.SAB.assemble( + sabRelativePath, + OperateSABAssetBundleMainService.unsafeGetSAB(sabRelativePath, state), + OperateWABAssetBundleMainService.unsafeGetWholeDependencyRelationMap( + wabRelativePath, + state, + ), + ) + |> Most.tap(sceneGameObject => { + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); + + let state = state |> GameObjectAPI.initGameObject(sceneGameObject); + + let state = + state + |> ImportABSystem.disposeSceneAllChildren + |> ImportABSystem.setSABSceneGameObjectToBeScene(sceneGameObject); + + StateDataMainService.setState(StateDataMain.stateData, state) + |> ignore; + }) + |> Most.map(_ => ()) : + /* let sabAllGameObjects = + OperateSABAssetBundleMainService.unsafeFindAllGameObjects( + sabRelativePath, state, ); - } : + /* |> OptionService.unsafeGet; */ + + let state = + state + |> ImportABSystem.disposeSceneAllChildren + |> GameObjectSceneMainService.addChildren(sabAllGameObjects); + + sabAllGameObjects + |> WonderCommonlib.ArrayService.reduceOneParam( + (. state, gameObject) => + GameObjectAPI.initGameObject(gameObject, state), + state, + ); */ /* wait for finish */ /* TODO show loading bar? */ - state; + /* state; */ + Most.empty(); +}; +/* let sabRelativePath = "sab/a.sab"; */ let replaceToRabLightMaterial = (rabRelativePath, needRewriteAPI, state) => - AssembleABSystem.isAssembled(rabRelativePath, state) ? + OperateRABAssetBundleMainService.isAssembled(rabRelativePath, state) ? { let lightMaterial1 = OperateRABAssetBundleMainService.unsafeFindLightMaterialByName( diff --git a/src/asset_bundle/all/dependency/FindDependencyDataSystem.re b/src/asset_bundle/all/dependency/FindDependencyDataSystem.re index d461fa166c..37d8e93cd1 100644 --- a/src/asset_bundle/all/dependency/FindDependencyDataSystem.re +++ b/src/asset_bundle/all/dependency/FindDependencyDataSystem.re @@ -43,9 +43,9 @@ module RAB = { let dataView = DataViewCommon.create(rab); let (byteOffset, jsonByteLength, bufferByteLength) = - DependencyDataUtils.RAB.readHeader(dataView); + DependencyDataUtils.All.readHeader(dataView); - let jsonStr = DependencyDataUtils.RAB.getJsonStr(jsonByteLength, rab); + let jsonStr = DependencyDataUtils.All.getJsonStr(jsonByteLength, rab); let resourceAssetBundleContent: RABType.resourceAssetBundleContent = jsonStr |> Js.Json.parseExn |> Obj.magic; @@ -252,7 +252,7 @@ let rec _find = ) }; -let findAllDependencyAbRelativePath = (abRelativePath, dependencyRelation) => +let findAllDependencyRAbRelativePath = (abRelativePath, dependencyRelation) => _find( abRelativePath, dependencyRelation, diff --git a/src/asset_bundle/all/dependency/RemoveDependencyDataSystem.re b/src/asset_bundle/all/dependency/RemoveDependencyDataSystem.re index 21c9b05ea5..742ccbd437 100644 --- a/src/asset_bundle/all/dependency/RemoveDependencyDataSystem.re +++ b/src/asset_bundle/all/dependency/RemoveDependencyDataSystem.re @@ -10,7 +10,7 @@ module All = { dependencyRelation, abBufferDataNameMap: bufferDataNameMap, ) => - FindDependencyDataSystem.findAllDependencyAbRelativePath( + FindDependencyDataSystem.findAllDependencyRAbRelativePath( abRelativePath, dependencyRelation, ) @@ -291,10 +291,10 @@ module RAB = { let dataView = DataViewCommon.create(rab); let (byteOffset, jsonByteLength, bufferByteLength) = - DependencyDataUtils.RAB.readHeader(dataView); + DependencyDataUtils.All.readHeader(dataView); - let jsonStr = DependencyDataUtils.RAB.getJsonStr(jsonByteLength, rab); - let buffer = DependencyDataUtils.RAB.getBuffer(jsonByteLength, rab); + let jsonStr = DependencyDataUtils.All.getJsonStr(jsonByteLength, rab); + let buffer = DependencyDataUtils.All.getBuffer(jsonByteLength, rab); let resourceAssetBundleContent: RABType.resourceAssetBundleContent = jsonStr |> Js.Json.parseExn |> Obj.magic; diff --git a/src/asset_bundle/all/dependency/utils/DependencyDataUtils.re b/src/asset_bundle/all/dependency/utils/DependencyDataUtils.re index 5e3ecfc3ae..a1555e95ab 100644 --- a/src/asset_bundle/all/dependency/utils/DependencyDataUtils.re +++ b/src/asset_bundle/all/dependency/utils/DependencyDataUtils.re @@ -108,7 +108,7 @@ let checkCircleDependency = dependencyRelation => _createDependencyRelation(), ); */ -module RAB = { +module All = { open Js.Typed_array; let readHeader = dataView => { @@ -143,4 +143,6 @@ module RAB = { ); }; +module RAB = {}; + module SAB = {}; \ No newline at end of file diff --git a/src/asset_bundle/all/utils/ABArrayBufferUtils.re b/src/asset_bundle/all/utils/ABArrayBufferUtils.re index 3d8fba7e09..98c3a7321f 100644 --- a/src/asset_bundle/all/utils/ABArrayBufferUtils.re +++ b/src/asset_bundle/all/utils/ABArrayBufferUtils.re @@ -1,14 +1,27 @@ module RAB = { + let getArrayBufferFromBufferViews = + (buffer, bufferView, bufferViews: array(RABType.bufferView)) => { + let {byteOffset, byteLength}: RABType.bufferView = + Array.unsafe_get(bufferViews, bufferView); -let getArrayBufferFromBufferViews = - (buffer, bufferView, bufferViews: array(RABType.bufferView)) => { - let {byteOffset, byteLength}: RABType.bufferView = - Array.unsafe_get(bufferViews, bufferView); - - buffer - |> Js.Typed_array.ArrayBuffer.slice( - ~start=byteOffset, - ~end_=byteOffset + byteLength, - ); + buffer + |> Js.Typed_array.ArrayBuffer.slice( + ~start=byteOffset, + ~end_=byteOffset + byteLength, + ); + }; }; -} + +module SAB = { + let getArrayBufferFromBufferViews = + (buffer, bufferView, bufferViews: array(WDType.bufferView)) => { + let {byteOffset, byteLength}: WDType.bufferView = + Array.unsafe_get(bufferViews, bufferView); + + buffer + |> Js.Typed_array.ArrayBuffer.slice( + ~start=byteOffset, + ~end_=byteOffset + byteLength, + ); + }; +}; \ No newline at end of file diff --git a/src/asset_bundle/all/utils/GenerateManifestABUtils.re b/src/asset_bundle/all/utils/GenerateManifestABUtils.re index f4f898d0ca..e9b642ece5 100644 --- a/src/asset_bundle/all/utils/GenerateManifestABUtils.re +++ b/src/asset_bundle/all/utils/GenerateManifestABUtils.re @@ -1,6 +1,6 @@ open Js.Typed_array; -module RAB = { +module All = { let readHeader = dataView => { let (manifestJsonByteLength, byteOffset) = DataViewCommon.getUint32_1(. 0, dataView); @@ -11,8 +11,8 @@ module RAB = { (byteOffset, manifestJsonByteLength, contentBufferByteLength); }; - let getContentBuffer = (manifestJsonByteLength, rab) => - rab + let getContentBuffer = (manifestJsonByteLength, ab) => + ab |> ArrayBuffer.sliceFrom( GenerateABUtils.getHeaderTotalByteLength() + manifestJsonByteLength @@ -20,4 +20,6 @@ module RAB = { ); }; +module RAB = {}; + module SAB = {}; \ No newline at end of file diff --git a/src/asset_bundle/import/ImportABSystem.re b/src/asset_bundle/import/ImportABSystem.re index ba4bdc188a..0633533f24 100644 --- a/src/asset_bundle/import/ImportABSystem.re +++ b/src/asset_bundle/import/ImportABSystem.re @@ -1,106 +1,177 @@ open WonderBsMost; -let loadAndAssembleAB = - ( - abRelativePath, - wholeManifest, - wholeDependencyRelationMap, +module All = { + let loadAB = ( - getAssetBundlePathFunc, - isAssetBundleArrayBufferCachedFunc, - getAssetBundleArrayBufferCacheFunc, - cacheAssetBundleArrayBufferFunc, - ), - ) => { - /* state, */ + abRelativePath, + wholeManifest, + wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + ) => { + let hashId = + ParseABSystem.WAB.unsafeGetHashId(abRelativePath, wholeManifest); + + isAssetBundleArrayBufferCachedFunc(abRelativePath, hashId) ? + getAssetBundleArrayBufferCacheFunc(abRelativePath) |> Most.just : + LoadABSystem.load(getAssetBundlePathFunc() ++ abRelativePath) + |> Most.tap(ab => + cacheAssetBundleArrayBufferFunc(abRelativePath, ab, hashId) + ); + /* |> Most.flatMap(ab => + AssembleABSystem.assemble( + abRelativePath, + ab, + wholeDependencyRelationMap, + /* StateDataMainService.setState(StateDataMain.stateData, state) + |> ignore; */ + ) + ); */ + }; +}; - let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); +module SAB = { + let loadSABAndSetToState = + ( + sabRelativePath, + wholeManifest, + wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + ) => { + let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); - AssembleABSystem.isAssembled(abRelativePath, state) ? - Most.empty() : - { - let hashId = - ParseABSystem.WAB.unsafeGetHashId(abRelativePath, wholeManifest); + OperateSABAssetBundleMainService.isLoaded(sabRelativePath, state) ? + Most.empty() : + All.loadAB( + sabRelativePath, + wholeManifest, + wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + ) + |> Most.tap(sab => { + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); - isAssetBundleArrayBufferCachedFunc(abRelativePath, hashId) ? - getAssetBundleArrayBufferCacheFunc(abRelativePath) |> Most.just : - LoadABSystem.load(getAssetBundlePathFunc() ++ abRelativePath) - |> Most.tap(ab => - cacheAssetBundleArrayBufferFunc(abRelativePath, ab, hashId) - ) - |> Most.flatMap(ab => - AssembleABSystem.assemble( - abRelativePath, - ab, - wholeDependencyRelationMap, - /* StateDataMainService.setState(StateDataMain.stateData, state) - |> ignore; */ - ) - ); - }; + state + |> OperateSABAssetBundleMainService.markLoaded(sabRelativePath) + |> OperateSABAssetBundleMainService.setSAB(sabRelativePath, sab) + |> StateDataMainService.setState(StateDataMain.stateData) + |> ignore; + }) + |> Most.map(_ => ()); + }; }; -let loadAndAssembleAllDependencies = - ( - abRelativePath, - wholeManifest, +module RAB = { + let _loadAndAssembleRAB = ( - getAssetBundlePathFunc, - isAssetBundleArrayBufferCachedFunc, - getAssetBundleArrayBufferCacheFunc, - cacheAssetBundleArrayBufferFunc, - ), - ) => { - let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); - - AssembleABSystem.isAssembled(abRelativePath, state) ? - Most.empty() : - { - let wholeDependencyRelationMap = - ParseABSystem.WAB.getWholeDependencyRelationMap(wholeManifest); + rabRelativePath, + wholeManifest, + wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + ) => { + let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); - FindDependencyDataSystem.findAllDependencyAbRelativePath( - abRelativePath, + OperateRABAssetBundleMainService.isAssembled(rabRelativePath, state) ? + Most.empty() : + All.loadAB( + rabRelativePath, + wholeManifest, wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), ) - |> Js.Array.map(abRelativePath - /* let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); */ - => - loadAndAssembleAB( - abRelativePath, - wholeManifest, - wholeDependencyRelationMap, - ( - getAssetBundlePathFunc, - isAssetBundleArrayBufferCachedFunc, - getAssetBundleArrayBufferCacheFunc, - cacheAssetBundleArrayBufferFunc, - ), - /* state, */ - ) + |> Most.flatMap(rab => + AssembleABSystem.RAB.assemble( + rabRelativePath, + rab, + wholeDependencyRelationMap, ) - |> Most.mergeArray; - /* |> WonderCommonlib.ArrayService.reduceOneParam( - (. stream, abRelativePath) => - stream - |> Most.flatMap(state => - loadAndAssembleAB( - abRelativePath, - wholeManifest, - ( - getAssetBundlePathFunc, - isAssetBundleArrayBufferCachedFunc, - getAssetBundleArrayBufferCacheFunc, - cacheAssetBundleArrayBufferFunc, - ), - state, - ) - ), - Most.just(state), - ); */ - }; + ); + }; + + let loadAndAssembleAllDependencyRAB = + ( + abRelativePath, + wholeManifest, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + ) => { + let wholeDependencyRelationMap = + ParseABSystem.WAB.getWholeDependencyRelationMap(wholeManifest); + + FindDependencyDataSystem.findAllDependencyRAbRelativePath( + abRelativePath, + wholeDependencyRelationMap, + ) + |> Js.Array.map(rabRelativePath + /* let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); */ + => + _loadAndAssembleRAB( + rabRelativePath, + wholeManifest, + wholeDependencyRelationMap, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + /* state, */ + ) + ) + |> Most.mergeArray; + /* |> WonderCommonlib.ArrayService.reduceOneParam( + (. stream, rabRelativePath) => + stream + |> Most.flatMap(state => + loadAndAssembleAB( + rabRelativePath, + wholeManifest, + ( + getAssetBundlePathFunc, + isAssetBundleArrayBufferCachedFunc, + getAssetBundleArrayBufferCacheFunc, + cacheAssetBundleArrayBufferFunc, + ), + state, + ) + ), + Most.just(state), + ); */ + }; }; +let setSABSceneGameObjectToBeScene = (sceneGameObject, state) => + GameObjectSceneMainService.setSceneGameObject(sceneGameObject, state); + let disposeSceneAllChildren = state => { let scene = state |> GameObjectSceneMainService.getSceneGameObject; diff --git a/src/asset_bundle/import/assemble/AssembleABSystem.re b/src/asset_bundle/import/assemble/AssembleABSystem.re index 840a3234d9..1e9f6de1c4 100644 --- a/src/asset_bundle/import/assemble/AssembleABSystem.re +++ b/src/asset_bundle/import/assemble/AssembleABSystem.re @@ -2,54 +2,566 @@ open Js.Typed_array; open WonderBsMost; -/* TODO finish */ -module SAB = { - let isSAB = abRelativePath => abRelativePath |> Js.String.includes(".sab"); - - let assemble = (sabRelativePath, sab, wholeDependencyRelationMap) => - Most.empty() - |> Most.tap(() => { - let state = - StateDataMainService.unsafeGetState(StateDataMain.stateData); - - state - |> OperateSABAssetBundleMainService.markAssembled(sabRelativePath) - |> StateDataMainService.setState(StateDataMain.stateData) - |> ignore; - }); -}; - -module RAB = { - let isRAB = abRelativePath => abRelativePath |> Js.String.includes(".rab"); - - let _getContentData = rab => { - let dataView = DataViewCommon.create(rab); +module All = { + let getContentData = ab => { + let dataView = DataViewCommon.create(ab); let (byteOffset, manifestJsonByteLength, contentBufferByteLength) = - GenerateManifestABUtils.RAB.readHeader(dataView); + GenerateManifestABUtils.All.readHeader(dataView); let contentArrayBuffer = - GenerateManifestABUtils.RAB.getContentBuffer( + GenerateManifestABUtils.All.getContentBuffer( manifestJsonByteLength, - rab, + ab, ); let contentDataView = DataViewCommon.create(contentArrayBuffer); let (byteOffset, jsonByteLength, bufferByteLength) = - DependencyDataUtils.RAB.readHeader(contentDataView); + DependencyDataUtils.All.readHeader(contentDataView); let jsonStr = - DependencyDataUtils.RAB.getJsonStr(jsonByteLength, contentArrayBuffer); + DependencyDataUtils.All.getJsonStr(jsonByteLength, contentArrayBuffer); let buffer = - DependencyDataUtils.RAB.getBuffer(jsonByteLength, contentArrayBuffer); + DependencyDataUtils.All.getBuffer(jsonByteLength, contentArrayBuffer); + + (jsonStr |> Js.Json.parseExn |> Obj.magic, buffer); + }; +}; + +/* TODO finish */ +module SAB = { + open SABType; + + open WDType; + + open Js.Promise; + + let isSAB = abRelativePath => abRelativePath |> Js.String.includes(".sab"); + + let _isImageBufferDataDependencyAndRemoved = ({name, bufferView, mimeType}) => + ABBufferViewUtils.isNoneBufferViewIndex(bufferView); + + let _buildImageArray = + ({images, bufferViews}, binBuffer, allDependencyRAbRelativePath, state) => { + let blobObjectUrlImageArr = [||]; + /* let imageUint8ArrayDataMap = + WonderCommonlib.MutableSparseMapService.createEmpty(); */ + + images |> OptionService.isJsonSerializedValueNone ? + blobObjectUrlImageArr |> resolve : + images + |> OptionService.unsafeGetJsonSerializedValue + |> ArrayService.reduceOneParamValidi( + (. + streamArr, + ({name, bufferView, mimeType}: image) as imageData, + imageIndex, + ) => + streamArr + |> ArrayService.push( + _isImageBufferDataDependencyAndRemoved(imageData) ? + OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, + name, + state, + OperateRABAssetBundleMainService.findImageByName, + ) + |> Most.just : + { + let arrayBuffer = + ABArrayBufferUtils.SAB.getArrayBufferFromBufferViews( + binBuffer, + bufferView, + bufferViews, + ); + + /* imageUint8ArrayDataMap + |> WonderCommonlib.MutableSparseMapService.set( + imageIndex, + (mimeType, Uint8Array.fromBuffer(arrayBuffer)), + ) + |> ignore; */ + + AssembleUtils.buildLoadImageStream( + arrayBuffer, + mimeType, + {j|load image error. imageName: $name|j}, + ); + } + |> WonderBsMost.Most.tap(image => { + ImageUtils.setImageName(image, name); + + Array.unsafe_set( + blobObjectUrlImageArr, + imageIndex, + image, + ); + }), + ), + [||], + ) + |> WonderBsMost.Most.mergeArray + |> WonderBsMost.Most.drain + |> then_(() => blobObjectUrlImageArr |> resolve); + }; + + let _isGeometryBufferDataDependencyAndRemoved = + ({name, position, normal, texCoord, index}) => + ABBufferViewUtils.isNoneAccessorIndex(position) + && OptionService.isJsonSerializedValueNone(normal) + && OptionService.isJsonSerializedValueNone(texCoord) + && ABBufferViewUtils.isNoneAccessorIndex(index); + + let _replaceCreatedGeometryToDependencyGeometry = + ( + {geometrys, bufferViews, accessors}, + binBuffer, + allDependencyRAbRelativePath, + state, + createdGeometryArr, + ) => + geometrys + |> WonderCommonlib.ArrayService.reduceOneParami( + (. + geometryArr, + /* {name, position, normal, texCoord, index} as geometryData, */ + geometryData, + geometryIndex, + ) => { + let geometry = + geometryData |> OptionService.isJsonSerializedValueNone ? + Array.unsafe_get(createdGeometryArr, geometryIndex) : + { + let {name, position, normal, texCoord, index} as geometryData = + geometryData |> OptionService.unsafeGetJsonSerializedValue; + + _isGeometryBufferDataDependencyAndRemoved(geometryData) ? + OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, + name, + state, + OperateRABAssetBundleMainService.findGeometryByName, + ) : + Array.unsafe_get(createdGeometryArr, geometryIndex); + }; + + geometryArr |> ArrayService.push(geometry); + }, + WonderCommonlib.ArrayService.createEmpty(), + ); + + let _checkDependencyGeometryhasVertices = + (geometryArr, geometryIndex, state) => + WonderLog.Contract.requireCheck( + () => + WonderLog.( + Contract.( + Operators.( + test( + Log.buildAssertMessage( + ~expect={j|dependency geometry has vertices|j}, + ~actual={j|not|j}, + ), + () => { + let geometry = Array.unsafe_get(geometryArr, geometryIndex); + + VerticesGeometryMainService.hasVertices(geometry, state) + |> assertTrue; + }, + ) + ) + ) + ), + IsDebugMainService.getIsDebug(StateDataMain.stateData), + ); + + let _batchSetGeometryData = + ({geometrys} as sceneAssetBundleContent, geometryArr, bufferArr, state) => { + let dataViewArr = + bufferArr |> Js.Array.map(buffer => DataViewCommon.create(buffer)); + + geometrys + |> WonderCommonlib.ArrayService.reduceOneParami( + (. state, geometryData, geometryIndex) => + geometryData |> OptionService.isJsonSerializedValueNone ? + state : + { + let ({position, normal, texCoord, index}: WDType.geometry) as geometryData = + geometryData |> OptionService.unsafeGetJsonSerializedValue; + + _isGeometryBufferDataDependencyAndRemoved(geometryData) ? + { + _checkDependencyGeometryhasVertices( + geometryArr, + geometryIndex, + state, + ); + + state; + } : + { + let geometry = Array.unsafe_get(geometryArr, geometryIndex); + + BatchOperateWholeGeometrySystem.setGeometryData( + geometry, + sceneAssetBundleContent, + dataViewArr, + geometryData, + state, + ); + }; + }, + state, + ); + }; - let resourceAssetBundleContent: RABType.resourceAssetBundleContent = - jsonStr |> Js.Json.parseExn |> Obj.magic; + let _batchOperate = + ( + {geometrys, indices, gameObjects, basicSourceTextures} as sceneAssetBundleContent, + blobObjectUrlImageArr, + bufferArr, + /* (isBindEvent, isActiveCamera), */ + ( + state, + gameObjectArr, + ( + transformArr, + geometryArr, + meshRendererArr, + basicCameraViewArr, + perspectiveCameraProjectionArr, + arcballCameraControllerArr, + basicMaterialArr, + lightMaterialArr, + directionLightArr, + pointLightArr, + scriptArr, + ), + basicSourceTextureArr, + ), + ) => { + let state = + state + |> BatchOperateSystem.batchSetNames( + (gameObjectArr, basicSourceTextureArr), + (gameObjects, basicSourceTextures), + (geometrys, geometryArr), + ) + |> BatchOperateSystem.batchSetIsActive(gameObjectArr, gameObjects) + |> BatchOperateSystem.batchSetIsRoot(gameObjectArr, gameObjects); + + let ( + ( + parentTransforms, + childrenTransforms, + transformGameObjects, + gameObjectTransforms, + geometryGameObjects, + gameObjectGeometrys, + basicCameraViewGameObjects, + gameObjectBasicCameraViews, + perspectiveCameraProjectionGameObjects, + gameObjectPerspectiveCameraProjection, + arcballCameraControllerGameObjects, + gameObjectArcballCameraController, + basicMaterialGameObjects, + gameObjectBasicMaterials, + lightMaterialGameObjects, + gameObjectLightMaterials, + meshRendererGameObjects, + gameObjectMeshRenderers, + directionLightGameObjects, + gameObjectDirectionLights, + pointLightGameObjects, + gameObjectPointLights, + scriptGameObjects, + gameObjectScripts, + ), + state, + ) = + BatchOperateSystem.getBatchComponentGameObjectData( + ( + gameObjectArr, + transformArr, + geometryArr, + meshRendererArr, + basicCameraViewArr, + perspectiveCameraProjectionArr, + arcballCameraControllerArr, + basicMaterialArr, + lightMaterialArr, + directionLightArr, + pointLightArr, + scriptArr, + ), + indices, + sceneAssetBundleContent, + state, + ); - (resourceAssetBundleContent, buffer); + let state = + BatchSetTextureAllDataSystem.batchSetFormat( + basicSourceTextureArr, + basicSourceTextures, + state, + ); + + let basicSourceTextureData = + BatchOperateWholeSystem.getBatchAllTypeTextureData( + lightMaterialArr, + basicSourceTextureArr, + blobObjectUrlImageArr, + sceneAssetBundleContent, + ); + + /* let imageUint8ArrayDataMap = + BatchSetWholeTextureAllDataSystem.convertKeyFromImageIndexToBasicSourceTexture( + indices.imageTextureIndices, + basicSourceTextureArr, + imageUint8ArrayDataMap, + ); */ + + ( + state + |> BatchOperateSystem.batchSetTransformData( + sceneAssetBundleContent, + gameObjectTransforms, + ) + |> BatchOperateSystem.batchSetTransformParent( + parentTransforms, + childrenTransforms, + ) + |> _batchSetGeometryData( + sceneAssetBundleContent, + geometryArr, + bufferArr, + ) + |> BatchOperateSystem.batchSetBasicCameraViewData( + sceneAssetBundleContent, + basicCameraViewArr, + /* isActiveCamera, */ + true, + ) + |> BatchOperateSystem.batchSetPerspectiveCameraProjectionData( + sceneAssetBundleContent, + perspectiveCameraProjectionArr, + ) + |> BatchOperateSystem.batchSetArcballCameraControllerData( + sceneAssetBundleContent, + arcballCameraControllerArr, + /* isBindEvent, */ + true, + ) + |> BatchOperateSystem.batchSetMeshRendererData( + sceneAssetBundleContent, + meshRendererArr, + ) + |> BatchOperateSystem.batchSetBasicMaterialData( + sceneAssetBundleContent, + basicMaterialArr, + ) + |> BatchOperateSystem.batchSetLightMaterialData( + sceneAssetBundleContent, + lightMaterialArr, + ) + |> BatchOperateSystem.batchSetScriptData( + sceneAssetBundleContent, + scriptArr, + ) + |> BatchOperateLightSystem.batchSetDirectionLightData( + sceneAssetBundleContent, + directionLightArr, + ) + |> BatchOperateLightSystem.batchSetPointLightData( + sceneAssetBundleContent, + pointLightArr, + ) + |> BatchOperateLightSystem.setAmbientLightData(sceneAssetBundleContent) + |> BatchAddGameObjectComponentMainService.batchAddTransformComponentForCreate( + transformGameObjects, + gameObjectTransforms, + ) + |> BatchAddGameObjectComponentMainService.batchAddGeometryComponentForCreate( + geometryGameObjects, + gameObjectGeometrys, + ) + |> BatchAddGameObjectComponentMainService.batchAddBasicCameraViewComponentForCreate( + basicCameraViewGameObjects, + gameObjectBasicCameraViews, + ) + |> BatchAddGameObjectComponentMainService.batchAddPerspectiveCameraProjectionComponentForCreate( + perspectiveCameraProjectionGameObjects, + gameObjectPerspectiveCameraProjection, + ) + |> BatchAddGameObjectComponentMainService.batchAddArcballCameraControllerComponentForCreate( + arcballCameraControllerGameObjects, + gameObjectArcballCameraController, + ) + |> BatchAddGameObjectComponentMainService.batchAddBasicMaterialComponentForCreate( + basicMaterialGameObjects, + gameObjectBasicMaterials, + ) + |> BatchAddGameObjectComponentMainService.batchAddLightMaterialComponentForCreate( + lightMaterialGameObjects, + gameObjectLightMaterials, + ) + |> BatchAddGameObjectComponentMainService.batchAddMeshRendererComponentForCreate( + meshRendererGameObjects, + gameObjectMeshRenderers, + ) + |> BatchAddGameObjectComponentMainService.batchAddDirectionLightComponentForCreate( + directionLightGameObjects, + gameObjectDirectionLights, + ) + |> BatchAddGameObjectComponentMainService.batchAddPointLightComponentForCreate( + pointLightGameObjects, + gameObjectPointLights, + ) + |> BatchAddGameObjectComponentMainService.batchAddScriptComponentForCreate( + scriptGameObjects, + gameObjectScripts, + ) + |> BatchSetWholeTextureAllDataSystem.batchSet(basicSourceTextureData), + /* imageUint8ArrayDataMap, */ + gameObjectArr, + ); }; + let assemble = (sabRelativePath, sab, wholeDependencyRelationMap) => { + let allDependencyRAbRelativePath = + FindDependencyDataSystem.findAllDependencyRAbRelativePath( + sabRelativePath, + wholeDependencyRelationMap, + ); + + let (sceneAssetBundleContent: SABType.sceneAssetBundleContent, binBuffer) = + All.getContentData(sab); + + /* let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); */ + + /* Most.empty() + |> Most.tap(() => { + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); + + state + |> OperateSABAssetBundleMainService.markAssembled(sabRelativePath) + |> StateDataMainService.setState(StateDataMain.stateData) + |> ignore; + }); */ + + let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); + + _buildImageArray( + sceneAssetBundleContent, + binBuffer, + allDependencyRAbRelativePath, + state, + ) + |> then_(blobObjectUrlImageArr => { + let state = + StateDataMainService.unsafeGetState(StateDataMain.stateData); + + let imguiData = sceneAssetBundleContent.scene.imgui; + let hasIMGUIFunc = + !OptionService.isJsonSerializedValueNone(imguiData); + let state = + /* isSetIMGUIFunc && hasIMGUIFunc ? */ + hasIMGUIFunc ? + state |> SetIMGUIFuncSystem.setIMGUIFunc(sceneAssetBundleContent) : + state; + + /* let state = + hasIMGUIFunc ? + OperateSABAssetBundleMainService.setIMGUIData( + sabRelativePath, + imguiData, + state, + ) : + state; */ + + let ( + state, + gameObjectArr, + ( + transformArr, + geometryArr, + meshRendererArr, + basicCameraViewArr, + perspectiveCameraProjectionArr, + arcballCameraControllerArr, + basicMaterialArr, + lightMaterialArr, + directionLightArr, + pointLightArr, + scriptArr, + ), + basicSourceTextureArr, + ) = + state + |> BatchCreateSystem.batchCreate( + /* isRenderLight, */ + true, + sceneAssetBundleContent, + ); + + let geometryArr = + _replaceCreatedGeometryToDependencyGeometry( + sceneAssetBundleContent, + binBuffer, + allDependencyRAbRelativePath, + state, + geometryArr, + ); + + let (state, gameObjectArr) = + /* state */ + _batchOperate( + sceneAssetBundleContent, + /* imageDataTuple, */ + blobObjectUrlImageArr, + AssembleWholeWDBUtils.buildBufferArray(sceneAssetBundleContent.buffers, binBuffer), + /* _buildBufferArray(buffers, binBuffer), */ + /* (isBindEvent, isActiveCamera), */ + ( + state, + gameObjectArr, + ( + transformArr, + geometryArr, + meshRendererArr, + basicCameraViewArr, + perspectiveCameraProjectionArr, + arcballCameraControllerArr, + basicMaterialArr, + lightMaterialArr, + directionLightArr, + pointLightArr, + scriptArr, + ), + basicSourceTextureArr, + ), + ); + + let (state, rootGameObject) = + BuildRootGameObjectSystem.build( + sceneAssetBundleContent, + (state, gameObjectArr), + ); + + StateDataMainService.setState(StateDataMain.stateData, state) + |> ignore; + + rootGameObject |> resolve; + }) + |> WonderBsMost.Most.fromPromise; + }; +}; + +module RAB = { + let isRAB = abRelativePath => abRelativePath |> Js.String.includes(".rab"); + let _buildLoadImageStream = (arrayBuffer, mimeType, errorMsg) => { let blob = Blob.newBlobFromArrayBuffer(arrayBuffer, mimeType); @@ -65,7 +577,7 @@ module RAB = { ( {images, bufferViews}: RABType.resourceAssetBundleContent, buffer, - allDependencyAbRelativePath, + allDependencyRAbRelativePath, state, ) => images @@ -78,8 +590,8 @@ module RAB = { streamArr |> ArrayService.push( _isImageBufferDataDependencyAndRemoved(imageData) ? - OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyAbByName( - allDependencyAbRelativePath, + OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, name, state, OperateRABAssetBundleMainService.findImageByName, @@ -296,10 +808,10 @@ module RAB = { indexBufferView, }: RABType.geometry, ) => - ABBufferViewUtils.isNoneAccessorIndex(vertexBufferView) + ABBufferViewUtils.isNoneBufferViewIndex(vertexBufferView) && OptionService.isJsonSerializedValueNone(normalBufferView) && OptionService.isJsonSerializedValueNone(texCoordBufferView) - && ABBufferViewUtils.isNoneAccessorIndex(indexBufferView); + && ABBufferViewUtils.isNoneBufferViewIndex(indexBufferView); let _setGeometryDataFromBuffer = ( @@ -328,7 +840,7 @@ module RAB = { let _buildGeometryData = ( {geometrys, bufferViews}: RABType.resourceAssetBundleContent, - allDependencyAbRelativePath, + allDependencyRAbRelativePath, buffer, state, ) => @@ -351,8 +863,8 @@ module RAB = { _isGeometryBufferDataDependencyAndRemoved(geometryData) ? ( state, - OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyAbByName( - allDependencyAbRelativePath, + OperateRABAssetBundleMainService.unsafeFindDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, name, state, OperateRABAssetBundleMainService.findGeometryByName, @@ -514,20 +1026,24 @@ module RAB = { ); let assemble = (rabRelativePath, rab, wholeDependencyRelationMap) => { - let allDependencyAbRelativePath = - FindDependencyDataSystem.findAllDependencyAbRelativePath( + let allDependencyRAbRelativePath = + FindDependencyDataSystem.findAllDependencyRAbRelativePath( rabRelativePath, wholeDependencyRelationMap, ); - let (resourceAssetBundleContent, buffer) = _getContentData(rab); + let ( + resourceAssetBundleContent: RABType.resourceAssetBundleContent, + buffer, + ) = + All.getContentData(rab); let state = StateDataMainService.unsafeGetState(StateDataMain.stateData); _buildImageData( resourceAssetBundleContent, buffer, - allDependencyAbRelativePath, + allDependencyRAbRelativePath, state, ) |> Most.fromPromise @@ -552,7 +1068,7 @@ module RAB = { let (geometryMap, state) = _buildGeometryData( resourceAssetBundleContent, - allDependencyAbRelativePath, + allDependencyRAbRelativePath, buffer, state, ); @@ -586,32 +1102,32 @@ module RAB = { }; }; -let assemble = (abRelativePath, ab, wholeDependencyRelationMap) => - RAB.isRAB(abRelativePath) ? - RAB.assemble(abRelativePath, ab, wholeDependencyRelationMap) : - SAB.isSAB(abRelativePath) ? - SAB.assemble(abRelativePath, ab, wholeDependencyRelationMap) : - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="assemble", - ~description={j|unknown abRelativePath: $abRelativePath|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ); - -let isAssembled = (abRelativePath, state) => - RAB.isRAB(abRelativePath) ? - OperateRABAssetBundleMainService.isAssembled(abRelativePath, state) : - SAB.isSAB(abRelativePath) ? - OperateSABAssetBundleMainService.isAssembled(abRelativePath, state) : - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="isAssembled", - ~description={j|unknown abRelativePath: $abRelativePath|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ); \ No newline at end of file +/* let assemble = (abRelativePath, ab, wholeDependencyRelationMap) => + RAB.isRAB(abRelativePath) ? + RAB.assemble(abRelativePath, ab, wholeDependencyRelationMap) : + SAB.isSAB(abRelativePath) ? + SAB.assemble(abRelativePath, ab, wholeDependencyRelationMap) : + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="assemble", + ~description={j|unknown abRelativePath: $abRelativePath|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ); */ + +/* let isAssembled = (abRelativePath, state) => + RAB.isRAB(abRelativePath) ? + OperateRABAssetBundleMainService.isAssembled(abRelativePath, state) : + SAB.isSAB(abRelativePath) ? + OperateSABAssetBundleMainService.isAssembled(abRelativePath, state) : + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="isAssembled", + ~description={j|unknown abRelativePath: $abRelativePath|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ); */ \ No newline at end of file diff --git a/src/service/atom/OptionService.re b/src/service/atom/OptionService.re index 81cb324b9a..dffe9bcc2f 100755 --- a/src/service/atom/OptionService.re +++ b/src/service/atom/OptionService.re @@ -20,6 +20,29 @@ let unsafeGet = optionData => { optionData |> Js.Option.getExn; }; +let unsafeGetWithMessage = (msg, optionData) => { + WonderLog.Contract.requireCheck( + () => + WonderLog.( + Contract.( + Operators.( + test( + msg, + /* Log.buildAssertMessage( + ~expect={j|data exist(get by getExn)|j}, + ~actual={j|not|j}, + ), */ + () => + optionData |> assertExist + ) + ) + ) + ), + IsDebugMainService.getIsDebug(StateDataMain.stateData), + ); + optionData |> Js.Option.getExn; +}; + /* let unsafeGetJsonSerializedValue = [%raw optionData => {| if(optionData === null){ diff --git a/src/service/record/main/data/assetBundle/AssetBundleType.re b/src/service/record/main/data/assetBundle/AssetBundleType.re index f43bf5eddf..73da4d391f 100644 --- a/src/service/record/main/data/assetBundle/AssetBundleType.re +++ b/src/service/record/main/data/assetBundle/AssetBundleType.re @@ -41,14 +41,29 @@ ), }; */ +/* type imguiData = { + imguiFunc: string, + customData: WonderImgui.IMGUIType.customDataForIMGUIFunc, + }; */ + type assembleSABData = { - isAssembled: WonderCommonlib.ImmutableHashMapService.t(bool), + isLoadedMap: WonderCommonlib.ImmutableHashMapService.t(bool), + sabMap: + WonderCommonlib.ImmutableHashMapService.t(Js.Typed_array.ArrayBuffer.t), gameObjectMap: WonderCommonlib.ImmutableHashMapService.t( WonderCommonlib.ImmutableHashMapService.t( GameObjectPrimitiveType.gameObject, ), ), + /* imguiDataMap: WonderCommonlib.ImmutableHashMapService.t(imguiData), */ +}; + +type wabData = { + wholeDependencyRelationMap: + WonderCommonlib.ImmutableHashMapService.t( + DependencyDataType.dependencyRelation, + ), }; /* type assetBundleRecord = { diff --git a/src/service/state/main/assetBundle/OperateRABAssetBundleMainService.re b/src/service/state/main/assetBundle/OperateRABAssetBundleMainService.re index 330f578d9c..cde64965c8 100644 --- a/src/service/state/main/assetBundle/OperateRABAssetBundleMainService.re +++ b/src/service/state/main/assetBundle/OperateRABAssetBundleMainService.re @@ -6,8 +6,8 @@ let markAssembled = (rabRelativePath, {assetBundleRecord} as state) => { ...assetBundleRecord, assembleRABData: { ...assetBundleRecord.assembleRABData, - isAssembled: - assetBundleRecord.assembleRABData.isAssembled + isAssembledMap: + assetBundleRecord.assembleRABData.isAssembledMap |> WonderCommonlib.ImmutableHashMapService.set(rabRelativePath, true), }, }, @@ -15,7 +15,7 @@ let markAssembled = (rabRelativePath, {assetBundleRecord} as state) => { let isAssembled = (rabRelativePath, {assetBundleRecord} as state) => switch ( - assetBundleRecord.assembleRABData.isAssembled + assetBundleRecord.assembleRABData.isAssembledMap |> WonderCommonlib.ImmutableHashMapService.get(rabRelativePath) ) { | None => false @@ -101,9 +101,9 @@ let setAssembleRABData = }; }; -let findDataInAllDependencyAbByName = - (allDependencyAbRelativePath, name, state, findDataByNameFunc) => - allDependencyAbRelativePath +let findDataInAllDependencyRAbByName = + (allDependencyRAbRelativePath, name, state, findDataByNameFunc) => + allDependencyRAbRelativePath |> WonderCommonlib.ArrayService.reduceOneParam( (. result, dependencyAbRelativePath) => result |> Js.Option.isSome ? @@ -111,80 +111,120 @@ let findDataInAllDependencyAbByName = None, ); -let unsafeFindDataInAllDependencyAbByName = - (allDependencyAbRelativePath, name, state, findDataByNameFunc) => - switch ( - findDataInAllDependencyAbByName( - allDependencyAbRelativePath, - name, - state, - findDataByNameFunc, - ) - ) { - | None => - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="unsafeFindDataInAllDependencyAbByName", - ~description={j|shouldn't find nothing|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ) - | Some(result) => result - }; +let unsafeFindDataInAllDependencyRAbByName = + (allDependencyRAbRelativePath, name, state, findDataByNameFunc) => + findDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, + name, + state, + findDataByNameFunc, + ) + |> OptionService.unsafeGetWithMessage( + WonderLog.Log.buildAssertMessage( + ~expect={j|data by name:$name exist in all dependency rabs|j}, + ~actual={j|not|j}, + ), + ); + +/* switch ( + findDataInAllDependencyRAbByName( + allDependencyRAbRelativePath, + name, + state, + findDataByNameFunc, + ) + ) { + | None => + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="unsafeFindDataInAllDependencyRAbByName", + ~description={j|shouldn't find nothing|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ) + | Some(result) => result + }; */ let findLightMaterialByName = (rabRelativePath, name, state) => Some(0); let unsafeFindLightMaterialByName = (rabRelativePath, name, state) => - switch (findLightMaterialByName(rabRelativePath, name, state)) { - | None => - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="unsafeFindLightMaterialByName", - ~description={j|shouldn't find nothing|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ) - | Some(result) => result - }; + findLightMaterialByName(rabRelativePath, name, state) + |> OptionService.unsafeGetWithMessage( + WonderLog.Log.buildAssertMessage( + ~expect= + {j|lightMaterial by name:$name exist in rabRelativePath:$rabRelativePath|j}, + ~actual={j|not|j}, + ), + ); + +/* switch (findLightMaterialByName(rabRelativePath, name, state)) { + | None => + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="unsafeFindLightMaterialByName", + ~description={j|shouldn't find nothing|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ) + | Some(result) => result + }; */ let findImageByName = (rabRelativePath, name, state) => Obj.magic(-1)->Some; let unsafeFindImageByName = (rabRelativePath, name, state) => - switch (findImageByName(rabRelativePath, name, state)) { - | None => - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="unsafeFindImageByName", - ~description={j|shouldn't find nothing|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ) - | Some(result) => result - }; + findImageByName(rabRelativePath, name, state) + |> OptionService.unsafeGetWithMessage( + WonderLog.Log.buildAssertMessage( + ~expect= + {j|image by name:$name exist in rabRelativePath:$rabRelativePath|j}, + ~actual={j|not|j}, + ), + ); + +/* switch (findImageByName(rabRelativePath, name, state)) { + | None => + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="unsafeFindImageByName", + ~description={j|shouldn't find nothing|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ) + | Some(result) => result + }; */ let findGeometryByName = (rabRelativePath, name, state) => Obj.magic(-1)->Some; let unsafeFindGeometryByName = (rabRelativePath, name, state) => - switch (findGeometryByName(rabRelativePath, name, state)) { - | None => - WonderLog.Log.fatal( - WonderLog.Log.buildFatalMessage( - ~title="unsafeFindGeometryByName", - ~description={j|shouldn't find nothing|j}, - ~reason="", - ~solution={j||j}, - ~params={j||j}, - ), - ) - | Some(result) => result - }; + findGeometryByName(rabRelativePath, name, state) + |> OptionService.unsafeGetWithMessage( + WonderLog.Log.buildAssertMessage( + ~expect= + {j|geometry by name:$name exist in rabRelativePath:$rabRelativePath|j}, + ~actual={j|not|j}, + ), + ); + +/* switch (findGeometryByName(rabRelativePath, name, state)) { + | None => + WonderLog.Log.fatal( + WonderLog.Log.buildFatalMessage( + ~title="unsafeFindGeometryByName", + ~description={j|shouldn't find nothing|j}, + ~reason="", + ~solution={j||j}, + ~params={j||j}, + ), + ) + | Some(result) => result + }; */ /* let findBasicMaterialByName = (rabRelativePath, name, state) => 0; diff --git a/src/service/state/main/assetBundle/OperateSABAssetBundleMainService.re b/src/service/state/main/assetBundle/OperateSABAssetBundleMainService.re index 327036b8f2..d92418a154 100644 --- a/src/service/state/main/assetBundle/OperateSABAssetBundleMainService.re +++ b/src/service/state/main/assetBundle/OperateSABAssetBundleMainService.re @@ -2,30 +2,75 @@ open StateDataMainType; -let markAssembled = (sabRelativePath, {assetBundleRecord} as state) => { +/* let getIMGUIData = (sabRelativePath, {assetBundleRecord} as state) => + assetBundleRecord.assembleSABData.imguiDataMap + |> WonderCommonlib.ImmutableHashMapService.get(sabRelativePath); + + let setIMGUIData = + ( + sabRelativePath, + imguiData: AssetBundleType.imguiData, + {assetBundleRecord} as state, + ) => { + ...state, + assetBundleRecord: { + ...assetBundleRecord, + assembleSABData: { + ...assetBundleRecord.assembleSABData, + imguiDataMap: + assetBundleRecord.assembleSABData.imguiDataMap + |> WonderCommonlib.ImmutableHashMapService.set( + sabRelativePath, + imguiData, + ), + }, + }, + }; */ + +let getSAB = (sabRelativePath, {assetBundleRecord} as state) => + assetBundleRecord.assembleSABData.sabMap + |> WonderCommonlib.ImmutableHashMapService.get(sabRelativePath); + +let unsafeGetSAB = (sabRelativePath, state) => + getSAB(sabRelativePath, state) |> OptionService.unsafeGet; + +let setSAB = (sabRelativePath, sab, {assetBundleRecord} as state) => { + ...state, + assetBundleRecord: { + ...assetBundleRecord, + assembleSABData: { + ...assetBundleRecord.assembleSABData, + sabMap: + assetBundleRecord.assembleSABData.sabMap + |> WonderCommonlib.ImmutableHashMapService.set(sabRelativePath, sab), + }, + }, +}; + +let markLoaded = (sabRelativePath, {assetBundleRecord} as state) => { ...state, assetBundleRecord: { ...assetBundleRecord, assembleSABData: { ...assetBundleRecord.assembleSABData, - isAssembled: - assetBundleRecord.assembleSABData.isAssembled + isLoadedMap: + assetBundleRecord.assembleSABData.isLoadedMap |> WonderCommonlib.ImmutableHashMapService.set(sabRelativePath, true), }, }, }; -let isAssembled = (sabRelativePath, {assetBundleRecord} as state) => +let isLoaded = (sabRelativePath, {assetBundleRecord} as state) => switch ( - assetBundleRecord.assembleSABData.isAssembled + assetBundleRecord.assembleSABData.isLoadedMap |> WonderCommonlib.ImmutableHashMapService.get(sabRelativePath) ) { | None => false - | Some(isAssembled) => isAssembled + | Some(isLoaded) => isLoaded }; -let unsafeFindGameObjectByName = (sabRelativePath, gameObjectName, state) => 0; +/* let unsafeFindGameObjectByName = (sabRelativePath, gameObjectName, state) => 0; /* let findAllGameObjects = (sabRelativePath, state) => [|0|]->Some; */ -let unsafeFindAllGameObjects = (sabRelativePath, state) => [|0|]; \ No newline at end of file +let unsafeFindAllGameObjects = (sabRelativePath, state) => [|0|]; */ \ No newline at end of file diff --git a/src/service/state/main/assetBundle/OperateWABAssetBundleMainService.re b/src/service/state/main/assetBundle/OperateWABAssetBundleMainService.re new file mode 100644 index 0000000000..e6494d3ff5 --- /dev/null +++ b/src/service/state/main/assetBundle/OperateWABAssetBundleMainService.re @@ -0,0 +1,27 @@ +open StateDataMainType; + +let getWholeDependencyRelationMap = + (wabRelativePath, {assetBundleRecord} as state) => + assetBundleRecord.wabData.wholeDependencyRelationMap + |> WonderCommonlib.ImmutableHashMapService.get(wabRelativePath); + +let unsafeGetWholeDependencyRelationMap = (wabRelativePath, state) => + getWholeDependencyRelationMap(wabRelativePath, state) + |> OptionService.unsafeGet; + +let setWholeDependencyRelationMap = + (wabRelativePath, wholeDependencyRelation, {assetBundleRecord} as state) => { + ...state, + assetBundleRecord: { + ...assetBundleRecord, + wabData: { + ...assetBundleRecord.wabData, + wholeDependencyRelationMap: + assetBundleRecord.wabData.wholeDependencyRelationMap + |> WonderCommonlib.ImmutableHashMapService.set( + wabRelativePath, + wholeDependencyRelation, + ), + }, + }, +}; \ No newline at end of file diff --git a/src/service/state/main/assetBundle/RecordAssetBundleMainService.re b/src/service/state/main/assetBundle/RecordAssetBundleMainService.re index b6b0050691..5a6638e5dc 100644 --- a/src/service/state/main/assetBundle/RecordAssetBundleMainService.re +++ b/src/service/state/main/assetBundle/RecordAssetBundleMainService.re @@ -4,7 +4,7 @@ open StateDataMainType; let create = () => { assembleRABData: { - isAssembled: WonderCommonlib.ImmutableHashMapService.createEmpty(), + isAssembledMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), textureMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), imageMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), basicMaterialMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), @@ -15,7 +15,12 @@ let create = () => { scriptAttributeMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), }, assembleSABData: { - isAssembled: WonderCommonlib.ImmutableHashMapService.createEmpty(), + isLoadedMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), + sabMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), gameObjectMap: WonderCommonlib.ImmutableHashMapService.createEmpty(), }, + wabData: { + wholeDependencyRelationMap: + WonderCommonlib.ImmutableHashMapService.createEmpty(), + }, }; \ No newline at end of file diff --git a/src/service/state/main/data/StateDataMainType.re b/src/service/state/main/data/StateDataMainType.re index 098f132cc1..7c02fa872d 100755 --- a/src/service/state/main/data/StateDataMainType.re +++ b/src/service/state/main/data/StateDataMainType.re @@ -375,7 +375,7 @@ and scriptRecord = { ), } and assembleRABData = { - isAssembled: WonderCommonlib.ImmutableHashMapService.t(bool), + isAssembledMap: WonderCommonlib.ImmutableHashMapService.t(bool), textureMap: WonderCommonlib.ImmutableHashMapService.t( WonderCommonlib.ImmutableHashMapService.t( @@ -383,13 +383,12 @@ and assembleRABData = { ), ), imageMap: - WonderCommonlib.ImmutableHashMapService.t - ( - WonderCommonlib.ImmutableHashMapService.t - /* (WonderWebgl.DomExtendType.imageElement, Js.Typed_array.Uint8Array.t), */ - (WonderWebgl.DomExtendType.imageElement), - ), - /* (ImageType.image), */ + WonderCommonlib.ImmutableHashMapService.t( + WonderCommonlib.ImmutableHashMapService.t + /* (WonderWebgl.DomExtendType.imageElement, Js.Typed_array.Uint8Array.t), */ + (WonderWebgl.DomExtendType.imageElement), + ), + /* (ImageType.image), */ basicMaterialMap: WonderCommonlib.ImmutableHashMapService.t( WonderCommonlib.ImmutableHashMapService.t( @@ -420,6 +419,7 @@ and assembleRABData = { and assetBundleRecord = { assembleRABData, assembleSABData, + wabData, } and state = { settingRecord,