148 lines
4.7 KiB
C#
148 lines
4.7 KiB
C#
using System;
|
|
using System.Text;
|
|
using Fantasy.Exporter;
|
|
using Fantasy.Tools.ConfigTable;
|
|
using System.Collections.Generic;
|
|
|
|
namespace Exporter;
|
|
|
|
public class ConstValueToConst : ACustomExport
|
|
{
|
|
private class ConstValueInfo
|
|
{
|
|
public string Name;
|
|
public string Type;
|
|
public string Value;
|
|
public string Remark;
|
|
}
|
|
[Flags]
|
|
private enum ExportType
|
|
{
|
|
None = 0,
|
|
Server = 1,
|
|
Client = 1 << 1,
|
|
All = Server | Client
|
|
}
|
|
public override void Run()
|
|
{
|
|
if (!ExcelExporter.IgnoreTable.TryGetValue("#ConstValue", out var constValuePath))
|
|
{
|
|
Log.Error("ConstValue is null");
|
|
return;
|
|
}
|
|
|
|
var serverDic = new List<ConstValueInfo>();
|
|
var clientDic = new List<ConstValueInfo>();
|
|
var excelWorksheet = ExcelExporter.LoadExcel(constValuePath, false);
|
|
|
|
for (int row = 2; row <= excelWorksheet.Dimension.Rows; row++)
|
|
{
|
|
var exportType = ExportType.All;
|
|
var exportTypeStr = excelWorksheet.GetCellValue(row, 1);
|
|
|
|
if (!string.IsNullOrEmpty(exportTypeStr))
|
|
{
|
|
exportType = ExportType.None;
|
|
exportTypeStr = exportTypeStr.ToUpper();
|
|
|
|
if (exportTypeStr.Contains("S"))
|
|
{
|
|
exportType |= ExportType.Server;
|
|
}
|
|
|
|
if (exportTypeStr.Contains("C"))
|
|
{
|
|
exportType |= ExportType.Client;
|
|
}
|
|
}
|
|
|
|
var constValueInfo = new ConstValueInfo()
|
|
{
|
|
Name = excelWorksheet.GetCellValue(row, 2),
|
|
Type = excelWorksheet.GetCellValue(row, 3),
|
|
Value = excelWorksheet.GetCellValue(row, 4),
|
|
Remark = excelWorksheet.GetCellValue(row, 5)
|
|
};
|
|
|
|
if (exportType.HasFlag(ExportType.Server))
|
|
{
|
|
serverDic.Add(constValueInfo);
|
|
}
|
|
|
|
if (exportType.HasFlag(ExportType.Client))
|
|
{
|
|
clientDic.Add(constValueInfo);
|
|
}
|
|
}
|
|
|
|
if (serverDic.Count > 0)
|
|
{
|
|
Write(serverDic, CustomExportType.Server);
|
|
}
|
|
|
|
if (clientDic.Count > 0)
|
|
{
|
|
Write(clientDic, CustomExportType.Client);
|
|
}
|
|
}
|
|
|
|
private void Write(List<ConstValueInfo> dic, CustomExportType customExportType)
|
|
{
|
|
var strBuilder = new StringBuilder();
|
|
strBuilder.AppendLine("namespace Fantasy\n{");
|
|
strBuilder.AppendLine("\t// 生成器自动生成,请不要手动编辑,修改请在#ConstValue.xsl里。");
|
|
strBuilder.AppendLine("\tpublic partial class ConstValue\n\t{");
|
|
foreach (var constValueInfo in dic)
|
|
{
|
|
var remark = string.IsNullOrEmpty(constValueInfo.Remark) ? constValueInfo.Name : constValueInfo.Remark;
|
|
strBuilder.AppendLine($"\t\t/// <summary>\n\t\t/// {remark}\n\t\t/// </summary>");
|
|
strBuilder.AppendLine(
|
|
$"\t\tpublic const {constValueInfo.Type} {constValueInfo.Name} = {DefaultValue(constValueInfo.Type, constValueInfo.Value)};");
|
|
}
|
|
strBuilder.AppendLine("\t}\n}");
|
|
Write("ConstValue.cs", strBuilder.ToString(), customExportType);
|
|
}
|
|
|
|
private static string DefaultValue(string type, string value)
|
|
{
|
|
switch (type)
|
|
{
|
|
case "byte[]":
|
|
case "int[]":
|
|
case "long[]":
|
|
case "string[]":
|
|
case "double[]":
|
|
case "float[]":
|
|
return $"new {type} {{{value}}}";
|
|
case "byte[,]":
|
|
case "int[,]":
|
|
case "long[,]":
|
|
case "string[,]":
|
|
case "float[,]":
|
|
case "double[,]":
|
|
return $"new {type} {{{value}}}";
|
|
case "int":
|
|
case "bool":
|
|
case "uint":
|
|
case "long":
|
|
case "double":
|
|
return $"{value}";
|
|
case "float":
|
|
return value[^1] == 'f' ? value : $"{value}f";
|
|
case "string":
|
|
return $"\"{value}\"";
|
|
case "Vector2":
|
|
{
|
|
var strings = value.Split(',', StringSplitOptions.TrimEntries);
|
|
return $"new Vector2({strings[0]},{strings[1]})";
|
|
}
|
|
case "Vector3":
|
|
{
|
|
var strings = value.Split(',', StringSplitOptions.TrimEntries);
|
|
return $"new Vector3({strings[0]},{strings[1]},{strings[2]})";
|
|
}
|
|
default:
|
|
throw new Exception($"不支持此类型: {type}");
|
|
}
|
|
}
|
|
} |