using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Media.Imaging; using LinePutScript; using LinePutScript.Dictionary; namespace VPet_Simulator.Windows.Interface { /// /// 资源集 /// public class Resources : LPS_D { public Resources() { } /// /// 添加资源,后来覆盖之前 /// /// 资源行 /// 功能位置 public void AddSource(ILine line, string modpath) { ISub source = line.Find("source"); if (source == null) return; //else if (!source.Info.Contains(":\\")) source.Info = modpath + '\\' + source.Info; line.Name = line.Name.ToLower(); AddorReplaceLine(line); } /// /// 添加资源,后来覆盖之前 /// /// 资源行 public void AddSource(ILine line) { ISub source = line.Find("source"); if (source == null) return; //else if (!source.Info.Contains(":\\")) line.Name = line.Name.ToLower(); AddorReplaceLine(line); } /// /// 添加多个资源,后来覆盖之前 /// /// 资源表 public void AddSources(ILPS lps) { foreach (ILine line in lps) { line.Name = line.Name.ToLower(); AddSource(line); } } /// /// 添加多个资源,后来覆盖之前 /// /// 资源表 /// 功能位置 public void AddSources(ILPS lps, string modpath = "") { foreach (ILine line in lps) { AddSource(line, modpath); } } /// /// 添加资源,后来覆盖之前的 /// /// 资源名字 /// 资源位置 public void AddSource(string name, string path) { AddorReplaceLine(new Line(name.ToLower(), "", "", new Sub("source", path))); } /// /// 查找资源 /// /// 资源名称 /// 如果未找到,退回这个值 /// 返回资源位置,如果未找到,则退回nofind public string FindSource(string name, string nofind = null) { ILine line = FindLine(name.ToLower()); if (line == null) return nofind; return line.Find("source").Info; } /// /// 查找资源 /// /// 资源名称 /// 如果未找到,退回这个值 /// 返回资源位置,如果未找到,则退回nofind public Uri FindSourceUri(string name, string nofind = null) { ILine line = FindLine(name.ToLower()); if (line == null) if (nofind != null) return new Uri(nofind); else return null; return new Uri(line.Find("source").Info); } } /// /// 图片资源集合 /// public class ImageResources : Resources { public ImageResources() { } /// /// 添加图片集,后来覆盖之前 /// /// 图片集 /// 文件夹位置 public void AddImages(LpsDocument lps, string modpath = "") => AddSources(lps, modpath); /// /// 添加单个图片,后来覆盖之前 /// /// 图片行 /// 文件夹位置 public void AddImage(ILine line, string modpath = "") => AddSource(line, modpath); /// /// 查找图片资源 /// /// 图片名称 /// 图片资源,如果未找到则退回错误提示图片 public BitmapImage FindImage(string imagename) => NewSafeBitmapImage(FindImageUri(imagename)); public Uri FindImageUri(string imagename) { #if DEBUGs var v = FindSourceUri(imagename, "pack://application:,,,/Res/Image/system/error.png"); if (v.AbsoluteUri == "pack://application:,,,/Res/Image/system/error.png") throw new Exception($"image nofound {imagename}"); return v; #else return FindSourceUri(imagename, "pack://application:,,,/Res/img/error.png"); #endif } /// /// 查找图片资源 如果找不到则使用上级 /// /// 图片名称 /// 图片资源,如果未找到则退回错误提示图片 /// 上级图片 如果没有专属的图片,则提供上级的图片 public BitmapImage FindImage(string imagename, string superior) { string source = FindSource(imagename); if (source == null) { return NewSafeBitmapImage(FindImageUri(superior)); } return NewSafeBitmapImage(source); } /// /// 图片设置 (eg:定位锚点等) /// public LpsDocument ImageSetting = new LpsDocument(); /// /// 更加安全的图片URI加载 /// /// 图片源 /// BitmapImage public static BitmapImage NewSafeBitmapImage(string source) => NewSafeBitmapImage(new Uri(source)); /// /// 更加安全的图片URI加载 /// /// 图片源 /// BitmapImage public static BitmapImage NewSafeBitmapImage(Uri source) { BitmapImage bi = new BitmapImage(); bi.BeginInit(); bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; bi.UriSource = source; bi.EndInit(); return bi; } } }