diff --git a/src/def.cpp b/src/def.cpp index 4879c33..b19fc93 100644 --- a/src/def.cpp +++ b/src/def.cpp @@ -51,7 +51,7 @@ String ptrtow(const QWORD value) { return qwtowb16(value, 16); } namespace $LimitedUse { Release::~Release() { delete &gc; - Logger.put(L"--------- Last Check ---------\n"); + Logger.put(L"--------- Last Check ---------"); for (const auto& [addr, info] : memoryManager.allocated) { Logger.print(L" using", addr, info.size, L"B", info.msg); } delete &Logger; delete &memoryManager; diff --git a/src/def.h b/src/def.h index fef6df9..31391a5 100644 --- a/src/def.h +++ b/src/def.h @@ -5,7 +5,7 @@ #pragma once #define __CARLBEKS_DEBUG__ -#define __CARLBEKS_MEMORY__ 3 +#define __CARLBEKS_MEMORY__ 2 #pragma warning(disable: 4819) @@ -69,6 +69,8 @@ using Function = std::function; #pragma comment(lib, "Uxtheme.lib") #pragma comment(lib, "winmm.lib") +#include "warnings.h" + template concept Copyable = requires(const T& t) { T(t); }; template @@ -94,18 +96,19 @@ namespace $LimitedUse { ~Release(); } inline gcRelease_LoggerRelease_memoryManagerRelease; -} // namespace $LimitedUse -struct MemoryManager { - struct MemoryInfo { - const String msg; - std::size_t size; - }; + struct MemoryManager { + struct MemoryInfo { + const String msg; + std::size_t size; + }; - Map allocated{}; + Map allocated{}; + std::atomic_bool acquiring = false; - constexpr MemoryManager() noexcept = default; -} inline& [[carlbeks::releasedat("def.cpp")]] memoryManager = *new MemoryManager; + constexpr MemoryManager() noexcept = default; + } inline& [[carlbeks::releasedat("def.cpp")]] memoryManager = *new MemoryManager; +} void requireNonnull(const void* value) noexcept(false); void checkAllocation(const void* value) noexcept(false); @@ -123,30 +126,40 @@ extern String atow(const char* chars); template T* allocatedFor$(T* value, const String& msg = L"", std::size_t size = sizeof(T)) { requireNonnull(value); - const auto& k = memoryManager.allocated.emplace(value, MemoryManager::MemoryInfo{L"[" + atow(typeid(T).name()) + L"] " + msg, size}).first; -#if __CARLBEKS_MEMORY__ > 1 + bool expect = false; + while (!$LimitedUse::memoryManager.acquiring.compare_exchange_strong(expect, true)) expect = false; + const auto& k = $LimitedUse::memoryManager.allocated.emplace(value, $LimitedUse::MemoryManager::MemoryInfo{L"[" + atow(typeid(T).name()) + L"] " + msg, size}).first; +#if __CARLBEKS_MEMORY__ > 2 $LimitedUse::printAllocate(value, k->second.size, k->second.msg); #endif + $LimitedUse::memoryManager.acquiring.store(false); return value; } template T* deallocating$(T* value, const String& stack) { -#if __CARLBEKS_MEMORY__ > 1 - const MemoryManager::MemoryInfo* info = nullptr; - if (memoryManager.allocated.contains(value)) info = &memoryManager.allocated.at(value); + bool expect = false; + while (!$LimitedUse::memoryManager.acquiring.compare_exchange_strong(expect, true)) expect = false; +#if __CARLBEKS_MEMORY__ > 2 + const $LimitedUse::MemoryManager::MemoryInfo* info = nullptr; + if ($LimitedUse::memoryManager.allocated.contains(value)) info = &$LimitedUse::memoryManager.allocated.at(value); $LimitedUse::printDeallocate(value, info ? info->size : 0, info ? info->msg : L"???"); #endif - if (value) { if (!memoryManager.allocated.erase(value)) $LimitedUse::printDeallocateWarning(value, L"value not recorded" + stack); } + if (value) { if (!$LimitedUse::memoryManager.allocated.erase(value)) $LimitedUse::printDeallocateWarning(value, L"value not recorded" + stack); } + $LimitedUse::memoryManager.acquiring.store(false); return value; } +#ifndef __FUNCSIG__ +#define __FUNCSIG__ __FUNCTION__ +#endif + #if __CARLBEKS_MEMORY__ > 3 #define allocatedFor(val, ...) allocatedFor$(val, L"\n From " __FUNCSIG__ "\n At " __FILE__ ":" _STL_STRINGIZE(__LINE__) __VA_OPT__(,) __VA_ARGS__) #else #define allocatedFor(val, ...) allocatedFor$(val, L"" __VA_OPT__(,) __VA_ARGS__) #endif -#if __CARLBEKS_MEMORY__ > 2 +#if __CARLBEKS_MEMORY__ > 1 #define deallocating(val) deallocating$(val, L"\n From " __FUNCSIG__ "\n At " __FILE__ ":" _STL_STRINGIZE(__LINE__)) #else #define deallocating(val) deallocating$(val) diff --git a/src/game/Game.h b/src/game/Game.h index 51a5b59..c99c9f6 100644 --- a/src/game/Game.h +++ b/src/game/Game.h @@ -4,10 +4,10 @@ #pragma once -#include "../utils/gc.h" -#include "../ui/Hud.h" -#include "../utils/Task.h" -#include "../ui/Window.h" +// #include "..\utils\gc.h" +#include "..\ui\Hud.h" +#include "..\utils\Task.h" +#include "..\ui\Window.h" class [[carlbeks::predecl, carlbeks::defineat("World.h")]] WorldManager; class [[carlbeks::predecl, carlbeks::defineat("Entity.h")]] EntityManager; diff --git a/src/game/world/World.h b/src/game/world/World.h index 8c55398..7a6c5e9 100644 --- a/src/game/world/World.h +++ b/src/game/world/World.h @@ -121,6 +121,7 @@ public: block->onRemove(); } gc.submit(this); + Logger.debug(L"World::onRemove() called"); } }; diff --git a/src/hbp.h b/src/hbp.h index 15fb22e..f2261f3 100644 --- a/src/hbp.h +++ b/src/hbp.h @@ -2,22 +2,12 @@ #include "def.h" -inline BOOL NewProcess(const String& cmdline) noexcept { - STARTUPINFOW si = { - sizeof(si), nullptr, nullptr, nullptr, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, nullptr, nullptr, nullptr, nullptr - }; - PROCESS_INFORMATION pi{ - nullptr, nullptr, 0, 0 - }; - return CreateProcessW(nullptr, const_cast(cmdline.c_str()), nullptr, nullptr, 0, 0, nullptr, nullptr, &si, &pi); -} - inline HRESULT RemoveDefaultCaption(const HWND hWnd, const MARGINS* p) noexcept { return DwmExtendFrameIntoClientArea(hWnd, p); } inline bool ShowConsoleIO() noexcept { AllocConsole(); freopen("CONOUT$", "w", stdout); - freopen("CONOUT$", "r+", stdin); + freopen("CONIN$", "r+", stdin); return true; } diff --git a/src/main.cpp b/src/main.cpp index 5c1d9a0..11ea72b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -494,13 +494,16 @@ void renderThread() { using Time = time_point; renderer.initialize(); Time lastRender = system_clock::now(); + // bool _TestFlag = false; while (isRunning) { const Time thisTime = system_clock::now(); if (thisTime - lastRender < milliseconds(interactSettings.constants.msPerRender)) { Sleep(1); + // if (_TestFlag) _TestFlag = false, Logger.debug(L"Render Test: " + std::to_wstring((thisTime - lastRender) / milliseconds(1))); continue; } - game.render(nRange(static_cast((thisTime - lastTick).count()) / interactSettings.constants.msPerRender, 0.0, 1.0)); + // _TestFlag = true; + game.render(nRange(static_cast((thisTime - lastTick).count()) / static_cast(interactSettings.constants.msPerRender), 0.0, 1.0)); lastRender = thisTime; } } catch (const Exception& e) { Logger.log(L"Render thread exception: " + e.getMessage()); } @@ -516,7 +519,7 @@ int __stdcall wWinMain(const HINSTANCE hInstance, const HINSTANCE, [[maybe_unuse SetUnhandledExceptionFilter(UnhandledExceptionFilter); translator.initialize(); Logger.info(L"--------Program Start--------"); - for (const auto& [addr, info] : memoryManager.allocated) { Logger.print(L" using", addr, info.size, L"B", info.msg); } + for (const auto& [addr, info] : $LimitedUse::memoryManager.allocated) { Logger.print(L" using", addr, info.size, L"B", info.msg); } WNDCLASSEX wc = {}; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; @@ -541,26 +544,26 @@ int __stdcall wWinMain(const HINSTANCE hInstance, const HINSTANCE, [[maybe_unuse .cyBottomHeight = 0 }; RemoveDefaultCaption(MainWindowHandle, &margins); - int a = -40; - game.tasks.pushNewed(new Task([&a](Task& self) { - if (a) { - ++a; - SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, static_cast(0xff * (40 + a) / 40), LWA_COLORKEY | LWA_ALPHA); - } else { - SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, 0xff, LWA_COLORKEY | LWA_ALPHA); - self.schedulePop(true); - SetWindowLongW(MainWindowHandle, GWL_EXSTYLE, GetWindowLongW(MainWindowHandle, GWL_EXSTYLE) & ~WS_EX_LAYERED); - } - return 0; - })); SetWindowLongW(MainWindowHandle, GWL_EXSTYLE, GetWindowLongW(MainWindowHandle, GWL_EXSTYLE) | WS_EX_LAYERED); - SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, 0xe0, LWA_COLORKEY | LWA_ALPHA); + // SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, 0xe0, LWA_COLORKEY | LWA_ALPHA); + SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, 0, LWA_COLORKEY); ShowWindow(MainWindowHandle, nShowCmd); const HHOOK hook = SetWindowsHookW(WH_GETMESSAGE, HookProc); const HACCEL hAccelTable = LoadAcceleratorsW(hInstance, MAKEINTRESOURCE(109)); if (!hook) Logger.error(Logger.of(L"SetWindowsHookW failed. LastError:", GetLastError())); test(); { + // int a = -40; + // game.tasks.pushNewed(allocatedFor(new Task([&a](Task& self) { + // if (a) SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, static_cast(0xff * (40 + ++a) / 40), LWA_COLORKEY | LWA_ALPHA); + // else { + // SetLayeredWindowAttributes(MainWindowHandle, 0xffffff, 0xff, LWA_COLORKEY | LWA_ALPHA); + // self.schedulePop(true); + // SetWindowLongW(MainWindowHandle, GWL_EXSTYLE, GetWindowLongW(MainWindowHandle, GWL_EXSTYLE) & ~WS_EX_LAYERED); + // } + // return 0; + // }) + // )); game.initialize(); interactManager.initialize(); World* w = StartWorld::create(); @@ -584,10 +587,10 @@ int __stdcall wWinMain(const HINSTANCE hInstance, const HINSTANCE, [[maybe_unuse DestroyAcceleratorTable(hAccelTable); UnhookWindowsHookEx(hook); Logger.info(L"------- Program End --------"); - for (const auto& [addr, info] : memoryManager.allocated) { Logger.print(L" using", addr, info.size, L"B", info.msg); } - _wsystem(L"pause"); + for (const auto& [addr, info] : $LimitedUse::memoryManager.allocated) { Logger.print(L" using", addr, info.size, L"B", info.msg); } { fontManager.finalize(); // 似乎GDI有终止自动回收,所以此代码需要提前 } + _wsystem(L"pause"); return static_cast(msg.wParam); } diff --git a/src/ui/Window.h b/src/ui/Window.h index d98d280..8dda4c2 100644 --- a/src/ui/Window.h +++ b/src/ui/Window.h @@ -103,6 +103,7 @@ public: virtual void onMouseUp(const MouseButtonCode code) noexcept { if (mouseUp) mouseUp(*this, code); if (hasMouseTrigger) onMouseClick(code); + hasMouseTrigger = false; } virtual void onMouseLeave(const MouseButtonCode value) noexcept { diff --git a/src/utils/Chars.h b/src/utils/Chars.h index de8e227..7b119f0 100644 --- a/src/utils/Chars.h +++ b/src/utils/Chars.h @@ -108,11 +108,11 @@ static constexpr wchar Table16[17] = L"0123456789ABCDEF"; [[nodiscard]] inline String qwtowb10(QWORD value, const unsigned int fills = 1) noexcept { static constexpr wchar Table10[11] = L"0123456789"; static constexpr QWORD Compare10[20] = { - 0, 10, 100, 1000, 10000, 100000, 1000000, 10000000, - 100000000, 1000000000, 10000000000, 100000000000, - 1000000000000, 10000000000000, 100000000000000, - 1000000000000000, 10000000000000000, 100000000000000000, - 1000000000000000000, 10000000000000000000 + 0ull, 10ull, 100ull, 1000ull, 10000ull, 100000ull, 1000000ull, 10000000ull, + 100000000ull, 1000000000ull, 10000000000ull, 100000000000ull, + 1000000000000ull, 10000000000000ull, 100000000000000ull, + 1000000000000000ull, 10000000000000000ull, 100000000000000000ull, + 1000000000000000000ull, 10000000000000000000ull }; String ret; if (value < Compare10[fills]) { diff --git a/src/warnings.h b/src/warnings.h index 47fa544..0ad0814 100644 --- a/src/warnings.h +++ b/src/warnings.h @@ -6,4 +6,5 @@ #pragma warning(disable: 4554) #pragma warning(default: 4555) +#pragma warning(disable: 4996) #pragma warning(disable: 5030)