public DreamProc(Func <DreamObject, DreamObject, DreamProcArguments, DreamValue> nativeProc) { _runAction = (DreamObject instance, DreamObject usr, DreamProcArguments arguments) => { if (_defaultArgumentValues != null) { foreach (KeyValuePair <string, DreamValue> defaultArgumentValue in _defaultArgumentValues) { int argumentIndex = ArgumentNames.IndexOf(defaultArgumentValue.Key); if (arguments.GetArgument(argumentIndex, defaultArgumentValue.Key) == DreamValue.Null) { arguments.NamedArguments.Add(defaultArgumentValue.Key, defaultArgumentValue.Value); } } } return(nativeProc(instance, usr, arguments)); }; ArgumentNames = new List <string>(); _defaultArgumentValues = null; List <Attribute> attributes = new(nativeProc.GetInvocationList()[0].Method.GetCustomAttributes()); List <Attribute> parameterAttributes = attributes.FindAll(attribute => attribute is DreamProcParameterAttribute); foreach (Attribute attribute in parameterAttributes) { DreamProcParameterAttribute parameterAttribute = (DreamProcParameterAttribute)attribute; ArgumentNames.Add(parameterAttribute.Name); if (parameterAttribute.DefaultValue != default) { if (_defaultArgumentValues == null) { _defaultArgumentValues = new Dictionary <string, DreamValue>(); } _defaultArgumentValues.Add(parameterAttribute.Name, new DreamValue(parameterAttribute.DefaultValue)); } } }
public DreamProc(Func <DreamObject, DreamObject, DreamProcArguments, DreamValue> nativeProc) { _runAction = (DreamObject instance, DreamObject usr, DreamProcArguments arguments) => { for (int i = 0; i < ArgumentNames.Count; i++) { string argumentName = ArgumentNames[i]; if (arguments.GetArgument(i, argumentName).Value == null) { if (_defaultArgumentValues != null && _defaultArgumentValues.TryGetValue(argumentName, out DreamValue defaultValue)) { arguments.NamedArguments.Add(argumentName, defaultValue); } } } return(nativeProc(instance, usr, arguments)); }; ArgumentNames = new List <string>(); _defaultArgumentValues = null; List <Attribute> attributes = new(nativeProc.GetInvocationList()[0].Method.GetCustomAttributes()); List <Attribute> parameterAttributes = attributes.FindAll(attribute => attribute is DreamProcParameterAttribute); foreach (Attribute attribute in parameterAttributes) { DreamProcParameterAttribute parameterAttribute = (DreamProcParameterAttribute)attribute; ArgumentNames.Add(parameterAttribute.Name); if (parameterAttribute.DefaultValue != default) { if (_defaultArgumentValues == null) { _defaultArgumentValues = new Dictionary <string, DreamValue>(); } _defaultArgumentValues.Add(parameterAttribute.Name, new DreamValue(parameterAttribute.DefaultValue)); } } }