Skip to content

Commit

Permalink
work in progress
Browse files Browse the repository at this point in the history
* tweak about dialog
* `d2mapapi_piped` returns init error string
  • Loading branch information
soarqin committed Dec 4, 2021
1 parent b8370b3 commit a20ec7b
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 17 deletions.
5 changes: 5 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# next release
* new transparency mechanism, now you can set each color with alpha channel, and are stack with global `alpha` setting
* separate d2mapapi out as a standalone project, and re-add by git-subrepo, with lots of tweaks
* D2RMH main program can be built in 64bit now, while using `d2mapapi_piped` as a child process for querying map data

# v0.8.1
* fix lines for some quest targets
* fix random color blocks outside of map area sometime
Expand Down
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ Check [TODO](TODO.md)

# How to build
## Quick instruction
* Just use [cmake](https://www.cmake.org/) to build
* Add `cmake\bin` to your `PATH` environment variable so that you can type `cmake` in command line to call it directly
* Install [cmake](https://www.cmake.org/) and add `cmake\bin` to your `PATH` environment variable so that you can type `cmake` in command line to call it directly
* Run `build_msvc2019.bat`, `build_msvc2022.bat`, `build_msys2_clang.bat` or `build_msys2_mingw.bat` to build.
Note: You should have certain compilers intalled
## Detailed instruction w/o .bat scripts
### MinGW GCC 32bit
Note: You should have certain compilers intalled. For msys2 builds, install required packages as instructions below.
## Detailed instruction without .bat scripts
### MinGW GCC
* Install MSYS2(https://www.msys2.org), type `pacman -Syu --noconfirm && pacman -S --noconfirm --needed make git mingw-w64-i686-toolchain mingw-w64-i686-cmake mingw-w64-ucrt-x86_64-toolchain mingw-w64-ucrt-x86_64-cmake` in MSYS2 command line to install required components
* Build D2RMH(64bit):
* Open new Shell using ucrt64.exe
Expand All @@ -44,6 +43,14 @@ Check [TODO](TODO.md)
* Change current directory to D2RMH source
* Type `cmake -Bbuild_d2mapapi -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DUSE_STATIC_CRT=ON d2mapapi`
* Then `make -Cbuild_d2mapapi d2mapapi-piped` to get the compiled binary in `build_d2mapapi/bin` folder
### MSYS2 Clang
* Mostly same as MinGW GCC, with following changes:
* `mingw-w64-i686-toolchain`->`mingw-w64-clang-i686-toolchain`
* `mingw-w64-i686-cmake`->`mingw-w64-clang-i686-cmake`
* `mingw-w64-ucrt-x86_64-toolchain`->`mingw-w64-clang-x86_64-toolchain`
* `mingw-w64-ucrt-x86_64-cmake`->`mingw-w64-clang-x86_64-cmake`
* `ucrt64.exe`->`clang64.exe`
* `mingw32.exe`->`clang32.exe`
### Microsoft Visual Studio 2019/2022
* Install Visual Studio 2019 or 2022 Community Edition(or Pro/Ent if you have)
* Unpack downloaded source code file, or you can use git to Clone D2RMH source by type: `git clone https://github.com/soarqin/D2RMH`. Note: Using git requires [Git for windows](https://git-scm.com/download/win) installed
Expand Down
105 changes: 105 additions & 0 deletions cmake/GetVersion.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
macro(get_project_version VER_PROJ_NAME)
find_package(Git QUIET)

# Check if git is found...
if (GIT_FOUND)

# Get last tag from git
execute_process(COMMAND ${GIT_EXECUTABLE} describe --abbrev=0 --tags
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${VER_PROJ_NAME}_VERSION_STRING
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Get name of current branch
execute_process(COMMAND ${GIT_EXECUTABLE} symbolic-ref --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${VER_PROJ_NAME}_BRANCH_NAME
OUTPUT_STRIP_TRAILING_WHITESPACE)

#How many commits since last tag
execute_process(COMMAND ${GIT_EXECUTABLE} rev-list ${${VER_PROJ_NAME}_BRANCH_NAME} ${${VER_PROJ_NAME}_VERSION_STRING}..HEAD --count
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${VER_PROJ_NAME}_VERSION_AHEAD
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Get current commit SHA from git
execute_process(COMMAND ${GIT_EXECUTABLE} rev-parse --short HEAD
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE ${VER_PROJ_NAME}_VERSION_GIT_SHA
OUTPUT_STRIP_TRAILING_WHITESPACE)

# Get partial versions into a list
string(REGEX MATCHALL "-.*$|[0-9]+" ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST
${${VER_PROJ_NAME}_VERSION_STRING})

# Set the version numbers
list(GET ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST
0 ${VER_PROJ_NAME}_VERSION_MAJOR)
list(GET ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST
1 ${VER_PROJ_NAME}_VERSION_MINOR)
list(GET ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST
2 ${VER_PROJ_NAME}_VERSION_PATCH)

# The tweak part is optional, so check if the list contains it
list(LENGTH ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST
${VER_PROJ_NAME}_PARTIAL_VERSION_LIST_LEN)
if (${VER_PROJ_NAME}_PARTIAL_VERSION_LIST_LEN GREATER 3)
list(GET ${VER_PROJ_NAME}_PARTIAL_VERSION_LIST 3 ${VER_PROJ_NAME}_VERSION_TWEAK)
string(SUBSTRING ${${VER_PROJ_NAME}_VERSION_TWEAK} 1 -1 ${VER_PROJ_NAME}_VERSION_TWEAK)
endif()

# Unset the list
unset(${VER_PROJ_NAME}_PARTIAL_VERSION_LIST)

# Save version to file (which will be used when Git is not available
# or VERSION_UPDATE_FROM_GIT is disabled)
file(WRITE ${CMAKE_CURRENT_SOURCE_DIR}/version_generated ${${VER_PROJ_NAME}_VERSION_STRING}
"*" ${${VER_PROJ_NAME}_VERSION_MAJOR}
"*" ${${VER_PROJ_NAME}_VERSION_MINOR}
"*" ${${VER_PROJ_NAME}_VERSION_PATCH}
"*" ${${VER_PROJ_NAME}_VERSION_TWEAK}
"*" ${${VER_PROJ_NAME}_VERSION_AHEAD}
"*" ${${VER_PROJ_NAME}_VERSION_GIT_SHA})

else()

# Git not available, get version from file
file(STRINGS ${CMAKE_CURRENT_SOURCE_DIR}/version_generated ${VER_PROJ_NAME}_VERSION_LIST)
string(REPLACE "*" ";" ${VER_PROJ_NAME}_VERSION_LIST "${${VER_PROJ_NAME}_VERSION_LIST}")
# Set partial versions
list(GET ${VER_PROJ_NAME}_VERSION_LIST 0 ${VER_PROJ_NAME}_VERSION_STRING)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 1 ${VER_PROJ_NAME}_VERSION_MAJOR)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 2 ${VER_PROJ_NAME}_VERSION_MINOR)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 3 ${VER_PROJ_NAME}_VERSION_PATCH)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 4 ${VER_PROJ_NAME}_VERSION_TWEAK)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 5 ${VER_PROJ_NAME}_VERSION_AHEAD)
list(GET ${VER_PROJ_NAME}_VERSION_LIST 6 ${VER_PROJ_NAME}_VERSION_GIT_SHA)

endif()

# Set full project version string
if (${VER_PROJ_NAME}_VERSION_AHEAD GREATER 0)
set(${VER_PROJ_NAME}_VERSION_STRING_FULL
${${VER_PROJ_NAME}_VERSION_STRING}+${${VER_PROJ_NAME}_VERSION_AHEAD}.${${VER_PROJ_NAME}_VERSION_GIT_SHA})
else()
set(${VER_PROJ_NAME}_VERSION_STRING_FULL
${${VER_PROJ_NAME}_VERSION_STRING}.${${VER_PROJ_NAME}_VERSION_GIT_SHA})
endif()

# Set project version (without the preceding 'v')
set(${VER_PROJ_NAME}_VERSION ${${VER_PROJ_NAME}_VERSION_MAJOR}.${${VER_PROJ_NAME}_VERSION_MINOR}.${${VER_PROJ_NAME}_VERSION_PATCH})
if (${VER_PROJ_NAME}_VERSION_TWEAK)
set(${VER_PROJ_NAME}_VERSION ${${VER_PROJ_NAME}_VERSION}-${${VER_PROJ_NAME}_VERSION_TWEAK})
endif()

target_compile_definitions(${VER_PROJ_NAME} PRIVATE
VERSION_STRING_FULL="${${VER_PROJ_NAME}_VERSION_STRING_FULL}"
VERSION_STRING="${${VER_PROJ_NAME}_VERSION_STRING}"
VERSION_MAJOR=${${VER_PROJ_NAME}_VERSION_MAJOR}
VERSION_MINOR=${${VER_PROJ_NAME}_VERSION_MINOR}
VERSION_PATCH=${${VER_PROJ_NAME}_VERSION_PATCH}
VERSION_TWEAK="${${VER_PROJ_NAME}_VERSION_TWEAK}"
VERSION_AHEAD="${${VER_PROJ_NAME}_VERSION_AHEAD}"
VERSION_GIT_SHA="${${VER_PROJ_NAME}_VERSION_GIT_SHA}"
)
endmacro()
2 changes: 2 additions & 0 deletions d2mapapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.13)

project(d2mapapi)

set(VERSION_MAJOR)

if(MSVC)
set(CMAKE_CXX_STANDARD 20)
else()
Expand Down
3 changes: 3 additions & 0 deletions d2mapapi/host.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "pipehost.h"

#include <windows.h>

int main(int argc, char *argv[]) {
d2mapapi::PipedChildProcess pcp;
if (!pcp.start(L"d2mapapi_piped.exe", nullptr)) {
MessageBoxA(nullptr, pcp.errMsg().c_str(), nullptr, 0);
return -1;
}
for (int i = 1; i < argc; i += 3) {
Expand Down
4 changes: 3 additions & 1 deletion d2mapapi/piped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ int wmain(int argc, wchar_t *argv[]) {
}
RegCloseKey(key);
}
MessageBoxA(nullptr, errstr, "d2mapapi", MB_OK | MB_ICONERROR);
ret = -1;
DWORD written;
WriteFile(hStdout, &ret, sizeof(int), &written, nullptr);
auto sz = strlen(errstr);
WriteFile(hStdout, &sz, sizeof(uint32_t), &written, nullptr);
WriteFile(hStdout, errstr, sz, &written, nullptr);
return -1;
} while (false);
}
Expand Down
11 changes: 10 additions & 1 deletion d2mapapi/pipehost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,16 @@ bool PipedChildProcess::start(const wchar_t *filename, const wchar_t *parameters
CloseHandle(HANDLE(childStdoutWr));
CloseHandle(HANDLE(childStdinRd));
int ret;
return readPipe(&ret, 4) && ret == 0;
if (!readPipe(&ret, 4)) {
errMsg_ = "failed to read from child process!";
return false;
}
if (ret == 0) { return true; }
uint32_t len;
readPipe(&len, 4);
errMsg_.resize(len);
readPipe(errMsg_.data(), len);
return false;
}

bool PipedChildProcess::writePipe(const void *data, size_t size) {
Expand Down
3 changes: 3 additions & 0 deletions d2mapapi/pipehost.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class PipedChildProcess final {
bool writePipe(const void *data, size_t size);
bool readPipe(void *data, size_t size);

[[nodiscard]] inline const std::string &errMsg() const { return errMsg_; }

std::string queryMapRaw(uint32_t seed, uint8_t difficulty, uint32_t levelId);
CollisionMap *queryMap(uint32_t seed, uint8_t difficulty, uint32_t levelId);

Expand All @@ -32,6 +34,7 @@ class PipedChildProcess final {
void *childStdoutRd = nullptr;
void *childStdoutWr = nullptr;
void *process = nullptr;
std::string errMsg_;
};

}
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
add_subdirectory(glad)

include(GetVersion)

file(GLOB D2RMH_SRC_FILES *.cpp *.h res.rc)
if(NOT MSVC)
set(D2RMH_SRC_FILES ${D2RMH_SRC_FILES} manifest.rc)
endif()
add_executable(D2RMH WIN32 ${D2RMH_SRC_FILES})
get_project_version(D2RMH)
add_custom_target(D2RMH_RES DEPENDS D2RMH.ico)
add_dependencies(D2RMH D2RMH_RES)
if(TARGET d2mapapi_piped)
Expand Down
5 changes: 3 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
loadCfg();
d2mapapi::PipedChildProcess pcp;
if (!pcp.start(L"d2mapapi_piped.exe", (wchar_t *)cfg->d2Path.c_str())) {
MessageBoxA(nullptr, pcp.errMsg().c_str(), nullptr, 0);
return -1;
}
loadData();
Expand All @@ -45,9 +46,9 @@ int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
MapRenderer map(renderer, pcp);
wnd.enableTrayMenu(true,
(const wchar_t *)1,
L"D2RMH",
L"D2RMH " VERSION_STRING_FULL,
L"D2RMH is running.\nYou can close it from tray-icon popup menu.",
L"D2RMH");
L"D2RMH " VERSION_STRING);
wnd.addAboutMenu();
wnd.addTrayMenuItem(L"Reload Config", -1, 0, [&wnd, &renderer, &map]() {
loadCfg();
Expand Down
13 changes: 7 additions & 6 deletions src/res.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include "winres.h"

101 DIALOG DISCARDABLE 0, 0, 240, 130
101 DIALOG DISCARDABLE 0, 0, 240, 155
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About D2RMH"
FONT 10, "Arial Bold"
BEGIN
LTEXT "D2RMH, a map revealing tool for Diablo II Resurrected",IDC_STATIC,20,20,200,10
LTEXT "by Soar Qin <[email protected]>",IDC_STATIC,70,35,150,10
DEFPUSHBUTTON "OK",IDOK,100,103,40,12
CONTROL "Project page: <a href=""https://github.com/soarqin/D2RMH"">https://github.com/soarqin/D2RMH</a>",1001,"SysLink",WS_TABSTOP,20,65,200,10
CONTROL "New releases: <a href=""https://github.com/soarqin/D2RMH/releases"">https://github.com/soarqin/D2RMH/releases</a>",1002,"SysLink",WS_TABSTOP,20,80,200,10
LTEXT "D2RMH",1000,20,20,200,20,SS_CENTER
LTEXT "a map revealing tool for Diablo II Resurrected",IDC_STATIC,20,45,200,10,SS_CENTER
LTEXT "by Soar Qin<[email protected]>",IDC_STATIC,20,60,200,10,SS_CENTER
DEFPUSHBUTTON "OK",IDOK,100,128,40,12
CONTROL "Project page: <a href=""https://github.com/soarqin/D2RMH"">https://github.com/soarqin/D2RMH</a>",1001,"SysLink",WS_TABSTOP,20,90,200,10
CONTROL "New releases: <a href=""https://github.com/soarqin/D2RMH/releases"">https://github.com/soarqin/D2RMH/releases</a>",1002,"SysLink",WS_TABSTOP,20,105,200,10
END
21 changes: 19 additions & 2 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

#include "window.h"

#include "cfg.h"

#include <windows.h>
#include <versionhelpers.h>
#include <dwmapi.h>
Expand Down Expand Up @@ -199,6 +197,9 @@ Window::~Window() {

INT_PTR CALLBACK dialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
switch (uMsg) {
case WM_CLOSE:
EndDialog(hWnd, TRUE);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
Expand Down Expand Up @@ -230,8 +231,24 @@ INT_PTR CALLBACK dialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
auto width = rc2.right - rc2.left;
auto height = rc2.bottom - rc2.top;
MoveWindow(hWnd, (rc.right + rc.left - width) / 2, (rc.top + rc.bottom - height) / 2, width, height, FALSE);
auto caption = GetDlgItem(hWnd, 1000);
SetWindowTextW(caption, L"D2RMH " VERSION_STRING_FULL);
auto font = HFONT(SendMessage(caption, WM_GETFONT, 0, 0));
LOGFONTW lf = {};
GetObjectW(font, sizeof(lf), &lf);
lf.lfHeight *= 2; lf.lfWidth *= 2;
auto font2 = CreateFontIndirectW(&lf);
SendMessage(caption, WM_SETFONT, (WPARAM)font2, TRUE);
break;
}
case WM_CTLCOLORSTATIC: {
if (GetDlgCtrlID((HWND)lParam) == 1000) {
SetTextColor((HDC)wParam, 0x881111);
SetBkMode((HDC)wParam, TRANSPARENT);
return (INT_PTR)::GetStockObject(NULL_BRUSH);
}
return FALSE;
}
default:
return FALSE;
}
Expand Down

0 comments on commit a20ec7b

Please sign in to comment.