XLSNPCDataLoader.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.IO;
  9. using XmdsCommonServer.XLS.Data;
  10. namespace XmdsCommonServer.XLS
  11. {
  12. public class XLSNPCDataLoader : XLSLoader
  13. {
  14. private HashMap<string, NPCData> NPCDatas = new HashMap<string, NPCData>();
  15. public XLSNPCDataLoader(string path)
  16. {
  17. byte[] data = Resource.LoadData(path);
  18. if ( data == null )
  19. {
  20. throw new Exception("Can not read xls file : " + path);
  21. }
  22. using ( MemoryStream ms = new MemoryStream(data) )
  23. {
  24. IWorkbook Workbook = null;
  25. if ( path.Contains(".xlsx") )
  26. {
  27. Workbook = new XSSFWorkbook(ms);
  28. }
  29. else if ( path.Contains(".xls") )
  30. {
  31. Workbook = new HSSFWorkbook(ms);
  32. }
  33. for ( int si = 0 ; si < Workbook.NumberOfSheets ; si++ )
  34. {
  35. ISheet sheet = Workbook.GetSheetAt(si) as ISheet;
  36. try
  37. {
  38. if ( sheet.SheetName == "NpcList" )
  39. {
  40. LoadSheet(sheet);
  41. }
  42. }
  43. catch ( Exception error )
  44. {
  45. log.Error(string.Format("XLSNPCDataLoader 初始化NPC配置错误SheetName = {0},Error = {1}",
  46. sheet.SheetName, error.ToString()));
  47. }
  48. }
  49. }
  50. log.Info("XLSMonsterDataLoader 初始化怪物配置完成.");
  51. }
  52. private void LoadSheet(ISheet sheet)
  53. {
  54. string name = null;
  55. int index = 0;
  56. NPCData temp = null;
  57. try
  58. {
  59. foreach ( NPCData m in LoadSheet<NPCData>(sheet) )
  60. {
  61. //name = string.Format("{0}_{1}", m.NpcID, m.Level);
  62. name = m.NpcID.ToString();
  63. temp = m;
  64. NPCDatas.Add(name, m);
  65. index++;
  66. }
  67. }
  68. catch ( Exception error )
  69. {
  70. throw new Exception(string.Format("XLSNPCDataLoader Error : index = {0} name = {1} Error = {2}", index, name, error.ToString()));
  71. }
  72. log.Info(string.Format("loadSheet【{0}】Complete", sheet.SheetName));
  73. }
  74. public NPCData GetNPCAbility(int unit_template_id, int unit_level)
  75. {
  76. NPCData ret = null;
  77. if ( !NPCDatas.TryGetValue(unit_template_id.ToString(), out ret) )
  78. {
  79. log.Error(string.Format("npc表不存在npc, GetNPCAbility Error: id = {0} level = {1}", unit_template_id, unit_level));
  80. }
  81. return ret;
  82. }
  83. }
  84. }