Skip to content

Commit

Permalink
fix sys coding auto completion (cocos#8995)
Browse files Browse the repository at this point in the history
* fix sys coding auto completion

* fix ci
  • Loading branch information
PPpro authored Jul 16, 2021
1 parent ed4ba94 commit 5b230d7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 68 deletions.
1 change: 1 addition & 0 deletions cocos/2d/renderer/batcher-2d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export class Batcher2D {

// HACK: After sharing buffer between drawcalls, the performance degradation a lots on iOS 14 or iPad OS 14 device
// TODO: Maybe it can be removed after Apple fixes it?
// @ts-expect-error Property '__isWebIOS14OrIPadOS14Env' does not exist on 'sys'
if (sys.__isWebIOS14OrIPadOS14Env && !this._currIsStatic) {
this._currMeshBuffer = null;
}
Expand Down
18 changes: 0 additions & 18 deletions cocos/core/asset-manager/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
*/
import { Asset } from '..';
import { IMemoryImageSource } from '../assets/image-asset';
import { sys } from '../platform/sys';
import { js } from '../utils';
import Cache from './cache';
import deserialize from './deserialize';
Expand Down Expand Up @@ -155,11 +154,6 @@ export class Parser {
'.pvr': this.parsePVRTex,
'.pkm': this.parsePKMTex,
'.astc': this.parseASTCTex,
// Audio
'.mp3': this.parseAudio,
'.ogg': this.parseAudio,
'.wav': this.parseAudio,
'.m4a': this.parseAudio,

// plist
'.plist': this.parsePlist,
Expand All @@ -181,18 +175,6 @@ export class Parser {
});
}

public parseAudio (file: ArrayBuffer | HTMLAudioElement, options: IDownloadParseOptions, onComplete: CompleteCallback<AudioBuffer|HTMLAudioElement>) {
if (file instanceof ArrayBuffer) {
sys.__audioSupport.context.decodeAudioData(file, (buffer) => {
onComplete(null, buffer);
}, (e) => {
onComplete(new Error(`Error with decoding audio data${e.err}`), null);
});
} else {
onComplete(null, file);
}
}

public parsePVRTex (file: ArrayBuffer | ArrayBufferView, options: IDownloadParseOptions, onComplete: CompleteCallback<IMemoryImageSource>) {
let err: Error | null = null;
let out: IMemoryImageSource | null = null;
Expand Down
99 changes: 49 additions & 50 deletions cocos/core/platform/sys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const pixelRatio = systemInfo.pixelRatio;
* @zh 一系列系统相关环境变量
* @main
*/
export const sys: Record<string, any> = {
export const sys = {
/**
* @en
* Network type enumeration
Expand Down Expand Up @@ -194,7 +194,7 @@ export const sys: Record<string, any> = {
* @en It is a local storage component based on HTML5 localStorage API, on web platform, it's equal to window.localStorage
* @zh HTML5 标准中的 localStorage 的本地存储功能,在 Web 端等价于 window.localStorage
*/
localStorage: null,
localStorage: {} as Storage,

/**
* @en Get the network type of current device, return `sys.NetworkType.LAN` if failure.
Expand Down Expand Up @@ -313,55 +313,54 @@ export const sys: Record<string, any> = {
const height = rightTop.y - leftBottom.y;
return new Rect(x, y, width, height);
},

__init () {
try {
let localStorage: Storage | null = sys.localStorage = window.localStorage;
localStorage.setItem('storage', '');
localStorage.removeItem('storage');
localStorage = null;
} catch (e) {
const warn = function () {
warnID(5200);
};
sys.localStorage = {
getItem: warn,
setItem: warn,
removeItem: warn,
clear: warn,
};
}

// TODO: move into pal/input
const win = window; const nav = win.navigator; const doc = document; const docEle = doc.documentElement;
const capabilities = sys.capabilities;
if (docEle.ontouchstart !== undefined || doc.ontouchstart !== undefined || nav.msPointerEnabled) {
capabilities.touches = true;
}
if (docEle.onmouseup !== undefined) {
capabilities.mouse = true;
}
if (docEle.onkeyup !== undefined) {
capabilities.keyboard = true;
}
if (win.DeviceMotionEvent || win.DeviceOrientationEvent) {
capabilities.accelerometer = true;
}

// HACK: this private property only needed on web
sys.__isWebIOS14OrIPadOS14Env = (sys.os === OS.IOS || sys.os === OS.OSX) && systemInfo.isBrowser
&& /(OS 1[4-9])|(Version\/1[4-9])/.test(window.navigator.userAgent);

screenAdapter.on('window-resize', () => {
const windowSize = screenAdapter.windowSize;
sys.windowPixelResolution = {
width: Math.round(windowSize.width * pixelRatio),
height: Math.round(windowSize.height * pixelRatio),
};
});
},
};

sys.__init();
(function initSys () {
try {
let localStorage: Storage | null = sys.localStorage = window.localStorage;
localStorage.setItem('storage', '');
localStorage.removeItem('storage');
localStorage = null;
} catch (e) {
const warn = function () {
warnID(5200);
};
sys.localStorage = {
// @ts-expect-error Type '() => void' is not assignable to type '(key: string) => string | null'
getItem: warn,
setItem: warn,
clear: warn,
removeItem: warn,
};
}

// TODO: move into pal/input
const win = window; const nav = win.navigator; const doc = document; const docEle = doc.documentElement;
const capabilities = sys.capabilities;
if (docEle.ontouchstart !== undefined || doc.ontouchstart !== undefined || nav.msPointerEnabled) {
capabilities.touches = true;
}
if (docEle.onmouseup !== undefined) {
capabilities.mouse = true;
}
if (docEle.onkeyup !== undefined) {
capabilities.keyboard = true;
}
if (win.DeviceMotionEvent || win.DeviceOrientationEvent) {
capabilities.accelerometer = true;
}

// @ts-expect-error HACK: this private property only needed on web
sys.__isWebIOS14OrIPadOS14Env = (sys.os === OS.IOS || sys.os === OS.OSX) && systemInfo.isBrowser
&& /(OS 1[4-9])|(Version\/1[4-9])/.test(window.navigator.userAgent);

screenAdapter.on('window-resize', () => {
const windowSize = screenAdapter.windowSize;
sys.windowPixelResolution = {
width: Math.round(windowSize.width * pixelRatio),
height: Math.round(windowSize.height * pixelRatio),
};
});
}());

legacyCC.sys = sys;

0 comments on commit 5b230d7

Please sign in to comment.