예제 #1
0
        def visitor(cls: GDScriptClass, depth: int):
            class_context = self.class_contexts[cls.type_id]
            writer.write(f"""\
                {{
                    //printf("  Register class: {cls.name}\\n");
                    godot_instance_create_func create = {{ NULL, NULL, NULL }};
                    create.create_func = {class_context.ctor_identifier};
                    godot_instance_destroy_func destroy = {{ NULL, NULL, NULL }};
                    destroy.destroy_func = {class_context.dtor_identifier};
                    nativescript10->godot_nativescript_register_class(p_handle, "{cls.name}", "{cls.built_in_type}", create, destroy);
                }}
            """)

            writer.write(f"""\
                {{
                    //printf("  Register method: __gd2c_is_class_instanceof\\n");
                    godot_instance_method method = {{ NULL, NULL, NULL }};
                    method.method = &__gd2c_is_class_instanceof;
                    godot_method_attributes attributes = {{ GODOT_METHOD_RPC_MODE_DISABLED }};
                    nativescript10->godot_nativescript_register_method(p_handle, "{cls.name}", "__gd2c_is_class_instanceof", attributes, method);
                }}
            """)

            for entry in class_context.vtable_entries:
                writer.write(f"""\
                    {{
                        //printf("  Register method: {entry.func_context.function_identifier}\\n");
                        godot_instance_method method = {{ NULL, NULL, NULL }};
                        method.method = &{entry.func_context.function_identifier};
                        godot_method_attributes attributes = {{ GODOT_METHOD_RPC_MODE_DISABLED }};
                        nativescript10->godot_nativescript_register_method(p_handle, "{cls.name}", "{entry.func_context.func.name}", attributes, method);
                    }}
                """)

            for signal in cls.signals():
                writer.write(f"""\
                    {{
                        //printf("  Register signal: {signal}\\n");
                        godot_string name = api10->godot_string_chars_to_utf8("{signal}");
                        godot_signal signal = {{ name, 0, NULL, 0, NULL }};
                        nativescript10->godot_nativescript_register_signal(p_handle, "{signal}", &signal);
                    }}
                """)

            for member_context in class_context.member_contexts.values():
                writer.write(f"""\
                    {{
                        //printf("  Register member: {member_context.member_identifier}\\n");
                        godot_property_set_func setter = {{ NULL, NULL, NULL }};
                        setter.set_func = &{member_context.setter_identifier};
                        godot_property_get_func getter = {{ NULL, NULL, NULL }};
                        getter.get_func = &{member_context.getter_identifier};
                        godot_property_attributes attributes = {{ GODOT_METHOD_RPC_MODE_DISABLED }};
                        nativescript10->godot_nativescript_register_property(p_handle, "{class_context.cls.name}", "{member_context.path}", &attributes, setter, getter);
                    }}
                """)

            writer.write(f"""\
                {class_context.vtable_init_function_identifier}();
                """)
예제 #2
0
 def set_inherited_flag(cls: GDScriptClass, depth: int):
     if cls.base:
         for member in cls.members():
             member.is_inherited = cls.base.has_member(member.name)
예제 #3
0
 def setUp(self):
     self.class0 = GDScriptClass("res://class0", "class0", 0)
     self.class1 = GDScriptClass("res://class1", "class1", 1)
예제 #4
0
    def _build_class(self, physical_path: Path, data) -> GDScriptClass:
        cls = GDScriptClass(
            self._project.to_resource_path(str(physical_path)),
            data.get("name", None) or self._project.generate_unique_class_name(
                to_camel_case(physical_path.with_suffix('').stem)),
            self._project.generate_unique_class_type_id())
        cls.base_resource_path = data["base_type"]
        cls.built_in_type = data["type"]

        for index, entry in enumerate(data["global_constants"]):
            glob = GDScriptGlobal(index, entry["name"], entry["original_name"],
                                  entry["type_code"], entry["kind_code"],
                                  entry["value"], entry["source"])
            cls.globals[glob.index] = glob

        for signal in data["signals"]:
            cls.add_signal(signal)

        for entry in data["members"]:
            member = GDScriptMember(entry["name"], int(entry["index"]),
                                    entry["type"])
            cls.add_member(member)

        for index, entry in enumerate(data["constants"]):
            cconst = GDScriptClassConstant(entry["name"], int(entry["type"]),
                                           bytes(list(entry["data"])),
                                           entry["declaration"])
            cls.add_constant(cconst)

        for index, entry in enumerate(data["methods"]):
            func = GDScriptFunction(entry["name"],
                                    GDScriptFunction.TYPE_METHOD)
            func.stack_size = int(entry["stack_size"])
            func.default_arguments_jump_table = list(
                map(lambda x: int(x), entry["default_arguments"]))
            func.return_vtype = VariantType.get(
                int(entry["return_type"]["type"]))
            func.global_names = entry["global_names"]

            num_parameters = len(entry["parameters"])
            len_jump_table = len(func.default_arguments_jump_table)
            for pindex, pentry in enumerate(entry["parameters"]):
                param = GDScriptFunctionParameter(
                    pentry["name"], VariantType.get(pentry["type"]), pindex)
                param.is_optional = pindex >= num_parameters - len_jump_table
                func.add_parameter(param)

            for centry in entry["constants"]:
                mconst = GDScriptFunctionConstant(
                    int(centry["index"]), centry["type"],
                    bytes(list(map(lambda x: int(x), centry["data"]))),
                    centry["declaration"])
                func.add_constant(mconst)

            ip = 0
            while ip < len(entry["bytecode"]):
                op = extract(func, entry["bytecode"], ip)
                func.add_op(ip, op)
                ip += op.stride

            cls.add_function(func)

        return cls