From b0f2a94e5e23b26255e7df8704919ec4d967f3a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Sat, 26 Dec 2020 18:21:31 +0100 Subject: [PATCH 1/2] Rework expo loader for better support --- .../webgltexture-loader-expo/package.json | 1 + .../src/ExpoModuleTextureLoader.js | 82 ++------ packages/webgltexture-loader-expo/src/md5.js | 191 ------------------ yarn.lock | 5 + 4 files changed, 21 insertions(+), 258 deletions(-) delete mode 100644 packages/webgltexture-loader-expo/src/md5.js diff --git a/packages/webgltexture-loader-expo/package.json b/packages/webgltexture-loader-expo/package.json index c84ee62..10671a2 100644 --- a/packages/webgltexture-loader-expo/package.json +++ b/packages/webgltexture-loader-expo/package.json @@ -15,6 +15,7 @@ "react-native": "*" }, "dependencies": { + "expo-asset-utils": "^1.2.0", "webgltexture-loader": "^1.0.0" } } diff --git a/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js b/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js index 9d57a77..ecd3cdf 100644 --- a/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js +++ b/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js @@ -1,87 +1,36 @@ //@flow import { globalRegistry, - WebGLTextureLoaderAsyncHashCache + WebGLTextureLoaderAsyncHashCache, } from "webgltexture-loader"; -import { Image } from "react-native"; -import * as FileSystem from "expo-file-system"; +import AssetUtils from "expo-asset-utils"; import { Asset } from "expo-asset"; -import md5 from "./md5"; - const neverEnding = new Promise(() => {}); -type AssetModel = { - width: number, - height: number, - uri: string, - localUri: string -}; - -const hash = (module: number | { uri: string }) => - typeof module === "number" ? module : module.uri; - const localAsset = (module: number) => { const asset = Asset.fromModule(module); return asset.downloadAsync().then(() => asset); }; -const remoteAssetCache = {}; - -const remoteAsset = (uri: string) => { - const i = uri.lastIndexOf("."); - const ext = i !== -1 ? uri.slice(i) : ".jpg"; - const key = md5(uri); - if (key in remoteAssetCache) { - return Promise.resolve(remoteAssetCache[key]); - } - const promise = Promise.all([ - new Promise((success, failure) => - Image.getSize(uri, (width, height) => success({ width, height }), failure) - ), - FileSystem.downloadAsync( - uri, - FileSystem.documentDirectory + `ExponentAsset-${key}${ext}`, - { - cache: true - } - ) - ]).then(([size, asset]) => ({ ...size, uri, localUri: asset.uri })); - remoteAssetCache[key] = promise; - return promise; +type AssetModel = { + width: number, + height: number, + uri: string, + localUri: string, }; -const localFile = (uri: string) => { - const i = uri.lastIndexOf("."); - const ext = i !== -1 ? uri.slice(i) : ".jpg"; - const key = md5(uri); - if (key in remoteAssetCache) { - return Promise.resolve(remoteAssetCache[key]); - } - const promise = new Promise((success, failure) => - Image.getSize(uri, (width, height) => success({ width, height }), failure) - ).then(size => ({ ...size, uri, localUri: uri })); - remoteAssetCache[key] = promise; - return promise; -}; +type M = number | { uri: string } | AssetModel; -export const loadAsset = ( - module: number | { uri: string } -): Promise => +export const loadAsset = (module: M): Promise => typeof module === "number" ? localAsset(module) - : module.uri.startsWith("file:") || - module.uri.startsWith("data:") || - module.uri.startsWith("asset:") || // All local paths in Android Expo standalone app - module.uri.startsWith("assets-library:") || // CameraRoll.getPhotos iOS - module.uri.startsWith("content:") || // CameraRoll.getPhotos Android - module.uri.startsWith("/") // Expo.takeSnapshotAsync in DEV in Expo 31 - ? localFile(module.uri) - : remoteAsset(module.uri); + : module.localUri + ? // $FlowFixMe + Promise.resolve(module) + : AssetUtils.resolveAsync(module.uri); -class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache< - number | { uri: string } -> { +class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache { objIds: WeakMap = new WeakMap(); canLoad(input: any) { @@ -101,12 +50,11 @@ class ExpoModuleTextureLoader extends WebGLTextureLoaderAsyncHashCache< const dispose = () => { disposed = true; }; - const promise = loadAsset(module).then(asset => { + const promise = loadAsset(module).then((asset) => { if (disposed) return neverEnding; const { width, height } = asset; const texture = gl.createTexture(); gl.bindTexture(gl.TEXTURE_2D, texture); - // $FlowFixMe gl.texImage2D( gl.TEXTURE_2D, 0, diff --git a/packages/webgltexture-loader-expo/src/md5.js b/packages/webgltexture-loader-expo/src/md5.js deleted file mode 100644 index e500ea8..0000000 --- a/packages/webgltexture-loader-expo/src/md5.js +++ /dev/null @@ -1,191 +0,0 @@ -let add32; - -function md5cycle(x, k) { - var a = x[0], - b = x[1], - c = x[2], - d = x[3]; - - a = ff(a, b, c, d, k[0], 7, -680876936); - d = ff(d, a, b, c, k[1], 12, -389564586); - c = ff(c, d, a, b, k[2], 17, 606105819); - b = ff(b, c, d, a, k[3], 22, -1044525330); - a = ff(a, b, c, d, k[4], 7, -176418897); - d = ff(d, a, b, c, k[5], 12, 1200080426); - c = ff(c, d, a, b, k[6], 17, -1473231341); - b = ff(b, c, d, a, k[7], 22, -45705983); - a = ff(a, b, c, d, k[8], 7, 1770035416); - d = ff(d, a, b, c, k[9], 12, -1958414417); - c = ff(c, d, a, b, k[10], 17, -42063); - b = ff(b, c, d, a, k[11], 22, -1990404162); - a = ff(a, b, c, d, k[12], 7, 1804603682); - d = ff(d, a, b, c, k[13], 12, -40341101); - c = ff(c, d, a, b, k[14], 17, -1502002290); - b = ff(b, c, d, a, k[15], 22, 1236535329); - - a = gg(a, b, c, d, k[1], 5, -165796510); - d = gg(d, a, b, c, k[6], 9, -1069501632); - c = gg(c, d, a, b, k[11], 14, 643717713); - b = gg(b, c, d, a, k[0], 20, -373897302); - a = gg(a, b, c, d, k[5], 5, -701558691); - d = gg(d, a, b, c, k[10], 9, 38016083); - c = gg(c, d, a, b, k[15], 14, -660478335); - b = gg(b, c, d, a, k[4], 20, -405537848); - a = gg(a, b, c, d, k[9], 5, 568446438); - d = gg(d, a, b, c, k[14], 9, -1019803690); - c = gg(c, d, a, b, k[3], 14, -187363961); - b = gg(b, c, d, a, k[8], 20, 1163531501); - a = gg(a, b, c, d, k[13], 5, -1444681467); - d = gg(d, a, b, c, k[2], 9, -51403784); - c = gg(c, d, a, b, k[7], 14, 1735328473); - b = gg(b, c, d, a, k[12], 20, -1926607734); - - a = hh(a, b, c, d, k[5], 4, -378558); - d = hh(d, a, b, c, k[8], 11, -2022574463); - c = hh(c, d, a, b, k[11], 16, 1839030562); - b = hh(b, c, d, a, k[14], 23, -35309556); - a = hh(a, b, c, d, k[1], 4, -1530992060); - d = hh(d, a, b, c, k[4], 11, 1272893353); - c = hh(c, d, a, b, k[7], 16, -155497632); - b = hh(b, c, d, a, k[10], 23, -1094730640); - a = hh(a, b, c, d, k[13], 4, 681279174); - d = hh(d, a, b, c, k[0], 11, -358537222); - c = hh(c, d, a, b, k[3], 16, -722521979); - b = hh(b, c, d, a, k[6], 23, 76029189); - a = hh(a, b, c, d, k[9], 4, -640364487); - d = hh(d, a, b, c, k[12], 11, -421815835); - c = hh(c, d, a, b, k[15], 16, 530742520); - b = hh(b, c, d, a, k[2], 23, -995338651); - - a = ii(a, b, c, d, k[0], 6, -198630844); - d = ii(d, a, b, c, k[7], 10, 1126891415); - c = ii(c, d, a, b, k[14], 15, -1416354905); - b = ii(b, c, d, a, k[5], 21, -57434055); - a = ii(a, b, c, d, k[12], 6, 1700485571); - d = ii(d, a, b, c, k[3], 10, -1894986606); - c = ii(c, d, a, b, k[10], 15, -1051523); - b = ii(b, c, d, a, k[1], 21, -2054922799); - a = ii(a, b, c, d, k[8], 6, 1873313359); - d = ii(d, a, b, c, k[15], 10, -30611744); - c = ii(c, d, a, b, k[6], 15, -1560198380); - b = ii(b, c, d, a, k[13], 21, 1309151649); - a = ii(a, b, c, d, k[4], 6, -145523070); - d = ii(d, a, b, c, k[11], 10, -1120210379); - c = ii(c, d, a, b, k[2], 15, 718787259); - b = ii(b, c, d, a, k[9], 21, -343485551); - - x[0] = add32(a, x[0]); - x[1] = add32(b, x[1]); - x[2] = add32(c, x[2]); - x[3] = add32(d, x[3]); -} - -function cmn(q, a, b, x, s, t) { - a = add32(add32(a, q), add32(x, t)); - return add32((a << s) | (a >>> (32 - s)), b); -} - -function ff(a, b, c, d, x, s, t) { - return cmn((b & c) | (~b & d), a, b, x, s, t); -} - -function gg(a, b, c, d, x, s, t) { - return cmn((b & d) | (c & ~d), a, b, x, s, t); -} - -function hh(a, b, c, d, x, s, t) { - return cmn(b ^ c ^ d, a, b, x, s, t); -} - -function ii(a, b, c, d, x, s, t) { - return cmn(c ^ (b | ~d), a, b, x, s, t); -} - -function md51(s) { - var n = s.length, - state = [1732584193, -271733879, -1732584194, 271733878], - i; - for (i = 64; i <= s.length; i += 64) { - md5cycle(state, md5blk(s.substring(i - 64, i))); - } - s = s.substring(i - 64); - var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; - for (i = 0; i < s.length; i++) - tail[i >> 2] |= s.charCodeAt(i) << ((i % 4) << 3); - tail[i >> 2] |= 0x80 << ((i % 4) << 3); - if (i > 55) { - md5cycle(state, tail); - for (i = 0; i < 16; i++) tail[i] = 0; - } - tail[14] = n * 8; - md5cycle(state, tail); - return state; -} - -/* there needs to be support for Unicode here, - * unless we pretend that we can redefine the MD-5 - * algorithm for multi-byte characters (perhaps - * by adding every four 16-bit characters and - * shortening the sum to 32 bits). Otherwise - * I suggest performing MD-5 as if every character - * was two bytes--e.g., 0040 0025 = @%--but then - * how will an ordinary MD-5 sum be matched? - * There is no way to standardize text to something - * like UTF-8 before transformation; speed cost is - * utterly prohibitive. The JavaScript standard - * itself needs to look at this: it should start - * providing access to strings as preformed UTF-8 - * 8-bit unsigned value arrays. - */ -function md5blk(s) { - /* I figured global was faster. */ - var md5blks = [], - i; /* Andy King said do it this way. */ - for (i = 0; i < 64; i += 4) { - md5blks[i >> 2] = - s.charCodeAt(i) + - (s.charCodeAt(i + 1) << 8) + - (s.charCodeAt(i + 2) << 16) + - (s.charCodeAt(i + 3) << 24); - } - return md5blks; -} - -var hex_chr = "0123456789abcdef".split(""); - -function rhex(n) { - var s = "", - j = 0; - for (; j < 4; j++) - s += hex_chr[(n >> (j * 8 + 4)) & 0x0f] + hex_chr[(n >> (j * 8)) & 0x0f]; - return s; -} - -function hex(x) { - for (var i = 0; i < x.length; i++) x[i] = rhex(x[i]); - return x.join(""); -} - -function md5(s) { - return hex(md51(s)); -} - -/* this function is much faster, -so if possible we use it. Some IEs -are the only ones I know of that -need the idiotic second function, -generated by an if clause. */ - -add32 = function(a, b) { - return (a + b) & 0xffffffff; -}; - -if (md5("hello") != "5d41402abc4b2a76b9719d911017c592") { - add32 = function(x, y) { - var lsw = (x & 0xffff) + (y & 0xffff), - msw = (x >> 16) + (y >> 16) + (lsw >> 16); - return (msw << 16) | (lsw & 0xffff); - }; -} - -export default md5; diff --git a/yarn.lock b/yarn.lock index ecca161..04358be 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3554,6 +3554,11 @@ expect@^26.6.2: jest-message-util "^26.6.2" jest-regex-util "^26.0.0" +expo-asset-utils@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/expo-asset-utils/-/expo-asset-utils-1.2.0.tgz#df52f8e8c836c343ad713a0044db79be69cfe64b" + integrity sha512-06zVi5aXzyMq7SFiawxu2FUbpbVlxnE9W44cG4K5HyhLaqyRqss+o5MZMEGn8Ibd+008UiK7yCPy/bSpx2hVag== + extend-shallow@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/extend-shallow/-/extend-shallow-2.0.1.tgz#51af7d614ad9a9f610ea1bafbb989d6b1c56890f" From 284a738438020ee350cdd42948a9b47dcc2ed61e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Renaudeau?= Date: Tue, 31 May 2022 07:57:10 +0200 Subject: [PATCH 2/2] Fixes import --- .../webgltexture-loader-expo/src/ExpoModuleTextureLoader.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js b/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js index ecd3cdf..329eb85 100644 --- a/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js +++ b/packages/webgltexture-loader-expo/src/ExpoModuleTextureLoader.js @@ -3,7 +3,7 @@ import { globalRegistry, WebGLTextureLoaderAsyncHashCache, } from "webgltexture-loader"; -import AssetUtils from "expo-asset-utils"; +import * as AssetUtils from "expo-asset-utils"; import { Asset } from "expo-asset"; const neverEnding = new Promise(() => {});