逆向一直都不是我的强项,但是作为 Wasm 系列中很重要的一个知识点,还是将相关资源罗列在此
https://www.52pojie.cn/forum.php?mod=viewthread&tid=1900845
https://www.52pojie.cn/forum.php?mod=viewthread&tid=1862975
https://www.52pojie.cn/forum.php?mod=viewthread&tid=1863743
对于常规的 Godot 的 Wasm 文件
wasm2wat --enable-threads index.wasm -o index.wat
对于常规的 Godot 加密的 Wasm 文件
对于我们的项目
wasm-objdump -h target.wasm
target.wasm: file format wasm 0x1
Sections:
Type start=0x0000000b end=0x00001255 (size=0x0000124a) count: 481
Import start=0x00001258 end=0x000039c4 (size=0x0000276c) count: 337
Function start=0x000039c8 end=0x00013350 (size=0x0000f988) count: 63154
Table start=0x00013352 end=0x0001335b (size=0x00000009) count: 1
Memory start=0x0001335d end=0x00013364 (size=0x00000007) count: 1
Tag start=0x00013366 end=0x00013369 (size=0x00000003) count: 1
Global start=0x0001336b end=0x0001337e (size=0x00000013) count: 3
Export start=0x00013381 end=0x0001354d (size=0x000001cc) count: 22
Elem start=0x00013551 end=0x00041554 (size=0x0002e003) count: 1
DataCount start=0x00041556 end=0x00041558 (size=0x00000002) count: 4974
Code start=0x0004155d end=0x01c71025 (size=0x01c2fac8) count: 63154
Data start=0x01c7102a end=0x0202fbb2 (size=0x003beb88) count: 4974
js
/**
* Load the engine from the specified base path.
*
* @param {string} basePath Base path of the engine to load.
* @param {number=} [size=0] The file size if known.
* @returns {Promise} A Promise that resolves once the engine is loaded.
*
* @function Engine.load
*/
Engine.load = function (basePath, size) {
if (loadPromise == null) {
loadPath = basePath;
loadPromise = preloader.loadPromise(`${loadPath}.wasm`, size, true);
requestAnimationFrame(preloader.animateProgress);
}
return loadPromise;
};
const Preloader = /** @constructor */ function () { // eslint-disable-line no-unused-vars
function getTrackedResponse(response, load_status) {
function onloadprogress(reader, controller) {
return reader.read().then(function (result) {
if (load_status.done) {
return Promise.resolve();
}
if (result.value) {
controller.enqueue(result.value);
load_status.loaded += result.value.length;
}
if (!result.done) {
return onloadprogress(reader, controller);
}
load_status.done = true;
return Promise.resolve();
});
}
const reader = response.body.getReader();
return new Response(new ReadableStream({
start: function (controller) {
onloadprogress(reader, controller).then(function () {
controller.close();
});
},
}), { headers: response.headers });
}
function loadFetch(file, tracker, fileSize, raw) {
tracker[file] = {
total: fileSize || 0,
loaded: 0,
done: false,
};
return fetch(file).then(function (response) {
if (!response.ok) {
return Promise.reject(new Error(`Failed loading file '${file}'`));
}
const tr = getTrackedResponse(response, tracker[file]);
if (raw) {
return Promise.resolve(tr);
}
return tr.arrayBuffer();
});
}
function retry(func, attempts = 1) {
function onerror(err) {
if (attempts <= 1) {
return Promise.reject(err);
}
return new Promise(function (resolve, reject) {
setTimeout(function () {
retry(func, attempts - 1).then(resolve).catch(reject);
}, 1000);
});
}
return func().catch(onerror);
}
const DOWNLOAD_ATTEMPTS_MAX = 4;
const loadingFiles = {};
const lastProgress = { loaded: 0, total: 0 };
let progressFunc = null;
const animateProgress = function () {
let loaded = 0;
let total = 0;
let totalIsValid = true;
let progressIsFinal = true;
Object.keys(loadingFiles).forEach(function (file) {
const stat = loadingFiles[file];
if (!stat.done) {
progressIsFinal = false;
}
if (!totalIsValid || stat.total === 0) {
totalIsValid = false;
total = 0;
} else {
total += stat.total;
}
loaded += stat.loaded;
});
if (loaded !== lastProgress.loaded || total !== lastProgress.total) {
lastProgress.loaded = loaded;
lastProgress.total = total;
if (typeof progressFunc === 'function') {
progressFunc(loaded, total);
}
}
if (!progressIsFinal) {
requestAnimationFrame(animateProgress);
}
};
this.animateProgress = animateProgress;
this.setProgressFunc = function (callback) {
progressFunc = callback;
};
this.loadPromise = function (file, fileSize, raw = false) {
return retry(loadFetch.bind(null, file, loadingFiles, fileSize, raw), DOWNLOAD_ATTEMPTS_MAX);
};
this.preloadedFiles = [];
this.preload = function (pathOrBuffer, destPath, fileSize) {
let buffer = null;
if (typeof pathOrBuffer === 'string') {
const me = this;
return this.loadPromise(pathOrBuffer, fileSize).then(function (buf) {
me.preloadedFiles.push({
path: destPath || pathOrBuffer,
buffer: buf,
});
return Promise.resolve();
});
} else if (pathOrBuffer instanceof ArrayBuffer) {
buffer = new Uint8Array(pathOrBuffer);
} else if (ArrayBuffer.isView(pathOrBuffer)) {
buffer = new Uint8Array(pathOrBuffer.buffer);
}
if (buffer) {
this.preloadedFiles.push({
path: destPath,
buffer: pathOrBuffer,
});
return Promise.resolve();
}
return Promise.reject(new Error('Invalid object for preloading'));
};
};
/**
* @ignore
* @param {string} loadPath
* @param {Response} response
*/
Config.prototype.getModuleConfig = function (loadPath, response) {
let r = response;
const gdext = this.gdextensionLibs;
return {
'print': this.onPrint,
'printErr': this.onPrintError,
'thisProgram': this.executable,
'noExitRuntime': false,
'dynamicLibraries': [`${loadPath}.side.wasm`].concat(this.gdextensionLibs),
'instantiateWasm': function (imports, onSuccess) {
function done(result) {
onSuccess(result['instance'], result['module']);
}
if (typeof (WebAssembly.instantiateStreaming) !== 'undefined') {
WebAssembly.instantiateStreaming(Promise.resolve(r), imports).then(done);
} else {
r.arrayBuffer().then(function (buffer) {
WebAssembly.instantiate(buffer, imports).then(done);
});
}
r = null;
return {};
},
'locateFile': function (path) {
if (!path.startsWith('godot.')) {
return path;
} else if (path.endsWith('.worker.js')) {
return `${loadPath}.worker.js`;
} else if (path.endsWith('.audio.worklet.js')) {
return `${loadPath}.audio.worklet.js`;
} else if (path.endsWith('.js')) {
return `${loadPath}.js`;
} else if (path in gdext) {
return path;
} else if (path.endsWith('.side.wasm')) {
return `${loadPath}.side.wasm`;
} else if (path.endsWith('.wasm')) {
return `${loadPath}.wasm`;
}
return path;
},
};
};