From fd2ea47f74ee1124c73a85f298739a5eac9ddc71 Mon Sep 17 00:00:00 2001 From: Adubbz Date: Tue, 13 Aug 2019 22:58:49 +1000 Subject: [PATCH] lr: Introducing registered data --- .../ncm/source/impl/lr_redirection.cpp | 50 ---------- .../ncm/source/impl/lr_redirection.hpp | 19 ---- .../ncm/source/impl/lr_registered_data.hpp | 99 +++++++++++++++++++ .../lr_addoncontentlocationresolver.cpp | 13 ++- .../lr_addoncontentlocationresolver.hpp | 6 +- .../source/lr_registeredlocationresolver.cpp | 29 +++--- .../source/lr_registeredlocationresolver.hpp | 6 +- 7 files changed, 122 insertions(+), 100 deletions(-) create mode 100644 stratosphere/ncm/source/impl/lr_registered_data.hpp diff --git a/stratosphere/ncm/source/impl/lr_redirection.cpp b/stratosphere/ncm/source/impl/lr_redirection.cpp index 240e0f575..9af961411 100644 --- a/stratosphere/ncm/source/impl/lr_redirection.cpp +++ b/stratosphere/ncm/source/impl/lr_redirection.cpp @@ -75,56 +75,6 @@ namespace sts::lr::impl { } } - bool RegisteredLocationRedirector::FindRedirection(Path *out, ncm::TitleId title_id) { - auto redirection = this->redirections.Find(title_id); - if (redirection) { - *out = *redirection; - return true; - } - - return false; - } - - bool RegisteredLocationRedirector::SetRedirection(ncm::TitleId title_id, const Path& path) { - if (this->redirections.IsFull()) { - return false; - } - - this->redirections[title_id] = path; - return true; - } - - void RegisteredLocationRedirector::EraseRedirection(ncm::TitleId title_id) { - this->redirections.Remove(title_id); - } - - void RegisteredLocationRedirector::ClearRedirections() { - this->redirections.RemoveAll(); - } - - bool AddOnContentRedirector::FindRedirection(ncm::StorageId *out, ncm::TitleId title_id) { - auto redirection = this->redirections.Find(title_id); - - if (redirection) { - *out = *redirection; - return true; - } - - return false; - } - - Result AddOnContentRedirector::SetRedirection(ncm::TitleId title_id, ncm::StorageId storage_id) { - if (this->redirections.IsFull()) { - return ResultLrTooManyRegisteredPaths; - } - - this->redirections[title_id] = storage_id; - return ResultSuccess; - } - - void AddOnContentRedirector::ClearRedirections() { - this->redirections.RemoveAll(); - } } \ No newline at end of file diff --git a/stratosphere/ncm/source/impl/lr_redirection.hpp b/stratosphere/ncm/source/impl/lr_redirection.hpp index 834dffe54..fccd85c0d 100644 --- a/stratosphere/ncm/source/impl/lr_redirection.hpp +++ b/stratosphere/ncm/source/impl/lr_redirection.hpp @@ -56,23 +56,4 @@ namespace sts::lr::impl { void ClearRedirections(u32 flags = RedirectionFlags_None); }; - class RegisteredLocationRedirector { - private: - BoundedMap redirections; - public: - bool FindRedirection(Path *out, ncm::TitleId title_id); - bool SetRedirection(ncm::TitleId title_id, const Path& path); - void EraseRedirection(ncm::TitleId title_id); - void ClearRedirections(); - }; - - class AddOnContentRedirector { - private: - BoundedMap redirections; - public: - bool FindRedirection(ncm::StorageId *out, ncm::TitleId title_id); - Result SetRedirection(ncm::TitleId title_id, ncm::StorageId storage_id); - void ClearRedirections(); - }; - } \ No newline at end of file diff --git a/stratosphere/ncm/source/impl/lr_registered_data.hpp b/stratosphere/ncm/source/impl/lr_registered_data.hpp new file mode 100644 index 000000000..5d54b8e16 --- /dev/null +++ b/stratosphere/ncm/source/impl/lr_registered_data.hpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 2019 Adubbz + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include + +#include "../lr_types.hpp" + +namespace sts::lr::impl { + + template + class RegisteredData { + NON_COPYABLE(RegisteredData); + NON_MOVEABLE(RegisteredData); + private: + struct Entry { + Value value; + Key key; + bool is_valid; + }; + private: + Entry entries[NumEntries]; + public: + RegisteredData() { + this->Clear(); + } + + bool Register(const Key &key, const Value &value) { + /* Try to find an existing value. */ + for (size_t i = 0; i < NumEntries; i++) { + Entry& entry = this->entries[i]; + if (entry.is_valid && entry.key == key) { + entry.value = value; + return true; + } + } + + for (size_t i = 0; i < NumEntries; i++) { + Entry& entry = this->entries[i]; + if (!entry.is_valid) { + entry.key = key; + entry.value = value; + entry.is_valid = true; + return true; + } + } + + return false; + } + + void Unregister(const Key &key) { + for (size_t i = 0; i < NumEntries; i++) { + Entry& entry = this->entries[i]; + if (entry.is_valid && entry.key == key) { + entry.is_valid = false; + } + } + } + + bool Find(Value *out, const Key &key) { + for (size_t i = 0; i < NumEntries; i++) { + Entry& entry = this->entries[i]; + if (entry.is_valid && entry.key == key) { + *out = entry.value; + return true; + } + } + + return false; + } + + void Clear() { + for (size_t i = 0; i < NumEntries; i++) { + this->entries[i].is_valid = false; + } + } + }; + + template + using RegisteredLocations = RegisteredData; + + template + using RegisteredStorages = RegisteredData; + +} \ No newline at end of file diff --git a/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp b/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp index a6051bdc9..4f346968d 100644 --- a/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp +++ b/stratosphere/ncm/source/lr_addoncontentlocationresolver.cpp @@ -19,15 +19,11 @@ namespace sts::lr { - AddOnContentLocationResolverInterface::AddOnContentLocationResolverInterface() { - this->redirector.ClearRedirections(); - } - Result AddOnContentLocationResolverInterface::ResolveAddOnContentPath(OutPointerWithServerSize out, ncm::TitleId tid) { Path path; ncm::StorageId storage_id = ncm::StorageId::None; - if (!this->redirector.FindRedirection(&storage_id, tid)) { + if (!this->registered_storages.Find(&storage_id, tid)) { return ResultLrAddOnContentNotFound; } @@ -45,12 +41,15 @@ namespace sts::lr { } Result AddOnContentLocationResolverInterface::RegisterAddOnContentStorage(ncm::StorageId storage_id, ncm::TitleId tid) { - R_TRY(this->redirector.SetRedirection(tid, storage_id)); + if (!this->registered_storages.Register(tid, storage_id)) { + return ResultLrTooManyRegisteredPaths; + } + return ResultSuccess; } Result AddOnContentLocationResolverInterface::UnregisterAllAddOnContentPath() { - this->redirector.ClearRedirections(); + this->registered_storages.Clear(); return ResultSuccess; } diff --git a/stratosphere/ncm/source/lr_addoncontentlocationresolver.hpp b/stratosphere/ncm/source/lr_addoncontentlocationresolver.hpp index b0d779430..cb9056b6d 100644 --- a/stratosphere/ncm/source/lr_addoncontentlocationresolver.hpp +++ b/stratosphere/ncm/source/lr_addoncontentlocationresolver.hpp @@ -18,7 +18,7 @@ #include #include -#include "impl/lr_redirection.hpp" +#include "impl/lr_registered_data.hpp" #include "lr_types.hpp" namespace sts::lr { @@ -31,9 +31,7 @@ namespace sts::lr { UnregisterAllAddOnContentPath = 2, }; private: - impl::AddOnContentRedirector redirector; - public: - AddOnContentLocationResolverInterface(); + impl::RegisteredStorages registered_storages; public: virtual Result ResolveAddOnContentPath(OutPointerWithServerSize out, ncm::TitleId tid); virtual Result RegisterAddOnContentStorage(ncm::StorageId storage_id, ncm::TitleId tid); diff --git a/stratosphere/ncm/source/lr_registeredlocationresolver.cpp b/stratosphere/ncm/source/lr_registeredlocationresolver.cpp index a21c41120..5d7af37ff 100644 --- a/stratosphere/ncm/source/lr_registeredlocationresolver.cpp +++ b/stratosphere/ncm/source/lr_registeredlocationresolver.cpp @@ -18,11 +18,6 @@ namespace sts::lr { - RegisteredLocationResolverInterface::RegisteredLocationResolverInterface() { - this->registered_program_redirector.ClearRedirections(); - this->registered_html_docs_redirector.ClearRedirections(); - } - RegisteredLocationResolverInterface::~RegisteredLocationResolverInterface() { /* Ensure entries are deallocated */ this->html_docs_redirector.ClearRedirections(); @@ -33,7 +28,7 @@ namespace sts::lr { Path path; if (!this->program_redirector.FindRedirection(&path, tid)) { - if (!this->registered_program_redirector.FindRedirection(&path, tid)) { + if (!this->registered_program_locations.Find(&path, tid)) { return ResultLrProgramNotFound; } } @@ -45,16 +40,16 @@ namespace sts::lr { Result RegisteredLocationResolverInterface::RegisterProgramPath(InPointer path, ncm::TitleId tid) { Path tmp_path = *path.pointer; - if (!this->registered_program_redirector.SetRedirection(tid, tmp_path)) { - this->registered_program_redirector.ClearRedirections(); - this->registered_program_redirector.SetRedirection(tid, tmp_path); + if (!this->registered_program_locations.Register(tid, tmp_path)) { + this->registered_program_locations.Clear(); + this->registered_program_locations.Register(tid, tmp_path); } return ResultSuccess; } Result RegisteredLocationResolverInterface::UnregisterProgramPath(ncm::TitleId tid) { - this->registered_program_redirector.EraseRedirection(tid); + this->registered_program_locations.Unregister(tid); return ResultSuccess; } @@ -68,7 +63,7 @@ namespace sts::lr { Path path; if (!this->html_docs_redirector.FindRedirection(&path, tid)) { - if (!this->registered_html_docs_redirector.FindRedirection(&path, tid)) { + if (!this->registered_html_docs_locations.Find(&path, tid)) { return ResultLrProgramNotFound; } } @@ -80,16 +75,16 @@ namespace sts::lr { Result RegisteredLocationResolverInterface::RegisterHtmlDocumentPath(InPointer path, ncm::TitleId tid) { Path tmp_path = *path.pointer; - if (!this->registered_html_docs_redirector.SetRedirection(tid, tmp_path)) { - this->registered_html_docs_redirector.ClearRedirections(); - this->registered_html_docs_redirector.SetRedirection(tid, tmp_path); + if (!this->registered_html_docs_locations.Register(tid, tmp_path)) { + this->registered_html_docs_locations.Clear(); + this->registered_html_docs_locations.Register(tid, tmp_path); } return ResultSuccess; } Result RegisteredLocationResolverInterface::UnregisterHtmlDocumentPath(ncm::TitleId tid) { - this->registered_html_docs_redirector.EraseRedirection(tid); + this->registered_html_docs_locations.Unregister(tid); return ResultSuccess; } @@ -100,8 +95,8 @@ namespace sts::lr { } Result RegisteredLocationResolverInterface::Refresh() { - this->registered_program_redirector.ClearRedirections(); - this->registered_html_docs_redirector.ClearRedirections(); + this->registered_program_locations.Clear(); + this->registered_html_docs_locations.Clear(); return ResultSuccess; } diff --git a/stratosphere/ncm/source/lr_registeredlocationresolver.hpp b/stratosphere/ncm/source/lr_registeredlocationresolver.hpp index 3c40f9462..cc0516272 100644 --- a/stratosphere/ncm/source/lr_registeredlocationresolver.hpp +++ b/stratosphere/ncm/source/lr_registeredlocationresolver.hpp @@ -19,6 +19,7 @@ #include #include "impl/lr_redirection.hpp" +#include "impl/lr_registered_data.hpp" #include "lr_types.hpp" namespace sts::lr { @@ -38,11 +39,10 @@ namespace sts::lr { }; private: impl::LocationRedirector program_redirector; - impl::RegisteredLocationRedirector registered_program_redirector; + impl::RegisteredLocations registered_program_locations; impl::LocationRedirector html_docs_redirector; - impl::RegisteredLocationRedirector registered_html_docs_redirector; + impl::RegisteredLocations registered_html_docs_locations; public: - RegisteredLocationResolverInterface(); ~RegisteredLocationResolverInterface(); Result ResolveProgramPath(OutPointerWithServerSize out, ncm::TitleId tid);