Skip to content

Commit

Permalink
ci: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jan 18, 2024
1 parent 2894f26 commit 48c8143
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 71 deletions.
56 changes: 29 additions & 27 deletions include/Hooks.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
#pragma once
#include "Distributor.h"

namespace Hooks {

struct InitItemImpl
{
static void Thunk(RE::TESObjectREFR* a_ref)
{
func(a_ref);

if (a_ref->HasContainer()) {
Distributor::RuntimeDistribute(a_ref);
}
}
static inline REL::Relocation<decltype(Thunk)> func;
static inline constexpr std::size_t idx{ 0x13 };

static void Install()
{
stl::write_vfunc<RE::TESObjectREFR, InitItemImpl>();
logger::info("Installed Object Reference hook");
}
};

void Install();
}
#pragma once
#include "Distributor.h"

namespace Hooks
{

struct InitItemImpl
{
static void Thunk(RE::TESObjectREFR* a_ref)
{
func(a_ref);

if (a_ref->HasContainer()) {
Distributor::RuntimeDistribute(a_ref);
}
}

inline static REL::Relocation<decltype(Thunk)> func;
inline static constexpr std::size_t idx{ 0x13 };

static void Install()
{
stl::write_vfunc<RE::TESObjectREFR, InitItemImpl>();
logger::info("Installed Object Reference hook");
}
};

void Install();
} // namespace Hooks
23 changes: 12 additions & 11 deletions include/Maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class DistrObject
std::optional<int> chance{};
};

class RuntimeDistrMap {
class RuntimeDistrMap
{
public:
std::vector<DistrObject> to_add;
std::vector<DistrObject> to_remove;
Expand Down Expand Up @@ -126,19 +127,19 @@ inline auto format_as(const DistrType& type)
inline auto format_as(const DistrToken& token)
{
const auto& [type, filename, to_identifier, identifier, count, rhs, rhs_count, chance]{ token };
return fmt::format("[Type: {} / Filename: {} / To: {} / Identifier: {} / Count: {} / RHS: {} / RHS Count: {} / Chance: {}]", type, filename, to_identifier, identifier, count.value_or(-1),
rhs.value_or("null"), rhs_count.value_or(-1), chance.value_or(100));
return fmt::format("[Type: {} / Filename: {} / To: {} / Identifier: {} / Count: {} / RHS: {} / RHS Count: {} / Chance: {}]", type, filename, to_identifier, identifier,
count.value_or(-1), rhs.value_or("null"), rhs_count.value_or(-1), chance.value_or(100));
}

inline auto format_as(const DistrObject& obj)
{
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ obj };
return fmt::format("[Type: {} / Filename: {} / Bound object: {} (0x{:x}) / Leveled list: {} (0x{:x} / Replace with obj: {} (0x{:x}) / Replace with list: {} (0x{:x}) "
"/ Count: {} / Replace count: {} / Container: {} (0x{:x}) ({})] / Chance: {}",
type, filename, bound_object ? bound_object->GetName() : "null", bound_object ? bound_object->GetFormID() : 0,
leveled_list ? leveled_list->GetFormEditorID() : "null", leveled_list ? leveled_list->GetFormID() : 0,
replace_with_obj ? replace_with_obj->GetName() : "null", replace_with_obj ? replace_with_obj->GetFormID() : 0,
replace_with_list ? replace_with_list->GetName() : "null", replace_with_list ? replace_with_list->GetFormID() : 0, count.value_or(-1),
replace_with_count.value_or(-1), container.has_value() ? container.value().container_name : "null",
container.has_value() ? container.value().container_form_id : 0, container.has_value() ? container->container_type : RE::FormType::Container, chance.value_or(100));
return fmt::format(
"[Type: {} / Filename: {} / Bound object: {} (0x{:x}) / Leveled list: {} (0x{:x} / Replace with obj: {} (0x{:x}) / Replace with list: {} (0x{:x}) "
"/ Count: {} / Replace count: {} / Container: {} (0x{:x}) ({})] / Chance: {}",
type, filename, bound_object ? bound_object->GetName() : "null", bound_object ? bound_object->GetFormID() : 0, leveled_list ? leveled_list->GetFormEditorID() : "null",
leveled_list ? leveled_list->GetFormID() : 0, replace_with_obj ? replace_with_obj->GetName() : "null", replace_with_obj ? replace_with_obj->GetFormID() : 0,
replace_with_list ? replace_with_list->GetName() : "null", replace_with_list ? replace_with_list->GetFormID() : 0, count.value_or(-1), replace_with_count.value_or(-1),
container.has_value() ? container.value().container_name : "null", container.has_value() ? container.value().container_form_id : 0,
container.has_value() ? container->container_type : RE::FormType::Container, chance.value_or(100));
}
29 changes: 16 additions & 13 deletions include/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class Utility : public Singleton<Utility>
{
static auto IsEditorID(const std::string_view identifier) { return std::strchr(identifier.data(), '~') == nullptr; }



static FormIDAndPluginName GetFormIDAndPluginName(const std::string_view identifier)
{
if (const auto tilde{ std::strchr(identifier.data(), '~') }) {
Expand Down Expand Up @@ -83,17 +81,19 @@ class Utility : public Singleton<Utility>
public:
static auto CachePlayerLevel() { player_level = RE::PlayerCharacter::GetSingleton()->GetLevel(); }

static int GetRandomChance() {
std::random_device dev;
std::mt19937 rng(dev());
static int GetRandomChance()
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distr(0, 100);

return distr(rng);
}

static int GetRandomCount(int count, unsigned int chance) {
std::random_device dev;
std::mt19937 rng(dev());
static int GetRandomCount(int count, unsigned int chance)
{
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> distr(0, 100);

int actual_count = 0;
Expand All @@ -106,11 +106,12 @@ class Utility : public Singleton<Utility>
return actual_count;
}

static int GetChance(const std::string& str) {
const auto quest_pos{ Maps::GetPos(str, '?') };
static int GetChance(const std::string& str)
{
const auto quest_pos{ Maps::GetPos(str, '?') };

logger::info("Has Chance: {}", quest_pos != ULLONG_MAX);
const auto chance = quest_pos == ULLONG_MAX ? 100 : Maps::ToInt(str.substr(quest_pos + 1));
const auto chance = quest_pos == ULLONG_MAX ? 100 : Maps::ToInt(str.substr(quest_pos + 1));

return chance;
}
Expand Down Expand Up @@ -147,10 +148,12 @@ class Utility : public Singleton<Utility>
if (const auto cont{ GetContainer(distr_token.to_identifier) }; cont.container) {
if (distr_token.type <=> DistrType::Replace == 0 || distr_token.type <=> DistrType::ReplaceAll == 0) {
if (const auto replace_with_list{ GetLevItem(distr_token.rhs.value()) })
return { distr_token.type, bound_obj, nullptr, distr_token.filename, nullptr, replace_with_list, distr_token.count, distr_token.rhs_count, cont, distr_token.chance };
return { distr_token.type, bound_obj, nullptr, distr_token.filename, nullptr, replace_with_list, distr_token.count,
distr_token.rhs_count, cont, distr_token.chance };

if (const auto replace_with_obj{ GetBoundObject(distr_token.rhs.value()) })
return { distr_token.type, bound_obj, nullptr, distr_token.filename, replace_with_obj, nullptr, distr_token.count, distr_token.rhs_count, cont, distr_token.chance };
return { distr_token.type, bound_obj, nullptr, distr_token.filename, replace_with_obj, nullptr, distr_token.count,
distr_token.rhs_count, cont, distr_token.chance };
}
return { distr_token.type, bound_obj, nullptr, distr_token.filename, nullptr, nullptr, distr_token.count, std::nullopt, cont, distr_token.chance };
}
Expand Down
18 changes: 7 additions & 11 deletions src/Distributor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,8 @@ void Distributor::ReplaceDistribute(const Maps::TDistrVec& distr_vec) noexcept
logger::info("^-------Finished REPLACE distribution in {} us-------^", elapsed.count());
}



void Distributor::RuntimeDistribute(RE::TESObjectREFR* a_ref) {
void Distributor::RuntimeDistribute(RE::TESObjectREFR* a_ref)
{
if (!a_ref)
return;

Expand All @@ -198,30 +197,27 @@ void Distributor::RuntimeDistribute(RE::TESObjectREFR* a_ref) {
auto counts = a_ref->GetInventoryCounts();

for (auto& distr_obj : to_modify->to_add) {
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance] { distr_obj };
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ distr_obj };

const auto num_to_add = Utility::GetRandomCount(count.value(), chance.value());

logger::info("Adding {} {} to {}", num_to_add, bound_object->GetFormID(), a_ref->GetFormID());

a_ref->AddObjectToContainer(bound_object, nullptr, num_to_add, nullptr);

}

for (auto& distr_obj : to_modify->to_remove) {
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance] { distr_obj };
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ distr_obj };

const auto num_to_remove = Utility::GetRandomCount(count.value(), chance.value());

logger::info("Removing {} {} from {}", num_to_remove, bound_object->GetFormID(), a_ref->GetFormID());

a_ref->RemoveItem(bound_object, num_to_remove, RE::ITEM_REMOVE_REASON::kRemove, nullptr, nullptr);

}

for (auto& distr_obj : to_modify->to_remove_all) {
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance] { distr_obj };

const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ distr_obj };

if (Utility::GetRandomChance() < chance.value()) {
const auto num_to_remove = counts[bound_object];
Expand All @@ -231,7 +227,7 @@ void Distributor::RuntimeDistribute(RE::TESObjectREFR* a_ref) {
}

for (auto& distr_obj : to_modify->to_replace) {
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance] { distr_obj };
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ distr_obj };

const auto num_to_swap = Utility::GetRandomCount(count.value(), chance.value());

Expand All @@ -242,7 +238,7 @@ void Distributor::RuntimeDistribute(RE::TESObjectREFR* a_ref) {
}

for (auto& distr_obj : to_modify->to_replace_all) {
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance] { distr_obj };
const auto& [type, bound_object, leveled_list, filename, replace_with_obj, replace_with_list, count, replace_with_count, container, chance]{ distr_obj };

if (Utility::GetRandomChance() < chance.value()) {
const auto num_to_swap = counts[bound_object];
Expand Down
11 changes: 6 additions & 5 deletions src/Hooks.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "Hooks.h"

void Hooks::Install() {
Hooks::InitItemImpl::Install();
}
#include "Hooks.h"

void Hooks::Install()
{
Hooks::InitItemImpl::Install();
}
3 changes: 1 addition & 2 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "Conflicts.h"
#include "Distributor.h"
#include "Hooks.h"
#include "Logging.h"
#include "Settings.h"
#include "Hooks.h"

void Listener(SKSE::MessagingInterface::Message* message) noexcept
{
Expand All @@ -29,7 +29,6 @@ SKSEPluginLoad(const SKSE::LoadInterface* skse)

Init(skse);


if (const auto messaging{ SKSE::GetMessagingInterface() }; !messaging->RegisterListener(Listener))
return false;

Expand Down
5 changes: 3 additions & 2 deletions src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ void Parser::ParseINIs(CSimpleIniA& ini) noexcept
logger::debug("\t* Key {}:", key);
for (const auto& [type, filename, to_identifier, identifier, count, rhs, rhs_count, chance] : distr_token_vec) {
if (count.has_value())
logger::debug("\t\t^ REPLACE {} {} with {} {} in {} with chance {}", count.value(), identifier, rhs_count.value_or(-1), rhs.value(), to_identifier, chance.value_or(100));
logger::debug("\t\t^ REPLACE {} {} with {} {} in {} with chance {}", count.value(), identifier, rhs_count.value_or(-1), rhs.value(), to_identifier,
chance.value_or(100));
else
logger::debug("\t\t^ REPLACE ALL {} with {} {} in {} with chance {}", identifier, rhs_count.value_or(-1), rhs.value(), to_identifier, chance.value_or(100));
}
Expand All @@ -162,7 +163,7 @@ DistrToken Parser::Tokenize(const std::string& str, std::string_view to_containe
const auto& identifier{ str.substr(0, bar_pos) };
const auto count{ Maps::ToInt(str.substr(bar_pos + 1)) };

DistrToken distr_token{ DistrType::Add, filename, to_container.data(), identifier, count, std::nullopt, std::nullopt, Utility::GetChance(str)};
DistrToken distr_token{ DistrType::Add, filename, to_container.data(), identifier, count, std::nullopt, std::nullopt, Utility::GetChance(str) };

return distr_token;
}
Expand Down

0 comments on commit 48c8143

Please sign in to comment.