public ContentPackAsset(ContentPackManager manager, ContentPackObject contentPack, StringWithTokens target, StringWithTokens file, List <TokenEntry> when, Rectangle fromArea, Rectangle toArea, bool overlay) { this.Manager = manager; this.ContentPack = contentPack; this.Target = target; this.File = file; this.When = when; this.Overlay = overlay; this.FromArea = fromArea; this.ToArea = toArea; foreach (string token in this.Target.GetTokenNames()) { this.Manager.DynamicTokenManager.RegisterAsset(token, this); } foreach (string token in this.File.GetTokenNames()) { this.Manager.DynamicTokenManager.RegisterAsset(token, this); } foreach (var entry in this.When) { this.Manager.DynamicTokenManager.RegisterAsset(entry.Name, this); } }
public Texture2D LoadTexture(string file, ContentPackObject contentPack) { if (!this.ContentPackTextures.TryGetValue(file, out Texture2D texture)) { GraphicsDevice device = HDSpritesMod.GraphicsDevice; if (device == null) { return(null); } string modFile = Path.Combine(contentPack.ModPath, file); string contentFile = Path.Combine(contentPack.ContentPath, file); if (FileExistsCaseInsensitive(contentFile, out contentFile)) { FileStream inStream = new FileStream(contentFile, FileMode.Open); texture = Texture2D.FromStream(device, inStream); } else { if (!FileExistsCaseInsensitive(modFile, out modFile)) { return(null); } FileStream inStream = new FileStream(modFile, FileMode.Open); Texture2D modTexture = Texture2D.FromStream(device, inStream); texture = Upscaler.Upscale(modTexture); Directory.CreateDirectory(contentFile.Substring(0, contentFile.Replace("/", "\\").LastIndexOf("\\"))); FileStream outStream = new FileStream(contentFile, FileMode.Create); texture.SaveAsPng(outStream, texture.Width, texture.Height); } this.ContentPackTextures.Add(file, texture); } return(texture); }
public void AddContentPack(ContentPackObject contentPack) { Dictionary <string, DynamicToken> configTokens = new Dictionary <string, DynamicToken>(); foreach (var entry in contentPack.Config) { DynamicToken token = new DynamicToken(entry.Key); token.AddValue(new TokenValue(entry.Value, true)); configTokens.Add(entry.Key, token); } if (contentPack.Config.Count < 1) { foreach (var token in contentPack.Content.ConfigSchema) { if (token.Value.Default != null) { contentPack.Config.Add(token.Key, token.Value.Default); } else if (token.Value.AllowValues != null) { contentPack.Config.Add(token.Key, token.Value.AllowValues.Split(',')[0]); } } } foreach (var dynamicToken in contentPack.Content.DynamicTokens) { if (!this.DynamicTokenManager.DynamicTokens.TryGetValue(dynamicToken.Name, out var token)) { token = new DynamicToken(dynamicToken.Name); this.DynamicTokenManager.AddToken(token); } List <TokenEntry> parsedWhen = new List <TokenEntry>(); foreach (var entry in dynamicToken.When) { parsedWhen.Add(new TokenEntry(entry.Key, entry.Value)); } List <TokenEntry> when = new List <TokenEntry>(); bool enabled = true; foreach (var entry in parsedWhen) { if (contentPack.Config.TryGetValue(entry.Name, out var value)) { if ((entry.IsConditional && entry.Condition.RawString.Equals(value) != value.ToLower().Equals("true")) || !entry.Values.Contains(value)) { enabled = false; break; } } else { when.Add(entry); } } token.AddValue(new DynamicTokenValue(dynamicToken.Value, enabled, this.DynamicTokenManager, when)); } foreach (var change in contentPack.Content.Changes) { if (!(change.Action.Equals("Load") || change.Action.Equals("EditImage")) || change.Enabled.ToLower().Equals("false") || !change.FromFile.ToLower().EndsWith(".png")) { continue; } List <TokenEntry> parsedWhen = new List <TokenEntry>(); foreach (var entry in change.When) { parsedWhen.Add(new TokenEntry(entry.Key, entry.Value)); } List <TokenEntry> when = new List <TokenEntry>(); bool enabled = true; foreach (var entry in parsedWhen) { if (contentPack.Config.TryGetValue(entry.Name, out var value)) { if ((entry.IsConditional && entry.Condition.Parse(this.DynamicTokenManager.DynamicTokens).Equals(value) != value.ToLower().Equals("true")) || !entry.Values.Contains(value)) { enabled = false; break; } } else { when.Add(entry); } } if (!enabled) { continue; } string[] targetSplit = change.Target.Split(','); foreach (string targetStr in targetSplit) { string targetStrFix = targetStr; if (targetStr.StartsWith(" ")) { targetStrFix = targetStr.Substring(1); } StringWithTokens target = new StringWithTokens(targetStrFix.Replace("/", $"\\")).Parse(configTokens); StringWithTokens file = new StringWithTokens(change.FromFile).Parse(configTokens); bool overlay = change.Patchmode.ToLower().Equals("overlay"); Rectangle fromArea = new Rectangle(change.FromArea.X * 2, change.FromArea.Y * 2, change.FromArea.Width * 2, change.FromArea.Height * 2); Rectangle toArea = new Rectangle(change.ToArea.X * 2, change.ToArea.Y * 2, change.ToArea.Width * 2, change.ToArea.Height * 2); ContentPackAsset asset = new ContentPackAsset(this, contentPack, target, file, when, fromArea, toArea, overlay); this.ContentPackAssets.Add(asset); } } }