Skip to content

Commit

Permalink
Display a little ImGUI frame on the screen, part #1
Browse files Browse the repository at this point in the history
  • Loading branch information
shadergz committed Aug 2, 2023
1 parent 3482a5a commit f21e7ca
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 55 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ add_library(saclient MODULE)

# https://developer.android.com/ndk/guides/cpp-support#libc
# RTTI is enable by default for ensure a better and safety objects handle
target_compile_options(saclient PRIVATE -Wall -Werror -frtti
target_compile_options(saclient PRIVATE -Wall -Werror -frtti
$<$<CONFIG:Release>:-Ofast;-fno-stack-protector;-DNDEBUG>)
target_link_options(saclient PRIVATE $<$<CONFIG:Release>:-s;-Wl,--gc-sections>)

Expand All @@ -33,8 +33,7 @@ target_sources(saclient PRIVATE
render/engine.cpp

game/game_objects.cpp
game/hud_manager.cpp
game/state.cpp
game/clock.cpp

ui/user_graphics.cpp
ui/imgui_rw.cpp)
Expand Down
23 changes: 23 additions & 0 deletions game/clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <game/game_objects.h>
#include <log_client.h>

namespace saglobal {
void (*g_CClock_Update)(uint64_t rX0);
}

namespace samimic {

// Updates the game clock every second based on the current FPS, which could be either after 60 frames or 30 frames, depending on the situation
void CClock_Update(uint64_t rX0)
{
static uint64_t callTimes{};
salog::printFormat(salog::Debug,
"Game world clock has been incrementing since the beginning by a factor of %ld", callTimes++);

saglobal::g_CClock_Update(rX0);

if (saglobal::g_playerUi)
saglobal::g_playerUi->renderByEachGameSecond();
}
}
4 changes: 2 additions & 2 deletions game/game_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

namespace samimic {
uint64_t CGame_InitializeRenderWare();
void Render2dStuff();
void CClock_Update(uint64_t rX0);
}

namespace saglobal {
extern uintptr_t g_gameAddr;
extern UiClientUser* g_playerUi;

extern uint64_t (*g_CGame_InitializeRenderWare)();
extern void (*g_Render2dStuff)(void);
extern void (*g_CClock_Update)(uint64_t rX0);

}
10 changes: 0 additions & 10 deletions game/hud_manager.cpp

This file was deleted.

22 changes: 0 additions & 22 deletions game/state.cpp

This file was deleted.

7 changes: 5 additions & 2 deletions patches_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void AArch64Patcher::placeHookAt(const char* sbName, const uintptr_t method,

*saveIn = (uintptr_t)(trContext);

salog::printFormat(salog::Info, "Hook installed in addr %#llx by %#llx, (| %#llx | %u |)",
salog::printFormat(salog::Info, "HOOK installed in ADDR %#llx by %#llx, (| %#llx | %u |)",
method, replace, (uintptr_t)trContext & 0xffffffffff, hookableCtx->m_instCount);
}
void AArch64Patcher::unfuckPageRWX(uintptr_t unfuckAddr, uint64_t region_size)
Expand Down Expand Up @@ -137,9 +137,12 @@ namespace sapatch {
g_textureDatabase = new TextureDatabaseRuntime();
// MenuItem_add is no longer present
g_patcherMicro->placeHookAt("AddAllItems", g_gameAddr + 0x358010, (uintptr_t)samimic::MainMenuScreen_AddAllItems, (uintptr_t*)&saglobal::g_MainMenuScreen_AddAllItems);

g_patcherMicro->placeHookAt("InitRenderWare", g_gameAddr + 0x55b668, (uintptr_t)samimic::CGame_InitializeRenderWare, (uintptr_t*)&saglobal::g_CGame_InitializeRenderWare);

g_patcherMicro->placeHookAt("NVThreadSpawnProc", g_gameAddr + 0x332040, (uintptr_t)samimic::NVThreadSpawnProc, (uintptr_t*)&saglobal::g_NVThreadSpawnProc);
g_patcherMicro->placeHookAt("Render2dStuff", g_gameAddr + 0x5d8994, (uintptr_t)samimic::Render2dStuff, (uintptr_t*)&saglobal::g_Render2dStuff);

g_patcherMicro->placeHookAt("CClock::Update", g_gameAddr + 0x5c20a4, (uintptr_t)samimic::CClock_Update, (uintptr_t*)&saglobal::g_CClock_Update);
}

}
30 changes: 30 additions & 0 deletions plugin_jni.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include <sched.h>
#include <thread>
#include <signal.h>
#include <sys/ucontext.h>

#include <cstdlib>

Expand Down Expand Up @@ -64,6 +66,31 @@ extern "C" JNIEXPORT void JNICALL Java_com_rockstargames_gtasa_GTASA_JVMSaMobile
// This thread should fall to the end, before game itself starts
}

/*
static struct sigaction originSigSegv;
[[noreturn]] void segvSaHandler(int32_t signal, siginfo_t* info, void* ctxPtr)
{
auto segvContext{reinterpret_cast<ucontext_t*>(ctxPtr)};
const uint64_t faultAddress{segvContext->uc_mcontext.fault_address};
salog::printFormat(salog::Error, "--------\n\tSEG FAULT caught in address or page: %#p (invalid dereference)\n--------\nComputer state:", faultAddress);
salog::printFormat(salog::Error, "\tGTASA base library around: %#p", saglobal::g_gameAddr);
uint64_t PC{segvContext->uc_mcontext.pc};
salog::printFormat(salog::Error, "\tGame space region without base library, at: %#p", (PC & 0xffffff) - (saglobal::g_gameAddr & 0xffffff));
salog::printFormat(salog::Error, "\t1. Backtrace Program Counter at: Hex: %#p, Dec: %llu", PC, PC);
salog::printFormat(salog::Error, "\t2. Backtrace Stack Pointer at %#p", segvContext->uc_mcontext.sp);
sigaction(signal, &originSigSegv, nullptr);
raise(signal);
_Exit(1);
}
*/

extern "C" jint JNI_OnLoad(JavaVM* vm, [[maybe_unused]] void* reserved)
{
using namespace saglobal;
Expand Down Expand Up @@ -93,6 +120,9 @@ extern "C" jint JNI_OnLoad(JavaVM* vm, [[maybe_unused]] void* reserved)
g_gameAddr = safs::getLibrary("libGTASA.so");
g_audioBackend = safs::getLibrary("libOpenAL64.so");

//static const struct sigaction ourHandler{.sa_flags = SA_SIGINFO, .sa_sigaction = segvSaHandler};
//sigaction(SIGSEGV, &ourHandler, &originSigSegv);

SALOG_ASSERT(g_gameAddr && g_audioBackend, "Can't found a valid address space of GTASA and/or OpenAL, "
"SAMobile is being halted now :[");
salog::printFormat(salog::Info, "Native libraries base region address found in:\n"
Expand Down
30 changes: 23 additions & 7 deletions ui/imgui_rw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ std::vector<RwIm2DVertex> vertexBuffer;

void ImGui_ImplRenderWare_RenderDrawData([[maybe_unused]] ImDrawData* drawData)
{
salog::print(salog::Info, "Impl IMGUI RenderWare for data draw purposes, has been called");

const RwReal* nearScreenZ{reinterpret_cast<RwReal*>(saglobal::g_gameAddr + 0xe20868)};
const RwReal* recipNearClip{reinterpret_cast<RwReal*>(saglobal::g_gameAddr + 0xe20864)};

Expand Down Expand Up @@ -110,9 +112,11 @@ bool ImGui_ImplRenderWare_Init()
}

static RwRaster* fontRaster{nullptr};
static RwImage* fontPixels{};

static void ImGui_ImplRenderWare_CreateDeviceObjects()
{
salog::printFormat(salog::Info, "GUI: Creating new device objects");
auto& io{ImGui::GetIO()};
uint8_t* pxS;

Expand All @@ -122,11 +126,18 @@ static void ImGui_ImplRenderWare_CreateDeviceObjects()
io.Fonts->GetTexDataAsRGBA32(&pxS, &outWidth, &outHeight, &outBytesPerPixel);
outBytesPerPixel *= 8;

salog::printFormat(salog::Info, "GUI: Font atlas width (%d), height (%d)",
outWidth, outHeight, outBytesPerPixel);
salog::printFormat(salog::Info, "GUI: Font atlas pixels (%#lp), width (%d), height (%d)",
pxS, outWidth, outHeight);

RwImage* fontData{RwImageCreate(outWidth, outHeight, outBytesPerPixel)};
RwImageAllocatePixels(fontData);
salog::printFormat(salog::Info, "Font data allocated at %#p", fontData);

if (!fontData) {
salog::print(salog::Error, "RW couldn't allocate the desired image, quitting...");
std::terminate();
}

fontPixels = RwImageAllocatePixels(fontData);
/*
struct RwImage {
RwInt32 flags, width, height, depth, stride;
Expand All @@ -137,20 +148,22 @@ static void ImGui_ImplRenderWare_CreateDeviceObjects()

RwUInt8* fontPixelsDest{fontData->cpPixels};

for (uint32_t pixelIdx{0}; pixelIdx < fontData->height; pixelIdx++) {
memcpy(fontPixelsDest, pxS + fontData->stride * pixelIdx, fontData->stride);
for (uint32_t strideIdx{0}; strideIdx < fontData->height; strideIdx++) {
memcpy(fontPixelsDest, pxS + fontData->stride * strideIdx, fontData->stride);
fontPixelsDest += fontData->stride;
}

RwInt32 width,
height,
delph,
height,
delph,
flags;

RwImageFindRasterFormat(fontData, rwRASTERTYPETEXTURE, &width, &height, &delph, &flags);
RwRaster* preFontImage{RwRasterCreate(width, height, delph, flags)};

fontRaster = RwRasterSetFromImage(preFontImage, fontData);
salog::printFormat(salog::Info, "Font raster in %#p by %#p", fontRaster, fontPixels);

RwImageDestroy(fontData);

io.Fonts->TexID = reinterpret_cast<ImTextureID*>(fontRaster);
Expand All @@ -162,6 +175,9 @@ void ImGui_ImplRenderWare_Shutdown()
auto& io{ImGui::GetIO()};
// Destroy font Raster data
RwRasterDestroy(fontRaster);
// The font pixels are no longer needed
RwImageFreePixels(fontPixels);
fontPixels = nullptr;

fontRaster = nullptr;
io.Fonts->TexID = nullptr;
Expand Down
17 changes: 9 additions & 8 deletions ui/user_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,17 @@ UiClientUser::UiClientUser()
// Loading related fonts
auto gameDataDrive{reinterpret_cast<const char*>(saglobal::g_gameAddr + 0x8b46a8)};

static SaFont* const SaFontRefs[]{
[[maybe_unused]] static SaFont* const SaFontRefs[]{
&arialFont,
&sampAux3Font,
&userDefaultFont,
nullptr
};

for (auto font : SaFontRefs) {
if (!font)
break;
auto font{&arialFont};
//for (auto font : SaFontRefs) {
//if (!font)
//break;

std::snprintf(font->m_fontPathBuffer, std::size(font->m_fontPathBuffer),
"%sfonts/%s", gameDataDrive, font->m_fontName);
Expand All @@ -78,19 +79,19 @@ UiClientUser::UiClientUser()
std::terminate();
}

if (!isFound)
continue;
//if (!isFound)
// continue;

font->m_fontObject = io.Fonts->AddFontFromFileTTF(font->m_fontPathBuffer,
m_inScreenfontSize, nullptr, ranges);

m_loadedFonts.push_back(font);
salog::printFormat(salog::Info, "New SA font with name %s successful loaded", font->m_fontName);

}
//}
}

int UiClientUser::renderDrawCommand()
int UiClientUser::renderByEachGameSecond()
{
ImGui_ImplRenderWare_NewFrame();
ImGui::NewFrame();
Expand Down
2 changes: 1 addition & 1 deletion ui/user_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class UiClientUser {

~UiClientUser();

int renderDrawCommand();
int renderByEachGameSecond();
void renderClientDetails();

struct SaFont {
Expand Down

0 comments on commit f21e7ca

Please sign in to comment.