Skip to content
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
2 changes: 2 additions & 0 deletions src/mod/features/features.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ extern const Feature kHavenHistory;

// hotkeys.cc
extern const Feature kHotkeys;
// Arms the tutorial-input override before the Settings section transition begins.
void PrepareSettingsInput();
// The shortcut dispatch for one frame, run by the frame tick. True when the game's own ScreenManager.Update must
// not run, so it does not handle the key as well. False until kHotkeys is installed.
bool HandleShortcuts();
Expand Down
94 changes: 92 additions & 2 deletions src/mod/features/hotkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,81 @@ namespace
// The frame tick dispatches shortcuts whether or not this feature installed; this says whether it did.
bool hotkeys_installed = false;
std::optional<std::chrono::steady_clock::time_point> hotkeys_escape_first_press;
il2cpp::GcHandle suspended_tutorial_mask;
std::optional<bool> suspended_tutorial_active;
std::optional<bool> suspended_tutorial_input;
std::optional<bool> suspended_touch_block;
bool settings_input_requested = false;
bool settings_input_entered = false;
int settings_input_wait = 0;

void RestoreTutorialInput()
{
auto* object = suspended_tutorial_mask.Get<unity::GameObject>();
suspended_tutorial_mask.Reset();
if (unity::Alive(object))
object->SetActive(true);
if (suspended_tutorial_active) {
TutorialManager::SetActive(*suspended_tutorial_active);
suspended_tutorial_active.reset();
}
if (suspended_tutorial_input) {
TutorialManager::SetBlockingInput(*suspended_tutorial_input);
suspended_tutorial_input.reset();
}
if (suspended_touch_block) {
TouchKit::SetBlockTouch(*suspended_touch_block);
suspended_touch_block.reset();
}
}

void ReleaseSettingsInput()
{
if (!suspended_tutorial_active)
suspended_tutorial_active = TutorialManager::Active();
TutorialManager::SetActive(false);
if (!suspended_tutorial_input)
suspended_tutorial_input = TutorialManager::BlockingInput();
TutorialManager::SetBlockingInput(false);
if (!suspended_touch_block)
suspended_touch_block = TouchKit::BlockTouch();
TouchKit::SetBlockTouch(false);
}

void ResetHotkeys()
{ hotkeys_escape_first_press.reset(); }
{
RestoreTutorialInput();
settings_input_requested = false;
settings_input_entered = false;
settings_input_wait = 0;
hotkeys_escape_first_press.reset();
}

void SettingsTutorialInputFrame()
{
if (Hub::CurrentSection() != SectionID::GameSettings) {
if (settings_input_entered || (settings_input_requested && --settings_input_wait == 0)) {
RestoreTutorialInput();
settings_input_requested = false;
settings_input_entered = false;
}
return;
}
settings_input_entered = true;
ReleaseSettingsInput();
if (suspended_tutorial_mask)
return;

auto* manager = Hub::Class::Get<"TutorialManager", TutorialManager*>();
auto* loader = manager ? manager->loadAndShow() : nullptr;
auto* controller = loader ? loader->Controller() : nullptr;
auto* object = unity::ActiveAndEnabled(controller) ? controller->gameObject() : nullptr;
if (!object)
return;

suspended_tutorial_mask = il2cpp::GcHandle::Strong(object);
object->SetActive(false);
}

// --- Setting shortcuts ---

Expand Down Expand Up @@ -118,10 +190,19 @@ void ShortcutsManager_InitializeActions_Hook(auto original, ShortcutsManager* _t
original(_this);
}

bool TutorialManager_get_IsBlockingInput_Hook(auto original)
{
// A screen opened directly by a mod shortcut is not one of the tutorial's highlighted controls. Let Settings
// receive pointer input while it is open, then restore the tutorial's normal restriction as soon as it closes.
return settings_input_requested || Hub::CurrentSection() == SectionID::GameSettings ? false : original();
}

bool InstallHotkeys()
{
il2cpp::HookSet hooks("Hotkeys");
IL2CPP_STAGE(hooks, ShortcutsManager::Class::Method("InitializeActions"), ShortcutsManager_InitializeActions_Hook);
IL2CPP_STAGE(hooks, TutorialManager::Class::StaticMethod<bool()>("get_IsBlockingInput"),
TutorialManager_get_IsBlockingInput_Hook);
// The badges only make sense when the game's own input actions are under the mod's control.
if (hooks.Commit())
InstallKeybindBadges();
Expand All @@ -133,6 +214,14 @@ bool InstallHotkeys()
}
} // namespace

void PrepareSettingsInput()
{
ReleaseSettingsInput();
settings_input_requested = true;
settings_input_entered = false;
settings_input_wait = 300;
}

bool HandleShortcuts()
{
if (!hotkeys_installed)
Expand Down Expand Up @@ -208,4 +297,5 @@ bool HandleShortcuts()
return FleetActionShortcuts() || SwallowEscape();
}

const Feature kHotkeys{"Hotkeys", &Config::Patches::hotkeyhooks, InstallHotkeys, nullptr, ResetHotkeys};
const Feature kHotkeys{"Hotkeys", &Config::Patches::hotkeyhooks, InstallHotkeys, SettingsTutorialInputFrame,
ResetHotkeys};
8 changes: 7 additions & 1 deletion src/mod/features/screens.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ void ShowCoordinateSearch()
}
}

void ShowSettings()
{
PrepareSettingsInput();
Hub::GotoSection(SectionID::GameSettings);
}

// Opens Manage Ships for the fleet selected in the fleet bar. False when none is selected.
bool OpenManageShips()
{
Expand Down Expand Up @@ -202,7 +208,7 @@ constexpr ScreenShortcut kScreens[] = {
{Action::kShowAlliance, Goto<SectionID::Alliance_Main>},
{Action::kShowAllianceHelp, Goto<SectionID::Alliance_Help>},
{Action::kShowAllianceArmada, Goto<SectionID::Alliance_Armadas>},
{Action::kShowSettings, Goto<SectionID::GameSettings>},
{Action::kShowSettings, ShowSettings},
{Action::kShowShips, [] { OpenManageShips(); }, false},
};

Expand Down
55 changes: 55 additions & 0 deletions src/mod/game/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// the services the mod touches that belong to no screen (tutorial gates, audio, the Windows window procedure).

#include <il2cpp/il2cpp.h>
#include <unity/core.h>

#include <algorithm>
#include <array>
Expand Down Expand Up @@ -261,6 +262,60 @@ struct Hub {
}
};

// The tutorial mask is a full-screen canvas that routes pointer input only to the highlighted control.
struct TutorialMaskUILoader : unity::Behaviour {
using Class = il2cpp::Class<"Assembly-CSharp", "Digit.Prime.Tutorial", "TutorialMaskUILoader">;

unity::Behaviour* Controller()
{
return Get<"Controller", unity::Behaviour*>();
}
};

struct TutorialManager : il2cpp::Object {
using Class = il2cpp::Class<"Assembly-CSharp", "Digit.Prime.Tutorial", "TutorialManager">;

static bool Active()
{
return Class::Field<"_isActive", bool>();
}

static void SetActive(bool active)
{
Class::SetField<"_isActive">(active);
}

static bool BlockingInput()
{
return Class::Field<"_isBlockingInput", bool>();
}

static void SetBlockingInput(bool blocked)
{
Class::SetField<"_isBlockingInput">(blocked);
}

TutorialMaskUILoader* loadAndShow()
{
return Field<"_loadAndShow", TutorialMaskUILoader*>();
}
};

// TouchKit's global pointer-input gate. Tutorials hold it while only their highlighted control may receive input.
struct TouchKit {
using Class = il2cpp::Class<"TouchKit", "", "TouchKit">;

static bool BlockTouch()
{
return Class::Field<"BlockTouch", bool>();
}

static void SetBlockTouch(bool blocked)
{
Class::SetField<"BlockTouch">(blocked);
}
};

// Fabric.EventManager, the game's sound middleware. Its shorter PostEvent overloads funnel into the
// seven-argument one.
using AudioEventManager = il2cpp::Class<"Fabric.Core", "Fabric", "EventManager">;
Expand Down
Loading