C++: Add il2cppi_to_string() helpers

Refactor other helper names to il2cppi_*
This commit is contained in:
Katy Coe
2020-09-09 17:18:05 +02:00
parent deba3035fc
commit 64c7310e28
2 changed files with 47 additions and 24 deletions

View File

@@ -150,21 +150,24 @@ BOOL APIENTRY DllMain( HMODULE hModule,
<value>// Generated C++ file by Il2CppInspector - http://www.djkaty.com - https://github.com/djkaty
// Helper functions
#include "pch-il2cpp.h"
#define WIN32_LEAN_AND_MEAN
#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;codecvt&gt;
#include "helpers.h"
// Log file location
extern const LPCWSTR LOG_FILE;
// Helper function to get the module base address
uintptr_t GetBaseAddress() {
uintptr_t il2cppi_get_base_address() {
return (uintptr_t) GetModuleHandleW(L"GameAssembly.dll");
}
// Helper function to append text to a file
void LogWrite(std::string text) {
void il2cppi_log_write(std::string text) {
HANDLE hfile = CreateFileW(LOG_FILE, FILE_APPEND_DATA, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hfile == INVALID_HANDLE_VALUE)
@@ -177,9 +180,21 @@ void LogWrite(std::string text) {
}
// Helper function to open a new console window and redirect stdout there
void NewConsole() {
void il2cppi_new_console() {
AllocConsole();
freopen_s((FILE**) stdout, "CONOUT$", "w", stdout);
}
// Helper function to convert Il2CppString to std::string
std::string il2cppi_to_string(Il2CppString* str) {
std::u16string u16(reinterpret_cast&lt;const char16_t*&gt;(str-&gt;chars));
return std::wstring_convert&lt;std::codecvt_utf8_utf16&lt;char16_t&gt;, char16_t&gt;{}.to_bytes(u16);
}
// Helper function to convert System.String to std::string
std::string il2cppi_to_string(app::String* str) {
std::u16string u16(reinterpret_cast&lt;const char16_t*&gt;(&amp;str-&gt;fields.m_firstChar));
return std::wstring_convert&lt;std::codecvt_utf8_utf16&lt;char16_t&gt;, char16_t&gt;{}.to_bytes(u16);
}</value>
</data>
<data name="Cpp-HelpersH" xml:space="preserve">
@@ -193,13 +208,19 @@ void NewConsole() {
#include &lt;iomanip&gt;
// Helper function to get the module base address
uintptr_t GetBaseAddress();
uintptr_t il2cppi_get_base_address();
// Helper function to append text to a file
void LogWrite(std::string text);
void il2cppi_log_write(std::string text);
// Helper function to open a new console window and redirect stdout there
void NewConsole();
void il2cppi_new_console();
// Helper function to convert Il2CppString to std::string
std::string il2cppi_to_string(Il2CppString* str);
// Helper function to convert System.String to std::string
std::string il2cppi_to_string(app::String* str);
// Helper function to convert a pointer to hex
template&lt;typename T&gt; std::string to_hex_string(T i) {
@@ -237,11 +258,11 @@ extern const LPCWSTR LOG_FILE = L"il2cpp-log.txt";
// Custom injected code entry point
void Run()
{
// If you would like to write to a log file, specify the name above and use LogWrite()
// LogWrite("Startup");
// If you would like to write to a log file, specify the name above and use il2cppi_log_write()
// il2cppi_log_write("Startup");
// If you would like to output to a new console window, use NewConsole() to open one and redirect stdout
// NewConsole();
// If you would like to output to a new console window, use il2cppi_new_console() to open one and redirect stdout
// il2cppi_new_console();
// Place your custom code here
}</value>
@@ -1198,7 +1219,7 @@ namespace app {
void init_il2cpp()
{
// Get base address of IL2CPP module
uintptr_t baseAddress = GetBaseAddress();
uintptr_t baseAddress = il2cppi_get_base_address();
using namespace app;