Skip to content

Commit

Permalink
feat(wdb): finish export package store wdb snapshot to asb, import pa…
Browse files Browse the repository at this point in the history
…ckage should use it
  • Loading branch information
AmyOrz committed Apr 25, 2019
1 parent bc1c407 commit a7c265b
Show file tree
Hide file tree
Showing 10 changed files with 282 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type wdb = {
name: string,
path: string,
bufferView: bufferViewIndex,
snapshot: imageIndex,
};

type image = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ let _buildMaterialData =

let _buildWDBData =
(
imageUint8ArrayMap,
(imageIndexMap, imageUint8ArrayMap),
imageAlignedByteLength,
imageBufferViewArr,
(editorState, engineState),
Expand All @@ -264,7 +264,7 @@ let _buildWDBData =
(engineState, wdbArr, arrayBufferArr, bufferViewArr, byteOffset),
node,
) => {
let {name, wdbGameObject}: NodeAssetType.wdbNodeData =
let {name, wdbGameObject, imageDataIndex}: NodeAssetType.wdbNodeData =
WDBNodeAssetService.getNodeData(node);

let (engineState, wdbArrayBuffer) =
Expand All @@ -289,6 +289,11 @@ let _buildWDBData =
),
bufferView:
imageBufferViewIndex + (bufferViewArr |> Js.Array.length),
snapshot:
imageIndexMap
|> WonderCommonlib.ImmutableSparseMapService.unsafeGet(
imageDataIndex,
),
}: ExportAssetType.wdb,
),
arrayBufferArr |> ArrayService.push(wdbArrayBuffer),
Expand Down Expand Up @@ -414,7 +419,7 @@ let buildJsonData = (imageUint8ArrayMap, (editorState, engineState)) => {
bufferTotalAlignedByteLength,
) =
_buildWDBData(
imageUint8ArrayMap,
(imageIndexMap, imageUint8ArrayMap),
imageAlignedByteLength,
imageBufferViewArr,
(editorState, engineState),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ let generateASB = (imageUint8ArrayMap, (editorState, engineState)) => {
(imageUint8ArrayArr, wdbArrayBufferArr),
bufferTotalAlignedByteLength,
) =
BuildJsonDataUtils.buildJsonData(
HeaderBuildJsonDataUtils.buildJsonData(
imageUint8ArrayMap,
(editorState, engineState),
);

let jsonUint8Array =
BuildJsonDataUtils.buildJsonUint8Array(
HeaderBuildJsonDataUtils.buildJsonUint8Array(
bufferTotalAlignedByteLength,
(
Js.Array.concat(wdbBufferViewArr, imageBufferViewArr),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,11 @@ let buildMaterialData =
(editorState, engineState),
);

(basicMaterialMap, lightMaterialMap, (editorState, engineState));
(
imageDataIndexMap,
(basicMaterialMap, lightMaterialMap),
(editorState, engineState),
);
};

let _buildScriptEventFunctionEditorData =
Expand Down Expand Up @@ -582,6 +586,7 @@ let _mergeImageUint8ArrayDataMap =

let buildWDBData =
(
imageDataIndexMap,
{wdbs, bufferViews}: ExportAssetType.assets,
buffer,
(editorState, engineState),
Expand All @@ -595,7 +600,7 @@ let buildWDBData =
wdbs
|> WonderBsMost.Most.from
|> WonderBsMost.Most.concatMap(
({name, bufferView, path}: ExportAssetType.wdb) => {
({name, bufferView, path, snapshot}: ExportAssetType.wdb) => {
let editorState = StateEditorService.getState();
let engineState = StateEngineService.unsafeGetState();

Expand All @@ -609,9 +614,8 @@ let buildWDBData =
path,
(editorState, engineState),
);

HeaderImportASBWDBUtils.importWDB(
(name, arrayBuffer),
(imageDataIndexMap, snapshot, name, arrayBuffer),
(assetNodeId, parentFolderNode),
(editorState, engineState),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ let importASB = asb => {
);
})
|> WonderBsMost.Most.flatMap(
((basicMaterialMap, lightMaterialMap, (editorState, engineState))) =>
(
(
imageDataIndexMap,
(basicMaterialMap, lightMaterialMap),
(editorState, engineState),
),
) =>
HeaderBuildAssetDataUtils.buildWDBData(
imageDataIndexMap,
asbRecord,
buffer,
(editorState, engineState),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
let importWDB =
(
(name, wdbArrayBuffer),
(imageDataIndexMap, snapshot, name, wdbArrayBuffer),
(wdbNodeId, parentFolderNode),
(editorState, engineState),
) =>
WDBAssetLogicService.importAssetWDB(
(name, wdbArrayBuffer),
(wdbNodeId, parentFolderNode),
false,
(wdbNodeId, parentFolderNode, false),
WDBAssetLogicService.createWDBNodeUseImageDataMapSnapshot((
imageDataIndexMap,
snapshot,
)),
(editorState, engineState),
);
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,15 @@ let _handleAssetWDBType =
WDBAssetLogicService.importAssetWDB(
(
FileNameService.getBaseName(fileName)
->(
OperateTreeAssetLogicService.getUniqueNodeName(
selectedFolderNodeInAssetTree,
engineState,
)
),
|> OperateTreeAssetLogicService.getUniqueNodeName(
_,
selectedFolderNodeInAssetTree,
engineState,
),
wdbArrayBuffer,
),
(wdbNodeId, selectedFolderNodeInAssetTree),
true,
(wdbNodeId, selectedFolderNodeInAssetTree, true),
WDBAssetLogicService.createWDBNodeUseCreatedSnapshot,
(editorState, engineState),
)
|> then_(
Expand Down Expand Up @@ -287,7 +286,9 @@ let fileLoad = ((uiState, dispatchFunc), createJsZipFunc, event) => {
FileReader.makeSureCanLoadSameNameFileAgain(target);

dispatchFunc(
AppStore.UpdateAction(Update([|UpdateStore.Inspector,UpdateStore.Project|])),
AppStore.UpdateAction(
Update([|UpdateStore.Inspector, UpdateStore.Project|]),
),
);

resolve();
Expand Down
79 changes: 51 additions & 28 deletions src/service/stateTuple/logic/asset/WDBAssetLogicService.re
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,56 @@ let _createWDBNodeAndSnapshot =
);
};

let createWDBNodeUseImageDataMapSnapshot =
(
(imageDataIndexMap, snapshot),
(wdbNodeId, name, gameObject, parentFolderNode),
editorState,
) =>
editorState
|> OperateTreeAssetEditorService.insertNode(
NodeAssetService.getNodeId(~node=parentFolderNode),
WDBNodeAssetService.buildNode(
~nodeId=wdbNodeId,
~name,
~wdbGameObject=gameObject,
~imageDataIndex=
imageDataIndexMap
|> WonderCommonlib.ImmutableSparseMapService.unsafeGet(snapshot),
),
);

let createWDBNodeUseCreatedSnapshot =
((wdbNodeId, name, gameObject, parentFolderNode), editorState) => {
let inspectorEngineState =
(editorState, StateInspectorEngineService.unsafeGetState())
|> AssetTreeInspectorUtils.disposeContainerGameObjectAllChildrenAndReallocateCPUMemory
|> WDBInspectorEngineUtils.createWDBIntoInspectorCanvas(
gameObject,
(StateEditorService.getState(), StateEngineService.unsafeGetState()),
)
|> StateLogicService.renderInspectorEngineStateAndReturnState;

let editorState =
editorState
|> _createWDBNodeAndSnapshot(
parentFolderNode,
(wdbNodeId, name, gameObject),
);

(editorState, inspectorEngineState)
|> AssetTreeInspectorUtils.disposeContainerGameObjectAllChildrenAndReallocateCPUMemory
|> AssetTreeInspectorUtils.setCameraDefaultDistance
|> StateInspectorEngineService.setState;

editorState;
};

let importAssetWDB =
(
(name, wdbArrayBuffer),
(wdbNodeId, parentFolderNode),
isLoadImage,
(wdbNodeId, parentFolderNode, isLoadImage),
createWDBNodeFunc,
(editorState, engineState),
) => {
let allGameObjectsRef = ref([||]);
Expand All @@ -61,32 +106,10 @@ let importAssetWDB =
engineState,
);

/* TODO need test */
let inspectorEngineState =
(editorState, StateInspectorEngineService.unsafeGetState())
|> AssetTreeInspectorUtils.disposeContainerGameObjectAllChildrenAndReallocateCPUMemory
|> WDBInspectorEngineUtils.createWDBIntoInspectorCanvas(
gameObject,
(
StateEditorService.getState(),
StateEngineService.unsafeGetState(),
),
)
|> StateLogicService.renderInspectorEngineStateAndReturnState;

let editorState =
editorState
|> _createWDBNodeAndSnapshot(
parentFolderNode,
(wdbNodeId, name, gameObject),
);

(editorState, inspectorEngineState)
|> AssetTreeInspectorUtils.disposeContainerGameObjectAllChildrenAndReallocateCPUMemory
|> AssetTreeInspectorUtils.setCameraDefaultDistance
|> StateInspectorEngineService.setState;

editorState |> StateEditorService.setState |> ignore;
editorState
|> createWDBNodeFunc((wdbNodeId, name, gameObject, parentFolderNode))
|> StateEditorService.setState
|> ignore;

engineState
|> GameObjectEngineService.setAllGameObjectsIsRenderIfHasMeshRenderer(
Expand Down
71 changes: 68 additions & 3 deletions test/integration/header/headerExportPackage_test.re
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ let _ =
describe("header export package", () => {
let sandbox = getSandboxDefaultVal();

let boxTexturedWDBArrayBuffer = ref(Obj.magic(1));
let sceneWDBArrayBuffer = ref(Obj.magic(1));

beforeAll(() => {
boxTexturedWDBArrayBuffer := WDBTool.convertGLBToWDB("BoxTextured");
sceneWDBArrayBuffer := WDBTool.generateSceneWDB();
});

beforeEach(() => {
sandbox := createSandbox();

Expand Down Expand Up @@ -145,7 +153,7 @@ let _ =

MainEditorAssetHeaderOperateNodeTool.addMaterial();

let wpkArrayBuffer = ExportPackageTool.exportWPK();
ExportPackageTool.exportWPK() |> ignore;

let editorState = StateEditorService.getState();

Expand Down Expand Up @@ -195,7 +203,7 @@ let _ =
(),
);

let wpkArrayBuffer = ExportPackageTool.exportWPK();
ExportPackageTool.exportWPK() |> ignore;

let editorState = StateEditorService.getState();

Expand All @@ -212,10 +220,67 @@ let _ =
({base64, uint8Array}) =>
uint8Array
|> OptionService.unsafeGet
|> expect == BufferUtils.convertBase64ToUint8Array(imgCanvasFakeBase64Str)
|> expect
== BufferUtils.convertBase64ToUint8Array(
imgCanvasFakeBase64Str,
)
);
},
);
});

describe("test export all wdbs->snapshot", () =>
testPromise(
"upload one wdb w1;
export;
should convert w1->snapshot base64 to uint8Array;",
() => {
let (
addedMaterialNodeId,
newMaterialComponent,
imgCanvasFakeBase64Str,
(inspectorCanvasDom, imgCanvasDom),
) =
MainEditorLightMaterialForAssetTool.prepareInspectorMaterialSphereAndImgCanvas(
~sandbox,
(),
);

MainEditorAssetUploadTool.loadOneWDB(
~arrayBuffer=boxTexturedWDBArrayBuffer^,
(),
)
|> then_(uploadedWDBNodeId => {
ExportPackageTool.exportWPK() |> ignore;

let editorState = StateEditorService.getState();
let engineState = StateEngineService.unsafeGetState();

let {imageDataIndex}: wdbNodeData =
editorState
|> OperateTreeAssetEditorService.unsafeFindNodeById(
uploadedWDBNodeId,
)
|> WDBNodeAssetService.getNodeData;

editorState
|> ImageDataMapAssetEditorService.unsafeGetData(
imageDataIndex,
)
|> (
({base64, uint8Array}) =>
uint8Array
|> OptionService.unsafeGet
|> expect
== BufferUtils.convertBase64ToUint8Array(
imgCanvasFakeBase64Str,
)
|> resolve
);
});
},
)
);
});
});
Loading

0 comments on commit a7c265b

Please sign in to comment.