Ejemplo n.º 1
0
        private IEnumerable <(string group, string path)> LoadMetadata()
        {
            string[] metadataFiles = Directory.GetFiles(@".\Resources\", "Metadata.json", SearchOption.AllDirectories);
            List <(string, Resource)> resources = metadataFiles.Select(path => (Path.GetDirectoryName(path) ?? String.Empty, Resource.Load(path))).ToList();

            foreach ((string directoryPath, Resource resource) in resources)
            {
                if (resource.Group is null)
                {
                    continue;
                }

                foreach (Resource.BlockDefinition blockDefinition in resource.BlockDefinitions ?? Enumerable.Empty <Resource.BlockDefinition>())
                {
                    if (blockDefinition.Name is null)
                    {
                        continue;
                    }

                    IBlockDefinition.Attribute attributes = 0;

                    if (blockDefinition.Attributes is not null && !TryParseAttributes(blockDefinition.Attributes, out attributes))
                    {
                        continue;
                    }

                    ushort id = RegisterBlock(resource.Group, blockDefinition.Name, attributes, blockDefinition.MeshingStrategy);

                    if (resource.Group.Equals("Core"))
                    {
                        switch (blockDefinition.Name)
                        {
                        case "Null":
                            NullID = id;
                            break;

                        case "Air":
                            AirID = id;
                            break;
                        }
                    }

                    foreach (string textureName in blockDefinition.Textures ?? Enumerable.Empty <string>())
                    {
                        string fileName = $"{(textureName.Equals("Self") ? blockDefinition.Name : textureName)}.png";
                        yield return(resource.Group, Path.Combine(directoryPath, resource.RelativeTexturesPath ?? string.Empty, fileName));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public ushort RegisterBlock(string group, string blockName, IBlockDefinition.Attribute attributes, string?meshingStrategy)
        {
            const string group_with_block_name_format = "{0}:{1}";

            if (Blocks.Count >= ushort.MaxValue)
            {
                throw new OverflowException($"{nameof(BlockRegistry)} has run out of valid block IDs.");
            }

            ushort blockID     = (ushort)Blocks.Count;
            string groupedName = string.Format(group_with_block_name_format, group, blockName);

            int strategyIndex = ChunkMesher.MeshingStrategies.GetMeshingStrategyIndex(meshingStrategy ?? ChunkMesher.DEFAULT_STRATEGY);
            IBlockDefinition blockDefinition = new BlockDefinition(blockID, groupedName, strategyIndex, attributes);

            Blocks.Add(blockDefinition);
            BlocksIndexer.Add(groupedName, blockID);

            Log.Debug($"({nameof(BlockRegistry)}) Registered ID {blockID}: \"{groupedName}\"");

            return(blockDefinition.ID);
        }
Ejemplo n.º 3
0
        private bool TryParseAttributes(IEnumerable <string> attributes, [MaybeNullWhen(false)] out IBlockDefinition.Attribute result)
        {
            result = (IBlockDefinition.Attribute) 0;

            foreach (string attribute in attributes)
            {
                if (attribute.StartsWith("Alias"))
                {
                    string aliasName = attribute.Substring(attribute.IndexOf(' ') + 1);

                    if (_AttributeAliases.TryGetValue(aliasName, out IBlockDefinition.Attribute aliasAttribute))
                    {
                        result |= aliasAttribute;
                    }
                    else
                    {
                        Log.Error(string.Format(_LogFormat,
                                                $"Failed to parse {nameof(IBlockDefinition.Attribute)}: alias \"{aliasName}\" does not exist."));

                        return(false);
                    }
                }
                else if (Enum.TryParse(typeof(IBlockDefinition.Attribute), attribute, true, out object?parsed))
                {
                    result |= (IBlockDefinition.Attribute)parsed !;
                }
                else
                {
                    Log.Error(string.Format(_LogFormat,
                                            $"Failed to parse {nameof(IBlockDefinition.Attribute)}: attribute \"{attribute}\" does not exist."));

                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 4
0
 public bool CheckBlockHasProperty(ushort blockID, IBlockDefinition.Attribute attribute) => Blocks[blockID].HasAttribute(attribute);