public void LoadSkin(Skin skin) { //PooledTexture2D skinTexture = null; Image <Rgba32> skinBitmap = null; if (!skin.TryGetBitmap(out skinBitmap)) { Log.Warn($"No custom skin data for player {Name}"); if (Alex.Instance.Resources.ResourcePack.TryGetBitmap("entity/alex", out var rawTexture)) { skinBitmap = rawTexture; //skinTexture = TextureUtils.BitmapToTexture2D(AlexInstance.GraphicsDevice, rawTexture); } } EntityModel model = null; if (!skin.IsPersonaSkin) { if (!string.IsNullOrWhiteSpace(skin.GeometryData) && skin.GeometryData != "null") { try { if (string.IsNullOrWhiteSpace(skin.ResourcePatch) || skin.ResourcePatch == "null") { Log.Warn($"Resourcepatch null for player {Name}"); } else { var resourcePatch = JsonConvert.DeserializeObject <SkinResourcePatch>( skin.ResourcePatch, GeometrySerializationSettings); GeometryModel geometryModel = null; // Dictionary<string, EntityModel> models = new Dictionary<string, EntityModel>(); if (!GeometryModel.TryParse(skin.GeometryData, resourcePatch, out geometryModel)) { Log.Warn($"Failed to parse geometry for player {Name}"); } /*try * { * geometryModel = MCJsonConvert.DeserializeObject<GeometryModel>(skin.GeometryData); * } * catch (Exception ex) * { * Log.Warn($"Failed to parse geometry for player {Name}: {ex.ToString()}"); * }*/ if (geometryModel == null || geometryModel.Geometry.Count == 0) { Log.Warn($"!! Model count was 0 for player {Name} !!"); //EntityModel.GetEntries(r.Skin.GeometryData, models); } else { if (resourcePatch?.Geometry != null) { model = geometryModel.FindGeometry(resourcePatch.Geometry.Default); if (model == null) { Log.Warn( $"Invalid geometry: {resourcePatch.Geometry.Default} for player {Name}"); } else { var modelTextureSize = model.Description != null ? new Point((int)model.Description.TextureWidth, (int)model.Description.TextureHeight) : new Point((int)model.Texturewidth, (int)model.Textureheight); var textureSize = new Point(skinBitmap.Width, skinBitmap.Height); if (modelTextureSize != textureSize) { int newHeight = modelTextureSize.Y > textureSize.Y ? textureSize.Y : modelTextureSize.Y; int newWidth = modelTextureSize.X > textureSize.X ? textureSize.X: modelTextureSize.X; var bitmap = skinBitmap; // bitmap.Mutate<Rgba32>(xx => xx.Resize(newWidth, newHeight)); Image <Rgba32> skinTexture = new Image <Rgba32>(modelTextureSize.X, modelTextureSize.Y); skinTexture.Mutate <Rgba32>( c => { c.DrawImage(bitmap, new SixLabors.ImageSharp.Point(0, 0), 1f); }); skinBitmap = skinTexture; } } } else { Log.Warn($"Resourcepatch geometry was null for player {Name}"); } } /*foreach (var mm in models.ToArray()) * { * if (ProcessEntityModel(models, mm.Value, false)) * { * models.Remove(mm.Key); * } * } * * foreach (var mm in models.ToArray()) * { * if (ProcessEntityModel(models, mm.Value, true)) * { * models.Remove(mm.Key); * } * }*/ } } catch (Exception ex) { string name = "N/A"; /*if (r.Skin.SkinResourcePatch != null) * { * name = r.Skin.SkinResourcePatch.Geometry.Default; * }*/ Log.Warn(ex, $"Could not create geometry ({name}): {ex.ToString()} for player {Name}"); // File.WriteAllBytes(Path.Combine("/home/kenny/.config/Alex/skinDebug/failed", $"failed-{Environment.TickCount}.json"), Encoding.UTF8.GetBytes(skin.GeometryData)); } } else { Log.Warn($"Geometry data null for player {Name}"); } } if (model == null) { model = skin.Slim ? (EntityModel) new Models.HumanoidCustomslimModel() : (EntityModel) new Models.HumanoidCustomGeometryHumanoidModel(); } if (model != null && ValidateModel(model, Name)) { var modelRenderer = new EntityModelRenderer(model, TextureUtils.BitmapToTexture2D(Alex.Instance.GraphicsDevice, skinBitmap)); if (modelRenderer.Valid) { ModelRenderer = modelRenderer; } else { modelRenderer.Dispose(); Log.Warn($"Invalid model: for player {Name} (Disposing)"); } } else { Log.Warn($"Invalid model for player {Name}"); } }
public void LoadSkin(Skin skin) { Image <Rgba32> skinBitmap = null; if (!skin.TryGetBitmap(out skinBitmap)) { Log.Warn($"No custom skin data for player {Name}"); if (Alex.Instance.Resources.ResourcePack.TryGetBitmap("entity/alex", out var rawTexture)) { skinBitmap = rawTexture; } } EntityModel model = null; if (!skin.IsPersonaSkin) { if (!string.IsNullOrWhiteSpace(skin.GeometryData) && skin.GeometryData != "null") { 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); GeometryModel geometryModel = null; if (!GeometryModel.TryParse(skin.GeometryData, resourcePatch, out geometryModel)) { Log.Debug($"Failed to parse geometry for player {Name}"); } if (geometryModel == null || geometryModel.Geometry.Count == 0) { //Log.Warn($"!! Model count was 0 for player {Name} !!"); } else { if (resourcePatch?.Geometry != null) { model = geometryModel.FindGeometry(resourcePatch.Geometry.Default); if (model == null) { Log.Debug( $"Invalid geometry: {resourcePatch.Geometry.Default} for player {Name}"); } else { var modelTextureSize = new Point((int)model.Description.TextureWidth, (int)model.Description.TextureHeight); var textureSize = new Point(skinBitmap.Width, skinBitmap.Height); if (modelTextureSize != textureSize) { int newHeight = modelTextureSize.Y > textureSize.Y ? textureSize.Y : modelTextureSize.Y; int newWidth = modelTextureSize.X > textureSize.X ? textureSize.X: modelTextureSize.X; var bitmap = skinBitmap; bitmap.Mutate <Rgba32>(xx => { xx.Resize(modelTextureSize.X, modelTextureSize.Y); xx.Flip(FlipMode.Horizontal); }); skinBitmap = bitmap; } } } 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) { model = skin.Slim ? (EntityModel) new Models.HumanoidCustomslimModel() : (EntityModel) new HumanoidModel(); // new Models.HumanoidCustomGeometryHumanoidModel(); } if (model != null && ValidateModel(model, Name)) { 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}"); } }