hzengine-core 0.1.2-dev → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hzengine-core",
3
- "version": "0.1.2-dev",
3
+ "version": "0.1.3",
4
4
  "description": "An Visual Novel Engine for Zepp OS",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./src/index.ts",
@@ -12,15 +12,20 @@ export interface Platform<WidgetFactory = any> {
12
12
  isFileSync({ path }: { path: string }): boolean;
13
13
  readdirSync(option: Platform.readdirSync.Option): Platform.readdirSync.Result;
14
14
  statSync(option: Platform.statSync.Option): Platform.statSync.Result;
15
- readFileSync(option: Platform.readFileSync.Option): Platform.readFileSync.Result;
15
+ readFileSync(
16
+ option: Platform.readFileSync.Option
17
+ ): Platform.readFileSync.Result;
16
18
  writeFileSync(option: Platform.writeFileSync.Option): void;
17
19
 
18
-
19
-
20
- getImageInfo(img_path: string): {
21
- width: number;
22
- height: number;
23
- };
20
+ getImageInfo(img_path: string):
21
+ | {
22
+ width: number;
23
+ height: number;
24
+ }
25
+ | Promise<{
26
+ width: number;
27
+ height: number;
28
+ }>;
24
29
 
25
30
  // timer & async
26
31
  getTime(): number; // in Utc
@@ -33,8 +38,6 @@ export interface Platform<WidgetFactory = any> {
33
38
  }
34
39
 
35
40
  export declare namespace Platform {
36
-
37
-
38
41
  export interface AudioPlayer {
39
42
  onPrepared?: (success: boolean) => void | undefined;
40
43
  onCompleted?: () => void | undefined;
@@ -43,7 +46,7 @@ export declare namespace Platform {
43
46
  stop(): void;
44
47
  release(): void;
45
48
  getMediaInfo(): MediaInfo;
46
- setSource({ path }: { path: string }): void;
49
+ setSource({ path }: { path: string }): void; //TODO path? file?
47
50
  }
48
51
 
49
52
  export interface MediaInfo {
@@ -154,5 +157,4 @@ export declare namespace Platform {
154
157
  }
155
158
  }
156
159
 
157
-
158
- export default Platform;
160
+ export default Platform;
@@ -146,7 +146,7 @@ export class Animation<P extends Profile.PropsType = unknown> {
146
146
  if (typeof props[key] === "number") {
147
147
  if (_former_props[key] == undefined) _former_props[key] = 0;
148
148
  res[key] =
149
- _former_props[key] + wraped_prog * (props[key] - _former_props[key]);
149
+ _former_props[key] + wraped_prog * ((props[key] as any) - _former_props[key]);
150
150
  } else if (typeof props[key] === "boolean") {
151
151
  if (_former_props[key] == undefined) _former_props[key] = false;
152
152
  res[key] = wraped_prog === 1 ? props[key] : _former_props[key];
@@ -36,6 +36,18 @@ export class Script {
36
36
  private accessor _nextRunPosition: [path: string, index: number] | null =
37
37
  null;
38
38
 
39
+ public get nextRunPosition() {
40
+ return this._nextRunPosition;
41
+ }
42
+
43
+ /**
44
+ * 当前正在执行的命令内容
45
+ */
46
+ private _currentRawCommand: string = "-";
47
+ public get currentRawCommand() {
48
+ return this._currentRawCommand;
49
+ }
50
+
39
51
  // Script Run
40
52
 
41
53
  /**
@@ -61,6 +73,7 @@ export class Script {
61
73
  // remove comment
62
74
 
63
75
  rawCommand = removeComment(rawCommand);
76
+ this._currentRawCommand = rawCommand.trim();
64
77
 
65
78
  if (rawCommand.trim().length && !rawCommand.trim().startsWith("#")) {
66
79
  if (rawCommand.trim().startsWith("*")) {
@@ -4,7 +4,7 @@ import { HZEngineCore, Platform } from "../index.js";
4
4
  import Path from "../utils/path.js";
5
5
  // import { isFileSync } from "./fs.js";
6
6
 
7
- export class Storage{
7
+ export class Storage {
8
8
  constructor(private _core: HZEngineCore) {}
9
9
 
10
10
  projectRoot: string | null = null;
@@ -354,53 +354,55 @@ export class Storage{
354
354
  if (!this._core.platform.readdirSync({ path: scriptDir }))
355
355
  throw "项目文件夹中script文件夹不存在";
356
356
 
357
-
358
- /**
357
+ /**
359
358
  * 1. 预加载所有的label并检查冲突
360
359
  * 2. 记录所有脚本文件的行数
361
360
  */
362
- const preloadHzs = (path: string) => {
363
- // let fd = this._core.platform.openSync({ path });
364
- // if (fd < 0) throw "Fd<0";
365
- // let size = this._core.platform.statSync({ path })!.size;
366
- // let arrbuf = new ArrayBuffer(size);
367
- // this._core.platform.readSync({ fd, buffer: arrbuf });
368
- // let buffer = Buffer.from(arrbuf);
369
- // let contentStr = buffer.toString();
370
- let contentStr = this._core.platform.readFileSync({ path, options: { encoding: "utf8" }}) as string;
371
- let contentLines = contentStr.split("\n");
372
- let totalLines = contentLines.length;
373
- hzsInfoMap[path] = { totalLines };
374
- for (let i = 0; i < totalLines; ++i) {
375
- let line = contentLines[i].trim();
376
- if (line.startsWith("*")) {
377
- let len = line.length,
378
- p = 1,
379
- q;
380
- while (p < len && line.charAt(p) === " ") ++p;
381
- if (p === len)
382
- throw `Lost Label Name at file(${path}) line(${i + 1})`;
383
- q = p;
384
- while (q < len && line.charAt(q) !== " ") ++q;
385
- // [p, q)
386
- let label = line.slice(p, q);
387
-
388
- if (labelMap[label]) {
389
- throw `Label name "${label}" conflict : \
361
+ const preloadHzs = (path: string) => {
362
+ // let fd = this._core.platform.openSync({ path });
363
+ // if (fd < 0) throw "Fd<0";
364
+ // let size = this._core.platform.statSync({ path })!.size;
365
+ // let arrbuf = new ArrayBuffer(size);
366
+ // this._core.platform.readSync({ fd, buffer: arrbuf });
367
+ // let buffer = Buffer.from(arrbuf);
368
+ // let contentStr = buffer.toString();
369
+ let contentStr = this._core.platform.readFileSync({
370
+ path,
371
+ options: { encoding: "utf8" },
372
+ }) as string;
373
+ let contentLines = contentStr.split("\n");
374
+ let totalLines = contentLines.length;
375
+ hzsInfoMap[path] = { totalLines };
376
+ for (let i = 0; i < totalLines; ++i) {
377
+ let line = contentLines[i].trim();
378
+ if (line.startsWith("*")) {
379
+ let len = line.length,
380
+ p = 1,
381
+ q;
382
+ while (p < len && line.charAt(p) === " ") ++p;
383
+ if (p === len)
384
+ throw `Lost Label Name at file(${path}) line(${i + 1})`;
385
+ q = p;
386
+ while (q < len && line.charAt(q) !== " ") ++q;
387
+ // [p, q)
388
+ let label = line.slice(p, q);
389
+
390
+ if (labelMap[label]) {
391
+ throw `Label name "${label}" conflict : \
390
392
  at [${labelMap[label][0]}(line ${labelMap[label][1]})] \
391
393
  [${path}(line ${i + 1})]`;
392
- }
393
-
394
- labelMap[label] = [path, i];
395
- }
396
394
  }
395
+
396
+ labelMap[label] = [path, i];
397
397
  }
398
+ }
399
+ };
398
400
 
399
401
  // 遍历所有hzs文件
400
402
  const traverseScript = (path: string) => {
401
403
  let dirs = this._core.platform.readdirSync({ path });
402
404
  // console.log(dirs);
403
-
405
+
404
406
  for (let dir of dirs!) {
405
407
  let subpath = Path.join(path, dir);
406
408
  if (this._core.platform.isFileSync({ path: subpath })) {
@@ -413,10 +415,8 @@ export class Storage{
413
415
  traverseScript(subpath);
414
416
  }
415
417
  }
416
- }
418
+ };
417
419
  traverseScript(scriptDir);
418
-
419
-
420
420
  }
421
421
  /**
422
422
  * 预加载资源
@@ -448,7 +448,7 @@ export class Storage{
448
448
  const traverseImage = (path: string) => {
449
449
  let dirs = this._core.platform.readdirSync({ path });
450
450
  // console.log(dirs);
451
-
451
+
452
452
  for (let dir of dirs!) {
453
453
  let subpath = Path.join(path, dir);
454
454
  if (this._core.platform.isFileSync({ path: subpath })) {
@@ -461,10 +461,9 @@ export class Storage{
461
461
  traverseImage(subpath);
462
462
  }
463
463
  }
464
- }
464
+ };
465
465
  traverseImage(imageDir);
466
466
 
467
-
468
467
  // console.log(
469
468
  // `Preloaded image: ${JSON.stringify(this.preloadedData.image.nameMap)}`
470
469
  // );
@@ -479,10 +478,12 @@ export class Storage{
479
478
  this.preloadedData.animation.profileMap;
480
479
 
481
480
  let animationDir = Path.join(this.projectRoot!, "animation");
482
- if (!this._core.platform.readdirSync({ path: animationDir }))
483
- throw "项目文件夹中animation文件夹不存在";
481
+ if (!this._core.platform.readdirSync({ path: animationDir })) {
482
+ // throw "项目文件夹中animation文件夹不存在";
483
+ console.log("Warning: 项目文件夹中animation文件夹不存在");
484
+ return; // allow project without animation folder
485
+ }
484
486
 
485
-
486
487
  function preloadAnimation(path: string) {
487
488
  let raw_name = Path.parse(path).name;
488
489
  let name_key = raw_name.trim().replace(/ +/, "_");
@@ -506,9 +507,8 @@ export class Storage{
506
507
  traverseAnimation(subpath);
507
508
  }
508
509
  }
509
- }
510
+ };
510
511
  traverseAnimation(animationDir);
511
-
512
512
  }
513
513
  // Decorator Field
514
514
  // _archiveStateSetterRegisteredList: Set<string> = new Set();
package/tsconfig.json CHANGED
@@ -11,7 +11,7 @@
11
11
  // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
12
12
 
13
13
  /* Language and Environment */
14
- "target": "ES6", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
14
+ "target": "ES2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
15
15
  // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
16
  // "jsx": "preserve", /* Specify what JSX code is generated. */
17
17
  // "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
@@ -1,134 +1,137 @@
1
- export interface Platform<WidgetFactory = any> {
2
- name: string;
3
- getScreenSize(): [width: number, height: number];
4
- createUILayer({ z_index }: {
5
- z_index: number;
6
- }): WidgetFactory;
7
- deleteUILayer(widgetFactory: WidgetFactory): void;
8
- isFileSync({ path }: {
9
- path: string;
10
- }): boolean;
11
- readdirSync(option: Platform.readdirSync.Option): Platform.readdirSync.Result;
12
- statSync(option: Platform.statSync.Option): Platform.statSync.Result;
13
- readFileSync(option: Platform.readFileSync.Option): Platform.readFileSync.Result;
14
- writeFileSync(option: Platform.writeFileSync.Option): void;
15
- getImageInfo(img_path: string): {
16
- width: number;
17
- height: number;
18
- };
19
- getTime(): number;
20
- createAudioPlayer(): Platform.AudioPlayer;
21
- releaseAudioPlayer(audio_player: Platform.AudioPlayer): void;
22
- setFrameInterval(callback: () => void): void;
23
- }
24
- export declare namespace Platform {
25
- interface AudioPlayer {
26
- onPrepared?: (success: boolean) => void | undefined;
27
- onCompleted?: () => void | undefined;
28
- prepare(): void;
29
- start(): void;
30
- stop(): void;
31
- release(): void;
32
- getMediaInfo(): MediaInfo;
33
- setSource({ path }: {
34
- path: string;
35
- }): void;
36
- }
37
- interface MediaInfo {
38
- title: string | undefined;
39
- artist: string | undefined;
40
- duration: number;
41
- }
42
- namespace readdirSync {
43
- interface Option {
44
- /**
45
- * @zh 目录路径
46
- * @en Directory path
47
- */
48
- path: string;
49
- }
50
- /**
51
- * @zh 如果返回 `undefined` 则目录不存在,否则返回文件名数组
52
- * @en If `undefined` is returned, the directory does not exist, otherwise an array of filenames is returned
53
- */
54
- type Result = Array<string> | undefined;
55
- }
56
- namespace statSync {
57
- interface Option {
58
- /**
59
- * @zh 路径
60
- * @en path
61
- */
62
- path: string;
63
- }
64
- /**
65
- * @zh 如果返回 `undefined` 则目标文件不存在,否则返回文件信息对象
66
- * @en If `undefined` is returned, the target file does not exist, otherwise the file information object is returned
67
- */
68
- type Result = FSStat | undefined;
69
- /**
70
- * @output
71
- */
72
- interface FSStat {
73
- /**
74
- * @zh 文件大小(单位为字节)
75
- * @en The size of the file in bytes
76
- */
77
- size: number;
78
- }
79
- }
80
- namespace readFileSync {
81
- interface Option {
82
- /**
83
- * @zh 文件路径
84
- * @en path
85
- */
86
- path: string;
87
- /**
88
- * @zh 其他选项
89
- * @en Other Options
90
- */
91
- options?: Options;
92
- }
93
- interface Options {
94
- /**
95
- * @zh 当指定了编码方式之后,API 返回结果为 `string`
96
- * @en When the encoding method is specified, the API returns `string` as the result
97
- */
98
- encoding?: string;
99
- }
100
- /**
101
- * @zh 文件内容。如果返回 `undefined`,则表明读取文件失败
102
- * @en File content. If `undefined` is returned, the file failed to be read
103
- */
104
- type Result = ArrayBuffer | string | undefined;
105
- }
106
- namespace writeFileSync {
107
- interface Option {
108
- /**
109
- * @zh 文件路径或者文件句柄
110
- * @en File path or file descriptor
111
- */
112
- path: string | number;
113
- /**
114
- * @zh 写入目标文件的数据
115
- * @en Data to be written to the target file
116
- */
117
- data: ArrayBuffer | string | DataView;
118
- /**
119
- * @zh 其他选项
120
- * @en Other Options
121
- */
122
- options?: Options;
123
- }
124
- interface Options {
125
- /**
126
- * @zh 如果数据格式为 `string`,需要指定编码方式
127
- * @en If the `data` format is `string`, you need to specify the encoding method
128
- * @defaultValue utf8
129
- */
130
- encoding?: string;
131
- }
132
- }
133
- }
134
- export default Platform;
1
+ export interface Platform<WidgetFactory = any> {
2
+ name: string;
3
+ getScreenSize(): [width: number, height: number];
4
+ createUILayer({ z_index }: {
5
+ z_index: number;
6
+ }): WidgetFactory;
7
+ deleteUILayer(widgetFactory: WidgetFactory): void;
8
+ isFileSync({ path }: {
9
+ path: string;
10
+ }): boolean;
11
+ readdirSync(option: Platform.readdirSync.Option): Platform.readdirSync.Result;
12
+ statSync(option: Platform.statSync.Option): Platform.statSync.Result;
13
+ readFileSync(option: Platform.readFileSync.Option): Platform.readFileSync.Result;
14
+ writeFileSync(option: Platform.writeFileSync.Option): void;
15
+ getImageInfo(img_path: string): {
16
+ width: number;
17
+ height: number;
18
+ } | Promise<{
19
+ width: number;
20
+ height: number;
21
+ }>;
22
+ getTime(): number;
23
+ createAudioPlayer(): Platform.AudioPlayer;
24
+ releaseAudioPlayer(audio_player: Platform.AudioPlayer): void;
25
+ setFrameInterval(callback: () => void): void;
26
+ }
27
+ export declare namespace Platform {
28
+ interface AudioPlayer {
29
+ onPrepared?: (success: boolean) => void | undefined;
30
+ onCompleted?: () => void | undefined;
31
+ prepare(): void;
32
+ start(): void;
33
+ stop(): void;
34
+ release(): void;
35
+ getMediaInfo(): MediaInfo;
36
+ setSource({ path }: {
37
+ path: string;
38
+ }): void;
39
+ }
40
+ interface MediaInfo {
41
+ title: string | undefined;
42
+ artist: string | undefined;
43
+ duration: number;
44
+ }
45
+ namespace readdirSync {
46
+ interface Option {
47
+ /**
48
+ * @zh 目录路径
49
+ * @en Directory path
50
+ */
51
+ path: string;
52
+ }
53
+ /**
54
+ * @zh 如果返回 `undefined` 则目录不存在,否则返回文件名数组
55
+ * @en If `undefined` is returned, the directory does not exist, otherwise an array of filenames is returned
56
+ */
57
+ type Result = Array<string> | undefined;
58
+ }
59
+ namespace statSync {
60
+ interface Option {
61
+ /**
62
+ * @zh 路径
63
+ * @en path
64
+ */
65
+ path: string;
66
+ }
67
+ /**
68
+ * @zh 如果返回 `undefined` 则目标文件不存在,否则返回文件信息对象
69
+ * @en If `undefined` is returned, the target file does not exist, otherwise the file information object is returned
70
+ */
71
+ type Result = FSStat | undefined;
72
+ /**
73
+ * @output
74
+ */
75
+ interface FSStat {
76
+ /**
77
+ * @zh 文件大小(单位为字节)
78
+ * @en The size of the file in bytes
79
+ */
80
+ size: number;
81
+ }
82
+ }
83
+ namespace readFileSync {
84
+ interface Option {
85
+ /**
86
+ * @zh 文件路径
87
+ * @en path
88
+ */
89
+ path: string;
90
+ /**
91
+ * @zh 其他选项
92
+ * @en Other Options
93
+ */
94
+ options?: Options;
95
+ }
96
+ interface Options {
97
+ /**
98
+ * @zh 当指定了编码方式之后,API 返回结果为 `string`
99
+ * @en When the encoding method is specified, the API returns `string` as the result
100
+ */
101
+ encoding?: string;
102
+ }
103
+ /**
104
+ * @zh 文件内容。如果返回 `undefined`,则表明读取文件失败
105
+ * @en File content. If `undefined` is returned, the file failed to be read
106
+ */
107
+ type Result = ArrayBuffer | string | undefined;
108
+ }
109
+ namespace writeFileSync {
110
+ interface Option {
111
+ /**
112
+ * @zh 文件路径或者文件句柄
113
+ * @en File path or file descriptor
114
+ */
115
+ path: string | number;
116
+ /**
117
+ * @zh 写入目标文件的数据
118
+ * @en Data to be written to the target file
119
+ */
120
+ data: ArrayBuffer | string | DataView;
121
+ /**
122
+ * @zh 其他选项
123
+ * @en Other Options
124
+ */
125
+ options?: Options;
126
+ }
127
+ interface Options {
128
+ /**
129
+ * @zh 如果数据格式为 `string`,需要指定编码方式
130
+ * @en If the `data` format is `string`, you need to specify the encoding method
131
+ * @defaultValue utf8
132
+ */
133
+ encoding?: string;
134
+ }
135
+ }
136
+ }
137
+ export default Platform;