Merge branch 'master' of ssh://git.axiodl.com:6431/libAthena/athena

This commit is contained in:
Jack Andersen 2019-09-30 21:16:11 -10:00
commit 42581c922a
18 changed files with 391 additions and 306 deletions

View File

@ -19,19 +19,6 @@ set(ATHENA_VERSION
add_subdirectory(extern)
if(WIN32)
list(APPEND CORE_EXTRA src/win32_largefilewrapper.c include/win32_largefilewrapper.h
src/athena/FileWriterWin32.cpp src/athena/FileReaderWin32.cpp)
else()
list(APPEND CORE_EXTRA src/athena/FileWriterNix.cpp src/athena/FileReader.cpp)
if(APPLE OR GEKKO OR NX OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
list(APPEND CORE_EXTRA src/osx_largefilewrapper.c include/osx_largefilewrapper.h)
if(GEKKO OR NX)
list(APPEND CORE_EXTRA src/gekko_support.c include/gekko_support.h)
endif()
endif()
endif()
add_library(athena-core
src/athena/Utility.cpp
src/athena/MemoryReader.cpp
@ -49,7 +36,6 @@ add_library(athena-core
src/athena/FileInfo.cpp
src/athena/Dir.cpp
src/athena/DNAYaml.cpp
${CORE_EXTRA}
include/athena/IStream.hpp
include/athena/IStreamReader.hpp
@ -81,10 +67,46 @@ add_library(athena-core
include/yaml.h
include/utf8proc.h
)
if(WIN32)
target_sources(athena-core PRIVATE
src/win32_largefilewrapper.c
include/win32_largefilewrapper.h
src/athena/FileWriterWin32.cpp
src/athena/FileReaderWin32.cpp
)
target_compile_definitions(athena-core PRIVATE
-DNOMINMAX
-DWIN32_LEAN_AND_MEAN
)
else()
target_sources(athena-core PRIVATE
src/athena/FileWriterNix.cpp
src/athena/FileReader.cpp
)
if(APPLE OR GEKKO OR NX OR ${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
target_sources(athena-core PRIVATE
src/osx_largefilewrapper.c
include/osx_largefilewrapper.h
)
if(GEKKO OR NX)
target_sources(athena-core PRIVATE
src/gekko_support.c
include/gekko_support.h
)
endif()
endif()
endif()
target_include_directories(athena-core PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${ZLIB_INCLUDE_DIR}>)
target_link_libraries(athena-core PUBLIC athena-libyaml fmt)
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${ZLIB_INCLUDE_DIR}>
)
target_link_libraries(athena-core PUBLIC
athena-libyaml
fmt
)
add_library(athena-sakura EXCLUDE_FROM_ALL
src/athena/Sprite.cpp
@ -129,7 +151,7 @@ add_library(athena-wiisave EXCLUDE_FROM_ALL
include/sha1.h
)
if(NOT MSVC AND NOT GEKKO AND NOT NX)
set_source_files_properties(src/aes.cpp PROPERTIES COMPILE_FLAGS -maes)
set_source_files_properties(src/aes.cpp PROPERTIES COMPILE_FLAGS -maes)
endif()

View File

@ -1,19 +1,22 @@
#include <cstdint>
#include <cstdio>
#include <iostream>
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Frontend/Utils.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Sema/Sema.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/TypeLoc.h"
#include "clang/Basic/Version.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/CommandLine.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <clang/AST/ASTConsumer.h>
#include <clang/AST/DeclCXX.h>
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/Version.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Frontend/Utils.h>
#include <clang/Sema/Sema.h>
#include <clang/Tooling/Tooling.h>
#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Format.h>
using namespace std::literals;
@ -134,15 +137,15 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
const auto* vType = static_cast<const clang::VectorType*>(field->getType().getTypePtr());
if (vType->isVectorType()) {
const auto* eType = static_cast<const clang::BuiltinType*>(vType->getElementType().getTypePtr());
const uint64_t width = context.getTypeInfo(eType).Width;
if (!eType->isBuiltinType() || !eType->isFloatingPoint() || (width != 32 && width != 64))
const uint64_t typeWidth = context.getTypeInfo(eType).Width;
if (!eType->isBuiltinType() || !eType->isFloatingPoint() || (typeWidth != 32 && typeWidth != 64))
continue;
if (vType->getNumElements() == 2) {
return width / 8 * 2;
return typeWidth / 8 * 2;
} else if (vType->getNumElements() == 3) {
return width / 8 * 3;
return typeWidth / 8 * 3;
} else if (vType->getNumElements() == 4) {
return width / 8 * 4;
return typeWidth / 8 * 4;
}
}
}
@ -164,7 +167,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
static std::string GetPropIdExpr(const clang::FieldDecl* field, const std::string& fieldName) {
std::string fieldStr = GetFieldString(fieldName);
std::string propIdExpr = "\"" + fieldStr + "\"sv";
std::string propIdExpr = "\""s.append(fieldStr).append("\"sv");
for (clang::Attr* attr : field->attrs()) {
if (clang::AnnotateAttr* annot = clang::dyn_cast_or_null<clang::AnnotateAttr>(attr)) {
llvm::StringRef textRef = annot->getAnnotation();
@ -184,22 +187,39 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
static std::string GetOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& endianExpr) {
return "<Op, "s + endianExpr + ">(athena::io::PropId(" + propIdExpr + "), " + fieldName + ", s)";
return "<Op, "s.append(endianExpr)
.append(">(athena::io::PropId(")
.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", s)");
}
static std::string GetOpString(const std::string& fieldName, const std::string& propIdExpr) {
return "<Op>(athena::io::PropId(" + propIdExpr + "), " + fieldName + ", s)";
return "<Op>(athena::io::PropId("s.append(propIdExpr).append("), ").append(fieldName).append(", s)");
}
static std::string GetVectorOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& sizeExpr, const std::string& endianExpr) {
return "<Op, "s + endianExpr + ">(athena::io::PropId(" + propIdExpr + "), " + fieldName + ", " + sizeExpr + ", s)";
return "<Op, "s.append(endianExpr)
.append(">(athena::io::PropId(")
.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", ")
.append(sizeExpr)
.append(", s)");
}
static std::string GetVectorOpString(const std::string& fieldName, const std::string& propIdExpr,
const std::string& sizeExpr) {
return "<Op>(athena::io::PropId(" + propIdExpr + "), " + fieldName + ", " + sizeExpr + ", s)";
return "<Op>(athena::io::PropId("s.append(propIdExpr)
.append("), ")
.append(fieldName)
.append(", ")
.append(sizeExpr)
.append(", s)");
}
static void RecurseNestedTypeName(const clang::DeclContext* decl, std::string& templateStmt, std::string& qualType) {
@ -220,7 +240,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
templateStmt += ", ";
qualType += ", ";
}
templateStmt += "class "s + tpParm->getName().data();
templateStmt += "class "s.append(tpParm->getName().str());
qualType += tpParm->getName();
needsComma = true;
} else if (const clang::NonTypeTemplateParmDecl* nonTypeParm =
@ -229,7 +249,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
templateStmt += ", ";
qualType += ", ";
}
templateStmt += nonTypeParm->getType().getAsString() + ' ' + nonTypeParm->getName().data();
templateStmt += nonTypeParm->getType().getAsString().append(1, ' ').append(nonTypeParm->getName().str());
qualType += nonTypeParm->getName();
needsComma = true;
}
@ -277,10 +297,13 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
int numTuples = int(specParms.size()) / numParms;
for (const auto& parent : parentSpecializations) {
for (int i = 0; i < numTuples; ++i) {
if (parent.first.empty())
specializations.emplace_back(std::string(rec->getName().data()) + '<', 1);
else
specializations.emplace_back(parent.first + "::" + rec->getName().data() + '<', parent.second + 1);
if (parent.first.empty()) {
specializations.emplace_back(std::string(rec->getName().str()).append(1, '<'), 1);
} else {
auto specialization =
std::string(parent.first).append("::").append(rec->getName().str()).append(1, '<');
specializations.emplace_back(std::move(specialization), parent.second + 1);
}
bool needsComma = false;
for (auto it = specParms.begin() + i * numParms;
it != specParms.end() && it != specParms.begin() + (i + 1) * numParms; ++it) {
@ -290,7 +313,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
specializations.back().first += trimmed;
needsComma = true;
}
specializations.back().first += ">";
specializations.back().first += '>';
}
}
foundSpecializations = true;
@ -301,16 +324,20 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
}
}
if (!foundSpecializations)
if (!foundSpecializations) {
for (const auto& parent : parentSpecializations) {
if (const clang::NamedDecl* namedDecl = clang::dyn_cast_or_null<clang::NamedDecl>(decl)) {
if (parent.first.empty())
specializations.emplace_back(namedDecl->getName().data(), parent.second);
else
specializations.emplace_back(parent.first + "::" + namedDecl->getName().data(), parent.second);
} else
if (parent.first.empty()) {
specializations.emplace_back(namedDecl->getName().str(), parent.second);
} else {
specializations.emplace_back(std::string(parent.first).append("::").append(namedDecl->getName().str()),
parent.second);
}
} else {
specializations.push_back(parent);
}
}
}
}
static std::vector<std::pair<std::string, int>> GetNestedTypeSpecializations(const clang::DeclContext* decl) {
@ -336,8 +363,8 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
std::string m_fieldName;
std::string m_ioOp;
bool m_squelched = false;
OutputNode(NodeType type, const std::string& fieldName, const std::string& ioOp, bool squelched)
: m_type(type), m_fieldName(fieldName), m_ioOp(ioOp), m_squelched(squelched) {}
OutputNode(NodeType type, std::string fieldName, std::string ioOp, bool squelched)
: m_type(type), m_fieldName(std::move(fieldName)), m_ioOp(std::move(ioOp)), m_squelched(squelched) {}
};
std::vector<OutputNode> outputNodes;
@ -389,24 +416,24 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
}
std::string ioOp;
bool isDNAType = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
if (defaultEndian)
if (defaultEndian) {
ioOp = GetOpString(fieldName, propIdExpr);
else
} else {
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
}
}
if (ioOp.empty()) {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '" + tsDecl->getName().str() + "' with Athena");
diag.AddString("Unable to use type '"s.append(tsDecl->getName().str()).append("' with Athena"));
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
outputNodes.emplace_back(NodeType::Do, fieldName, ioOp, false);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
} else if (tsDecl->getName() == "Vector") {
llvm::APSInt endian(64, -1);
std::string endianExprStr;
@ -457,20 +484,20 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
clang::QualType templateType;
std::string ioOp;
bool isDNAType = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
templateType = arg.getAsType().getCanonicalType();
if (defaultEndian)
if (defaultEndian) {
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr);
else
} else {
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExpr, endianExprStr);
}
}
}
if (ioOp.empty()) {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '" + templateType.getAsString() + "' with Athena");
diag.AddString("Unable to use type '"s.append(templateType.getAsString()).append("' with Athena"));
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
@ -482,7 +509,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
continue;
}
outputNodes.emplace_back(NodeType::Do, fieldName, ioOp, false);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
} else if (tsDecl->getName() == "Buffer") {
const clang::Expr* sizeExpr = nullptr;
std::string sizeExprStr;
@ -514,8 +541,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
}
std::string ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
outputNodes.emplace_back(NodeType::Do, fieldName, ioOp, false);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
} else if (tsDecl->getName() == "String") {
std::string sizeExprStr;
for (const clang::TemplateArgument& arg : *tsType) {
@ -537,12 +563,13 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
}
std::string ioOp;
if (!sizeExprStr.empty())
if (!sizeExprStr.empty()) {
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
else
} else {
ioOp = GetOpString(fieldName, propIdExpr);
}
outputNodes.emplace_back(NodeType::Do, fieldName, ioOp, false);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
} else if (tsDecl->getName() == "WString") {
llvm::APSInt endian(64, -1);
std::string endianExprStr;
@ -585,22 +612,24 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
std::string ioOp;
if (!sizeExprStr.empty()) {
if (defaultEndian)
if (defaultEndian) {
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr);
else
} else {
ioOp = GetVectorOpString(fieldName, propIdExpr, sizeExprStr, endianExprStr);
}
} else {
if (defaultEndian)
if (defaultEndian) {
ioOp = GetOpString(fieldName, propIdExpr);
else
} else {
ioOp = GetOpString(fieldName, propIdExpr, endianExprStr);
}
}
outputNodes.emplace_back(NodeType::Do, fieldName, ioOp, false);
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), std::move(ioOp), false);
} else if (tsDecl->getName() == "Seek") {
size_t idx = 0;
std::string offsetExprStr;
llvm::APSInt direction(64, 0);
llvm::APSInt direction(64, false);
const clang::Expr* directionExpr = nullptr;
bool bad = false;
for (const clang::TemplateArgument& arg : *tsType) {
@ -620,8 +649,11 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
offsetExprStr = offsetLiteral.toString(10);
}
} else {
directionExpr = expr;
if (!expr->isIntegerConstantExpr(direction, context)) {
clang::APValue result;
if (expr->isCXX11ConstantExpr(context, &result)) {
directionExpr = expr;
direction = result.getInt();
} else {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(expr->getExprLoc(), AthenaError);
diag.AddString("Unable to use non-constant direction expression in Athena");
diag.AddSourceRange(clang::CharSourceRange(expr->getSourceRange(), true));
@ -650,15 +682,16 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
continue;
}
if (directionVal == 0)
outputNodes.emplace_back(NodeType::DoSeek, fieldName, "<Op>("s + offsetExprStr + ", athena::Begin, s)",
false);
else if (directionVal == 1)
outputNodes.emplace_back(NodeType::DoSeek, fieldName, "<Op>("s + offsetExprStr + ", athena::Current, s)",
false);
else if (directionVal == 2)
outputNodes.emplace_back(NodeType::DoSeek, fieldName, "<Op>("s + offsetExprStr + ", athena::End, s)",
false);
if (directionVal == 0) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::Begin, s)"), false);
} else if (directionVal == 1) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::Current, s)"), false);
} else if (directionVal == 2) {
outputNodes.emplace_back(NodeType::DoSeek, std::move(fieldName),
"<Op>("s.append(offsetExprStr).append(", athena::SeekOrigin::End, s)"), false);
}
} else if (tsDecl->getName() == "Align") {
llvm::APSInt align(64, 0);
bool bad = false;
@ -677,25 +710,26 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
if (bad)
continue;
int64_t alignVal = align.getSExtValue();
const int64_t alignVal = align.getSExtValue();
if (alignVal) {
outputNodes.emplace_back(NodeType::DoAlign, fieldName, "<Op>("s + align.toString(10, true) + ", s)", false);
outputNodes.emplace_back(NodeType::DoAlign, std::move(fieldName),
"<Op>("s.append(align.toString(10, true)).append(", s)"), false);
}
} else {
const clang::NamedDecl* nd = tsDecl->getTemplatedDecl();
if (const clang::CXXRecordDecl* rd = clang::dyn_cast_or_null<clang::CXXRecordDecl>(nd)) {
std::string baseDNA;
if (isDNARecord(rd, baseDNA))
outputNodes.emplace_back(NodeType::Do, fieldName, GetOpString(fieldName, propIdExpr), false);
std::string baseDNA2;
if (isDNARecord(rd, baseDNA2)) {
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), GetOpString(fieldName, propIdExpr), false);
}
}
}
}
else if (regType->getTypeClass() == clang::Type::Record) {
} else if (regType->getTypeClass() == clang::Type::Record) {
const clang::CXXRecordDecl* cxxRDecl = regType->getAsCXXRecordDecl();
std::string baseDNA;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA))
outputNodes.emplace_back(NodeType::Do, fieldName, GetOpString(fieldName, propIdExpr), false);
std::string baseDNA2;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA2)) {
outputNodes.emplace_back(NodeType::Do, std::move(fieldName), GetOpString(fieldName, propIdExpr), false);
}
}
}
@ -781,7 +815,6 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
}
std::string ioOp;
bool isDNAType = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
if (defaultEndian)
@ -793,7 +826,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
if (ioOp.empty()) {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '" + tsDecl->getName().str() + "' with Athena");
diag.AddString("Unable to use type '"s.append(tsDecl->getName().str()).append("' with Athena"));
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
@ -841,7 +874,6 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
clang::QualType templateType;
std::string ioOp;
bool isDNAType = false;
for (const clang::TemplateArgument& arg : *tsType) {
if (arg.getKind() == clang::TemplateArgument::Type) {
templateType = arg.getAsType().getCanonicalType();
@ -854,7 +886,7 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
if (ioOp.empty()) {
clang::DiagnosticBuilder diag = context.getDiagnostics().Report(field->getLocation(), AthenaError);
diag.AddString("Unable to use type '" + templateType.getAsString() + "' with Athena");
diag.AddString("Unable to use type '"s.append(templateType.getAsString()).append("' with Athena"));
diag.AddSourceRange(clang::CharSourceRange(field->getSourceRange(), true));
continue;
}
@ -992,20 +1024,18 @@ class ATDNAEmitVisitor : public clang::RecursiveASTVisitor<ATDNAEmitVisitor> {
} else {
const clang::NamedDecl* nd = tsDecl->getTemplatedDecl();
if (const clang::CXXRecordDecl* rd = clang::dyn_cast_or_null<clang::CXXRecordDecl>(nd)) {
std::string baseDNA;
if (isDNARecord(rd, baseDNA)) {
std::string baseDNA2;
if (isDNARecord(rd, baseDNA2)) {
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << GetOpString(fieldName, propIdExpr) << ";\n"
<< " return true;\n";
}
}
}
}
else if (regType->getTypeClass() == clang::Type::Record) {
} else if (regType->getTypeClass() == clang::Type::Record) {
const clang::CXXRecordDecl* cxxRDecl = regType->getAsCXXRecordDecl();
std::string baseDNA;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA)) {
std::string baseDNA2;
if (cxxRDecl && isDNARecord(cxxRDecl, baseDNA2)) {
fileOut << " AT_PROP_CASE(" << propIdExpr << "):\n"
<< " Do" << GetOpString(fieldName, propIdExpr) << ";\n"
<< " return true;\n";
@ -1167,24 +1197,27 @@ int main(int argc, const char** argv) {
if (Help)
llvm::cl::PrintHelpMessage();
std::vector<std::string> args = {"clang-tool",
std::vector<std::string> args = {
"clang-tool",
#ifdef __linux__
"--gcc-toolchain=/usr",
"--gcc-toolchain=/usr",
#endif
"-fsyntax-only",
"-std=c++1z",
"-D__atdna__=1",
"-Wno-expansion-to-defined",
"-Wno-nullability-completeness",
"-Werror=shadow-field",
"-I" XSTR(INSTALL_PREFIX) "/lib/clang/" CLANG_VERSION_STRING "/include",
"-I" XSTR(INSTALL_PREFIX) "/include/Athena"};
for (int a = 1; a < argc; ++a)
args.push_back(argv[a]);
"-fsyntax-only",
"-std=c++1z",
"-D__atdna__=1",
"-Wno-expansion-to-defined",
"-Wno-nullability-completeness",
"-Werror=shadow-field",
"-I" XSTR(INSTALL_PREFIX) "/lib/clang/" CLANG_VERSION_STRING "/include",
"-I" XSTR(INSTALL_PREFIX) "/include/Athena",
};
for (int a = 1; a < argc; ++a) {
args.emplace_back(argv[a]);
}
llvm::IntrusiveRefCntPtr<clang::FileManager> fman(new clang::FileManager(clang::FileSystemOptions()));
ATDNAAction* action = new ATDNAAction();
clang::tooling::ToolInvocation TI(args, action, fman.get());
clang::tooling::ToolInvocation TI(std::move(args), action, fman.get());
if (!TI.run())
return 1;

View File

@ -1,7 +1,7 @@
#include <athena/DNAYaml.hpp>
using namespace athena;
typedef io::DNA<Big> BigDNA;
typedef io::DNA<Endian::Big> BigDNA;
enum ETest : atUint8 { ZERO, ONE, TWO, THREE };
@ -54,13 +54,13 @@ struct AT_SPECIALIZE_PARMS(atUint16, 42, atUint32, 87, atUint32, 2) TESTFile : p
Value<TESTTemplateSubFile<atInt32, 36>> nestedTemplate1;
Value<TESTTemplateSubFile<atInt64, 96>> nestedTemplate2;
Value<atUint32, Little> arrCount[2];
Value<atUint32, Endian::Little> arrCount[2];
Vector<atUint32, AT_DNA_COUNT(arrCount[0])> array;
Value<atUint32> arrAltCount;
Vector<atUint32, AT_DNA_COUNT(arrAltCount)> arrayAlt;
Seek<21, Current> seek;
Seek<21, SeekOrigin::Current> seek;
Value<atUint32> arrCount2;
Vector<TESTSubFile<ETest::ZERO>, AT_DNA_COUNT(arrCount[1] + arrCount2)> array2;

View File

@ -372,7 +372,7 @@ struct Read {
}
static void DoSeek(atInt64 amount, SeekOrigin whence, StreamT& r) { r.seek(amount, whence); }
static void DoAlign(atInt64 amount, StreamT& r) {
r.seek((r.position() + amount - 1) / amount * amount, athena::Begin);
r.seek((r.position() + amount - 1) / amount * amount, athena::SeekOrigin::Begin);
}
};
#define __READ_S(type, endian) \
@ -1148,29 +1148,29 @@ void __BinarySizeProp64(const T& obj, size_t& s) {
#define AT_SUBSPECIALIZE_DNA(...) \
template <> \
template <> \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Big>::BinarySize>(typename BinarySize::StreamT & s) { \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Endian::Big>::BinarySize>(typename BinarySize::StreamT & s) { \
_binarySize(s); \
} \
template <> \
template <> \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Big>::Read>(typename Read::StreamT & r) { \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Endian::Big>::Read>(typename Read::StreamT & r) { \
_read(r); \
} \
template <> \
template <> \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Big>::Write>(typename Write::StreamT & w) { \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Endian::Big>::Write>(typename Write::StreamT & w) { \
_write(w); \
}
#define AT_SUBSPECIALIZE_DNA_YAML(...) \
template <> \
template <> \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Big>::ReadYaml>(typename ReadYaml::StreamT & r) { \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Endian::Big>::ReadYaml>(typename ReadYaml::StreamT & r) { \
_read(r); \
} \
template <> \
template <> \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Big>::WriteYaml>(typename WriteYaml::StreamT & w) { \
void __VA_ARGS__::Enumerate<athena::io::DNA<athena::Endian::Big>::WriteYaml>(typename WriteYaml::StreamT & w) { \
_write(w); \
} \
AT_SUBSPECIALIZE_DNA(__VA_ARGS__)

View File

@ -116,7 +116,7 @@ bool ValidateFromYAMLStream(athena::io::IStreamReader& fin) {
atUint64 pos = fin.position();
yaml_parser_set_input(reader.getParser(), (yaml_read_handler_t*)YAMLAthenaReader, &fin);
bool retval = reader.ValidateClassType(DNASubtype::DNAType());
fin.seek(pos, athena::Begin);
fin.seek(pos, athena::SeekOrigin::Begin);
return retval;
}

View File

@ -87,33 +87,33 @@ typedef struct stat64 atStat64_t;
#ifndef ENABLE_BITWISE_ENUM
#define ENABLE_BITWISE_ENUM(type) \
constexpr type operator|(type a, type b) { \
constexpr type operator|(type a, type b) noexcept { \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) | static_cast<T>(b)); \
} \
constexpr type operator&(type a, type b) { \
constexpr type operator&(type a, type b) noexcept { \
using T = std::underlying_type_t<type>; \
return type(static_cast<T>(a) & static_cast<T>(b)); \
} \
inline type& operator|=(type& a, const type& b) { \
constexpr type& operator|=(type& a, type b) noexcept { \
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) | static_cast<T>(b)); \
return a; \
} \
inline type& operator&=(type& a, const type& b) { \
constexpr type& operator&=(type& a, type b) noexcept { \
using T = std::underlying_type_t<type>; \
a = type(static_cast<T>(a) & static_cast<T>(b)); \
return a; \
} \
constexpr type operator~(type key) { \
constexpr type operator~(type key) noexcept { \
using T = std::underlying_type_t<type>; \
return type(~static_cast<T>(key)); \
} \
constexpr bool True(type key) { \
constexpr bool True(type key) noexcept { \
using T = std::underlying_type_t<type>; \
return static_cast<T>(key) != 0; \
} \
constexpr bool False(type key) { \
constexpr bool False(type key) noexcept { \
using T = std::underlying_type_t<type>; \
return static_cast<T>(key) == 0; \
}
@ -123,9 +123,9 @@ namespace athena {
namespace error {
enum class Level { Message, Warning, Error, Fatal };
}
enum SeekOrigin { Begin, Current, End };
enum class SeekOrigin { Begin, Current, End };
enum Endian { Little, Big };
enum class Endian { Little, Big };
namespace io {
template <Endian DNAE>

View File

@ -23,9 +23,9 @@ protected:
void setError() { m_hasError = true; }
bool m_hasError = false;
#if __BYTE_ORDER == __BIG_ENDIAN
Endian m_endian = Big;
Endian m_endian = Endian::Big;
#else
Endian m_endian = Little;
Endian m_endian = Endian::Little;
#endif
};
} // namespace athena::io

View File

@ -143,7 +143,7 @@ public:
atInt16 readInt16() {
atInt16 val = 0;
readUBytesToBuf(&val, 2);
return m_endian == Big ? utility::BigInt16(val) : utility::LittleInt16(val);
return m_endian == Endian::Big ? utility::BigInt16(val) : utility::LittleInt16(val);
}
template <class T>
atInt16 readVal(std::enable_if_t<std::is_same_v<T, atInt16>>* = nullptr) {
@ -229,7 +229,7 @@ public:
atInt32 readInt32() {
atInt32 val = 0;
readUBytesToBuf(&val, 4);
return m_endian == Big ? utility::BigInt32(val) : utility::LittleInt32(val);
return m_endian == Endian::Big ? utility::BigInt32(val) : utility::LittleInt32(val);
}
template <class T>
atInt32 readVal(std::enable_if_t<std::is_same_v<T, atInt32>>* = nullptr) {
@ -315,7 +315,7 @@ public:
atInt64 readInt64() {
atInt64 val = 0;
readUBytesToBuf(&val, 8);
return m_endian == Big ? utility::BigInt64(val) : utility::LittleInt64(val);
return m_endian == Endian::Big ? utility::BigInt64(val) : utility::LittleInt64(val);
}
template <class T>
atInt64 readVal(std::enable_if_t<std::is_same_v<T, atInt64>>* = nullptr) {
@ -401,7 +401,7 @@ public:
float readFloat() {
float val = 0.f;
readUBytesToBuf(&val, 4);
return m_endian == Big ? utility::BigFloat(val) : utility::LittleFloat(val);
return m_endian == Endian::Big ? utility::BigFloat(val) : utility::LittleFloat(val);
}
template <class T>
float readVal(std::enable_if_t<std::is_same_v<T, float>>* = nullptr) {
@ -446,7 +446,7 @@ public:
double readDouble() {
double val = 0.0;
readUBytesToBuf(&val, 8);
return m_endian == Big ? utility::BigDouble(val) : utility::LittleDouble(val);
return m_endian == Endian::Big ? utility::BigDouble(val) : utility::LittleDouble(val);
}
template <class T>
double readVal(std::enable_if_t<std::is_same_v<T, double>>* = nullptr) {
@ -513,7 +513,7 @@ public:
atVec2f readVec2f() {
simd_floats val = {};
readUBytesToBuf(val.data(), 8);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigFloat(val[0]);
val[1] = utility::BigFloat(val[1]);
} else {
@ -581,7 +581,7 @@ public:
atVec3f readVec3f() {
simd_floats val = {};
readUBytesToBuf(val.data(), 12);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigFloat(val[0]);
val[1] = utility::BigFloat(val[1]);
val[2] = utility::BigFloat(val[2]);
@ -650,7 +650,7 @@ public:
atVec4f readVec4f() {
simd_floats val = {};
readUBytesToBuf(val.data(), 16);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigFloat(val[0]);
val[1] = utility::BigFloat(val[1]);
val[2] = utility::BigFloat(val[2]);
@ -720,7 +720,7 @@ public:
atVec2d readVec2d() {
simd_doubles val = {};
readUBytesToBuf(val.data(), 16);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigDouble(val[0]);
val[1] = utility::BigDouble(val[1]);
} else {
@ -788,7 +788,7 @@ public:
atVec3d readVec3d() {
simd_doubles val = {};
readUBytesToBuf(val.data(), 24);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigDouble(val[0]);
val[1] = utility::BigDouble(val[1]);
val[2] = utility::BigDouble(val[2]);
@ -857,7 +857,7 @@ public:
atVec4d readVec4d() {
simd_doubles val = {};
readUBytesToBuf(val.data(), 32);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
val[0] = utility::BigDouble(val[0]);
val[1] = utility::BigDouble(val[1]);
val[2] = utility::BigDouble(val[2]);

View File

@ -91,10 +91,11 @@ public:
* @param val The value to write to the buffer
*/
void writeInt16(atInt16 val) {
if (m_endian == Big)
if (m_endian == Endian::Big) {
utility::BigInt16(val);
else
} else {
utility::LittleInt16(val);
}
writeUBytes((atUint8*)&val, 2);
}
void writeVal(atInt16 val) { writeInt16(val); }
@ -151,10 +152,11 @@ public:
* @param val The value to write to the buffer
*/
void writeInt32(atInt32 val) {
if (m_endian == Big)
if (m_endian == Endian::Big) {
utility::BigInt32(val);
else
} else {
utility::LittleInt32(val);
}
writeUBytes((atUint8*)&val, 4);
}
void writeVal(atInt32 val) { writeInt32(val); }
@ -211,10 +213,11 @@ public:
* @param val The value to write to the buffer
*/
void writeInt64(atInt64 val) {
if (m_endian == Big)
if (m_endian == Endian::Big) {
utility::BigInt64(val);
else
} else {
utility::LittleInt64(val);
}
writeUBytes((atUint8*)&val, 8);
}
void writeVal(atInt64 val) { writeInt64(val); }
@ -271,10 +274,11 @@ public:
* @param val The value to write to the buffer
*/
void writeFloat(float val) {
if (m_endian == Big)
if (m_endian == Endian::Big) {
val = utility::BigFloat(val);
else
} else {
val = utility::LittleFloat(val);
}
writeUBytes((atUint8*)&val, 4);
}
void writeVal(float val) { writeFloat(val); }
@ -307,10 +311,11 @@ public:
* @param val The value to write to the buffer
*/
void writeDouble(double val) {
if (m_endian == Big)
if (m_endian == Endian::Big) {
utility::BigDouble(val);
else
} else {
utility::LittleDouble(val);
}
writeUBytes((atUint8*)&val, 8);
}
void writeVal(double val) { writeDouble(val); }
@ -354,7 +359,7 @@ public:
*/
void writeVec2f(const atVec2f& vec) {
simd_floats tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigFloat(tmp[0]);
tmp[1] = utility::BigFloat(tmp[1]);
} else {
@ -398,7 +403,7 @@ public:
*/
void writeVec3f(const atVec3f& vec) {
simd_floats tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigFloat(tmp[0]);
tmp[1] = utility::BigFloat(tmp[1]);
tmp[2] = utility::BigFloat(tmp[2]);
@ -446,7 +451,7 @@ public:
*/
void writeVec4f(const atVec4f& vec) {
simd_floats tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigFloat(tmp[0]);
tmp[1] = utility::BigFloat(tmp[1]);
tmp[2] = utility::BigFloat(tmp[2]);
@ -498,7 +503,7 @@ public:
*/
void writeVec2d(const atVec2d& vec) {
simd_doubles tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigDouble(tmp[0]);
tmp[1] = utility::BigDouble(tmp[1]);
} else {
@ -542,7 +547,7 @@ public:
*/
void writeVec3d(const atVec3d& vec) {
simd_doubles tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigDouble(tmp[0]);
tmp[1] = utility::BigDouble(tmp[1]);
tmp[2] = utility::BigDouble(tmp[2]);
@ -590,7 +595,7 @@ public:
*/
void writeVec4d(const atVec4d& vec) {
simd_doubles tmp(vec.simd);
if (m_endian == Big) {
if (m_endian == Endian::Big) {
tmp[0] = utility::BigDouble(tmp[0]);
tmp[1] = utility::BigDouble(tmp[1]);
tmp[2] = utility::BigDouble(tmp[2]);

View File

@ -90,11 +90,11 @@ public:
void writeUBytes(const atUint8* data, atUint64 length) override;
protected:
MemoryWriter() {}
atUint8* m_data;
atUint64 m_length;
atUint64 m_position;
bool m_bufferOwned;
MemoryWriter() = default;
atUint8* m_data = nullptr;
atUint64 m_length = 0;
atUint64 m_position = 0;
bool m_bufferOwned = false;
std::string m_filepath; //!< Path to the target file
};

View File

@ -16,8 +16,8 @@ constexpr bool isSystemBigEndian() { return false; }
#else
constexpr bool isSystemBigEndian() { return __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__; }
#endif
constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? Big : Little;
constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? Little : Big;
constexpr ::athena::Endian SystemEndian = isSystemBigEndian() ? ::athena::Endian::Big : ::athena::Endian::Little;
constexpr ::athena::Endian NotSystemEndian = isSystemBigEndian() ? ::athena::Endian::Little : ::athena::Endian::Big;
#if _MSC_VER
#define BSWAP_CONSTEXPR inline

View File

@ -1,7 +1,12 @@
#include "LZ77/LZLookupTable.hpp"
#include "LZ77/LZType10.hpp"
#include <athena/MemoryWriter.hpp>
#include <cstddef>
#include <cstring>
#include <memory>
#include "LZ77/LZLookupTable.hpp"
#include <athena/MemoryWriter.hpp>
LZType10::LZType10(atInt32 MinimumOffset, atInt32 SlidingWindow, atInt32 MinimumMatch, atInt32 BlockSize)
: LZBase(MinimumOffset, SlidingWindow, MinimumMatch, BlockSize) {
@ -17,25 +22,25 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
athena::io::MemoryCopyWriter outbuf("tmp");
outbuf.writeUint32(encodeSize);
atUint8* ptrStart = (atUint8*)src;
atUint8* ptrEnd = (atUint8*)(src + srcLength);
const atUint8* ptrStart = src;
const atUint8* ptrEnd = src + srcLength;
// At most their will be two bytes written if the bytes can be compressed. So if all bytes in the block can be
// compressed it would take blockSize*2 bytes
atUint8* compressedBytes = new atUint8[m_blockSize * 2]; // Holds the compressed bytes yet to be written
auto compressedBytes = std::unique_ptr<atUint8[]>(new atUint8[m_blockSize * 2]); // Holds the compressed bytes yet to be written
while (ptrStart < ptrEnd) {
atUint8 blockLen = 0;
// In Binary represents 1 if byte is compressed or 0 if not compressed
// For example 01001000 means that the second and fifth byte in the blockSize from the left is compressed
atUint8* ptrBytes = compressedBytes;
atUint8* ptrBytes = compressedBytes.get();
for (atInt32 i = 0; i < m_blockSize; i++) {
// length_offset searchResult=Search(ptrStart, filedata, ptrEnd);
LZLengthOffset searchResult = m_lookupTable.search(ptrStart, src, ptrEnd);
const LZLengthOffset searchResult = m_lookupTable.search(ptrStart, src, ptrEnd);
// If the number of bytes to be compressed is at least the size of the Minimum match
if (searchResult.length >= (atUint32)m_minMatch) {
if (searchResult.length >= static_cast<atUint32>(m_minMatch)) {
// Gotta swap the bytes since system is wii is big endian and most computers are little endian
atUint16 lenOff = (((searchResult.length - m_minMatch) & 0xF) << 12) | ((searchResult.offset - 1) & 0xFFF);
athena::utility::BigUint16(lenOff);
@ -49,45 +54,50 @@ atUint32 LZType10::compress(const atUint8* src, atUint8** dstBuf, atUint32 srcLe
blockLen |= (1 << (7 - i));
// Stores which of the next 8 bytes is compressed
// bit 1 for compress and bit 0 for not compressed
} else
} else {
*ptrBytes++ = *ptrStart++;
}
}
outbuf.writeByte(blockLen);
outbuf.writeUBytes(compressedBytes, (atUint64)(ptrBytes - compressedBytes));
outbuf.writeUBytes(compressedBytes.get(), static_cast<atUint64>(ptrBytes - compressedBytes.get()));
}
delete[] compressedBytes;
compressedBytes = nullptr;
// Add zeros until the file is a multiple of 4
while ((outbuf.position() % 4) != 0)
while ((outbuf.position() % 4) != 0) {
outbuf.writeByte(0);
}
*dstBuf = outbuf.data();
outbuf.save();
return (atUint32)outbuf.length();
return static_cast<atUint32>(outbuf.length());
}
atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLength) {
if (*(atUint8*)(src) != 0x10)
if (*src != 0x10) {
return 0;
}
atUint32 uncompressedSize = *(atUint32*)(src); // Size of data when it is uncompressed
athena::utility::LittleUint32(uncompressedSize); // The compressed file has the filesize encoded in little endian
uncompressedSize = uncompressedSize >> 8; // first byte is the encode flag
// Size of data when it is uncompressed
atUint32 uncompressedSize;
std::memcpy(&uncompressedSize, src, sizeof(uncompressedSize));
atUint8* uncompressedData = new atUint8[uncompressedSize];
atUint8* outputPtr = uncompressedData;
atUint8* outputEndPtr = uncompressedData + uncompressedSize;
atUint8* inputPtr = (atUint8*)src + 4;
atUint8* inputEndPtr = (atUint8*)src + srcLength;
// The compressed file has the filesize encoded in little endian
athena::utility::LittleUint32(uncompressedSize);
// first byte is the encode flag
uncompressedSize = uncompressedSize >> 8;
auto uncompressedData = std::unique_ptr<atUint8[]>(new atUint8[uncompressedSize]);
atUint8* outputPtr = uncompressedData.get();
atUint8* outputEndPtr = uncompressedData.get() + uncompressedSize;
const atUint8* inputPtr = src + 4;
const atUint8* inputEndPtr = src + srcLength;
while (inputPtr < inputEndPtr && outputPtr < outputEndPtr) {
const atUint8 isCompressed = *inputPtr++;
atUint8 isCompressed = *inputPtr++;
for (atUint32 i = 0; i < (atUint32)m_blockSize; i++) {
for (atUint32 i = 0; i < static_cast<atUint32>(m_blockSize); i++) {
// Checks to see if the next byte is compressed by looking
// at its binary representation - E.g 10010000
// This says that the first extracted byte and the four extracted byte is compressed
@ -101,28 +111,29 @@ atUint32 LZType10::decompress(const atUint8* src, atUint8** dst, atUint32 srcLen
decoding.length = (lenOff >> 12) + m_minMatch;
decoding.offset = static_cast<atUint16>((lenOff & 0xFFF) + 1);
if ((outputPtr - decoding.offset) < uncompressedData) {
if ((outputPtr - decoding.offset) < uncompressedData.get()) {
// If the offset to look for uncompressed is passed the current uncompresed data then the data is not
// compressed
delete[] uncompressedData;
uncompressedData = nullptr;
dst = nullptr;
return 0;
}
for (atUint32 j = 0; j < decoding.length; ++j)
for (size_t j = 0; j < decoding.length; ++j) {
outputPtr[j] = (outputPtr - decoding.offset)[j];
}
outputPtr += decoding.length;
} else
} else {
*outputPtr++ = *inputPtr++;
}