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

ConsoleEnablerMod overhaul #687

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions assets/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ The callback of `NotifyOnNewObject` can now optionally return `true` to unregist
### BPModLoader
BPModLoader now supports loading mods from subdirectories within the `LogicMods` folder ([UE4SS #412](https://github.com/UE4SS-RE/RE-UE4SS/pull/412)) - Ethan Green

### ConsoleEnablerMod
Added additional console key **~** (Tilde) ([UE4SS #687](https://github.com/UE4SS-RE/RE-UE4SS/pull/687))
The console keys **F10** and **~** are now added by the mod in addition to the existing keys instead of replacing them ([UE4SS #687](https://github.com/UE4SS-RE/RE-UE4SS/pull/687))

### Repo & Build Process
Switch to xmake from cmake which makes building much more streamlined ([UE4SS #377](https://github.com/UE4SS-RE/RE-UE4SS/pull/377), [UEPseudo #81](https://github.com/Re-UE4SS/UEPseudo/pull/81)) - localcc

Expand Down
60 changes: 36 additions & 24 deletions assets/Mods/ConsoleEnablerMod/Scripts/main.lua
Original file line number Diff line number Diff line change
@@ -1,42 +1,54 @@
local Engine = FindFirstOf("Engine")
local UEHelpers = require("UEHelpers")

local PlayerControllerHookActive = false
local WasFirstConsoleCreated = false

local function RemapConsoleKeys()
-- Change console key
local InputSettings = StaticFindObject("/Script/Engine.Default__InputSettings")
local InputSettings = StaticFindObject("/Script/Engine.Default__InputSettings") ---@cast InputSettings UInputSettings
if not InputSettings:IsValid() then print("[ConsoleEnabler] InputSettings not found, could not change console key\n") return end

local ConsoleKeys = InputSettings.ConsoleKeys

-- This sets the first console key to F10
ConsoleKeys[1].KeyName = FName("F10", EFindName.FNAME_Find)

ConsoleKeys:ForEach(function(index, elem_wrapper)
local KeyStruct = elem_wrapper:get()
local KeyFName = KeyStruct.KeyName
-- The ToString() call here will go bad if the FName is not ansi
print(string.format("[ConsoleEnabler] ConsoleKey[%d]: %s\n", index, KeyFName:ToString()))
end)

local KeysToAdd = {
UEHelpers.FindFName("Tilde"),
UEHelpers.FindFName("F10")
}

for _, KeyName in ipairs(KeysToAdd) do
if KeyName ~= NAME_None then
local KeyIsAlreadySet = false
for i = 1, #ConsoleKeys do
if ConsoleKeys[i].KeyName == KeyName then
KeyIsAlreadySet = true
end
end
if not KeyIsAlreadySet then
ConsoleKeys[#ConsoleKeys + 1].KeyName = KeyName
end
end
end

for i = 1, #ConsoleKeys do
print(string.format("[ConsoleEnabler] ConsoleKey[%d]: %s\n", i, ConsoleKeys[i].KeyName:ToString()))
end
end

local function CreateConsole()
if not Engine:IsValid() then
Engine = FindFirstOf("Engine")
end

local Engine = UEHelpers.GetEngine()
if not Engine:IsValid() then print("[ConsoleEnabler] Was unable to find an instance of UEngine\n") return end
local ConsoleClass = Engine.ConsoleClass

local ConsoleClass = Engine.ConsoleClass ---@type UClass
local GameViewport = Engine.GameViewport
if GameViewport.ViewportConsole:IsValid() then

if GameViewport:IsValid() and GameViewport.ViewportConsole:IsValid() then
-- Console already exists, let's just remap the keys
RemapConsoleKeys()
elseif ConsoleClass:IsValid() and GameViewport:IsValid() then
local CreatedConsole = StaticConstructObject(ConsoleClass, GameViewport, 0, 0, 0, nil, false, false, nil)
local CreatedConsole = StaticConstructObject(ConsoleClass, GameViewport) ---@cast CreatedConsole UConsole
if not CreatedConsole:IsValid() then print("[CreateConsole] Was unable to construct an UConsole object\n") return end

GameViewport.ViewportConsole = CreatedConsole

PlayerControllerHookActive = true
WasFirstConsoleCreated = true

Expand All @@ -48,7 +60,7 @@ end

CreateConsole()

NotifyOnNewObject("/Script/Engine.PlayerController", function(CreatedObject)
NotifyOnNewObject("/Script/Engine.PlayerController", function()
if PlayerControllerHookActive or not WasFirstConsoleCreated then
CreateConsole()
end
Expand Down