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))