|
18 | 18 | #include <LuaType/LuaUStruct.hpp> |
19 | 19 | #include <LuaType/LuaUWorld.hpp> |
20 | 20 | #include <LuaType/LuaUDataTable.hpp> |
| 21 | +#include <LuaType/LuaXDelegateProperty.hpp> |
21 | 22 | #include <LuaType/LuaXObjectProperty.hpp> |
22 | 23 | #include <LuaType/LuaXProperty.hpp> |
23 | 24 | #pragma warning(disable : 4005) |
|
33 | 34 | #include <Unreal/Property/FSoftClassProperty.hpp> |
34 | 35 | #include <Unreal/Property/FStructProperty.hpp> |
35 | 36 | #include <Unreal/Property/FWeakObjectProperty.hpp> |
| 37 | +#include <Unreal/Property/FDelegateProperty.hpp> |
| 38 | +#include <Unreal/Property/FMulticastDelegateProperty.hpp> |
| 39 | +#include <Unreal/Property/FMulticastSparseDelegateProperty.hpp> |
36 | 40 | #include <Unreal/Property/NumericPropertyTypes.hpp> |
37 | 41 | #include <Unreal/UClass.hpp> |
38 | 42 | #include <Unreal/UEnum.hpp> |
@@ -1822,6 +1826,220 @@ namespace RC::LuaType |
1822 | 1826 | } |
1823 | 1827 | } |
1824 | 1828 |
|
| 1829 | + auto push_delegateproperty(const PusherParams& params) -> void |
| 1830 | + { |
| 1831 | + using namespace Unreal; |
| 1832 | + auto* delegate_value = static_cast<FScriptDelegate*>(params.data); |
| 1833 | + if (!delegate_value) |
| 1834 | + { |
| 1835 | + params.throw_error("push_delegateproperty", "data pointer is nullptr"); |
| 1836 | + } |
| 1837 | + |
| 1838 | + switch (params.operation) |
| 1839 | + { |
| 1840 | + case Operation::GetNonTrivialLocal: |
| 1841 | + case Operation::Get: { |
| 1842 | + // Return a table with {Object = UObject, FunctionName = FName} |
| 1843 | + LuaMadeSimple::Lua::Table lua_table = params.lua.prepare_new_table(); |
| 1844 | + |
| 1845 | + lua_table.add_key("Object"); |
| 1846 | + auto_construct_object(params.lua, delegate_value->GetUObject()); |
| 1847 | + lua_table.fuse_pair(); |
| 1848 | + |
| 1849 | + lua_table.add_key("FunctionName"); |
| 1850 | + FName::construct(params.lua, delegate_value->GetFunctionName()); |
| 1851 | + lua_table.fuse_pair(); |
| 1852 | + |
| 1853 | + lua_table.make_local(); |
| 1854 | + break; |
| 1855 | + } |
| 1856 | + case Operation::Set: { |
| 1857 | + if (params.lua.is_table()) |
| 1858 | + { |
| 1859 | + lua_pushstring(params.lua.get_lua_state(), "Object"); |
| 1860 | + int adjusted_index = params.stored_at_index; |
| 1861 | + if (params.stored_at_index < 0) |
| 1862 | + { |
| 1863 | + adjusted_index = params.stored_at_index - 1; |
| 1864 | + } |
| 1865 | + lua_rawget(params.lua.get_lua_state(), adjusted_index); |
| 1866 | + |
| 1867 | + Unreal::UObject* obj = nullptr; |
| 1868 | + if (params.lua.is_userdata()) |
| 1869 | + { |
| 1870 | + const auto& lua_object = params.lua.get_userdata<LuaType::UObject>(); |
| 1871 | + obj = lua_object.get_remote_cpp_object(); |
| 1872 | + } |
| 1873 | + params.lua.discard_value(); |
| 1874 | + |
| 1875 | + lua_pushstring(params.lua.get_lua_state(), "FunctionName"); |
| 1876 | + lua_rawget(params.lua.get_lua_state(), adjusted_index); |
| 1877 | + |
| 1878 | + Unreal::FName fname = NAME_None; |
| 1879 | + if (params.lua.is_userdata()) |
| 1880 | + { |
| 1881 | + auto& lua_fname = params.lua.get_userdata<LuaType::FName>(); |
| 1882 | + fname = lua_fname.get_local_cpp_object(); |
| 1883 | + } |
| 1884 | + params.lua.discard_value(); |
| 1885 | + |
| 1886 | + // Bind the delegate - this modifies the existing FWeakObjectPtr which calls |
| 1887 | + // the engine's assignment operator that will allocate serial numbers |
| 1888 | + delegate_value->BindUFunction(obj, fname); |
| 1889 | + params.lua.discard_value(params.stored_at_index); |
| 1890 | + } |
| 1891 | + else if (params.lua.is_nil()) |
| 1892 | + { |
| 1893 | + delegate_value->Clear(); |
| 1894 | + params.lua.discard_value(params.stored_at_index); |
| 1895 | + } |
| 1896 | + else |
| 1897 | + { |
| 1898 | + params.throw_error("push_delegateproperty", "Value must be a table {Object = UObject, FunctionName = FName} or nil"); |
| 1899 | + } |
| 1900 | + break; |
| 1901 | + } |
| 1902 | + case Operation::GetParam: |
| 1903 | + RemoteUnrealParam::construct(params.lua, params.data, params.base, params.property); |
| 1904 | + break; |
| 1905 | + default: |
| 1906 | + params.throw_error("push_delegateproperty", "Operation not supported"); |
| 1907 | + break; |
| 1908 | + } |
| 1909 | + } |
| 1910 | + |
| 1911 | + auto push_multicastdelegateproperty(const PusherParams& params) -> void |
| 1912 | + { |
| 1913 | + using namespace Unreal; |
| 1914 | + |
| 1915 | + auto* delegate_property = static_cast<FMulticastDelegateProperty*>(params.property); |
| 1916 | + if (!delegate_property) |
| 1917 | + { |
| 1918 | + params.throw_error("push_multicastdelegateproperty", "property is not FMulticastDelegateProperty"); |
| 1919 | + } |
| 1920 | + |
| 1921 | + switch (params.operation) |
| 1922 | + { |
| 1923 | + case Operation::Get: |
| 1924 | + XMulticastDelegateProperty::construct(params); |
| 1925 | + break; |
| 1926 | + case Operation::GetNonTrivialLocal: { |
| 1927 | + const FMulticastScriptDelegate* delegate_value = delegate_property->GetMulticastDelegate(params.data); |
| 1928 | + if (!delegate_value) |
| 1929 | + { |
| 1930 | + params.lua.set_nil(); |
| 1931 | + break; |
| 1932 | + } |
| 1933 | + |
| 1934 | + LuaMadeSimple::Lua::Table lua_table = params.lua.prepare_new_table(); |
| 1935 | + |
| 1936 | + int32 count = delegate_value->Num(); |
| 1937 | + for (int32 i = 0; i < count; ++i) |
| 1938 | + { |
| 1939 | + lua_table.add_key(i + 1); // Lua is 1-indexed |
| 1940 | + |
| 1941 | + LuaMadeSimple::Lua::Table delegate_entry = params.lua.prepare_new_table(); |
| 1942 | + |
| 1943 | + delegate_entry.add_key("Object"); |
| 1944 | + auto_construct_object(params.lua, delegate_value->InvocationList[i].GetUObject()); |
| 1945 | + delegate_entry.fuse_pair(); |
| 1946 | + |
| 1947 | + delegate_entry.add_key("FunctionName"); |
| 1948 | + FName::construct(params.lua, delegate_value->InvocationList[i].GetFunctionName()); |
| 1949 | + delegate_entry.fuse_pair(); |
| 1950 | + |
| 1951 | + delegate_entry.make_local(); |
| 1952 | + lua_table.fuse_pair(); // Set array element |
| 1953 | + } |
| 1954 | + |
| 1955 | + lua_table.make_local(); |
| 1956 | + break; |
| 1957 | + } |
| 1958 | + case Operation::Set: |
| 1959 | + // Multicast delegates cannot be set directly. Use the property wrapper methods instead: |
| 1960 | + // local prop = obj.OnSomething |
| 1961 | + // prop:Add(targetObj, FName("FunctionName")) |
| 1962 | + // prop:Remove(targetObj, FName("FunctionName")) |
| 1963 | + // prop:Clear() |
| 1964 | + params.throw_error("push_multicastdelegateproperty", |
| 1965 | + "Multicast delegate values are read-only. Use property methods: GetPropertyByName():Add/Remove/Clear"); |
| 1966 | + break; |
| 1967 | + case Operation::GetParam: |
| 1968 | + RemoteUnrealParam::construct(params.lua, params.data, params.base, params.property); |
| 1969 | + break; |
| 1970 | + default: |
| 1971 | + params.throw_error("push_multicastdelegateproperty", "Operation not supported"); |
| 1972 | + break; |
| 1973 | + } |
| 1974 | + } |
| 1975 | + |
| 1976 | + auto push_multicastsparsedelegateproperty(const PusherParams& params) -> void |
| 1977 | + { |
| 1978 | + using namespace Unreal; |
| 1979 | + |
| 1980 | + // Sparse delegates work similarly to regular multicast delegates |
| 1981 | + // The main difference is they use 1 byte + global storage instead of inline InvocationList |
| 1982 | + auto* delegate_property = static_cast<FMulticastSparseDelegateProperty*>(params.property); |
| 1983 | + if (!delegate_property) |
| 1984 | + { |
| 1985 | + params.throw_error("push_multicastsparsedelegateproperty", "property is not FMulticastSparseDelegateProperty"); |
| 1986 | + } |
| 1987 | + |
| 1988 | + switch (params.operation) |
| 1989 | + { |
| 1990 | + case Operation::Get: |
| 1991 | + XMulticastSparseDelegateProperty::construct(params); |
| 1992 | + break; |
| 1993 | + case Operation::GetNonTrivialLocal: { |
| 1994 | + const FMulticastScriptDelegate* delegate_value = delegate_property->GetMulticastDelegate(params.data); |
| 1995 | + if (!delegate_value) |
| 1996 | + { |
| 1997 | + params.lua.set_nil(); |
| 1998 | + break; |
| 1999 | + } |
| 2000 | + |
| 2001 | + LuaMadeSimple::Lua::Table lua_table = params.lua.prepare_new_table(); |
| 2002 | + |
| 2003 | + int32 count = delegate_value->Num(); |
| 2004 | + for (int32 i = 0; i < count; ++i) |
| 2005 | + { |
| 2006 | + lua_table.add_key(i + 1); // Lua is 1-indexed |
| 2007 | + |
| 2008 | + LuaMadeSimple::Lua::Table delegate_entry = params.lua.prepare_new_table(); |
| 2009 | + |
| 2010 | + delegate_entry.add_key("Object"); |
| 2011 | + auto_construct_object(params.lua, delegate_value->InvocationList[i].GetUObject()); |
| 2012 | + delegate_entry.fuse_pair(); |
| 2013 | + |
| 2014 | + delegate_entry.add_key("FunctionName"); |
| 2015 | + FName::construct(params.lua, delegate_value->InvocationList[i].GetFunctionName()); |
| 2016 | + delegate_entry.fuse_pair(); |
| 2017 | + |
| 2018 | + delegate_entry.make_local(); |
| 2019 | + lua_table.fuse_pair(); |
| 2020 | + } |
| 2021 | + |
| 2022 | + lua_table.make_local(); |
| 2023 | + break; |
| 2024 | + } |
| 2025 | + case Operation::Set: |
| 2026 | + // Multicast delegates cannot be set directly. Use the property wrapper methods instead: |
| 2027 | + // local prop = obj.OnSomething |
| 2028 | + // prop:Add(targetObj, FName("FunctionName")) |
| 2029 | + // prop:Remove(targetObj, FName("FunctionName")) |
| 2030 | + // prop:Clear() |
| 2031 | + params.throw_error("push_multicastsparsedelegateproperty", |
| 2032 | + "Sparse multicast delegate values are read-only. Use property methods: GetPropertyByName():Add/Remove/Clear"); |
| 2033 | + break; |
| 2034 | + case Operation::GetParam: |
| 2035 | + RemoteUnrealParam::construct(params.lua, params.data, params.base, params.property); |
| 2036 | + break; |
| 2037 | + default: |
| 2038 | + params.throw_error("push_multicastsparsedelegateproperty", "Operation not supported"); |
| 2039 | + break; |
| 2040 | + } |
| 2041 | + } |
| 2042 | + |
1825 | 2043 | auto static is_a_internal(const LuaMadeSimple::Lua& lua, Unreal::UObject* object, Unreal::UClass* object_class) -> bool |
1826 | 2044 | { |
1827 | 2045 | if (!object || !object_class) |
|
0 commit comments