Exemplo n.º 1
0
        private void LoadBlockStates()
        {
            var idMapping = new Dictionary <uint, string>();

            using (var idMappingJsonFile =
                       typeof(MinecraftResourcePack).Assembly.GetManifestResourceStream(
                           typeof(MinecraftResourcePack).Namespace + ".blockstate-ids.json"))
            {
                using (var reader = new StreamReader(idMappingJsonFile))
                {
                    var json = reader.ReadToEnd();
                    idMapping = MCJsonConvert.DeserializeObject <Dictionary <uint, string> >(json);
                }
            }

            foreach (var kvp in idMapping)
            {
                _blockStateIds.Add(kvp.Key, kvp.Value);
            }

            var jsonFiles = _archive.Entries
                            .Where(e => e.FullName.StartsWith("assets/minecraft/blockstates/") && e.FullName.EndsWith(".json")).ToArray();

            foreach (var jsonFile in jsonFiles)
            {
                using (var r = new StreamReader(jsonFile.Open()))
                {
                    var blockState = MCJsonConvert.DeserializeObject <BlockState>(r.ReadToEnd());
                    ProcessBlockState(blockState);
                    _blockStates[jsonFile.Name.Replace(".json", "")] = blockState;
                }
            }
        }
Exemplo n.º 2
0
        private void LoadBlockState(ZipArchiveEntry entry, Match match)
        {
            try
            {
                string name      = match.Groups["filename"].Value;
                string nameSpace = match.Groups["namespace"].Value;
                string key       = $"{nameSpace}:{name}";

                using (var stream = entry.Open())
                {
                    var json = Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length));

                    var blockState = MCJsonConvert.DeserializeObject <BlockStateResource>(json);
                    blockState.Name      = name;
                    blockState.Namespace = nameSpace;


                    _blockStates[key] = blockState;

                    //return blockState;
                }
            }
            catch (Exception ex)
            {
                Log.Warn($"Could not load {entry.Name}!", ex);
                //	return null;
            }
        }
Exemplo n.º 3
0
        private BlockModel LoadBlockModel(ZipArchiveEntry entry, Match match)
        {
            try
            {
                string name      = match.Groups["filename"].Value.Replace("\\", "/");
                string nameSpace = match.Groups["namespace"].Value;

                using (var stream = entry.Open())
                {
                    var blockModel = MCJsonConvert.DeserializeObject <BlockModel>(Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length)));
                    blockModel.Name      = name; // name.Replace("block/", "");
                    blockModel.Namespace = nameSpace;

                    if (blockModel.ParentName != null)
                    {
                        //blockModel.ParentName = blockModel.ParentName.Replace("block/", "");
                    }

                    //blockModel = ProcessBlockModel(blockModel);
                    //_blockModels[$"{nameSpace}:{name}"] = blockModel;
                    return(blockModel);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error loading blockmodel.");

                return(null);
            }
        }
Exemplo n.º 4
0
        private bool TryGetBlockModel(string modelName, out BlockModel model)
        {
            model = null;

            var modelFile =
                _archive.Entries.FirstOrDefault(e => e.FullName.Equals("assets/minecraft/models/" + modelName + ".json"));

            if (modelFile == null)
            {
                Debug.WriteLine("Failed to load Block Model: File Not Found (" + "assets/minecraft/models/" + modelName + ".json)");
                return(false);
            }


            using (var r = new StreamReader(modelFile.Open()))
            {
                var json = r.ReadToEnd();
                try
                {
                    var blockModel = MCJsonConvert.DeserializeObject <BlockModel>(json);
                    blockModel.Name = modelName;

                    ProcessBlockModel(blockModel);
                    _blockModelsCache[modelName] = blockModel;

                    model = blockModel;
                    return(true);
                }
                catch (JsonException ex)
                {
                    Debug.WriteLine("Failed to load Block Model: " + modelName);
                    throw;
                }
            }
        }
Exemplo n.º 5
0
        private void LoadBlockState(ZipArchiveEntry entry, Match match)
        {
            try
            {
                string name      = match.Groups["filename"].Value;
                string nameSpace = match.Groups["namespace"].Value;

                using (var r = new StreamReader(entry.Open()))
                {
                    var json = r.ReadToEnd();

                    var blockState = MCJsonConvert.DeserializeObject <BlockStateResource>(json);
                    blockState.Name      = name;
                    blockState.Namespace = nameSpace;


                    _blockStates[$"{nameSpace}:{name}"] = blockState;

                    //return blockState;
                }
            }
            catch (Exception ex)
            {
                Log.Warn($"Could not load {entry.Name}!", ex);
                //	return null;
            }
        }
Exemplo n.º 6
0
        private void LoadLocalization(ZipArchiveEntry entry, Match match)
        {
            string name      = match.Groups["filename"].Value;
            string nameSpace = match.Groups["namespace"].Value;

            bool isJson = match.Groups["filetype"].Value.Equals("json", StringComparison.InvariantCultureIgnoreCase);

            using (var stream = entry.Open())
            {
                var text = Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length));
                LanguageResource lang;

                if (isJson)
                {
                    lang = MCJsonConvert.DeserializeObject <LanguageResource>(text);
                }
                else
                {
                    lang = LanguageResource.ParseLangFile(text);
                }

                lang.CultureName = name;
                lang.Namespace   = nameSpace;

                _languageCache[$"{nameSpace}:{name}"] = lang;
            }
        }
Exemplo n.º 7
0
        private void ProcessBlockModel(BlockModel model)
        {
            if (model.ParentName != null)
            {
                BlockModel parent;
                if (TryGetBlockModel(model.ParentName, out parent))
                {
                    model.Parent = parent;

                    if (model.Elements.Length == 0 && parent.Elements.Length > 0)
                    {
                        model.Elements = MCJsonConvert.DeserializeObject <BlockModelElement[]>(MCJsonConvert.SerializeObject(parent.Elements));
                    }

                    foreach (var kvp in parent.TextureDefinitions)
                    {
                        if (!model.TextureDefinitions.ContainsKey(kvp.Key))
                        {
                            model.TextureDefinitions.Add(kvp.Key, kvp.Value);
                        }
                    }

                    foreach (var kvp in parent.Textures)
                    {
                        if (!model.Textures.ContainsKey(kvp.Key))
                        {
                            model.Textures.Add(kvp.Key, kvp.Value);
                        }
                    }
                }
            }


            foreach (var textureDef in model.TextureDefinitions.OrderBy(e => e.Value.StartsWith("#")).ToArray())
            {
                //Debug.WriteLine("Lookup Texture {2}: {0} {1}", textureDef.Key, textureDef.Value, model.Name);
                if (!model.Textures.ContainsKey(textureDef.Key))
                {
                    Bitmap texture;
                    if (TryGetTexture(model, textureDef.Value, out texture))
                    {
                        model.Textures[textureDef.Key] = texture;
                    }
                }
            }


            foreach (var element in model.Elements)
            {
                foreach (var face in element.Faces)
                {
                    face.Value.Texture = GetBitmapFromBlockModel(model, face.Value.TextureName);
                    if (face.Value.Texture == null)
                    {
                        //Debug.WriteLine("Failed to find texture for face {0} on block {1}", face.Key, model.Name);
                    }
                }
            }
        }
Exemplo n.º 8
0
        private void Load(ZipArchive archive)
        {
            var manifestEntry = archive.GetEntry("manifest.json");
            var contentEntry  = archive.GetEntry("content.zipe");

            if (manifestEntry == null)
            {
                throw new InvalidMCPackException("No manifest found!");
            }

            Manifest = MCJsonConvert.DeserializeObject <McPackManifest>(manifestEntry.ReadAsString());

            if (contentEntry == null)
            {
                throw new InvalidMCPackException($"No content found for MCPack: {Manifest.Header.Name}");
            }

            List <MCPackModule> modules = new List <MCPackModule>();

            foreach (var module in Manifest.Modules)
            {
                switch (module.Type.ToLower())
                {
                case "skin_pack":
                    try
                    {
                        MCSkinPack skinPack = new MCSkinPack(contentEntry);
                        modules.Add(skinPack);
                    }
                    catch
                    {
                    }
                    break;
                }
            }

            List <MCPackModule> toRemove = new List <MCPackModule>();

            foreach (var module in modules)
            {
                try
                {
                    module.Load();
                }
                catch (Exception ex)
                {
                    Log.Error(ex, $"Failed to load MCPack module: {module.Name} from {Manifest.Header.Name}: {ex}");
                    toRemove.Add(module);
                }
            }

            foreach (var rm in toRemove)
            {
                modules.Remove(rm);
            }

            Modules = modules.ToArray();
        }
Exemplo n.º 9
0
        /// <inheritdoc />
        internal override bool Load()
        {
            try
            {
                List <LoadedSkin> skins = new List <LoadedSkin>();

                using (var archive = new ZipFileSystem(Entry.Open(), Entry.Name))
                {
                    var skinsEntry = archive.GetEntry("skins.json");
                    Info = MCJsonConvert.DeserializeObject <MCPackSkins>(skinsEntry.ReadAsString());

                    var geometryEntry = archive.GetEntry("geometry.json");

                    Dictionary <string, EntityModel> models =
                        MCJsonConvert.DeserializeObject <Dictionary <string, EntityModel> >(geometryEntry.ReadAsString());

                    foreach (var skin in Info.Skins)
                    {
                        EntityModel model;

                        if (!models.TryGetValue(skin.Geometry, out model))
                        {
                            continue;
                        }

                        var textureEntry = archive.GetEntry(skin.Texture);

                        if (textureEntry == null)
                        {
                            continue;
                        }

                        Image <Rgba32> img;

                        using (var s = textureEntry.Open())
                        {
                            //img = new Bitmap(s);
                            img = Image.Load <Rgba32>(s.ReadToSpan(textureEntry.Length), PngDecoder);
                        }

                        LoadedSkin loaded = new LoadedSkin(skin.LocalizationName, model, img);
                        skins.Add(loaded);
                        //skin.
                    }
                }

                Skins = skins.ToArray();

                return(true);
            }
            catch (InvalidDataException ex)
            {
                Log.Debug(ex, $"Could not load module.");
            }

            return(false);
        }
Exemplo n.º 10
0
        private Tag LoadTag(ZipArchiveEntry entry)
        {
            string nameSpace = entry.FullName.Split('/')[1];
            string name      = Path.GetFileNameWithoutExtension(entry.FullName);

            using (var r = new StreamReader(entry.Open()))
            {
                var blockModel = MCJsonConvert.DeserializeObject <Tag>(r.ReadToEnd());
                blockModel.Name      = name;
                blockModel.Namespace = nameSpace;

                blockModel = ProcessTag(blockModel);
                return(blockModel);
            }
        }
Exemplo n.º 11
0
        protected static ResourcePackManifest GetManifest(ZipArchive archive, ResourcePackType type)
        {
            if (type == ResourcePackType.Java)
            {
                var entry = archive.GetEntry("pack.mcmeta");
                if (entry != null)
                {
                    ResourcePackInfo info;
                    using (TextReader reader = new StreamReader(entry.Open()))
                    {
                        ResourcePackInfoWrapper wrap = MCJsonConvert.DeserializeObject <ResourcePackInfoWrapper>(reader.ReadToEnd());
                        info = wrap.pack;
                    }

                    try
                    {
                        var imgEntry = archive.GetEntry("pack.png");
                        if (imgEntry != null)
                        {
                            // Bitmap bmp = new Bitmap(imgEntry.Open());
                            using (var stream = imgEntry.Open())
                            {
                                var bmp = Image.Load <Rgba32>(stream.ReadToSpan(entry.Length));
                                return(new ResourcePackManifest(bmp, "", info.Description));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Warn(ex, $"Could not read resourcepack logo: {archive.ToString()}");
                    }

                    return(new ResourcePackManifest("", info.Description));
                }
                else
                {
                    throw new InvalidResourcePackException();
                }
            }
            else if (type == ResourcePackType.Bedrock)
            {
            }

            return(null);
        }
Exemplo n.º 12
0
        private ResourcePackItem LoadItemModel(ZipArchiveEntry entry, Match match)
        {
            string name      = match.Groups["filename"].Value.Replace("\\", "/");
            string nameSpace = match.Groups["namespace"].Value;

            using (var stream = entry.Open())
            {
                var blockModel = MCJsonConvert.DeserializeObject <ResourcePackItem>(Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length)));
                blockModel.Name      = name;           //name.Replace("item/", "");
                blockModel.Namespace = nameSpace;
                if (blockModel.ParentName != null)
                {
                    //	blockModel.ParentName = blockModel.ParentName.Replace("item/", "");
                }

                return(blockModel);
            }
        }
Exemplo n.º 13
0
        private ResourcePackItem LoadItemModel(ZipArchiveEntry entry, Match match)
        {
            string name      = match.Groups["filename"].Value;
            string nameSpace = match.Groups["namespace"].Value;

            using (var r = new StreamReader(entry.Open()))
            {
                var blockModel = MCJsonConvert.DeserializeObject <ResourcePackItem>(r.ReadToEnd());
                blockModel.Name      = name;           //name.Replace("item/", "");
                blockModel.Namespace = nameSpace;
                if (blockModel.ParentName != null)
                {
                    //	blockModel.ParentName = blockModel.ParentName.Replace("item/", "");
                }

                return(blockModel);
            }
        }
Exemplo n.º 14
0
        /// <inheritdoc />
        internal override void Load()
        {
            List <LoadedSkin> skins = new List <LoadedSkin>();

            using (var archive = new ZipArchive(Entry.Open(), ZipArchiveMode.Read))
            {
                var skinsEntry = archive.GetEntry("skins.json");
                Info = MCJsonConvert.DeserializeObject <MCPackSkins>(skinsEntry.ReadAsString());

                var geometryEntry = archive.GetEntry("geometry.json");

                Dictionary <string, EntityModel> models =
                    MCJsonConvert.DeserializeObject <Dictionary <string, EntityModel> >(geometryEntry.ReadAsString());

                foreach (var skin in Info.Skins)
                {
                    EntityModel model;
                    if (!models.TryGetValue(skin.Geometry, out model))
                    {
                        continue;
                    }

                    var textureEntry = archive.GetEntry(skin.Texture);
                    if (textureEntry == null)
                    {
                        continue;
                    }

                    Image <Rgba32> img;
                    using (var s = textureEntry.Open())
                    {
                        //img = new Bitmap(s);
                        img = Image.Load <Rgba32>(s.ReadToSpan(textureEntry.Length), PngDecoder);
                    }

                    LoadedSkin loaded = new LoadedSkin(skin.LocalizationName, model, img);
                    skins.Add(loaded);
                    //skin.
                }
            }

            Skins = skins.ToArray();
        }
Exemplo n.º 15
0
        private ResourcePackModelBase ReadModel(ZipArchiveEntry entry, ResourceLocation location)
        {
            try
            {
                using (var stream = entry.Open())
                {
                    var model = MCJsonConvert.DeserializeObject <ResourcePackModelBase>(Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length)));
                    model.Type = location.Path.Contains("block/", StringComparison.InvariantCultureIgnoreCase) ?
                                 ModelType.Block : ModelType.Item;

                    return(model);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex, $"Error loading model.");

                return(null);
            }
        }
Exemplo n.º 16
0
        private void LoadMeta(ZipArchive archive)
        {
            ResourcePackInfo info;

            var entry = archive.GetEntry("pack.mcmeta");

            if (entry == null)
            {
                info = new ResourcePackInfo();
            }
            else
            {
                using (TextReader reader = new StreamReader(entry.Open()))
                {
                    ResourcePackInfoWrapper wrap = MCJsonConvert.DeserializeObject <ResourcePackInfoWrapper>(reader.ReadToEnd());
                    info = wrap.pack;
                }
            }

            Info = info;
        }
Exemplo n.º 17
0
        private void LoadMeta(ZipArchive archive)
        {
            ResourcePackInfo info;

            var entry = archive.GetEntry("pack.mcmeta");

            if (entry == null)
            {
                info = new ResourcePackInfo();
            }
            else
            {
                using (var stream = entry.Open())
                {
                    ResourcePackInfoWrapper wrap =
                        MCJsonConvert.DeserializeObject <ResourcePackInfoWrapper>(
                            Encoding.UTF8.GetString(stream.ReadToSpan(entry.Length)));
                    info = wrap.pack;
                }
            }

            Info = info;
        }
Exemplo n.º 18
0
        protected static ResourcePackManifest GetManifest(ZipArchive archive, ResourcePackType type)
        {
            if (type == ResourcePackType.Java)
            {
                var entry = archive.GetEntry("pack.mcmeta");
                if (entry != null)
                {
                    ResourcePackInfo info;
                    using (TextReader reader = new StreamReader(entry.Open()))
                    {
                        ResourcePackInfoWrapper wrap = MCJsonConvert.DeserializeObject <ResourcePackInfoWrapper>(reader.ReadToEnd());
                        info = wrap.pack;
                    }

                    var imgEntry = archive.GetEntry("pack.png");
                    if (imgEntry != null)
                    {
                        // Bitmap bmp = new Bitmap(imgEntry.Open());
                        var bmp = Image.Load <Rgba32>(imgEntry.Open());
                        return(new ResourcePackManifest(bmp, "", info.Description));
                    }
                    else
                    {
                        return(new ResourcePackManifest("", info.Description));
                    }
                }
                else
                {
                    throw new InvalidResourcePackException();
                }
            }
            else if (type == ResourcePackType.Bedrock)
            {
            }

            return(null);
        }
Exemplo n.º 19
0
        private void InitializeGame(IProgressReceiver progressReceiver)
        {
            progressReceiver.UpdateProgress(0, "Initializing...");
            API.Extensions.Init(GraphicsDevice);
            MCPacketFactory.Load();
            //ConfigureServices();

            var eventDispatcher = Services.GetRequiredService <IEventDispatcher>() as EventDispatcher;

            foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
            {
                eventDispatcher.LoadFrom(assembly);
            }

            //var options = Services.GetService<IOptionsProvider>();

            //	Log.Info($"Loading resources...");
            if (!Resources.CheckResources(GraphicsDevice, progressReceiver,
                                          OnResourcePackPreLoadCompleted))
            {
                Console.WriteLine("Press enter to exit...");
                Console.ReadLine();
                Exit();
                return;
            }

            var profileManager = Services.GetService <ProfileManager>();

            profileManager.LoadProfiles(progressReceiver);

            //GuiRenderer.LoadResourcePack(Resources.ResourcePack, null);
            AnvilWorldProvider.LoadBlockConverter();

            PluginManager.EnablePlugins();

            var storage = Services.GetRequiredService <IStorageSystem>();

            if (storage.TryReadString("skin.json", out var str, Encoding.UTF8))
            {
                //var entries = new Dictionary<string, EntityModel>();
                //EntityModel.GetEntries(str, entries);
                var geometryModel =
                    MCJsonConvert.DeserializeObject <GeometryModel>(str);

                var model = geometryModel.FindGeometry("geometry.humanoid.custom");

                if (model == null)
                {
                    model = geometryModel.FindGeometry("geometry.humanoid.customSlim");
                }

                if (model != null)
                {
                    PlayerModel = model;
                }
            }

            if (PlayerModel == null)
            {
                if (ModelFactory.TryGetModel("geometry.humanoid.customSlim", out var model))
                {
                    model.Name  = "geometry.humanoid.customSlim";
                    PlayerModel = model;
                }
            }

            if (PlayerModel != null)
            {
                Log.Info($"Player model loaded...");
            }

            if (storage.TryReadBytes("skin.png", out byte[] skinBytes))
Exemplo n.º 20
0
        public static ResourcePackManifest GetManifest(IFilesystem archive)
        {
            //if (archive.GetEntry("pack.mcmeta") != null)

            var entry = archive.GetEntry("pack.mcmeta");

            if (entry != null)
            {
                ResourcePackInfo info;

                using (TextReader reader = new StreamReader(entry.Open()))
                {
                    ResourcePackInfoWrapper wrap =
                        MCJsonConvert.DeserializeObject <ResourcePackInfoWrapper>(reader.ReadToEnd());

                    info = wrap.pack;
                }

                try
                {
                    var imgEntry = archive.GetEntry("pack.png");

                    if (imgEntry != null && imgEntry.Length > 0)
                    {
                        // Bitmap bmp = new Bitmap(imgEntry.Open());
                        using (var stream = imgEntry.Open())
                        {
                            //var data = stream.ReadToSpan(entry.Length);

                            var bmp = Image.Load(stream).CloneAs <Rgba32>();

                            return(new ResourcePackManifest(bmp, "", info.Description, ResourcePackType.Java));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Warn(ex, $"Could not read resourcepack logo: {archive.ToString()}");
                }

                return(new ResourcePackManifest("", info.Description, ResourcePackType.Java));
            }

            entry = archive.GetEntry("manifest.json");

            if (entry != null)
            {
                //Load bedrock meta
                McPackManifest info;

                using (TextReader reader = new StreamReader(entry.Open()))
                {
                    info =
                        MCJsonConvert.DeserializeObject <McPackManifest>(reader.ReadToEnd());

                    //info = new ResourcePackInfo() {Description = wrap.Header.Description};
                    //info = wrap.pack;
                }

                try
                {
                    var imgEntry = archive.GetEntry("pack_icon.png");

                    if (imgEntry != null && imgEntry.Length > 0)
                    {
                        // Bitmap bmp = new Bitmap(imgEntry.Open());
                        using (var stream = imgEntry.Open())
                        {
                            //var data = stream.ReadToSpan(entry.Length);

                            var bmp = Image.Load(stream).CloneAs <Rgba32>();

                            return(new ResourcePackManifest(bmp, info.Header.Name, info.Header.Description, ResourcePackType.Bedrock));
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Warn(ex, $"Could not read resourcepack logo: {archive.ToString()}");
                }

                return(new ResourcePackManifest(info.Header.Name, info.Header.Description, ResourcePackType.Bedrock));
            }


            return(null);
        }