forked from sc2ad/beatsaber-hook
-
Notifications
You must be signed in to change notification settings - Fork 9
Flamingo priority and uninstalls #55
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
Open
Fernthedev
wants to merge
8
commits into
master
Choose a base branch
from
more-flamingo
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a14c42
Initial flamingo draft
Fernthedev 9367e94
Finish flamingo reordering implementation
Fernthedev 8921f92
Refactor FlamingoHandleHelper to use std::optional for handle managem…
Fernthedev 5e38f2f
Redo hook API for better ergonomics
Fernthedev 52fb59e
Pragma once header
Fernthedev a6da168
Move Result to bs_hook, alias for temporary measures
Fernthedev 678bac5
Namespaced hooks by default
Fernthedev 709108e
Add example using string namespace
Fernthedev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #pragma once | ||
|
|
||
| #include <iterator> | ||
| #include <optional> | ||
| #include <string_view> | ||
| #include <variant> | ||
| #include "flamingo/shared/hook-data.hpp" | ||
| #include "flamingo/shared/hook-installation-result.hpp" | ||
| #include "flamingo/shared/installer.hpp" | ||
| #include "flamingo/shared/target-data.hpp" | ||
|
|
||
| #include "./result.hpp" | ||
|
|
||
| namespace bs_hook { | ||
| struct FlamingoHandle; | ||
|
|
||
| struct FlamingoHandleBuilder { | ||
| Paper::LoggerContext logger; | ||
| flamingo::HookInfo hookInfo; | ||
|
|
||
| FlamingoHandleBuilder(Paper::LoggerContext const& log, flamingo::HookInfo info) : logger(log), hookInfo(std::move(info)) { | ||
| hookInfo.metadata.priority.is_final = false; | ||
| } | ||
| FlamingoHandleBuilder(FlamingoHandleBuilder const&) = default; | ||
| FlamingoHandleBuilder(FlamingoHandleBuilder&&) = default; | ||
| ~FlamingoHandleBuilder() = default; | ||
|
|
||
| FlamingoHandleBuilder& operator=(FlamingoHandleBuilder const&) = default; | ||
| FlamingoHandleBuilder& operator=(FlamingoHandleBuilder&&) = default; | ||
|
|
||
| FlamingoHandleBuilder& final(bool isFinal = true) { | ||
| hookInfo.metadata.priority.is_final = isFinal; | ||
| return *this; | ||
| } | ||
|
|
||
| FlamingoHandleBuilder& after(modloader::ModInfo const& info, std::string_view name = {}) { | ||
| after(info.id, name); | ||
| return *this; | ||
| } | ||
|
|
||
| FlamingoHandleBuilder& after(std::string_view modID, std::string_view name = {}) { | ||
| hookInfo.metadata.priority.afters.emplace_back(flamingo::HookNameMetadata{ | ||
| .name = std::string(name), | ||
| .namespaze = std::string(modID), | ||
| }); | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| FlamingoHandleBuilder& before(modloader::ModInfo const& info, std::string_view name = {}) { | ||
| before(info.id, name); | ||
| return *this; | ||
| } | ||
|
|
||
| FlamingoHandleBuilder& before(std::string_view modID, std::string_view name = {}) { | ||
| hookInfo.metadata.priority.befores.emplace_back(flamingo::HookNameMetadata{ .name = std::string(name), .namespaze = std::string(modID) }); | ||
| return *this; | ||
| } | ||
|
|
||
| [[nodiscard]] | ||
| bs_hook::Result<FlamingoHandle, flamingo::installation::Error> installOrError() noexcept; | ||
|
|
||
| [[nodiscard]] | ||
| FlamingoHandle install(); | ||
| }; | ||
|
|
||
| struct FlamingoHandle { | ||
| Paper::LoggerContext logger; | ||
| flamingo::HookInfo info; | ||
| flamingo::HookHandle handle; | ||
|
|
||
| FlamingoHandle(Paper::LoggerContext logger, flamingo::HookHandle h, flamingo::HookInfo i) : logger(logger), info(i), handle(h) {} | ||
|
|
||
| FlamingoHandle(FlamingoHandle const&) = delete; | ||
| FlamingoHandle(FlamingoHandle&&) = default; | ||
|
|
||
| FlamingoHandle& operator=(FlamingoHandle const&) = delete; | ||
| FlamingoHandle& operator=(FlamingoHandle&&) = default; | ||
|
|
||
| ~FlamingoHandle() = default; | ||
|
|
||
| operator flamingo::HookHandle() const { | ||
| return handle; | ||
| } | ||
|
|
||
| [[nodiscard]] | ||
| flamingo::HookHandle const& get_handle() const { | ||
| return handle; | ||
| } | ||
|
|
||
| /// @brief Uninstalls the hook associated with this handle. | ||
| [[nodiscard]] | ||
| bs_hook::Result<FlamingoHandleBuilder, std::monostate> uninstall() { | ||
| using Result = bs_hook::Result<FlamingoHandleBuilder, std::monostate>; | ||
| auto result = flamingo::Uninstall(handle); | ||
| if (result.has_value()) { | ||
| return Result::Ok(FlamingoHandleBuilder(logger, info)); | ||
| } | ||
| return Result::Err(std::monostate{}); | ||
| } | ||
|
|
||
| [[nodiscard]] | ||
| auto reinstall() { | ||
| auto result = flamingo::Reinstall({ handle.hook_location->target }); | ||
| return result; | ||
| } | ||
| }; | ||
|
|
||
| inline bs_hook::Result<FlamingoHandle, flamingo::installation::Error> FlamingoHandleBuilder::installOrError() noexcept { | ||
| using Result = bs_hook::Result<FlamingoHandle, flamingo::installation::Error>; | ||
|
|
||
| logger.info("Installing hook: {} to offset: {}", hookInfo.metadata.name_info, fmt::ptr(hookInfo.target)); | ||
| auto install_result = flamingo::Install(std::move(hookInfo)); | ||
| if (install_result.has_value()) { | ||
| return Result::Ok(FlamingoHandle(logger, install_result.value().returned_handle, std::move(hookInfo))); | ||
| } else { | ||
| return Result::Err(install_result.error()); | ||
| } | ||
| } | ||
|
|
||
| inline FlamingoHandle FlamingoHandleBuilder::install() { | ||
| auto result = installOrError(); | ||
| if (!result.has_result()) { | ||
| logger.critical("Failed to install hook: {} with flamingo: {}", hookInfo.metadata.name_info, result.get_exception()); | ||
| SAFE_ABORT(); | ||
| } | ||
|
|
||
| return result.move_result(); | ||
| } | ||
| } // namespace bs_hook | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need pragma once