2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-08 12:24:56 +00:00

Use UTF-8 exclusively internally

This removes SystemString, SystemChar, etc.
All filepaths and log strings are assumed to be UTF-8,
with conversions to UTF-16 for Windows APIs as appropriate.

Updates amuse, athena, boo, kabufua and nod
This commit is contained in:
2021-06-30 14:20:45 -04:00
parent 6e12554026
commit 9ca1a38171
160 changed files with 2029 additions and 2753 deletions

View File

@@ -4,6 +4,7 @@
#include "glslang/Public/ShaderLang.h"
#include "hecl/hecl.hpp"
#include <sstream>
#include <nowide/args.hpp>
static logvisor::Module Log("shaderc");
@@ -32,12 +33,12 @@ static bool FindBestD3DCompile() {
}
return false;
}
int wmain(int argc, const hecl::SystemChar** argv)
#else
int main(int argc, const hecl::SystemChar** argv)
#endif
{
int main(int argc, char** argv) {
#if _WIN32
nowide::args _(argc, argv);
#endif
logvisor::RegisterConsoleLogger();
logvisor::RegisterStandardExceptions();
@@ -53,7 +54,7 @@ int main(int argc, const hecl::SystemChar** argv)
return 0;
}
hecl::SystemString outPath;
std::string outPath;
hecl::shaderc::Compiler c;
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-') {
@@ -68,7 +69,7 @@ int main(int argc, const hecl::SystemChar** argv)
return 1;
}
} else if (argv[i][1] == 'D') {
const hecl::SystemChar* define;
const char* define;
if (argv[i][2]) {
define = &argv[i][2];
} else if (i + 1 < argc) {
@@ -78,14 +79,12 @@ int main(int argc, const hecl::SystemChar** argv)
Log.report(logvisor::Error, FMT_STRING("Invalid -D argument"));
return 1;
}
hecl::SystemUTF8Conv conv(define);
const char* defineU8 = conv.c_str();
if (const char* equals = strchr(defineU8, '='))
c.addDefine(std::string(defineU8, equals - defineU8), equals + 1);
if (const char* equals = strchr(define, '='))
c.addDefine(std::string(define, equals - define), equals + 1);
else
c.addDefine(defineU8, "");
c.addDefine(define, "");
} else {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Unrecognized flag option '{:c}'")), argv[i][1]);
Log.report(logvisor::Error, FMT_STRING("Unrecognized flag option '{:c}'"), argv[i][1]);
return 1;
}
} else {
@@ -98,9 +97,9 @@ int main(int argc, const hecl::SystemChar** argv)
return 1;
}
hecl::SystemStringView baseName;
auto slashPos = outPath.find_last_of(_SYS_STR("/\\"));
if (slashPos != hecl::SystemString::npos)
std::string_view baseName;
auto slashPos = outPath.find_last_of("/\\");
if (slashPos != std::string::npos)
baseName = outPath.data() + slashPos + 1;
else
baseName = outPath;
@@ -110,16 +109,15 @@ int main(int argc, const hecl::SystemChar** argv)
return 1;
}
hecl::SystemUTF8Conv conv(baseName);
std::pair<std::stringstream, std::stringstream> ret;
if (!c.compile(conv.str(), ret))
if (!c.compile(baseName, ret))
return 1;
{
hecl::SystemString headerPath = outPath + _SYS_STR(".hpp");
std::string headerPath = outPath + ".hpp";
athena::io::FileWriter w(headerPath);
if (w.hasError()) {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Error opening '{}' for writing")), headerPath);
Log.report(logvisor::Error, FMT_STRING("Error opening '{}' for writing"), headerPath);
return 1;
}
std::string header = ret.first.str();
@@ -127,10 +125,10 @@ int main(int argc, const hecl::SystemChar** argv)
}
{
hecl::SystemString impPath = outPath + _SYS_STR(".cpp");
std::string impPath = outPath + ".cpp";
athena::io::FileWriter w(impPath);
if (w.hasError()) {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Error opening '{}' for writing")), impPath);
Log.report(logvisor::Error, FMT_STRING("Error opening '{}' for writing"), impPath);
return 1;
}
std::string source = ret.second.str();

View File

@@ -28,7 +28,7 @@ static const char* StageNames[] = {
"hecl::PipelineStage::Evaluation"
};
const std::string* Compiler::getFileContents(SystemStringView path) {
const std::string* Compiler::getFileContents(std::string_view path) {
// TODO: Heterogeneous lookup when C++20 available
auto search = m_fileContents.find(path.data());
if (search == m_fileContents.end()) {
@@ -42,7 +42,7 @@ const std::string* Compiler::getFileContents(SystemStringView path) {
return &search->second;
}
void Compiler::addInputFile(SystemStringView file) {
void Compiler::addInputFile(std::string_view file) {
if (std::find(m_inputFiles.begin(), m_inputFiles.end(), file) == m_inputFiles.end())
m_inputFiles.emplace_back(file);
}
@@ -207,25 +207,25 @@ static const std::regex regGeometry(R"(#\s*geometry\s+(.*))", RegexFlags);
static const std::regex regControl(R"(#\s*control\s+(.*))", RegexFlags);
static const std::regex regEvaluation(R"(#\s*evaluation\s+(.*))", RegexFlags);
bool Compiler::includeFile(SystemStringView file, std::string& out, int depth) {
bool Compiler::includeFile(std::string_view file, std::string& out, int depth) {
if (depth > 32) {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Too many levels of includes (>32) at '{}'")), file);
Log.report(logvisor::Error, FMT_STRING("Too many levels of includes (>32) at '{}'"), file);
return false;
}
const std::string* data = getFileContents(file);
if (!data) {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Unable to access '{}'")), file);
Log.report(logvisor::Error, FMT_STRING("Unable to access '{}'"), file);
return false;
}
const std::string& sdata = *data;
SystemString directory;
auto slashPos = file.find_last_of(_SYS_STR("/\\"));
if (slashPos != SystemString::npos)
directory = SystemString(file.data(), slashPos);
std::string directory;
auto slashPos = file.find_last_of("/\\");
if (slashPos != std::string::npos)
directory = std::string(file.data(), slashPos);
else
directory = _SYS_STR(".");
directory = ".";
auto begin = sdata.cbegin();
auto end = sdata.cend();
@@ -241,14 +241,13 @@ bool Compiler::includeFile(SystemStringView file, std::string& out, int depth) {
if (std::regex_search(begin, nextBegin, subMatch, regInclude)) {
std::string path = subMatch[1].str();
if (path.empty()) {
Log.report(logvisor::Error, FMT_STRING(_SYS_STR("Empty path provided to include in '{}'")), file);
Log.report(logvisor::Error, FMT_STRING("Empty path provided to include in '{}'"), file);
return false;
}
hecl::SystemString pathStr(hecl::SystemStringConv(path).sys_str());
if (!hecl::IsAbsolute(pathStr))
pathStr = directory + _SYS_STR('/') + pathStr;
if (!includeFile(pathStr, out, depth + 1))
if (!hecl::IsAbsolute(path))
path = directory + '/' + path;
if (!includeFile(path, out, depth + 1))
return false;
} else {
out.insert(out.end(), begin, nextBegin);
@@ -442,7 +441,7 @@ constexpr void SemanticOut(std::stringstream& out,
attr.second);
}
bool Compiler::compileFile(SystemStringView file, std::string_view baseName,
bool Compiler::compileFile(std::string_view file, std::string_view baseName,
std::pair<std::stringstream, std::stringstream>& out) {
std::string includesPass;
if (!includeFile(file, includesPass))

View File

@@ -1,5 +1,4 @@
#pragma once
#include "hecl/SystemChar.hpp"
#include <string>
#include <vector>
#include <unordered_map>
@@ -9,9 +8,9 @@ namespace hecl::shaderc {
class Compiler {
enum class StageType { Vertex, Fragment, Geometry, Control, Evaluation };
std::vector<SystemString> m_inputFiles;
std::unordered_map<SystemString, std::string> m_fileContents;
const std::string* getFileContents(SystemStringView path);
std::vector<std::string> m_inputFiles;
std::unordered_map<std::string, std::string> m_fileContents;
const std::string* getFileContents(std::string_view path);
std::unordered_map<std::string, std::string> m_defines;
template <typename Action, typename P>
static bool StageAction(StageType type, const std::string& name, const std::string& basename,
@@ -19,12 +18,12 @@ class Compiler {
template <typename Action>
static bool StageAction(const std::string& platforms, StageType type, const std::string& name,
const std::string& basename, const std::string& stage, std::stringstream& implOut);
bool includeFile(SystemStringView file, std::string& out, int depth = 0);
bool compileFile(SystemStringView file, std::string_view baseName,
bool includeFile(std::string_view file, std::string& out, int depth = 0);
bool compileFile(std::string_view file, std::string_view baseName,
std::pair<std::stringstream, std::stringstream>& out);
public:
void addInputFile(SystemStringView file);
void addInputFile(std::string_view file);
void addDefine(std::string_view var, std::string_view val);
bool compile(std::string_view baseName, std::pair<std::stringstream, std::stringstream>& out);
};