Skip to content

Commit f8c2734

Browse files
committed
Make Il2Cpp::application.version fallback to IL2CPP module hash
1 parent 4d5fb6a commit f8c2734

6 files changed

Lines changed: 40 additions & 24 deletions

File tree

src/application.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ namespace Il2Cpp {
4040
/**
4141
* Gets the version name of the current application, e.g. `4.12.8`.
4242
*
43-
* **This information is not guaranteed to exist.**
43+
* In case the version cannot be retrieved, an hash of the IL2CPP
44+
* module is returned instead.
4445
*
4546
* ```ts
4647
* Il2Cpp.perform(() => {
@@ -49,8 +50,8 @@ namespace Il2Cpp {
4950
* });
5051
* ```
5152
*/
52-
get version(): string | null {
53-
return unityEngineCall("get_version");
53+
get version(): string {
54+
return unityEngineCall("get_version") ?? exportsHash(Il2Cpp.module).toString(16);
5455
}
5556
};
5657

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/// <reference path="./utils/console.ts">/>
33
/// <reference path="./utils/decorate.ts">/>
44
/// <reference path="./utils/getter.ts">/>
5+
/// <reference path="./utils/hash.ts">/>
56
/// <reference path="./utils/lazy.ts">/>
67
/// <reference path="./utils/native-struct.ts">/>
78
/// <reference path="./utils/offset-of.ts">/>

src/tracer.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -369,24 +369,4 @@ namespace Il2Cpp {
369369

370370
return new Il2Cpp.Tracer(applier());
371371
}
372-
373-
/** https://stackoverflow.com/a/52171480/16885569 */
374-
function cyrb53(str: string): number {
375-
let h1 = 0xdeadbeef;
376-
let h2 = 0x41c6ce57;
377-
378-
for (let i = 0, ch; i < str.length; i++) {
379-
ch = str.charCodeAt(i);
380-
h1 = Math.imul(h1 ^ ch, 2654435761);
381-
h2 = Math.imul(h2 ^ ch, 1597334677);
382-
}
383-
384-
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
385-
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
386-
387-
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
388-
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
389-
390-
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
391-
}
392372
}

src/utils/hash.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/** @internal https://stackoverflow.com/a/52171480/16885569 */
2+
function cyrb53(str: string): number {
3+
let h1 = 0xdeadbeef;
4+
let h2 = 0x41c6ce57;
5+
6+
for (let i = 0, ch; i < str.length; i++) {
7+
ch = str.charCodeAt(i);
8+
h1 = Math.imul(h1 ^ ch, 2654435761);
9+
h2 = Math.imul(h2 ^ ch, 1597334677);
10+
}
11+
12+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
13+
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
14+
15+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
16+
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
17+
18+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
19+
}
20+
21+
/** @internal */
22+
function exportsHash(module: Module): number {
23+
return cyrb53(
24+
module
25+
.enumerateExports()
26+
.sort((a, b) => a.name.localeCompare(b.name))
27+
.map(_ => _.name + _.address.sub(module.base))
28+
.join("")
29+
);
30+
}

test/agent/src/assert.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class Assertion<T> {
3737
}
3838
}
3939

40-
not(unexpected: T) {
40+
not(unexpected: T | null) {
4141
if (eq(unexpected, this.actual)) {
4242
throw new AssertionError(`\x1b[1m${unexpected}\x1b[22m was not expected`);
4343
}

test/agent/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ Il2Cpp.perform(() => {
1111
assert(Il2Cpp.application.identifier).is("host");
1212
});
1313

14+
test("Application version is detected", () => {
15+
assert(Il2Cpp.application.version).not(null);
16+
});
17+
1418
test("Il2Cpp.Thread::id", () => {
1519
assert(Il2Cpp.currentThread?.id).is(Process.getCurrentThreadId());
1620
});

0 commit comments

Comments
 (0)