Skip to content

Commit

Permalink
game api stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Auto committed Oct 29, 2023
1 parent b64d34c commit 1c425b4
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 140 deletions.
32 changes: 32 additions & 0 deletions src/game_api/game_api.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "game_api.hpp"

#include "search.hpp"

GameAPI* GameAPI::get()
{
using GetGameAPI = GameAPI*();
static auto addr = (GetGameAPI*)get_address("get_game_api");
return addr();
}

float GameAPI::get_current_zoom()
{
return renderer->current_zoom + renderer->current_zoom_offset;
}

float GameAPI::get_target_zoom()
{
return renderer->target_zoom + renderer->target_zoom_offset;
}

void GameAPI::set_zoom(std::optional<float> current, std::optional<float> target)
{
if (current.has_value())
{
renderer->current_zoom = current.value(); // - renderer->current_zoom_offset;
}
if (target.has_value())
{
renderer->target_zoom = target.value(); // - renderer->target_zoom_offset;
}
}
72 changes: 72 additions & 0 deletions src/game_api/game_api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <cstddef>
#include <cstdint>
#include <optional>

struct Renderer
{
uint32_t render_width; // sam as window size unless resolution scale is set
uint32_t render_height;

uint32_t fps; // changing it doesn't seam to do anything
uint32_t fps_denominator;

uint32_t render_width2; // repeat
uint32_t render_height2;

uint8_t flags1;
uint8_t flags2;
uint8_t padding[6];

uint8_t skip[0x1228]; // tons of pointers

uint8_t skip2[0x7F284]; // a lot of nothing

float current_zoom;
float target_zoom;
float target_zoom_offset;
float current_zoom_offset;
float backlayer_light_level; // constantly overwritten by theme virtual get_backlayer_light_level
uint8_t unknown2;
uint8_t unknown3;
uint16_t unknown4;

uint8_t skip3[0xAE4];

size_t swap_chain;

// somewhere there should be shareds stored

// added just to have the vtable
virtual ~Renderer() = 0;
virtual void some_dx_stuff() = 0; // it actually has a ton of parameters
};

struct GameAPI // size 0x60
{
static GameAPI* get();

float get_current_zoom();
float get_target_zoom();

void set_zoom(std::optional<float> current, std::optional<float> target);

bool unknown1;
size_t unknown2; // pointer
Renderer* renderer;
uint32_t window_width;
uint32_t window_height;

size_t unknown5; // garbage?
size_t unknown6; // exe start
size_t unknown7; // some offset
size_t unknown8; // garbage?
size_t SteamAPI_Callback; // just vtable? don't know much about steam stuff

uint8_t unknown10a; // bool ?
uint32_t unknown10b;

size_t unknown11; // garbage?
size_t unknown12; // garbage?
};
34 changes: 9 additions & 25 deletions src/game_api/render_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <vector> // for vector

#include "entity.hpp" // for Entity, EntityDB
#include "game_api.hpp" //
#include "level_api.hpp" // for ThemeInfo
#include "logger.h" // for DEBUG
#include "memory.hpp" // for memory_read, to_le_bytes, write_mem_prot
Expand All @@ -31,21 +32,20 @@ struct Layer;

RenderAPI& RenderAPI::get()
{
static RenderAPI render_api = []()
{
return RenderAPI{(size_t*)get_address("render_api_callback"sv), get_address("render_api_offset"sv)};
}();
static RenderAPI render_api;
return render_api;
}

size_t RenderAPI::renderer() const
Renderer* RenderAPI::renderer() const
{
return memory_read<uint64_t>(*api + 0x10);
auto game_api = GameAPI::get();
return game_api->renderer;
}

size_t RenderAPI::swap_chain() const
{
return memory_read<uint64_t>(renderer() + swap_chain_off);
return renderer()->swap_chain;
// return memory_read<uint64_t>(renderer() + swap_chain_off); // swap_chain_off from pattern: render_api_offset
}

void (*g_post_render_game)(){nullptr};
Expand All @@ -64,7 +64,6 @@ void render_loading(size_t param_1)
}

std::optional<TEXTURE> g_forced_lut_textures[2]{};
float g_layer_zoom_offset[2]{0};

using RenderLayer = void(const std::vector<Illumination*>&, uint8_t, const Camera&, const char**, const char**);
RenderLayer* g_render_layer_trampoline{nullptr};
Expand All @@ -73,16 +72,6 @@ void render_layer(const std::vector<Illumination*>& lightsources, uint8_t layer,
if (trigger_vanilla_render_layer_callbacks(ON::RENDER_PRE_LAYER, layer))
return;

static size_t offset = 0;
if (offset == 0)
{
auto addr = State::get_zoom_level_address();
offset = addr + 8;
}
if (offset != 0)
{
g_layer_zoom_offset[layer] = memory_read<float>(offset);
}
// The lhs and rhs LUTs are blended in the shader, but we don't know where that value is CPU side so we can only override
// with a single LUT for now
if (g_forced_lut_textures[layer])
Expand Down Expand Up @@ -117,11 +106,6 @@ void render_game(StateMemory* state)
trigger_vanilla_render_callbacks(ON::RENDER_POST_GAME);
}

float get_layer_zoom_offset(uint8_t layer)
{
return g_layer_zoom_offset[layer];
}

void RenderAPI::set_lut(TEXTURE texture_id, uint8_t layer)
{
g_forced_lut_textures[layer] = texture_id;
Expand Down Expand Up @@ -482,7 +466,7 @@ void RenderAPI::draw_world_texture(Texture* texture, Quad source, Quad dest, Col
dest.top_left_y,
unknown};

typedef void render_func(size_t, WorldShader, const char*** texture_name, uint32_t render_as_non_liquid, float* destination, Quad* source, void*, Color*, float*);
typedef void render_func(Renderer*, WorldShader, const char*** texture_name, uint32_t render_as_non_liquid, float* destination, Quad* source, void*, Color*, float*);
static render_func* rf = (render_func*)(func_offset);
auto texture_name = texture->name;
rf(renderer(), shader, &texture_name, 1, destination, &source, (void*)param_7, &color, nullptr);
Expand All @@ -500,7 +484,7 @@ void RenderAPI::set_advanced_hud()

void RenderAPI::reload_shaders()
{
using ReloadShadersFun = void(size_t);
using ReloadShadersFun = void(Renderer*);
static ReloadShadersFun* reload_shaders_impl = (ReloadShadersFun*)get_address("reload_shaders"sv);
reload_shaders_impl(renderer());
}
Expand Down
7 changes: 2 additions & 5 deletions src/game_api/render_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
struct JournalUI;
struct Layer;
class Entity;
struct Renderer;

using VANILLA_TEXT_ALIGNMENT = uint32_t;
using VANILLA_FONT_STYLE = uint32_t;
Expand Down Expand Up @@ -225,9 +226,6 @@ struct TextureRenderingInfo

struct RenderAPI
{
const size_t* api;
size_t swap_chain_off;

mutable std::mutex custom_textures_lock;
std::unordered_map<TEXTURE, Texture> custom_textures;
std::unordered_map<TEXTURE, Texture> original_textures;
Expand All @@ -236,7 +234,7 @@ struct RenderAPI

static RenderAPI& get();

size_t renderer() const;
Renderer* renderer() const;
size_t swap_chain() const;

void set_lut(TEXTURE texture_id, uint8_t layer);
Expand Down Expand Up @@ -366,7 +364,6 @@ struct RenderInfo
void init_render_api_hooks();
bool& get_journal_enabled();
void on_open_journal_chapter(JournalUI* journal_ui, uint8_t chapter, bool instant, bool play_sound);
float get_layer_zoom_offset(uint8_t layer);
void render_draw_depth(Layer* layer, uint8_t draw_depth, float bbox_left, float bbox_bottom, float bbox_right, float bbox_top);

struct HudInventory
Expand Down
17 changes: 10 additions & 7 deletions src/game_api/script/lua_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "entities_items.hpp" // for Container, Player...
#include "entity.hpp" // for get_entity_ptr
#include "entity_lookup.hpp" //
#include "game_api.hpp" //
#include "game_manager.hpp" // for get_game_manager
#include "handle_lua_function.hpp" // for handle_function
#include "illumination.hpp" //
Expand Down Expand Up @@ -671,27 +672,26 @@ end
lua["read_prng"] = []() -> std::vector<int64_t>
{ return read_prng(); };

using Toast = void(const char16_t*);
using Say = void(size_t, Entity*, const char16_t*, int, bool);

/// Show a message that looks like a level feeling.
lua["toast"] = [](std::u16string message)
{
using Toast = void(const char16_t*);
static Toast* toast_fun = (Toast*)get_address("toast");
toast_fun(message.c_str());
};
/// Show a message coming from an entity
lua["say"] = [](uint32_t entity_uid, std::u16string message, int sound_type, bool top)
{
using Say = void(HudData*, Entity*, const char16_t*, int, bool);
static auto say = (Say*)get_address("speech_bubble_fun");
static const auto say_context = get_address("say_context");
const auto hud = get_hud();

auto entity = get_entity_ptr(entity_uid);

if (entity == nullptr)
return;

say(say_context, entity, message.c_str(), sound_type, top);
say(hud, entity, message.c_str(), sound_type, top);
};
/// Add an integer option that the user can change in the UI. Read with `options.name`, `value` is the default. Keep in mind these are just soft
/// limits, you can override them in the UI with double click.
Expand Down Expand Up @@ -1108,7 +1108,7 @@ end
/// Set the `more_flags` field from entity by uid
lua["set_entity_flags2"] = set_entity_flags2;
/// Deprecated
/// As the name is misleading. use entity `move_state` field instead
/// As the name is misleading. use Movable.`move_state` field instead
lua["get_entity_ai_state"] = get_entity_ai_state;
/// Get `state.level_flags`
lua["get_level_flags"] = get_level_flags;
Expand All @@ -1118,7 +1118,10 @@ end
lua["get_entity_type"] = get_entity_type;
/// Get the current set zoom level
lua["get_zoom_level"] = []() -> float
{ return State::get_zoom_level(); };
{
auto game_api = GameAPI::get();
return game_api->get_current_zoom();
};
/// Get the game coordinates at the screen position (`x`, `y`)
lua["game_position"] = [](float x, float y) -> std::pair<float, float>
{ return State::click_position(x, y); };
Expand Down
Loading

0 comments on commit 1c425b4

Please sign in to comment.