/************************************************** * Data Structure initialization logic **************************************************/ private void GetSources() { OverlaySources.Clear(); foreach (IContentPack cp in Helper.ContentPacks.GetOwned()) { Log.Debug($"Reading content pack: {cp.Manifest.UniqueID}|v{cp.Manifest.Version} from {cp.DirectoryPath}"); Log.Trace($" Friendly name: {cp.Manifest.Name}"); if (!cp.HasFile("content.json")) { Log.Trace(" Does not have content.json, skipping"); continue; } ContentPackContent cp_content = cp.ReadJsonFile <ContentPackContent>("content.json"); if (cp_content is null) { Log.Trace(" Error parsing content.json, skipping"); continue; } foreach (var kvp in cp_content.overlays) { if (BuiltInOverlaySets.Contains(kvp.Key)) { Log.Trace($" Overwriting built-in overlay set '{kvp.Key}' is forbidden, skipping!"); continue; } if (OverlaySources.ContainsKey(kvp.Key)) { Log.Trace($" Overwriting previous overlay set '{kvp.Key}'"); } OverlaySources[kvp.Key] = new OverlaySource(cp, kvp.Value); } Log.Trace($" Added {cp_content.overlays.Count} overlays from {cp.Manifest.UniqueID}"); } foreach (var nm in BuiltInOverlaySets) { Log.Debug($"Adding built-in overlay set '{nm}'"); OverlaySources[nm] = new OverlaySource(nm); } }
private void LoadOverlay() { Log.Info($"Loading overlay '{Config.overlay}' from assets"); Thresholds.Clear(); Textures.Clear(); PreviousDispSize = new Point(); // This will force scale recalculation Dictionary <string, Texture2D> images = new Dictionary <string, Texture2D>(); OverlaySource source = OverlaySources[Config.overlay]; OverlayDefinition data = source.GetDefinition(); if (data is null) { Log.Error($"Cannot load overlay definition file {source.OverlayJsonFullPath}"); Log.Warn("Mod will not run!"); return; } foreach (var kvp in data.images) { string image_label = kvp.Key; string image_filename = kvp.Value; Log.Trace($"Loading image {image_label} => {source.AssetFullPath(image_filename)}"); try { images.Add(image_label, source.GetAsset <Texture2D>(image_filename)); } catch (Exception ex) { Log.Error($"Failed loading image {image_label} ; technical details:\n{ex}"); } } Log.Trace("Images loaded"); foreach (var kvp in data.textures) { string texture_label = kvp.Key.Trim(); if (texture_label == "") { Log.Error($"Invalid texture label, skipping!"); continue; } TextureDefinition texture_data = kvp.Value; Log.Trace($"Processing texture {texture_label} => {texture_data}"); if (!images.TryGetValue(texture_data.image, out Texture2D texturr)) { Log.Error($"Texture '{texture_label}' referring to non-existent image '{texture_data.image}', skipping!"); continue; } Rectangle _crop = texture_data.crop?.ToRectangle() ?? texturr.Bounds; //Color _basecolor = texture_data.blend_color?.ToColor() ?? Color.White; Color _basecolor = Color.White; var origin = new Vector2(_crop.Width >> 1, _crop.Height >> 1); var opacity = 1.0f - Math.Max(0.0f, Math.Min(1.0f, texture_data.transparency)); Textures.Add(texture_label, new TextureWithPost(texturr) { Crop = _crop, BlendColor = _basecolor * opacity, Rotation = texture_data.rotation, _Origin = origin // _Scale depends on viewport size, so it will be calculated in CalculateScales() }); } Log.Trace("Textures post-processing loaded"); foreach (var kvp in data.thresholds) { int hp = kvp.Key; string texture_label = kvp.Value; if (!Textures.TryGetValue(texture_label, out TextureWithPost twp)) { Log.Error($"Threshold {hp} refers to non-existent texture '{texture_label}', skipping!"); continue; } Thresholds.Add(hp, twp); } Log.Debug($"Sorted thresholds: {string.Join(", ", Thresholds.Keys)}"); }