From fba09315353aa5bb24f3d50c4e89ac535d0bd011 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 19 Jun 2021 13:38:36 +0200 Subject: [PATCH 1/6] Code quality - Fix some of the regex. - Prefer `for ([])` - Use some better array functions. --- scripts/update_benchmarks.ts | 6 +++--- src/index.ts | 18 +++++++++--------- src/internal/cleanRenderer.ts | 4 ++-- src/internal/deobfuscateAppleGPU.ts | 2 +- src/internal/deobfuscateRenderer.ts | 6 +----- src/internal/getGPUVersion.ts | 4 ++-- test/index.test.ts | 6 +++--- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/scripts/update_benchmarks.ts b/scripts/update_benchmarks.ts index 62fcd09e..db484a37 100755 --- a/scripts/update_benchmarks.ts +++ b/scripts/update_benchmarks.ts @@ -46,8 +46,8 @@ const outputFile = async (name: string, content: any) => { .map((benchmark) => { benchmark.gpu = benchmark.gpu .toLowerCase() - .replace(/\s*\([^\)]+(\))/g, '') - .replace(/([0-9]+)\/[^ ]+/, '$1') + .replace(/\s*\([^)]+(\))/g, '') + .replace(/(\d+)\/[^ ]+/, '$1') .replace( /x\.org |inc\. |open source technology center |imagination technologies |™ |nvidia corporation |apple inc\. |advanced micro devices, inc\. | series$| edition$| graphics$/g, '' @@ -99,7 +99,7 @@ const outputFile = async (name: string, content: any) => { fps: fpses[index] === '' ? undefined - : Math.round(Number(fpses[index].replace(/[^0-9.]+/g, ''))), + : Math.round(Number(fpses[index].replace(/[^\d.]+/g, ''))), gpu: gpuNameLookup[gpuIndex], mobile: formFactorLookup[formFactor[index]].includes('mobile'), resolution: screenSizeLookup[screenSizes[index]], diff --git a/src/index.ts b/src/index.ts index 1c7cd8f0..ead062c1 100644 --- a/src/index.ts +++ b/src/index.ts @@ -126,7 +126,7 @@ export const getGPUTier = async ({ ? (['adreno', 'apple', 'mali-t', 'mali', 'nvidia', 'powervr'] as const) : (['intel', 'amd', 'radeon', 'nvidia', 'geforce'] as const); for (const type of types) { - if (renderer.indexOf(type) > -1) { + if (renderer.includes(type)) { return type; } } @@ -168,7 +168,7 @@ export const getGPUTier = async ({ // If nothing matched, try comparing model names: if (!matched.length) { - matched = benchmarks.filter(([model]) => model.indexOf(renderer) > -1); + matched = benchmarks.filter(([model]) => model.includes(renderer)); debug?.( `found ${matched.length} matching entries comparing model names`, @@ -229,7 +229,7 @@ export const getGPUTier = async ({ } if (!closest) { - return undefined; + return; } // eslint-disable-next-line @typescript-eslint/no-non-null-assertion @@ -287,10 +287,10 @@ export const getGPUTier = async ({ ); if (!results.length) { - const blocklistedModel: string | undefined = BLOCKLISTED_GPUS.filter( + const blocklistedModel: string | undefined = BLOCKLISTED_GPUS.find( // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - (blocklistedModel) => renderer!.indexOf(blocklistedModel) > -1 - )[0]; + (blocklistedModel) => renderer!.includes(blocklistedModel) + ); return blocklistedModel ? toResult(0, 'BLOCKLISTED', blocklistedModel) : toResult(1, 'FALLBACK', `${renderer} (${rawRenderer})`); @@ -307,9 +307,9 @@ export const getGPUTier = async ({ const tiers = isMobile ? mobileTiers : desktopTiers; let tier = 0; - for (let i = 0; i < tiers.length; i++) { - if (fps >= tiers[i]) { - tier = i; + for (const [index, tierFPS] of tiers.entries()) { + if (fps >= tierFPS) { + tier = index; } } diff --git a/src/internal/cleanRenderer.ts b/src/internal/cleanRenderer.ts index 7ea1fe14..33b59db6 100644 --- a/src/internal/cleanRenderer.ts +++ b/src/internal/cleanRenderer.ts @@ -7,11 +7,11 @@ export const cleanRenderer = (renderer: string) => { .toLowerCase() // Strip off ANGLE() - for example: // 'ANGLE (NVIDIA TITAN Xp)' becomes 'NVIDIA TITAN Xp'': - .replace(/angle \((.+)\)*$/, '$1') + .replace(/^angle ?\((.+)\)*$/, '$1') // Strip off [number]gb & strip off direct3d and after - for example: // 'Radeon (TM) RX 470 Series Direct3D11 vs_5_0 ps_5_0' becomes // 'Radeon (TM) RX 470 Series' - .replace(/\s+([0-9]+gb|direct3d.+$)|\(r\)| \([^)]+\)$/g, ''); + .replace(/\s(\d{1,2}gb|direct3d)|\(r\)| \([^)]+\)$/g, ''); debug?.('cleanRenderer - renderer cleaned to', { renderer }); diff --git a/src/internal/deobfuscateAppleGPU.ts b/src/internal/deobfuscateAppleGPU.ts index e2540e06..f5e5069f 100644 --- a/src/internal/deobfuscateAppleGPU.ts +++ b/src/internal/deobfuscateAppleGPU.ts @@ -82,7 +82,7 @@ export const deobfuscateAppleGPU = ( gl.vertexAttribPointer(aPosition, 3, GL_FLOAT, false, 0, 0); gl.enableVertexAttribArray(aPosition); - gl.clearColor(1.0, 1.0, 1.0, 1.0); + gl.clearColor(1, 1, 1, 1); gl.clear(GL_COLOR_BUFFER_BIT); gl.viewport(0, 0, 1, 1); gl.drawArrays(GL_TRIANGLES, 0, 3); diff --git a/src/internal/deobfuscateRenderer.ts b/src/internal/deobfuscateRenderer.ts index 36cac742..1f1e6daa 100644 --- a/src/internal/deobfuscateRenderer.ts +++ b/src/internal/deobfuscateRenderer.ts @@ -6,9 +6,5 @@ export const deobfuscateRenderer = ( renderer: string, isMobileTier: boolean ) => { - if (renderer === 'apple gpu') { - return deobfuscateAppleGPU(gl, renderer, isMobileTier); - } else { - return [renderer]; - } + return renderer === 'apple gpu' ? deobfuscateAppleGPU(gl, renderer, isMobileTier) : [renderer]; }; diff --git a/src/internal/getGPUVersion.ts b/src/internal/getGPUVersion.ts index 855d02a3..6e16db11 100644 --- a/src/internal/getGPUVersion.ts +++ b/src/internal/getGPUVersion.ts @@ -3,9 +3,9 @@ export const getGPUVersion = (model: string) => { const matches = // First set of digits - model.match(/[\d]+/) || + model.match(/\d+/) || // If the renderer did not contain any numbers, match letters - model.match(/(\W|^)([a-zA-Z]{1,3})(\W|$)/g); + model.match(/(\W|^)([A-Za-z]{1,3})(\W|$)/g); // Remove any non-word characters and also remove 'amd' which could be matched // in the clause above diff --git a/test/index.test.ts b/test/index.test.ts index 2f11a005..6aab68a5 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -80,7 +80,7 @@ test(`Bottom tier desktop: ${bottomTierDesktop}`, async () => { }); // expect BENCHMARK results: -[ +for (const { input, expected } of [ { expected: { gpu: 'intel uhd graphics 620', @@ -233,7 +233,7 @@ test(`Bottom tier desktop: ${bottomTierDesktop}`, async () => { renderer: 'Mali-G51', }, }, -].forEach(({ input, expected }) => { +]) { test(`${input.renderer} should find ${expected.gpu}`, async () => { expectGPUResults( { @@ -243,7 +243,7 @@ test(`Bottom tier desktop: ${bottomTierDesktop}`, async () => { await getTier(input) ); }); -}); +} // expect FALLBACK results: [ From d8cffaf52b6a5b1c2ca21806c8e8004173597b1f Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 19 Jun 2021 13:45:20 +0200 Subject: [PATCH 2/6] Woops fix tests --- src/internal/cleanRenderer.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/internal/cleanRenderer.ts b/src/internal/cleanRenderer.ts index 33b59db6..f6849d8c 100644 --- a/src/internal/cleanRenderer.ts +++ b/src/internal/cleanRenderer.ts @@ -11,7 +11,7 @@ export const cleanRenderer = (renderer: string) => { // Strip off [number]gb & strip off direct3d and after - for example: // 'Radeon (TM) RX 470 Series Direct3D11 vs_5_0 ps_5_0' becomes // 'Radeon (TM) RX 470 Series' - .replace(/\s(\d{1,2}gb|direct3d)|\(r\)| \([^)]+\)$/g, ''); + .replace(/\s(\d{1,2}gb|direct3d.+$)|\(r\)| \([^)]+\)$/g, ''); debug?.('cleanRenderer - renderer cleaned to', { renderer }); From 148c04ef6eb16220c41834fc32fcc2618c70b667 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 19 Jun 2021 13:54:07 +0200 Subject: [PATCH 3/6] fix rollup build --- tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index 4acb39b8..9ee0d468 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,8 @@ "resolveJsonModule": true, "declarationDir": "dist/types", "outDir": "dist/lib", - "typeRoots": ["node_modules/@types"] + "typeRoots": ["node_modules/@types"], + "downlevelIteration": true, }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] From 65b70007779f965181228b4e6f357d3eb6cde206 Mon Sep 17 00:00:00 2001 From: Gusted Date: Sat, 19 Jun 2021 21:16:37 +0200 Subject: [PATCH 4/6] Let's make it less broken changes --- src/index.ts | 12 +++++++++--- tsconfig.json | 1 - 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/index.ts b/src/index.ts index ead062c1..50a7d62d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,6 +89,12 @@ export type ModelEntry = [string, string, 0 | 1, ModelEntryScreen[]]; const debug = false ? console.log : undefined; +if (!String.prototype.includes) { + String.prototype.includes = function(search, start) { + return this.indexOf(search, start) !== -1; + } +} + export const getGPUTier = async ({ mobileTiers = [0, 15, 30, 60], desktopTiers = [0, 15, 30, 60], @@ -307,9 +313,9 @@ export const getGPUTier = async ({ const tiers = isMobile ? mobileTiers : desktopTiers; let tier = 0; - for (const [index, tierFPS] of tiers.entries()) { - if (fps >= tierFPS) { - tier = index; + for (let i = 0; i < tiers.length; i++) { + if (fps >= tiers[i]) { + tier = i; } } diff --git a/tsconfig.json b/tsconfig.json index 9ee0d468..c41e243e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,7 +14,6 @@ "declarationDir": "dist/types", "outDir": "dist/lib", "typeRoots": ["node_modules/@types"], - "downlevelIteration": true, }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] From 64a54bfd78855a0e7e3c5239e9c7c1ba1dcf0205 Mon Sep 17 00:00:00 2001 From: Tim van Scherpenzeel Date: Mon, 12 Jul 2021 13:55:13 +0200 Subject: [PATCH 5/6] Update tsconfig.json --- tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsconfig.json b/tsconfig.json index c41e243e..4acb39b8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -13,7 +13,7 @@ "resolveJsonModule": true, "declarationDir": "dist/types", "outDir": "dist/lib", - "typeRoots": ["node_modules/@types"], + "typeRoots": ["node_modules/@types"] }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] From f25f7d12484e59a92b79d7b738820103ce4555e7 Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 12 Jul 2021 19:26:07 +0200 Subject: [PATCH 6/6] Remove polyfill --- src/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/index.ts b/src/index.ts index 50a7d62d..a370a959 100644 --- a/src/index.ts +++ b/src/index.ts @@ -89,12 +89,6 @@ export type ModelEntry = [string, string, 0 | 1, ModelEntryScreen[]]; const debug = false ? console.log : undefined; -if (!String.prototype.includes) { - String.prototype.includes = function(search, start) { - return this.indexOf(search, start) !== -1; - } -} - export const getGPUTier = async ({ mobileTiers = [0, 15, 30, 60], desktopTiers = [0, 15, 30, 60],