2023-11-02 22:02:39 +08:00
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Vector;
|
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
public class PythonWriter {
|
|
|
|
|
|
|
|
private static final Map<String, String> g_CppTypeMap = cppTypeMapCreator();
|
|
|
|
|
|
|
|
private static Map<String, String> cppTypeMapCreator() {
|
|
|
|
Map<String, String> cache = new HashMap<String, String>();
|
|
|
|
cache.put("CKSTRING", "CKSTRING");
|
|
|
|
cache.put("CKDWORD", "CKDWORD");
|
2023-11-03 10:43:06 +08:00
|
|
|
cache.put("CKWORD", "CKWORD");
|
2023-11-02 22:02:39 +08:00
|
|
|
cache.put("CKINT", "CKINT");
|
|
|
|
cache.put("bool", "bool");
|
|
|
|
cache.put("CKFLOAT", "CKFLOAT");
|
|
|
|
cache.put("CKBYTE", "CKBYTE");
|
|
|
|
cache.put("CK_ID", "CKID");
|
2023-11-15 21:47:58 +08:00
|
|
|
cache.put("NakedOutputCallback", "callback");
|
2023-11-02 22:02:39 +08:00
|
|
|
cache.put("BMFile", "void");
|
|
|
|
cache.put("BMMeshTransition", "void");
|
2023-11-10 12:23:03 +08:00
|
|
|
cache.put("VxVector3", "VxVector3");
|
2023-11-02 22:02:39 +08:00
|
|
|
cache.put("VxVector2", "VxVector2");
|
|
|
|
cache.put("VxColor", "VxColor");
|
|
|
|
cache.put("VxMatrix", "VxMatrix");
|
|
|
|
cache.put("CK_TEXTURE_SAVEOPTIONS", "enum");
|
|
|
|
cache.put("VX_PIXELFORMAT", "enum");
|
|
|
|
cache.put("VXTEXTURE_BLENDMODE", "enum");
|
|
|
|
cache.put("VXTEXTURE_FILTERMODE", "enum");
|
|
|
|
cache.put("VXTEXTURE_ADDRESSMODE", "enum");
|
|
|
|
cache.put("VXBLEND_MODE", "enum");
|
|
|
|
cache.put("VXFILL_MODE", "enum");
|
|
|
|
cache.put("VXSHADE_MODE", "enum");
|
|
|
|
cache.put("VXCMPFUNC", "enum");
|
2023-11-15 21:47:58 +08:00
|
|
|
cache.put("VXMESH_LITMODE", "enum");
|
2023-11-02 22:02:39 +08:00
|
|
|
return Collections.unmodifiableMap(cache);
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
private static String pythonTypeGetter(ExpFctParamDecl paramdecl) throws IllegalArgumentException {
|
2023-11-02 22:02:39 +08:00
|
|
|
VariableType vt = paramdecl.mVarType;
|
|
|
|
if (!paramdecl.mIsInput) {
|
|
|
|
vt = vt.getPointerOfThis();
|
|
|
|
}
|
|
|
|
|
2024-04-23 16:01:26 +08:00
|
|
|
// create string builder for build final type string
|
2023-11-02 22:02:39 +08:00
|
|
|
StringBuilder sb = new StringBuilder();
|
2024-04-23 16:01:26 +08:00
|
|
|
// add type prefix
|
2023-11-02 22:02:39 +08:00
|
|
|
sb.append("bm_");
|
2024-04-23 16:01:26 +08:00
|
|
|
// try getting cpp type from base type
|
|
|
|
String cpp_type = g_CppTypeMap.get(vt.getBaseType());
|
|
|
|
if (cpp_type == null) {
|
|
|
|
throw new IllegalArgumentException("Unexpected type: " + vt.toCType());
|
|
|
|
}
|
|
|
|
// assign cpp type
|
|
|
|
sb.append(cpp_type);
|
|
|
|
// add pointer suffix
|
2023-11-02 22:02:39 +08:00
|
|
|
if (vt.isPointer()) {
|
|
|
|
sb.append("_");
|
|
|
|
sb.append(String.join("", Collections.nCopies(vt.getPointerLevel(), "p")));
|
|
|
|
}
|
2024-04-23 16:01:26 +08:00
|
|
|
// return built type string.
|
2023-11-02 22:02:39 +08:00
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void writePythonCode(Vector<ExpFctDecl> data) throws Exception {
|
|
|
|
OutputStreamWriter writer = CommonHelper.openWriter("dest/BMExports.py");
|
|
|
|
IndentHelper helper = new IndentHelper(writer);
|
|
|
|
|
|
|
|
// write snippet
|
|
|
|
CommonHelper.writeSnippet(writer, "snippets/header.py");
|
|
|
|
|
|
|
|
// write function decls
|
2024-04-23 16:01:26 +08:00
|
|
|
|
2023-11-03 10:43:06 +08:00
|
|
|
helper.puts("");
|
|
|
|
helper.puts("#region Function Defines");
|
|
|
|
helper.puts("");
|
2024-04-23 16:01:26 +08:00
|
|
|
|
2023-11-02 22:02:39 +08:00
|
|
|
for (ExpFctDecl fctdecl : data) {
|
|
|
|
// write annotation
|
|
|
|
// function name
|
|
|
|
helper.printf("## %s", fctdecl.mFctName);
|
|
|
|
// param
|
|
|
|
for (ExpFctParamDecl paramdecl : fctdecl.mFctParams) {
|
|
|
|
helper.printf("# @param %s[%s] Type: %s. %s%s", paramdecl.mVarName,
|
|
|
|
CommonHelper.getDoxygenInOutStr(paramdecl.mIsInput), paramdecl.mVarType.toCType(),
|
|
|
|
(paramdecl.mIsInput ? "" : "Use ctypes.byref(data) pass it. "), paramdecl.mVarDesc);
|
|
|
|
}
|
|
|
|
// return val
|
|
|
|
helper.puts("# @return True if no error, otherwise False.");
|
|
|
|
|
|
|
|
// write real declaration
|
|
|
|
// first, we need join all param
|
|
|
|
helper.printf("%s = _create_bmap_func('%s', [%s])", fctdecl.mFctName, fctdecl.mFctName, fctdecl.mFctParams
|
|
|
|
.stream().map(value -> pythonTypeGetter(value)).collect(Collectors.joining(", ")));
|
|
|
|
}
|
|
|
|
|
2023-11-03 10:43:06 +08:00
|
|
|
helper.puts("");
|
|
|
|
helper.puts("#endregion");
|
|
|
|
helper.puts("");
|
2024-04-23 16:01:26 +08:00
|
|
|
|
2023-11-02 22:02:39 +08:00
|
|
|
writer.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|