MCPcopy Create free account
hub / github.com/melonjs/melonJS / tryLoadSource

Function tryLoadSource

packages/melonjs/tests/imageFallback.node-test.js:37–106  ·  view source on GitHub ↗

* Simulates the image loader's tryLoadSource logic: * - For compressed formats: fetch as arrayBuffer, parse, verify format support * - For standard images: fetch as blob, create bitmap * * @param {string} src - source path * @param {string} name - cache key * @param {Map }

(src, name, fileServer, supportedFormats, imgCache)

Source from the content-addressed store, hash-verified

35 * @returns {Promise}
36 */
37function tryLoadSource(src, name, fileServer, supportedFormats, imgCache) {
38 const ext = src.split(".").pop().toLowerCase();
39
40 // Simulate fetching — reject if file not found
41 const data = fileServer.get(src);
42 if (!data) {
43 return Promise.reject(new Error("404 Not Found: " + src));
44 }
45
46 switch (ext) {
47 case "dds":
48 case "pvr":
49 case "pkm":
50 case "ktx":
51 case "ktx2": {
52 return new Promise((resolve, reject) => {
53 try {
54 let texture;
55 switch (ext) {
56 case "dds":
57 texture = parseDDS(data);
58 break;
59 case "pvr":
60 texture = parsePVR(data);
61 break;
62 case "pkm":
63 texture = parsePKM(data);
64 break;
65 case "ktx":
66 texture = parseKTX(data);
67 break;
68 case "ktx2":
69 texture = parseKTX2(data);
70 break;
71 }
72 // post-parse: check if the GPU supports this specific format
73 if (supportedFormats.has(texture.format)) {
74 imgCache[name] = texture;
75 resolve();
76 } else {
77 reject(
78 new Error(
79 "unsupported texture format: " +
80 ext +
81 " (0x" +
82 texture.format.toString(16) +
83 ")",
84 ),
85 );
86 }
87 } catch (e) {
88 reject(e);
89 }
90 });
91 }
92
93 // Standard image (png, jpg, etc.)
94 default:

Callers 1

loadWithFallbackFunction · 0.70

Calls 9

parseDDSFunction · 0.90
parsePVRFunction · 0.90
parsePKMFunction · 0.90
parseKTXFunction · 0.90
parseKTX2Function · 0.90
splitMethod · 0.65
getMethod · 0.65
hasMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected