@galacean/effects-plugin-alipay-downgrade 1.5.0-alpha.0 → 1.5.0

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/dist/alipay.js ADDED
@@ -0,0 +1,472 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var alipay = require('@galacean/appx-adapter/alipay');
6
+ var effects = require('@galacean/effects/alipay');
7
+
8
+ /******************************************************************************
9
+ Copyright (c) Microsoft Corporation.
10
+
11
+ Permission to use, copy, modify, and/or distribute this software for any
12
+ purpose with or without fee is hereby granted.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
15
+ REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16
+ AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
17
+ INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
18
+ LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
19
+ OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
20
+ PERFORMANCE OF THIS SOFTWARE.
21
+ ***************************************************************************** */
22
+ /* global Reflect, Promise, SuppressedError, Symbol */
23
+
24
+ var extendStatics = function(d, b) {
25
+ extendStatics = Object.setPrototypeOf ||
26
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
27
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
28
+ return extendStatics(d, b);
29
+ };
30
+
31
+ function __extends(d, b) {
32
+ if (typeof b !== "function" && b !== null)
33
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
34
+ extendStatics(d, b);
35
+ function __() { this.constructor = d; }
36
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37
+ }
38
+
39
+ function __awaiter(thisArg, _arguments, P, generator) {
40
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
41
+ return new (P || (P = Promise))(function (resolve, reject) {
42
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
43
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
44
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
45
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
46
+ });
47
+ }
48
+
49
+ function __generator(thisArg, body) {
50
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
51
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
52
+ function verb(n) { return function (v) { return step([n, v]); }; }
53
+ function step(op) {
54
+ if (f) throw new TypeError("Generator is already executing.");
55
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
56
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
57
+ if (y = 0, t) op = [op[0] & 2, t.value];
58
+ switch (op[0]) {
59
+ case 0: case 1: t = op; break;
60
+ case 4: _.label++; return { value: op[1], done: false };
61
+ case 5: _.label++; y = op[1]; op = [0]; continue;
62
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
63
+ default:
64
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
65
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
66
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
67
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
68
+ if (t[2]) _.ops.pop();
69
+ _.trys.pop(); continue;
70
+ }
71
+ op = body.call(thisArg, _);
72
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
73
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
74
+ }
75
+ }
76
+
77
+ function __read(o, n) {
78
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
79
+ if (!m) return o;
80
+ var i = m.call(o), r, ar = [], e;
81
+ try {
82
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
83
+ }
84
+ catch (error) { e = { error: error }; }
85
+ finally {
86
+ try {
87
+ if (r && !r.done && (m = i["return"])) m.call(i);
88
+ }
89
+ finally { if (e) throw e.error; }
90
+ }
91
+ return ar;
92
+ }
93
+
94
+ function __spreadArray(to, from, pack) {
95
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
96
+ if (ar || !(i in from)) {
97
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
98
+ ar[i] = from[i];
99
+ }
100
+ }
101
+ return to.concat(ar || Array.prototype.slice.call(from));
102
+ }
103
+
104
+ typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
105
+ var e = new Error(message);
106
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
107
+ };
108
+
109
+ var DEVICE_PERFORMANCE_LOW = 'low';
110
+ var DEVICE_PERFORMANCE_MIDDLE = 'middle';
111
+ var DEVICE_PERFORMANCE_HIGH = 'high';
112
+ var devicePending;
113
+ var devicePerformance;
114
+ var deviceName = 'DESKTOP_DEBUG';
115
+ var deviceSystem = 'Unknown';
116
+ var isIOS = false;
117
+ function getDeviceName() {
118
+ return __awaiter(this, void 0, void 0, function () {
119
+ return __generator(this, function (_a) {
120
+ if (!devicePending) {
121
+ devicePending = getSystemInfo().then(function (info) {
122
+ var performance = info.performance, platform = info.platform, _a = info.model, model = _a === void 0 ? 'UNKNOWN_DEVICE' : _a, _b = info.system, system = _b === void 0 ? 'Unknown' : _b;
123
+ if (!devicePerformance) {
124
+ devicePerformance = performance;
125
+ }
126
+ isIOS = platform === 'iOS';
127
+ deviceName = model;
128
+ deviceSystem = system;
129
+ if (/iPhone(\d+),/.test(deviceName) && !devicePerformance) {
130
+ var gen = +RegExp.$1;
131
+ // https://gist.github.com/adamawolf/3048717 device code
132
+ if (gen <= 9) { // iphone 7,iphone 7p
133
+ devicePerformance = DEVICE_PERFORMANCE_LOW;
134
+ }
135
+ else if (gen < 10) {
136
+ devicePerformance = DEVICE_PERFORMANCE_MIDDLE;
137
+ }
138
+ else {
139
+ devicePerformance = DEVICE_PERFORMANCE_HIGH;
140
+ }
141
+ }
142
+ if (deviceName.indexOf(info.brand) === 0) {
143
+ deviceName = deviceName.replace(info.brand, '').trim();
144
+ }
145
+ return deviceName;
146
+ }, function (e) {
147
+ });
148
+ }
149
+ return [2 /*return*/, devicePending.then(function () { return deviceName; }, function () { return deviceName; })];
150
+ });
151
+ });
152
+ }
153
+ function getRenderLevelByDevice(renderLevel) {
154
+ if (renderLevel === 'auto' || !renderLevel) {
155
+ if (devicePerformance === DEVICE_PERFORMANCE_HIGH) {
156
+ return effects.spec.RenderLevel.S;
157
+ }
158
+ else if (devicePerformance === DEVICE_PERFORMANCE_MIDDLE) {
159
+ return effects.spec.RenderLevel.A;
160
+ }
161
+ else if (devicePerformance === DEVICE_PERFORMANCE_LOW) {
162
+ return effects.spec.RenderLevel.B;
163
+ }
164
+ return isIOS ? effects.spec.RenderLevel.S : effects.spec.RenderLevel.B;
165
+ }
166
+ return /[ABS]/.test(renderLevel) ? renderLevel : effects.spec.RenderLevel.S;
167
+ }
168
+ function resetDevicePending() {
169
+ devicePending = undefined;
170
+ }
171
+ var mockIdPass = 'mock-pass';
172
+ var mockIdFail = 'mock-fail';
173
+ /**
174
+ *
175
+ * @param bizId
176
+ * @param options
177
+ * @returns
178
+ */
179
+ function checkDowngrade(bizId, options) {
180
+ if (options === void 0) { options = {}; }
181
+ return __awaiter(this, void 0, void 0, function () {
182
+ var ap, now_1;
183
+ return __generator(this, function (_a) {
184
+ if (bizId === mockIdFail || bizId === mockIdPass) {
185
+ return [2 /*return*/, Promise.resolve({ downgrade: bizId === mockIdFail, reason: 'mock' })];
186
+ }
187
+ ap = effects.isAlipayMiniApp() ? my : alipay.window.AlipayJSBridge;
188
+ if (ap) {
189
+ now_1 = performance.now();
190
+ return [2 /*return*/, getDeviceName().then(function () {
191
+ return new Promise(function (resolve) {
192
+ var _a;
193
+ var techPoint = ['mars'];
194
+ var tc = options.techPoint;
195
+ if (tc) {
196
+ techPoint.push.apply(techPoint, __spreadArray([], __read(tc), false));
197
+ }
198
+ var callBridge = (_a = options.callBridge) !== null && _a !== void 0 ? _a : ap.call;
199
+ callBridge('getDowngradeResult', {
200
+ bizId: bizId,
201
+ scene: 0,
202
+ ext: {
203
+ techPoint: techPoint,
204
+ },
205
+ }, function (result) {
206
+ var reason = undefined;
207
+ console.info("downgrade time: ".concat(performance.now() - now_1, "ms"));
208
+ if (!result.error) {
209
+ try {
210
+ var ret = effects.isString(result) ? JSON.parse(result) : result;
211
+ if ('downgradeResultType' in ret) {
212
+ reason = ret.downgradeResultType;
213
+ }
214
+ else if ('resultType' in ret) {
215
+ reason = ret.resultType;
216
+ }
217
+ if (result.context) {
218
+ var deviceInfo = result.context.deviceInfo;
219
+ if (deviceInfo) {
220
+ var level = deviceInfo.deviceLevel;
221
+ if (level === DEVICE_PERFORMANCE_HIGH || level === DEVICE_PERFORMANCE_LOW) {
222
+ devicePerformance = level;
223
+ }
224
+ else if (level === 'medium') {
225
+ devicePerformance = DEVICE_PERFORMANCE_MIDDLE;
226
+ }
227
+ }
228
+ }
229
+ }
230
+ catch (ex) {
231
+ console.error(ex);
232
+ }
233
+ }
234
+ else {
235
+ // 无权调用的情况下不降级
236
+ resolve({ downgrade: result.error !== 4, reason: 'api error:' + result.error });
237
+ }
238
+ if (reason === undefined) {
239
+ resolve({ downgrade: true, reason: 'call downgrade fail' });
240
+ }
241
+ else {
242
+ if (effects.isAlipayMiniApp() && downgradeForMiniprogram()) {
243
+ resolve({ downgrade: true, reason: 'Force downgrade by downgrade plugin' });
244
+ }
245
+ else {
246
+ resolve({ downgrade: reason === 1, reason: reason });
247
+ }
248
+ }
249
+ });
250
+ });
251
+ })];
252
+ }
253
+ return [2 /*return*/, Promise.resolve({ downgrade: false, reason: 'no AP env' })];
254
+ });
255
+ });
256
+ }
257
+ function getSystemInfo() {
258
+ return __awaiter(this, void 0, void 0, function () {
259
+ return __generator(this, function (_a) {
260
+ return [2 /*return*/, new Promise(function (resolve, reject) {
261
+ //@ts-expect-error
262
+ var ap = effects.isAlipayMiniApp() ? my : alipay.window.AlipayJSBridge;
263
+ if (ap) {
264
+ ap.call('getSystemInfo', function (e) {
265
+ if (e.error) {
266
+ reject(e);
267
+ }
268
+ else {
269
+ resolve(e);
270
+ }
271
+ });
272
+ }
273
+ else {
274
+ reject('no ap');
275
+ }
276
+ })];
277
+ });
278
+ });
279
+ }
280
+ var deviceNameList = ['12,8', '13,1', '13,2', '13,3', '13,4'];
281
+ /**
282
+ * iPhone SE2 和 12 全系列机型,如果是 iOS16 系统,在小程序中强制降级
283
+ * @returns
284
+ */
285
+ function downgradeForMiniprogram() {
286
+ if (isIOS) {
287
+ if (deviceNameList.find(function (v) { return v === deviceName; })) {
288
+ var versionList = deviceSystem.split('.');
289
+ if (versionList.length > 0 && versionList[0] === '16') {
290
+ return true;
291
+ }
292
+ }
293
+ }
294
+ return false;
295
+ }
296
+
297
+ var AlipayDowngradePlugin = /** @class */ (function (_super) {
298
+ __extends(AlipayDowngradePlugin, _super);
299
+ function AlipayDowngradePlugin() {
300
+ return _super !== null && _super.apply(this, arguments) || this;
301
+ }
302
+ AlipayDowngradePlugin.onPlayerCreated = function (player) {
303
+ return __awaiter(this, void 0, void 0, function () {
304
+ var result;
305
+ return __generator(this, function (_a) {
306
+ switch (_a.label) {
307
+ case 0:
308
+ if (AlipayDowngradePlugin.glLostOccurred) {
309
+ console.warn('gl lost happened, new player will be destroyed.');
310
+ return [2 /*return*/, player.dispose()];
311
+ }
312
+ if (!AlipayDowngradePlugin.currentBizId) return [3 /*break*/, 2];
313
+ return [4 /*yield*/, checkDowngrade(AlipayDowngradePlugin.currentBizId)];
314
+ case 1:
315
+ result = _a.sent();
316
+ if (result.downgrade) {
317
+ console.warn('automatically destroy downgraded player.');
318
+ player.dispose();
319
+ }
320
+ _a.label = 2;
321
+ case 2: return [2 /*return*/];
322
+ }
323
+ });
324
+ });
325
+ };
326
+ AlipayDowngradePlugin.processRawJSON = function (json, options) {
327
+ var _a, _b;
328
+ if (options === void 0) { options = {}; }
329
+ return __awaiter(this, void 0, void 0, function () {
330
+ var result;
331
+ return __generator(this, function (_c) {
332
+ switch (_c.label) {
333
+ case 0:
334
+ if (AlipayDowngradePlugin.glLostOccurred) {
335
+ return [2 /*return*/, Promise.reject('gl lost happened')];
336
+ }
337
+ return [4 /*yield*/, checkDowngrade((_b = (_a = options.pluginData) === null || _a === void 0 ? void 0 : _a['alipayBizId']) !== null && _b !== void 0 ? _b : AlipayDowngradePlugin.currentBizId)];
338
+ case 1:
339
+ result = _c.sent();
340
+ if (result.downgrade) {
341
+ throw new Error("downgraded, reason: ".concat(result.reason));
342
+ }
343
+ if (!options.renderLevel) {
344
+ options.renderLevel = getRenderLevelByDevice(options.renderLevel);
345
+ }
346
+ return [2 /*return*/];
347
+ }
348
+ });
349
+ });
350
+ };
351
+ AlipayDowngradePlugin.currentBizId = '';
352
+ AlipayDowngradePlugin.glLostOccurred = false;
353
+ return AlipayDowngradePlugin;
354
+ }(effects.AbstractPlugin));
355
+
356
+ var DowngradeVFXItem = /** @class */ (function (_super) {
357
+ __extends(DowngradeVFXItem, _super);
358
+ function DowngradeVFXItem() {
359
+ return _super !== null && _super.apply(this, arguments) || this;
360
+ }
361
+ return DowngradeVFXItem;
362
+ }(effects.VFXItem));
363
+
364
+ var prefix = '[Galacean Effects]';
365
+ //@ts-expect-error
366
+ var ap = effects.isAlipayMiniApp() ? my : alipay.window.AlipayJSBridge;
367
+ effects.logger.register(nativeLogger);
368
+ function nativeLogger(type, msg) {
369
+ var args = [];
370
+ for (var _i = 2; _i < arguments.length; _i++) {
371
+ args[_i - 2] = arguments[_i];
372
+ }
373
+ var content = {
374
+ 'message': "".concat(prefix, " ").concat(msg, " ").concat(args.join('')),
375
+ 'level': type,
376
+ };
377
+ if (effects.isAndroid()) {
378
+ content['anr_info'] = 'mars';
379
+ }
380
+ try {
381
+ ap === null || ap === void 0 ? void 0 : ap.call('localLog', content);
382
+ }
383
+ catch (e) {
384
+ console.error(e);
385
+ }
386
+ }
387
+
388
+ var registered = false;
389
+ /**
390
+ * 支付宝端内统一降级方案,传入降级 ID,每次加载场景前都会自动调用降级
391
+ * - 如果要为某个 json 单独配置降级 ID,可以使用 `player.loadScene(url, { pluginData: { alipayBizId: '#bizId#' }})`
392
+ * @param bizId - 降级 ID,如果为 `mock-pass` 将 mock 不降级场景,如果为 `mock-fail` 将 mock 降级场景,业务请不要使用这两个字符串
393
+ * @param options - 优化策略
394
+ */
395
+ function setAlipayDowngradeBizId(bizId, options) {
396
+ if (options === void 0) { options = {}; }
397
+ var ignoreGLLost = options.ignoreGLLost, autoPause = options.autoPause;
398
+ var downgradeWhenGLLost = ignoreGLLost !== true;
399
+ AlipayDowngradePlugin.currentBizId = bizId;
400
+ if (registered) {
401
+ return;
402
+ }
403
+ effects.registerPlugin('alipay-downgrade', AlipayDowngradePlugin, DowngradeVFXItem, true);
404
+ alipay.window.addEventListener('unload', function () {
405
+ effects.getActivePlayers().forEach(function (player) { return player.dispose(); });
406
+ });
407
+ alipay.window.addEventListener('webglcontextlost', function (e) {
408
+ if (effects.isCanvasUsedByPlayer(e.target)) {
409
+ AlipayDowngradePlugin.glLostOccurred = true;
410
+ console.error('webgl lost occur');
411
+ if (downgradeWhenGLLost) {
412
+ console.warn('webgl lost occur, all players will be downgraded from now on');
413
+ effects.disableAllPlayer(true);
414
+ effects.getActivePlayers().forEach(function (player) { return player.dispose(); });
415
+ }
416
+ }
417
+ }, true);
418
+ if (autoPause) {
419
+ document.addEventListener('pause', pauseAllActivePlayers);
420
+ document.addEventListener('resume', resumePausedPlayers);
421
+ }
422
+ registered = true;
423
+ void getDeviceName();
424
+ }
425
+ /**
426
+ *
427
+ * @returns
428
+ */
429
+ function getAlipayDowngradeBizId() {
430
+ return AlipayDowngradePlugin.currentBizId;
431
+ }
432
+ var internalPaused = Symbol('@@_inter_pause');
433
+ function pauseAllActivePlayers(e) {
434
+ if (e.target === document) {
435
+ effects.logger.info('Auto pause all players with data offloaded');
436
+ var players = effects.getActivePlayers();
437
+ players.forEach(function (player) {
438
+ if (!player.paused) {
439
+ player.pause({ offloadTexture: true });
440
+ // @ts-expect-error
441
+ player[internalPaused] = true;
442
+ }
443
+ });
444
+ }
445
+ }
446
+ function resumePausedPlayers(e) {
447
+ if (e.target === document) {
448
+ effects.logger.info('auto resume all players');
449
+ var players = effects.getActivePlayers();
450
+ players.forEach(function (player) {
451
+ // @ts-expect-error
452
+ if (player[internalPaused]) {
453
+ void player.resume();
454
+ // @ts-expect-error
455
+ player[internalPaused] = false;
456
+ }
457
+ });
458
+ }
459
+ }
460
+ var version = "1.5.0";
461
+ effects.logger.info('plugin alipay downgrade version: ' + version);
462
+
463
+ exports.checkDowngrade = checkDowngrade;
464
+ exports.downgradeForMiniprogram = downgradeForMiniprogram;
465
+ exports.getAlipayDowngradeBizId = getAlipayDowngradeBizId;
466
+ exports.getDeviceName = getDeviceName;
467
+ exports.getRenderLevelByDevice = getRenderLevelByDevice;
468
+ exports.getSystemInfo = getSystemInfo;
469
+ exports.resetDevicePending = resetDevicePending;
470
+ exports.setAlipayDowngradeBizId = setAlipayDowngradeBizId;
471
+ exports.version = version;
472
+ //# sourceMappingURL=alipay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"alipay.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}