Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically setup DXVK if the option is checked #66

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions M2TWEOP Code/M2TWEOP GUI/dataG.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class dataG
struct
{
bool isContextMenuNeeded = true;
bool isDXVKEnabled = true;
bool isTacticalMapViewerNeeded = true;
bool isDeveloperModeNeeded = true;

Expand Down
18 changes: 18 additions & 0 deletions M2TWEOP Code/M2TWEOP GUI/gameStarter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,24 @@ bool gameStarter::initM2TWEOP()
return true;
}

// d3d9_vk.dll
path wrapd3dS_vk = system_complete("d3d9_vk.dll");

path d3dS_vk = system_complete("..\\..\\d3d9_vk.dll");
string wrapd3dStr_vk = wrapd3dS_vk.string();
string d3dStr_vk = d3dS_vk.string();

if (helpers::compareFiles(d3dStr_vk, wrapd3dStr_vk) == false)
{

if (CopyFileA(wrapd3dStr_vk.c_str(), d3dStr_vk.c_str(), FALSE) == false)
{
DWORD ERR = GetLastError();
MessageBoxA(NULL, "Cannot run M2TWEOP, d3d9_vk.dll replacing error! Try to delete d3d9_vk.dll in game folder or copy d3d9_vk.dll from M2TWEOP archive AND START M2TWEOP WITH ADMIN RIGHTS IF IT STILL NOT WORK AFTER THIS. ", "ERROR", MB_OK);
exit(0);
}
}

// D3D9.dll
path wrapd3dS = system_complete("d3d9.dll");

Expand Down
5 changes: 5 additions & 0 deletions M2TWEOP Code/M2TWEOP GUI/managerG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ namespace managerG

try
{
if (json.contains("isDXVKEnabled"))
{
getJson(dataG::data.modulesData.isDXVKEnabled, "isDXVKEnabled");
}
if (json.contains("isContextMenuNeeded"))
{
getJson(dataG::data.modulesData.isContextMenuNeeded, "isContextMenuNeeded");
Expand Down Expand Up @@ -214,6 +218,7 @@ namespace managerG

// Save game config
fPath = ".\\eopData\\gameCfg.json";
setJson("isDXVKEnabled", dataG::data.modulesData.isDXVKEnabled);
setJson("isContextMenuNeeded", dataG::data.modulesData.isContextMenuNeeded);
setJson("isTacticalMapViewerNeeded", dataG::data.modulesData.isTacticalMapViewerNeeded);
setJson("isDeveloperModeNeeded", dataG::data.modulesData.isDeveloperModeNeeded);
Expand Down
3 changes: 3 additions & 0 deletions M2TWEOP Code/M2TWEOP GUI/modSettingsUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,9 @@ namespace modSettingsUI

void drawGameSettings()
{
ImGui::Checkbox("DXVK Rendering Mode", &dataG::data.modulesData.isDXVKEnabled);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { ImGui::SetTooltip("Experimental: Forces Medieval 2 to use DXVK instead of DirectX for rendering. Can massively improve performance on some hardware. \nNote: The first time you use DXVK Rendering, you may experience worse performance due to compilation of shaders. \nThe second time you launch the game, assuming the shaders have compiled, performance should be much better (even better than Vanilla DirectX Rendering)");}

ImGui::Checkbox("Campaign Context Menu", &dataG::data.modulesData.isContextMenuNeeded);
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { ImGui::SetTooltip("Enable the context menu on the campaign map while pressing MMB");}

Expand Down
5 changes: 3 additions & 2 deletions M2TWEOP Code/d3d9/d3d9.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<IncludePath>$(SolutionDir)\3rd\M2TWEOPBoost;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)\3rd\M2TWEOPBoost;$(IncludePath)$(SolutionDir)\3rd\nlohmann;</IncludePath>
<LibraryPath>$(SolutionDir)\3rd\M2TWEOPBoost\boostLibs;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<IncludePath>$(SolutionDir)\3rd\M2TWEOPBoost;$(IncludePath)</IncludePath>
<IncludePath>$(SolutionDir)\3rd\M2TWEOPBoost;$(IncludePath)$(SolutionDir)\3rd\nlohmann;</IncludePath>
<LibraryPath>$(SolutionDir)\3rd\M2TWEOPBoost\boostLibs;$(LibraryPath)</LibraryPath>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
Expand Down Expand Up @@ -100,6 +100,7 @@
<ClCompile Include="IDirect3DVolumeTexture9.cpp" />
<ClCompile Include="InterfaceQuery.cpp" />
<ClCompile Include="m2tweopStarter.cpp" />
<ClInclude Include="..\3rd\nlohmann\json.hpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="AddressLookupTable.h" />
Expand Down
61 changes: 50 additions & 11 deletions M2TWEOP Code/d3d9/helpers.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "helpers.h"
#include "json.hpp"

using namespace std;
vector<std::string> helpers::splitString(const std::string& phrase, const std::string& delimiter) {
vector<std::string> helpers::splitString(const std::string &phrase, const std::string &delimiter)
{
vector<string> list;
string s = string(phrase);
size_t pos = 0;
string token;
while ((pos = s.find(delimiter)) != string::npos) {
while ((pos = s.find(delimiter)) != string::npos)
{
token = s.substr(0, pos);
list.push_back(token);
s.erase(0, pos + delimiter.length());
Expand All @@ -14,17 +18,18 @@ vector<std::string> helpers::splitString(const std::string& phrase, const std::s
return list;
}


#define BOOST_DATE_TIME_NO_LIB 1
#include <boost/interprocess/windows_shared_memory.hpp>
#include <boost/interprocess/mapped_region.hpp>
void helpers::doEOPPipe(std::string& result, int waitSeconds)
{
std::string helpers::doEOPPipe(int waitSeconds, bool cleanup)
{
std::string result;
ULONGLONG startTime = GetTickCount64();
ULONGLONG endTime = startTime + 1000ull * waitSeconds;
namespace bip = boost::interprocess;

do {
do
{
//boost way ;(
try
{
Expand All @@ -35,15 +40,49 @@ void helpers::doEOPPipe(std::string& result, int waitSeconds)
char* mem = static_cast<char*>(region.get_address());
result = mem;

*mem = 0;

region.flush();
if (cleanup == true) {
*mem = 0;
region.flush();
}
}
catch (...)
{

}

} while (GetTickCount64() < endTime&& result.size()==0);
} while (GetTickCount64() < endTime && result.size() == 0);

return result;
}

std::string helpers::getModFolderFromPipe(const string &msg)
{
vector<string> args = helpers::splitString(msg, "\n");
return args[2];
}

std::string helpers::getModPathFromSharedMemory()
{
string resMsg = helpers::doEOPPipe(5, false);
string modPath = helpers::getModFolderFromPipe(resMsg);
return modPath;
}

jsn::json helpers::loadJsonFromFile(const std::string &fpath)
{
try
{
jsn::json json;

std::ifstream f1(fpath);
if (f1.is_open())
{
f1 >> json;
}
f1.close();
return std::move(json);
}
catch (jsn::json::exception &e)
{
MessageBoxA(NULL, e.what(), "Could not load JSON from file!", MB_APPLMODAL | MB_SETFOREGROUND);
}
}
19 changes: 18 additions & 1 deletion M2TWEOP Code/d3d9/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,26 @@
#include <string>
#include <fstream>
#include <vector>
#include "json.hpp"

// Json namespace
namespace jsn = nlohmann;

//macro for fast getting values from json. Need jsn::json obect, named json!
#define getJson(field,jsonName)\
field= json.at(jsonName);

//macro for fast setting values to json. Need jsn::json obect, named json!
#define setJson(jsonName,field)\
json[jsonName]=field;


class helpers
{
public:
static std::vector<std::string> splitString(const std::string& phrase, const std::string& delimiter);
static void doEOPPipe(std::string& result, int waitSeconds);
static std::string doEOPPipe(int waitSeconds, bool cleanup);
static jsn::json loadJsonFromFile(const std::string& fpath);
static std::string helpers::getModPathFromSharedMemory();
static std::string helpers::getModFolderFromPipe(const std::string &msg);
};
54 changes: 25 additions & 29 deletions M2TWEOP Code/d3d9/initDX.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include "d3d9.h"
#include "helpers.h"
#include "m2tweopStarter.h"
Direct3DShaderValidatorCreate9Proc m_pDirect3DShaderValidatorCreate9;
PSGPErrorProc m_pPSGPError;
PSGPSampleTextureProc m_pPSGPSampleTexture;
Expand All @@ -26,38 +28,39 @@ void initDX9()
{
return;
}
// Load dll

wchar_t path[MAX_PATH];
GetCurrentDirectoryW(MAX_PATH, path);
std::wstring libPath = path;
libPath += L"\\d3d9_vk.dll";
d3d9dll = LoadLibraryW(libPath.c_str());
if (d3d9dll == NULL)
std::wstring libPath;

// Check if DXVK is enabled
std::string cfgPath = "\\eopData\\gameCfg.json";
std::string modPath = helpers::getModPathFromSharedMemory();
modPath += cfgPath;

jsn::json json = helpers::loadJsonFromFile(modPath);
bool isDXVKEnabled = false;
if (json.contains("isDXVKEnabled"))
{
getJson(isDXVKEnabled, "isDXVKEnabled");
}

if (isDXVKEnabled == true) {
GetCurrentDirectoryW(MAX_PATH, path);
libPath = path;
libPath += L"\\d3d9_vk.dll";
} else {
GetSystemDirectoryW(path, MAX_PATH);
libPath = path;
libPath += L"\\d3d9.dll";

d3d9dll = LoadLibraryW(libPath.c_str());
if (d3d9dll == NULL)
{
MessageBoxA(NULL, "Cannot find d3d9.dll in system directory!", "ATTENTION!", NULL);
exit(0);
}
}

//GetCurrentDirectoryA(MAX_PATH,path);



/*DWORD err = GetLastError();
if (err)
d3d9dll = LoadLibraryW(libPath.c_str());
if (d3d9dll == NULL)
{
std::string errorS = std::to_string(err);
MessageBoxA(NULL, errorS.c_str(), "ATTENTION!", NULL);
MessageBoxA(NULL, "Cannot find d3d9.dll (or d3d9_vk.dll if you have DXVK Rendering enabled) in system directory!", "ATTENTION!", NULL);
exit(0);
}*/
}

// Get function addresses
m_pDirect3DShaderValidatorCreate9 = (Direct3DShaderValidatorCreate9Proc)GetProcAddress(d3d9dll, "Direct3DShaderValidatorCreate9");
m_pPSGPError = (PSGPErrorProc)GetProcAddress(d3d9dll, "PSGPError");
Expand All @@ -74,13 +77,6 @@ void initDX9()
m_pDirect3D9EnableMaximizedWindowedModeShim = (Direct3D9EnableMaximizedWindowedModeShimProc)GetProcAddress(d3d9dll, "Direct3D9EnableMaximizedWindowedModeShim");
m_pDirect3DCreate9 = (Direct3DCreate9Proc)GetProcAddress(d3d9dll, "Direct3DCreate9");
m_pDirect3DCreate9Ex = (Direct3DCreate9ExProc)GetProcAddress(d3d9dll, "Direct3DCreate9Ex");
/*DWORD err = GetLastError();
if (err)
{
std::string errorS = std::to_string(err);
MessageBoxA(NULL, errorS.c_str(), "ATTENTION!", NULL);
exit(0);
}*/
}

void deInit()
Expand Down
4 changes: 2 additions & 2 deletions M2TWEOP Code/d3d9/m2tweopStarter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ namespace m2tweopStarter
dataEOP.gameVer = stoi(args[4]);
return true;
}

void doM2TWEOP()
{
string resMsg;
helpers::doEOPPipe(resMsg, 5);
string resMsg = helpers::doEOPPipe(5, true);
dataEOP.isEOPipe=parsePipeMessage(resMsg);

if (dataEOP.isEOPipe == false)
Expand Down
2 changes: 2 additions & 0 deletions M2TWEOP Code/d3d9/m2tweopStarter.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once
#include <d3d9.h>
#include <string>

namespace m2tweopStarter
{
void doM2TWEOP();
Expand Down
Binary file added M2TWEOP DataFiles/d3d9_vk.dll
Binary file not shown.
3 changes: 2 additions & 1 deletion M2TWEOP DataFiles/eopData/gameCfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
"isDiscordRichPresenceEnabled": false,
"isDeveloperModeNeeded": true,
"isTacticalMapViewerNeeded": true,
"isContextMenuNeeded": true
"isContextMenuNeeded": true,
"isDXVKEnabled": false
}
2 changes: 1 addition & 1 deletion documentationGenerator/releaseInfo/releaseDescription.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
![](https://i.imgur.com/h8UlYMT.png)

### **Library**
- Added support for [DXVK project](https://github.com/doitsujin/dxvk) (Vulcan wrapper over game d3d9 graphics). For use unpack dxvk d3d9.dll to game folder and rename it to d3d9_vk.dll - *youneuoy*
- Added support for [DXVK project](https://github.com/doitsujin/dxvk) (Vulcan wrapper over game d3d9 graphics). To try this out simply enable the option in the launcher. For some hardware, this can massively improve performance - *youneuoy and Medik*
- "Show planned retreat route" no longer shows on the Stratmap Context Menu if the option is disabled in the launcher - *Medik*

### **Lua Plugin**
Expand Down
Loading