private void HandleComponentAdded(UTinyEntity entity, UTinyObject component) { component.Refresh(); var type = component.Type.Dereference(Registry); if (type.HasAttribute <BindingsAttribute>()) { var bindings = type.GetAttribute <BindingsAttribute>().Binding; // Invoke bindings to register and add `unity` components bindings.Run(BindingTiming.OnAddBindings, entity, component); bindings.Run(BindingTiming.OnUpdateBindings, entity, component); // Invoke callback to perform first time setup hook bindings.Run(BindingTiming.OnAddComponent, entity, component); } }
public void Run(BindingTiming timing, UTinyEntity entity, UTinyObject component) { if (!ValidateBindingsParams(entity, component) || !MatchesRequiredComponentTypes(entity)) { return; } component.Refresh(); switch (timing) { case BindingTiming.OnAddBindings: OnAddBinding(entity, component); break; case BindingTiming.OnUpdateBindings: OnUpdateBinding(entity, component); break; case BindingTiming.OnRemoveBindings: OnRemoveBinding(entity, component); break; case BindingTiming.OnAddComponent: OnAddComponent(entity, component); break; case BindingTiming.OnRemoveComponent: OnRemoveComponent(entity, component); break; default: break; } }
public static object MigrateFieldValue(IRegistry registry, IVersionStorage versionStorage, object value, UTinyType type, bool array, object defaultValue = null, bool skipTypeCheck = false) { if (array) { var list = value as UTinyList; if (null == list) { list = new UTinyList(registry, versionStorage) { Type = (UTinyType.Reference)type }; list.Refresh(defaultValue as UTinyList, skipTypeCheck); } else { list.Type = (UTinyType.Reference)type; list.Refresh(defaultValue as UTinyList, skipTypeCheck); } return(list); } switch (type.TypeCode) { case UTinyTypeCode.Unknown: break; case UTinyTypeCode.Int8: return(TryChangeType <sbyte>(value)); case UTinyTypeCode.Int16: return(TryChangeType <short>(value)); case UTinyTypeCode.Int32: return(TryChangeType <int>(value)); case UTinyTypeCode.Int64: return(TryChangeType <long>(value)); case UTinyTypeCode.UInt8: return(TryChangeType <byte>(value)); case UTinyTypeCode.UInt16: return(TryChangeType <ushort>(value)); case UTinyTypeCode.UInt32: return(TryChangeType <uint>(value)); case UTinyTypeCode.UInt64: return(TryChangeType <ulong>(value)); case UTinyTypeCode.Float32: return(TryChangeType <float>(value)); case UTinyTypeCode.Float64: return(TryChangeType <double>(value)); case UTinyTypeCode.Boolean: return(TryChangeType <bool>(value)); case UTinyTypeCode.Char: return(TryChangeType <char>(value)); case UTinyTypeCode.String: return(TryChangeType <string>(value) ?? string.Empty); case UTinyTypeCode.Configuration: case UTinyTypeCode.Component: // Components can not be fields, they can only exist at the entity level throw new NotSupportedException(); case UTinyTypeCode.Struct: { var obj = value as UTinyObject; if (null == obj) { obj = new UTinyObject(registry, (UTinyType.Reference)type, versionStorage, false); obj.Refresh(defaultValue as UTinyObject, skipTypeCheck); } else { obj.Type = (UTinyType.Reference)type; obj.Refresh(defaultValue as UTinyObject, skipTypeCheck); } return(obj); } case UTinyTypeCode.Enum: { if (value is UTinyEnum.Reference) { return(new UTinyEnum.Reference(type, ((UTinyEnum.Reference)value).Id)); } return(defaultValue is UTinyEnum.Reference ? new UTinyEnum.Reference(type, ((UTinyEnum.Reference)defaultValue).Id) : new UTinyEnum.Reference(type, type.Fields.First().Id)); } case UTinyTypeCode.EntityReference: { if (value is UTinyEntity.Reference) { return(value); } return(defaultValue is UTinyEntity.Reference ? defaultValue : UTinyEntity.Reference.None); } case UTinyTypeCode.UnityObject: { if (value is Object) { return(value); } return(defaultValue is Object ? defaultValue : null); } default: throw new ArgumentOutOfRangeException(); } return(null); }