public int LoadResourcePack(IProgressReceiver progressReceiver, BedrockResourcePack resourcePack, bool replace) { int imported = 0; var total = resourcePack.EntityModels.Count; progressReceiver?.UpdateProgress(0, total, "Loading entity models..."); foreach (var blockmodel in resourcePack.EntityModels) { progressReceiver?.UpdateProgress(imported, total, null, blockmodel.Key.ToString()); var key = blockmodel.Key; if (ContainsKey(key)) { if (replace) { var entry = new EntityModelEntry(blockmodel.Value); Set(key, () => entry); } } else { Register(blockmodel.Key, new EntityModelEntry(blockmodel.Value)); } imported++; } return(imported); }
private void LoadEntityModels(BedrockResourcePack resourcePack, IProgressReceiver progressReceiver = null) { Stopwatch sw = Stopwatch.StartNew(); var modelRegistry = EntityModelRegistry; int imported = 0; imported = modelRegistry.LoadResourcePack(progressReceiver, resourcePack, true); Log.Info($"Imported {imported} block models from resourcepack in {sw.ElapsedMilliseconds}ms!"); sw.Stop(); }
private static void Add(BedrockResourcePack resourcepack, GraphicsDevice graphics, EntityDescription def, EntityModel model, ResourceLocation name) { _registeredRenderers.AddOrUpdate(name, (t) => { if (t == null) { var textures = def.Textures; string texture; if (!textures.TryGetValue("default", out texture) && !textures.TryGetValue(name.Path, out texture)) { texture = textures.FirstOrDefault().Value; } if (resourcepack.Textures.TryGetValue(texture, out var bmp)) { t = TextureUtils.BitmapToTexture2D(graphics, bmp.Value); } } return(new EntityModelRenderer(model, t)); }, (s, func) => { return((t) => { var textures = def.Textures; string texture; if (!(textures.TryGetValue("default", out texture) || textures.TryGetValue(name.Path, out texture))) { texture = textures.FirstOrDefault().Value; } if (resourcepack.Textures.TryGetValue(texture, out var bmp)) { t = TextureUtils.BitmapToTexture2D(graphics, bmp.Value); } return new EntityModelRenderer(model, t); }); }); }
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); }
public bool CheckResources(GraphicsDevice device, IProgressReceiver progressReceiver, McResourcePack.McResourcePackPreloadCallback preloadCallback) { PreloadCallback = preloadCallback; byte[] defaultResources; byte[] defaultBedrock; if (!CheckRequiredPaths(progressReceiver, out defaultResources, out defaultBedrock)) { return(false); } Log.Info($"Loading registries..."); progressReceiver?.UpdateProgress(0, "Loading registries..."); Registries = JsonConvert.DeserializeObject <Registries>(ReadStringResource("Alex.Resources.registries.json")); progressReceiver?.UpdateProgress(100, "Loading registries..."); Log.Info($"Loading vanilla resources..."); using (MemoryStream stream = new MemoryStream(defaultResources)) { var vanilla = LoadResourcePack(progressReceiver, stream, false, preloadCallback); vanilla.Manifest.Name = "Vanilla"; ActiveResourcePacks.AddFirst(vanilla); } var bedrockPath = Path.Combine("assets", "bedrock"); DirectoryInfo directory; if (!Storage.TryGetDirectory(bedrockPath, out directory)) { Log.Warn($"The bedrock resources required to play this game are not set-up correctly!"); Console.ReadLine(); Environment.Exit(1); return(false); } /*if (!directories.Any(x => x.Name.Equals("models"))) * { * Log.Warn($"Please make sure to extract the MC:Bedrock resource pack into \"{directory.FullName}\""); * Console.ReadLine(); * Environment.Exit(1); * return false; * }*/ if (directory.GetFileSystemInfos().Length == 0) { Log.Info($"Extracting required resources..."); progressReceiver?.UpdateProgress(50, "Extracting resources..."); byte[] zipped = defaultBedrock; //ReadResource("Alex.Resources.resources.zip"); using (MemoryStream ms = new MemoryStream(zipped)) { using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read)) { archive.ExtractToDirectory(directory.FullName); } } } var directories = directory.GetDirectories(); /* if (!directories.Any(x => x.Name.Equals("definitions"))) * { * Log.Warn($"The required definition files are not found. Any questions can be asked on Discord."); * Console.ReadLine(); * Environment.Exit(1); * return false; * }*/ //report(ResourcePack.AsciiFont); Log.Info($"Loading bedrock resources..."); BedrockResourcePack = new BedrockResourcePack(directory); EntityFactory.LoadModels(this, device, true, progressReceiver); Log.Info($"Loading known entity data..."); EntityFactory.Load(this, progressReceiver); Storage.TryGetDirectory(Path.Combine("assets", "resourcepacks"), out DirectoryInfo root); ResourcePackDirectory = root; LoadRegistries(progressReceiver); LoadResourcePacks(device, progressReceiver, Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Value); ItemFactory.Init(RegistryManager, this, ResourcePack, progressReceiver); if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out DirectoryInfo info)) { SkinPackDirectory = info; LoadBedrockPacks(progressReceiver, info); } else { if (Storage.TryCreateDirectory(Path.Combine("assets", "bedrockpacks"))) { if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out var dirInfo)) { SkinPackDirectory = dirInfo; } } } Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Bind(ResourcePacksChanged); _hasInit = true; return(true); }
private void LoadSkin(Skin skin) { try { if (skin == null) { return; } EntityModel model = null; if (!skin.IsPersonaSkin) { if (!string.IsNullOrWhiteSpace(skin.GeometryData) && !skin.GeometryData.Equals( "null", StringComparison.InvariantCultureIgnoreCase)) { try { if (string.IsNullOrWhiteSpace(skin.ResourcePatch) || skin.ResourcePatch == "null") { Log.Debug($"Resourcepatch null for player {Name}"); } else { var resourcePatch = JsonConvert.DeserializeObject <SkinResourcePatch>( skin.ResourcePatch, GeometrySerializationSettings); Dictionary <string, EntityModel> models = new Dictionary <string, EntityModel>(); BedrockResourcePack.LoadEntityModel(skin.GeometryData, models); var processedModels = BedrockResourcePack.ProcessEntityModels(models); if (processedModels == null || processedModels.Count == 0) { Log.Warn($"!! Model count was 0 for player {NameTag} !!"); if (!Directory.Exists("failed")) { Directory.CreateDirectory("failed"); } File.WriteAllText( Path.Combine( "failed", $"{Environment.TickCount64}-{resourcePatch.Geometry.Default}.json"), skin.GeometryData); } else { if (resourcePatch?.Geometry != null) { if (!processedModels.TryGetValue(resourcePatch.Geometry.Default, out model)) { Log.Debug( $"Invalid geometry: {resourcePatch.Geometry.Default} for player {Name}"); } else { } } else { Log.Debug($"Resourcepatch geometry was null for player {Name}"); } } } } catch (Exception ex) { string name = "N/A"; Log.Debug(ex, $"Could not create geometry ({name}): {ex.ToString()} for player {Name}"); } } else { //Log.Debug($"Geometry data null for player {Name}"); } } if (model == null) { ModelFactory.TryGetModel( skin.Slim ? "geometry.humanoid.custom" : "geometry.humanoid.customSlim", out model); /*model = skin.Slim ? (EntityModel) new Models.HumanoidCustomslimModel() : * (EntityModel) new HumanoidModel();*/// new Models.HumanoidCustomGeometryHumanoidModel(); } if (model != null && ValidateModel(model, Name)) { Image <Rgba32> skinBitmap = null; if (!skin.TryGetBitmap(model, out skinBitmap)) { Log.Warn($"No custom skin data for player {Name}"); if (Alex.Instance.Resources.TryGetBitmap("entity/alex", out var rawTexture)) { skinBitmap = rawTexture; } } //if (!Directory.Exists("skins")) // Directory.CreateDirectory("skins"); //var path = Path.Combine("skins", $"{model.Description.Identifier}-{Environment.TickCount64}"); //File.WriteAllText($"{path}.json", skin.GeometryData); //skinBitmap.SaveAsPng($"{path}.png"); var modelTextureSize = new Point( (int)model.Description.TextureWidth, (int)model.Description.TextureHeight); var textureSize = new Point(skinBitmap.Width, skinBitmap.Height); if (modelTextureSize != textureSize) { if (modelTextureSize.Y > textureSize.Y) { skinBitmap = SkinUtils.ConvertSkin(skinBitmap, modelTextureSize.X, modelTextureSize.Y); } /*var bitmap = skinBitmap; * bitmap.Mutate<Rgba32>(xx => * { * xx.Resize(modelTextureSize.X, modelTextureSize.Y); * // xx.Flip(FlipMode.Horizontal); * }); * * skinBitmap = bitmap;*/ } GeometryName = model.Description.Identifier; var modelRenderer = new EntityModelRenderer( model, TextureUtils.BitmapToTexture2D(Alex.Instance.GraphicsDevice, skinBitmap)); if (modelRenderer.Valid) { ModelRenderer = modelRenderer; } else { modelRenderer.Dispose(); Log.Debug($"Invalid model: for player {Name} (Disposing)"); } } else { Log.Debug($"Invalid model for player {Name}"); } } catch (Exception ex) { Log.Warn(ex, $"Error while handling player skin."); } }
private void InitializeGame(IProgressReceiver progressReceiver) { progressReceiver.UpdateProgress(0, "Initializing..."); API.Extensions.Init(GraphicsDevice); MCPacketFactory.Load(); //ConfigureServices(); //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)) { Dictionary <string, EntityModel> models = new Dictionary <string, EntityModel>(); BedrockResourcePack.LoadEntityModel(str, models); models = BedrockResourcePack.ProcessEntityModels(models); if (models.Count > 0) { if (models.TryGetValue("geometry.humanoid.custom", out var entityModel) || models.TryGetValue( "geometry.humanoid.customSlim", out entityModel)) { PlayerModel = entityModel; Log.Info($"Player model loaded..."); } } } 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))
public static int LoadModels(BedrockResourcePack resourcePack, GraphicsDevice graphics, bool replaceModels, IProgressReceiver progressReceiver = null) { var entityDefinitions = resourcePack.EntityDefinitions; int done = 0; int total = entityDefinitions.Count; foreach (var def in entityDefinitions) { // double percentage = 100D * ((double)done / (double)total); progressReceiver?.UpdateProgress(done, total, $"Importing entity definitions...", def.Key.ToString()); try { if (def.Value.Textures == null) { continue; } if (def.Value.Geometry == null) { continue; } if (def.Value.Textures.Count == 0) { continue; } if (def.Value.Geometry.Count == 0) { continue; } var geometry = def.Value.Geometry; string modelKey; if (!geometry.TryGetValue("default", out modelKey) && !geometry.TryGetValue(new ResourceLocation(def.Value.Identifier).Path, out modelKey)) { modelKey = geometry.FirstOrDefault().Value; } EntityModel model; if (ModelFactory.TryGetModel(modelKey + ".v1.8", out model) && model != null) { Add(resourcePack, graphics, def.Value, model, def.Value.Identifier); Add(resourcePack, graphics, def.Value, model, def.Key.ToString()); } else if (ModelFactory.TryGetModel(modelKey, out model) && model != null) { Add(resourcePack, graphics, def.Value, model, def.Value.Identifier); Add(resourcePack, graphics, def.Value, model, def.Key.ToString()); } } catch (Exception ex) { Log.Warn(ex, $"Failed to load model {def.Key}!"); } finally { done++; } } if (_registeredRenderers.TryGetValue("minecraft:armor_stand", out var func)) { _registeredRenderers.TryAdd("minecraft:armorstand", func); } // Log.Info($"Registered {(Assembly.GetExecutingAssembly().GetTypes().Count(t => t.Namespace == "Alex.Entities.Models"))} entity models"); // Log.Info($"Loaded {_registeredRenderers.Count} entity models"); return(_registeredRenderers.Count); }
public bool CheckResources(GraphicsDevice device, IProgressReceiver progressReceiver, McResourcePack.McResourcePackPreloadCallback preloadCallback) { LoadHWID(); PreloadCallback = preloadCallback; Log.Info($"Loading registries..."); progressReceiver?.UpdateProgress(0, "Loading registries..."); Registries = JsonConvert.DeserializeObject <Registries>(ReadStringResource("Alex.Resources.registries.json")); progressReceiver?.UpdateProgress(100, "Loading registries..."); string defaultResources; string defaultBedrock; if (!CheckJavaAssets(progressReceiver, out defaultResources)) { return(false); } if (!CheckBedrockAssets(progressReceiver, out defaultBedrock)) { return(false); } progressReceiver?.UpdateProgress(0, "Loading vanilla resources..."); var vanilla = LoadResourcePack(progressReceiver, new RealFileSystem(defaultResources), preloadCallback); vanilla.Manifest.Name = "Vanilla"; ActiveResourcePacks.AddFirst(vanilla); Alex.AudioEngine.Initialize(vanilla); // Log.Info($"Loading bedrock resources..."); progressReceiver?.UpdateProgress(0, "Loading bedrock resources..."); using (RealFileSystem fileSystem = new RealFileSystem(defaultBedrock)) { BedrockResourcePack = new BedrockResourcePack( fileSystem, (percentage, file) => { progressReceiver?.UpdateProgress(percentage, null, file); }); int modelCount = EntityFactory.LoadModels(BedrockResourcePack, device, true, progressReceiver); Log.Info($"Imported {modelCount} entity models..."); } //Log.Info($"Loading known entity data..."); EntityFactory.Load(this, progressReceiver); Storage.TryGetDirectory(Path.Combine("assets", "resourcepacks"), out DirectoryInfo root); ResourcePackDirectory = root; LoadRegistries(progressReceiver); LoadResourcePacks( device, progressReceiver, Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Value); ItemFactory.Init(RegistryManager, this, ResourcePack, progressReceiver); BlockEntityFactory.LoadResources(device, ResourcePack); if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out DirectoryInfo info)) { SkinPackDirectory = info; LoadBedrockPacks(progressReceiver, info); } else { if (Storage.TryCreateDirectory(Path.Combine("assets", "bedrockpacks"))) { if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out var dirInfo)) { SkinPackDirectory = dirInfo; } } } BlockMapper.Init(progressReceiver); Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Bind(ResourcePacksChanged); _hasInit = true; return(true); }
public bool CheckResources(GraphicsDevice device, IProgressReceiver progressReceiver, McResourcePack.McResourcePackPreloadCallback preloadCallback) { LoadHWID(); PreloadCallback = preloadCallback; Log.Info($"Loading registries..."); progressReceiver?.UpdateProgress(0, "Loading registries..."); Registries = JsonConvert.DeserializeObject <Registries>(ReadStringResource("Alex.Resources.registries.json")); progressReceiver?.UpdateProgress(100, "Loading registries..."); byte[] defaultResources; byte[] defaultBedrock; if (!CheckJavaAssets(progressReceiver, out defaultResources)) { return(false); } Log.Info($"Loading vanilla resources..."); progressReceiver?.UpdateProgress(0, "Loading vanilla resources..."); using (MemoryStream stream = new MemoryStream(defaultResources)) { var vanilla = LoadResourcePack(progressReceiver, stream, false, preloadCallback); vanilla.Manifest.Name = "Vanilla"; ActiveResourcePacks.AddFirst(vanilla); } if (!CheckBedrockAssets(progressReceiver, out defaultBedrock)) { return(false); } var bedrockPath = Path.Combine("assets", "bedrock"); DirectoryInfo directory; if (!Storage.TryGetDirectory(bedrockPath, out directory) && !Storage.TryCreateDirectory(bedrockPath)) { Log.Warn($"The bedrock resources required to play this game are not set-up correctly!"); Console.ReadLine(); Environment.Exit(1); return(false); } if (directory.GetFileSystemInfos().Length == 0) { Log.Info($"Extracting required resources..."); progressReceiver?.UpdateProgress(50, "Extracting resources..."); byte[] zipped = defaultBedrock; //ReadResource("Alex.Resources.resources.zip"); using (MemoryStream ms = new MemoryStream(zipped)) { using (ZipArchive archive = new ZipArchive(ms, ZipArchiveMode.Read)) { archive.ExtractToDirectory(directory.FullName); } } } Log.Info($"Loading bedrock resources..."); progressReceiver?.UpdateProgress(0, "Loading bedrock resources..."); BedrockResourcePack = new BedrockResourcePack(directory, (percentage, file) => { progressReceiver?.UpdateProgress(percentage, null, file); }); EntityFactory.LoadModels(this, device, true, progressReceiver); Log.Info($"Loading known entity data..."); EntityFactory.Load(this, progressReceiver); Storage.TryGetDirectory(Path.Combine("assets", "resourcepacks"), out DirectoryInfo root); ResourcePackDirectory = root; LoadRegistries(progressReceiver); LoadResourcePacks(device, progressReceiver, Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Value); ItemFactory.Init(RegistryManager, this, ResourcePack, progressReceiver); BlockEntityFactory.LoadResources(device, ResourcePack); if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out DirectoryInfo info)) { SkinPackDirectory = info; LoadBedrockPacks(progressReceiver, info); } else { if (Storage.TryCreateDirectory(Path.Combine("assets", "bedrockpacks"))) { if (Storage.TryGetDirectory(Path.Combine("assets", "bedrockpacks"), out var dirInfo)) { SkinPackDirectory = dirInfo; } } } Options.AlexOptions.ResourceOptions.LoadedResourcesPacks.Bind(ResourcePacksChanged); _hasInit = true; return(true); }