Example #1
0
        public static void Init(ResourceManager resources, McResourcePack resourcePack, IProgressReceiver progressReceiver = null)
        {
            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");

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


            var ii = resources.Registries.Items.Entries;

            LoadModels();

            Dictionary <string, Item> items = new Dictionary <string, Item>();

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

                var blockState = BlockFactory.GetBlockState(entry.Key);

                Item item;
                if (blockState != null)
                {
                    item = new ItemBlock(blockState);
                }
                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;
                    }
                    item.Material = minetItem.ItemMaterial;
                    item.Meta     = minetItem.Metadata;
                    item.Id       = minetItem.Id;
                }

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

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


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

                        {
                        }

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

                /* if (ResourcePack.ItemModels.TryGetValue(entry.Key.Replace("minecraft:", "minecraft:item/"), out ResourcePackItem iii))
                 * {
                 *       ItemModelRenderer renderer;
                 *       if (ItemRenderers.TryGetValue(entry.Key, out renderer))
                 *       {
                 *
                 *       }
                 *       else if (ItemRenderers.TryGetValue(entry.Key, out renderer))
                 *
                 *       {
                 *
                 *       }
                 *
                 *       if (renderer != null)
                 *       {
                 *               Log.Info($"Found renderer for {entry.Key}, textures: {iii.Textures.Count}");
                 *       }
                 *
                 *       item.Renderer = renderer;
                 * }*/

                //   Log.Info($"Loaded item: {entry.Key} (Renderer: {item.Renderer != null})");
                items.TryAdd(entry.Key, item);
            }

            Items = new ReadOnlyDictionary <string, Item>(items);
        }
Example #2
0
        private void LoadResourcePacks(GraphicsDevice device, IProgressReceiver progress, string[] resourcePacks)
        {
            var countBefore = ActiveResourcePacks.Count;

            var first  = ActiveResourcePacks.First.Value;
            var before = ActiveResourcePacks.ToArray();

            foreach (var item in before.Skip(1))
            {
                item.Dispose();
            }

            ActiveResourcePacks.Clear();

            ActiveResourcePacks.AddFirst(first);

            if (_hasInit)
            {
                PreloadCallback?.Invoke(first.FontBitmap, McResourcePack.BitmapFontCharacters.ToList());
                Atlas.Reset();
            }

            foreach (string file in resourcePacks)
            {
                try
                {
                    string resourcePackPath = Path.Combine(ResourcePackDirectory.FullName, file);
                    if (File.Exists(resourcePackPath))
                    {
                        //Log.Info($"Loading resourcepack {file}...");

                        //using (FileStream stream = new FileStream(resourcePackPath, FileMode.Open))
                        {
                            // using (ZipFileSystem zipFileSystem = )
                            {
                                var pack = LoadResourcePack(progress, new ZipFileSystem(new FileStream(resourcePackPath, FileMode.Open), Path.GetFileNameWithoutExtension(resourcePackPath)), null);

                                if (pack is McResourcePack javaPack)
                                {
                                    if (pack.Info != null && string.IsNullOrWhiteSpace(pack.Info.Name))
                                    {
                                        pack.Info.Name = Path.GetFileNameWithoutExtension(file);
                                    }

                                    ActiveResourcePacks.AddLast(javaPack);
                                }
                                else if (pack is BedrockResourcePack bedrockPack)
                                {
                                    ActiveBedrockResourcePacks.AddLast(bedrockPack);
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    Log.Warn(e, $"Could not load resourcepack {file}: {e.ToString()}");
                }
            }

            var  active  = ActiveResourcePacks.ToArray();
            bool isFirst = true;

            foreach (var resourcePack in active)
            {
                Alex.GuiRenderer.LoadLanguages(resourcePack, progress);

                LoadModels(progress, resourcePack, !isFirst, isFirst);
                if (isFirst)
                {         //Only load models for vanilla until above we fix blockstate replacement issues.
                    isFirst = false;
                    // break;
                }
            }

            Stopwatch sw = Stopwatch.StartNew();

            MeasureProfiler.StartCollectingData();
            var imported = BlockFactory.LoadBlockstates(RegistryManager, this, false, false, progress);

            MeasureProfiler.SaveData();

            MeasureProfiler.StopCollectingData();

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

            var textures = new Dictionary <ResourceLocation, AtlasGenerator.ImageEntry>();

            for (var index = 0; index < active.Length; index++)
            {
                var resourcePack = active[index];

                progress?.UpdateProgress(0, $"Loading textures: {resourcePack.Info?.Name ?? "Unknown"}");

                Atlas.LoadResourcePackOnTop(device,
                                            textures,
                                            resourcePack,
                                            progress, index == active.Length - 1);
                //LoadTextures(device, progress, resourcePack, ref textures, index == active.Length - 1);
            }

            foreach (var resourcePack in ActiveBedrockResourcePacks)
            {
                LoadEntityModels(resourcePack, progress);
                int modelCount = EntityFactory.LoadModels(resourcePack, this, device, true, progress);

                Log.Info($"Imported {modelCount} entity models...");
            }

            progress?.UpdateProgress(0, $"Loading UI textures...");
            Alex.GuiRenderer.LoadResourcePackTextures(this, progress);

            progress?.UpdateProgress(50, "Loading language...");
            if (!Alex.GuiRenderer.SetLanguage(Options.AlexOptions.MiscelaneousOptions.Language.Value))
            {
                Alex.GuiRenderer.SetLanguage(CultureInfo.InstalledUICulture.Name);
            }

            progress?.UpdateProgress(100, "Loading language...");

            var f = ActiveResourcePacks.LastOrDefault(x => x.FontBitmap != null);

            if (f != null)
            {
                PreloadCallback?.Invoke(f.FontBitmap, McResourcePack.BitmapFontCharacters.ToList());
            }
        }