Beispiel #1
0
        private void LoadModels(IProgressReceiver progressReceiver, McResourcePack resourcePack, bool replaceModels,
                                bool reportMissingModels)
        {
            Stopwatch sw            = Stopwatch.StartNew();
            var       modelRegistry = RegistryManager.GetRegistry <ResourcePackModelBase>();

            int imported = 0;

            if (modelRegistry is BlockModelRegistry bmr)
            {
                imported = bmr.LoadResourcePack(progressReceiver, resourcePack);
            }

            Log.Info($"Imported {imported} block models from resourcepack in {sw.ElapsedMilliseconds}ms!");

            sw.Restart();

            MeasureProfiler.StartCollectingData();
            imported = BlockFactory.LoadBlockstates(RegistryManager, this, resourcePack, replaceModels, reportMissingModels, progressReceiver);
            MeasureProfiler.SaveData();

            MeasureProfiler.StopCollectingData();

            Log.Info($"Imported {imported} blockstates from resourcepack in {sw.ElapsedMilliseconds}ms!");
        }
Beispiel #2
0
        private static BlockModel GetOrCacheModel(ResourceManager resources, McResourcePack resourcePack, BlockState state, uint id, bool rebuild)
        {
            BlockModel result = null;

            if (ModelCache.TryGetValue(id, out result) && !rebuild)
            {
                return(result);
            }
            else
            {
                var r = ResolveModel(resources, resourcePack, state);
                if (r == null)
                {
                    return(result);
                }

                result = r;

                if (!ModelCache.TryAdd(id, result))
                {
                    if (rebuild)
                    {
                        ModelCache[id] = result;
                    }
                    else
                    {
                        Log.Warn($"Could not register model in cache! {state.Name} - {state.ID}");
                    }
                }

                return(result);
            }
        }
Beispiel #3
0
        public int LoadResourcePack(IProgressReceiver progressReceiver, McResourcePack resourcePack, bool replace)
        {
            int imported = 0;
            var total    = resourcePack.BlockModels.Count;

            progressReceiver?.UpdateProgress(0, total, "Loading block models...");

            foreach (var blockmodel in resourcePack.BlockModels.Where(x => x.Value.Elements.Length > 0))
            {
                progressReceiver?.UpdateProgress(imported, total, null, blockmodel.Key.ToString());
                var key = blockmodel.Key;

                if (ContainsKey(key))
                {
                    if (replace)
                    {
                        var entry = new BlockModelEntry(blockmodel.Value);
                        Set(key, () => entry);
                    }
                }
                else
                {
                    Register(blockmodel.Key, new BlockModelEntry(blockmodel.Value));
                }

                imported++;
            }

            return(imported);
        }
Beispiel #4
0
        private McResourcePack LoadResourcePack(IProgressReceiver progressReceiver, Stream stream, bool useModelResolver = false, McResourcePack.McResourcePackPreloadCallback preloadCallback = null)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Log.Info($"Loading resource pack...");

            McResourcePack resourcePack = null;

            using (var archive = new ZipArchive(stream, ZipArchiveMode.Read, false))
            {
                resourcePack = new McResourcePack(archive, preloadCallback);
            }

            sw.Stop();

            Log.Info($"Loaded {resourcePack.BlockModels.Count} block models from resourcepack");
            Log.Info($"Loaded {resourcePack.ItemModels.Count} item models from resourcepack");
            Log.Info($"Resource pack loading took: {sw.ElapsedMilliseconds}ms");

            var language = resourcePack.Languages.Values.FirstOrDefault(x => x.Namespace.Equals("minecraft"));

            if (language != null)
            {
                foreach (var translation in language)
                {
                    ChatParser.TranslationRules[translation.Key] = translation.Value;
                }
            }

            return(resourcePack);
        }
Beispiel #5
0
        private Vector2 GetTextureDimensions(McResourcePack resourcePack, ResourceLocation location)
        {
            Vector2 size = new Vector2(TextureWidth, TextureHeight);

            return(size);

            foreach (var state in resourcePack.BlockModels)
            {
                foreach (var element in state.Value.Elements)
                {
                    foreach (var face in element.Faces)
                    {
                        var val = face.Value;
                        if (ResourcePackBlockModel.ResolveTexture(state.Value, val.Texture) == location)
                        {
                            var width  = val.UV.X2 - val.UV.X1;
                            var height = val.UV.Y2 - val.UV.Y1;

                            if (width > size.X)
                            {
                                size.X = width;
                            }

                            if (height > size.Y)
                            {
                                size.Y = height;
                            }
                        }
                    }
                }
            }

            return(size);
        }
Beispiel #6
0
        private void LoadTextures(GraphicsDevice device, IProgressReceiver progressReceiver,
                                  McResourcePack resourcePack, bool isFirst)
        {
            progressReceiver.UpdateProgress(0, $"Loading textures: {resourcePack.Manifest?.Name ?? "Unknown"}");

            if (!isFirst)
            {
                Atlas.LoadResourcePackOnTop(device,
                                            ActiveResourcePacks.First().TexturesAsBitmaps.Where(x => x.Key.StartsWith("block")).ToArray(),
                                            resourcePack.TexturesAsBitmaps.Where(x => x.Key.StartsWith("block")).ToArray(),
                                            resourcePack.TextureMetas,
                                            progressReceiver);
            }
            else
            {
                Atlas.GenerateAtlas(device, resourcePack.TexturesAsBitmaps.Where(x => x.Key.StartsWith("block")).ToArray(),
                                    resourcePack.TextureMetas,
                                    progressReceiver);


                //Atlas.Atlas.Save("atlas.png", ImageFormat.Png);
            }

            //  if (!isFirst)
            {
                progressReceiver.UpdateProgress(0, $"Loading UI textures: {resourcePack.Manifest?.Name ?? "Unknown"}");
                Alex.GuiRenderer.LoadResourcePackTextures(resourcePack, progressReceiver);
            }
        }
Beispiel #7
0
 public ItemModelRenderer(ResourcePackModelBase model, McResourcePack resourcePack) : base(model, resourcePack,
                                                                                           VertexPositionColor.VertexDeclaration)
 {
     if (model.ParentName != "item/handheld")
     {
         Offset = new Vector3(0f, -0.5f, 0f);
     }
 }
Beispiel #8
0
        public override void Cache(McResourcePack pack)
        {
            if (!Model.Textures.TryGetValue("layer0", out var t))
            {
                t = Model.Textures.FirstOrDefault(x => x.Value != null).Value;
            }

            if (t == null)
            {
                return;
            }

            List <VertexPositionColor> vertices = new List <VertexPositionColor>();

            if (pack.TryGetBitmap(t, out var rawTexture))
            {
                var texture = rawTexture.CloneAs <Rgba32>();

                float toolPosX = 0.0f;
                float toolPosY = 1.0f;
                float toolPosZ = (1f / 16f) * 7.5f;

                for (int y = 0; y < texture.Height; y++)
                {
                    for (int x = 0; x < texture.Width; x++)
                    {
                        var pixel = texture[x, y];
                        if (pixel.A == 0)
                        {
                            continue;
                        }

                        Color color = new Color(pixel.R, pixel.G, pixel.B, pixel.A);

                        ItemModelCube built =
                            new ItemModelCube(new Vector3(1f / texture.Width, 1f / texture.Height, 1f / 16f));
                        built.BuildCube(color);

                        var origin = new Vector3(
                            (toolPosX + (1f / texture.Width) * x),
                            toolPosY - (1f / texture.Height) * y,
                            toolPosZ
                            );

                        vertices.AddRange(Modify(built.Front, origin));
                        vertices.AddRange(Modify(built.Bottom, origin));
                        vertices.AddRange(Modify(built.Back, origin));
                        vertices.AddRange(Modify(built.Top, origin));
                        vertices.AddRange(Modify(built.Left, origin));
                        vertices.AddRange(Modify(built.Right, origin));
                    }
                }
            }

            Vertices = vertices.ToArray();
        }
Beispiel #9
0
        private void LoadModels(IProgressReceiver progressReceiver, McResourcePack resourcePack, bool replaceModels,
                                bool reportMissingModels)
        {
            Stopwatch sw       = Stopwatch.StartNew();
            int       imported = BlockFactory.LoadResources(RegistryManager, this, resourcePack, replaceModels, reportMissingModels, progressReceiver);

            sw.Stop();

            Log.Info($"Imported {imported} blockstate variants from resourcepack in {sw.ElapsedMilliseconds}ms!");
        }
Beispiel #10
0
        public ItemBlockModelRenderer(BlockState block, ResourcePackModelBase model, McResourcePack resourcePack,
                                      ResourceManager resourceManager) : base(model, resourcePack,
                                                                              VertexPositionNormalTextureColor.VertexDeclaration)
        {
            _block    = block;
            _resource = resourceManager;

            Offset = new Vector3(0f, -0.5f, 0f);
            //  Translation = -Vector3.Forward * 8f;
        }
Beispiel #11
0
        public void LoadResourcePack(GraphicsDevice graphicsDevice,
                                     McResourcePack resourcePack,
                                     IProgressReceiver progressReceiver)
        {
            Dictionary <ResourceLocation, ImageEntry> textures = new Dictionary <ResourceLocation, ImageEntry>();

            GetTextures(resourcePack, textures, progressReceiver);

            GenerateAtlas(graphicsDevice, textures, progressReceiver);
        }
Beispiel #12
0
        private void ProcessBlockStateModel(McResourcePack resourcePack, Dictionary <ResourceLocation, ImageEntry> textures, BlockStateModel bsModel)
        {
            var model = bsModel.Model;

            foreach (var texture in model.Textures)
            {
                var text = texture.Value;

                if (text[0] == '#')
                {
                    var substr = text.Substring(1);

                    if (model.Textures.TryGetValue(substr, out var p))
                    {
                        text = p;
                    }
                    else
                    {
                        var parent = model.Parent;

                        while (parent != null)
                        {
                            if (parent.Textures.TryGetValue(substr, out string parentName))
                            {
                                text = parentName;

                                break;
                            }

                            parent = parent.Parent;
                        }
                    }
                }

                var key = new ResourceLocation(text);

                var alreadyLoaded = textures.ContainsKey(key);
                if (!alreadyLoaded && resourcePack.TryGetBitmap(key, out var bmp))
                {
                    TextureMeta meta = null;
                    resourcePack.TryGetTextureMeta(key, out meta);

                    textures.TryAdd(key, new ImageEntry(bmp, meta));
                }
                else if (!alreadyLoaded)
                {
                    if (texture.Value[0] != '#' || text[0] != '#')
                    {
                        Log.Warn(
                            $"Could not get bitmap {texture.Value} or {text} (Key: {texture.Key} Model: {bsModel.ModelName})");
                    }
                }
            }
        }
Beispiel #13
0
 public static void LoadResources(GraphicsDevice graphicsDevice, McResourcePack resourcePack)
 {
     if (resourcePack.TryGetBitmap("minecraft:entity/chest/normal", out var bmp))
     {
         ChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, bmp);
     }
     else
     {
         Log.Warn($"Could not load chest texture.");
     }
 }
Beispiel #14
0
        public override void Cache(McResourcePack pack)
        {
            if (Vertices != null)
            {
                return;
            }

            var data = _block.Model.GetVertices(new ItemRenderingWorld(_block.Block), Vector3.Zero, _block.Block);

            Vertices = data.vertices;
            Indexes  = data.indexes.Select(x => (short)x).ToArray();
        }
Beispiel #15
0
        private void GetTextures(McResourcePack resourcePack,
                                 Dictionary <ResourceLocation, ImageEntry> textures,
                                 IProgressReceiver progressReceiver)
        {
            foreach (var texture in resourcePack.Textures.Where(x => x.Key.Path.StartsWith("block/", StringComparison.InvariantCultureIgnoreCase)))
            {
                if (textures.ContainsKey(texture.Key))
                {
                    continue;
                }

                TextureMeta meta = null;
                resourcePack.TryGetTextureMeta(texture.Key, out meta);
                textures.Add(texture.Key, new ImageEntry(texture.Value, meta));
            }

            /*
             * progressReceiver.UpdateProgress(0, "Processing blockstate textures...");
             * int blockstatesProcessed = 0;
             * int totalStates          = resourcePack.BlockStates.Count;
             *
             * foreach (var kv in resourcePack.BlockStates)
             * {
             *      progressReceiver.UpdateProgress(
             *              (int) (100D * ((double) blockstatesProcessed / (double) totalStates)), null, kv.Key);
             *
             *      var state = kv.Value;
             *
             *      foreach (var variant in state.Variants)
             *      {
             *              foreach (var va in variant.Value)
             *              {
             *                      ProcessBlockStateModel(resourcePack, textures, va);
             *              }
             *      }
             *
             *
             *      foreach (var part in state.Parts)
             *      {
             *              if (part.Apply == null)
             *                      continue;
             *
             *              foreach (var applied in part.Apply)
             *              {
             *                      ProcessBlockStateModel(resourcePack, textures, applied);
             *              }
             *      }
             *
             *      blockstatesProcessed++;
             *      // state.
             * }*/
        }
Beispiel #16
0
        public void LoadResourcePack(GraphicsDevice graphicsDevice,
                                     McResourcePack resourcePack,
                                     IProgressReceiver progressReceiver)
        {
            Dictionary <ResourceLocation, Image <Rgba32>[]> frames   = new Dictionary <ResourceLocation, Image <Rgba32>[]>();
            Dictionary <ResourceLocation, Image <Rgba32> >  textures = new Dictionary <ResourceLocation, Image <Rgba32> >();

            GetTextures(resourcePack, frames, textures, progressReceiver);

            GenerateAtlas(graphicsDevice, frames, textures, progressReceiver);

            string a = "b";
        }
Beispiel #17
0
        public void Initialize(McResourcePack resourcePack)
        {
            if (resourcePack.SoundDefinitions == null)
            {
                return;
            }

            foreach (var sound in resourcePack.SoundDefinitions)
            {
                foreach (var element in sound.Value.Sounds)
                {
                    string path        = StoragePath;
                    string elementPath = null;

                    if (element.SoundClass != null)
                    {
                        elementPath = $"minecraft/sounds/{element.SoundClass.Name}.ogg";
                        path        = Path.Combine(path, elementPath);
                    }
                    else if (element.Path != null)
                    {
                        elementPath = $"minecraft/sounds/{element.Path}.ogg";
                        path        = Path.Combine(path, elementPath);
                    }

                    if (!StorageSystem.Exists(path) && elementPath != null)
                    {
                        if (!StorageSystem.TryCreateDirectory(Path.GetDirectoryName(path)))
                        {
                        }

                        try
                        {
                            /*	using (var stream = resourcePack.GetStream(elementPath))
                             *      {
                             *              var data = stream.ReadToEnd();
                             *
                             *              if (StorageSystem.TryWriteBytes(path, data.ToArray()))
                             *              {
                             *                      Log.Info($"Saved {elementPath}...");
                             *              }
                             *      }*/
                        }
                        catch (FileNotFoundException)
                        {
                            Log.Warn($"File not found: {elementPath}");
                        }
                    }
                }
            }
        }
Beispiel #18
0
        public void LoadResourcePackOnTop(GraphicsDevice device, McResourcePack vanilla, McResourcePack resourcePack, IProgressReceiver progressReceiver)
        {
            int textureWidth = 16, textureHeight = 16;

            Dictionary <ResourceLocation, Image <Rgba32>[]> vanillaFrames = new Dictionary <ResourceLocation, Image <Rgba32>[]>();
            Dictionary <ResourceLocation, Image <Rgba32> >  vanillaTextures = new Dictionary <ResourceLocation, Image <Rgba32> >();

            GetTextures(vanilla, vanillaFrames, vanillaTextures, progressReceiver);

            Dictionary <ResourceLocation, Image <Rgba32>[]> frames = new Dictionary <ResourceLocation, Image <Rgba32>[]>();
            Dictionary <ResourceLocation, Image <Rgba32> >  textures = new Dictionary <ResourceLocation, Image <Rgba32> >();

            GetTextures(resourcePack, frames, textures, progressReceiver);

            foreach (var image in vanillaFrames.ToArray())
            {
                if (!frames.ContainsKey(image.Key))
                {
                    frames.Add(image.Key, image.Value);
                }
            }

            foreach (var image in vanillaTextures.ToArray())
            {
                if (!textures.ContainsKey(image.Key))
                {
                    textures.Add(image.Key, image.Value);
                }

                var texture = image.Value;
                if (texture.Width > textureWidth && texture.Width % 16 == 0 && texture.Height > textureHeight &&
                    texture.Height % 16 == 0)
                {
                    if (texture.Width == texture.Height)
                    {
                        textureWidth  = texture.Width;
                        textureHeight = texture.Height;
                    }
                }
            }

            var a      = (int)Math.Ceiling(textures.Count / 32D);
            int height = a * TextureHeight;

            AtlasSize     = new Vector2(Math.Min(32, textures.Count), height);
            TextureHeight = textureHeight;
            TextureWidth  = textureWidth;

            GenerateAtlas(device, frames, textures, progressReceiver);
        }
Beispiel #19
0
        public void GetTextures(McResourcePack resourcePack,
                                Dictionary <ResourceLocation, ImageEntry> textures,
                                IProgressReceiver progressReceiver)
        {
            /*List<ResourceLocation> texturePaths = new List<ResourceLocation>();
             * foreach (var model in resourcePack.BlockModels)
             * {
             *      foreach (var texture in model.Value.Textures)
             *      {
             *              if (!texturePaths.Contains(texture.Value))
             *                      texturePaths.Add(texture.Value);
             *      }
             *     // model.Value.Textures
             * }*/

            int done = 0;
            //    var items = resourcePack.Textures.Where(x => texturePaths.Contains(x.Key)).ToArray();
            var texturePaths = resourcePack.Textures.Where(x => x.Key.Path.Contains("block/")).ToArray();

            foreach (var path in texturePaths)
            {
                progressReceiver?.UpdateProgress(done++, texturePaths.Length, "Resolving textures...", path.ToString());

                if (resourcePack.TryGetBitmap(path.Key, out var texture))
                {
                    TextureMeta meta = null;
                    resourcePack.TryGetTextureMeta(path.Key, out meta);
                    //var entry = new ImageEntry(texture.Value.Value, meta);

                    if (textures.ContainsKey(path.Key))
                    {
                        if (meta != null)
                        {
                            textures[path.Key].Meta = meta;
                        }

                        if (texture != null)
                        {
                            textures[path.Key].Image = texture;
                        }

                        //textures[texture.Key] = entry;
                    }
                    else
                    {
                        textures.Add(path.Key, new ImageEntry(texture, meta));
                    }
                }
            }
        }
Beispiel #20
0
        public int LoadResourcePack(IProgressReceiver progressReceiver, McResourcePack resourcePack)
        {
            int imported = 0;
            var total    = resourcePack.BlockModels.Count;

            progressReceiver?.UpdateProgress(0, total, "Loading block models...");

            foreach (var blockmodel in resourcePack.BlockModels.Where(x => x.Value.Elements.Length > 0))
            {
                progressReceiver?.UpdateProgress(imported, total, null, blockmodel.Key.ToString());
                Register(blockmodel.Key, new BlockModelEntry(blockmodel.Value));
                imported++;
            }

            return(imported);
        }
Beispiel #21
0
        private void GetTextures(McResourcePack resourcePack,
                                 Dictionary <ResourceLocation, ImageEntry> textures,
                                 IProgressReceiver progressReceiver)
        {
            foreach (var texture in resourcePack.Textures.Where(x => x.Key.Path.StartsWith("block/", StringComparison.OrdinalIgnoreCase)))
            {
                if (textures.ContainsKey(texture.Key))
                {
                    continue;
                }

                TextureMeta meta = null;
                resourcePack.TryGetTextureMeta(texture.Key, out meta);
                textures.Add(texture.Key, new ImageEntry(texture.Value.Value, meta));
            }
        }
Beispiel #22
0
        private void LoadModels(IProgressReceiver progressReceiver, McResourcePack resourcePack, bool replaceModels,
                                bool reportMissingModels)
        {
            Stopwatch sw            = Stopwatch.StartNew();
            var       modelRegistry = RegistryManager.GetRegistry <ResourcePackModelBase>();

            int imported = 0;

            if (modelRegistry is BlockModelRegistry bmr)
            {
                imported = bmr.LoadResourcePack(progressReceiver, resourcePack, replaceModels);
            }

            Log.Info($"Imported {imported} block models from resourcepack in {sw.ElapsedMilliseconds}ms!");

            sw.Stop();
        }
Beispiel #23
0
        private ResourcePack LoadResourcePack(IProgressReceiver progressReceiver, IFilesystem fs, McResourcePack.McResourcePackPreloadCallback preloadCallback = null)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Log.Info($"Loading resource pack ({fs.Name})...");

            try
            {
                var manifest = ResourcePackLib.ResourcePack.GetManifest(fs);

                if (manifest == null || manifest.Type == ResourcePackType.Java)
                {
                    McResourcePack resourcePack = new McResourcePack(
                        fs, preloadCallback, (percentage, file) =>
                    {
                        progressReceiver?.UpdateProgress(percentage, null, file);
                    });

                    sw.Stop();

                    Log.Info($"Loaded {resourcePack.BlockModels.Count} block models from resourcepack");
                    Log.Info($"Loaded {resourcePack.ItemModels.Count} item models from resourcepack");
                    Log.Info($"Loading resourcepack took: {sw.ElapsedMilliseconds}ms");

                    return(resourcePack);
                }
                else if (manifest.Type == ResourcePackType.Bedrock)
                {
                    BedrockResourcePack brp = new BedrockResourcePack(
                        fs, (percentage, file) =>
                    {
                        progressReceiver?.UpdateProgress(percentage, null, file);
                    });

                    return(brp);
                }
            }
            catch (Exception ex)
            {
                Log.Warn(ex, $"Failed to load.");
            }

            return(null);
        }
Beispiel #24
0
        private void GetTextures(McResourcePack resourcePack,
                                 Dictionary <ResourceLocation, ImageEntry> textures,
                                 IProgressReceiver progressReceiver)
        {
            progressReceiver.UpdateProgress(0, "Processing blockstate textures...");
            int blockstatesProcessed = 0;
            int totalStates          = resourcePack.BlockStates.Count;

            foreach (var kv in resourcePack.BlockStates)
            {
                progressReceiver.UpdateProgress(
                    (int)(100D * ((double)blockstatesProcessed / (double)totalStates)), null, kv.Key);

                var state = kv.Value;

                foreach (var variant in state.Variants)
                {
                    foreach (var va in variant.Value)
                    {
                        ProcessBlockStateModel(resourcePack, textures, va);
                    }
                }


                foreach (var part in state.Parts)
                {
                    if (part.Apply == null)
                    {
                        continue;
                    }

                    foreach (var applied in part.Apply)
                    {
                        ProcessBlockStateModel(resourcePack, textures, applied);
                    }
                }

                blockstatesProcessed++;
                // state.
            }
        }
Beispiel #25
0
        private McResourcePack LoadResourcePack(IProgressReceiver progressReceiver, IFilesystem fs, McResourcePack.McResourcePackPreloadCallback preloadCallback = null)
        {
            Stopwatch sw = Stopwatch.StartNew();

            Log.Info($"Loading resource pack...");

            McResourcePack resourcePack = null;

            resourcePack = new McResourcePack(
                fs, preloadCallback, (percentage, file) =>
            {
                progressReceiver?.UpdateProgress(percentage, null, file);
            });

            sw.Stop();

            Log.Info($"Loaded {resourcePack.BlockModels.Count} block models from resourcepack");
            Log.Info($"Loaded {resourcePack.ItemModels.Count} item models from resourcepack");
            Log.Info($"Loading resourcepack took: {sw.ElapsedMilliseconds}ms");

            return(resourcePack);
        }
        public static void LoadResources(GraphicsDevice graphicsDevice, McResourcePack resourcePack)
        {
            if (resourcePack.TryGetBitmap("minecraft:entity/chest/normal", out var bmp))
            {
                ChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, bmp);
            }
            else
            {
                Log.Warn($"Could not load chest texture.");
            }

            if (resourcePack.TryGetBitmap("minecraft:entity/chest/ender", out var enderBmp))
            {
                EnderChestTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, enderBmp);
            }
            else
            {
                Log.Warn($"Could not load enderchest texture");
            }

            if (resourcePack.TryGetBitmap("minecraft:entity/steve", out var steveBmp))
            {
                SkullTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, steveBmp);
            }
            else
            {
                Log.Warn($"Could not load skull texture");
            }


            if (resourcePack.TryGetBitmap("minecraft:entity/signs/oak", out var signBmp))
            {
                SignTexture = TextureUtils.BitmapToTexture2D(graphicsDevice, signBmp);
            }
            else
            {
                Log.Warn($"Could not load sign texture");
            }
        }
Beispiel #27
0
        private void LoadTextures(GraphicsDevice device, IProgressReceiver progressReceiver,
                                  McResourcePack resourcePack, bool isFirst)
        {
            progressReceiver.UpdateProgress(0, $"Loading textures: {resourcePack.Manifest?.Name ?? "Unknown"}");

            if (!isFirst)
            {
                //        Atlas.LoadResourcePackOnTop(device,
                //            ActiveResourcePacks.First(),
                //            resourcePack,
                //           progressReceiver);
            }
            else
            {
                Atlas.LoadResourcePack(device, resourcePack, progressReceiver);
            }

            //  if (!isFirst)
            {
                progressReceiver.UpdateProgress(0, $"Loading UI textures: {resourcePack.Manifest?.Name ?? "Unknown"}");
                Alex.GuiRenderer.LoadResourcePackTextures(resourcePack, progressReceiver);
            }
        }
Beispiel #28
0
        public override void Cache(McResourcePack pack)
        {
            if (Vertices != null)
            {
                return;
            }

            ChunkData chunkData = new ChunkData();

            _block.Model.GetVertices(new ItemRenderingWorld(_block.Block), chunkData, BlockCoordinates.Zero, Vector3.Zero, _block.Block);

            var rawVertices = chunkData.Vertices;
            int count       = rawVertices.Length;

            while (count > 0 && count % 3 != 0)
            {
                count--;
            }

            Vertices = rawVertices.Take(count).Select(
                x => new VertexPositionColorTexture(x.Position, x.Color, x.TexCoords)).ToArray();

            chunkData.Dispose();
        }
Beispiel #29
0
        public static void Init(IRegistryManager registryManager, ResourceManager resources, McResourcePack resourcePack, IProgressReceiver progressReceiver = null)
        {
            var blockRegistry = registryManager.GetRegistry <Block>();

            ResourceManager = resources;
            ResourcePack    = resourcePack;

            var otherRaw = ResourceManager.ReadStringResource("Alex.Resources.items3.json");

            SecItemEntries = JsonConvert.DeserializeObject <SecondItemEntry[]>(otherRaw);

            var raw = ResourceManager.ReadStringResource("Alex.Resources.items2.json");

            ItemEntries = JsonConvert.DeserializeObject <ItemEntry[]>(raw);


            var ii     = resources.Registries.Items.Entries;
            var blocks = resources.Registries.Blocks.Entries;

            LoadModels();

            Dictionary <ResourceLocation, Func <Item> > items = new Dictionary <ResourceLocation, Func <Item> >();

            for (int i = 0; i < blocks.Count; i++)
            {
                var entry = blocks.ElementAt(i);
                progressReceiver?.UpdateProgress((int)(100D * ((double)i / (double)blocks.Count)), $"Processing block items...", entry.Key);

                Item item;

                /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                 * {
                 *      item = new ItemBlock(blockState.Value);
                 * }*/
                var bs = BlockFactory.GetBlockState(entry.Key);
                if (!(bs.Block is Air))
                {
                    item = new ItemBlock(bs);
                    //  Log.Info($"Registered block item: {entry.Key}");
                }
                else
                {
                    continue;
                }

                var minetItem = MiNET.Items.ItemFactory.GetItem(entry.Key.Replace("minecraft:", ""));
                if (minetItem != null)
                {
                    if (Enum.TryParse <ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                    {
                        item.ItemType = t;
                    }

                    SetItemMaterial(item, minetItem.ItemMaterial);
                    // item.Material = minetItem.ItemMaterial;

                    item.Meta = minetItem.Metadata;
                    item.Id   = minetItem.Id;
                }

                item.Name        = entry.Key;
                item.DisplayName = entry.Key;

                var data = ItemEntries.FirstOrDefault(x =>
                                                      x.name.Equals(entry.Key.Substring(10), StringComparison.InvariantCultureIgnoreCase));
                if (data != null)
                {
                    item.MaxStackSize = data.stackSize;
                    item.DisplayName  = data.displayName;
                }

                string ns   = ResourceLocation.DefaultNamespace;
                string path = entry.Key;
                if (entry.Key.Contains(':'))
                {
                    var index = entry.Key.IndexOf(':');
                    ns   = entry.Key.Substring(0, index);
                    path = entry.Key.Substring(index + 1);
                }

                var key = new ResourceLocation(ns, $"block/{path}");

                ResourcePackModelBase model;
                if (!(ResourcePack.ItemModels.TryGetValue(key, out model)))
                {
                    foreach (var it in ResourcePack.ItemModels)
                    {
                        if (it.Key.Path.Equals(key.Path, StringComparison.InvariantCultureIgnoreCase))
                        {
                            model = it.Value;
                            break;
                        }
                    }
                }

                if (model != null)
                {
                    item.Renderer = new ItemBlockModelRenderer(bs, model, resourcePack, resources);
                    item.Renderer.Cache(resourcePack);
                }
                else
                {
                    Log.Warn($"Could not find block model renderer for: {key.ToString()}");
                }

                items.TryAdd(entry.Key, () =>
                {
                    return(item.Clone());
                });
            }

            for (int i = 0; i < ii.Count; i++)
            {
                var entry = ii.ElementAt(i);
                progressReceiver?.UpdateProgress(i * (100 / ii.Count), $"Processing items...", entry.Key);

                Item item;

                /*if (blockRegistry.TryGet(entry.Key, out var blockState))
                 * {
                 *      item = new ItemBlock(blockState.Value);
                 * }*/
                /*   if (blocks.ContainsKey(entry.Key) && blockRegistry.TryGet(entry.Key, out var registryEntry))
                 * {
                 *         item = new ItemBlock(registryEntry.Value);
                 * }
                 * else
                 * {*/
                item = new Item();
                // }

                var minetItem = MiNET.Items.ItemFactory.GetItem(entry.Key.Replace("minecraft:", ""));
                if (minetItem != null)
                {
                    if (Enum.TryParse <ItemType>(minetItem.ItemType.ToString(), out ItemType t))
                    {
                        item.ItemType = t;
                    }

                    SetItemMaterial(item, minetItem.ItemMaterial);

                    // item.Material = minetItem.ItemMaterial;
                    item.Meta = minetItem.Metadata;
                    item.Id   = minetItem.Id;
                }

                item.Name        = entry.Key;
                item.DisplayName = entry.Key;

                var data = ItemEntries.FirstOrDefault(x =>
                                                      x.name.Equals(entry.Key.Substring(10), StringComparison.InvariantCultureIgnoreCase));
                if (data != null)
                {
                    item.MaxStackSize = data.stackSize;
                    item.DisplayName  = data.displayName;
                }

                string ns   = ResourceLocation.DefaultNamespace;
                string path = entry.Key;
                if (entry.Key.Contains(':'))
                {
                    var index = entry.Key.IndexOf(':');
                    ns   = entry.Key.Substring(0, index);
                    path = entry.Key.Substring(index + 1);
                }

                var key = new ResourceLocation(ns, $"item/{path}");

                foreach (var it in ResourcePack.ItemModels)
                {
                    if (it.Key.Path.Equals(key.Path, StringComparison.InvariantCultureIgnoreCase))
                    {
                        //Log.Info($"Model found: {entry.Key} = {it.Key}");
                        ItemModelRenderer renderer;
                        if (ItemRenderers.TryGetValue(it.Key, out renderer))
                        {
                        }
                        else if (ItemRenderers.TryGetValue(key, out renderer))

                        {
                        }

                        if (renderer != null)
                        {
                            //Log.Debug($"Found renderer for {entry.Key}, textures: {it.Value.Textures.Count}");
                            item.Renderer = renderer;
                            break;
                        }
                    }
                }

                if (item.Renderer == null)
                {
                    Log.Warn($"Could not find item model renderer for: {key.ToString()}");
                }

                items.TryAdd(key, () => { return(item.Clone()); });
            }

            Items = new ReadOnlyDictionary <ResourceLocation, Func <Item> >(items);
        }
Beispiel #30
0
        private static BlockModel ResolveModel(ResourceManager resources, McResourcePack resourcePack,
                                               BlockState state)
        {
            string name = state.Name;

            if (string.IsNullOrWhiteSpace(name))
            {
                Log.Warn($"State name is null!");
                return(null);
            }

            if (name.Contains("water"))
            {
                return(new LiquidBlockModel()
                {
                    //	IsFlowing = false,
                    IsLava = false,
                    //	Level = state.GetTypedValue(Water.LEVEL)
                });
            }

            if (name.Contains("lava"))
            {
                return(new LiquidBlockModel()
                {
                    //	IsFlowing = false,
                    IsLava = true,
                    //	Level = state.GetTypedValue(Water.LEVEL)
                });;
            }

            BlockStateResource blockStateResource;

            if (resourcePack.BlockStates.TryGetValue(name, out blockStateResource))
            {
                if (blockStateResource != null && blockStateResource.Parts != null &&
                    blockStateResource.Parts.Length > 0 &&
                    blockStateResource.Parts.All(x => x.Apply.All(b => b.Model != null)))
                {
                    var models = MultiPartModels.GetModels(state, blockStateResource);


                    state.MultiPartHelper = blockStateResource;
                    state.IsMultiPart     = true;
                    state.AppliedModels   = models.Select(x => x.ModelName).ToArray();

                    return(new ResourcePackBlockModel(resources, models));
                }

                if (blockStateResource?.Variants == null ||
                    blockStateResource.Variants.Count == 0)
                {
                    return(null);
                }

                if (blockStateResource.Variants.Count == 1)
                {
                    var v = blockStateResource.Variants.FirstOrDefault();
                    if (v.Value == null)
                    {
                        return(null);
                    }

                    var models = v.Value.Where(x => x.Model?.Elements != null && x.Model.Elements.Length > 0).ToArray();

                    if (models.Length == 0)
                    {
                        return(null);
                    }

                    return(new ResourcePackBlockModel(resources, models, v.Value.ToArray().Length > 1));
                }

                BlockStateVariant blockStateVariant = null;

                var data = state.ToDictionary();
                //	data.Remove("waterlogged");

                int closestMatch = 0;
                KeyValuePair <string, BlockStateVariant> closest = default(KeyValuePair <string, BlockStateVariant>);
                foreach (var v in blockStateResource.Variants)
                {
                    int matches           = 0;
                    var variantBlockState = Blocks.State.BlockState.FromString(v.Key);

                    foreach (var kv in data)
                    {
                        if (variantBlockState.TryGetValue(kv.Key, out string vValue))
                        {
                            if (vValue.Equals(kv.Value, StringComparison.InvariantCultureIgnoreCase))
                            {
                                matches++;
                            }
                            else
                            {
                                break;
                            }
                        }
                        else
                        {
                            break;
                        }
                    }

                    if (matches > closestMatch)
                    {
                        closestMatch = matches;
                        closest      = v;

                        if (matches == data.Count)
                        {
                            break;
                        }
                    }
                }

                blockStateVariant = closest.Value;

                if (blockStateVariant == null)
                {
                    var a = blockStateResource.Variants.FirstOrDefault();
                    blockStateVariant = a.Value;
                }

                var asArray = blockStateVariant.ToArray();

                if (asArray.Length == 0 || asArray.Any(x => x.Model?.Elements == null || x.Model.Elements.Length == 0))
                {
                    return(null);
                }

                return(new ResourcePackBlockModel(resources, asArray, asArray.Length > 1));
            }

            return(null);
        }