Skip to content

Commit

Permalink
Merge pull request #529 from WSSDude420/ui-changes
Browse files Browse the repository at this point in the history
Big UI changes (full change list in description)
  • Loading branch information
Yamashi authored Apr 2, 2021
2 parents 5735670 + 84e893b commit 650077b
Show file tree
Hide file tree
Showing 19 changed files with 501 additions and 269 deletions.
3 changes: 1 addition & 2 deletions src/CET.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,13 @@ bool CET::IsRunning() noexcept

CET::CET()
: m_options(m_paths)
, m_bindings(m_paths)
, m_bindings(m_paths, m_options)
, m_window(&m_overlay, &m_bindings, &m_d3d12)
, m_d3d12(m_window, m_paths, m_options)
, m_vm(m_paths, m_bindings, m_d3d12, m_options)
, m_overlay(m_d3d12, m_bindings, m_options, m_vm)
, m_tasks(m_options)
{
m_bindings.Bind(m_options.OverlayKeyBind, m_overlay.GetBind());
m_bindings.ConnectUpdate(m_d3d12);

m_vm.Initialize();
Expand Down
49 changes: 22 additions & 27 deletions src/Options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@

void Options::Load()
{
IsFirstLaunch = !exists(m_paths.Config());
if (!IsFirstLaunch)
if (exists(m_paths.Config()))
{
std::ifstream configFile(m_paths.Config());
if(configFile)
{
auto config = nlohmann::json::parse(configFile);
PatchEnableDebug = config.value("enable_debug", PatchEnableDebug);
PatchRemovePedestrians = config.value("remove_pedestrians", PatchRemovePedestrians);
PatchSkipStartMenu = config.value("skip_start_menu", PatchSkipStartMenu);
PatchAmdSmt = config.value("amd_smt", PatchAmdSmt);
Expand All @@ -22,44 +20,32 @@ void Options::Load()
PatchDisableVignette = config.value("disable_vignette", PatchDisableVignette);
PatchDisableBoundaryTeleport = config.value("disable_boundary_teleport", PatchDisableBoundaryTeleport);
PatchDisableWin7Vsync = config.value("disable_win7_vsync", PatchDisableWin7Vsync);


RemoveDeadBindings = config.value("cetdev_remove_dead_bindings", RemoveDeadBindings);
EnableImGuiAssertions = config.value("cetdev_enable_imgui_assertions", EnableImGuiAssertions);
DumpGameOptions = config.value("dump_game_options", DumpGameOptions);
PatchEnableDebug = config.value("enable_debug", PatchEnableDebug);

// font config
FontPath = config.value("font_path", FontPath);
FontGlyphRanges = config.value("font_glyph_ranges", FontGlyphRanges);
FontSize = config.value("font_size", FontSize);

OverlayKeyBind = config.value("overlay_key", OverlayKeyBind);
if (OverlayKeyBind == 0)
IsFirstLaunch = true; // is for sure in this case

if (exists(m_paths.CETRoot() / "hotkeys.json"))
{
// encoded key bind was 32-bit number in old config, convert it to new 64-bit format
OverlayKeyBind =
{
((OverlayKeyBind & 0x000000FF) << 8*0)
| ((OverlayKeyBind & 0x0000FF00) << 8*1)
| ((OverlayKeyBind & 0x00FF0000) << 8*2)
| ((OverlayKeyBind & 0xFF000000) << 8*3)
};
}

// check old config names
if (config.value("unlock_menu", false))
PatchEnableDebug = true;
}
configFile.close();
}

// set global "Enable ImGui Assertions"
g_ImGuiAssertionsEnabled = EnableImGuiAssertions;
}

void Options::Save()
{
nlohmann::json config;

config["overlay_key"] = OverlayKeyBind;
config["enable_debug"] = PatchEnableDebug;

config["remove_pedestrians"] = PatchRemovePedestrians;
config["disable_async_compute"] = PatchAsyncCompute;
config["disable_antialiasing"] = PatchAntialiasing;
Expand All @@ -69,18 +55,25 @@ void Options::Save()
config["disable_vignette"] = PatchDisableVignette;
config["disable_boundary_teleport"] = PatchDisableBoundaryTeleport;
config["disable_win7_vsync"] = PatchDisableWin7Vsync;

config["cetdev_remove_dead_bindings"] = RemoveDeadBindings;
config["cetdev_enable_imgui_assertions"] = EnableImGuiAssertions;
config["enable_debug"] = PatchEnableDebug;
config["dump_game_options"] = DumpGameOptions;

config["font_path"] = FontPath;
config["font_glyph_ranges"] = FontGlyphRanges;
config["font_size"] = FontSize;

std::ofstream o(m_paths.Config());
o << config.dump(4) << std::endl;

// set global "Enable ImGui Assertions"
g_ImGuiAssertionsEnabled = EnableImGuiAssertions;
}

void Options::ResetToDefaults()
{
PatchEnableDebug = false;
PatchRemovePedestrians = false;
PatchAsyncCompute = false;
PatchAntialiasing = false;
Expand All @@ -90,6 +83,10 @@ void Options::ResetToDefaults()
PatchDisableVignette = false;
PatchDisableBoundaryTeleport = false;
PatchDisableWin7Vsync = false;

RemoveDeadBindings = true;
EnableImGuiAssertions = false;
PatchEnableDebug = false;
DumpGameOptions = false;

Save();
Expand Down Expand Up @@ -166,7 +163,5 @@ Options::Options(Paths& aPaths)
}

Load();

if (!IsFirstLaunch)
Save();
Save();
}
6 changes: 4 additions & 2 deletions src/Options.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ struct Options
void ResetToDefaults();

Image GameImage;
uint64_t OverlayKeyBind{ 0 };
bool PatchEnableDebug{ false };
bool PatchRemovePedestrians{ false };
bool PatchAsyncCompute{ false };
Expand All @@ -30,7 +29,10 @@ struct Options
std::string FontGlyphRanges{""};
float FontSize{ 13.0f };
bool ExeValid{ false };
bool IsFirstLaunch{ false };
bool IsFirstLaunch { true };
bool RemoveDeadBindings { true };
bool DrawImGuiDiagnosticWindow { false };
bool EnableImGuiAssertions { false };

private:

Expand Down
109 changes: 68 additions & 41 deletions src/VKBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ uint64_t VKBindInfo::Apply()
return CodeBind;
}

VKBindings::VKBindings(Paths& aPaths)
VKBindings::VKBindings(Paths& aPaths, const Options& acOptions)
: m_paths(aPaths)
, m_cOptions(acOptions)
{
}

Expand Down Expand Up @@ -58,58 +59,67 @@ std::vector<VKBindInfo> VKBindings::InitializeMods(std::vector<VKBindInfo> aVKBi
vkBindInfo.CodeBind = 0;
}

// now, find all dead bindings
std::vector<std::pair<std::string, UINT>> deadIDToBinds;
for (auto& idToBind : m_idToBind)
// filter dead bindings if option is enabled (default)
if (m_cOptions.RemoveDeadBindings)
{
// always ignore internal CET binds here!
if (!idToBind.first.compare(0, 4, "cet."))
continue;

// TODO - try to avoid O(n^2) situation here
auto found = false;
for (auto& vkBindInfo : aVKBindInfos)
// now, find all dead bindings
std::vector<std::pair<std::string, UINT>> deadIDToBinds;
for (auto& idToBind : m_idToBind)
{
found = (idToBind.first == vkBindInfo.Bind.ID);
if (found)
// always ignore internal CET binds here!
if (!idToBind.first.compare(0, 4, "cet."))
continue;

// TODO - try to avoid O(n^2) situation here
auto found = false;
for (auto& vkBindInfo : aVKBindInfos)
{
// test if bind is hotkey and if not, check if bind is valid for input (which means it has single input key, not combo)
found = (vkBindInfo.Bind.IsHotkey() || ((idToBind.second & 0xFFFF000000000000) == idToBind.second));
break; // we just reset found flag accordingly and exit here, we found valid entry, no need to continue regardless of result
found = (idToBind.first == vkBindInfo.Bind.ID);
if (found)
{
// test if bind is hotkey and if not, check if bind is valid for input (which means it has single input key, not combo)
found = (vkBindInfo.Bind.IsHotkey() || ((idToBind.second & 0xFFFF000000000000) == idToBind.second));
break; // we just reset found flag accordingly and exit here, we found valid entry, no need to continue regardless of result
}
}

if (!found)
deadIDToBinds.emplace_back(idToBind);
}

if (!found)
deadIDToBinds.emplace_back(idToBind);
}
// and remove them
for (auto& idToBind : deadIDToBinds)
{
m_idToBind.erase(idToBind.first);
m_binds.erase(idToBind.second);
}

// and remove them
for (auto& idToBind : deadIDToBinds)
{
m_idToBind.erase(idToBind.first);
m_binds.erase(idToBind.second);
// finally, save our filtered bindings back to not lose them
Save();
}

// finally, save our filtered bindings back to not lose them
Save();
// insert CET overlay bind info
assert(m_pOverlay); // overlay must be set before first use!
const auto overlayKeyBind { m_cpOverlay->GetBind() };
const auto overlayKeyCodeIt { m_idToBind.find(overlayKeyBind.ID) };
const auto overlayKeyCode { (overlayKeyCodeIt == m_idToBind.cend()) ? (0) : (overlayKeyCodeIt->second) };
aVKBindInfos.insert(aVKBindInfos.cbegin(), VKBindInfo{overlayKeyBind, overlayKeyCode, overlayKeyCode, false});

// return corrected bindings
return aVKBindInfos;
}

void VKBindings::Load(const Overlay& aOverlay)
bool VKBindings::Load(const Overlay& acOverlay)
{
auto parseConfig {[this, &aOverlay](const std::filesystem::path& path, bool old = false) -> bool {
auto parseConfig {[this, &acOverlay](const std::filesystem::path& path, bool old = false) -> std::pair<bool, uint64_t> {
std::ifstream ifs { path };
if (ifs)
{
auto config { nlohmann::json::parse(ifs) };
VKBind overlayBind { acOverlay.GetBind() };
uint64_t overlayBindCode { 0 };
for (auto& it : config.items())
{
// properly auto-bind Overlay if it is present here (could be this is first start so it may not be
// in here yet)
auto vkBind { (it.key() == aOverlay.GetBind().ID) ? aOverlay.GetBind() : VKBind{ it.key() } };

uint64_t key { it.value() };
if (old)
{
Expand All @@ -123,29 +133,46 @@ void VKBindings::Load(const Overlay& aOverlay)
};
}

// properly auto-bind Overlay if it is present here (could be this is first start so it may not be
// in here yet)
VKBind vkBind { it.key() };
if (it.key() == overlayBind.ID)
{
vkBind = overlayBind;
overlayBindCode = key;
}

const auto ret { Bind(key, vkBind) };
assert(ret); // we want this to never fail!
}
return true;
return std::make_pair(true, overlayBindCode);
}
return false;
return std::make_pair(false, 0);
}};

// try to load from current path
if (!parseConfig(m_paths.VKBindings()))
auto [res, key] = parseConfig(m_paths.VKBindings());
if (!res)
{
// if failed, try to look for old config
auto oldPath = m_paths.CETRoot() / "hotkeys.json";
if (parseConfig(oldPath, true))
const auto [resOld, keyOld] = parseConfig(oldPath, true);
if (resOld)
{
// old config found and parsed, remove it and save it to current location
remove(oldPath);
Save();

// replace former res and key with ones from old config
res = resOld;
key = keyOld;
}
}

m_pOverlay = &aOverlay;
m_cpOverlay = &acOverlay;
m_initialized = true;

return res && key;
}

void VKBindings::Save()
Expand Down Expand Up @@ -557,7 +584,7 @@ LRESULT VKBindings::RecordKeyDown(USHORT aVKCode)

if (bind && (bind != VKBRecord_OK))
{
if (m_pOverlay && m_pOverlay->IsEnabled())
if (m_cpOverlay && m_cpOverlay->IsEnabled())
return 0; // we dont want to handle bindings if overlay is open and we are not in binding state!

if (bind->IsValid()) // prevention for freshly loaded bind from file without rebinding
Expand Down Expand Up @@ -601,7 +628,7 @@ LRESULT VKBindings::RecordKeyUp(USHORT aVKCode)
{
if (cpBind->IsInput())
{
if (m_pOverlay && !m_pOverlay->IsEnabled()) // we dont want to handle bindings if overlay is open
if (m_cpOverlay && !m_cpOverlay->IsEnabled()) // we dont want to handle bindings if overlay is open
{
if (cpBind->IsValid()) // prevention for freshly loaded bind from file without rebinding
m_queuedCallbacks.Add(cpBind->DelayedCall(false));
Expand Down Expand Up @@ -645,7 +672,7 @@ LRESULT VKBindings::RecordKeyUp(USHORT aVKCode)
const auto bind = m_binds.find(m_recordingResult);
if (bind != m_binds.end())
{
if (m_pOverlay && m_pOverlay->IsEnabled() && (bind->second.ID != m_pOverlay->GetBind().ID))
if (m_cpOverlay && m_cpOverlay->IsEnabled() && (bind->second.ID != m_cpOverlay->GetBind().ID))
return 0; // we dont want to handle bindings if toolbar is open and we are not in binding state!

if (bind->second.IsValid()) // prevention for freshly loaded bind from file without rebinding
Expand Down Expand Up @@ -713,7 +740,7 @@ LRESULT VKBindings::HandleRAWInput(HRAWINPUT ahRAWInput)
}
else if (raw->header.dwType == RIM_TYPEMOUSE)
{
if (m_pOverlay && m_isBindRecording && (m_recordingBind.ID == m_pOverlay->GetBind().ID))
if (m_cpOverlay && m_isBindRecording && (m_recordingBind.ID == m_cpOverlay->GetBind().ID))
return 0; // ignore mouse keys for toolbar key binding!

const auto& m = raw->data.mouse;
Expand Down
8 changes: 5 additions & 3 deletions src/VKBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ constexpr USHORT VKBC_MWHEELDOWN { RI_MOUSE_WHEEL | 0 };
constexpr USHORT VKBC_MWHEELRIGHT { RI_MOUSE_HWHEEL | 1 };
constexpr USHORT VKBC_MWHEELLEFT { RI_MOUSE_HWHEEL | 0 };

struct Options;
struct Overlay;
struct D3D12;
struct VKBindings
{
VKBindings(Paths& aPaths);
VKBindings(Paths& aPaths, const Options& acOptions);
~VKBindings() = default;

[[nodiscard]] bool IsInitialized() const noexcept;
Expand All @@ -87,7 +88,7 @@ struct VKBindings
static uint64_t EncodeVKCodeBind(VKCodeBindDecoded aVKCodeBindDecoded);
static const char* GetSpecialKeyName(USHORT aVKCode);

void Load(const Overlay& aOverlay);
bool Load(const Overlay& acOverlay);
void Save();

void Update();
Expand Down Expand Up @@ -141,7 +142,8 @@ struct VKBindings
bool m_initialized{ false };

Paths& m_paths;
const Overlay* m_pOverlay{ nullptr };
const Options& m_cOptions;
const Overlay* m_cpOverlay{ nullptr };

size_t m_connectUpdate{ static_cast<size_t>(-1) };
};
2 changes: 1 addition & 1 deletion src/imgui_impl/imgui_user_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// NOTE: imgui_user_config.h is included by imgui.h which is included with precompiled header, so no need to include it here once more

// global definition "Enable ImGui Assertions"
bool g_ImGuiAssertionsEnabled{ true };
bool g_ImGuiAssertionsEnabled{ false };

#ifdef NDEBUG
// inline _wassert decl for NDEBUG as it is not emitted inside assert.h header in this case
Expand Down
Loading

0 comments on commit 650077b

Please sign in to comment.