Improve Performance

This commit is contained in:
TheBrokenRail 2024-02-08 00:34:43 -05:00
parent 28dbb2eacd
commit 6951ebe620
11 changed files with 127 additions and 0 deletions

View File

@ -102,6 +102,8 @@ else()
# text-input-box
src/text-input-box/TextInputBox.cpp
src/text-input-box/TextInputScreen.cpp
# fps
src/fps/fps.cpp
)
# Install Splashes
install(

View File

@ -0,0 +1,11 @@
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
extern double fps;
#ifdef __cplusplus
}
#endif

View File

@ -24,6 +24,7 @@ void init_touch();
void init_atlas();
void init_title_screen();
void init_skin();
void init_fps();
#endif
void init_textures();
void init_creative();

2
mods/src/fps/README.md Normal file
View File

@ -0,0 +1,2 @@
# `fps` Mod
This mod tracks the game's FPS.

54
mods/src/fps/fps.cpp Normal file
View File

@ -0,0 +1,54 @@
#include <libreborn/libreborn.h>
#include <symbols/minecraft.h>
#include <mods/init/init.h>
#include <mods/misc/misc.h>
#include <mods/feature/feature.h>
#include <mods/fps/fps.h>
// Track FPS
#define NANOSECONDS_IN_SECOND 1000000000ll
static long long int get_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
long long int a = (long long int) ts.tv_nsec;
long long int b = ((long long int) ts.tv_sec) * NANOSECONDS_IN_SECOND;
return a + b;
}
double fps = 0;
static void update_fps(__attribute__((unused)) Minecraft *minecraft) {
static bool is_last_update_time_set = false;
static long long int last_update_time;
long long int time = get_time();
if (is_last_update_time_set) {
long long int update_time = time - last_update_time;
fps = ((double) NANOSECONDS_IN_SECOND) / ((double) update_time);
} else {
is_last_update_time_set = true;
}
last_update_time = time;
}
// Print FPS
static void print_fps(__attribute__((unused)) Minecraft *minecraft) {
// Track Ticks
static int ticks = 0;
ticks++;
if (ticks == 20) {
// One Second Has Passed, Reset
ticks = 0;
}
// Print
if (ticks == 0) {
INFO("FPS: %f", fps);
}
}
// Init
void init_fps() {
if (feature_has("Track FPS", server_disabled)) {
misc_run_on_update(update_fps);
misc_run_on_tick(print_fps);
}
}

View File

@ -23,6 +23,7 @@ __attribute__((constructor)) static void init(int argc, char *argv[]) {
init_atlas();
init_title_screen();
init_skin();
init_fps();
#endif
init_textures();
init_creative();

View File

@ -1,6 +1,7 @@
#include <string>
#include <fstream>
#include <streambuf>
#include <algorithm>
#include <cstring>
@ -90,6 +91,38 @@ ItemInstance *Item_getCraftingRemainingItem_injection(Item *self, ItemInstance *
return NULL;
}
// Sort Chunks
struct chunk_data {
Chunk *chunk;
float distance;
};
#define MAX_CHUNKS_SIZE 24336
chunk_data data[MAX_CHUNKS_SIZE];
static void sort_chunks(Chunk **chunks_begin, Chunk **chunks_end, DistanceChunkSorter sorter) {
// Calculate Distances
int chunks_size = chunks_end - chunks_begin;
if (chunks_size > MAX_CHUNKS_SIZE) {
IMPOSSIBLE();
}
for (int i = 0; i < chunks_size; i++) {
Chunk *chunk = chunks_begin[i];
float distance = Chunk_distanceToSqr(chunk, (Entity *) sorter.mob);
if ((1024.0 <= distance) && chunk->y < 0x40) {
distance = distance * 10.0;
}
data[i].chunk = chunk;
data[i].distance = distance;
}
// Sort
std::sort(data, data + chunks_size, [](chunk_data &a, chunk_data &b) {
return a.distance < b.distance;
});
for (int i = 0; i < chunks_size; i++) {
chunks_begin[i] = data[i].chunk;
}
}
// Init
void _init_misc_cpp() {
// Implement AppPlatform::readAssetFile So Translations Work
@ -106,4 +139,7 @@ void _init_misc_cpp() {
// Implement crafting remainders
overwrite_call((void *) 0x2e230, (void *) PaneCraftingScreen_craftSelectedItem_PaneCraftingScreen_recheckRecipes_injection);
overwrite((void *) Item_getCraftingRemainingItem_non_virtual, (void *) Item_getCraftingRemainingItem_injection);
// Replace 2011 std::sort With Optimized(TM) Code
overwrite((void *) 0x51fac, (void *) sort_chunks);
}

View File

@ -51,3 +51,11 @@ Who is StevePi?
Why must you hurt me?
Wow! Modded MCPI!
mcpi-revival.github.io!
discord.gg/aDqejQGMMy!
Buckets!
Caves!
Multiplayer!
Where's my sound?
Animated Lava!
Made with Ghidra!
Python API!

View File

@ -78,6 +78,8 @@ set(SRC
src/level/Dimension.def
src/level/MultiPlayerLevel.def
src/level/LevelSummary.def
src/level/DistanceChunkSorter.def
src/level/Chunk.def
src/item/ItemRenderer.def
src/item/ItemInHandRenderer.def
src/item/AuxDataTileItem.def

View File

@ -0,0 +1,5 @@
property int x = 0x4;
property int y = 0x8;
property int z = 0xc;
method float distanceToSqr(Entity *entity) = 0x47ba0;

View File

@ -0,0 +1,5 @@
size 0x4;
property Mob *mob = 0x0;
method bool compare(Chunk *param_1, Chunk *param_2) = 0x5118c; // Actually Called _ZN19DistanceChunkSorterclEPK5ChunkS2_