private static void AcceptEntity(IDictionary <string, object> dictionary, IRegistry registry) { var entityId = ParseId(GetValue(dictionary, "Id")); var entityName = GetValue <string>(dictionary, "Name"); var entity = registry.CreateEntity(entityId, entityName); ParseRegistryObjectBase(entity, dictionary); entity.Enabled = ParseBoolean(GetValue(dictionary, "Enabled")); entity.Layer = ParseInt(GetValue(dictionary, "Layer")); IList <object> componentList; if (TryGetValue(dictionary, "Components", out componentList)) { foreach (IDictionary <string, object> componentDictionary in componentList) { var componentType = ParseTypeReference(GetValue(componentDictionary, "Type"), false); var component = entity.AddComponent(componentType); component.Refresh(null, true); ParseUTinyObject(registry, component, componentDictionary); component.Refresh(null, true); } } }
/// <summary> /// WIP attempting toe make a 'fast' path for entitiy deserialization... its currently the same speed or slow than the generic /// Makes assumptions about the structure of the data /// </summary> /// <param name="reader"></param> /// <param name="registry"></param> public void ReadEntity(BinaryReader reader, IRegistry registry) { var token = reader.ReadByte(); Assert.AreEqual(token, BinaryToken.BeginObject); reader.ReadUInt32(); // Read TypeId ReadPropertyName(reader); ReadPropertyValue(reader); // Read Id ReadPropertyName(reader); var id = ParseId(ReadPropertyValue(reader)); // Read Name ReadPropertyName(reader); var name = ReadPropertyValue(reader) as string; var entity = registry.CreateEntity(id, name); // Read Components ReadPropertyName(reader); token = reader.ReadByte(); Assert.AreEqual(BinaryToken.BeginArray, token); // Read object size // NOTE: This is NOT the length, this is the bytesize from the BeginArray token to the EndArray token reader.ReadUInt32(); while ((token = reader.ReadByte()) != BinaryToken.EndArray) { Assert.AreEqual(BinaryToken.BeginObject, token); reader.ReadUInt32(); // Size ReadPropertyName(reader); // "Type" var type = ReadTypeReferenceValue(reader); var component = entity.AddComponent(type); component.Refresh(); // Read Name ReadPropertyName(reader); ReadPropertyValue(reader); ReadPropertyName(reader); // "Properties" token = reader.ReadByte(); Assert.AreEqual(BinaryToken.BeginObject, token); reader.ReadUInt32(); // Size while ((token = reader.ReadByte()) != BinaryToken.EndObject) { reader.BaseStream.Position -= 1; ReadUTinyObjectProperty(reader, component); } token = reader.ReadByte(); // EndObject Assert.AreEqual(BinaryToken.EndObject, token); } }
/// <summary> /// Sets up or migrates the initial state of the project /// * Includes required modules /// * Perfrorms any migration /// </summary> /// <param name="registry"></param> /// <param name="project"></param> private static void SetupProject(IRegistry registry, UTinyProject project) { var module = project.Module.Dereference(registry); // Make sure there's a dependency on the core modules AddRequiredModuleDependencies(registry, module); if (project.Configuration.Equals(UTinyEntity.Reference.None)) { var configurationEntity = registry.CreateEntity(UTinyId.New(), "Configuration"); project.Configuration = (UTinyEntity.Reference)configurationEntity; } }
public void SetUp() { m_Registry = new UTinyRegistry(); // Create a component with a single int field m_ComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_ComponentType.CreateField( "TestIntField", (UTinyType.Reference)UTinyType.Int32); // Default the TestStruct.IntField to 7 m_ComponentType.DefaultValue = new UTinyObject(m_Registry, (UTinyType.Reference)m_ComponentType) { ["TestIntField"] = KTestFieldDefaultValue }; // Create an entity with our test component m_DefaultEntity = m_Registry.CreateEntity(UTinyId.New(), "DefaultEntity"); { var c = m_DefaultEntity.AddComponent((UTinyType.Reference)m_ComponentType); c.Refresh(); } // Create another entity with our test component m_OverridenEntity = m_Registry.CreateEntity(UTinyId.New(), "OverridenEntity"); { var c = m_OverridenEntity.AddComponent((UTinyType.Reference)m_ComponentType); c.Refresh(); c["TestIntField"] = KTestFieldOverrideValue; } }
/// <summary> /// Sets up or migrates the initial state of the project /// * Includes required modules /// * Perfrorms any migration /// </summary> /// <param name="registry"></param> /// <param name="project"></param> private static void SetupProject(IRegistry registry, UTinyProject project) { var module = project.Module.Dereference(registry); // Make sure there's a dependency on the core modules module.AddExplicitModuleDependency((UTinyModule.Reference)registry.FindByName <UTinyModule>("UTiny.Core")); module.AddExplicitModuleDependency((UTinyModule.Reference)registry.FindByName <UTinyModule>("UTiny.Math")); module.AddExplicitModuleDependency((UTinyModule.Reference)registry.FindByName <UTinyModule>("UTiny.Core2D")); if (project.Configuration.Equals(UTinyEntity.Reference.None)) { var configurationEntity = registry.CreateEntity(UTinyId.New(), "Configuration"); project.Configuration = (UTinyEntity.Reference)configurationEntity; } }
public void SetUp() { m_Registry = new UTinyRegistry(); m_EnumType = m_Registry.CreateType( UTinyId.New(), "TestEnum", UTinyTypeCode.Enum); m_EnumType.BaseType = (UTinyType.Reference)UTinyType.Int32; m_EnumType.CreateField("A", (UTinyType.Reference)UTinyType.Int32); m_EnumType.CreateField("B", (UTinyType.Reference)UTinyType.Int32); m_EnumType.CreateField("C", (UTinyType.Reference)UTinyType.Int32); m_EnumType.DefaultValue = new UTinyObject(m_Registry, (UTinyType.Reference)m_EnumType) { // @NOTE We are intentionally starting at 1 to detect 0 case as errors ["A"] = 1, ["B"] = 2, ["C"] = 3 }; // Create a component with a single int field m_ComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_ComponentType.CreateField( "TestEnumField", (UTinyType.Reference)m_EnumType); m_Entity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity"); var component = m_Entity.AddComponent((UTinyType.Reference)m_ComponentType); component.Refresh(); }
public void SetUp() { m_Registry = new UTinyRegistry(); // Create a component with a single Texture2D field m_ComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_ComponentType.CreateField( "TestTexture2DField", (UTinyType.Reference)UTinyType.Texture2DEntity); m_Entity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity"); var component = m_Entity.AddComponent((UTinyType.Reference)m_ComponentType); component.Refresh(); // Create some asset on disc File.WriteAllBytes(Application.dataPath + "/TestTexture.png", new Texture2D(32, 32).EncodeToPNG()); AssetDatabase.ImportAsset("Assets/TestTexture.png"); m_Texture2D = AssetDatabase.LoadAssetAtPath <Texture2D>("Assets/TestTexture.png"); }
private static void CreateEntityForAsset(IRegistry registry, UTinyProject project, UTinyEntityGroup entityGroup, UTinyAssetInfo asset) { var @object = asset.Object; UTinyEntity entity = null; if (@object is Texture2D) { var texture = @object as Texture2D; var path = AssetDatabase.GetAssetPath(texture); var importer = (TextureImporter)AssetImporter.GetAtPath(path); entity = registry.CreateEntity(UTinyId.New(), $"{GetAssetEntityPath(typeof(Texture2D))}{asset.Name}"); var image2d = entity.AddComponent(registry.GetImage2DType()); image2d.Refresh(); image2d["imageFile"] = $"ut-asset:{asset.Name}"; var settings = UTinyUtility.GetAssetExportSettings(project, @object) as UTinyTextureSettings; if (settings != null && settings.FormatType == TextureFormatType.JPG && UTinyAssetExporter.TextureExporter.ReallyHasAlpha(texture)) { image2d["maskFile"] = $"ut-asset:{asset.Name}_a"; } image2d["disableSmoothing"] = importer.filterMode == FilterMode.Point; var sprite = AssetDatabase.LoadAllAssetsAtPath(path).OfType <Sprite>().FirstOrDefault(); // @NOTE The `importer.spritePixelsPerUnit` is currently NOT used in the editor... // We ALWAYS draw sprites at 1 pixel to world unit in the editor. // When we switch to using SpriteRenderer as our editor drawer we can just pass `sprite.pixelsPerUnit` directly here. var pixelsToWorldUnits = sprite ? sprite.pixelsPerUnit : 1; image2d["pixelsToWorldUnits"] = 1.0f / pixelsToWorldUnits; } else if (@object is Sprite) { var sprite = (Sprite)@object; entity = registry.CreateEntity(UTinyId.New(), $"{GetAssetEntityPath(typeof(Sprite))}{asset.Name}"); var sprite2d = entity.AddComponent(registry.GetSprite2DType()); sprite2d.Refresh(); sprite2d["image"] = sprite.texture; var region = sprite2d["imageRegion"] as UTinyObject; if (null != region) { region["x"] = sprite.rect.x / sprite.texture.width; region["y"] = sprite.rect.y / sprite.texture.height; region["width"] = sprite.rect.width / sprite.texture.width; region["height"] = sprite.rect.height / sprite.texture.height; } var pivot = sprite2d["pivot"] as UTinyObject; if (null != pivot) { pivot["x"] = sprite.pivot.x / sprite.rect.width; pivot["y"] = sprite.pivot.y / sprite.rect.height; } } else if (@object is AudioClip) { entity = registry.CreateEntity(UTinyId.New(), $"{GetAssetEntityPath(typeof(AudioClip))}{asset.Name}"); var audioClip = entity.AddComponent(registry.GetAudioClipType()); audioClip.Refresh(); audioClip["file"] = $"ut-asset:{asset.Name}"; } else if (@object is Font) { entity = registry.CreateEntity(UTinyId.New(), $"{GetAssetEntityPath(typeof(Font))}{asset.Name}"); var fontAsset = entity.AddComponent(registry.GetFontType()); fontAsset.Refresh(); fontAsset["file"] = $"ut-asset:{asset.Name}"; } if (null != entity) { entityGroup.AddEntityReference((UTinyEntity.Reference)entity); } foreach (var child in asset.Children) { CreateEntityForAsset(registry, project, entityGroup, child); } }
public void SetUp() { m_Registry = new UTinyRegistry(); // Create a component with an int array field m_IntArrayComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent", UTinyTypeCode.Component); m_IntArrayComponentType.CreateField( "TestIntArrayField", (UTinyType.Reference)UTinyType.Int32, true); m_IntArrayEntity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity"); var component = m_IntArrayEntity.AddComponent((UTinyType.Reference)m_IntArrayComponentType); component.Refresh(); component["TestIntArrayField"] = new UTinyList(m_Registry, (UTinyType.Reference)UTinyType.Int32) { 3, 6, 9 }; m_StructType = m_Registry.CreateType( UTinyId.New(), "TestStruct", UTinyTypeCode.Struct); m_StructType.CreateField( "TestIntField", (UTinyType.Reference)UTinyType.Int32); m_StructArrayComponentType = m_Registry.CreateType( UTinyId.New(), "TestComponent2", UTinyTypeCode.Component); m_StructArrayComponentType.CreateField( "TestStructArrayField", (UTinyType.Reference)m_StructType, true); m_StructArrayEntity = m_Registry.CreateEntity(UTinyId.New(), "TestEntity2"); var component2 = m_StructArrayEntity.AddComponent((UTinyType.Reference)m_StructArrayComponentType); component2.Refresh(); component2["TestStructArrayField"] = new UTinyList(m_Registry, (UTinyType.Reference)m_StructType) { new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType) { ["TestIntField"] = 3 }, new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType) { ["TestIntField"] = 6 }, new UTinyObject(m_Registry, (UTinyType.Reference)m_StructType) { ["TestIntField"] = 9 } }; }