Example #1
0
        public static BlockModel Parse(string source, string path)
        {
            BlockModelParent m;

            try
            {
                m = JsonConvert.DeserializeObject <BlockModelParent>(source);
            }
            catch (Exception e)
            {
                Logger.Error($"Error loading block model \"{path}\"");
                Logger.Exception(e);
                throw;
            }

            var sources     = new List <string>();
            var currentPath = path;

            while (!string.IsNullOrEmpty(m.Parent))
            {
                if (sources.Count > 100)
                {
                    throw new Exception($"\"{m.Parent}\" has either more than 100 parents or is an endless loop!");
                }



                var filename = GetRelativePaths(currentPath, m.Parent, JsonExtension).FirstOrDefault(ResourceReader.Exists);

                if (filename == null)
                {
                    Logger.Error($"{m.Parent} could not be found in {currentPath}!");
                    m.Parent = null;
                    continue;
                }

                currentPath = filename;

                //Find first existing source, add it to the sources and find its parent
                var parentSource = ResourceReader.ReadString(filename);
                try
                {
                    m = JsonConvert.DeserializeObject <BlockModelParent>(parentSource);
                }
                catch (Exception e)
                {
                    Logger.Error($"Error loading block model \"{filename}\"");
                    Logger.Exception(e);
                    throw;
                }
                sources.Add(parentSource);
            }

            sources.Reverse();
            sources.Add(source);
            var model = new BlockModel();

            try
            {
                sources.ForEach(s => JsonConvert.PopulateObject(s, model));
            }
            catch (Exception e)
            {
                Logger.Error($"Error populating block model \"{path}\"");
                Logger.Exception(e);
                throw;
            }

            //Dont load textures if there arent any
            if (model.Textures == null)
            {
                Logger.Error($"\"{path}\" does not contain any texture definitions!");
            }
            else
            {
                LoadModelTextures(model, path);
            }

            return(model);
        }
Example #2
0
        private static void LoadModelTextures(BlockModel model, string path)
        {
            var loadedTextures  = new Dictionary <string, BlockTexture>();
            var variableFound   = true;
            var loadedSomething = true;
            var counter         = 0;

            while (variableFound && loadedSomething)
            {
                variableFound   = false;
                loadedSomething = false;
                foreach (var entry in model.Textures)
                {
                    //If texture is already loaded continue
                    if (loadedTextures.ContainsKey(entry.Key))
                    {
                        continue;
                    }

                    if (!entry.Value.StartsWith("#")) //If not a variable load texture
                    {
                        var filename = GetRelativePaths(path, entry.Value, PngExtension).FirstOrDefault(ResourceReader.Exists);

                        if (filename == null)
                        {
                            Logger.Error($"Texture \"{entry.Value}\" could not be found in {path}!");
                            loadedTextures.Add(entry.Key, ClientResources.MissingTexture);
                            continue;
                        }

                        loadedTextures.Add(entry.Key, ResourceReader.ReadBlockTexture(filename));

                        loadedSomething = true;
                    }
                    else //If variable try to find its value
                    {
                        if (loadedTextures.TryGetValue(entry.Value.Substring(1), out var texture))
                        {
                            loadedTextures.Add(entry.Key, texture);
                        }

                        variableFound = true;
                    }
                }

                counter++;

                if (counter >= 100)
                {
                    throw new Exception($"\"{path}\" has either more than 100 texture parents or is an endless loop!");
                }
            }

            foreach (var element in model.Elements)
            {
                foreach (var entry in element.Faces)
                {
                    if (loadedTextures.TryGetValue(entry.Value.Texture.Substring(1), out var texture))
                    {
                        entry.Value.LoadedTexture = texture;
                    }
                    else
                    {
                        throw new Exception($"Texture variable \"{entry.Value.Texture}\" in \"{path}\" does not have a value!");
                    }
                }
            }
        }