From 28819dede14b2159092ad50f83b7ecf1aaac9f64 Mon Sep 17 00:00:00 2001 From: GPUCode Date: Thu, 26 Oct 2023 23:29:05 +0300 Subject: [PATCH] code: Replace printf/scanf with type safe fmt --- src/Core/PS4/HLE/Graphics/graphics_render.cpp | 18 +++++++-------- src/Core/PS4/Linker.cpp | 6 ++--- src/Core/PS4/Loader/SymbolsResolver.cpp | 23 ++++++++----------- src/Emulator/Util/singleton.h | 2 +- src/Util/Disassembler.cpp | 15 ++++++------ src/Util/config.cpp | 9 ++++---- src/emulator.cpp | 20 ++++++++-------- src/main.cpp | 5 ++-- src/vulkan_util.cpp | 4 ++-- 9 files changed, 50 insertions(+), 52 deletions(-) diff --git a/src/Core/PS4/HLE/Graphics/graphics_render.cpp b/src/Core/PS4/HLE/Graphics/graphics_render.cpp index e51e3721..22829e7c 100644 --- a/src/Core/PS4/HLE/Graphics/graphics_render.cpp +++ b/src/Core/PS4/HLE/Graphics/graphics_render.cpp @@ -1,5 +1,5 @@ #include "graphics_render.h" - +#include #include "Emulator/Util/singleton.h" #include "emulator.h" @@ -60,7 +60,7 @@ void GPU::CommandBuffer::begin() const { auto result = vkBeginCommandBuffer(buffer, &begin_info); if (result != VK_SUCCESS) { - printf("vkBeginCommandBuffer failed\n"); + fmt::print("vkBeginCommandBuffer failed\n"); std::exit(0); } } @@ -70,7 +70,7 @@ void GPU::CommandBuffer::end() const { auto result = vkEndCommandBuffer(buffer); if (result != VK_SUCCESS) { - printf("vkEndCommandBuffer failed\n"); + fmt::print("vkEndCommandBuffer failed\n"); std::exit(0); } } @@ -96,7 +96,7 @@ void GPU::CommandBuffer::executeWithSemaphore() { m_execute = true; if (result != VK_SUCCESS) { - printf("vkQueueSubmit failed\n"); + fmt::print("vkQueueSubmit failed\n"); std::exit(0); } } @@ -122,7 +122,7 @@ void GPU::CommandBuffer::execute() { m_execute = true; if (result != VK_SUCCESS) { - printf("vkQueueSubmit failed\n"); + fmt::print("vkQueueSubmit failed\n"); std::exit(0); } } @@ -141,7 +141,7 @@ void GPU::CommandPool::createPool(int id) { vkCreateCommandPool(ctx->m_device, &pool_info, nullptr, &m_pool[id]->pool); if (m_pool[id]->pool == nullptr) { - printf("pool is nullptr"); + fmt::print("pool is nullptr"); std::exit(0); } @@ -158,7 +158,7 @@ void GPU::CommandPool::createPool(int id) { alloc_info.commandBufferCount = m_pool[id]->buffers_count; if (vkAllocateCommandBuffers(ctx->m_device, &alloc_info, m_pool[id]->buffers) != VK_SUCCESS) { - printf("Can't allocate command buffers\n"); + fmt::print("Can't allocate command buffers\n"); std::exit(0); } @@ -171,7 +171,7 @@ void GPU::CommandPool::createPool(int id) { fence_info.flags = 0; if (vkCreateFence(ctx->m_device, &fence_info, nullptr, &m_pool[id]->fences[i]) != VK_SUCCESS) { - printf("Can't create fence\n"); + fmt::print("Can't create fence\n"); std::exit(0); } @@ -181,7 +181,7 @@ void GPU::CommandPool::createPool(int id) { semaphore_info.flags = 0; if (vkCreateSemaphore(ctx->m_device, &semaphore_info, nullptr, &m_pool[id]->semaphores[i]) != VK_SUCCESS) { - printf("Can't create semas\n"); + fmt::print("Can't create semas\n"); std::exit(0); } } diff --git a/src/Core/PS4/Linker.cpp b/src/Core/PS4/Linker.cpp index 76e4a6b3..14367dc5 100644 --- a/src/Core/PS4/Linker.cpp +++ b/src/Core/PS4/Linker.cpp @@ -1,6 +1,7 @@ #include "Linker.h" #include "../virtual_memory.h" #include +#include #include "Zydis.h" #include #include "Util/aerolib.h" @@ -175,7 +176,7 @@ void Linker::LoadModuleToMemory(Module* m) /* length: */ sizeof(rt1) - offset, /* instruction: */ &instruction ))) { - printf("%016" PRIX64 " %s\n", runtime_address, instruction.text); + fmt::print("{:#x}" PRIX64 " {}\n", runtime_address, instruction.text); offset += instruction.info.length; runtime_address += instruction.info.length; } @@ -625,8 +626,7 @@ using exit_func_t = PS4_SYSV_ABI void (*)(); using entry_func_t = PS4_SYSV_ABI void (*)(EntryParams* params, exit_func_t atexit_func); static PS4_SYSV_ABI void ProgramExitFunc() { - - printf("exit function called\n"); + fmt::print("exit function called\n"); } static void run_main_entry(u64 addr, EntryParams* params, exit_func_t exit_func) { diff --git a/src/Core/PS4/Loader/SymbolsResolver.cpp b/src/Core/PS4/Loader/SymbolsResolver.cpp index b8740836..c0e56d53 100644 --- a/src/Core/PS4/Loader/SymbolsResolver.cpp +++ b/src/Core/PS4/Loader/SymbolsResolver.cpp @@ -2,7 +2,6 @@ #include "SymbolsResolver.h" #include - void SymbolsResolver::AddSymbol(const SymbolRes& s, u64 virtual_addr) { SymbolRecord r{}; @@ -12,21 +11,19 @@ void SymbolsResolver::AddSymbol(const SymbolRes& s, u64 virtual_addr) } std::string SymbolsResolver::GenerateName(const SymbolRes& s) { - char str[256]; - sprintf(str, "%s lib[%s_v%d]mod[%s_v%d.%d]", s.name.c_str(),s.library.c_str(), s.library_version, s.module.c_str(), - s.module_version_major, s.module_version_minor); - return std::string(str); + return fmt::format("{} lib[{}_v{}]mod[{}_v{}.{}]", + s.name, s.library, s.library_version, + s.module, s.module_version_major, s.module_version_minor); } const SymbolRecord* SymbolsResolver::FindSymbol(const SymbolRes& s) const { - std::string name = GenerateName(s); - int index = 0; - for (auto symbol : m_symbols) { - if (symbol.name.compare(name) == 0) { - return &m_symbols.at(index); + const std::string name = GenerateName(s); + for (u32 i = 0; i < m_symbols.size(); i++) { + if (m_symbols[i].name.compare(name) == 0) { + return &m_symbols[i]; } - index++; } - LOG_INFO_IF(true, "unresolved! {}\n", name); + + LOG_INFO("Unresolved! {}\n", name); return nullptr; -} \ No newline at end of file +} diff --git a/src/Emulator/Util/singleton.h b/src/Emulator/Util/singleton.h index d5086133..01b3751b 100644 --- a/src/Emulator/Util/singleton.h +++ b/src/Emulator/Util/singleton.h @@ -9,7 +9,7 @@ public: if (!m_instance) { m_instance = std::make_unique(); } - return m_instance; + return m_instance.get(); } protected: diff --git a/src/Util/Disassembler.cpp b/src/Util/Disassembler.cpp index 83b9289f..e1af76bb 100644 --- a/src/Util/Disassembler.cpp +++ b/src/Util/Disassembler.cpp @@ -1,6 +1,5 @@ #include "Disassembler.h" -#include - +#include Disassembler::Disassembler() { @@ -15,11 +14,11 @@ Disassembler::~Disassembler() void Disassembler::printInstruction(void* code,u64 address)//print a single instruction { ZydisDecodedInstruction instruction; - ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE]; - ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands); + ZydisDecodedOperand operands[ZYDIS_MAX_OPERAND_COUNT_VISIBLE]; + ZyanStatus status = ZydisDecoderDecodeFull(&m_decoder, code, sizeof(code), &instruction, operands); if (!ZYAN_SUCCESS(status)) { - printf("decode instruction failed at %p\n", code); + fmt::print("decode instruction failed at {}\n", fmt::ptr(code)); } else { @@ -30,7 +29,7 @@ void Disassembler::printInstruction(void* code,u64 address)//print a single inst void Disassembler::printInst(ZydisDecodedInstruction& inst, ZydisDecodedOperand* operands,u64 address) { const int bufLen = 256; - char szBuffer[bufLen]; + char szBuffer[bufLen]; ZydisFormatterFormatInstruction(&m_formatter, &inst, operands,inst.operand_count_visible, szBuffer, sizeof(szBuffer), address, ZYAN_NULL); - printf("instruction: %s\n", szBuffer); -} \ No newline at end of file + fmt::print("instruction: {}\n", szBuffer); +} diff --git a/src/Util/config.cpp b/src/Util/config.cpp index aa5ff8a4..ab4eaf02 100644 --- a/src/Util/config.cpp +++ b/src/Util/config.cpp @@ -2,6 +2,7 @@ #include #include +#include #include namespace Config { @@ -29,7 +30,7 @@ void load(const std::filesystem::path& path) { try { data = toml::parse(path); } catch (std::exception& ex) { - printf("Got exception trying to load config file. Exception: %s\n", ex.what()); + fmt::print("Got exception trying to load config file. Exception: {}\n", ex.what()); return; } @@ -61,14 +62,14 @@ void save(const std::filesystem::path& path) { try { data = toml::parse(path); } catch (const std::exception& ex) { - printf("Exception trying to parse config file. Exception: %s\n", ex.what()); + fmt::print("Exception trying to parse config file. Exception: {}\n", ex.what()); return; } } else { if (error) { - printf("Filesystem error accessing %s (error: %s)\n", path.string().c_str(), error.message().c_str()); + fmt::print("Filesystem error accessing {} (error: {})\n", path.string(), error.message().c_str()); } - printf("Saving new configuration file %s\n", path.string().c_str()); + fmt::print("Saving new configuration file {}\n", path.string()); } data["General"]["isPS4Pro"] = isNeo; diff --git a/src/emulator.cpp b/src/emulator.cpp index 7e67849d..b489b39a 100644 --- a/src/emulator.cpp +++ b/src/emulator.cpp @@ -1,5 +1,5 @@ #include "emulator.h" - +#include #include #include #include "Emulator/Util/singleton.h" @@ -33,7 +33,7 @@ static void CreateSdlWindow(WindowCtx* ctx) { int height = static_cast(ctx->m_graphic_ctx.screen_height); if (SDL_Init(SDL_INIT_VIDEO) < 0) { - printf("%s\n", SDL_GetError()); + fmt::print("{}\n", SDL_GetError()); std::exit(0); } std::string title = "shadps4 v" + std::string(Emulator::VERSION); @@ -43,7 +43,7 @@ static void CreateSdlWindow(WindowCtx* ctx) { ctx->is_window_hidden = true; // hide window until we need to show something (should draw something in buffers) if (ctx->m_window == nullptr) { - printf("%s\n", SDL_GetError()); + fmt::print("{}\n", SDL_GetError()); std::exit(0); } @@ -115,11 +115,11 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { window_ctx->swapchain->present_complete_fence, &window_ctx->swapchain->current_index); if (result != VK_SUCCESS) { - printf("Can't aquireNextImage\n"); + fmt::print("Can't aquireNextImage\n"); std::exit(0); } if (window_ctx->swapchain->current_index == static_cast(-1)) { - printf("Unsupported:swapchain current index is -1\n"); + fmt::print("Unsupported:swapchain current index is -1\n"); std::exit(0); } @@ -127,7 +127,7 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { result = vkWaitForFences(window_ctx->m_graphic_ctx.m_device, 1, &window_ctx->swapchain->present_complete_fence, VK_TRUE, 100000000); } while (result == VK_TIMEOUT); if (result != VK_SUCCESS) { - printf("vkWaitForFences is not success\n"); + fmt::print("vkWaitForFences is not success\n"); std::exit(0); } @@ -137,11 +137,11 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { auto* blt_dst_image = window_ctx->swapchain; if (blt_src_image == nullptr) { - printf("blt_src_image is null\n"); + fmt::print("blt_src_image is null\n"); std::exit(0); } if (blt_dst_image == nullptr) { - printf("blt_dst_image is null\n"); + fmt::print("blt_dst_image is null\n"); std::exit(0); } @@ -187,13 +187,13 @@ void DrawBuffer(HLE::Libs::Graphics::VideoOutVulkanImage* image) { const auto& queue = window_ctx->m_graphic_ctx.queues[10]; if (queue.mutex != nullptr) { - printf("queue.mutexe is null\n"); + fmt::print("queue.mutexe is null\n"); std::exit(0); } result = vkQueuePresentKHR(queue.vk_queue, &present); if (result != VK_SUCCESS) { - printf("vkQueuePresentKHR failed\n"); + fmt::print("vkQueuePresentKHR failed\n"); std::exit(0); } } diff --git a/src/main.cpp b/src/main.cpp index bb9aed7d..97cfc909 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,8 @@ #include #include -#include +#include #include "types.h" +#include "Util/log.h" #include #include #include @@ -16,7 +17,7 @@ // Main code int main(int argc, char* argv[]) { if (argc == 1) { - printf("Usage: %s \n", argv[0]); + fmt::print("Usage: {} \n", argv[0]); return -1; } Config::load("config.toml"); diff --git a/src/vulkan_util.cpp b/src/vulkan_util.cpp index f414a7f0..39f5305b 100644 --- a/src/vulkan_util.cpp +++ b/src/vulkan_util.cpp @@ -1,5 +1,5 @@ #include "vulkan_util.h" - +#include #include #include #include @@ -588,7 +588,7 @@ void Graphics::Vulkan::vulkanCreateBuffer(HLE::Libs::Graphics::GraphicCtx* ctx, bool allocated = GPU::vulkanAllocateMemory(ctx, &buffer->memory); if (!allocated) { - printf("Can't allocate vulkan\n"); + fmt::print("Can't allocate vulkan\n"); std::exit(0); } vkBindBufferMemory(ctx->m_device, buffer->buffer, buffer->memory.memory, buffer->memory.offset);