Skip to content

Commit

Permalink
Merge pull request #450 from akashic-games/follow-pdi-types-1.10.0
Browse files Browse the repository at this point in the history
feat: follow [email protected]
  • Loading branch information
yu-ogi authored Jun 28, 2023
2 parents 2156ac3 + b5d2ea9 commit 148491e
Show file tree
Hide file tree
Showing 9 changed files with 215 additions and 34 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# ChangeLog

## 3.14.0
* @akashic/pdi-types@1.10.0 に追従
* `"binary"` アセットに対応
* `ScriptAsset#exports` に対応

## 3.13.0
* どのボタンでマウスクリックが行われたか認識できるように
* 内部モジュールの更新
Expand Down
40 changes: 20 additions & 20 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
{
"name": "@akashic/akashic-engine",
"version": "3.13.0",
"version": "3.14.0",
"description": "The core library of Akashic Engine",
"main": "index.js",
"dependencies": {
"@akashic/game-configuration": "~1.10.0",
"@akashic/game-configuration": "~1.12.0",
"@akashic/pdi-types": "~1.11.1",
"@akashic/playlog": "~3.2.0",
"@akashic/trigger": "~2.0.0"
},
"devDependencies": {
"@akashic/eslint-config": "1.1.1",
"@akashic/pdi-common-impl": "^1.3.0",
"@akashic/pdi-common-impl": "^1.4.0",
"@types/jest": "^29.0.0",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"eslint": "^8.18.0",
Expand Down
56 changes: 55 additions & 1 deletion src/AssetAccessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { AudioAsset, ImageAsset, ScriptAsset, TextAsset, VectorImageAsset } from "@akashic/pdi-types";
import type { AudioAsset, BinaryAsset, ImageAsset, ScriptAsset, TextAsset, VectorImageAsset } from "@akashic/pdi-types";
import type { AssetManager } from "./AssetManager";

/**
Expand Down Expand Up @@ -108,6 +108,30 @@ export class AssetAccessor {
return this._assetManager.peekLiveAssetByAccessorPath(path, "vector-image");
}

/**
* パスから読み込み済みのバイナリアセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得する。
*
* パスはgame.jsonのあるディレクトリをルート (`/`) とする、 `/` 区切りの絶対パスでなければならない。
* 当該のバイナリアセットが読み込まれていない場合、エラー。
*
* @param path 取得するバイナリアセットのパス
*/
getBinary(path: string): BinaryAsset {
return this._assetManager.peekLiveAssetByAccessorPath(path, "binary");
}

/**
* パスから読み込み済みのバイナリアセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得し、その内容のバイト配列を返す。
*
* パスはgame.jsonのあるディレクトリをルート (`/`) とする、 `/` 区切りの絶対パスでなければならない。
* 当該のバイナリアセットが読み込まれていない場合、エラー。
*
* @param path 内容のバイト配列を取得するバイナリアセットのパス
*/
getBinaryData(path: string): ArrayBuffer {
return this.getBinary(path).data;
}

/**
* 与えられたパターンまたはフィルタにマッチするパスを持つ、読み込み済みの全画像アセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得する。
*
Expand Down Expand Up @@ -167,6 +191,16 @@ export class AssetAccessor {
return this._assetManager.peekAllLiveAssetsByPattern(patternOrFilter ?? "**/*", "vector-image");
}

/**
* 与えられたパターンまたはフィルタにマッチするパスを持つ、読み込み済みのバイナリアセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得する。
* 引数の仕様については `AssetAccessor#getAllImages()` の仕様を参照のこと。
*
* @param patternOrFilter 取得するベクタ画像アセットのパスパターンまたはフィルタ。省略した場合、読み込み済みの全て
*/
getAllBinaries(patternOrFilter?: string | ((path: string) => boolean)): BinaryAsset[] {
return this._assetManager.peekAllLiveAssetsByPattern(patternOrFilter ?? "**/*", "binary");
}

/**
* アセットIDから読み込み済みの画像アセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得する。
* 当該の画像アセットが読み込まれていない場合、エラー。
Expand Down Expand Up @@ -236,4 +270,24 @@ export class AssetAccessor {
getVectorImageById(assetId: string): VectorImageAsset {
return this._assetManager.peekLiveAssetById(assetId, "vector-image");
}

/**
* アセットIDから読み込み済みのバイナリアセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得する。
* 当該のバイナリアセットが読み込まれていない場合、エラー。
*
* @param assetId 取得するバイナリアセットのID
*/
getBinaryById(assetId: string): BinaryAsset {
return this._assetManager.peekLiveAssetById(assetId, "binary");
}

/**
* アセットIDから読み込み済みのバイナリアセット(現在のシーンで読み込んだ、またはグローバルなアセット)を取得し、その内容のバイト配列を返す。
* 当該のバイナリアセットが読み込まれていない場合、エラー。
*
* @param assetId 取得するバイナリアセットのID
*/
getBinaryDataById(assetId: string): ArrayBuffer {
return this.getBinaryById(assetId).data;
}
}
22 changes: 17 additions & 5 deletions src/AssetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import type {
TextAssetConfigurationBase,
AudioAssetConfigurationBase,
VideoAssetConfigurationBase,
VectorImageAssetConfigurationBase
VectorImageAssetConfigurationBase,
BinaryAssetConfigurationBase
} from "@akashic/game-configuration";
import type {
Asset,
Expand All @@ -23,20 +24,22 @@ import type {
ScriptAsset,
TextAsset,
VideoAsset,
VectorImageAsset
VectorImageAsset,
BinaryAsset
} from "@akashic/pdi-types";
import type { AssetGenerationConfiguration } from "./AssetGenerationConfiguration";
import type { AssetManagerLoadHandler } from "./AssetManagerLoadHandler";
import type { AudioSystem } from "./AudioSystem";
import type { AudioSystemManager } from "./AudioSystemManager";
import { EmptyBinaryAsset } from "./auxiliary/EmptyBinaryAsset";
import { EmptyGeneratedVectorImageAsset } from "./auxiliary/EmptyGeneratedVectorImageAsset";
import { EmptyVectorImageAsset } from "./auxiliary/EmptyVectorImageAsset";
import { PartialImageAsset } from "./auxiliary/PartialImageAsset";
import type { DynamicAssetConfiguration } from "./DynamicAssetConfiguration";
import { ExceptionFactory } from "./ExceptionFactory";
import { VideoSystem } from "./VideoSystem";

export type OneOfAsset = AudioAsset | ImageAsset | ScriptAsset | TextAsset | VideoAsset | VectorImageAsset;
export type OneOfAsset = AudioAsset | ImageAsset | ScriptAsset | TextAsset | VideoAsset | VectorImageAsset | BinaryAsset;

// TODO: 以下の internal types を game-configuration に切り出す
type AssetConfigurationCore =
Expand All @@ -45,7 +48,8 @@ type AssetConfigurationCore =
| VideoAssetConfiguration
| AudioAssetConfiguration
| TextAssetConfiguration
| ScriptAssetConfiguration;
| ScriptAssetConfiguration
| BinaryAssetConfiguration;

type UnneededKeysForAsset = "path" | "virtualPath" | "global";

Expand All @@ -67,6 +71,9 @@ interface TextAssetConfiguration
interface ScriptAssetConfiguration
extends Omit<AssetConfigurationCommonBase, "type">,
Omit<ScriptAssetConfigurationBase, UnneededKeysForAsset> {}
interface BinaryAssetConfiguration
extends Omit<BinaryAssetConfigurationBase, "type">,
Omit<BinaryAssetConfigurationBase, UnneededKeysForAsset> {}

type AssetIdOrConf = string | DynamicAssetConfiguration | AssetGenerationConfiguration;

Expand Down Expand Up @@ -649,7 +656,7 @@ export class AssetManager implements AssetLoadHandler {
case "text":
return resourceFactory.createTextAsset(id, uri);
case "script":
return resourceFactory.createScriptAsset(id, uri);
return resourceFactory.createScriptAsset(id, uri, conf.exports);
case "video":
// VideoSystemはまだ中身が定義されていなが、将来のためにVideoAssetにVideoSystemを渡すという体裁だけが整えられている。
// 以上を踏まえ、ここでは簡単のために都度新たなVideoSystemインスタンスを生成している。
Expand All @@ -660,6 +667,11 @@ export class AssetManager implements AssetLoadHandler {
return new EmptyVectorImageAsset(id, uri, conf.width, conf.height, conf.hint);
}
return resourceFactory.createVectorImageAsset(id, uri, conf.width, conf.height, conf.hint);
case "binary":
if (!resourceFactory.createBinaryAsset) {
return new EmptyBinaryAsset(id, uri);
}
return resourceFactory.createBinaryAsset(id, uri);
default:
throw ExceptionFactory.createAssertionError(
"AssertionError#_createAssetFor: unknown asset type " + type + " for asset ID: " + id
Expand Down
11 changes: 10 additions & 1 deletion src/DynamicAssetConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
AssetConfigurationCommonBase,
AudioAssetConfigurationBase,
BinaryAssetConfigurationBase,
ImageAssetConfigurationBase,
ScriptAssetConfigurationBase,
TextAssetConfigurationBase,
Expand All @@ -14,7 +15,8 @@ export type DynamicAssetConfiguration =
| DynamicVectorImageAssetConfigurationBase
| DynamicTextAssetConfigurationBase
| DynamicScriptAssetConfigurationBase
| DynamicVideoAssetConfigurationBase;
| DynamicVideoAssetConfigurationBase
| DynamicBinaryAssetConfigurationBase;

/**
* (実行時に定義される)Assetの設定を表すインターフェース。
Expand Down Expand Up @@ -75,5 +77,12 @@ export interface DynamicScriptAssetConfigurationBase
extends Omit<DynamicAssetConfigurationBase, "type">,
Omit<ScriptAssetConfigurationBase, UnneededKeysForDynamicAsset> {}

/**
* BinaryAssetの設定。
*/
export interface DynamicBinaryAssetConfigurationBase
extends Omit<DynamicAssetConfigurationBase, "type">,
Omit<BinaryAssetConfigurationBase, UnneededKeysForDynamicAsset> {}

// interface メンバは多重継承できないため、 DynamicAssetConfigurationBase の type メンバ を Omit する
type UnneededKeysForDynamicAsset = "path" | "virtualPath" | "global";
32 changes: 32 additions & 0 deletions src/__tests__/AssetAccessorSpec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe("test AssetAccessor", () => {
width: 64,
height: 64
},
"id-assets/bin/lib01.wasm": {
type: "binary",
path: "assets/bin/lib01.wasm",
virtualPath: "assets/bin/lib01.wasm"
},
"node_modules/@akashic-extension/some-library/lib/index.js": {
type: "script",
path: "node_modules/@akashic-extension/some-library/lib/index.js",
Expand Down Expand Up @@ -94,6 +99,8 @@ describe("test AssetAccessor", () => {
testValue: true
});

const sampleArrayBufferContent = new ArrayBuffer(8);

const assetIds = [
"id-script/main.js",
"id-assets/stage01/bgm01",
Expand All @@ -102,6 +109,7 @@ describe("test AssetAccessor", () => {
"id-assets/stage01/map.json",
"id-assets/chara01/image.png",
"id-assets/icon/icon01.svg",
"id-assets/bin/lib01.wasm",
"node_modules/@akashic-extension/some-library/lib/index.js",
"node_modules/@akashic-extension/some-library/assets/image.png",
"node_modules/@akashic-extension/some-library/assets/boss.png",
Expand All @@ -111,6 +119,7 @@ describe("test AssetAccessor", () => {
function setupAssetAccessor(assetIds: string[], fail: (arg: any) => void, callback: (accessor: AssetAccessor) => void): void {
const game = new Game(gameConfiguration);
game.resourceFactory.scriptContents["assets/stage01/map.json"] = sampleJSONFileContent;
game.resourceFactory.binaryContents["assets/bin/lib01.wasm"] = sampleArrayBufferContent;

const manager = game._assetManager;
const accessor = new AssetAccessor(manager);
Expand Down Expand Up @@ -165,8 +174,16 @@ describe("test AssetAccessor", () => {
path: "assets/icon/icon01.svg"
});

expect(extractAssetProps(accessor.getBinary("/assets/bin/lib01.wasm"))).toEqual({
id: "id-assets/bin/lib01.wasm",
type: "binary",
path: "assets/bin/lib01.wasm"
});

expect(accessor.getTextContent("/assets/stage01/map.json")).toBe(sampleJSONFileContent);
expect(accessor.getJSONContent("/assets/stage01/map.json")).toEqual(JSON.parse(sampleJSONFileContent));
expect(accessor.getBinaryData("/assets/bin/lib01.wasm")).toBe(sampleArrayBufferContent);

done();
}
);
Expand Down Expand Up @@ -268,6 +285,14 @@ describe("test AssetAccessor", () => {
}
]);

expect(accessor.getAllBinaries().map(extractAssetProps)).toEqual([
{
id: "id-assets/bin/lib01.wasm",
type: "binary",
path: "assets/bin/lib01.wasm"
}
]);

done();
}
);
Expand Down Expand Up @@ -308,8 +333,15 @@ describe("test AssetAccessor", () => {
path: "assets/icon/icon01.svg"
});

expect(extractAssetProps(accessor.getBinaryById("id-assets/bin/lib01.wasm"))).toEqual({
id: "id-assets/bin/lib01.wasm",
type: "binary",
path: "assets/bin/lib01.wasm"
});

expect(accessor.getTextContentById("id-assets/stage01/map.json")).toBe(sampleJSONFileContent);
expect(accessor.getJSONContentById("id-assets/stage01/map.json")).toEqual(JSON.parse(sampleJSONFileContent));
expect(accessor.getBinaryDataById("id-assets/bin/lib01.wasm")).toBe(sampleArrayBufferContent);
done();
}
);
Expand Down
Loading

0 comments on commit 148491e

Please sign in to comment.