From 0419dc3939f2bdfbb5e1e4e116ddd942271c9dee Mon Sep 17 00:00:00 2001 From: yyc12345 Date: Tue, 27 Jan 2026 16:38:29 +0800 Subject: [PATCH] fix: finish enums migration --- .../EnumsAnalyzer/CSharpWriter.java | 168 ----------- .../EnumsAnalyzer/CommonHelper.java | 132 +++------ .../EnumsAnalyzer/CppWriter.java | 273 ------------------ .../EnumsAnalyzer/IndentHelper.java | 116 -------- .../EnumsAnalyzer/JsonWriter.java | 4 +- .../EnumsAnalyzer/MainRunner.java | 58 +--- .../EnumsAnalyzer/PythonWriter.java | 185 ------------ .../EnumsRender/template_render.py | 94 +++++- .../templates/CKERROR.docstring.cpp.jinja | 4 +- .../templates/CK_CLASSID.docstring.cpp.jinja | 4 +- .../EnumsRender/templates/generic.cs.jinja | 6 +- .../templates/generic.docstring.cs.jinja | 6 +- .../templates/generic.docstring.py.jinja | 16 + .../EnumsRender/templates/generic.hpp.jinja | 4 +- .../EnumsRender/templates/generic.py.jinja | 14 +- 15 files changed, 165 insertions(+), 919 deletions(-) delete mode 100644 Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CSharpWriter.java delete mode 100644 Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CppWriter.java delete mode 100644 Assets/CodeGen/EnumsMigration/EnumsAnalyzer/IndentHelper.java delete mode 100644 Assets/CodeGen/EnumsMigration/EnumsAnalyzer/PythonWriter.java diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CSharpWriter.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CSharpWriter.java deleted file mode 100644 index 101d785..0000000 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CSharpWriter.java +++ /dev/null @@ -1,168 +0,0 @@ -import java.io.OutputStreamWriter; - -/** - * Write enum declarations and accessible value into CSharp format. - */ -public class CSharpWriter { - - // =========== C# Enum Declaration Writer =========== - - /** - * Get corredponding C# underlying type of given enum. - *

- * This is C# specific function. - * - * @param canUnsigned The parameter stored in Enum_t that indicate whether this - * enum can use unsigned int as its underlying type. - * @return The string form of its underlying type. - */ - private static String getEnumUnderlyingType(boolean canUnsigned) { - return canUnsigned ? "uint" : "int"; - } - - /** - * Internal real enum declaration writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @throws Exception - */ - private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog) - throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CSharp); - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum comment - indent.briefComment(benum.mEnumComment); - - // write enum start - // write flasg attribute if it is - if (benum.mUseFlags) { - indent.puts("[Flags]"); - } - indent.printf("public enum %s : %s {", benum.mEnumName, getEnumUnderlyingType(benum.mCanUnsigned)); - indent.inc(); - - // write enum entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - // write entry self - if (enumEntry.mEntryValue == null) { - indent.printf("%s,", enumEntry.mEntryName); - } else { - indent.printf("%s = %s,", enumEntry.mEntryName, enumEntry.mEntryValue); - } - - // write entry comment after member - indent.afterMemberComment(enumEntry.mEntryComment); - } - - // write enum tail - indent.dec(); - indent.puts("}"); - } - } - - /** - * Write an enum declaration collection into given file. - *

- * Actually this is a wrapper of internal enum declaration collection writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @throws Exception - */ - public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, prog); - fs.close(); - } - - /** - * Write a single enum declaration into given file. - *

- * Actually this is a wrapper of internal enum declaration collection writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @throws Exception - */ - public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception { - // create collection from single enum - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, col); - fs.close(); - } - - // =========== C# Enum Accessible Value Writer =========== - - /** - * Internal real enum accessible value writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @throws Exception - */ - private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog) - throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.CSharp); - // write enum collections - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum desc header - indent.printf( - "public static readonly System.Collections.Generic.Dictionary<%s, string> %s = new System.Collections.Generic.Dictionary<%s, string>() {", - benum.mEnumName, benum.mEnumName, benum.mEnumName); - indent.inc(); - - // write enum desc entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - indent.printf("{ %s.%s, \"%s\" },", benum.mEnumName, enumEntry.mEntryName, enumEntry.mEntryName); - } - - // write enum tail - indent.dec(); - indent.puts("};"); - } - } - - /** - * Write an enum accessible value collection into given file. - *

- * Actually this is a wrapper of internal enum accessible value collection - * writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @throws Exception - */ - public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog) throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, prog); - fs.close(); - } - - /** - * Write a single enum accessible value into given file. - *

- * Actually this is a wrapper of internal enum accessible value collection - * writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @throws Exception - */ - public static void writeAccVal(String filename, EnumsHelper.BEnum _enum) throws Exception { - // create a collection with single enum. - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, col); - fs.close(); - } -} diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CommonHelper.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CommonHelper.java index 995823e..aa9728b 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CommonHelper.java +++ b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CommonHelper.java @@ -1,7 +1,6 @@ import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.OutputStreamWriter; -import java.net.URL; import java.nio.charset.StandardCharsets; import java.nio.file.Path; import java.nio.file.Paths; @@ -48,11 +47,35 @@ public class CommonHelper { switch (comment.getType()) { case CKGeneralLexer.CKGENERAL_LINE_COMMENT: { - return removeStars(comment.getText().substring(2)); + // For line comment, we start to remove "//" prefix first + String slashRemoved = comment.getText().substring(2); + // Then remove successive starts + String starsRemoved = removeSeperator(slashRemoved); + // Do a strip for it. + String stripped = starsRemoved.strip(); + // Then remove EOL to avoid unexpected line break. + String eolRemoved = removeEol(stripped); + // Okey + return eolRemoved; } case CKGeneralLexer.CKGENERAL_BLOCK_COMMENT: { - String cache = comment.getText(); - return removeStars(cache.substring(2, cache.length() - 4)); + // For block comment, we first cut "/*" head and "*/" tail. + String blockComment = comment.getText(); + String slashRemoved = blockComment.substring(2, blockComment.length() - 4); + + // Then we break it at line breaker and process each line one by one. + String beautyComment = slashRemoved.lines().map((String line) -> { + // Remove successive starts + String starsRemoved = removeSeperator(line); + // Do a strip for it. + String stripped = starsRemoved.strip(); + // Then remove EOL to avoid unexpected line break. + String eolRemoved = removeEol(stripped); + // Line process is okey now + return eolRemoved; + }).collect(Collectors.joining("\n")); // Then re-join with fresh line breaker + // Then return + return beautyComment; } default: return comment.getText(); @@ -73,9 +96,8 @@ public class CommonHelper { return null; return tokens.stream().map(value -> { - String text = cutComment(value).strip(); - return text + " "; - }).collect(Collectors.joining("")); + return cutComment(value); + }).collect(Collectors.joining("\n")); } // =========== Number Operations =========== @@ -92,46 +114,13 @@ public class CommonHelper { /** * Check whether Altlr captured CKGENERAL_NUM is a hex number. - * + * * @param numstr The captured number. * @return true if it is hex number. */ public static boolean isHexNumber(String numstr) { return numstr.startsWith("0x") || numstr.startsWith("0X"); } - - /** - * Convert accepted string into Python cupported format. - * - * It actually just remove trail "UL". - * - * @param numstr The captured number. - * @return The Python style number string. - */ - public static String convertToPythonNumber(String numstr) { - return numstr.replaceFirst("[ulUL]+$", ""); - } - - // =========== Parts =========== - - enum CKParts { - CK2, VxMath - } - - enum LangType { - Cpp, Python, CSharp - } - - public static String getCKPartsNamespace(CKParts parts) { - switch (parts) { - case CK2: - return "CK2"; - case VxMath: - return "VxMath"; - default: - throw new IllegalArgumentException("Unexpected value: " + parts); - } - } // =========== File Operations =========== @@ -182,80 +171,25 @@ public class CommonHelper { // =========== String Process =========== - /** - * Escape String - * - * Escape all characters which are invalid in string quote. - * - * @param strl The string need to be escaped. - * @return The escaped string. - * @see removeEol - */ - public static String escapeString(String strl) { - return strl.replace("\\", "\\\\").replace("\t", "\\t").replace("\b", "\\b").replace("\n", "\\n") - .replace("\r", "\\r").replace("\f", "\\f").replace("\"", "\\\""); - } - /** * Remove all EOL (End of Line) characters. * * @param strl The string need to be processed. * @return The string eliminated all EOL. - * @see escapeString */ public static String removeEol(String strl) { return strl.replace("\n", "").replace("\r", ""); } /** - * Remove redundent star '*' (at least 5 continuous star chars) + * Remove seperator bar consisted by '*' or '-' (at least 5 successive chars) * * @param cmt The string provided. * @return The string processed. */ - public static String removeStars(String cmt) { + public static String removeSeperator(String cmt) { // only remove at least 5 continuous star chars. - return cmt.replaceAll("\\*{5,}", ""); - } - - /** - * Get indent char by style - * - * @param use_tab Whether indent use Tab, not Space. - * @return The indent chars - */ - public static String getIndentString(boolean use_tab) { - if (use_tab) - return "\t"; - else - return " "; - } - - /** - * Re-indent a block of string - * - * This function will first split block string by line. Then remove all indent - * (strip Tab and Space). At last, re-indent and join each line - * - * @param block_str The string provided. - * @param use_tab Use Tab, not Space as indent chars. - * @param indent_level The level of indent, started by 0. - * @return The re-indent string - */ - public static String reindentBlockString(String block_str, boolean use_tab, int indent_level) { - // pre-create indent string - String indentChars = getIndentString(use_tab); - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < indent_level; ++i) { - sb.append(indentChars); - } - String indents = sb.toString(); - - // split line - return block_str.lines().map((String line) -> { - // strip space and tab, then re-indent it. - return indents + line.trim(); - }).collect(Collectors.joining(System.lineSeparator())); // then join with new line breaker and return. + return cmt.replaceAll("[\\*\\-]{5,}", ""); } } diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CppWriter.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CppWriter.java deleted file mode 100644 index 8a6eca2..0000000 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/CppWriter.java +++ /dev/null @@ -1,273 +0,0 @@ -import java.io.OutputStreamWriter; -import java.util.stream.Collectors; - -/** - * Write enum declarations and accessible value into C++ format. - */ -public class CppWriter { - - // =========== C++ Enum Declaration Writer =========== - - /** - * Get corredponding C++ underlying type of given enum. - *

- * This is C++ specific function. - * - * @param canUnsigned The parameter stored in Enum_t that indicate whether this - * enum can use unsigned int as its underlying type. - * @return The string form of its underlying type. - */ - private static String getEnumUnderlyingType(boolean canUnsigned) { - return canUnsigned ? "CKDWORD" : "CKINT"; - } - - /** - * Internal real enum declarations writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @throws Exception - */ - private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog) - throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp); - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum comment - indent.briefComment(benum.mEnumComment); - - // write enum start - indent.printf("enum class %s : %s {", benum.mEnumName, getEnumUnderlyingType(benum.mCanUnsigned)); - indent.inc(); - - // write enum entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - // write entry self - if (enumEntry.mEntryValue == null) { - indent.printf("%s,", enumEntry.mEntryName); - } else { - indent.printf("%s = %s,", enumEntry.mEntryName, enumEntry.mEntryValue); - } - - // write entry comment after member - indent.afterMemberComment(enumEntry.mEntryComment); - } - - // write enum tail - indent.dec(); - indent.puts("};"); - } - } - - /** - * Write an enum collection into given file. - *

- * Actually this is a wrapper of internal enum collection writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @throws Exception - */ - public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, prog); - fs.close(); - } - - /** - * Write a single enum into given file. - *

- * Actually this is a wrapper of internal enum collection writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @throws Exception - */ - public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception { - // create an collection from single enum declaration - // for suit the argument requirement of real writer. - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, col); - fs.close(); - } - - // =========== C++ Enum Accessible Value Writer =========== - - /** - * Internal real enum collection accessible value writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @param parts The part of these enum declarations. It will indicate the - * namespace where find given enum collection. - * @throws Exception - */ - private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog, - CommonHelper.CKParts parts) throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp); - - // write type defination (just to let user know what the type is) - indent.puts("// struct GeneralReflection { const char8_t* mName; };"); - indent.puts("// template, int> = 0>"); - indent.puts("// using GeneralReflectionArray = std::vector>;"); - - indent.puts(""); - indent.puts(""); - indent.puts(""); - - // write declarations - for (EnumsHelper.BEnum benum : prog.mEnums) { - indent.printf("extern const GeneralReflectionArray %s;", - CommonHelper.getCKPartsNamespace(parts), benum.mEnumName, benum.mEnumName); - } - - indent.puts(""); - indent.puts(""); - indent.puts(""); - - // write implements - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum desc header - indent.printf("const GeneralReflectionArray %s {", CommonHelper.getCKPartsNamespace(parts), - benum.mEnumName, benum.mEnumName); - indent.inc(); - - // write enum desc entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - indent.printf("{ LibCmo::%s::%s::%s, { u8\"%s\" } },", CommonHelper.getCKPartsNamespace(parts), - benum.mEnumName, enumEntry.mEntryName, enumEntry.mEntryName); - } - - // write enum tail - indent.dec(); - indent.puts("};"); - } - } - - /** - * Write an enum collection accessible value into given file. - *

- * Actually this is a wrapper of internal enum collection accessible value - * writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @param parts The part of these enum declarations. - * @throws Exception - */ - public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog, CommonHelper.CKParts parts) - throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, prog, parts); - fs.close(); - } - - /** - * Write a single enum accessible value into given file. - *

- * Actually this is a wrapper of internal enum collection accessible value - * writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @param parts The part of these enum declarations. - * @throws Exception - */ - public static void writeAccVal(String filename, EnumsHelper.BEnum _enum, CommonHelper.CKParts parts) - throws Exception { - // create a enum collection to fulfill the requirement of internal writer. - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, col, parts); - fs.close(); - } - - // =========== Specialized C++ Enum Accessible Value Writer =========== - // Only accessible value part of CERROR and CK_CLASSID need to be specialized. - // The enum self do not need special treat. Just write them normally. - - /** - * Specialized CKERROR accessible value writer. - *

- * The declaration of CKERROR do not need special treat. It is okey to use - * common writer. - * - * @param filename The name of written file. - * @param errors The {@linkplain EnumsHelper.BEnum} instance storing CKERROR. - * @throws Exception - */ - public static void writeCkErrorAccVal(String filename, EnumsHelper.BEnum errors) throws Exception { - OutputStreamWriter writer = CommonHelper.openOutputFile(filename); - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp); - - // write type defination (just to let user know what the type is) - indent.puts("// struct CkErrorReflection { const char8_t* mName; const char8_t* mDescription; };"); - indent.puts("// using CkErrorReflectionArray = std::vector>;"); - - indent.puts(""); - indent.puts(""); - indent.puts(""); - - // write implementation - indent.puts("const CkErrorReflectionArray CKERROR {"); - indent.inc(); - for (EnumsHelper.BEnumEntry entry : errors.mEntries) { - String comment = CommonHelper.escapeString(entry.mEntryComment); - if (comment == null) - comment = ""; - - indent.printf("{ LibCmo::CK2::CKERROR::%s, { u8\"%s\", u8\"%s\" } },", entry.mEntryName, entry.mEntryName, - comment); - } - indent.dec(); - indent.puts("};"); - - writer.close(); - } - - /** - * Specialized CK_CLASSID accessible value writer. - *

- * The declaration of CK_CLASSID do not need special treat. It is okey to use - * common writer. - * - * @param filename The name of written file. - * @param classids The {@linkplain EnumsHelper.BEnum} instance storing - * CK_CLASSID. - * @throws Exception - */ - public static void writeCkClassidAccVal(String filename, EnumsHelper.BEnum classids) throws Exception { - OutputStreamWriter writer = CommonHelper.openOutputFile(filename); - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Cpp); - - // write type defination (just to let user know what the type is) - indent.puts("// struct CkClassidReflection { std::vector mHierarchy; };"); - indent.puts("// using CkClassidReflectionArray = std::vector>;"); - - indent.puts(""); - indent.puts(""); - indent.puts(""); - - indent.puts("const CkClassidReflectionArray CK_CLASSID {"); - indent.inc(); - for (EnumsHelper.BEnumEntry entry : classids.mEntries) { - EnumsHelper.BHierarchyEnumEntry specialized = (EnumsHelper.BHierarchyEnumEntry) entry; - - String hierarchy = specialized.mHierarchy.stream().map(value -> value.mEntryName) - .collect(Collectors.joining("\", u8\"")); - indent.printf("{ LibCmo::CK2::CK_CLASSID::%s, { { u8\"%s\" } } },", entry.mEntryName, hierarchy); - } - indent.dec(); - indent.puts("};"); - - writer.close(); - } -} diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/IndentHelper.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/IndentHelper.java deleted file mode 100644 index a7e5a04..0000000 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/IndentHelper.java +++ /dev/null @@ -1,116 +0,0 @@ -import java.io.OutputStreamWriter; - -public class IndentHelper { - public IndentHelper(OutputStreamWriter writer, CommonHelper.LangType lang_type) { - mIndent = 0; - mLangType = lang_type; - mWriter = writer; - - // set indent chars - switch (mLangType) { - case Cpp: - case CSharp: - mIndentChars = CommonHelper.getIndentString(true); - break; - case Python: - mIndentChars = CommonHelper.getIndentString(false); - break; - default: - mIndentChars = ""; - break; - } - } - - private int mIndent; - private CommonHelper.LangType mLangType; - private String mIndentChars; - private OutputStreamWriter mWriter; - - private void rawIndent() throws Exception { - for (int i = 0; i < mIndent; ++i) { - mWriter.write(mIndentChars); - } - } - - public void inc() { - ++mIndent; - } - - public void dec() { - --mIndent; - } - - public void indent() throws Exception { - mWriter.write(System.lineSeparator()); - rawIndent(); - } - - public void puts(String data) throws Exception { - indent(); - mWriter.write(data); - } - - public void printf(String fmt, Object... args) throws Exception { - indent(); - mWriter.write(String.format(fmt, args)); - } - - /** - * Write standard Doxygen document comment. - *

- * Usually be called before writing content. - * - * @param comment - * @throws Exception - */ - public void briefComment(String comment) throws Exception { - if (comment == null) - return; - - switch (mLangType) { - case Cpp: - case CSharp: - puts("/**"); - - mWriter.write(System.lineSeparator()); - mWriter.write(CommonHelper.reindentBlockString(comment, true, mIndent)); - - puts(" */"); - break; - case Python: - puts("\"\"\"!"); - - mWriter.write(System.lineSeparator()); - mWriter.write(CommonHelper.reindentBlockString(comment, false, mIndent)); - - puts("\"\"\""); - break; - } - } - - /** - * Write suffix style Doxygen document comment. - *

- * Usually be called after writing content. - * - * @param comment - * @throws Exception - */ - public void afterMemberComment(String comment) throws Exception { - if (comment == null) - return; - - mWriter.write(mIndentChars); - switch (mLangType) { - case Cpp: - case CSharp: - mWriter.write(String.format("/**< %s */", CommonHelper.removeEol(comment))); - break; - case Python: - mWriter.write(String.format("##< %s", CommonHelper.removeEol(comment))); - break; - } - - } - -} diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/JsonWriter.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/JsonWriter.java index 2f8f8d9..0b2083a 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/JsonWriter.java +++ b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/JsonWriter.java @@ -15,7 +15,7 @@ public class JsonWriter { // Export hierarchy if possible if (enumEntry instanceof EnumsHelper.BHierarchyEnumEntry hierarchyEnumEntry) { // We only export name in hierarchy. - // Otherwise we may cause recursive calling. + // Otherwise, we may cause recursive calling. JsonArray entries = new JsonArray(); for (EnumsHelper.BHierarchyEnumEntry subEntry : hierarchyEnumEntry.mHierarchy) { entries.add(subEntry.mEntryName); @@ -54,7 +54,7 @@ public class JsonWriter { private static void writeJson(String filename, EnumsHelper.BEnumCollection enumCollection) throws Exception { OutputStreamWriter writer = CommonHelper.openOutputFile(filename); //Gson gsonInstance = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); - Gson gsonInstance = new GsonBuilder().disableHtmlEscaping().create(); + Gson gsonInstance = new GsonBuilder().serializeNulls().disableHtmlEscaping().create(); writer.write(gsonInstance.toJson(writeBEnumCollection(enumCollection))); writer.close(); } diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/MainRunner.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/MainRunner.java index 66fb579..b7044ce 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/MainRunner.java +++ b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/MainRunner.java @@ -37,8 +37,8 @@ public class MainRunner { *

* This function will assume that given file only contain C++ "#define" syntax. * After reading it, it will re-organize it as an enum and return. This only is - * used by CKERROR now. But it suit for more scenarios if there are something - * like CKERROR in future. + * used by CKERROR now. But it is suit for more scenarios if there are something + * like CKERROR in the future. * * @param filename The file name relative to input directory for reading. * @param assignedEnumName The desired name of organized enum instance. @@ -106,89 +106,35 @@ public class MainRunner { // =========== CKERROR =========== EnumsHelper.BEnum ckerror = organiseDefines("CKERROR.txt", "CKERROR"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CKERROR.json"), ckerror); -// CppWriter.writeEnum("CKERROR.hpp", ckerror); -// PythonWriter.writeEnum("CKERROR.py", ckerror); -// CSharpWriter.writeEnum("CKERROR.cs", ckerror); -// CppWriter.writeCkErrorAccVal("CKERROR.AccVal.hpp", ckerror); -// PythonWriter.writeAccVal("CKERROR.AccVal.py", ckerror); -// CSharpWriter.writeAccVal("CKERROR.AccVal.cs", ckerror); // =========== CK_CLASSID =========== EnumsHelper.BEnum classid = organiseClassid("CK_CLASSID.txt"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_CLASSID.json"), classid); -// CppWriter.writeEnum("CK_CLASSID.hpp", classid); -// PythonWriter.writeEnum("CK_CLASSID.py", classid); -// CSharpWriter.writeEnum("CK_CLASSID.cs", classid); -// CppWriter.writeCkClassidAccVal("CK_CLASSID.AccVal.hpp", classid); -// PythonWriter.writeAccVal("CK_CLASSID.AccVal.py", classid); // =========== Define2 =========== - // Define2 do not need annotation output. - // Because they are CKStateChunk used value which are not exposed to outside. EnumsHelper.BEnumCollection def2 = getEnumsCollection("Defines2.txt"); JsonWriter.writeEnums(CommonHelper.getOutputFilePath("Defines2.json"), def2); -// CppWriter.writeEnums("Defines2.hpp", def2); -// PythonWriter.writeEnums("Defines2.py", def2); -// CSharpWriter.writeEnums("Defines2.cs", def2); // =========== Combined enums =========== EnumsHelper.BEnumCollection ck2Enums = getEnumsCollection("CKEnums.txt"), vxEnums = getEnumsCollection("VxEnums.txt"); JsonWriter.writeEnums(CommonHelper.getOutputFilePath("CKEnums.json"), ck2Enums); JsonWriter.writeEnums(CommonHelper.getOutputFilePath("VxEnums.json"), vxEnums); - -// CppWriter.writeEnums("CKEnums.hpp", ck2Enums); -// PythonWriter.writeEnums("CKEnums.py", ck2Enums); -// CSharpWriter.writeEnums("CKEnums.cs", ck2Enums); -// CppWriter.writeAccVals("CKEnums.AccVal.hpp", ck2Enums, CommonHelper.CKParts.CK2); -// PythonWriter.writeAccVals("CKEnums.AccVal.py", ck2Enums); -// CSharpWriter.writeAccVals("CKEnums.AccVal.cs", ck2Enums); - -// CppWriter.writeEnums("VxEnums.hpp", vxEnums); -// PythonWriter.writeEnums("VxEnums.py", vxEnums); -// CSharpWriter.writeEnums("VxEnums.cs", vxEnums); -// CppWriter.writeAccVals("VxEnums.AccVal.hpp", vxEnums, CommonHelper.CKParts.VxMath); -// PythonWriter.writeAccVals("VxEnums.AccVal.py", vxEnums); -// CSharpWriter.writeAccVals("VxEnums.AccVal.cs", vxEnums); // =========== Single enums =========== EnumsHelper.BEnum single; single = organiseDefines("CK_STATECHUNK_CHUNKVERSION.txt", "CK_STATECHUNK_CHUNKVERSION"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_STATECHUNK_CHUNKVERSION.json"), single); -// CppWriter.writeEnum("CK_STATECHUNK_CHUNKVERSION.hpp", single); -// PythonWriter.writeEnum("CK_STATECHUNK_CHUNKVERSION.py", single); -// CSharpWriter.writeEnum("CK_STATECHUNK_CHUNKVERSION.cs", single); -// CppWriter.writeAccVal("CK_STATECHUNK_CHUNKVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2); -// PythonWriter.writeAccVal("CK_STATECHUNK_CHUNKVERSION.AccVal.py", single); -// CSharpWriter.writeAccVal("CK_STATECHUNK_CHUNKVERSION.AccVal.cs", single); single = organiseDefines("CK_STATECHUNK_DATAVERSION.txt", "CK_STATECHUNK_DATAVERSION"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_STATECHUNK_DATAVERSION.json"), single); -// CppWriter.writeEnum("CK_STATECHUNK_DATAVERSION.hpp", single); -// PythonWriter.writeEnum("CK_STATECHUNK_DATAVERSION.py", single); -// CSharpWriter.writeEnum("CK_STATECHUNK_DATAVERSION.cs", single); -// CppWriter.writeAccVal("CK_STATECHUNK_DATAVERSION.AccVal.hpp", single, CommonHelper.CKParts.CK2); -// PythonWriter.writeAccVal("CK_STATECHUNK_DATAVERSION.AccVal.py", single); -// CSharpWriter.writeAccVal("CK_STATECHUNK_DATAVERSION.AccVal.cs", single); single = organiseDefines("CK_BITMAPDATA_FLAGS.txt", "CK_BITMAPDATA_FLAGS"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_BITMAPDATA_FLAGS.json"), single); -// CppWriter.writeEnum("CK_BITMAPDATA_FLAGS.hpp", single); -// PythonWriter.writeEnum("CK_BITMAPDATA_FLAGS.py", single); -// CSharpWriter.writeEnum("CK_BITMAPDATA_FLAGS.cs", single); -// CppWriter.writeAccVal("CK_BITMAPDATA_FLAGS.AccVal.hpp", single, CommonHelper.CKParts.CK2); -// PythonWriter.writeAccVal("CK_BITMAPDATA_FLAGS.AccVal.py", single); -// CSharpWriter.writeAccVal("CK_BITMAPDATA_FLAGS.AccVal.cs", single); single = organiseDefines("CK_CAMERA_PROJECTION.txt", "CK_CAMERA_PROJECTION"); JsonWriter.writeEnum(CommonHelper.getOutputFilePath("CK_CAMERA_PROJECTION.json"), single); -// CppWriter.writeEnum("CK_CAMERA_PROJECTION.hpp", single); -// PythonWriter.writeEnum("CK_CAMERA_PROJECTION.py", single); -// CSharpWriter.writeEnum("CK_CAMERA_PROJECTION.cs", single); -// CppWriter.writeAccVal("CK_CAMERA_PROJECTION.AccVal.hpp", single, CommonHelper.CKParts.CK2); -// PythonWriter.writeAccVal("CK_CAMERA_PROJECTION.AccVal.py", single); -// CSharpWriter.writeAccVal("CK_CAMERA_PROJECTION.AccVal.cs", single); // print message. System.out.println("Done"); diff --git a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/PythonWriter.java b/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/PythonWriter.java deleted file mode 100644 index 9437fd3..0000000 --- a/Assets/CodeGen/EnumsMigration/EnumsAnalyzer/PythonWriter.java +++ /dev/null @@ -1,185 +0,0 @@ -import java.io.OutputStreamWriter; -import java.util.Locale; - -/** - * Write enum declarations and accessible value into Python format. - */ -public class PythonWriter { - - // =========== Python Enum Declaration Writer =========== - - /** - * Internal real enum declaration writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @throws Exception - */ - private static void internalWriteEnums(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog) - throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python); - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum start - indent.printf("class %s(enum.IntEnum):", benum.mEnumName); - indent.inc(); - - // write enum comment - indent.briefComment(benum.mEnumComment); - - // write enum entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - // write entry self - if (enumEntry.mEntryValue == null) { - indent.printf("%s = auto()", enumEntry.mEntryName); - } else { - indent.printf("%s = %s", enumEntry.mEntryName, - CommonHelper.convertToPythonNumber(enumEntry.mEntryValue)); - } - - // write entry comment after member - indent.afterMemberComment(enumEntry.mEntryComment); - } - - // enum tail - indent.dec(); - } - } - - /** - * Write an enum declaration collection into given file. - *

- * Actually this is a wrapper of internal enum declaration collection writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @throws Exception - */ - public static void writeEnums(String filename, EnumsHelper.BEnumCollection prog) throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, prog); - fs.close(); - } - - /** - * Write a single enum declaration into given file. - *

- * Actually this is a wrapper of internal enum declaration collection writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @throws Exception - */ - public static void writeEnum(String filename, EnumsHelper.BEnum _enum) throws Exception { - // create collection from single enum - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteEnums(fs, col); - fs.close(); - } - - // =========== Python Enum Accessible Value Writer =========== - - /** - * Try generate human readable name from enum entry name. - *

- * This function is only served for Python code generation. - *

- * As you noticed, almost entries of CK enums are fully capital and splitted by - * underline. This is really not good for human reading, especially those who - * are not programmer. So this function will try give these programmer-oriented - * entry name a human readable name as its display name. However, this extract - * method is not perfect. It simply do some split and replacement so the - * generated content may still not good for reader. - * - * @param entry_name The name of enum entry - * @return A human readable entry name. No guaranteen that return value is must - * human readable. - */ - private static String extractHumanReadableEntryName(String entry_name) { - // remove first part (any content before underline '_') - entry_name = entry_name.replaceFirst("^[a-zA-Z0-9]+_", ""); - - // lower all chars except first char - if (entry_name.length() < 1) - return entry_name; - else - return entry_name.substring(0, 1) + entry_name.substring(1).toLowerCase(Locale.ROOT); - } - - /** - * Internal real enum accessible value writer. - * - * @param writer {@linkplain java.io.OutputStreamWriter} instance for writing. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for writing. - * @throws Exception - */ - private static void internalWriteAccVals(OutputStreamWriter writer, EnumsHelper.BEnumCollection prog) - throws Exception { - IndentHelper indent = new IndentHelper(writer, CommonHelper.LangType.Python); - - // write implements - for (EnumsHelper.BEnum benum : prog.mEnums) { - // write enum desc header - indent.printf("g_Annotation_%s: dict[int, EnumAnnotation] = {", benum.mEnumName); - indent.inc(); - - // write enum desc entries - for (EnumsHelper.BEnumEntry enumEntry : benum.mEntries) { - String comment = ""; - if (enumEntry.mEntryComment != null) { - comment = CommonHelper.escapeString(enumEntry.mEntryComment); - } - - indent.printf("%s.%s.value: EnumAnnotation(\"%s\", \"%s\"),", benum.mEnumName, enumEntry.mEntryName, - extractHumanReadableEntryName(enumEntry.mEntryName), comment); - } - - // write enum tail - indent.dec(); - indent.puts("}"); - } - } - - /** - * Write an enum accessible value collection into given file. - *

- * Actually this is a wrapper of internal enum accessible value collection - * writer. - * - * @param filename The name of written file. - * @param prog {@linkplain EnumsHelper.BEnumCollection} instance for - * writing. - * @throws Exception - */ - public static void writeAccVals(String filename, EnumsHelper.BEnumCollection prog) throws Exception { - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, prog); - fs.close(); - } - - /** - * Write a single enum accessible value into given file. - *

- * Actually this is a wrapper of internal enum accessible value collection - * writer. - * - * @param filename The name of written file. - * @param _enum {@linkplain EnumsHelper.BEnum} instance for writing. - * @throws Exception - */ - public static void writeAccVal(String filename, EnumsHelper.BEnum _enum) throws Exception { - // create a collection with single enum. - EnumsHelper.BEnumCollection col = new EnumsHelper.BEnumCollection(); - col.mEnums.add(_enum); - // open file and write - OutputStreamWriter fs = CommonHelper.openOutputFile(filename); - internalWriteAccVals(fs, col); - fs.close(); - } - -} diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/template_render.py b/Assets/CodeGen/EnumsMigration/EnumsRender/template_render.py index 5bc8a92..7552ed0 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/template_render.py +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/template_render.py @@ -1,13 +1,99 @@ import jinja2 +import jinja2.filters +import re import typing from json_loader import BEnumCollection, BEnum from utils import CKParts import utils class RenderUtils: - pass + """Possible used functions for jinja when rendering templates""" + + TRANTABLE_ESCAPE_STRING: typing.ClassVar[dict[int, str]] = str.maketrans({ + "\\": "\\\\", + "\t": "\\t", + "\b": "\\b", + "\n": "\\n", + "\r": "\\r", + "\f": "\\f", + "\"": "\\\"", + }) + + @staticmethod + def escape_string(strl: str) -> str: + """ + Escape string + + Escape all characters which are invalid in string quote. + + :param strl: The string need to be escaped. + :return: The escaped string. + """ + return strl.translate(RenderUtils.TRANTABLE_ESCAPE_STRING) + + TRANTABLE_REMOVE_EOL: typing.ClassVar[dict[int, str]] = str.maketrans({ + "\n": "", + "\r": "", + }) + + @staticmethod + def remove_eol(strl: str) -> str: + """ + Remove EOL of given string. + + When rendering code, adding line comment is a common case. + However, comment may have EOL. + So when adding this to line comment, we need to remove all EOL. + """ + return strl.translate(RenderUtils.TRANTABLE_REMOVE_EOL) + + REGEX_PY_TO_LITERAL_NUMBER: typing.ClassVar[re.Pattern] = re.compile("[ulUL]+$") + + @staticmethod + def convert_to_python_number(numstr: str) -> str: + """ + Convert accepted string into Python cupported format. + + It actually just remove trail "UL". + + This function is only served for Python code generation. + + :param numstr: The captured number. + :return: The Python style number string. + """ + return RenderUtils.REGEX_PY_TO_LITERAL_NUMBER.sub("", numstr, 1) + + REGEX_PY_EXT_HUMANRDABLE_ENTRY_NAME: typing.ClassVar[re.Pattern] = re.compile("^[a-zA-Z0-9]+_") + + @staticmethod + def extract_human_readable_entry_name(entry_name: str) -> str: + """ + Try generate human readable name from enum entry name. + + This function is only served for Python code generation. + + As you noticed, almost entries of CK enums are fully capital and splitted by + underline. This is really not good for human reading, especially those who + are not programmer. So this function will try give these programmer-oriented + entry name a human readable name as its display name. However, this extract + method is not perfect. It simply do some split and replacement so the + generated content may still not good for reader. + + :param entry_name: The name of enum entry + :return: A human readable entry name. No guaranteen that return value is must human readable. + """ + # remove first part (any content before underline '_') + entry_name = RenderUtils.REGEX_PY_EXT_HUMANRDABLE_ENTRY_NAME.sub("", entry_name, 1) + + # lower all chars except first char + if len(entry_name) < 1: + return entry_name + else: + return entry_name[0:1] + entry_name[1:].lower() class TemplateRender: + """Render templates to code files""" + __loader: jinja2.BaseLoader __environment: jinja2.Environment @@ -15,6 +101,12 @@ class TemplateRender: self.__loader = jinja2.FileSystemLoader(utils.get_template_directory()) self.__environment = jinja2.Environment(loader=self.__loader) + # prepare filters + self.__environment.filters['some_or_blank'] = lambda s: "" if s is None else s + self.__environment.filters['escape_string'] = lambda s: RenderUtils.escape_string(s) + self.__environment.filters['block_comment'] = lambda s, fmt: jinja2.filters.do_indent(s, fmt, True, True) + self.__environment.filters['line_comment'] = lambda s: RenderUtils.remove_eol(s) + def __render(self, template_name: str, dest_filename: str, payload: BEnumCollection, extra: dict[str, typing.Any] = {}) -> None: # prepare template argument template_argument: dict[str, typing.Any] = { diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CKERROR.docstring.cpp.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CKERROR.docstring.cpp.jinja index 248ca1e..dbf27ea 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CKERROR.docstring.cpp.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CKERROR.docstring.cpp.jinja @@ -1,6 +1,6 @@ -{%- set benum = first(payload.iter_enums()) %} +{%- set benum = payload.iter_enums() | first %} const CkErrorReflectionArray CKERROR { {%- for entry in benum.iter_entries() %} - { LibCmo::CK2::CKERROR::{{ entry.get_entry_name() }}, { u8"{{ entry.get_entry_name() }}", u8"{{- entry.get_entry_comment() }}" } }, + { LibCmo::CK2::CKERROR::{{ entry.get_entry_name() }}, { u8"{{ entry.get_entry_name() }}", u8"{{- entry.get_entry_comment() | some_or_blank | escape_string }}" } }, {%- endfor %} }; diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CK_CLASSID.docstring.cpp.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CK_CLASSID.docstring.cpp.jinja index 12ce858..ce4e942 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CK_CLASSID.docstring.cpp.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/CK_CLASSID.docstring.cpp.jinja @@ -1,6 +1,6 @@ -{%- set benum = first(payload.iter_enums()) %} +{%- set benum = payload.iter_enums() | first %} const CkClassidReflectionArray CK_CLASSID { {%- for entry in benum.iter_entries() %} - { LibCmo::CK2::CK_CLASSID::{{ entry.get_entry_name() }}, { { {% for item in entry.iter_hierarchy(benum) %} u8"{{ item.get_enum_name() }}" {%- if not loop.last -%}, {%- endif %} {%- endfor %} } } }, + { LibCmo::CK2::CK_CLASSID::{{ entry.get_entry_name() }}, { { {%- for item in entry.iter_hierarchy(benum) %} u8"{{ item.get_entry_name() }}" {%- if not loop.last -%}, {%- endif %} {%- endfor %} } } }, {%- endfor %} }; diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.cs.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.cs.jinja index 375c769..35c4584 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.cs.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.cs.jinja @@ -1,13 +1,11 @@ {%- for benum in payload.iter_enums() %} {%- if benum.get_enum_comment() is not none %} -/** -{{ benum.get_enum_comment() }} - */ +{{ benum.get_enum_comment() | block_comment('/// ') }} {%- endif %} {% if benum.get_use_flags() %}[Flags]{%- endif %} public enum {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} uint {%- else -%} int {%- endif %} { {%- for entry in benum.iter_entries() %} - {{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /**< {{ entry.get_entry_comment() }} */ {%- endif %} + {{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /// {{ entry.get_entry_comment() | line_comment }} {%- endif %} {%- endfor %} }; {%- endfor %} diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.cs.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.cs.jinja index d81a437..39fd121 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.cs.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.cs.jinja @@ -1,5 +1,7 @@ -{%- for benum in payload.iter_enums() %} -public static readonly System.Collections.Generic.Dictionary<{{ benum.get_enum_name() }}, string> {{ benum.get_enum_name() }} = new System.Collections.Generic.Dictionary<{{ benum.get_enum_name() }}, string>() { +using System.Collections.Generic; + +{% for benum in payload.iter_enums() %} +public static readonly Dictionary<{{ benum.get_enum_name() }}, string> {{ benum.get_enum_name() }} = new Dictionary<{{ benum.get_enum_name() }}, string>() { {%- for entry in benum.iter_entries() %} { {{ benum.get_enum_name() }}.{{ entry.get_entry_name() }}, "{{ entry.get_entry_name() }}" }, {%- endfor %} diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.py.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.py.jinja index e69de29..3df65c4 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.py.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.docstring.py.jinja @@ -0,0 +1,16 @@ +from dataclasses import dataclass + +@dataclass(frozen=True) +class EnumDocstring(): + display_name: str + """The name of this enum entry.""" + description: str + """The description of this enum entry.""" + +{% for benum in payload.iter_enums() %} +DOCSTRING_{{ benum.get_enum_name() }}: dict[{{ benum.get_enum_name() }}, EnumDocstring] = { +{%- for entry in benum.iter_entries() %} + {{ benum.get_enum_name() }}.{{ entry.get_entry_name() }}: EnumDocstring("{{ utils.extract_human_readable_entry_name(entry.get_entry_name()) }}", "{{ entry.get_entry_comment() | some_or_blank | escape_string }}"), +{%- endfor %} +} +{%- endfor %} diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.hpp.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.hpp.jinja index c59f604..e28996f 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.hpp.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.hpp.jinja @@ -1,12 +1,12 @@ {%- for benum in payload.iter_enums() %} {%- if benum.get_enum_comment() is not none %} /** -{{ benum.get_enum_comment() }} +{{ benum.get_enum_comment() | block_comment(' * ') }} */ {%- endif %} enum class {{ benum.get_enum_name() }} : {% if benum.get_can_unsigned() -%} CKDWORD {%- else -%} CKINT {%- endif %} { {%- for entry in benum.iter_entries() %} - {{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /**< {{ entry.get_entry_comment() }} */ {%- endif %} + {{ entry.get_entry_name() }} {%- if entry.get_entry_value() is not none %} = {{ entry.get_entry_value() }} {%- endif %}, {%- if entry.get_entry_comment() is not none %} /**< {{ entry.get_entry_comment() | line_comment }} */ {%- endif %} {%- endfor %} }; {%- endfor %} diff --git a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.py.jinja b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.py.jinja index f464b01..ad640a3 100644 --- a/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.py.jinja +++ b/Assets/CodeGen/EnumsMigration/EnumsRender/templates/generic.py.jinja @@ -1,14 +1,14 @@ import enum {% for benum in payload.iter_enums() %} -{%- if benum.get_enum_comment() is not none %} -/** -{{ benum.get_enum_comment() }} - */ -{%- endif %} class {{ benum.get_enum_name() }}(enum.IntEnum): +{%- if benum.get_enum_comment() is not none %} +""" +{{ benum.get_enum_comment() | block_comment('') }} +""" +{%- endif %} {%- for entry in benum.iter_entries() %} - {{ entry.get_entry_name() }} = {% if entry.get_entry_value() is none -%} auto() {%- else -%} {{ entry.get_entry_value() }} {%- endif %} {%- if entry.get_entry_comment() is not none %} - """{{ entry.get_entry_comment() }}""" {%- endif %} + {{ entry.get_entry_name() }} = {% if entry.get_entry_value() is none -%} auto() {%- else -%} {{ utils.convert_to_python_number(entry.get_entry_value()) }} {%- endif %} {%- if entry.get_entry_comment() is not none %} + """{{ entry.get_entry_comment() | line_comment }}""" {%- endif %} {%- endfor %} {%- endfor %}