2
0
mirror of https://github.com/Laupetin/OpenAssetTools.git synced 2026-04-07 12:18:40 +00:00

refactor: use std::expected instead of custom implementation

This commit is contained in:
Jan Laupetin
2026-04-04 14:47:10 +02:00
parent bd6f42dc54
commit 4242c4a325
9 changed files with 124 additions and 308 deletions

View File

@@ -50,11 +50,11 @@ void FastFileContext::Destroy()
m_loaded_zones.clear(); m_loaded_zones.clear();
} }
result::Expected<LoadedZone*, std::string> FastFileContext::LoadFastFile(const std::string& path) std::expected<LoadedZone*, std::string> FastFileContext::LoadFastFile(const std::string& path)
{ {
auto zone = ZoneLoading::LoadZone(path, std::make_unique<LoadingEventProgressReporter>(fs::path(path).filename().replace_extension().string())); auto zone = ZoneLoading::LoadZone(path, std::make_unique<LoadingEventProgressReporter>(fs::path(path).filename().replace_extension().string()));
if (!zone) if (!zone)
return result::Unexpected(std::move(zone.error())); return std::unexpected(std::move(zone.error()));
auto loadedZone = std::make_unique<LoadedZone>(std::move(*zone), path); auto loadedZone = std::make_unique<LoadedZone>(std::move(*zone), path);
@@ -69,7 +69,7 @@ result::Expected<LoadedZone*, std::string> FastFileContext::LoadFastFile(const s
return result; return result;
} }
result::Expected<NoResult, std::string> FastFileContext::UnloadZone(const std::string& zoneName) std::expected<void, std::string> FastFileContext::UnloadZone(const std::string& zoneName)
{ {
{ {
std::lock_guard lock(m_zone_lock); std::lock_guard lock(m_zone_lock);
@@ -83,9 +83,9 @@ result::Expected<NoResult, std::string> FastFileContext::UnloadZone(const std::s
{ {
m_loaded_zones.erase(existingZone); m_loaded_zones.erase(existingZone);
ui::NotifyZoneUnloaded(zoneName); ui::NotifyZoneUnloaded(zoneName);
return NoResult(); return {};
} }
} }
return result::Unexpected(std::format("No zone with name {} loaded", zoneName)); return std::unexpected(std::format("No zone with name {} loaded", zoneName));
} }

View File

@@ -1,8 +1,8 @@
#pragma once #pragma once
#include "Utils/Result.h"
#include "Zone/Zone.h" #include "Zone/Zone.h"
#include <expected>
#include <memory> #include <memory>
#include <shared_mutex> #include <shared_mutex>
#include <vector> #include <vector>
@@ -21,8 +21,8 @@ class FastFileContext
public: public:
void Destroy(); void Destroy();
result::Expected<LoadedZone*, std::string> LoadFastFile(const std::string& path); std::expected<LoadedZone*, std::string> LoadFastFile(const std::string& path);
result::Expected<NoResult, std::string> UnloadZone(const std::string& zoneName); std::expected<void, std::string> UnloadZone(const std::string& zoneName);
std::vector<std::unique_ptr<LoadedZone>> m_loaded_zones; std::vector<std::unique_ptr<LoadedZone>> m_loaded_zones;
std::shared_mutex m_zone_lock; std::shared_mutex m_zone_lock;

View File

@@ -50,7 +50,7 @@ namespace
double m_last_progress; double m_last_progress;
}; };
result::Expected<NoResult, std::string> UnlinkZoneInDbThread(const std::string& zoneName) std::expected<void, std::string> UnlinkZoneInDbThread(const std::string& zoneName)
{ {
const auto& context = ModManContext::Get().m_fast_file; const auto& context = ModManContext::Get().m_fast_file;
const auto existingZone = std::ranges::find_if(context.m_loaded_zones, const auto existingZone = std::ranges::find_if(context.m_loaded_zones,
@@ -60,7 +60,7 @@ namespace
}); });
if (existingZone == context.m_loaded_zones.end()) if (existingZone == context.m_loaded_zones.end())
return result::Unexpected(std::format("No zone with name {} loaded", zoneName)); return std::unexpected(std::format("No zone with name {} loaded", zoneName));
const auto& loadedZone = *existingZone->get(); const auto& loadedZone = *existingZone->get();
@@ -75,7 +75,7 @@ namespace
*loadedZone.m_zone, outputFolderPathStr, outputFolderOutputPath, searchPaths, std::make_unique<UnlinkingEventProgressReporter>(zoneName)); *loadedZone.m_zone, outputFolderPathStr, outputFolderOutputPath, searchPaths, std::make_unique<UnlinkingEventProgressReporter>(zoneName));
objWriter->DumpZone(dumpingContext); objWriter->DumpZone(dumpingContext);
return NoResult(); return {};
} }
void UnlinkZone(webview::webview& wv, std::string id, std::string zoneName) // NOLINT(performance-unnecessary-value-param) Copy is made for thread safety void UnlinkZone(webview::webview& wv, std::string id, std::string zoneName) // NOLINT(performance-unnecessary-value-param) Copy is made for thread safety

View File

@@ -72,7 +72,7 @@ namespace
DetermineSupportedArgumentTypes(); DetermineSupportedArgumentTypes();
} }
result::Expected<NoResult, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override std::expected<void, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override
{ {
m_shader_type = shaderType; m_shader_type = shaderType;
m_shader_name = name; m_shader_name = name;
@@ -88,21 +88,21 @@ namespace
} }
if (!maybeShader) if (!maybeShader)
return result::Unexpected<std::string>("Failed to load shader"); return std::unexpected<std::string>("Failed to load shader");
m_bin = *maybeShader; m_bin = *maybeShader;
return NoResult{}; return {};
} }
result::Expected<NoResult, std::string> LeaveShader() override std::expected<void, std::string> LeaveShader() override
{ {
m_bin = {}; m_bin = {};
return AutoCreateMissingArgs(); return AutoCreateMissingArgs();
} }
result::Expected<NoResult, std::string> AcceptShaderConstantArgument(const techset::CommonShaderArgCreatorDestination& destination, std::expected<void, std::string> AcceptShaderConstantArgument(const techset::CommonShaderArgCreatorDestination& destination,
const techset::CommonCodeConstSource codeConstSource, const techset::CommonCodeConstSource codeConstSource,
const unsigned sourceIndex) override const unsigned sourceIndex) override
{ {
@@ -113,15 +113,15 @@ namespace
if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination))
{ {
if (!errorMessage.empty()) if (!errorMessage.empty())
return result::Unexpected(std::move(errorMessage)); return std::unexpected(std::move(errorMessage));
return result::Unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); return std::unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name));
} }
return AcceptShaderConstantArgument(commonDestination, isTransposed, rowCount, codeConstSource, sourceIndex); return AcceptShaderConstantArgument(commonDestination, isTransposed, rowCount, codeConstSource, sourceIndex);
} }
result::Expected<NoResult, std::string> AcceptShaderSamplerArgument(const techset::CommonShaderArgCreatorDestination& destination, std::expected<void, std::string> AcceptShaderSamplerArgument(const techset::CommonShaderArgCreatorDestination& destination,
const techset::CommonCodeSamplerSource codeSamplerSource) override const techset::CommonCodeSamplerSource codeSamplerSource) override
{ {
techset::CommonShaderArgDestination commonDestination{}; techset::CommonShaderArgDestination commonDestination{};
@@ -129,15 +129,15 @@ namespace
if (!FindDestinationForSampler(commonDestination, errorMessage, destination)) if (!FindDestinationForSampler(commonDestination, errorMessage, destination))
{ {
if (!errorMessage.empty()) if (!errorMessage.empty())
return result::Unexpected(std::move(errorMessage)); return std::unexpected(std::move(errorMessage));
return result::Unexpected(std::format("Could not find sampler shader input with name {}", destination.m_argument_name)); return std::unexpected(std::format("Could not find sampler shader input with name {}", destination.m_argument_name));
} }
return AcceptShaderSamplerArgument(commonDestination, codeSamplerSource); return AcceptShaderSamplerArgument(commonDestination, codeSamplerSource);
} }
result::Expected<NoResult, std::string> AcceptShaderLiteralArgument(const techset::CommonShaderArgCreatorDestination& destination, std::expected<void, std::string> AcceptShaderLiteralArgument(const techset::CommonShaderArgCreatorDestination& destination,
const std::array<float, 4>& literalValue) override const std::array<float, 4>& literalValue) override
{ {
techset::CommonShaderArgDestination commonDestination{}; techset::CommonShaderArgDestination commonDestination{};
@@ -147,9 +147,9 @@ namespace
if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination))
{ {
if (!errorMessage.empty()) if (!errorMessage.empty())
return result::Unexpected(std::move(errorMessage)); return std::unexpected(std::move(errorMessage));
return result::Unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name)); return std::unexpected(std::format("Could not find constant shader input with name {}", destination.m_argument_name));
} }
techset::CommonShaderArgumentType argumentType{ techset::CommonShaderArgumentType argumentType{
@@ -158,15 +158,16 @@ namespace
}; };
if (!IsArgumentTypeSupported(argumentType)) if (!IsArgumentTypeSupported(argumentType))
return result::Unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); return std::unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type)));
techset::CommonShaderArgValue value{.literal_value = literalValue}; techset::CommonShaderArgValue value{.literal_value = literalValue};
m_args.emplace_back(argumentType, commonDestination, value); m_args.emplace_back(argumentType, commonDestination, value);
return NoResult{};
return {};
} }
result::Expected<NoResult, std::string> AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, std::expected<void, std::string> AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination,
const unsigned nameHash) override const unsigned nameHash) override
{ {
techset::CommonShaderArgumentType argumentType{ techset::CommonShaderArgumentType argumentType{
@@ -181,39 +182,40 @@ namespace
if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination)) if (!FindDestinationForConstant(commonDestination, isTransposed, rowCount, errorMessage, destination))
{ {
if (!errorMessage.empty()) if (!errorMessage.empty())
return result::Unexpected(std::move(errorMessage)); return std::unexpected(std::move(errorMessage));
argumentType.m_value_type = techset::CommonShaderValueType::MATERIAL_SAMPLER; argumentType.m_value_type = techset::CommonShaderValueType::MATERIAL_SAMPLER;
if (!FindDestinationForSampler(commonDestination, errorMessage, destination)) if (!FindDestinationForSampler(commonDestination, errorMessage, destination))
{ {
if (!errorMessage.empty()) if (!errorMessage.empty())
return result::Unexpected(std::move(errorMessage)); return std::unexpected(std::move(errorMessage));
return result::Unexpected(std::format("Could not find shader input with name {}", destination.m_argument_name)); return std::unexpected(std::format("Could not find shader input with name {}", destination.m_argument_name));
} }
} }
if (!IsArgumentTypeSupported(argumentType)) if (!IsArgumentTypeSupported(argumentType))
{ {
return result::Unexpected( return std::unexpected(
std::format("{} {} are unsupported", ShaderTypeName(argumentType.m_shader_type), ArgTypeName(argumentType.m_value_type))); std::format("{} {} are unsupported", ShaderTypeName(argumentType.m_shader_type), ArgTypeName(argumentType.m_value_type)));
} }
techset::CommonShaderArgValue value{.name_hash = nameHash}; techset::CommonShaderArgValue value{.name_hash = nameHash};
m_args.emplace_back(argumentType, commonDestination, value); m_args.emplace_back(argumentType, commonDestination, value);
return NoResult{};
return {};
} }
result::Expected<NoResult, std::string> AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination, std::expected<void, std::string> AcceptShaderMaterialArgument(const techset::CommonShaderArgCreatorDestination& destination,
const std::string& nameValue) override const std::string& nameValue) override
{ {
// All game's call R_HashString here which has the same implementation in all games // All game's call R_HashString here which has the same implementation in all games
return AcceptShaderMaterialArgument(destination, djb2_xor_nocase(nameValue.c_str(), 0)); return AcceptShaderMaterialArgument(destination, djb2_xor_nocase(nameValue.c_str(), 0));
} }
result::Expected<NoResult, std::string> FinalizePass(techset::CommonTechnique& technique, techset::CommonPass& pass) override std::expected<void, std::string> FinalizePass(techset::CommonTechnique& technique, techset::CommonPass& pass) override
{ {
std::ranges::sort(m_args, std::ranges::sort(m_args,
[this](const techset::CommonShaderArg& arg0, const techset::CommonShaderArg& arg1) -> bool [this](const techset::CommonShaderArg& arg0, const techset::CommonShaderArg& arg1) -> bool
@@ -247,11 +249,11 @@ namespace
m_args = std::vector<techset::CommonShaderArg>(); m_args = std::vector<techset::CommonShaderArg>();
m_sampler_flags = 0; m_sampler_flags = 0;
return NoResult{}; return {};
} }
protected: protected:
result::Expected<NoResult, std::string> AcceptShaderConstantArgument(const techset::CommonShaderArgDestination& commonDestination, std::expected<void, std::string> AcceptShaderConstantArgument(const techset::CommonShaderArgDestination& commonDestination,
const bool isTransposed, const bool isTransposed,
const unsigned rowCount, const unsigned rowCount,
const techset::CommonCodeConstSource codeConstSource, const techset::CommonCodeConstSource codeConstSource,
@@ -263,11 +265,11 @@ namespace
}; };
if (!IsArgumentTypeSupported(argumentType)) if (!IsArgumentTypeSupported(argumentType))
return result::Unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type))); return std::unexpected(std::format("{} constants are unsupported", ShaderTypeName(argumentType.m_shader_type)));
const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeConstSource(codeConstSource); const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeConstSource(codeConstSource);
if (!maybeInfo) if (!maybeInfo)
return result::Unexpected<std::string>("Could not find info for code constant"); return std::unexpected<std::string>("Could not find info for code constant");
const auto isMatrix = maybeInfo->transposedMatrix.has_value(); const auto isMatrix = maybeInfo->transposedMatrix.has_value();
techset::CommonShaderArgCodeConstValue value{ techset::CommonShaderArgCodeConstValue value{
@@ -279,7 +281,7 @@ namespace
if (isMatrix) if (isMatrix)
{ {
if (sourceIndex >= 4) if (sourceIndex >= 4)
return result::Unexpected(std::format("Index for matrix code const is out of bounds: {} (must be < 4)", sourceIndex)); return std::unexpected(std::format("Index for matrix code const is out of bounds: {} (must be < 4)", sourceIndex));
value.m_index = isTransposed ? *maybeInfo->transposedMatrix : codeConstSource; value.m_index = isTransposed ? *maybeInfo->transposedMatrix : codeConstSource;
value.m_first_row = sourceIndex; value.m_first_row = sourceIndex;
@@ -288,7 +290,7 @@ namespace
{ {
const auto arrayCount = std::max<unsigned>(maybeInfo->arrayCount, 1u); const auto arrayCount = std::max<unsigned>(maybeInfo->arrayCount, 1u);
if (sourceIndex >= arrayCount) if (sourceIndex >= arrayCount)
return result::Unexpected(std::format("Index for code const is out of bounds: {} (must be < {})", sourceIndex, arrayCount)); return std::unexpected(std::format("Index for code const is out of bounds: {} (must be < {})", sourceIndex, arrayCount));
value.m_index = codeConstSource + static_cast<techset::CommonCodeConstSource>(sourceIndex); value.m_index = codeConstSource + static_cast<techset::CommonCodeConstSource>(sourceIndex);
value.m_first_row = 0; value.m_first_row = 0;
@@ -298,10 +300,10 @@ namespace
if (maybeInfo->techFlags && (!maybeInfo->techFlagShaderType || *maybeInfo->techFlagShaderType == m_shader_type)) if (maybeInfo->techFlags && (!maybeInfo->techFlagShaderType || *maybeInfo->techFlagShaderType == m_shader_type))
m_tech_flags |= *maybeInfo->techFlags; m_tech_flags |= *maybeInfo->techFlags;
return NoResult{}; return {};
} }
result::Expected<NoResult, std::string> AcceptShaderSamplerArgument(const techset::CommonShaderArgDestination& commonDestination, std::expected<void, std::string> AcceptShaderSamplerArgument(const techset::CommonShaderArgDestination& commonDestination,
const techset::CommonCodeSamplerSource codeSamplerSource) const techset::CommonCodeSamplerSource codeSamplerSource)
{ {
techset::CommonShaderArgumentType argumentType{ techset::CommonShaderArgumentType argumentType{
@@ -310,11 +312,11 @@ namespace
}; };
if (!IsArgumentTypeSupported(argumentType)) if (!IsArgumentTypeSupported(argumentType))
return result::Unexpected(std::format("{} samplers are unsupported", ShaderTypeName(argumentType.m_shader_type))); return std::unexpected(std::format("{} samplers are unsupported", ShaderTypeName(argumentType.m_shader_type)));
const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(codeSamplerSource); const auto maybeInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(codeSamplerSource);
if (!maybeInfo) if (!maybeInfo)
return result::Unexpected<std::string>("Could not find info for code sampler"); return std::unexpected<std::string>("Could not find info for code sampler");
m_args.emplace_back(argumentType, commonDestination, techset::CommonShaderArgValue{.code_sampler_source = codeSamplerSource}); m_args.emplace_back(argumentType, commonDestination, techset::CommonShaderArgValue{.code_sampler_source = codeSamplerSource});
if (maybeInfo->techFlags) if (maybeInfo->techFlags)
@@ -322,7 +324,7 @@ namespace
if (maybeInfo->customSamplerIndex) if (maybeInfo->customSamplerIndex)
m_sampler_flags |= (1 << *maybeInfo->customSamplerIndex); m_sampler_flags |= (1 << *maybeInfo->customSamplerIndex);
return NoResult{}; return {};
} }
[[nodiscard]] bool IsArgumentTypeSupported(const techset::CommonShaderArgumentType& argumentType) const [[nodiscard]] bool IsArgumentTypeSupported(const techset::CommonShaderArgumentType& argumentType) const
@@ -369,7 +371,7 @@ namespace
std::string& errorMessage, std::string& errorMessage,
const techset::CommonShaderArgCreatorDestination& input) = 0; const techset::CommonShaderArgCreatorDestination& input) = 0;
virtual result::Expected<NoResult, std::string> AutoCreateMissingArgs() = 0; virtual std::expected<void, std::string> AutoCreateMissingArgs() = 0;
techset::ITechniqueShaderLoader& m_shader_loader; techset::ITechniqueShaderLoader& m_shader_loader;
techset::CommonCodeSourceInfos& m_common_code_source_infos; techset::CommonCodeSourceInfos& m_common_code_source_infos;
@@ -392,7 +394,7 @@ namespace
{ {
} }
result::Expected<NoResult, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override std::expected<void, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override
{ {
auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name); auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name);
if (!result) if (!result)
@@ -400,14 +402,14 @@ namespace
m_shader_info = d3d9::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size); m_shader_info = d3d9::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size);
if (!m_shader_info) if (!m_shader_info)
return result::Unexpected(std::format("Failed to analyse dx9 shader {}", name)); return std::unexpected(std::format("Failed to analyse dx9 shader {}", name));
m_arg_added = std::vector(m_shader_info->m_constants.size(), false); m_arg_added = std::vector(m_shader_info->m_constants.size(), false);
return NoResult{}; return {};
} }
result::Expected<NoResult, std::string> LeaveShader() override std::expected<void, std::string> LeaveShader() override
{ {
auto result = BaseCommonShaderArgCreator::LeaveShader(); auto result = BaseCommonShaderArgCreator::LeaveShader();
m_shader_info = nullptr; m_shader_info = nullptr;
@@ -473,7 +475,7 @@ namespace
return true; return true;
} }
result::Expected<NoResult, std::string> AutoCreateMissingArgs() override std::expected<void, std::string> AutoCreateMissingArgs() override
{ {
const auto argCount = m_shader_info->m_constants.size(); const auto argCount = m_shader_info->m_constants.size();
for (size_t argIndex = 0; argIndex < argCount; argIndex++) for (size_t argIndex = 0; argIndex < argCount; argIndex++)
@@ -499,11 +501,11 @@ namespace
} }
} }
return NoResult{}; return {};
} }
private: private:
result::Expected<NoResult, std::string> AutoCreateConstantArg(const d3d9::ShaderConstant& shaderArg) std::expected<void, std::string> AutoCreateConstantArg(const d3d9::ShaderConstant& shaderArg)
{ {
if (!IsArgumentTypeSupported( if (!IsArgumentTypeSupported(
techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST})) techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST}))
@@ -511,7 +513,7 @@ namespace
con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.", con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.",
m_shader_name, m_shader_name,
ShaderTypeName(m_shader_type)); ShaderTypeName(m_shader_type));
return NoResult{}; return {};
} }
const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(shaderArg.m_name); const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(shaderArg.m_name);
@@ -519,21 +521,21 @@ namespace
{ {
// Some variables are simply not added as args for some reason // Some variables are simply not added as args for some reason
if (m_common_code_source_infos.IsArgAccessorIgnored(shaderArg.m_name)) if (m_common_code_source_infos.IsArgAccessorIgnored(shaderArg.m_name))
return NoResult{}; return {};
return result::Unexpected(std::format("Missing assignment to shader constant {}", shaderArg.m_name)); return std::unexpected(std::format("Missing assignment to shader constant {}", shaderArg.m_name));
} }
const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst); const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst);
if (!constInfo) if (!constInfo)
return result::Unexpected(std::format("Missing info for code const {}", shaderArg.m_name)); return std::unexpected(std::format("Missing info for code const {}", shaderArg.m_name));
const auto elementSize = ElementSizeForArg(shaderArg); const auto elementSize = ElementSizeForArg(shaderArg);
const auto elementCount = utils::Align(shaderArg.m_register_count, elementSize) / elementSize; const auto elementCount = utils::Align(shaderArg.m_register_count, elementSize) / elementSize;
const auto infoArrayCount = std::max<unsigned>(constInfo->arrayCount, 1); const auto infoArrayCount = std::max<unsigned>(constInfo->arrayCount, 1);
if (elementCount > infoArrayCount) if (elementCount > infoArrayCount)
{ {
return result::Unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", return std::unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})",
shaderArg.m_name, shaderArg.m_name,
elementCount, elementCount,
infoArrayCount)); infoArrayCount));
@@ -552,7 +554,7 @@ namespace
if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type)) if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type))
m_tech_flags |= *constInfo->techFlags; m_tech_flags |= *constInfo->techFlags;
return NoResult{}; return {};
} }
[[nodiscard]] static unsigned ElementSizeForArg(const d3d9::ShaderConstant& arg) [[nodiscard]] static unsigned ElementSizeForArg(const d3d9::ShaderConstant& arg)
@@ -567,7 +569,7 @@ namespace
} }
} }
result::Expected<NoResult, std::string> AutoCreateSamplerArg(const d3d9::ShaderConstant& shaderArg) std::expected<void, std::string> AutoCreateSamplerArg(const d3d9::ShaderConstant& shaderArg)
{ {
if (!IsArgumentTypeSupported( if (!IsArgumentTypeSupported(
techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER})) techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER}))
@@ -575,16 +577,16 @@ namespace
con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.", con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.",
m_shader_name, m_shader_name,
ShaderTypeName(m_shader_type)); ShaderTypeName(m_shader_type));
return NoResult{}; return {};
} }
const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(shaderArg.m_name); const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(shaderArg.m_name);
if (!maybeCodeSampler) if (!maybeCodeSampler)
return result::Unexpected(std::format("Missing assignment to shader texture {}", shaderArg.m_name)); return std::unexpected(std::format("Missing assignment to shader texture {}", shaderArg.m_name));
const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler); const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler);
if (!samplerInfo) if (!samplerInfo)
return result::Unexpected(std::format("Missing info for code sampler {}", shaderArg.m_name)); return std::unexpected(std::format("Missing info for code sampler {}", shaderArg.m_name));
techset::CommonShaderArgDestination commonDestination; techset::CommonShaderArgDestination commonDestination;
commonDestination.dx9.m_destination_register = shaderArg.m_register_index; commonDestination.dx9.m_destination_register = shaderArg.m_register_index;
@@ -609,7 +611,7 @@ namespace
{ {
} }
result::Expected<NoResult, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override std::expected<void, std::string> EnterShader(const techset::CommonTechniqueShaderType shaderType, const std::string& name) override
{ {
auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name); auto result = BaseCommonShaderArgCreator::EnterShader(shaderType, name);
if (!result) if (!result)
@@ -617,14 +619,14 @@ namespace
m_shader_info = d3d11::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size); m_shader_info = d3d11::ShaderAnalyser::GetShaderInfo(m_bin.m_shader_bin, m_bin.m_shader_bin_size);
if (!m_shader_info) if (!m_shader_info)
return result::Unexpected(std::format("Failed to analyse dx11 shader {}", name)); return std::unexpected(std::format("Failed to analyse dx11 shader {}", name));
CountShaderArgs(); CountShaderArgs();
return NoResult{}; return {};
} }
result::Expected<NoResult, std::string> LeaveShader() override std::expected<void, std::string> LeaveShader() override
{ {
auto result = BaseCommonShaderArgCreator::LeaveShader(); auto result = BaseCommonShaderArgCreator::LeaveShader();
m_shader_info = nullptr; m_shader_info = nullptr;
@@ -777,7 +779,7 @@ namespace
return true; return true;
} }
result::Expected<NoResult, std::string> AutoCreateMissingArgs() override std::expected<void, std::string> AutoCreateMissingArgs() override
{ {
size_t usedConstantCount = 0; size_t usedConstantCount = 0;
size_t textureCount = 0; size_t textureCount = 0;
@@ -791,7 +793,7 @@ namespace
return boundResource.m_type == d3d11::BoundResourceType::CBUFFER && boundResource.m_name == buffer.m_name; return boundResource.m_type == d3d11::BoundResourceType::CBUFFER && boundResource.m_name == buffer.m_name;
}); });
if (bufferBinding == m_shader_info->m_bound_resources.end()) if (bufferBinding == m_shader_info->m_bound_resources.end())
return result::Unexpected(std::format("Failed to find binding for constant buffer {}", buffer.m_name)); return std::unexpected(std::format("Failed to find binding for constant buffer {}", buffer.m_name));
for (const auto& variable : buffer.m_variables) for (const auto& variable : buffer.m_variables)
{ {
@@ -829,7 +831,7 @@ namespace
return std::move(result); return std::move(result);
} }
return NoResult{}; return {};
} }
private: private:
@@ -857,7 +859,7 @@ namespace
m_texture_arg_added = std::vector(textureCount, false); m_texture_arg_added = std::vector(textureCount, false);
} }
result::Expected<NoResult, std::string> AutoCreateConstantArg(const d3d11::ConstantBufferVariable& variable, const size_t bufferIndex) std::expected<void, std::string> AutoCreateConstantArg(const d3d11::ConstantBufferVariable& variable, const size_t bufferIndex)
{ {
if (!IsArgumentTypeSupported( if (!IsArgumentTypeSupported(
techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST})) techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_CONST}))
@@ -865,7 +867,7 @@ namespace
con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.", con::warn("Shader {} uses unsupported argument type \"{} constant\". This may cause unstable behaviour.",
m_shader_name, m_shader_name,
ShaderTypeName(m_shader_type)); ShaderTypeName(m_shader_type));
return NoResult{}; return {};
} }
const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(variable.m_name); const auto maybeCodeConst = m_common_code_source_infos.GetCodeConstSourceForAccessor(variable.m_name);
@@ -873,21 +875,21 @@ namespace
{ {
// Some variables are simply not added as args for some reason // Some variables are simply not added as args for some reason
if (m_common_code_source_infos.IsArgAccessorIgnored(variable.m_name)) if (m_common_code_source_infos.IsArgAccessorIgnored(variable.m_name))
return NoResult{}; return {};
return result::Unexpected(std::format("Missing assignment to shader constant {}", variable.m_name)); return std::unexpected(std::format("Missing assignment to shader constant {}", variable.m_name));
} }
const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst); const auto constInfo = m_common_code_source_infos.GetInfoForCodeConstSource(*maybeCodeConst);
if (!constInfo) if (!constInfo)
return result::Unexpected(std::format("Missing info for code const {}", variable.m_name)); return std::unexpected(std::format("Missing info for code const {}", variable.m_name));
const auto variableElementCount = std::max<unsigned>(variable.m_element_count, 1); const auto variableElementCount = std::max<unsigned>(variable.m_element_count, 1);
const auto variableElementSize = variable.m_size / variableElementCount; const auto variableElementSize = variable.m_size / variableElementCount;
const auto infoArrayCount = std::max<unsigned>(constInfo->arrayCount, 1); const auto infoArrayCount = std::max<unsigned>(constInfo->arrayCount, 1);
if (variableElementCount > infoArrayCount) if (variableElementCount > infoArrayCount)
{ {
return result::Unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})", return std::unexpected(std::format("Could not auto create argument for constant {} as it has more elements ({}) than the code constant ({})",
variable.m_name, variable.m_name,
variableElementCount, variableElementCount,
infoArrayCount)); infoArrayCount));
@@ -908,10 +910,10 @@ namespace
if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type)) if (constInfo->techFlags && (!constInfo->techFlagShaderType || *constInfo->techFlagShaderType == m_shader_type))
m_tech_flags |= *constInfo->techFlags; m_tech_flags |= *constInfo->techFlags;
return NoResult{}; return {};
} }
result::Expected<NoResult, std::string> AutoCreateSamplerArg(const d3d11::BoundResource& textureResource, const unsigned samplerBindPoint) std::expected<void, std::string> AutoCreateSamplerArg(const d3d11::BoundResource& textureResource, const unsigned samplerBindPoint)
{ {
if (!IsArgumentTypeSupported( if (!IsArgumentTypeSupported(
techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER})) techset::CommonShaderArgumentType{.m_shader_type = m_shader_type, .m_value_type = techset::CommonShaderValueType::CODE_SAMPLER}))
@@ -919,16 +921,16 @@ namespace
con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.", con::warn("Shader {} uses unsupported argument type \"{} sampler\". This may cause unstable behaviour.",
m_shader_name, m_shader_name,
ShaderTypeName(m_shader_type)); ShaderTypeName(m_shader_type));
return NoResult{}; return {};
} }
const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(textureResource.m_name); const auto maybeCodeSampler = m_common_code_source_infos.GetCodeSamplerSourceForAccessor(textureResource.m_name);
if (!maybeCodeSampler) if (!maybeCodeSampler)
return result::Unexpected(std::format("Missing assignment to shader texture {}", textureResource.m_name)); return std::unexpected(std::format("Missing assignment to shader texture {}", textureResource.m_name));
const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler); const auto samplerInfo = m_common_code_source_infos.GetInfoForCodeSamplerSource(*maybeCodeSampler);
if (!samplerInfo) if (!samplerInfo)
return result::Unexpected(std::format("Missing info for code sampler {}", textureResource.m_name)); return std::unexpected(std::format("Missing info for code sampler {}", textureResource.m_name));
techset::CommonShaderArgDestination commonDestination; techset::CommonShaderArgDestination commonDestination;
commonDestination.dx11.m_location.texture_index = textureResource.m_bind_point; commonDestination.dx11.m_location.texture_index = textureResource.m_bind_point;

View File

@@ -2,9 +2,9 @@
#include "Asset/AssetCreationContext.h" #include "Asset/AssetCreationContext.h"
#include "Techset/CommonTechnique.h" #include "Techset/CommonTechnique.h"
#include "Utils/Result.h"
#include <array> #include <array>
#include <expected>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <string> #include <string>
@@ -38,21 +38,20 @@ namespace techset
CommonShaderArgCreator() = default; CommonShaderArgCreator() = default;
virtual ~CommonShaderArgCreator() = default; virtual ~CommonShaderArgCreator() = default;
virtual result::Expected<NoResult, std::string> EnterShader(CommonTechniqueShaderType shaderType, const std::string& name) = 0; virtual std::expected<void, std::string> EnterShader(CommonTechniqueShaderType shaderType, const std::string& name) = 0;
virtual result::Expected<NoResult, std::string> LeaveShader() = 0; virtual std::expected<void, std::string> LeaveShader() = 0;
virtual result::Expected<NoResult, std::string> virtual std::expected<void, std::string>
AcceptShaderConstantArgument(const CommonShaderArgCreatorDestination& destination, CommonCodeConstSource codeConstSource, unsigned sourceIndex) = 0; AcceptShaderConstantArgument(const CommonShaderArgCreatorDestination& destination, CommonCodeConstSource codeConstSource, unsigned sourceIndex) = 0;
virtual result::Expected<NoResult, std::string> AcceptShaderSamplerArgument(const CommonShaderArgCreatorDestination& destination, virtual std::expected<void, std::string> AcceptShaderSamplerArgument(const CommonShaderArgCreatorDestination& destination,
CommonCodeSamplerSource codeSamplerSource) = 0; CommonCodeSamplerSource codeSamplerSource) = 0;
virtual result::Expected<NoResult, std::string> AcceptShaderLiteralArgument(const CommonShaderArgCreatorDestination& destination, virtual std::expected<void, std::string> AcceptShaderLiteralArgument(const CommonShaderArgCreatorDestination& destination,
const std::array<float, 4>& literalValue) = 0; const std::array<float, 4>& literalValue) = 0;
virtual result::Expected<NoResult, std::string> AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, virtual std::expected<void, std::string> AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination, unsigned nameHash) = 0;
unsigned nameHash) = 0; virtual std::expected<void, std::string> AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination,
virtual result::Expected<NoResult, std::string> AcceptShaderMaterialArgument(const CommonShaderArgCreatorDestination& destination,
const std::string& nameValue) = 0; const std::string& nameValue) = 0;
virtual result::Expected<NoResult, std::string> FinalizePass(techset::CommonTechnique& technique, CommonPass& pass) = 0; virtual std::expected<void, std::string> FinalizePass(techset::CommonTechnique& technique, CommonPass& pass) = 0;
static std::unique_ptr<CommonShaderArgCreator> static std::unique_ptr<CommonShaderArgCreator>
CreateDx9(ITechniqueShaderLoader& shaderLoader, AssetCreationContext& context, CommonCodeSourceInfos& commonCodeSourceInfos); CreateDx9(ITechniqueShaderLoader& shaderLoader, AssetCreationContext& context, CommonCodeSourceInfos& commonCodeSourceInfos);

View File

@@ -241,7 +241,7 @@ namespace techset
} }
} }
result::Expected<NoResult, std::string> shaderCreatorResult(NoResult{}); std::expected<void, std::string> shaderCreatorResult;
if (isSampler) if (isSampler)
shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderSamplerArgument(destination, samplerSource); shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderSamplerArgument(destination, samplerSource);
else else
@@ -287,7 +287,7 @@ namespace techset
SequenceResult<SimpleParserValue>& result, SequenceResult<SimpleParserValue>& result,
const CommonShaderArgCreatorDestination& destination) const CommonShaderArgCreatorDestination& destination)
{ {
result::Expected<NoResult, std::string> shaderCreatorResult(NoResult{}); std::expected<void, std::string> shaderCreatorResult;
if (result.HasNextCapture(CAPTURE_MATERIAL_HASH)) if (result.HasNextCapture(CAPTURE_MATERIAL_HASH))
{ {
shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderMaterialArgument( shaderCreatorResult = state->m_shader_arg_creator.AcceptShaderMaterialArgument(
@@ -300,7 +300,7 @@ namespace techset
} }
if (!shaderCreatorResult.has_value()) if (!shaderCreatorResult.has_value())
throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), std::move(shaderCreatorResult.error())); throw ParsingException(result.NextCapture(CAPTURE_FIRST_TOKEN).GetPos(), std::move(shaderCreatorResult).error());
} }
}; };
} // namespace techset } // namespace techset

View File

@@ -1,185 +0,0 @@
#pragma once
#include <type_traits>
#include <variant>
using NoResult = std::monostate;
// Can be replaced by std::expected with c++23
namespace result
{
template<typename TError> class Unexpected
{
public:
Unexpected(TError result)
: m_data(std::move(result))
{
}
constexpr std::add_lvalue_reference_t<TError> value() &
{
return m_data;
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> value() const&
{
return m_data;
}
constexpr std::add_rvalue_reference_t<TError> value() &&
{
return std::move(m_data);
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> value() const&&
{
return std::move(m_data);
}
constexpr std::add_lvalue_reference_t<TError> operator*() &
{
return m_data;
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> operator*() const&
{
return m_data;
}
constexpr std::add_rvalue_reference_t<TError> operator*() &&
{
return std::move(m_data);
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> operator*() const&&
{
return std::move(m_data);
}
constexpr std::add_pointer_t<TError> operator->()
{
return m_data;
}
constexpr std::add_const_t<std::add_pointer_t<TError>> operator->() const
{
return m_data;
}
private:
TError m_data;
};
template<typename TResult, typename TError> class Expected
{
public:
Expected(TResult result)
: m_data(std::variant<TResult, TError>(std::in_place_index<0>, std::move(result)))
{
}
Expected(Unexpected<TError> unexpected)
: m_data(std::variant<TResult, TError>(std::in_place_index<1>, std::move(*unexpected)))
{
}
constexpr operator bool() const noexcept
{
return m_data.index() == 0;
}
constexpr bool has_value() const noexcept
{
return m_data.index() == 0;
}
constexpr std::add_lvalue_reference_t<TResult> value() &
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> value() const&
{
return std::get<0>(m_data);
}
constexpr std::add_rvalue_reference_t<TResult> value() &&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> value() const&&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_lvalue_reference_t<TResult> operator*() &
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TResult>> operator*() const&
{
return std::get<0>(m_data);
}
constexpr std::add_rvalue_reference_t<TResult> operator*() &&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TResult>> operator*() const&&
{
return std::move(std::get<0>(m_data));
}
constexpr std::add_pointer_t<TResult> operator->()
{
return std::get<0>(m_data);
}
constexpr std::add_const_t<std::add_pointer_t<TResult>> operator->() const
{
return std::get<0>(m_data);
}
constexpr std::add_lvalue_reference_t<TError> error() &
{
return std::get<1>(m_data);
}
constexpr std::add_const_t<std::add_lvalue_reference_t<TError>> error() const&
{
return std::get<1>(m_data);
}
constexpr std::add_rvalue_reference_t<TError> error() &&
{
return std::move(std::get<1>(m_data));
}
constexpr std::add_const_t<std::add_rvalue_reference_t<TError>> error() const&&
{
return std::move(std::get<1>(m_data));
}
private:
explicit Expected(std::variant<TResult, TError> data)
: m_data(std::move(data))
{
}
std::variant<TResult, TError> m_data;
};
#define ENSURE_RESULT_VAR(var) \
if (!(var)) \
return (var);
#define ENSURE_RESULT(expr) \
{ \
const auto result = (expr); \
if (!result) \
return result; \
}
} // namespace result

View File

@@ -11,19 +11,19 @@
using namespace std::string_literals; using namespace std::string_literals;
namespace fs = std::filesystem; namespace fs = std::filesystem;
result::Expected<std::unique_ptr<Zone>, std::string> ZoneLoading::LoadZone(const std::string& path, std::expected<std::unique_ptr<Zone>, std::string> ZoneLoading::LoadZone(const std::string& path,
std::optional<std::unique_ptr<ProgressCallback>> progressCallback) std::optional<std::unique_ptr<ProgressCallback>> progressCallback)
{ {
auto zoneName = fs::path(path).filename().replace_extension().string(); auto zoneName = fs::path(path).filename().replace_extension().string();
std::ifstream file(path, std::fstream::in | std::fstream::binary); std::ifstream file(path, std::fstream::in | std::fstream::binary);
if (!file.is_open()) if (!file.is_open())
return result::Unexpected(std::format("Could not open file '{}'.", path)); return std::unexpected(std::format("Could not open file '{}'.", path));
ZoneHeader header{}; ZoneHeader header{};
file.read(reinterpret_cast<char*>(&header), sizeof(header)); file.read(reinterpret_cast<char*>(&header), sizeof(header));
if (file.gcount() != sizeof(header)) if (file.gcount() != sizeof(header))
return result::Unexpected(std::format("Failed to read zone header from file '{}'.", path)); return std::unexpected(std::format("Failed to read zone header from file '{}'.", path));
std::unique_ptr<ZoneLoader> zoneLoader; std::unique_ptr<ZoneLoader> zoneLoader;
for (auto game = 0u; game < static_cast<unsigned>(GameId::COUNT); game++) for (auto game = 0u; game < static_cast<unsigned>(GameId::COUNT); game++)
@@ -37,14 +37,14 @@ result::Expected<std::unique_ptr<Zone>, std::string> ZoneLoading::LoadZone(const
} }
if (!zoneLoader) if (!zoneLoader)
return result::Unexpected(std::format("Could not create factory for zone '{}'.", zoneName)); return std::unexpected(std::format("Could not create factory for zone '{}'.", zoneName));
auto loadedZone = zoneLoader->LoadZone(file); auto loadedZone = zoneLoader->LoadZone(file);
file.close(); file.close();
if (!loadedZone) if (!loadedZone)
return result::Unexpected("Loading zone failed."s); return std::unexpected("Loading zone failed."s);
return std::move(loadedZone); return std::move(loadedZone);
} }

View File

@@ -1,14 +1,14 @@
#pragma once #pragma once
#include "Utils/ProgressCallback.h" #include "Utils/ProgressCallback.h"
#include "Utils/Result.h"
#include "Zone/Zone.h" #include "Zone/Zone.h"
#include <expected>
#include <string> #include <string>
class ZoneLoading class ZoneLoading
{ {
public: public:
static result::Expected<std::unique_ptr<Zone>, std::string> LoadZone(const std::string& path, static std::expected<std::unique_ptr<Zone>, std::string> LoadZone(const std::string& path,
std::optional<std::unique_ptr<ProgressCallback>> progressCallback); std::optional<std::unique_ptr<ProgressCallback>> progressCallback);
}; };