Skip to content

Commit

Permalink
feat(asset-bundle): export/import wpk add asset bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc-git committed May 17, 2019
1 parent baf234a commit 6310e5f
Show file tree
Hide file tree
Showing 12 changed files with 350 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ type scriptAttribute = {
attributeStr: string,
};

type assetBundle = {
name: string,
path: string,
type_: int,
assetBundleBufferView: bufferViewIndex,
};

type copyright = {
version: string,
author: string,
Expand All @@ -86,5 +93,6 @@ type assets = {
wdbs: array(wdb),
scriptEventFunctions: array(scriptEventFunction),
scriptAttributes: array(scriptAttribute),
assetBundles: array(assetBundle),
bufferViews: array(bufferView),
};
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,67 @@ let _buildScriptAttributeData = ((editorState, engineState)) =>
);
});

let _buildAssetBundleData =
(
bufferTotalAlignedByteLength,
(imageBufferViewArr, wdbBufferViewArr),
(editorState, engineState),
) => {
let startBufferViewIndex =
(imageBufferViewArr |> Js.Array.length)
+ (wdbBufferViewArr |> Js.Array.length);

let (assetBundleArr, arrayBufferArr, bufferViewArr, byteOffset) =
AssetBundleNodeAssetEditorService.findAllAssetBundleNodes(editorState)
|> WonderCommonlib.ArrayService.reduceOneParam(
(.
(assetBundleArr, arrayBufferArr, bufferViewArr, byteOffset),
node,
) => {
let path =
PathTreeAssetLogicService.getNodePath(
node,
(editorState, engineState),
);

let {type_, assetBundle, name}: NodeAssetType.assetBundleNodeData =
AssetBundleNodeAssetService.getNodeData(node);

let byteLength = assetBundle |> ArrayBuffer.byteLength;
let alignedByteLength = BufferUtils.alignedLength(byteLength);

(
assetBundleArr
|> ArrayService.push(
{
name,
path,
type_: type_ |> NodeAssetType.convertAssetBundleTypeToInt,
assetBundleBufferView:
startBufferViewIndex + (bufferViewArr |> Js.Array.length),
}: ExportAssetType.assetBundle,
),
arrayBufferArr |> ArrayService.push(assetBundle),
bufferViewArr
|> ArrayService.push(
{byteOffset, byteLength}: ExportAssetType.bufferView,
),
byteOffset + alignedByteLength,
);
},
([||], [||], [||], bufferTotalAlignedByteLength),
);

(
assetBundleArr,
arrayBufferArr,
bufferViewArr,
bufferViewArr |> Js.Array.length === 0 ?
bufferTotalAlignedByteLength :
_computeBufferViewDataByteLength(bufferViewArr),
);
};

let buildJsonData = (imageUint8ArrayMap, (editorState, engineState)) => {
let (
imageIndexMap,
Expand Down Expand Up @@ -426,6 +487,18 @@ let buildJsonData = (imageUint8ArrayMap, (editorState, engineState)) => {
let scriptAttributeArr =
_buildScriptAttributeData((editorState, engineState));

let (
assetBundleArr,
assetBundleArrayBufferArr,
assetBundleBufferViewArr,
bufferTotalAlignedByteLength,
) =
_buildAssetBundleData(
bufferTotalAlignedByteLength,
(imageBufferViewArr, wdbBufferViewArr),
(editorState, engineState),
);

(
engineState,
(
Expand All @@ -436,9 +509,10 @@ let buildJsonData = (imageUint8ArrayMap, (editorState, engineState)) => {
wdbArr,
scriptEventFunctionArr,
scriptAttributeArr,
assetBundleArr,
),
(imageBufferViewArr, wdbBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr),
(imageBufferViewArr, wdbBufferViewArr, assetBundleBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr, assetBundleArrayBufferArr),
bufferTotalAlignedByteLength,
);
};
Expand All @@ -455,6 +529,7 @@ let buildJsonUint8Array =
wdbArr,
scriptEventFunctionArr,
scriptAttributeArr,
assetBundleArr,
),
) => {
let encoder = TextEncoder.newTextEncoder();
Expand All @@ -474,6 +549,7 @@ let buildJsonUint8Array =
scriptEventFunctions: scriptEventFunctionArr,
scriptAttributes: scriptAttributeArr,
wdbs: wdbArr,
assetBundles: assetBundleArr,
bufferViews: bufferViewArr,
}: ExportAssetType.assets
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,8 @@ let _writeBuffer =
(
headerAndJsonAlignedByteOffset,
(
(imageBufferViewArr, wdbBufferViewArr),
imageUint8ArrayArr,
wdbArrayBufferArr,
(imageBufferViewArr, wdbBufferViewArr, assetBundleBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr, assetBundleArrayBufferArr),
),
arrayBuffer,
) => {
Expand Down Expand Up @@ -73,6 +72,26 @@ let _writeBuffer =
uint8Array,
);

let uint8Array =
assetBundleBufferViewArr
|> WonderCommonlib.ArrayService.reduceOneParami(
(.
uint8Array,
{byteOffset, byteLength}: ExportAssetType.bufferView,
index,
) => {
let assetBundleArrayBuffer =
Array.unsafe_get(assetBundleArrayBufferArr, index);

BufferUtils.mergeArrayBuffer(
uint8Array,
assetBundleArrayBuffer,
headerAndJsonAlignedByteOffset + byteOffset,
);
},
uint8Array,
);

uint8Array |> Uint8Array.buffer;
};

Expand Down Expand Up @@ -100,9 +119,10 @@ let generateASB = (imageUint8ArrayMap, (editorState, engineState)) => {
wdbArr,
scriptEventFunctionArr,
scriptAttributeArr,
assetBundleArr,
),
(imageBufferViewArr, wdbBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr),
(imageBufferViewArr, wdbBufferViewArr, assetBundleBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr, assetBundleArrayBufferArr),
bufferTotalAlignedByteLength,
) =
BuildJsonDataUtils.buildJsonData(
Expand All @@ -114,14 +134,19 @@ let generateASB = (imageUint8ArrayMap, (editorState, engineState)) => {
BuildJsonDataUtils.buildJsonUint8Array(
bufferTotalAlignedByteLength,
(
Js.Array.concat(wdbBufferViewArr, imageBufferViewArr),
ArrayService.fastImmutableConcatArrays([|
imageBufferViewArr,
wdbBufferViewArr,
assetBundleBufferViewArr,
|]),
imageArr,
textureArr,
basicMaterialArr,
lightMaterialArr,
wdbArr,
scriptEventFunctionArr,
scriptAttributeArr,
assetBundleArr,
),
);

Expand All @@ -147,9 +172,8 @@ let generateASB = (imageUint8ArrayMap, (editorState, engineState)) => {
_writeBuffer(
byteOffset,
(
(imageBufferViewArr, wdbBufferViewArr),
imageUint8ArrayArr,
wdbArrayBufferArr,
(imageBufferViewArr, wdbBufferViewArr, assetBundleBufferViewArr),
(imageUint8ArrayArr, wdbArrayBufferArr, assetBundleArrayBufferArr),
),
dataView |> DataView.buffer,
);
Expand Down
Loading

0 comments on commit 6310e5f

Please sign in to comment.