Autogen vector types (#594)

This commit is contained in:
PeachyPeach 2024-12-31 00:14:51 +01:00 committed by GitHub
parent bc27f126cc
commit 1c950ba57c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 238 additions and 164 deletions

View file

@ -3,6 +3,7 @@ import re
import math
from extract_functions import *
from common import *
from vec_types import *
rejects = ""
integer_types = ["u8", "u16", "u32", "u64", "s8", "s16", "s32", "s64", "int"]
@ -164,85 +165,12 @@ $[BINDS]
###########################################################
c_types = {
"Vec2f": {
"field_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
},
},
"Vec3f": {
"field_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
},
},
"Vec4f": {
"field_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
"w": "[3]",
},
},
"Vec3s": {
"field_type": "integer",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
},
},
"Vec4s": {
"field_type": "integer",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
"w": "[3]",
},
},
"Mat4": {
"field_type": "number",
"fields_mapping": {
"m00": "[0][0]",
"m01": "[0][1]",
"m02": "[0][2]",
"m03": "[0][3]",
"m10": "[1][0]",
"m11": "[1][1]",
"m12": "[1][2]",
"m13": "[1][3]",
"m20": "[2][0]",
"m21": "[2][1]",
"m22": "[2][2]",
"m23": "[2][3]",
"m30": "[3][0]",
"m31": "[3][1]",
"m32": "[3][2]",
"m33": "[3][3]",
},
},
"Color": {
"field_type": "integer",
"fields_mapping": {
"r": "[0]",
"g": "[1]",
"b": "[2]",
},
},
}
c_type_before = """
vec_type_before = """
%s $[IDENTIFIER];
smlua_get_%s($[IDENTIFIER], $[INDEX]);
"""
c_type_after = """
vec_type_after = """
smlua_push_%s($[IDENTIFIER], $[INDEX]);
"""
@ -817,20 +745,20 @@ def alter_type(t):
############################################################################
def build_types():
s = gen_comment_header("types")
for type_name, c_type in c_types.items():
def build_vec_types():
s = gen_comment_header("vec types")
for type_name, vec_type in VEC_TYPES.items():
# Get
s += "static void smlua_get_%s(%s dest, int index) {\n" % (type_name.lower(), type_name)
for lua_field, c_field in c_type["fields_mapping"].items():
s += " dest%s = smlua_get_%s_field(index, \"%s\");\n" % (c_field, c_type["field_type"], lua_field)
for lua_field, c_field in vec_type["fields_mapping"].items():
s += " dest%s = smlua_get_%s_field(index, \"%s\");\n" % (c_field, vec_type["field_lua_type"], lua_field)
s += "}\n\n"
# Push
s += "static void smlua_push_%s(%s src, int index) {\n" % (type_name.lower(), type_name)
for lua_field, c_field in c_type["fields_mapping"].items():
s += " smlua_push_%s_field(index, \"%s\", src%s);\n" % (c_type["field_type"], lua_field, c_field)
for lua_field, c_field in vec_type["fields_mapping"].items():
s += " smlua_push_%s_field(index, \"%s\", src%s);\n" % (vec_type["field_lua_type"], lua_field, c_field)
s += "}\n\n"
return s
@ -841,8 +769,8 @@ def build_param(param, i):
ptype = alter_type(param['type'])
pid = param['identifier']
if ptype in c_types:
return (c_type_before % (ptype, ptype.lower())).replace('$[IDENTIFIER]', str(pid)).replace('$[INDEX]', str(i))
if ptype in VEC_TYPES:
return (vec_type_before % (ptype, ptype.lower())).replace('$[IDENTIFIER]', str(pid)).replace('$[INDEX]', str(i))
elif ptype == 'bool':
return ' %s %s = smlua_to_boolean(L, %d);\n' % (ptype, pid, i)
elif ptype in integer_types:
@ -871,8 +799,8 @@ def build_param_after(param, i):
ptype = param['type']
pid = param['identifier']
if ptype in c_types:
return (c_type_after % (ptype.lower())).replace('$[IDENTIFIER]', str(pid)).replace('$[INDEX]', str(i))
if ptype in VEC_TYPES:
return (vec_type_after % (ptype.lower())).replace('$[IDENTIFIER]', str(pid)).replace('$[INDEX]', str(i))
else:
return ''
@ -1414,7 +1342,7 @@ def def_files(processed_files):
def main():
processed_files = process_files()
built_types = build_types()
built_vec_types = build_vec_types()
built_functions = build_functions(processed_files)
built_binds = build_binds(processed_files)
built_includes = build_includes()
@ -1422,7 +1350,7 @@ def main():
filename = get_path(out_filename)
gen = template \
.replace("$[TYPES]", built_types) \
.replace("$[TYPES]", built_vec_types) \
.replace("$[FUNCTIONS]", built_functions) \
.replace("$[BINDS]", built_binds) \
.replace("$[INCLUDES]", built_includes)

View file

@ -4,6 +4,7 @@ import sys
from extract_structs import *
from extract_object_fields import *
from common import *
from vec_types import *
in_files = [
"include/types.h",
@ -386,6 +387,32 @@ def output_fuzz_file():
############################################################################
def build_vec_types():
s = gen_comment_header("vec types")
for type_name, vec_type in VEC_TYPES.items():
s += '#define LUA_%s_FIELD_COUNT %d\n' % (type_name.upper(), len(vec_type['fields_mapping']))
s += 'static struct LuaObjectField s%sFields[LUA_%s_FIELD_COUNT] = {\n' % (type_name, type_name.upper())
field_c_type = vec_type['field_c_type']
for i, lua_field in enumerate(vec_type['fields_mapping'].keys()):
s += ' { "%s", LVT_%s, sizeof(%s) * %d, false, LOT_NONE },\n' % (lua_field, field_c_type.upper(), field_c_type, i)
s += '};\n\n'
s += 'struct LuaObjectTable sLuaObjectTable[LOT_MAX] = {\n'
s += ' [LOT_NONE] = { LOT_NONE, NULL, 0 },\n'
for type_name in VEC_TYPES.keys():
s += ' [LOT_%s] = { LOT_%s, s%sFields, LUA_%s_FIELD_COUNT },\n' % (type_name.upper(), type_name.upper(), type_name, type_name.upper())
s += ' [LOT_POINTER] = { LOT_POINTER, NULL, 0 },\n'
s += '};\n\n'
return s
############################################################################
sLuaObjectTable = []
sLotAutoGenList = []
@ -491,7 +518,9 @@ def build_structs(structs):
return s
def build_body(parsed):
built = build_structs(parsed)
built = build_vec_types()
built += gen_comment_header("autogen types")
built += build_structs(parsed)
obj_table_row_built, obj_table_count = table_to_string(sLuaObjectTable)
obj_table_built = 'struct LuaObjectTable sLuaObjectAutogenTable[LOT_AUTOGEN_MAX - LOT_AUTOGEN_MIN] = {\n'
@ -501,7 +530,19 @@ def build_body(parsed):
return built + obj_table_built
def build_lot_enum():
s = 'enum LuaObjectAutogenType {\n'
s = ''
s += 'enum LuaObjectType {\n'
s += ' LOT_NONE = 0,\n'
for type_name in VEC_TYPES.keys():
s += ' LOT_%s,\n' % (type_name.upper())
s += ' LOT_POINTER,\n'
s += ' LOT_MAX,\n'
s += '};\n\n'
s += 'enum LuaObjectAutogenType {\n'
s += ' LOT_AUTOGEN_MIN = 1000,\n'
global sLotAutoGenList

79
autogen/vec_types.py Normal file
View file

@ -0,0 +1,79 @@
VEC_TYPES = {
"Vec2f": {
"field_c_type": "f32",
"field_lua_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
},
},
"Vec3f": {
"field_c_type": "f32",
"field_lua_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
},
},
"Vec4f": {
"field_c_type": "f32",
"field_lua_type": "number",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
"w": "[3]",
},
},
"Vec3s": {
"field_c_type": "s16",
"field_lua_type": "integer",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
},
},
"Vec4s": {
"field_c_type": "s16",
"field_lua_type": "integer",
"fields_mapping": {
"x": "[0]",
"y": "[1]",
"z": "[2]",
"w": "[3]",
},
},
"Mat4": {
"field_c_type": "f32",
"field_lua_type": "number",
"fields_mapping": {
"m00": "[0][0]",
"m01": "[0][1]",
"m02": "[0][2]",
"m03": "[0][3]",
"m10": "[1][0]",
"m11": "[1][1]",
"m12": "[1][2]",
"m13": "[1][3]",
"m20": "[2][0]",
"m21": "[2][1]",
"m22": "[2][2]",
"m23": "[2][3]",
"m30": "[3][0]",
"m31": "[3][1]",
"m32": "[3][2]",
"m33": "[3][3]",
},
},
"Color": {
"field_c_type": "u8",
"field_lua_type": "integer",
"fields_mapping": {
"r": "[0]",
"g": "[1]",
"b": "[2]",
},
},
}

View file

@ -17,65 +17,7 @@
#include "pc/lua/utils/smlua_obj_utils.h"
#include "pc/mods/mods.h"
#define LUA_VEC3S_FIELD_COUNT 3
static struct LuaObjectField sVec3sFields[LUA_VEC3S_FIELD_COUNT] = {
{ "x", LVT_S16, sizeof(s16) * 0, false, LOT_NONE },
{ "y", LVT_S16, sizeof(s16) * 1, false, LOT_NONE },
{ "z", LVT_S16, sizeof(s16) * 2, false, LOT_NONE },
};
#define LUA_VEC3F_FIELD_COUNT 3
static struct LuaObjectField sVec3fFields[LUA_VEC3F_FIELD_COUNT] = {
{ "x", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "y", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "z", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
};
#define LUA_VEC4S_FIELD_COUNT 4
static struct LuaObjectField sVec4sFields[LUA_VEC4S_FIELD_COUNT] = {
{ "x", LVT_S16, sizeof(s16) * 0, false, LOT_NONE },
{ "y", LVT_S16, sizeof(s16) * 1, false, LOT_NONE },
{ "z", LVT_S16, sizeof(s16) * 2, false, LOT_NONE },
{ "w", LVT_S16, sizeof(s16) * 3, false, LOT_NONE },
};
#define LUA_VEC4F_FIELD_COUNT 4
static struct LuaObjectField sVec4fFields[LUA_VEC4F_FIELD_COUNT] = {
{ "x", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "y", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "z", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
{ "w", LVT_F32, sizeof(f32) * 3, false, LOT_NONE },
};
#define LUA_MAT4_FIELD_COUNT 16
static struct LuaObjectField sMat4Fields[LUA_MAT4_FIELD_COUNT] = {
{ "a", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "b", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "c", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
{ "d", LVT_F32, sizeof(f32) * 3, false, LOT_NONE },
{ "e", LVT_F32, sizeof(f32) * 4, false, LOT_NONE },
{ "f", LVT_F32, sizeof(f32) * 5, false, LOT_NONE },
{ "g", LVT_F32, sizeof(f32) * 6, false, LOT_NONE },
{ "h", LVT_F32, sizeof(f32) * 7, false, LOT_NONE },
{ "i", LVT_F32, sizeof(f32) * 8, false, LOT_NONE },
{ "j", LVT_F32, sizeof(f32) * 9, false, LOT_NONE },
{ "k", LVT_F32, sizeof(f32) * 10, false, LOT_NONE },
{ "l", LVT_F32, sizeof(f32) * 11, false, LOT_NONE },
{ "m", LVT_F32, sizeof(f32) * 12, false, LOT_NONE },
{ "n", LVT_F32, sizeof(f32) * 13, false, LOT_NONE },
{ "o", LVT_F32, sizeof(f32) * 14, false, LOT_NONE },
{ "p", LVT_F32, sizeof(f32) * 15, false, LOT_NONE },
};
struct LuaObjectTable sLuaObjectTable[LOT_MAX] = {
{ LOT_NONE, NULL, 0 },
{ LOT_VEC3S, sVec3sFields, LUA_VEC3S_FIELD_COUNT },
{ LOT_VEC3F, sVec3fFields, LUA_VEC3F_FIELD_COUNT },
{ LOT_VEC4S, sVec4sFields, LUA_VEC4S_FIELD_COUNT },
{ LOT_VEC4F, sVec4fFields, LUA_VEC4F_FIELD_COUNT },
{ LOT_MAT4, sMat4Fields, LUA_MAT4_FIELD_COUNT },
};
extern struct LuaObjectTable sLuaObjectTable[LOT_MAX];
struct LuaObjectField* smlua_get_object_field_from_ot(struct LuaObjectTable* ot, const char* key) {
// binary search

View file

@ -39,18 +39,6 @@ enum LuaValueType {
LVT_MAX,
};
enum LuaObjectType {
LOT_NONE = 0,
LOT_VEC3S,
LOT_VEC3F,
LOT_VEC4S,
LOT_VEC4F,
LOT_MAT4,
LOT_COLOR,
LOT_POINTER,
LOT_MAX,
};
struct LuaObjectField {
const char* key;
enum LuaValueType valueType;

View file

@ -29,6 +29,89 @@
#include "include/object_fields.h"
///////////////
// vec types //
///////////////
#define LUA_VEC2F_FIELD_COUNT 2
static struct LuaObjectField sVec2fFields[LUA_VEC2F_FIELD_COUNT] = {
{ "x", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "y", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
};
#define LUA_VEC3F_FIELD_COUNT 3
static struct LuaObjectField sVec3fFields[LUA_VEC3F_FIELD_COUNT] = {
{ "x", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "y", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "z", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
};
#define LUA_VEC4F_FIELD_COUNT 4
static struct LuaObjectField sVec4fFields[LUA_VEC4F_FIELD_COUNT] = {
{ "x", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "y", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "z", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
{ "w", LVT_F32, sizeof(f32) * 3, false, LOT_NONE },
};
#define LUA_VEC3S_FIELD_COUNT 3
static struct LuaObjectField sVec3sFields[LUA_VEC3S_FIELD_COUNT] = {
{ "x", LVT_S16, sizeof(s16) * 0, false, LOT_NONE },
{ "y", LVT_S16, sizeof(s16) * 1, false, LOT_NONE },
{ "z", LVT_S16, sizeof(s16) * 2, false, LOT_NONE },
};
#define LUA_VEC4S_FIELD_COUNT 4
static struct LuaObjectField sVec4sFields[LUA_VEC4S_FIELD_COUNT] = {
{ "x", LVT_S16, sizeof(s16) * 0, false, LOT_NONE },
{ "y", LVT_S16, sizeof(s16) * 1, false, LOT_NONE },
{ "z", LVT_S16, sizeof(s16) * 2, false, LOT_NONE },
{ "w", LVT_S16, sizeof(s16) * 3, false, LOT_NONE },
};
#define LUA_MAT4_FIELD_COUNT 16
static struct LuaObjectField sMat4Fields[LUA_MAT4_FIELD_COUNT] = {
{ "m00", LVT_F32, sizeof(f32) * 0, false, LOT_NONE },
{ "m01", LVT_F32, sizeof(f32) * 1, false, LOT_NONE },
{ "m02", LVT_F32, sizeof(f32) * 2, false, LOT_NONE },
{ "m03", LVT_F32, sizeof(f32) * 3, false, LOT_NONE },
{ "m10", LVT_F32, sizeof(f32) * 4, false, LOT_NONE },
{ "m11", LVT_F32, sizeof(f32) * 5, false, LOT_NONE },
{ "m12", LVT_F32, sizeof(f32) * 6, false, LOT_NONE },
{ "m13", LVT_F32, sizeof(f32) * 7, false, LOT_NONE },
{ "m20", LVT_F32, sizeof(f32) * 8, false, LOT_NONE },
{ "m21", LVT_F32, sizeof(f32) * 9, false, LOT_NONE },
{ "m22", LVT_F32, sizeof(f32) * 10, false, LOT_NONE },
{ "m23", LVT_F32, sizeof(f32) * 11, false, LOT_NONE },
{ "m30", LVT_F32, sizeof(f32) * 12, false, LOT_NONE },
{ "m31", LVT_F32, sizeof(f32) * 13, false, LOT_NONE },
{ "m32", LVT_F32, sizeof(f32) * 14, false, LOT_NONE },
{ "m33", LVT_F32, sizeof(f32) * 15, false, LOT_NONE },
};
#define LUA_COLOR_FIELD_COUNT 3
static struct LuaObjectField sColorFields[LUA_COLOR_FIELD_COUNT] = {
{ "r", LVT_U8, sizeof(u8) * 0, false, LOT_NONE },
{ "g", LVT_U8, sizeof(u8) * 1, false, LOT_NONE },
{ "b", LVT_U8, sizeof(u8) * 2, false, LOT_NONE },
};
struct LuaObjectTable sLuaObjectTable[LOT_MAX] = {
[LOT_NONE] = { LOT_NONE, NULL, 0 },
[LOT_VEC2F] = { LOT_VEC2F, sVec2fFields, LUA_VEC2F_FIELD_COUNT },
[LOT_VEC3F] = { LOT_VEC3F, sVec3fFields, LUA_VEC3F_FIELD_COUNT },
[LOT_VEC4F] = { LOT_VEC4F, sVec4fFields, LUA_VEC4F_FIELD_COUNT },
[LOT_VEC3S] = { LOT_VEC3S, sVec3sFields, LUA_VEC3S_FIELD_COUNT },
[LOT_VEC4S] = { LOT_VEC4S, sVec4sFields, LUA_VEC4S_FIELD_COUNT },
[LOT_MAT4] = { LOT_MAT4, sMat4Fields, LUA_MAT4_FIELD_COUNT },
[LOT_COLOR] = { LOT_COLOR, sColorFields, LUA_COLOR_FIELD_COUNT },
[LOT_POINTER] = { LOT_POINTER, NULL, 0 },
};
///////////////////
// autogen types //
///////////////////
#define LUA_ANIM_INFO_FIELD_COUNT 11
static struct LuaObjectField sAnimInfoFields[LUA_ANIM_INFO_FIELD_COUNT] = {
{ "animAccel", LVT_S32, offsetof(struct AnimInfo, animAccel), false, LOT_NONE },

View file

@ -3,6 +3,19 @@
#ifndef SMLUA_COBJECT_AUTOGEN_H
#define SMLUA_COBJECT_AUTOGEN_H
enum LuaObjectType {
LOT_NONE = 0,
LOT_VEC2F,
LOT_VEC3F,
LOT_VEC4F,
LOT_VEC3S,
LOT_VEC4S,
LOT_MAT4,
LOT_COLOR,
LOT_POINTER,
LOT_MAX,
};
enum LuaObjectAutogenType {
LOT_AUTOGEN_MIN = 1000,
LOT_ANIMINFO,

View file

@ -53,9 +53,9 @@
#include "src/audio/seqplayer.h"
///////////
// types //
///////////
///////////////
// vec types //
///////////////
static void smlua_get_vec2f(Vec2f dest, int index) {
dest[0] = smlua_get_number_field(index, "x");