@galacean/effects-plugin-alipay-downgrade 2.0.0-alpha.3 → 2.0.0-alpha.31

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/index.js CHANGED
@@ -2,309 +2,266 @@
2
2
  * Name: @galacean/effects-plugin-alipay-downgrade
3
3
  * Description: Galacean Effects player downgrade plugin for Alipay
4
4
  * Author: Ant Group CO., Ltd.
5
- * Contributors: 燃然,意绮
6
- * Version: v2.0.0-alpha.3
5
+ * Contributors: 燃然,意绮,飂兮
6
+ * Version: v2.0.0-alpha.31
7
7
  */
8
8
 
9
9
  'use strict';
10
10
 
11
11
  Object.defineProperty(exports, '__esModule', { value: true });
12
12
 
13
- var effects = require('@galacean/effects');
13
+ var EFFECTS = require('@galacean/effects');
14
14
 
15
- /******************************************************************************
16
- Copyright (c) Microsoft Corporation.
17
-
18
- Permission to use, copy, modify, and/or distribute this software for any
19
- purpose with or without fee is hereby granted.
20
-
21
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
22
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
23
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
24
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
25
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
26
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
27
- PERFORMANCE OF THIS SOFTWARE.
28
- ***************************************************************************** */
29
- /* global Reflect, Promise, SuppressedError, Symbol */
30
-
31
- var extendStatics = function(d, b) {
32
- extendStatics = Object.setPrototypeOf ||
33
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
34
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
35
- return extendStatics(d, b);
36
- };
37
-
38
- function __extends(d, b) {
39
- if (typeof b !== "function" && b !== null)
40
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
41
- extendStatics(d, b);
42
- function __() { this.constructor = d; }
43
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
44
- }
45
-
46
- function __awaiter(thisArg, _arguments, P, generator) {
47
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
- return new (P || (P = Promise))(function (resolve, reject) {
49
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
50
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
51
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
52
- step((generator = generator.apply(thisArg, _arguments || [])).next());
53
- });
54
- }
55
-
56
- function __generator(thisArg, body) {
57
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
58
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
59
- function verb(n) { return function (v) { return step([n, v]); }; }
60
- function step(op) {
61
- if (f) throw new TypeError("Generator is already executing.");
62
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
63
- 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;
64
- if (y = 0, t) op = [op[0] & 2, t.value];
65
- switch (op[0]) {
66
- case 0: case 1: t = op; break;
67
- case 4: _.label++; return { value: op[1], done: false };
68
- case 5: _.label++; y = op[1]; op = [0]; continue;
69
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
70
- default:
71
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
72
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
73
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
74
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
75
- if (t[2]) _.ops.pop();
76
- _.trys.pop(); continue;
77
- }
78
- op = body.call(thisArg, _);
79
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
80
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
81
- }
82
- }
83
-
84
- function __read(o, n) {
85
- var m = typeof Symbol === "function" && o[Symbol.iterator];
86
- if (!m) return o;
87
- var i = m.call(o), r, ar = [], e;
88
- try {
89
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
90
- }
91
- catch (error) { e = { error: error }; }
92
- finally {
93
- try {
94
- if (r && !r.done && (m = i["return"])) m.call(i);
95
- }
96
- finally { if (e) throw e.error; }
97
- }
98
- return ar;
99
- }
100
-
101
- function __spreadArray(to, from, pack) {
102
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
103
- if (ar || !(i in from)) {
104
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
105
- ar[i] = from[i];
106
- }
107
- }
108
- return to.concat(ar || Array.prototype.slice.call(from));
109
- }
110
-
111
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
112
- var e = new Error(message);
113
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
114
- };
115
-
116
- var DowngradeVFXItem = /** @class */ (function (_super) {
117
- __extends(DowngradeVFXItem, _super);
118
- function DowngradeVFXItem() {
119
- return _super !== null && _super.apply(this, arguments) || this;
15
+ function _interopNamespace(e) {
16
+ if (e && e.__esModule) return e;
17
+ var n = Object.create(null);
18
+ if (e) {
19
+ Object.keys(e).forEach(function (k) {
20
+ if (k !== 'default') {
21
+ var d = Object.getOwnPropertyDescriptor(e, k);
22
+ Object.defineProperty(n, k, d.get ? d : {
23
+ enumerable: true,
24
+ get: function () { return e[k]; }
25
+ });
26
+ }
27
+ });
120
28
  }
121
- return DowngradeVFXItem;
122
- }(effects.VFXItem));
29
+ n["default"] = e;
30
+ return Object.freeze(n);
31
+ }
123
32
 
124
- var mockIdPass = 'mock-pass';
125
- var mockIdFail = 'mock-fail';
126
- var DEVICE_LEVEL_HIGH = 'high';
127
- var DEVICE_LEVEL_MEDIUM = 'medium';
128
- var DEVICE_LEVEL_LOW = 'low';
129
- var DEVICE_LEVEL_NONE = 'none';
130
- var hasRegisterEvent = false;
131
- function getDowngradeResult(bizId, options) {
132
- if (options === void 0) { options = {}; }
133
- return __awaiter(this, void 0, void 0, function () {
134
- var ap, now_1;
135
- return __generator(this, function (_a) {
136
- if (!hasRegisterEvent) {
137
- registerEvent(options);
138
- hasRegisterEvent = true;
139
- }
140
- if (bizId === mockIdFail || bizId === mockIdPass) {
141
- return [2 /*return*/, Promise.resolve({ mock: { downgrade: bizId === mockIdFail } })];
142
- }
143
- ap = window.AlipayJSBridge;
144
- if (ap) {
145
- now_1 = performance.now();
146
- return [2 /*return*/, getSystemInfo().then(function (systemInfo) {
147
- return new Promise(function (resolve) {
148
- var _a;
149
- var techPoint = ['mars'];
150
- var tc = options.techPoint;
151
- if (tc) {
152
- techPoint.push.apply(techPoint, __spreadArray([], __read(tc), false));
153
- }
154
- var downgradeOptions = {
155
- bizId: bizId,
156
- scene: 0,
157
- ext: {
158
- techPoint: techPoint,
159
- },
160
- };
161
- var downgradeCallback = function (result) {
162
- var totalTime = performance.now() - now_1;
163
- console.info("downgrade time: ".concat(totalTime, "ms"));
164
- resolve({ bizId: bizId, systemInfo: systemInfo, downgradeResult: result, totalTime: totalTime });
165
- };
166
- var callBridge = (_a = options.callBridge) !== null && _a !== void 0 ? _a : ap.call;
167
- callBridge('getDowngradeResult', downgradeOptions, downgradeCallback);
168
- });
169
- })];
33
+ var EFFECTS__namespace = /*#__PURE__*/_interopNamespace(EFFECTS);
34
+
35
+ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
36
+ try {
37
+ var info = gen[key](arg);
38
+ var value = info.value;
39
+ } catch (error) {
40
+ reject(error);
41
+ return;
42
+ }
43
+ if (info.done) resolve(value);
44
+ else Promise.resolve(value).then(_next, _throw);
45
+ }
46
+ function _async_to_generator(fn) {
47
+ return function() {
48
+ var self = this, args = arguments;
49
+ return new Promise(function(resolve, reject) {
50
+ var gen = fn.apply(self, args);
51
+ function _next(value) {
52
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
170
53
  }
171
- else {
172
- return [2 /*return*/, Promise.resolve({ noAlipayEnv: true })];
54
+ function _throw(err) {
55
+ asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
173
56
  }
57
+ _next(undefined);
174
58
  });
175
- });
59
+ };
176
60
  }
177
- function registerEvent(options) {
178
- var ignoreGLLost = options.ignoreGLLost, autoPause = options.autoPause;
179
- var downgradeWhenGLLost = ignoreGLLost !== true;
180
- window.addEventListener('unload', function () {
181
- effects.getActivePlayers().forEach(function (player) { return player.dispose(); });
61
+
62
+ function _set_prototype_of(o, p) {
63
+ _set_prototype_of = Object.setPrototypeOf || function setPrototypeOf(o, p) {
64
+ o.__proto__ = p;
65
+ return o;
66
+ };
67
+ return _set_prototype_of(o, p);
68
+ }
69
+
70
+ function _inherits(subClass, superClass) {
71
+ if (typeof superClass !== "function" && superClass !== null) {
72
+ throw new TypeError("Super expression must either be null or a function");
73
+ }
74
+ subClass.prototype = Object.create(superClass && superClass.prototype, {
75
+ constructor: {
76
+ value: subClass,
77
+ writable: true,
78
+ configurable: true
79
+ }
182
80
  });
183
- window.addEventListener('webglcontextlost', function (e) {
184
- if (effects.isCanvasUsedByPlayer(e.target)) {
185
- AlipayDowngradePlugin.glLostOccurred = true;
186
- console.error('webgl lost occur');
187
- if (downgradeWhenGLLost) {
188
- console.warn('webgl lost occur, all players will be downgraded from now on');
189
- effects.disableAllPlayer(true);
190
- effects.getActivePlayers().forEach(function (player) { return player.dispose(); });
81
+ if (superClass) _set_prototype_of(subClass, superClass);
82
+ }
83
+
84
+ function __generator(thisArg, body) {
85
+ var _ = {
86
+ label: 0,
87
+ sent: function sent() {
88
+ if (t[0] & 1) throw t[1];
89
+ return t[1];
90
+ },
91
+ trys: [],
92
+ ops: []
93
+ }, f, y, t, g;
94
+ return g = {
95
+ next: verb(0),
96
+ "throw": verb(1),
97
+ "return": verb(2)
98
+ }, typeof Symbol === "function" && (g[Symbol.iterator] = function() {
99
+ return this;
100
+ }), g;
101
+ function verb(n) {
102
+ return function(v) {
103
+ return step([
104
+ n,
105
+ v
106
+ ]);
107
+ };
108
+ }
109
+ function step(op) {
110
+ if (f) throw new TypeError("Generator is already executing.");
111
+ while(g && (g = 0, op[0] && (_ = 0)), _)try {
112
+ 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;
113
+ if (y = 0, t) op = [
114
+ op[0] & 2,
115
+ t.value
116
+ ];
117
+ switch(op[0]){
118
+ case 0:
119
+ case 1:
120
+ t = op;
121
+ break;
122
+ case 4:
123
+ _.label++;
124
+ return {
125
+ value: op[1],
126
+ done: false
127
+ };
128
+ case 5:
129
+ _.label++;
130
+ y = op[1];
131
+ op = [
132
+ 0
133
+ ];
134
+ continue;
135
+ case 7:
136
+ op = _.ops.pop();
137
+ _.trys.pop();
138
+ continue;
139
+ default:
140
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
141
+ _ = 0;
142
+ continue;
143
+ }
144
+ if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
145
+ _.label = op[1];
146
+ break;
147
+ }
148
+ if (op[0] === 6 && _.label < t[1]) {
149
+ _.label = t[1];
150
+ t = op;
151
+ break;
152
+ }
153
+ if (t && _.label < t[2]) {
154
+ _.label = t[2];
155
+ _.ops.push(op);
156
+ break;
157
+ }
158
+ if (t[2]) _.ops.pop();
159
+ _.trys.pop();
160
+ continue;
191
161
  }
162
+ op = body.call(thisArg, _);
163
+ } catch (e) {
164
+ op = [
165
+ 6,
166
+ e
167
+ ];
168
+ y = 0;
169
+ } finally{
170
+ f = t = 0;
192
171
  }
193
- }, true);
194
- if (autoPause) {
195
- document.addEventListener('pause', pauseAllActivePlayers);
196
- document.addEventListener('resume', resumePausedPlayers);
172
+ if (op[0] & 5) throw op[1];
173
+ return {
174
+ value: op[0] ? op[1] : void 0,
175
+ done: true
176
+ };
197
177
  }
198
178
  }
199
- function getSystemInfo() {
200
- return __awaiter(this, void 0, void 0, function () {
201
- return __generator(this, function (_a) {
202
- return [2 /*return*/, new Promise(function (resolve, reject) {
203
- var ap = window.AlipayJSBridge;
204
- if (ap) {
205
- ap.call('getSystemInfo', function (e) {
206
- if (e.error) {
207
- reject(e);
208
- }
209
- else {
210
- resolve(e);
211
- }
212
- });
213
- }
214
- else {
215
- reject('no ap');
216
- }
217
- })];
218
- });
219
- });
179
+ typeof SuppressedError === "function" ? SuppressedError : function _SuppressedError(error, suppressed, message) {
180
+ var e = new Error(message);
181
+ return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
182
+ };
183
+
184
+ function _extends() {
185
+ _extends = Object.assign || function assign(target) {
186
+ for(var i = 1; i < arguments.length; i++){
187
+ var source = arguments[i];
188
+ for(var key in source)if (Object.prototype.hasOwnProperty.call(source, key)) target[key] = source[key];
189
+ }
190
+ return target;
191
+ };
192
+ return _extends.apply(this, arguments);
220
193
  }
221
- var DeviceProxy = /** @class */ (function () {
194
+
195
+ exports.DeviceLevel = void 0;
196
+ (function(DeviceLevel) {
197
+ DeviceLevel["High"] = "high";
198
+ DeviceLevel["Medium"] = "medium";
199
+ DeviceLevel["Low"] = "low";
200
+ DeviceLevel["Unknown"] = "unknown";
201
+ })(exports.DeviceLevel || (exports.DeviceLevel = {}));
202
+
203
+ var deviceModelList = [
204
+ "12,8",
205
+ "13,1",
206
+ "13,2",
207
+ "13,3",
208
+ "13,4"
209
+ ];
210
+ /**
211
+ * 设备代理类
212
+ *
213
+ * 负责将 JSAPI 返回的 SystemInfo 和 DowngradeResult 数据
214
+ * 转成设备相关的数据,在后面的降级判断中使用
215
+ */ var DeviceProxy = /*#__PURE__*/ function() {
222
216
  function DeviceProxy() {
223
- this.isIOS = false;
224
- this.model = 'DESKTOP_DEBUG';
225
- this.deviceLevel = DEVICE_LEVEL_NONE;
226
- this.isDowngrade = false;
217
+ /**
218
+ * 是否 iOS 系统
219
+ */ this.isIOS = false;
220
+ /**
221
+ * 硬件机型
222
+ */ this.model = "DESKTOP_DEBUG";
223
+ /**
224
+ * 系统版本
225
+ */ this.system = "Unknown";
226
+ /**
227
+ * 设备等级
228
+ */ this.level = exports.DeviceLevel.Unknown;
229
+ /**
230
+ * 是否降级
231
+ */ this.isDowngrade = false;
227
232
  }
228
- DeviceProxy.prototype.getRenderLevel = function () {
229
- if (this.deviceLevel === DEVICE_LEVEL_HIGH) {
230
- return effects.spec.RenderLevel.S;
231
- }
232
- else if (this.deviceLevel === DEVICE_LEVEL_MEDIUM) {
233
- return effects.spec.RenderLevel.A;
234
- }
235
- else if (this.deviceLevel === DEVICE_LEVEL_LOW) {
236
- return effects.spec.RenderLevel.B;
237
- }
238
- else {
239
- return this.isIOS ? effects.spec.RenderLevel.S : effects.spec.RenderLevel.B;
240
- }
241
- };
242
- DeviceProxy.prototype.hasDeviceLevel = function () {
243
- return isDeviceLevel(this.deviceLevel);
244
- };
245
- DeviceProxy.prototype.setBySystemInfo = function (systemInfo) {
246
- var performance = systemInfo.performance, platform = systemInfo.platform, _a = systemInfo.model, model = _a === void 0 ? 'UNKNOWN_DEVICE' : _a;
247
- this.isIOS = platform === 'iOS';
233
+ var _proto = DeviceProxy.prototype;
234
+ /**
235
+ * 设置 JSAPI 返回的系统信息
236
+ * @param systemInfo - JSAPI 返回的系统信息
237
+ */ _proto.setSystemInfo = function setSystemInfo(systemInfo) {
238
+ var performance = systemInfo.performance, platform = systemInfo.platform, _systemInfo_model = systemInfo.model, model = _systemInfo_model === void 0 ? "UNKNOWN_DEVICE" : _systemInfo_model, _systemInfo_system = systemInfo.system, system = _systemInfo_system === void 0 ? "Unknown" : _systemInfo_system;
239
+ this.isIOS = platform === "iOS";
248
240
  this.model = model;
249
- if (performance && !this.hasDeviceLevel()) {
250
- this.deviceLevel = performance;
251
- }
241
+ this.system = system;
242
+ this.setLevel(performance);
252
243
  };
253
- DeviceProxy.prototype.updateDeviceLevel = function () {
254
- if (this.hasDeviceLevel()) {
255
- return;
256
- }
257
- if (/iPhone(\d+),/.test(this.model)) {
258
- var gen = +RegExp.$1;
259
- if (gen <= 9) {
260
- this.deviceLevel = DEVICE_LEVEL_LOW;
261
- }
262
- else if (gen < 10) {
263
- this.deviceLevel = DEVICE_LEVEL_MEDIUM;
264
- }
265
- else {
266
- this.deviceLevel = DEVICE_LEVEL_HIGH;
267
- }
244
+ /**
245
+ * 根据传入的 JSAPI 降级结果,返回设备的降级决定
246
+ *
247
+ * @param result - JSAPI 返回的降级结果
248
+ * @returns 设备降级决定
249
+ */ _proto.getDowngradeDecision = function getDowngradeDecision(result) {
250
+ var resultType = undefined;
251
+ var resultReason = undefined;
252
+ if (result.error) {
253
+ // 无权调用的情况下不降级
254
+ return {
255
+ downgrade: result.error !== 4,
256
+ level: this.getRenderLevel(),
257
+ reason: "api error: " + result.error
258
+ };
268
259
  }
269
- };
270
- DeviceProxy.prototype.getDeviceInfo = function () {
271
- return {
272
- isIOS: this.isIOS,
273
- model: this.model,
274
- deviceLevel: this.deviceLevel,
275
- };
276
- };
277
- return DeviceProxy;
278
- }());
279
- var device = new DeviceProxy();
280
- function checkDowngradeResult(result) {
281
- if (result.mock) {
282
- var _a = result.mock, downgrade = _a.downgrade, _b = _a.deviceLevel, deviceLevel = _b === void 0 ? DEVICE_LEVEL_HIGH : _b;
283
- device.deviceLevel = deviceLevel;
284
- return { downgrade: downgrade, reason: 'mock' };
285
- }
286
- if (result.noAlipayEnv) {
287
- return { downgrade: false, reason: 'no AP env' };
288
- }
289
- if (!result.systemInfo) {
290
- return { downgrade: false, reason: 'systemInfo is required' };
291
- }
292
- if (!result.downgradeResult) {
293
- return { downgrade: false, reason: 'downgradeResult is required' };
294
- }
295
- device.setBySystemInfo(result.systemInfo);
296
- return parseDowngradeResult(result.downgradeResult);
297
- }
298
- function parseDowngradeResult(result) {
299
- var resultType = undefined;
300
- var resultReason = undefined;
301
- if (!result.error) {
302
260
  try {
303
- var ret = effects.isString(result) ? JSON.parse(result) : result;
304
- if ('downgradeResultType' in ret) {
261
+ var ret = EFFECTS.isString(result) ? JSON.parse(result) : result;
262
+ if ("downgradeResultType" in ret) {
305
263
  resultType = ret.downgradeResultType;
306
- }
307
- else if ('resultType' in ret) {
264
+ } else if ("resultType" in ret) {
308
265
  resultType = ret.resultType;
309
266
  resultReason = ret.resultReason;
310
267
  }
@@ -312,77 +269,296 @@ function parseDowngradeResult(result) {
312
269
  var deviceInfo = result.context.deviceInfo;
313
270
  if (deviceInfo) {
314
271
  var deviceLevel = deviceInfo.deviceLevel;
315
- if (isDeviceLevel(deviceLevel)) {
316
- device.deviceLevel = deviceLevel;
272
+ var newLevel = getDeviceLevel(deviceLevel);
273
+ if (newLevel !== exports.DeviceLevel.Unknown) {
274
+ this.level = newLevel;
317
275
  }
318
276
  }
319
277
  }
320
- device.updateDeviceLevel();
321
- }
322
- catch (ex) {
323
- console.error(ex);
278
+ } catch (ex) {
279
+ EFFECTS.logger.error(ex);
324
280
  }
325
281
  if (resultType === undefined) {
326
- return { downgrade: true, reason: 'call downgrade fail' };
282
+ return {
283
+ downgrade: true,
284
+ level: this.getRenderLevel(),
285
+ reason: "call downgrade fail"
286
+ };
287
+ }
288
+ if (resultType === 1) {
289
+ return {
290
+ downgrade: true,
291
+ level: this.getRenderLevel(),
292
+ reason: getDowngradeReason(resultReason)
293
+ };
294
+ }
295
+ if (EFFECTS.isAlipayMiniApp() && this.downgradeForMiniprogram()) {
296
+ return {
297
+ downgrade: true,
298
+ level: this.getRenderLevel(),
299
+ reason: "Force downgrade by downgrade plugin"
300
+ };
301
+ }
302
+ return {
303
+ downgrade: false,
304
+ level: this.getRenderLevel(),
305
+ reason: "" + resultType
306
+ };
307
+ };
308
+ /**
309
+ * 获取设备渲染等级
310
+ * @returns 设备渲染等级
311
+ */ _proto.getRenderLevel = function getRenderLevel() {
312
+ if (this.level === exports.DeviceLevel.High) {
313
+ return EFFECTS.spec.RenderLevel.S;
314
+ } else if (this.level === exports.DeviceLevel.Medium) {
315
+ return EFFECTS.spec.RenderLevel.A;
316
+ } else if (this.level === exports.DeviceLevel.Low) {
317
+ return EFFECTS.spec.RenderLevel.B;
318
+ } else {
319
+ return this.isIOS ? EFFECTS.spec.RenderLevel.S : EFFECTS.spec.RenderLevel.B;
327
320
  }
328
- else {
329
- if (resultType === 1) {
330
- return { downgrade: true, reason: getDowngradeReason(resultReason) };
321
+ };
322
+ _proto.setLevel = function setLevel(level) {
323
+ this.level = getDeviceLevel(level);
324
+ if (this.level === exports.DeviceLevel.Unknown) {
325
+ if (/iPhone(\d+),/.test(this.model)) {
326
+ var gen = +RegExp.$1;
327
+ if (gen <= 9) {
328
+ this.level = exports.DeviceLevel.Low;
329
+ } else if (gen < 10) {
330
+ this.level = exports.DeviceLevel.Medium;
331
+ } else {
332
+ this.level = exports.DeviceLevel.High;
333
+ }
331
334
  }
332
- else {
333
- return { downgrade: false, reason: resultType };
335
+ }
336
+ };
337
+ _proto.downgradeForMiniprogram = function downgradeForMiniprogram() {
338
+ var _this = this;
339
+ if (this.isIOS) {
340
+ if (deviceModelList.find(function(v) {
341
+ return v === _this.model;
342
+ })) {
343
+ var versionList = this.system.split(".");
344
+ if (versionList.length > 0 && versionList[0] === "16") {
345
+ return true;
346
+ }
334
347
  }
335
348
  }
336
- }
337
- else {
338
- // 无权调用的情况下不降级
339
- return { downgrade: result.error !== 4, reason: 'api error: ' + result.error };
340
- }
341
- }
349
+ return false;
350
+ };
351
+ return DeviceProxy;
352
+ }();
342
353
  function getDowngradeReason(reason) {
343
354
  if (reason === -1) {
344
- return "".concat(reason, "-no config");
345
- }
346
- else if (reason === 0) {
347
- return "".concat(reason, "-none");
348
- }
349
- else if (reason === 1) {
350
- return "".concat(reason, "-downgrade by memory");
351
- }
352
- else if (reason === 2) {
353
- return "".concat(reason, "-downgrade by crash");
354
- }
355
- else if (reason === 3) {
356
- return "".concat(reason, "-downgrade by device");
357
- }
358
- else if (reason === 4) {
359
- return "".concat(reason, "-downgrade by force");
360
- }
361
- else {
362
- return "".concat(reason);
355
+ return "" + reason + ", unable to pull configuration";
356
+ } else if (reason === 0) {
357
+ return "" + reason + ", no downgrade";
358
+ } else if (reason === 1) {
359
+ return "" + reason + ", memory downgrade";
360
+ } else if (reason === 2) {
361
+ return "" + reason + ", crash downgrade";
362
+ } else if (reason === 3) {
363
+ return "" + reason + ", basic dimension downgrade";
364
+ } else if (reason === 4) {
365
+ return "" + reason + ", technical point downgrade";
366
+ } else if (reason === 5) {
367
+ return "" + reason + ", GPU downgrade";
368
+ } else if (reason === 6) {
369
+ return "" + reason + ", self-healing downgrade";
370
+ } else if (reason === 7) {
371
+ return "" + reason + ", 32-bit CPU downgrade";
372
+ } else {
373
+ return "" + reason;
363
374
  }
364
375
  }
365
- function getRenderLevelByDevice(renderLevel) {
366
- if (!renderLevel) {
367
- return device.getRenderLevel();
376
+ function getDeviceLevel(level) {
377
+ if (level === "high") {
378
+ return exports.DeviceLevel.High;
379
+ } else if (level === "medium" || level === "middle") {
380
+ return exports.DeviceLevel.Medium;
381
+ } else if (level === "low") {
382
+ return exports.DeviceLevel.Low;
383
+ } else {
384
+ return exports.DeviceLevel.Unknown;
368
385
  }
369
- else {
370
- return /[ABS]/.test(renderLevel) ? renderLevel : effects.spec.RenderLevel.S;
386
+ }
387
+
388
+ var internalPaused = Symbol("@@_inter_pause");
389
+ var mockIdPass = "mock-pass";
390
+ var mockIdFail = "mock-fail";
391
+ var hasRegisterEvent = false;
392
+ /**
393
+ * 获取 GE 降级结果,在有 JSAPI 环境下调用,不需要创建 Canvas 和 WebGL 环境。
394
+ *
395
+ * @param bizId - 业务 bizId
396
+ * @param options - 降级选项
397
+ * @returns 降级结果
398
+ */ function getDowngradeResult(bizId) {
399
+ return _getDowngradeResult.apply(this, arguments);
400
+ }
401
+ function _getDowngradeResult() {
402
+ _getDowngradeResult = _async_to_generator(function(bizId, options) {
403
+ var _options_level, ap, _options_level1, systemStartTime;
404
+ return __generator(this, function(_state) {
405
+ if (options === void 0) options = {};
406
+ if (!hasRegisterEvent) {
407
+ hasRegisterEvent = true;
408
+ registerEvent(options);
409
+ }
410
+ if (bizId === mockIdFail || bizId === mockIdPass) {
411
+ return [
412
+ 2,
413
+ Promise.resolve({
414
+ bizId: bizId,
415
+ downgrade: bizId === mockIdFail,
416
+ level: (_options_level = options.level) != null ? _options_level : EFFECTS.spec.RenderLevel.S,
417
+ reason: "mock"
418
+ })
419
+ ];
420
+ }
421
+ ap = EFFECTS.isAlipayMiniApp() ? my : window.AlipayJSBridge;
422
+ // 当需要通过 ap 获取降级信息时,才进行降级环境的检查
423
+ if (!ap && (!options.systemInfo || !options.downgradeResult)) {
424
+ return [
425
+ 2,
426
+ {
427
+ bizId: bizId,
428
+ downgrade: false,
429
+ level: (_options_level1 = options.level) != null ? _options_level1 : EFFECTS.spec.RenderLevel.S,
430
+ reason: "Non-Alipay environment"
431
+ }
432
+ ];
433
+ }
434
+ systemStartTime = performance.now();
435
+ return [
436
+ 2,
437
+ getSystemInfoJSAPI(options, ap).then(function(systemInfo) {
438
+ var systemEndTime = performance.now();
439
+ return getDowngradeResultJSAPI(bizId, options, ap).then(function(downgradeResult) {
440
+ var downgradeEndTime = performance.now();
441
+ EFFECTS.logger.info("Downgrade time: " + (downgradeEndTime - systemStartTime) + "ms.");
442
+ var device = new DeviceProxy();
443
+ device.setSystemInfo(systemInfo);
444
+ var decision = device.getDowngradeDecision(downgradeResult);
445
+ if (options.level) {
446
+ decision.level = options.level;
447
+ }
448
+ var result = _extends({}, decision, {
449
+ bizId: bizId,
450
+ systemInfo: systemInfo,
451
+ systemTime: systemEndTime - systemStartTime,
452
+ downgradeResult: downgradeResult,
453
+ downgradeTime: downgradeEndTime - systemEndTime
454
+ });
455
+ return result;
456
+ });
457
+ })
458
+ ];
459
+ });
460
+ });
461
+ return _getDowngradeResult.apply(this, arguments);
462
+ }
463
+ function registerEvent(options) {
464
+ var autoPause = options.autoPause;
465
+ window.addEventListener("unload", function() {
466
+ EFFECTS.getActivePlayers().forEach(function(player) {
467
+ return player.dispose();
468
+ });
469
+ });
470
+ if (autoPause) {
471
+ document.addEventListener("pause", pauseAllActivePlayers);
472
+ document.addEventListener("resume", resumePausedPlayers);
371
473
  }
372
474
  }
373
- function isDeviceLevel(deviceLevel) {
374
- return deviceLevel === DEVICE_LEVEL_HIGH
375
- || deviceLevel === DEVICE_LEVEL_MEDIUM
376
- || deviceLevel === DEVICE_LEVEL_LOW;
475
+ function getSystemInfoJSAPI(options, ap) {
476
+ return _getSystemInfoJSAPI.apply(this, arguments);
477
+ }
478
+ function _getSystemInfoJSAPI() {
479
+ _getSystemInfoJSAPI = _async_to_generator(function(options, ap) {
480
+ return __generator(this, function(_state) {
481
+ if (options.systemInfo) {
482
+ return [
483
+ 2,
484
+ Promise.resolve(options.systemInfo)
485
+ ];
486
+ } else {
487
+ return [
488
+ 2,
489
+ new Promise(function(resolve, reject) {
490
+ ap.call("getSystemInfo", function(e) {
491
+ if (e.error) {
492
+ reject(e);
493
+ } else {
494
+ resolve(e);
495
+ }
496
+ });
497
+ })
498
+ ];
499
+ }
500
+ });
501
+ });
502
+ return _getSystemInfoJSAPI.apply(this, arguments);
503
+ }
504
+ function getDowngradeResultJSAPI(bizId, options, ap) {
505
+ return _getDowngradeResultJSAPI.apply(this, arguments);
506
+ }
507
+ function _getDowngradeResultJSAPI() {
508
+ _getDowngradeResultJSAPI = _async_to_generator(function(bizId, options, ap) {
509
+ return __generator(this, function(_state) {
510
+ if (options.downgradeResult) {
511
+ return [
512
+ 2,
513
+ Promise.resolve(options.downgradeResult)
514
+ ];
515
+ } else {
516
+ return [
517
+ 2,
518
+ new Promise(function(resolve, reject) {
519
+ var techPoint = [
520
+ "mars"
521
+ ];
522
+ var tc = options.techPoint;
523
+ if (tc) {
524
+ var _techPoint;
525
+ (_techPoint = techPoint).push.apply(_techPoint, [].concat(tc));
526
+ }
527
+ var downgradeOptions = {
528
+ bizId: bizId,
529
+ scene: 0,
530
+ ext: {
531
+ techPoint: techPoint
532
+ }
533
+ };
534
+ var _options_callBridge;
535
+ var callBridge = (_options_callBridge = options.callBridge) != null ? _options_callBridge : ap.call;
536
+ callBridge("getDowngradeResult", downgradeOptions, function(result) {
537
+ resolve(result);
538
+ });
539
+ })
540
+ ];
541
+ }
542
+ });
543
+ });
544
+ return _getDowngradeResultJSAPI.apply(this, arguments);
545
+ }
546
+ /**
547
+ * 获取默认渲染等级
548
+ *
549
+ * @returns 渲染等级
550
+ */ function getDefaultRenderLevel() {
551
+ return EFFECTS.isIOS() ? EFFECTS.spec.RenderLevel.S : EFFECTS.spec.RenderLevel.B;
377
552
  }
378
- var internalPaused = Symbol('@@_inter_pause');
379
553
  function pauseAllActivePlayers(e) {
380
554
  if (e.target === document) {
381
- effects.logger.info('Auto pause all players with data offloaded');
382
- var players = effects.getActivePlayers();
383
- players.forEach(function (player) {
555
+ EFFECTS.logger.info("Auto pause all players with data offloaded.");
556
+ var players = EFFECTS.getActivePlayers();
557
+ players.forEach(function(player) {
384
558
  if (!player.paused) {
385
- player.pause({ offloadTexture: true });
559
+ player.pause({
560
+ offloadTexture: true
561
+ });
386
562
  // @ts-expect-error
387
563
  player[internalPaused] = true;
388
564
  }
@@ -391,9 +567,9 @@ function pauseAllActivePlayers(e) {
391
567
  }
392
568
  function resumePausedPlayers(e) {
393
569
  if (e.target === document) {
394
- effects.logger.info('auto resume all players');
395
- var players = effects.getActivePlayers();
396
- players.forEach(function (player) {
570
+ EFFECTS.logger.info("Auto resume all players.");
571
+ var players = EFFECTS.getActivePlayers();
572
+ players.forEach(function(player) {
397
573
  // @ts-expect-error
398
574
  if (player[internalPaused]) {
399
575
  void player.resume();
@@ -404,122 +580,73 @@ function resumePausedPlayers(e) {
404
580
  }
405
581
  }
406
582
 
407
- var AlipayDowngradePlugin = /** @class */ (function (_super) {
408
- __extends(AlipayDowngradePlugin, _super);
583
+ /**
584
+ * Alipay 降级插件类
585
+ *
586
+ * 根据 SceneLoadOptions 中传入的 downgrade 数据,判断是否降级。
587
+ * 如果设备被降级,会在 processRawJSON 时抛出降级相关的异常和原因。
588
+ *
589
+ * 如果 SceneLoadOptions 中 renderLevel 没有设置,那么会根据 downgrade 数据
590
+ * 和默认的渲染等级规则设置其中的 renderLevel。
591
+ */ var AlipayDowngradePlugin = /*#__PURE__*/ function(AbstractPlugin) {
592
+ _inherits(AlipayDowngradePlugin, AbstractPlugin);
409
593
  function AlipayDowngradePlugin() {
410
- return _super !== null && _super.apply(this, arguments) || this;
594
+ return AbstractPlugin.apply(this, arguments);
411
595
  }
412
- AlipayDowngradePlugin.onPlayerCreated = function (player) {
413
- return __awaiter(this, void 0, void 0, function () {
414
- return __generator(this, function (_a) {
415
- if (AlipayDowngradePlugin.glLostOccurred) {
416
- console.warn('gl lost happened, new player will be destroyed.');
417
- return [2 /*return*/, player.dispose()];
418
- }
419
- return [2 /*return*/];
420
- });
421
- });
422
- };
423
- AlipayDowngradePlugin.processRawJSON = function (json, options) {
424
- var _a;
425
- if (options === void 0) { options = {}; }
426
- return __awaiter(this, void 0, void 0, function () {
427
- var downgradeResult, downgradeDecision;
428
- return __generator(this, function (_b) {
429
- if (AlipayDowngradePlugin.glLostOccurred) {
430
- return [2 /*return*/, Promise.reject('gl lost happened')];
431
- }
432
- downgradeResult = (_a = options.pluginData) === null || _a === void 0 ? void 0 : _a['downgrade'];
596
+ AlipayDowngradePlugin.processRawJSON = function processRawJSON(json, options) {
597
+ if (options === void 0) options = {};
598
+ return _async_to_generator(function() {
599
+ var _options_pluginData, downgradeResult, _downgradeResult_level;
600
+ return __generator(this, function(_state) {
601
+ downgradeResult = (_options_pluginData = options.pluginData) == null ? void 0 : _options_pluginData["downgrade"];
433
602
  if (downgradeResult) {
434
- downgradeDecision = checkDowngradeResult(downgradeResult);
435
- if (downgradeDecision.downgrade) {
436
- throw new Error("downgraded, reason: ".concat(downgradeDecision.reason));
603
+ if (downgradeResult.downgrade) {
604
+ throw new Error("Downgraded, reason: " + downgradeResult.reason);
437
605
  }
438
- }
439
- else {
440
- console.warn('No downgrade result in pluginData of SceneLoadOptions');
606
+ } else {
607
+ EFFECTS.logger.warn("No downgrade result in pluginData of SceneLoadOptions.");
441
608
  }
442
609
  if (!options.renderLevel) {
443
- options.renderLevel = getRenderLevelByDevice(options.renderLevel);
610
+ options.renderLevel = (_downgradeResult_level = downgradeResult == null ? void 0 : downgradeResult.level) != null ? _downgradeResult_level : getDefaultRenderLevel();
444
611
  }
445
- return [2 /*return*/];
612
+ return [
613
+ 2
614
+ ];
446
615
  });
447
- });
616
+ })();
448
617
  };
449
- AlipayDowngradePlugin.glLostOccurred = false;
450
618
  return AlipayDowngradePlugin;
451
- }(effects.AbstractPlugin));
619
+ }(EFFECTS.AbstractPlugin);
452
620
 
453
- var prefix = '[Galacean Effects]';
454
- effects.logger.register(nativeLogger);
621
+ var prefix = "[Galacean Effects]";
622
+ var ap = EFFECTS.isAlipayMiniApp() ? my : window.AlipayJSBridge;
623
+ EFFECTS.logger.register(nativeLogger);
455
624
  function nativeLogger(type, msg) {
456
- var args = [];
457
- for (var _i = 2; _i < arguments.length; _i++) {
458
- args[_i - 2] = arguments[_i];
459
- }
460
- if (effects.isAndroid()) {
461
- androidLogger.apply(void 0, __spreadArray([type, "".concat(prefix, " ").concat(msg)], __read(args), false));
462
- }
463
- else if (effects.isIOS()) {
464
- iOSLogger.apply(void 0, __spreadArray([type, msg], __read(args), false));
465
- }
466
- }
467
- function iOSLogger(type, msg) {
468
- var args = [];
469
- for (var _i = 2; _i < arguments.length; _i++) {
470
- args[_i - 2] = arguments[_i];
471
- }
472
- try {
473
- var ap = window.AlipayJSBridge;
474
- var content = formatMessage.apply(void 0, __spreadArray([type, msg], __read(args), false));
475
- ap === null || ap === void 0 ? void 0 : ap.call('H5APLog', {
476
- content: content,
477
- });
625
+ for(var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++){
626
+ args[_key - 2] = arguments[_key];
478
627
  }
479
- catch (e) {
480
- console.error(e);
481
- }
482
- }
483
- // Android H5 容器支持 console 日志直接写入 aplog
484
- function androidLogger(type, msg) {
485
- var args = [];
486
- for (var _i = 2; _i < arguments.length; _i++) {
487
- args[_i - 2] = arguments[_i];
628
+ var content = {
629
+ "message": prefix + " " + msg + " " + args.join(""),
630
+ "level": type
631
+ };
632
+ if (EFFECTS.isAndroid()) {
633
+ content["anr_info"] = "mars";
488
634
  }
489
635
  try {
490
- var ap = window.AlipayJSBridge;
491
- ap === null || ap === void 0 ? void 0 : ap.call('localLog', {
492
- 'anr_info': 'mars',
493
- 'message': msg + args.join(''),
494
- 'level': type,
495
- });
496
- }
497
- catch (e) {
636
+ ap == null ? void 0 : ap.call("localLog", content);
637
+ } catch (e) {
498
638
  console.error(e);
499
639
  }
500
640
  }
501
- function formatMessage(level, msg) {
502
- var args = [];
503
- for (var _i = 2; _i < arguments.length; _i++) {
504
- args[_i - 2] = arguments[_i];
505
- }
506
- var result = [
507
- '<MARS_PLAYER>',
508
- "(".concat(level, "):"),
509
- msg,
510
- ];
511
- args.forEach(function (i) {
512
- result.push(i + '');
513
- });
514
- return result.join(' ');
515
- }
516
641
 
517
- var version = "2.0.0-alpha.3";
518
- effects.registerPlugin('alipay-downgrade', AlipayDowngradePlugin, DowngradeVFXItem, true);
519
- effects.logger.info('plugin alipay downgrade version: ' + version);
642
+ var version = "2.0.0-alpha.31";
643
+ EFFECTS.registerPlugin("alipay-downgrade", AlipayDowngradePlugin, EFFECTS.VFXItem, true);
644
+ EFFECTS.logger.info("Plugin downgrade version: " + version + ".");
645
+ if (version !== EFFECTS__namespace.version) {
646
+ console.error("注意:请统一 Downgrade 插件与 Player 版本,不统一的版本混用会有不可预知的后果!", "\nAttention: Please ensure the Downgrade plugin is synchronized with the Player version. Mixing and matching incompatible versions may result in unpredictable consequences!");
647
+ }
520
648
 
521
- exports.checkDowngradeResult = checkDowngradeResult;
649
+ exports.getDefaultRenderLevel = getDefaultRenderLevel;
522
650
  exports.getDowngradeResult = getDowngradeResult;
523
- exports.getRenderLevelByDevice = getRenderLevelByDevice;
524
651
  exports.version = version;
525
652
  //# sourceMappingURL=index.js.map