生成结果:
在ET中,数值组件由5个附加值决定最终结果,当然你也可以更改其公式
public void Update(NumericType numericType)
{
if (numericType < NumericType.Max)
{
return;
}
int final = (int)numericType / 10;
int bas = final * 10 + 1;
int add = final * 10 + 2;
int pct = final * 10 + 3;
int finalAdd = final * 10 + 4;
int finalPct = final * 10 + 5;
// 一个数值可能会多种情况影响,比如速度,加个buff可能增加速度绝对值100,也有些buff增加10%速度,所以一个值可以由5个值进行控制其最终结果
// final = (((base + add) * (100 + pct) / 100) + finalAdd) * (100 + finalPct) / 100;
long result = (long)(((this.GetByKey(bas) + this.GetByKey(add)) * (100 + this.GetAsFloat(pct)) / 100f + this.GetByKey(finalAdd)) * (100 + this.GetAsFloat(finalPct)) / 100f * 10000);
this.NumericDic[final] = result;
}
最终值 = (( 基础值 + 固定值 ) X 百分比 + 最终固定值 ) X 最终百分比
举个栗子:一个简单攻击速度就需要以下 6 个值名称
AttackSpeed = 1000
AttackSpeedBase = AttackSpeed * 10 + 1,
AttackSpeedAdd = AttackSpeed * 10 + 2,
AttackSpeedPct = AttackSpeed * 10 + 3,
AttackSpeedFinalAdd = AttackSpeed * 10 + 4,
AttackSpeedFinalPct = AttackSpeed * 10 + 5,
如果手动复制粘贴简直是灾难。我们可以简单写一个生成工具,首先是把需要生成的属性写到excel
ID:属性的ID
Name:属性的名称
IsOperation:是否需要计算这个值(有些属性是不需要计算的,生成一个属性就够了)
Label:最后来个注释,方便访问的
准备好excel后就可以读取excel生成对应的类文件,在框架中已经集成了NPOI,直接使用它读取excel即可,NPOI有个小毛病,最后一行读不了。一定要在最后多加一行空行
using System;
using System.Collections.Generic;
using NPOI.XSSF.UserModel;
using NPOI.XWPF.UserModel;
using System.IO;
namespace ETModel
{
public static class ExcelHelper
{
public static List<string[]> ReadExcel(string path)
{
try
{
if (!File.Exists(path))
{
throw new Exception($"{path} 不存在");
}
XSSFWorkbook xssfWorkbook;
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
xssfWorkbook = new XSSFWorkbook(file);
}
var iSheet = xssfWorkbook.GetSheetAt(0);
int colCount = iSheet.GetRow(1).LastCellNum;
List<string[]> result = new List<string[]>();
var list = new List<string>(colCount);
for (int row = 0; row < iSheet.LastRowNum; row++)
{
var iRow = iSheet.GetRow(row);
for (int col = 0; col < colCount; col++)
{
if (iRow != null)
{
var cell = iRow.GetCell(col);
if (cell != null)
{
list.Add(cell.ToString() ?? string.Empty);
}
}
}
if (list.Count == 0)
continue;
result.Add(list.ToArray());
list.Clear();
}
return result;
}
catch (Exception ex)
{
throw ex;
}
}
public static void WriteExcel(string path, List<string[]> table)
{
XSSFWorkbook workbook = null;
FileStream fs = null;
try
{
workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("Sheet");
for (int i = 0; i < table.Count; i++)
{
var row = sheet.CreateRow(i);
var rowData = table[i];
for (int j = 0; j < rowData.Length; j++)
{
var cell = row.CreateCell(j);
var cellData = rowData[j];
cell.SetCellValue(cellData);
}
}
using (fs = File.OpenWrite(path))
{
workbook.Write(fs);
}
}
catch
{
workbook?.Close();
fs?.Close();
}
}
public static void ReplaceWord(string filePath, string savePath, Dictionary<string, string> dict)
{
if (!File.Exists(filePath))
throw new Exception($"Could not find file : {filePath}");
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
XWPFDocument doc = new XWPFDocument(fs);
var tables = doc.Tables;
foreach (var table in tables)
{
foreach (var row in table.Rows)
{
foreach (var cell in row.GetTableCells())
{
foreach (var para in cell.Paragraphs)
{
var oldString = para.ParagraphText;
var newString = oldString;
foreach (var item in dict)
{
newString = newString.Replace(item.Key, item.Value);
}
para.ReplaceText(oldString, newString);
}
}
}
}
using (FileStream output = new FileStream(savePath, FileMode.Create))
{
doc.Write(output);
}
}
}
}
}
现在你已经可以读取excel,那么就来生成文件
using System.IO;
using System.Text;
namespace ETModel
{
public class NumericHelper
{
const string start = "// This is an automatically generated class by tools. Please do not modify it.\r\n" +
"namespace ETModel{\r\n" +
"\tpublic enum NumericType{\r\n" +
"\t\tMax = 10000,\r\n";
const string end = "\t}\r\n}\r\n";
public static void Generate(string excelPath, string targetPath)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(start);
var table = ExcelHelper.ReadExcel(excelPath);
table.RemoveAt(0);
int id;
bool isOperation;
foreach (var line in table)
{
if (line.Length != 4)
continue;
if(!int.TryParse(line[0],out id))
continue;
if (!bool.TryParse(line[2], out isOperation))
continue;
var name = line[1];
stringBuilder.AppendLine($"\t\t/// <summary>");
stringBuilder.AppendLine($"\t\t/// {line[3]}");
stringBuilder.AppendLine($"\t\t/// </summary>");
stringBuilder.AppendLine($"\t\t{name}={id},");
if (isOperation)
{
stringBuilder.AppendLine($"\t\t{name}Base = {name} * 10 + 1,");
stringBuilder.AppendLine($"\t\t{name}Add = {name} * 10 + 2,");
stringBuilder.AppendLine($"\t\t{name}Pct = {name} * 10 + 3,");
stringBuilder.AppendLine($"\t\t{name}FinalAdd = {name} * 10 + 4,");
stringBuilder.AppendLine($"\t\t{name}FinalPct = {name} * 10 + 5,");
}
stringBuilder.AppendLine();
}
stringBuilder.Append(end);
File.WriteAllText(targetPath, stringBuilder.ToString(),Encoding.UTF8);
}
}
}