AppContents.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. using System.Collections.Generic;
  2. using System.Drawing;
  3. using System.IO;
  4. using NTERA.EmuEra.Game.EraEmu.Config;
  5. using NTERA.EmuEra.Game.EraEmu.Sub;
  6. namespace NTERA.EmuEra.Game.EraEmu.Content
  7. {
  8. static class AppContents
  9. {
  10. public static CroppedImage GetContent(string name)
  11. {
  12. if (name == null)
  13. return null;
  14. name = name.ToUpper();
  15. if (!itemDic.ContainsKey(name))
  16. return null;
  17. return itemDic[name];
  18. }
  19. public static void LoadContents()
  20. {
  21. if (!Directory.Exists(Program.ContentDir))
  22. return;
  23. try
  24. {
  25. List<string> bmpfilelist = new List<string>();
  26. bmpfilelist.AddRange(Directory.GetFiles(Program.ContentDir, "*.png", SearchOption.TopDirectoryOnly));
  27. bmpfilelist.AddRange(Directory.GetFiles(Program.ContentDir, "*.bmp", SearchOption.TopDirectoryOnly));
  28. bmpfilelist.AddRange(Directory.GetFiles(Program.ContentDir, "*.jpg", SearchOption.TopDirectoryOnly));
  29. bmpfilelist.AddRange(Directory.GetFiles(Program.ContentDir, "*.gif", SearchOption.TopDirectoryOnly));
  30. foreach (var filename in bmpfilelist)
  31. {//リスト化のみ。Loadはまだ
  32. string name = Path.GetFileName(filename).ToUpper();
  33. resourceDic.Add(name, new BaseImage(name, filename));
  34. }
  35. string[] csvFiles = Directory.GetFiles(Program.ContentDir, "*.csv", SearchOption.TopDirectoryOnly);
  36. foreach (var filename in csvFiles)
  37. {
  38. string[] lines = File.ReadAllLines(filename, Config.Config.Encode);
  39. foreach (var line in lines)
  40. {
  41. if (line.Length == 0)
  42. continue;
  43. string str = line.Trim();
  44. if (str.Length == 0 || str.StartsWith(";"))
  45. continue;
  46. string[] tokens = str.Split(',');
  47. CroppedImage item = CreateFromCsv(tokens);
  48. if (item != null && !itemDic.ContainsKey(item.Name))
  49. itemDic.Add(item.Name, item);
  50. }
  51. }
  52. }
  53. catch
  54. {
  55. throw new CodeEE("An error occurred while loading the resource file");
  56. }
  57. }
  58. public static void UnloadContents()
  59. {
  60. foreach (var img in resourceDic.Values)
  61. img.Dispose();
  62. resourceDic.Clear();
  63. itemDic.Clear();
  64. }
  65. private static CroppedImage CreateFromCsv(string[] tokens)
  66. {
  67. if(tokens.Length < 2)
  68. return null;
  69. string name = tokens[0].Trim().ToUpper();
  70. string parentName = tokens[1].ToUpper();
  71. if (name.Length == 0 || parentName.Length == 0)
  72. return null;
  73. if (!resourceDic.ContainsKey(parentName))
  74. return null;
  75. AContentFile parent = resourceDic[parentName];
  76. if(parent is BaseImage)
  77. {
  78. BaseImage parentImage = parent as BaseImage;
  79. parentImage.Load(Config.Config.TextDrawingMode == TextDrawingMode.WINAPI);
  80. if (!parentImage.Enabled)
  81. return null;
  82. Rectangle rect = new Rectangle(new Point(0, 0), parentImage.Bitmap.Size);
  83. bool noresize = false;
  84. if(tokens.Length >= 6)
  85. {
  86. int[] rectValue = new int[4];
  87. bool sccs = true;
  88. for (int i = 0; i < 4; i++)
  89. sccs &= int.TryParse(tokens[i + 2], out rectValue[i]);
  90. if (sccs)
  91. rect = new Rectangle(rectValue[0], rectValue[1], rectValue[2], rectValue[3]);
  92. if(tokens.Length >= 7)
  93. {
  94. string[] keywordTokens = tokens[6].Split('|');
  95. foreach(string keyword in keywordTokens)
  96. {
  97. switch(keyword.Trim().ToUpper())
  98. {
  99. case "NORESIZE":
  100. throw new NotImplCodeEE();
  101. noresize = true;
  102. break;
  103. }
  104. }
  105. }
  106. }
  107. CroppedImage image = new CroppedImage(name, parentImage.Bitmap, rect, noresize);
  108. return image;
  109. }
  110. return null;
  111. }
  112. static Dictionary<string, AContentFile> resourceDic = new Dictionary<string, AContentFile>();
  113. static Dictionary<string, CroppedImage> itemDic = new Dictionary<string, CroppedImage>();
  114. }
  115. }