Skip to content

Commit

Permalink
feat(asset-bundle): begin "import ab at runtime when run": rewrite sc…
Browse files Browse the repository at this point in the history
…ript api for asset bundle

add test cases;
  • Loading branch information
yyc-git committed May 15, 2019
1 parent 9d41c84 commit 594684b
Show file tree
Hide file tree
Showing 4 changed files with 391 additions and 32 deletions.
232 changes: 216 additions & 16 deletions src/core/job/init/InitScriptAPIJob.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,188 @@
module AssetBundle = {
open Js.Promise;

module Path = {
let getAssetBundlePath = (.) => "";
};

module Load = {
let _buildFakeFetchArrayBufferResponse =
arrayBuffer: Js.Promise.t(Fetch.Response.t) =>
{
"ok": true,
"status": "",
"statusText": "_buildFakeFetchArrayBufferResponse error",
"arrayBuffer": () => arrayBuffer |> resolve,
}
|> resolve
|> Obj.magic;

let rec _findAssetBundbleNodeData =
(nodeNameHierachy, folderNode, editorState)
: NodeAssetType.assetBundleNodeData => {
WonderLog.Contract.requireCheck(
() =>
WonderLog.(
Contract.(
Operators.(
test(
Log.buildAssertMessage(
~expect={j|nodeNameHierachy.length > 0|j},
~actual={j|not|j},
),
() =>
nodeNameHierachy |> Js.Array.length > 0
)
)
)
),
StateEditorService.getStateIsDebug(),
);

nodeNameHierachy |> Js.Array.length > 1 ?
{
let nextFolderNodeName =
ArrayService.unsafeGetFirst(nodeNameHierachy);

let nextFolderNode =
FolderNodeAssetService.getChildrenNodes(folderNode)
|> Js.Array.find(childNode =>
FolderNodeAssetService.isFolderNode(childNode)
&& FolderNodeAssetService.getNodeName(
FolderNodeAssetService.getNodeData(childNode),
)
=== nextFolderNodeName
)
|> OptionService.unsafeGet;

_findAssetBundbleNodeData(
nodeNameHierachy |> Js.Array.sliceFrom(1),
nextFolderNode,
editorState,
);
} :
{
let assetBundleNodeName =
ArrayService.unsafeGetFirst(nodeNameHierachy)
|> FileNameService.getBaseName;

FolderNodeAssetService.getChildrenNodes(folderNode)
|> Js.Array.find(childNode =>
AssetBundleNodeAssetService.isAssetBundleNode(childNode)
&& AssetBundleNodeAssetService.getNodeName(
AssetBundleNodeAssetService.getNodeData(childNode),
)
=== assetBundleNodeName
)
|> OptionService.unsafeGet
|> AssetBundleNodeAssetService.getNodeData;
};
};

let _buildFakeFetchAssetBundle =
(. abRelativePath) => {
let editorState = StateEditorService.getState();

let nodeNameHierachy = abRelativePath |> Js.String.split("/");

_findAssetBundbleNodeData(
nodeNameHierachy,
RootTreeAssetEditorService.getRootNode(editorState),
editorState,
).
assetBundle
|> _buildFakeFetchArrayBufferResponse;
};

let loadSABAndSetToState =
(
sabRelativePath,
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
),
) =>
Wonderjs.ImportABSystem.SAB.loadSABAndSetToState(
sabRelativePath,
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
_buildFakeFetchAssetBundle,
),
);

let loadAllDependencyRABAndSetToState =
(.
abRelativePath,
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
),
) =>
Wonderjs.ImportABSystem.RAB.loadAllDependencyRABAndSetToState(
abRelativePath,
wholeManifest,
(
getAssetBundlePathFunc,
initAssetBundleArrayBufferCacheFunc,
isAssetBundleArrayBufferCachedFunc,
getAssetBundleArrayBufferCacheFunc,
cacheAssetBundleArrayBufferFunc,
_buildFakeFetchAssetBundle,
),
);

let loadWABAndSetToState =
(. wabRelativePath, getAssetBundlePathFunc) =>
Wonderjs.ImportABSystem.WAB.loadWABAndSetToState(
wabRelativePath,
(getAssetBundlePathFunc, _buildFakeFetchAssetBundle),
);

let loadAssetBundle =
(. abPath) =>
Wonderjs.LoadABSystem.load(abPath, _buildFakeFetchAssetBundle);
};
module Cache = {
open WonderBsMost;

open Js.Typed_array;

let initAssetBundleArrayBufferCache = (.) => Most.empty();

let isAssetBundleArrayBufferCached =
(. abRelativePath: string, hashId: string) => Most.just(false);

let getAssetBundleArrayBufferCache =
(. abRelativePath: string) =>
WonderLog.Log.fatal(
WonderLog.Log.buildFatalMessage(
~title="getAssetBundleArrayBufferCache",
~description={j|need rewrite|j},
~reason="",
~solution={j||j},
~params={j||j},
),
);

let cacheAssetBundleArrayBuffer =
(. abRelativePath: string, ab: ArrayBuffer.t, hashId: string) =>
Most.empty();
};
};

let _buildDisposeGameObjectFunc = scriptAPIJsObj =>
(. gameObject, engineState) => {
let disposeGameObject = scriptAPIJsObj##disposeGameObject;
Expand All @@ -21,28 +206,43 @@ let _buildDisposeGameObjectFunc = scriptAPIJsObj =>
StateEngineService.unsafeGetState();
};

let _createScriptAPIJsObj = scriptAPIJsObj => {
"unsafeGetScriptAttribute": scriptAPIJsObj##unsafeGetScriptAttribute,
"unsafeGetScriptAttributeFieldValue":
scriptAPIJsObj##unsafeGetScriptAttributeFieldValue,
"setScriptAttributeFieldValue": scriptAPIJsObj##setScriptAttributeFieldValue,
"unsafeGetScriptGameObject": scriptAPIJsObj##unsafeGetScriptGameObject,
"getTransformLocalPosition": scriptAPIJsObj##getTransformLocalPosition,
/* TODO should only set MainEditorTransform->local position values */
"setTransformLocalPosition": scriptAPIJsObj##setTransformLocalPosition,
"unsafeGetGameObjectTransformComponent":
scriptAPIJsObj##unsafeGetGameObjectTransformComponent,
"disposeGameObject": _buildDisposeGameObjectFunc(scriptAPIJsObj),
"findGameObjectsByName": scriptAPIJsObj##findGameObjectsByName,
};
let _extend = [%raw
(destination, source) => {|
for (let property in source) {
destination[property] = source[property];
}
return destination;
|}
];

let _rewriteScriptAPIJsObj = scriptAPIJsObj =>
{
/* TODO improve: should only set MainEditorTransform->local position values
"setTransformLocalPosition": scriptAPIJsObj##setTransformLocalPosition, */
"disposeGameObject": _buildDisposeGameObjectFunc(scriptAPIJsObj),
"getAssetBundlePath": AssetBundle.Path.getAssetBundlePath,
"loadSABAndSetToState": AssetBundle.Load.loadSABAndSetToState,
"loadAllDependencyRABAndSetToState": AssetBundle.Load.loadAllDependencyRABAndSetToState,
"loadWABAndSetToState": AssetBundle.Load.loadWABAndSetToState,
"loadAssetBundle": AssetBundle.Load.loadAssetBundle,
"initAssetBundleArrayBufferCache": AssetBundle.Cache.initAssetBundleArrayBufferCache,
"isAssetBundleArrayBufferCached": AssetBundle.Cache.isAssetBundleArrayBufferCached,
"getAssetBundleArrayBufferCache": AssetBundle.Cache.getAssetBundleArrayBufferCache,
"cacheAssetBundleArrayBuffer": AssetBundle.Cache.cacheAssetBundleArrayBuffer,
}
|> _extend(
scriptAPIJsObj
|> Obj.magic
|> WonderCommonlib.MutableHashMapService.copy,
);

let initJob =
(_, ({apiRecord}: Wonderjs.StateDataMainType.state) as engineState) => {
...engineState,
apiRecord: {
...apiRecord,
scriptAPIJsObj:
_createScriptAPIJsObj(apiRecord.scriptAPIJsObj |> Obj.magic)
|> Obj.magic,
_rewriteScriptAPIJsObj(apiRecord.scriptAPIJsObj |> Obj.magic),
},
};
Loading

0 comments on commit 594684b

Please sign in to comment.