XLSMountainKingConfigLoader.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. 
  2. using CommonLang;
  3. using CommonLang.IO;
  4. using NPOI.HSSF.UserModel;
  5. using NPOI.SS.UserModel;
  6. using NPOI.XSSF.UserModel;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using XmdsCommonServer.XLS.Data;
  11. namespace XmdsCommonServer.XLS
  12. {
  13. public class XLSMountainKingConfigLoader : XLSLoader
  14. {
  15. //场景id,山大王
  16. private static HashMap<int, int> mMapMountainKing = new HashMap<int, int>();
  17. //山大王列表
  18. private static HashSet<int> mKings = new HashSet<int>();
  19. public XLSMountainKingConfigLoader(string path)
  20. {
  21. mMapMountainKing.Clear();
  22. byte[] data = Resource.LoadData(path);
  23. if (data == null)
  24. {
  25. throw new Exception("Can not read xls file : " + path);
  26. }
  27. using (MemoryStream ms = new MemoryStream(data))
  28. {
  29. IWorkbook Workbook = null;
  30. if (path.Contains(".xlsx"))
  31. {
  32. Workbook = new XSSFWorkbook(ms);
  33. }
  34. else if (path.Contains(".xls"))
  35. {
  36. Workbook = new HSSFWorkbook(ms);
  37. }
  38. for (int si = 0; si < Workbook.NumberOfSheets; si++)
  39. {
  40. ISheet sheet = Workbook.GetSheetAt(si) as ISheet;
  41. try
  42. {
  43. if (sheet.SheetName == "MountainKingConfig")
  44. {
  45. LoadSheet(sheet);
  46. }
  47. }
  48. catch (Exception error)
  49. {
  50. throw new Exception(string.Format("XLSRandomMonsterSkillLoader 初始化怪物配置错误SheetName = {0},Error = {1}",
  51. sheet.SheetName, error.ToString()));
  52. }
  53. }
  54. }
  55. log.Info("XLSMountainKingConfigLoader 初山大王场景配置完成.");
  56. }
  57. private void LoadSheet(ISheet sheet)
  58. {
  59. try
  60. {
  61. foreach (MountainKingConfig m in LoadSheet<MountainKingConfig>(sheet))
  62. {
  63. if(m.MapId == null || m.MapId.Length <= 0)
  64. {
  65. continue;
  66. }
  67. string[] mapIds = m.MapId.Split(';');
  68. foreach(string mapId in mapIds)
  69. {
  70. mMapMountainKing.Put(int.Parse(mapId), m.ID);
  71. }
  72. mKings.Add(m.ID);
  73. }
  74. }
  75. catch (Exception error)
  76. {
  77. throw new Exception(string.Format("XLSRandomMonsterSkillLoader Error : index = {0} Error = {1}", 0, error.ToString()));
  78. }
  79. log.Info(string.Format("loadSheet【{0}】Complete", sheet.SheetName));
  80. }
  81. public HashSet<int> GetMountainKings()
  82. {
  83. return mKings;
  84. }
  85. public int GetMapKing(int mapId)
  86. {
  87. return mMapMountainKing.Get(mapId);
  88. }
  89. }
  90. }