123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
-
- using CommonLang;
- using CommonLang.IO;
- using NPOI.HSSF.UserModel;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using System;
- using System.IO;
- using XmdsCommonServer.XLS.Data;
- namespace XmdsCommonServer.XLS
- {
- public class XLSNPCDataLoader : XLSLoader
- {
- private HashMap<string, NPCData> NPCDatas = new HashMap<string, NPCData>();
- public XLSNPCDataLoader(string path)
- {
- byte[] data = Resource.LoadData(path);
- if ( data == null )
- {
- throw new Exception("Can not read xls file : " + path);
- }
- using ( MemoryStream ms = new MemoryStream(data) )
- {
- IWorkbook Workbook = null;
- if ( path.Contains(".xlsx") )
- {
- Workbook = new XSSFWorkbook(ms);
- }
- else if ( path.Contains(".xls") )
- {
- Workbook = new HSSFWorkbook(ms);
- }
- for ( int si = 0 ; si < Workbook.NumberOfSheets ; si++ )
- {
- ISheet sheet = Workbook.GetSheetAt(si) as ISheet;
- try
- {
- if ( sheet.SheetName == "NpcList" )
- {
- LoadSheet(sheet);
- }
- }
- catch ( Exception error )
- {
- log.Error(string.Format("XLSNPCDataLoader 初始化NPC配置错误SheetName = {0},Error = {1}",
- sheet.SheetName, error.ToString()));
- }
- }
- }
- log.Info("XLSMonsterDataLoader 初始化怪物配置完成.");
- }
- private void LoadSheet(ISheet sheet)
- {
- string name = null;
- int index = 0;
- NPCData temp = null;
- try
- {
- foreach ( NPCData m in LoadSheet<NPCData>(sheet) )
- {
- //name = string.Format("{0}_{1}", m.NpcID, m.Level);
- name = m.NpcID.ToString();
- temp = m;
- NPCDatas.Add(name, m);
- index++;
- }
- }
- catch ( Exception error )
- {
- throw new Exception(string.Format("XLSNPCDataLoader Error : index = {0} name = {1} Error = {2}", index, name, error.ToString()));
- }
- log.Info(string.Format("loadSheet【{0}】Complete", sheet.SheetName));
- }
- public NPCData GetNPCAbility(int unit_template_id, int unit_level)
- {
- NPCData ret = null;
- if ( !NPCDatas.TryGetValue(unit_template_id.ToString(), out ret) )
- {
- log.Error(string.Format("npc表不存在npc, GetNPCAbility Error: id = {0} level = {1}", unit_template_id, unit_level));
- }
- return ret;
- }
- }
- }
|