Skip to content

Commit

Permalink
feat(asset-bundle): add "add manifest data" logic
Browse files Browse the repository at this point in the history
  • Loading branch information
yyc-git committed Apr 16, 2019
1 parent 259919d commit bcf6dd4
Show file tree
Hide file tree
Showing 12 changed files with 207 additions and 58 deletions.
10 changes: 8 additions & 2 deletions src/asset_bundle/all/data/AllABType.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
type abPath = string;
type abRelativePath = string;

type abDataArr = array((abPath, Js.Typed_array.ArrayBuffer.t));
type abDataArr = array((abRelativePath, Js.Typed_array.ArrayBuffer.t));

type manifest = {
hashId: string,
dependencyRelation:
WonderCommonlib.ImmutableHashMapService.t(array(abRelativePath)),
};
6 changes: 3 additions & 3 deletions src/asset_bundle/all/data/DependencyDataType.re
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
open AllABType;

type imageDependencyRelation =
WonderCommonlib.ImmutableHashMapService.t(array(abPath));
WonderCommonlib.ImmutableHashMapService.t(array(abRelativePath));

type geometryDependencyRelation =
WonderCommonlib.ImmutableHashMapService.t(array(abPath));
WonderCommonlib.ImmutableHashMapService.t(array(abRelativePath));

/* type dependencyRelation =
WonderCommonlib.ImmutableHashMapService.t(array(abPath)); */
WonderCommonlib.ImmutableHashMapService.t(array(abRelativePath)); */

type dependencyRelation = {
imageDependencyRelation,
Expand Down
31 changes: 29 additions & 2 deletions src/asset_bundle/all/dependency/FindDependencyDataSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ let rec _isCircleDependency =
(true, recordedAbPathArr) :
abPathArr
|> WonderCommonlib.ArrayService.reduceOneParam(
(. (isCircle, recordedAbPathArr), abPath) =>
(. (isCircle, recordedAbPathArr), abRelativePath) =>
switch (
dependencyRelation
|> WonderCommonlib.ImmutableHashMapService.get(abPath)
|> WonderCommonlib.ImmutableHashMapService.get(abRelativePath)
) {
| None =>
_isCircleDependency(
Expand Down Expand Up @@ -71,4 +71,31 @@ let checkCircleDependency = dependencyRelation =>
isCircle;
},
false,
);

let _createDependencyRelation = () =>
WonderCommonlib.ImmutableHashMapService.createEmpty();

let calcWholeDependencyRelation =
({imageDependencyRelation, geometryDependencyRelation}) =>
ArrayService.fastConcat(
imageDependencyRelation |> ImmutableHashMapService.getValidKeys,
geometryDependencyRelation |> ImmutableHashMapService.getValidKeys,
)
|> ArrayService.removeDuplicateItems((. item) => item)
|> WonderCommonlib.ArrayService.reduceOneParam(
(. wholeDependencyRelation, abRelativePath) =>
wholeDependencyRelation
|> WonderCommonlib.ImmutableHashMapService.set(
abRelativePath,
imageDependencyRelation
|> WonderCommonlib.ImmutableHashMapService.get(abRelativePath)
|> Js.Option.getWithDefault(
geometryDependencyRelation
|> WonderCommonlib.ImmutableHashMapService.unsafeGet(
abRelativePath,
),
),
),
_createDependencyRelation(),
);
4 changes: 2 additions & 2 deletions src/asset_bundle/all/dependency/RemoveDependencyDataSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ module RAB = {
|> TextDecoder.decodeUint8Array(
Uint8Array.fromBufferRange(
asb,
~offset=RABUtils.getHeaderTotalByteLength(),
~offset=GenerateABUtils.getHeaderTotalByteLength(),
~length=jsonByteLength,
),
);
Expand All @@ -252,7 +252,7 @@ module RAB = {
let _getBuffer = (jsonByteLength, asb) =>
asb
|> ArrayBuffer.sliceFrom(
RABUtils.getHeaderTotalByteLength()
GenerateABUtils.getHeaderTotalByteLength()
+ jsonByteLength
|> BufferUtils.alignedLength,
);
Expand Down
Empty file.
130 changes: 130 additions & 0 deletions src/asset_bundle/all/manifest/ManifestDataSystem.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
open Js.Promise;

open WonderBsMost;

open Js.Typed_array;

module All = {
let _getHashId = [%bs.raw
arrayBuffer => {|
return crypto.subtle.digest("SHA-256", arrayBuffer)
.then(hash => {
// here hash is an arrayBuffer, so we'll convert it to its hex version
let result = '';
const view = new DataView(hash);
for (let i = 0; i < hash.byteLength; i += 4) {
result += ('00000000' + view.getUint32(i).toString(16)).slice(-8);
}
return result;
});
|}
];

let buildManifestData = (wholeDependencyRelation, arrayBuffer) =>
_getHashId(arrayBuffer)
|> then_(hashId =>
(
{hashId, dependencyRelation: wholeDependencyRelation}: AllABType.manifest
)
|> resolve
)
|> Most.fromPromise;

let _writeBuffer =
(headerAndManifestJsonAlignedByteOffset, buffer, wholeArrayBuffer) => {
let uint8Array = Uint8Array.fromBuffer(wholeArrayBuffer);

let uint8Array =
BufferUtils.mergeArrayBuffer(
uint8Array,
buffer,
headerAndManifestJsonAlignedByteOffset,
);

uint8Array |> Uint8Array.buffer;
};

let generateAB =
(bufferTotalAlignedByteLength, manifestJsonUint8Array, buffer) => {
let (
manifestJsonByteLength,
manifestJsonAlignedByteLength,
totalByteLength,
) =
GenerateABUtils.computeByteLength(
bufferTotalAlignedByteLength,
manifestJsonUint8Array,
);

let ab = ArrayBuffer.make(totalByteLength);
let dataView = DataViewCommon.create(ab);

let byteOffset =
GenerateABUtils.writeHeader(
manifestJsonByteLength,
bufferTotalAlignedByteLength,
dataView,
);

let emptyEncodedUint8Data = GenerateABUtils.getEmptyEncodedUint8Data();

let (byteOffset, _, dataView) =
GenerateABUtils.writeJson(
byteOffset,
(
emptyEncodedUint8Data,
manifestJsonAlignedByteLength,
manifestJsonUint8Array,
),
dataView,
);

_writeBuffer(byteOffset, buffer, dataView |> DataView.buffer);
};
};

module SAB = {
let addManifestData = (wholeDependencyRelation, sab) => {
let manifestJsonUint8Array =
All.buildManifestData(wholeDependencyRelation, sab)
|> GenerateABUtils.buildJsonUint8Array;

All.generateAB(
BufferUtils.alignedLength(sab |> ArrayBuffer.byteLength),
manifestJsonUint8Array,
sab,
);
};
};

module RAB = {
let addManifestData = (wholeDependencyRelation, rab) => {
let manifestJsonUint8Array =
All.buildManifestData(wholeDependencyRelation, rab)
|> GenerateABUtils.buildJsonUint8Array;

All.generateAB(
BufferUtils.alignedLength(rab |> ArrayBuffer.byteLength),
manifestJsonUint8Array,
rab,
);
};
};

let addManifestData =
(
dependencyRelation: DependencyDataType.dependencyRelation,
(sabArr, rabArr),
) => {
let wholeDependencyRelation =
FindDependencyDataSystem.calcWholeDependencyRelation(dependencyRelation);

(
sabArr
|> Js.Array.map(sab => RAB.addManifestData(wholeDependencyRelation, sab)),
rabArr
|> Js.Array.map(rab => RAB.addManifestData(wholeDependencyRelation, rab)),
);
};
48 changes: 28 additions & 20 deletions src/asset_bundle/all/utils/GenerateABUtils.re
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
open Js.Typed_array;

let _writeHeader = (jsonByteLength, bufferAlignedByteLength, dataView) =>
let getHeaderTotalByteLength = () => 8;

let writeHeader = (jsonByteLength, bufferAlignedByteLength, dataView) =>
dataView
|> DataViewCommon.writeUint32_1(jsonByteLength, 0)
|> DataViewCommon.writeUint32_1(bufferAlignedByteLength, _, dataView);

let _getEmptyEncodedUint8Data = () => {
let getEmptyEncodedUint8Data = () => {
let encoder = TextEncoder.newTextEncoder();
let emptyUint8DataArr = encoder |> TextEncoder.encodeUint8Array(" ");

Expand Down Expand Up @@ -36,7 +38,7 @@ let _writeUint8ArrayToArrayBufferWithEmptyData =
(resultByteOffset, uint8Array, dataView);
};

let _writeJson =
let writeJson =
(
byteOffset,
(emptyEncodedUint8Data, jsonAlignedByteLength, jsonUint8Array),
Expand Down Expand Up @@ -93,13 +95,13 @@ let _writeBuffer =
uint8Array |> Uint8Array.buffer;
};

let _computeByteLength = (bufferTotalAlignedByteLength, jsonUint8Array) => {
let computeByteLength = (bufferTotalAlignedByteLength, jsonUint8Array) => {
let jsonByteLength = jsonUint8Array |> Uint8Array.byteLength;

let jsonAlignedByteLength = jsonByteLength |> BufferUtils.alignedLength;

let totalByteLength =
RABUtils.getHeaderTotalByteLength()
getHeaderTotalByteLength()
+ jsonAlignedByteLength
+ bufferTotalAlignedByteLength;

Expand All @@ -117,33 +119,39 @@ let generateAB =
jsonUint8Array,
) => {
let (jsonByteLength, jsonAlignedByteLength, totalByteLength) =
_computeByteLength(bufferTotalAlignedByteLength, jsonUint8Array);
computeByteLength(bufferTotalAlignedByteLength, jsonUint8Array);

let ab = ArrayBuffer.make(totalByteLength);
let dataView = DataViewCommon.create(ab);

let byteOffset =
_writeHeader(jsonByteLength, bufferTotalAlignedByteLength, dataView);
writeHeader(jsonByteLength, bufferTotalAlignedByteLength, dataView);

let emptyEncodedUint8Data = _getEmptyEncodedUint8Data();
let emptyEncodedUint8Data = getEmptyEncodedUint8Data();

let (byteOffset, _, dataView) =
_writeJson(
writeJson(
byteOffset,
(emptyEncodedUint8Data, jsonAlignedByteLength, jsonUint8Array),
dataView,
);

let _ =
_writeBuffer(
byteOffset,
(
(imageBufferViewArr, geometryBufferViewArr),
imageUint8ArrayArr,
geometryArrayBufferArr,
),
dataView |> DataView.buffer,
);
_writeBuffer(
byteOffset,
(
(imageBufferViewArr, geometryBufferViewArr),
imageUint8ArrayArr,
geometryArrayBufferArr,
),
dataView |> DataView.buffer,
);
};

let buildJsonUint8Array = jsonRecord => {
let encoder = TextEncoder.newTextEncoder();

ab;
encoder
|> TextEncoder.encodeUint8Array(
jsonRecord |> Obj.magic |> Js.Json.stringify,
);
};
20 changes: 2 additions & 18 deletions src/asset_bundle/rab/generate/BuildRABJsonDataSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -549,21 +549,5 @@ let buildJsonUint8Array =
scriptEventFunctionArr,
scriptAttributeArr,
), */
resourceAssetBundleContent => {
let encoder = TextEncoder.newTextEncoder();

encoder
|> TextEncoder.encodeUint8Array(
/* {
images: imageArr,
textures: textureArr,
basicMaterials: basicMaterialArr,
lightMaterials: lightMaterialArr,
scriptEventFunctions: scriptEventFunctionArr,
scriptAttributes: scriptAttributeArr,
geometrys: geometryArr,
bufferViews: bufferViewArr,
} */
resourceAssetBundleContent |> Obj.magic |> Js.Json.stringify,
);
};
resourceAssetBundleContent =>
GenerateABUtils.buildJsonUint8Array(resourceAssetBundleContent);
2 changes: 1 addition & 1 deletion src/asset_bundle/rab/generate/GenerateRABSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ open Js.Typed_array;
let jsonAlignedByteLength = jsonByteLength |> BufferUtils.alignedLength;
let totalByteLength =
RABUtils.getHeaderTotalByteLength()
GenerateABUtils.getHeaderTotalByteLength()
+ jsonAlignedByteLength
+ bufferTotalAlignedByteLength;
Expand Down
2 changes: 0 additions & 2 deletions src/asset_bundle/rab/utils/RABUtils.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let getHeaderTotalByteLength = () => 8;

let computeBufferViewDataByteLength = bufferViewArr =>
switch (bufferViewArr |> ArrayService.getLast) {
| None => 0
Expand Down
10 changes: 2 additions & 8 deletions src/asset_bundle/sab/generate/BuildSABJsonDataSystem.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,5 @@ open Js.Typed_array;

open SABType;

let buildJsonUint8Array = sceneAssetBundleContent => {
let encoder = TextEncoder.newTextEncoder();

encoder
|> TextEncoder.encodeUint8Array(
sceneAssetBundleContent |> Obj.magic |> Js.Json.stringify,
);
};
let buildJsonUint8Array = sceneAssetBundleContent =>
GenerateABUtils.buildJsonUint8Array(sceneAssetBundleContent);
2 changes: 2 additions & 0 deletions src/service/atom/ImmutableHashMapService.re
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let getValidKeys = map =>
map |> Js.Dict.keys |> Js.Array.filter(value => value |> NullService.isInMap);

0 comments on commit bcf6dd4

Please sign in to comment.