From db646bcb740bf350487a54c0d38fdb90e1780bcb Mon Sep 17 00:00:00 2001 From: FakeMichau <49685661+FakeMichau@users.noreply.github.com> Date: Tue, 3 Nov 2026 16:11:03 +0100 Subject: [PATCH] Small api changes and bug fixes --- OptiScaler/Config.cpp | 6 +++ OptiScaler/plugins/PluginApi.cpp | 76 ++++++++++++++++++++---------- plugin_sdk/README.md | 6 +-- plugin_sdk/optiscaler_plugin.h | 22 ++++----- plugin_sdk/sample/SamplePlugin.cpp | 14 ++---- 5 files changed, 72 insertions(+), 52 deletions(-) diff --git a/OptiScaler/Config.cpp b/OptiScaler/Config.cpp index 0cfae531..fa7af4fe 100644 --- a/OptiScaler/Config.cpp +++ b/OptiScaler/Config.cpp @@ -864,6 +864,8 @@ bool Config::LoadFromPath(const wchar_t* InPath) std::filesystem::path iniPath(InPath); auto newPath = iniPath / fileName; + std::lock_guard lock(iniMutex); + if (Reload(newPath)) { absoluteFileName = newPath; @@ -1212,6 +1214,8 @@ static std::optional UnpackOverlayValues(std::string_view data) std::string Config::ExportOverlaySettings() { + std::lock_guard lock(iniMutex); + WriteOverlaySettings(); std::string data; @@ -1256,6 +1260,8 @@ bool Config::ImportOverlaySettings(std::string_view code) bool Config::ApplyOverlaySettings(std::string_view text) { + std::lock_guard lock(iniMutex); + // Known keys only, their values are checked as the ini's are std::vector> values; std::istringstream entries { std::string(text) }; diff --git a/OptiScaler/plugins/PluginApi.cpp b/OptiScaler/plugins/PluginApi.cpp index c4b825d7..75c895e8 100644 --- a/OptiScaler/plugins/PluginApi.cpp +++ b/OptiScaler/plugins/PluginApi.cpp @@ -11,8 +11,16 @@ struct OptiPlugin { + enum class InitState + { + Running, + Accepted, + Declined, + }; + std::string name; std::string section; // Plugin. in OptiScaler.ini + InitState initState = InitState::Running; }; namespace @@ -31,6 +39,7 @@ struct PluginTab std::vector> _plugins; std::vector _initedModules; std::vector> _tabs; +std::vector> _pendingTabs; std::mutex _mutex; // State of the draw callback that is running, widgets do nothing without it @@ -42,7 +51,6 @@ struct TabDrawState }; thread_local TabDrawState* _draw = nullptr; -float _menuScale = 1.0f; // Plugin code is called through these, a crash in it disables the plugin instead of the game bool CallDraw(OptiDrawTabFn draw, void* userData) @@ -165,7 +173,18 @@ bool Api_RegisterTab(OptiPlugin* plugin, const char* name, const char* descripti tab->userData = userData; std::lock_guard lock(_mutex); - _tabs.push_back(std::move(tab)); + + switch (plugin->initState) + { + case OptiPlugin::InitState::Running: + _pendingTabs.push_back(std::move(tab)); + break; + case OptiPlugin::InitState::Accepted: + _tabs.push_back(std::move(tab)); + break; + default: + return false; + } LOG_INFO("[{}] Registered tab: {}", plugin->name, name); @@ -177,7 +196,15 @@ void Api_Section(const char* title) if (_draw == nullptr) return; + // Card border is drawn when the card closes, it shouldn't take the alpha of disabled widgets + if (_draw->disabled) + ImGui::EndDisabled(); + _draw->card.reset(); + + if (_draw->disabled) + ImGui::BeginDisabled(); + _draw->card.emplace(title); } @@ -268,8 +295,10 @@ bool Api_SliderInt(const char* label, int32_t* value, int32_t min, int32_t max, return ImGui::SliderInt(label, value, min, max, format != nullptr ? format : "%d", ImGuiSliderFlags_AlwaysClamp); } -bool Api_ComboEx(const char* label, int32_t* current, const char* const* items, int32_t itemCount, - uint64_t disabledItemsMask) +bool Api_ItemDeactivatedAfterEdit() { return _draw != nullptr && ImGui::IsItemDeactivatedAfterEdit(); } + +bool Api_Combo(const char* label, int32_t* current, const char* const* items, int32_t itemCount, + const bool* itemDisabled) { if (_draw == nullptr || label == nullptr || current == nullptr || (items == nullptr && itemCount > 0)) return false; @@ -284,8 +313,7 @@ bool Api_ComboEx(const char* label, int32_t* current, const char* const* items, { ImGui::PushID(i); - const bool disabled = i < 64 && (disabledItemsMask & (1ull << i)) != 0; - ImGui::BeginDisabled(disabled); + ImGui::BeginDisabled(itemDisabled != nullptr && itemDisabled[i]); const bool selected = *current == i; if (ImGui::Selectable(items[i] != nullptr ? items[i] : "", selected) && !selected) @@ -307,13 +335,6 @@ bool Api_ComboEx(const char* label, int32_t* current, const char* const* items, return changed; } -bool Api_Combo(const char* label, int32_t* current, const char* const* items, int32_t itemCount) -{ - return Api_ComboEx(label, current, items, itemCount, 0); -} - -bool Api_ItemDeactivatedAfterEdit() { return _draw != nullptr && ImGui::IsItemDeactivatedAfterEdit(); } - bool Api_InputText(const char* label, char* buffer, size_t bufferSize) { if (_draw == nullptr || label == nullptr || buffer == nullptr || bufferSize == 0) @@ -330,8 +351,6 @@ bool Api_ColorEdit(const char* label, float* rgba, bool withAlpha) return withAlpha ? ImGui::ColorEdit4(label, rgba) : ImGui::ColorEdit3(label, rgba); } -float Api_GetMenuScale() { return _menuScale; } - bool Api_IsMenuOpen() { return MenuCommon::IsVisible(); } bool Api_GetBool(OptiPlugin* plugin, const char* key, bool defaultValue) @@ -447,6 +466,7 @@ const OptiPluginApi _api = { .Spacing = Api_Spacing, .Disabled = Api_Disabled, .ItemTooltip = Api_ItemTooltip, + .ItemDeactivatedAfterEdit = Api_ItemDeactivatedAfterEdit, .Text = Api_Text, .TextDisabled = Api_TextDisabled, @@ -460,7 +480,6 @@ const OptiPluginApi _api = { .InputText = Api_InputText, .ColorEdit = Api_ColorEdit, - .GetMenuScale = Api_GetMenuScale, .IsMenuOpen = Api_IsMenuOpen, .GetBool = Api_GetBool, @@ -475,9 +494,6 @@ const OptiPluginApi _api = { .SaveSettings = Api_SaveSettings, .Log = Api_Log, - - .ComboEx = Api_ComboEx, - .ItemDeactivatedAfterEdit = Api_ItemDeactivatedAfterEdit, }; } // namespace @@ -521,16 +537,26 @@ void PluginApi::InitPlugin(HMODULE module, PFN_OptiPluginInit init, const std::f bool accepted = false; const bool finished = CallInit(init, &_api, plugin, &accepted); - if (finished && accepted) - return; - if (!finished) LOG_ERROR("[{}] Crashed in OptiPluginInit, its tabs are removed", plugin->name); - else + else if (!accepted) LOG_INFO("[{}] Declined the plugin API", plugin->name); std::lock_guard lock(_mutex); - std::erase_if(_tabs, [plugin](const auto& tab) { return tab->plugin == plugin; }); + + // Declined plugins can't register tabs later either + plugin->initState = finished && accepted ? OptiPlugin::InitState::Accepted : OptiPlugin::InitState::Declined; + + if (plugin->initState == OptiPlugin::InitState::Accepted) + { + for (const auto& tab : _pendingTabs) + { + if (tab->plugin == plugin) + _tabs.push_back(tab); + } + } + + std::erase_if(_pendingTabs, [plugin](const auto& tab) { return tab->plugin == plugin; }); } std::vector PluginApi::GetTabs() @@ -558,8 +584,6 @@ void PluginApi::DrawTab(size_t index, float menuScale, ImVec4 (*colorMap)(const tab = _tabs[index]; } - _menuScale = menuScale; - if (tab->faulted) { ImGui::TextColored(colorMap(ImVec4(1.0f, 0.4f, 0.4f, 1.0f)), "%s", diff --git a/plugin_sdk/README.md b/plugin_sdk/README.md index bc12e08c..b1047fb3 100644 --- a/plugin_sdk/README.md +++ b/plugin_sdk/README.md @@ -17,16 +17,16 @@ ASI plugins loaded by OptiScaler can add their own tabs to the OptiScaler menu a } ``` - OptiScaler calls it once, right after `InitializeASI`. Return `false` to opt out; any tabs you registered are then removed. + OptiScaler calls it once, right after `InitializeASI`. Tabs registered here show up once it returns `true`. Return `false` to opt out; the tabs you registered are then dropped and `RegisterTab` fails from then on. 2. Put the `.asi` file in the `plugins` folder and set `LoadAsiPlugins=true` in `OptiScaler.ini`. 3. Your tabs appear under **PLUGINS** in the menu sidebar. ## Drawing -Inside a draw callback you can use `Section`, `Text`, `Checkbox`, `SliderFloat`/`SliderInt`, `Combo`/`ComboEx`, `InputText`, `ColorEdit`, `Button`, `SameLine`, `Disabled`, `ItemTooltip` and `ItemDeactivatedAfterEdit`. +Inside a draw callback you can use `Section`, `Text`, `Checkbox`, `SliderFloat`/`SliderInt`, `Combo`, `InputText`, `ColorEdit`, `Button`, `SameLine`, `Disabled`, `ItemTooltip` and `ItemDeactivatedAfterEdit`. - `Section` starts a card like the ones in OptiScaler's own tabs. -- `ComboEx` is a combo whose items can be greyed out. `ItemDeactivatedAfterEdit` is true on the frame a slider is released, for values that should apply once instead of on every frame of a drag. +- `Combo` takes an optional array of bools that greys out items. `ItemDeactivatedAfterEdit` is true on the frame a slider is released, for values that should apply once instead of on every frame of a drag. - Cards, disabled blocks and IDs are closed by OptiScaler after the callback, so a plugin can't leave ImGui in a broken state. - Widget functions do nothing when called outside a draw callback or from another thread. - Draw callbacks run on the render thread while the menu is open. Keep them fast, don't call D3D/Vulkan from them, and protect any data you share with your own threads. diff --git a/plugin_sdk/optiscaler_plugin.h b/plugin_sdk/optiscaler_plugin.h index 4460bf70..c8f113c2 100644 --- a/plugin_sdk/optiscaler_plugin.h +++ b/plugin_sdk/optiscaler_plugin.h @@ -82,6 +82,9 @@ extern "C" void (*Disabled)(bool disabled); // Shows a tooltip when the previous widget is hovered void (*ItemTooltip)(const char* text); + // True on the frame the previous widget stopped being edited, a slider was released for example. + // Lets a plugin apply a value once instead of on every frame of a drag. + bool (*ItemDeactivatedAfterEdit)(void); // Text, only inside a draw callback ----------------------------------------------------------------- @@ -98,8 +101,11 @@ extern "C" bool (*Checkbox)(const char* label, bool* value); // true when changed bool (*SliderFloat)(const char* label, float* value, float min, float max, const char* format); bool (*SliderInt)(const char* label, int32_t* value, int32_t min, int32_t max, const char* format); - // items is an array of itemCount strings, current is the selected index - bool (*Combo)(const char* label, int32_t* current, const char* const* items, int32_t itemCount); + // items is an array of itemCount strings, current is the selected index. + // itemDisabled is NULL or an array of itemCount bools, true greys out the item. The selected item is + // shown even when it's disabled. + bool (*Combo)(const char* label, int32_t* current, const char* const* items, int32_t itemCount, + const bool* itemDisabled); // buffer is a zero terminated UTF-8 string that is edited in place bool (*InputText)(const char* label, char* buffer, size_t bufferSize); // rgba is float[4] in 0.0 - 1.0 @@ -107,8 +113,6 @@ extern "C" // Menu state ---------------------------------------------------------------------------------------- - // UI scale of the menu, multiply custom sizes by it - float (*GetMenuScale)(void); bool (*IsMenuOpen)(void); // Settings in [Plugin.] of OptiScaler.ini --------------------------------------------------- @@ -136,16 +140,6 @@ extern "C" // Writes to the OptiScaler log, prefixed with the plugin name void (*Log)(OptiPlugin* plugin, OptiLogLevel level, const char* message); - // More widgets, only inside a draw callback --------------------------------------------------------- - - // Combo with greyed out items, bit i of disabledItemsMask disables items[i] (the first 64 items). - // The selected item is shown even when it's disabled. - bool (*ComboEx)(const char* label, int32_t* current, const char* const* items, int32_t itemCount, - uint64_t disabledItemsMask); - // True on the frame the previous widget stopped being edited, a slider was released for example. - // Lets a plugin apply a value once instead of on every frame of a drag. - bool (*ItemDeactivatedAfterEdit)(void); - } OptiPluginApi; // True when the running OptiScaler has the given field of OptiPluginApi diff --git a/plugin_sdk/sample/SamplePlugin.cpp b/plugin_sdk/sample/SamplePlugin.cpp index 575d9f67..a56e6d5e 100644 --- a/plugin_sdk/sample/SamplePlugin.cpp +++ b/plugin_sdk/sample/SamplePlugin.cpp @@ -121,9 +121,10 @@ void DrawMainTab(void* /*userData*/) } // With too few samples Ultra stays in the list but can't be picked - const uint64_t disabledQualities = settings.samples < ultraMinSamples ? 1ull << ultraQuality : 0; + bool disabledQualities[qualityCount] = {}; + disabledQualities[ultraQuality] = settings.samples < ultraMinSamples; - if (api->ComboEx("Quality", &settings.quality, qualityNames, qualityCount, disabledQualities)) + if (api->Combo("Quality", &settings.quality, qualityNames, qualityCount, disabledQualities)) { api->SetInt(self, "Quality", settings.quality); unsaved = true; @@ -199,14 +200,13 @@ void DrawInfoTab(void* userData) api->Separator(); - snprintf(text, sizeof(text), "Menu scale: %.2f, menu open: %s", api->GetMenuScale(), - api->IsMenuOpen() ? "yes" : "no"); + snprintf(text, sizeof(text), "Menu open: %s", api->IsMenuOpen() ? "yes" : "no"); api->Text(text); api->Section("Counter"); // Not kept in the ini, only for this session - api->Combo("Log clicks as", &clickLogLevel, logLevelNames, logLevelCount); + api->Combo("Log clicks as", &clickLogLevel, logLevelNames, logLevelCount, nullptr); // userData given to RegisterTab comes back here if (api->Button("Click me")) @@ -226,10 +226,6 @@ void DrawInfoTab(void* userData) extern "C" __declspec(dllexport) bool OptiPluginInit(const OptiPluginApi* optiApi, OptiPlugin* plugin) { - // An older OptiScaler doesn't have everything this plugin uses - if (!OPTI_API_HAS(optiApi, ItemDeactivatedAfterEdit)) - return false; - api = optiApi; self = plugin;