add hashes
This commit is contained in:
@@ -26,9 +26,10 @@ namespace Generated
|
||||
{
|
||||
)END";
|
||||
|
||||
constexpr char StructHeader1[] =
|
||||
constexpr char StructHeader2[] =
|
||||
R"END( struct %s
|
||||
{
|
||||
static constexpr uint32_t Hash = %u;
|
||||
)END";
|
||||
|
||||
constexpr char StructField4[] =
|
||||
@@ -39,9 +40,10 @@ namespace Generated
|
||||
R"END( };
|
||||
)END";
|
||||
|
||||
constexpr char EnumHeader3[] =
|
||||
constexpr char EnumHeader4[] =
|
||||
R"END( struct %s
|
||||
{
|
||||
static constexpr uint32_t Hash = %u;
|
||||
static constexpr int32_t EntryCount = %u;
|
||||
enum Enum : %s
|
||||
{
|
||||
@@ -192,8 +194,11 @@ void CppFileWriter::WriteEnums(const Def::DefinitionFile& definitions)
|
||||
{
|
||||
const Def::Enum& e = definitions.Enums[enumIdx];
|
||||
|
||||
Write(
|
||||
WriteTemplates::EnumHeader3, e.Name, e.EntryCount, Generated::KnownType::CName[(int32_t)e.EnumType.Native]);
|
||||
Write(WriteTemplates::EnumHeader4,
|
||||
e.Name,
|
||||
e.Hash,
|
||||
e.EntryCount,
|
||||
Generated::KnownType::CName[(int32_t)e.EnumType.Native]);
|
||||
for (int32_t entryIdx = 0; entryIdx < e.EntryCount; ++entryIdx)
|
||||
{
|
||||
Write(WriteTemplates::EnumField1, e.EntryNames[entryIdx]);
|
||||
@@ -223,7 +228,7 @@ void CppFileWriter::WriteTypes(const Def::DefinitionFile& definitions)
|
||||
{
|
||||
const Def::Type& t = definitions.Types[typeIdx];
|
||||
|
||||
Write(WriteTemplates::StructHeader1, t.Name);
|
||||
Write(WriteTemplates::StructHeader2, t.Name, t.Hash);
|
||||
for (int32_t fieldIdx = 0; fieldIdx < t.FieldCount; ++fieldIdx)
|
||||
{
|
||||
char Type[64]{0};
|
||||
@@ -234,7 +239,7 @@ void CppFileWriter::WriteTypes(const Def::DefinitionFile& definitions)
|
||||
{
|
||||
bx::snprintf(Array, sizeof(Array), "[%u]", ArraySize);
|
||||
}
|
||||
Write(WriteTemplates::StructField4, Type, t.FieldNames[fieldIdx], Array, t.FieldVaules[fieldIdx]);
|
||||
Write(WriteTemplates::StructField4, Type, t.FieldNames[fieldIdx], Array, t.FieldValues[fieldIdx]);
|
||||
}
|
||||
Write(WriteTemplates::StructEnd);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include "Logging.h"
|
||||
#include "MiniDef.h"
|
||||
|
||||
#include "TypeDef.h"
|
||||
#include "bx/hash.h"
|
||||
#include "bx/string.h"
|
||||
#include <bx/filepath.h>
|
||||
#include <ctype.h>
|
||||
@@ -165,7 +167,7 @@ Parser::Result Parser::HandleType()
|
||||
Def::Type& t = Definitions.Types[Definitions.TypeCount];
|
||||
for (int32_t i = 0; i < Def::MaxFields; ++i)
|
||||
{
|
||||
bx::strCopy(t.FieldVaules[i], sizeof(t.FieldVaules[i]), "{}");
|
||||
bx::strCopy(t.FieldValues[i], sizeof(t.FieldValues[i]), "{}");
|
||||
}
|
||||
|
||||
CHECK(ReadName(t.Name));
|
||||
@@ -203,11 +205,11 @@ Parser::Result Parser::HandleType()
|
||||
CHECK(ExpectChar("("));
|
||||
CHECK(ExpectChar("\""));
|
||||
|
||||
int32_t Remaining = bx::min(GetRemaining(), (int32_t)BX_COUNTOF(Def::Type::FieldVaules[0]));
|
||||
int32_t Remaining = bx::min(GetRemaining(), (int32_t)BX_COUNTOF(Def::Type::FieldValues[0]));
|
||||
for (int32_t i = 0; i < Remaining; ++i)
|
||||
{
|
||||
if (*ReadPtr != '\\' && CmpAdvance("\"", Res)) break;
|
||||
t.FieldVaules[t.FieldCount][i] = *ReadPtr;
|
||||
t.FieldValues[t.FieldCount][i] = *ReadPtr;
|
||||
ReadPtr++;
|
||||
}
|
||||
if (Res != OK) return Res;
|
||||
@@ -404,7 +406,7 @@ Parser::Result Parser::ReadOptionalEnumValues(Def::Enum& Enum, int32_t EntryIdx)
|
||||
for (int32_t i = 0; i < Remaining; ++i)
|
||||
{
|
||||
if (CmpAdvance("\"", Res)) break;
|
||||
Enum.ExtraStringFields[extraIdx][EntryIdx][i] = *ReadPtr;
|
||||
Enum.ExtraStringFields[EntryIdx][extraIdx][i] = *ReadPtr;
|
||||
ReadPtr++;
|
||||
}
|
||||
if (Res != OK) return Res;
|
||||
@@ -425,6 +427,59 @@ int32_t Parser::GetRemaining()
|
||||
{
|
||||
return &Buffer[BufferSize] - ReadPtr;
|
||||
}
|
||||
uint32_t Parser::CalculateTypeHash(const Def::Type& t)
|
||||
{
|
||||
bx::HashMurmur2A hash;
|
||||
hash.begin();
|
||||
for (int32_t i = 0; i < t.FieldCount; ++i)
|
||||
{
|
||||
hash.add(t.FieldNames[i]);
|
||||
hash.add(t.FieldArraySizes[i]);
|
||||
|
||||
Def::EFieldType fieldType = t.FieldTypes[i].FieldKind;
|
||||
if (fieldType == Def::EFieldType::Native)
|
||||
{
|
||||
hash.add(t.FieldTypes[i].Native);
|
||||
}
|
||||
else if (fieldType == Def::EFieldType::DefinedClass)
|
||||
{
|
||||
Def::Type& dependType = Definitions.Types[t.FieldTypes[i].TypeIdx];
|
||||
if (dependType.Hash == 0)
|
||||
{
|
||||
CalculateTypeHash(dependType);
|
||||
}
|
||||
hash.add(dependType.Hash);
|
||||
}
|
||||
else if (fieldType == Def::EFieldType::DefinedEnum)
|
||||
{
|
||||
hash.add(Definitions.Enums[t.FieldTypes[i].TypeIdx].Hash);
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_ERROR(0, "TODO!");
|
||||
}
|
||||
}
|
||||
return hash.end();
|
||||
}
|
||||
void Parser::CalculateHashes()
|
||||
{
|
||||
for (int32_t i = 0; i < Definitions.EnumCount; ++i)
|
||||
{
|
||||
Def::Enum& e = Definitions.Enums[i];
|
||||
bx::HashMurmur2A hash;
|
||||
hash.begin();
|
||||
hash.add(e.EnumType.Native);
|
||||
e.Hash = hash.end();
|
||||
}
|
||||
for (int32_t i = 0; i < Definitions.TypeCount; ++i)
|
||||
{
|
||||
Def::Type& t = Definitions.Types[i];
|
||||
if (t.Hash == 0)
|
||||
{
|
||||
t.Hash = CalculateTypeHash(t);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Parser FileParser;
|
||||
CppFileWriter Writer;
|
||||
@@ -460,6 +515,8 @@ int main(int argc, const char** argv)
|
||||
return 1;
|
||||
}
|
||||
LOG(0, "Finished parsing!");
|
||||
|
||||
FileParser.CalculateHashes();
|
||||
Writer.GenerateCpp(outPath, FileParser.Definitions);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -76,6 +76,7 @@ class Parser
|
||||
|
||||
public:
|
||||
Result Parse();
|
||||
void CalculateHashes();
|
||||
|
||||
private:
|
||||
int32_t GetRemaining();
|
||||
@@ -94,6 +95,8 @@ class Parser
|
||||
Result ReadTypeToken();
|
||||
Result ReadOptionalEnumValues(Def::Enum& Enum, int32_t EntryIdx);
|
||||
|
||||
uint32_t CalculateTypeHash(const Def::Type& t);
|
||||
|
||||
void ErrorLine()
|
||||
{
|
||||
char line[64]{0};
|
||||
|
||||
@@ -6,7 +6,7 @@ namespace Def
|
||||
{
|
||||
constexpr int32_t MaxNameLength = 64;
|
||||
constexpr int32_t MaxFields = 64;
|
||||
constexpr int32_t MaxExtraEnumFields = 1;
|
||||
constexpr int32_t MaxExtraEnumFields = 2;
|
||||
enum class EFieldType
|
||||
{
|
||||
Native,
|
||||
@@ -26,8 +26,9 @@ namespace Def
|
||||
FieldType FieldTypes[MaxFields];
|
||||
char FieldNames[MaxFields][MaxNameLength];
|
||||
uint32_t FieldArraySizes[MaxFields]{0};
|
||||
char FieldVaules[MaxFields][128]{0};
|
||||
char FieldValues[MaxFields][128]{0};
|
||||
char Name[MaxNameLength]{0};
|
||||
uint32_t Hash = 0;
|
||||
};
|
||||
|
||||
struct Enum
|
||||
@@ -39,6 +40,7 @@ namespace Def
|
||||
char ExtraStringFieldNames[MaxExtraEnumFields][MaxNameLength];
|
||||
char ExtraStringFields[MaxFields][MaxExtraEnumFields][MaxNameLength];
|
||||
char Name[MaxNameLength]{0};
|
||||
uint32_t Hash = 0;
|
||||
};
|
||||
|
||||
struct DefinitionFile
|
||||
|
||||
Reference in New Issue
Block a user