Skip to content

Commit

Permalink
优化prof显示效果,给hsa draw命令增加开关
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohengying committed Jan 16, 2022
1 parent cecf9dd commit 77862ea
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 40 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ target_include_directories(${MOD_API_NAME}
target_link_libraries(${MOD_API_NAME} ${PROJECT_SOURCE_DIR}/api/lib/detours.lib)

####################这里是具体功能##########################
set(TRAPDOOR_VERSION 0.9.91)
set(TRAPDOOR_VERSION 0.9.93)
set(MCBE_VERSION 1.18.2.03)
set(BETA OFF)
set(TEST_NUMBER 1)
Expand Down
79 changes: 79 additions & 0 deletions api/lib/seh_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once
/**
* @Description: a class for convert SEH exception to c++ exception
* @Author: shang_cm
* @Date: 2020-06-01 16:09:00
* @LastEditTime: 2010-06-01 16:09:00
* @LastEditors: shang_cm
*/
#pragma once
#include <windows.h>

#include <exception>

class seh_exception : std::exception {
typedef ULONG(WINAPI* fpRtlNtStatusToDosError)(DWORD Status);

public:
seh_exception(unsigned int nExceptionCode,
_EXCEPTION_POINTERS* pstExcptionInfor)
: m_nExceptionCode(0),
m_pExcptionInfor(NULL),
m_szMsgBuff(NULL),
m_hNtModule(NULL),
RtlNtStatusToDosError(NULL) {
m_nExceptionCode = nExceptionCode;
m_pExcptionInfor = pstExcptionInfor;
m_hNtModule = GetModuleHandle(L"NTDLL.DLL");
if (NULL != m_hNtModule) {
RtlNtStatusToDosError = (fpRtlNtStatusToDosError)GetProcAddress(
m_hNtModule, "RtlNtStatusToDosError");
}
}

virtual ~seh_exception() {
m_nExceptionCode = 0;
m_pExcptionInfor = NULL;
RtlNtStatusToDosError = NULL;

if (NULL != m_szMsgBuff) {
LocalFree(m_szMsgBuff);
m_szMsgBuff = NULL;
}
};

const char* what() const noexcept {
if (RtlNtStatusToDosError != NULL) {
DWORD nConvertLen = FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_FROM_HMODULE,
m_hNtModule, RtlNtStatusToDosError(m_nExceptionCode), 0,
(char*)&m_szMsgBuff, 0, NULL);

if (0 != nConvertLen) {
return m_szMsgBuff;
}
}

return "SEH_UNKNOW_ERROR";
}

const PEXCEPTION_POINTERS info() const { return m_pExcptionInfor; }

const unsigned int code() const { return m_nExceptionCode; }

private:
HMODULE m_hNtModule;

unsigned int m_nExceptionCode;
char* m_szMsgBuff;
PEXCEPTION_POINTERS m_pExcptionInfor;
fpRtlNtStatusToDosError RtlNtStatusToDosError;

public:
static void(__cdecl TranslateSEHtoCE)(
unsigned int nExceptionCode,
struct _EXCEPTION_POINTERS* pstExcptionInfor) {
throw seh_exception(nExceptionCode, pstExcptionInfor);
}
};
8 changes: 4 additions & 4 deletions api/tools/MsgBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace trapdoor {
const uint8_t MessageBuilder::BLACK = 0x0;
const uint8_t MessageBuilder::DARK_GREEN = 0x2;
const uint8_t MessageBuilder::DARK_READ = 0x4;
const uint8_t MessageBuilder::DARK_RED = 0x4;
const uint8_t MessageBuilder::GOLD = 0x6;
const uint8_t MessageBuilder::GRAY = 0x7;
const uint8_t MessageBuilder::BLUE = 0x9;
Expand All @@ -27,9 +27,9 @@ namespace trapdoor {
const uint8_t MessageBuilder::RESET = 0x30;

const std::map<uint8_t, std::string> MessageBuilder::STYLE_MAP = {
{BLACK, "§0"}, {DARK_GREEN, "§2"}, {DARK_READ, "§4"}, {GOLD, "§6"},
{GRAY, "§7"}, {BLUE, "§9"}, {GREEN, "§a"}, {AQUA, "§b"},
{RED, "§c"}, {LIGHT_PURPLE, "§d"}, {YELLOW, "§e"}, {WHITE, "§f"},
{BLACK, "§0"}, {DARK_GREEN, "§2"}, {DARK_RED, "§4"}, {GOLD, "§6"},
{GRAY, "§7"}, {BLUE, "§9"}, {GREEN, "§a"}, {AQUA, "§b"},
{RED, "§c"}, {LIGHT_PURPLE, "§d"}, {YELLOW, "§e"}, {WHITE, "§f"},
{BOLD, "§l"}, {ITALIC, "§o"}, {RESET, "§r"}};

MessageBuilder &MessageBuilder::text(const std::string &s) {
Expand Down
2 changes: 1 addition & 1 deletion api/tools/MsgBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace trapdoor {

class MessageBuilder {
public:
static const uint8_t DARK_READ;
static const uint8_t DARK_RED;
static const uint8_t RED;
static const uint8_t GOLD;
static const uint8_t YELLOW;
Expand Down
2 changes: 2 additions & 0 deletions mod/TrapdoorMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ namespace mod {

inline HsaManager &getHsaManager() { return this->hsaManager; }

inline SimpleBuilder &getSimpleBuilder() { return this->simpleBuilder; }

inline std::string getLevelName() {
return this->configManager.getServerConfig().levelName;
}
Expand Down
4 changes: 2 additions & 2 deletions mod/config/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ namespace mod {
struct FunctionEnableConfig {
bool hopperCounter = true; //漏斗计数器
bool explosion = true; //爆炸破坏地形
bool spawnHelper = true; //刷怪指示器
bool spawnHelper = false; //刷怪指示器
bool cactusRotation = false; //仙人掌转方块
bool playerStat = false; //玩家行为统计
bool simpleDraw = true; //画圆
bool simpleDraw = false; //画圆
};

struct SelfEnableConfig {
Expand Down
2 changes: 1 addition & 1 deletion mod/function/SimpleBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace mod {

public:
inline void setAble(bool able) { this->enable = able; }

inline bool isAble() const { return this->enable; }
inline void setMaxRadius(int radius) { this->maxCircleRadius = radius; }

void buildSphere(trapdoor::Actor *player, size_t size,
Expand Down
10 changes: 7 additions & 3 deletions mod/spawn/HsaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,16 @@ namespace mod {
pos = trapdoor::BlockPos(x, y, z);
}
}
broadcastMsg("N=%d", maxPoints);
broadcastMsg("N = %d", maxPoints);
return pos;
}

void HsaManager::draw(trapdoor::Actor *player) {
auto mod = bdsMod->asInstance<mod::TrapdoorMod>();
if (!mod->getSimpleBuilder().isAble()) {
trapdoor::info(player, "builder未开启");
return;
}
trapdoor::BlockPos standPos = player->getStandPosition();
auto block = player->getBlockSource()->getBlock(standPos.x, standPos.y,
standPos.z);
Expand Down Expand Up @@ -170,7 +175,7 @@ THook(void, Spawner_spawnStructureMob_98a1693e, void *spawner,
auto type = biome->getBiomeType();
if (type == 5 || type == 18) {
info.type = mod::StructureType::NetherFortress;
info.dimensionID = 1;
info.dimensionID = trapdoor::Nether;
} else if (type == 10) {
info.type = mod::StructureType::OceanMonument;
} else if (type == 15) {
Expand All @@ -181,6 +186,5 @@ THook(void, Spawner_spawnStructureMob_98a1693e, void *spawner,

hsaManager.insert(info);
blockSource->getBiome(blockPos);

modInstance->getHsaManager().insert(info);
}
80 changes: 52 additions & 28 deletions mod/tick/SimpleProfiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "SimpleProfiler.h"

#include "DirtyLogger.h"
#include "Message.h"
#include "entity/Actor.h"
#include "tools/MsgBuilder.h"
Expand All @@ -14,12 +15,25 @@ namespace mod {

void addShowItem(trapdoor::MessageBuilder &builder,
const std::string &prefix, const std::string &key,
float value) {
builder.sText(prefix, trapdoor::MSG_COLOR::GRAY)
.sText(key + ": ", trapdoor::MSG_COLOR::WHITE)
.sTextF(
trapdoor::MSG_COLOR::WHITE | trapdoor::MessageBuilder::BOLD,
"%.3fms\n", value);
float value, float mspt) {
builder.sText(prefix, trapdoor::MSG_COLOR::GRAY);
L_DEBUG("%.3f, %.3f\n", value, mspt);
auto color = trapdoor::MSG_COLOR::WHITE;
if (mspt > 40 && mspt <= 50) {
if (value / mspt > 0.25) {
color = trapdoor::MSG_COLOR::YELLOW;
}
} else if (mspt > 50) {
auto ratio = value / mspt;
if (ratio > 0.2 && ratio < 0.4) {
color = trapdoor::MSG_COLOR::YELLOW;
} else if (ratio >= 0.4) {
color = trapdoor::MSG_COLOR::RED;
}
}
builder.sTextF(color, key + ": ", trapdoor::MSG_COLOR::WHITE)
.sTextF(color | trapdoor::MessageBuilder::BOLD, "%.3fms\n",
value);
}
} // namespace

Expand All @@ -36,45 +50,55 @@ namespace mod {

void SimpleProfiler::print() const {
auto rounds = static_cast<float>(this->totalRound * 1000);
int tps = static_cast<int>(1000.0 / (serverLevelTickTime / rounds));
float mspt = serverLevelTickTime / rounds;
int tps = static_cast<int>(1000.0 / mspt);
if (tps > 20) tps = 20;
trapdoor::MessageBuilder builder;
auto totalRedstoneTickTime =
static_cast<float>(redstoneTickTime + redstonePendingUpdateTime +
redstonePendingRemoveTime) /
rounds;

builder.textF("MSPT: ");
auto color =
tps < 20 ? trapdoor::MSG_COLOR::RED : trapdoor::MSG_COLOR::WHITE;
builder.sTextF(color, "%.3fms", serverLevelTickTime / rounds)
.text(" TPS: ")
auto firstLineColor =
trapdoor::MSG_COLOR::WHITE || trapdoor::MSG_COLOR::BOLD;
builder.sTextF(firstLineColor, "MSPT: ");

auto color = trapdoor::MSG_COLOR::WHITE;
if (mspt > 40 && mspt <= 50) {
color = trapdoor::MSG_COLOR::YELLOW;
} else if (mspt > 50) {
color = trapdoor::MSG_COLOR::RED;
}
color |= trapdoor::MSG_COLOR::BOLD;

builder.sTextF(color, "%.3fms", mspt)
.sTextF(firstLineColor, " TPS: ")
.sTextF(color, "%d", tps)
.textF(" Chunks: %d\n", tickChunkNum / this->totalRound);
addShowItem(builder, " - ", "Redstone", totalRedstoneTickTime);
addShowItem(builder, " - ", "SignalUpdate",
redstoneTickTime / rounds);
.sTextF(firstLineColor, " CHUNKS: %d\n",
tickChunkNum / this->totalRound);
addShowItem(builder, " - ", "Redstone", totalRedstoneTickTime, mspt);

addShowItem(builder, " - ", "SignalUpdate",
redstoneTickTime / rounds, mspt);
builder.sText(" - ", trapdoor::MSG_COLOR::GRAY)
.text("PendingUpdate: Invalid\n");
addShowItem(builder, " - ", "PendingRemove",
redstonePendingRemoveTime / rounds);
addShowItem(builder, " - ", "PendingRemove",
redstonePendingRemoveTime / rounds, mspt);

addShowItem(builder, " - ", "EntitySystem",
levelEntitySystemTickTime / rounds);
levelEntitySystemTickTime / rounds, mspt);
addShowItem(builder, " - ", "Chunk (un)load & village",
dimensionTickTime / rounds);

addShowItem(builder, " - ", "ChunkTick", chunkTickTime / rounds);
dimensionTickTime / rounds, mspt);

addShowItem(builder, " - ", "BlockEntities",
chunkBlockEntityTickTime / rounds);
addShowItem(builder, " - ", "RandomTick",
chunkRandomTickTime / rounds);
addShowItem(builder, " - ", "ChunkTick", chunkTickTime / rounds, mspt);

addShowItem(builder, " - ", "PendingTick",
chunkPendingTickTime / rounds);
addShowItem(builder, " - ", "BlockEntities",
chunkBlockEntityTickTime / rounds, mspt);
addShowItem(builder, " - ", "RandomTick",
chunkRandomTickTime / rounds, mspt);

addShowItem(builder, " - ", "PendingTick",
chunkPendingTickTime / rounds, mspt);
builder.broadcast();

// .textF(" - Chunk tick: %.3fms (%d)\n", chunkTickTime / rounds,
Expand Down

0 comments on commit 77862ea

Please sign in to comment.