【重构】为与LubanAssistant共享代码而略微调整了一些.net5与.net 4.7 之间不兼容的代码

main
walon 2021-10-11 23:05:16 +08:00
parent 2f9c970730
commit 8288591827
28 changed files with 148 additions and 90 deletions

View File

@ -23,13 +23,21 @@ namespace Luban.Common.Utils
public static string GetFileName(string path)
{
int index = path.Replace('\\', '/').LastIndexOf('/');
#if !LUBAN_ASSISTANT
return index >= 0 ? path[(index + 1)..] : path;
#else
return index >= 0 ? path.Substring(index + 1, path.Length - index - 1) : path;
#endif
}
public static string GetParent(string path)
{
int index = path.Replace('\\', '/').LastIndexOf('/');
#if !LUBAN_ASSISTANT
return index >= 0 ? path[..index] : ".";
#else
return index >= 0 ? path.Substring(0, index) : ".";
#endif
}
public static string GetFileNameWithoutExt(string file)
@ -62,7 +70,11 @@ namespace Luban.Common.Utils
}
var f = new FileInfo(file);
string fname = f.Name;
#if !LUBAN_ASSISTANT
return !fname.StartsWith('.') && !fname.StartsWith('_') && !fname.StartsWith('~');
#else
return !fname.StartsWith(".") && !fname.StartsWith("_") && !fname.StartsWith("~");
#endif
}
[ThreadStatic]
@ -100,6 +112,45 @@ namespace Luban.Common.Utils
return Standardize(Path.Combine(parent, sub));
}
public static bool IsExcelFile(string fullName)
{
return fullName.EndsWith(".csv", StringComparison.Ordinal)
|| fullName.EndsWith(".xls", StringComparison.Ordinal)
|| fullName.EndsWith(".xlsx", StringComparison.Ordinal);
}
public static (string, string) SplitFileAndSheetName(string url)
{
int sheetSepIndex = url.IndexOf('@');
if (sheetSepIndex < 0)
{
return (url, null);
}
else
{
int lastPathSep = url.LastIndexOf('/', sheetSepIndex);
#if !LUBAN_ASSISTANT
if (lastPathSep >= 0)
{
return (url[0..(lastPathSep + 1)] + url[(sheetSepIndex + 1)..], url[(lastPathSep + 1)..sheetSepIndex]);
}
else
{
return (url[(sheetSepIndex + 1)..], url[(lastPathSep + 1)..sheetSepIndex]);
}
#else
if (lastPathSep >= 0)
{
return (url.Substring(0, lastPathSep + 1) + url.Substring(sheetSepIndex + 1), url.Substring(lastPathSep + 1, sheetSepIndex - lastPathSep - 1));
}
else
{
return (url.Substring(sheetSepIndex + 1), url.Substring(lastPathSep + 1, sheetSepIndex - lastPathSep - 1));
}
#endif
}
}
public static async Task SaveFileAsync(string relateDir, string filePath, byte[] content)
{
// 调用此接口时,已保证 文件必然是改变的,不用再检查对比文件
@ -131,7 +182,12 @@ namespace Luban.Common.Utils
s_logger.Info("[new] {file}", outputPath);
}
#if !LUBAN_ASSISTANT
await File.WriteAllBytesAsync(outputPath, content);
#else
await Task.Run(() => File.WriteAllBytes(outputPath, content));
#endif
}
public static async Task<byte[]> ReadAllBytesAsync(string file)

View File

@ -113,7 +113,7 @@ namespace Luban.Job.Cfg.Cache
cacheList.Sort((a, b) => a.Value.LastAccessTime - b.Value.LastAccessTime);
for (int i = 0; i < CACHE_FILE_HIGH_WATER_MARK - CACHE_FILE_LOW_WATER_MARK; i++)
{
_caches.Remove(cacheList[i].Key, out _);
_caches.TryRemove(cacheList[i].Key, out _);
}
s_logger.Info("ShrinkCaches. after shrink, cache file num:{}", _caches.Count);
}

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Common.Utils;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources.Excel;

View File

@ -317,11 +317,11 @@ namespace Luban.Job.Cfg.DataCreators
public DType Accept(TMap type, Sheet.NamedRow x, bool multirow, bool nullable)
{
var map = new Dictionary<DType, DType>();
foreach (var (key, keyTitle) in x.Titles)
foreach (var e in x.Titles)
{
if (TryCreateColumnStream(x, keyTitle, out var stream))
if (TryCreateColumnStream(x, e.Value, out var stream))
{
var keyData = type.KeyType.Apply(StringDataCreator.Ins, key);
var keyData = type.KeyType.Apply(StringDataCreator.Ins, e.Key);
var valueData = type.ValueType.Apply(ExcelDataCreator.Ins, null, stream, DefAssembly.LocalAssebmly);
map.Add(keyData, valueData);
}

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Common.Utils;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Common.Utils;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Common.Utils;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.Defs;

View File

@ -18,19 +18,7 @@ namespace Luban.Job.Cfg.DataSources.Excel
private System.Text.Encoding DetectCsvEncoding(Stream fs)
{
Ude.CharsetDetector cdet = new Ude.CharsetDetector();
cdet.Feed(fs);
cdet.DataEnd();
fs.Seek(0, SeekOrigin.Begin);
if (cdet.Charset != null)
{
s_logger.Debug("Charset: {}, confidence: {}", cdet.Charset, cdet.Confidence);
return System.Text.Encoding.GetEncoding(cdet.Charset) ?? System.Text.Encoding.Default;
}
else
{
return System.Text.Encoding.Default;
}
return System.Text.Encoding.Default;
}
public override void Load(string rawUrl, string sheetName, Stream stream)

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using ExcelDataReader;
using Luban.Job.Cfg.DataCreators;
using Luban.Job.Cfg.Datas;

View File

@ -1,5 +1,4 @@
using Luban.Job.Cfg.Utils;
using System.Collections.Generic;
using System.Collections.Generic;
namespace Luban.Job.Cfg.Datas
{

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Common.Utils;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.DataSources.Excel;
@ -36,7 +37,7 @@ namespace Luban.Job.Cfg.Defs
private readonly List<string> _defaultGroups = new List<string>();
public CfgDefLoader(RemoteAgent agent) : base(agent)
public CfgDefLoader(IAgent agent) : base(agent)
{
RegisterRootDefineHandler("importexcel", AddImportExcel);
RegisterRootDefineHandler("patch", AddPatch);
@ -139,14 +140,22 @@ namespace Luban.Job.Cfg.Defs
{
if (!string.IsNullOrWhiteSpace(attr))
{
#if !LUBAN_ASSISTANT
foreach (var validatorStr in attr.Split('#', StringSplitOptions.RemoveEmptyEntries))
#else
foreach (var validatorStr in attr.Split('#'))
#endif
{
var sepIndex = validatorStr.IndexOf(':');
if (sepIndex < 0)
if (sepIndex <= 0)
{
throw new Exception($"定义文件:{defineFile} key:'{key}' attr:'{attr}' 不是合法的 validator 定义 (key1:value1#key2:value2 ...)");
}
#if !LUBAN_ASSISTANT
result.Add(new Validator() { Type = validatorStr[..sepIndex], Rule = validatorStr[(sepIndex + 1)..] });
#else
result.Add(new Validator() { Type = validatorStr.Substring(0, sepIndex), Rule = validatorStr.Substring(sepIndex + 1, validatorStr.Length - sepIndex - 1) });
#endif
}
}
}
@ -356,7 +365,11 @@ namespace Luban.Job.Cfg.Defs
for (int i = 1; i < attrs.Length; i++)
{
#if !LUBAN_ASSISTANT
var pair = attrs[i].Split('=', 2);
#else
var pair = attrs[i].Split(new char[] { '=' }, 2);
#endif
if (pair.Length != 2)
{
throw new Exception($"table:'{table.Name}' file:{file.OriginFile} title:'{f.Name}' attr:'{attrs[i]}' is invalid!");

View File

@ -7,24 +7,5 @@ namespace Luban.Job.Cfg.Defs
public abstract class CfgDefTypeBase : DefTypeBase
{
public DefAssembly Assembly => (DefAssembly)AssemblyBase;
public virtual string UeBpName => "U" + Name;
public virtual string UeBpFullName => TypeUtil.MakeCppJoinedFullName("U" + Namespace, Name);
public string UeBpHeaderFileName => "bp_" + RenderFileUtil.GetUeCppDefTypeHeaderFilePath(FullName);
public string UeBpHeaderFileNameWithoutSuffix => "bp_" + RenderFileUtil.GetUeCppDefTypeHeaderFilePathWithoutSuffix(FullName);
public string EditorUeFullName => TypeUtil.MakeCppFullName(Namespace, Name);
public string UeFname => "F" + Name;
public string UeFfullName => TypeUtil.MakeCppFullName(Namespace, UeFname);
public string UeHeaderFileName => RenderFileUtil.GetUeCppDefTypeHeaderFilePath(FullName);
public string UeEditorHeaderFileName => "editor_" + RenderFileUtil.GetUeCppDefTypeHeaderFilePath(FullName);
}
}

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Job.Cfg.Datas;
using Luban.Job.Cfg.l10n;
using Luban.Job.Cfg.RawDefs;
@ -43,7 +44,7 @@ namespace Luban.Job.Cfg.Defs
public TimeZoneInfo TimeZone { get; }
public DefAssembly(string patchName, TimeZoneInfo timezone, List<string> excludeTags, RemoteAgent agent)
public DefAssembly(string patchName, TimeZoneInfo timezone, List<string> excludeTags, IAgent agent)
{
this._patchName = patchName;
this.TimeZone = timezone;
@ -153,11 +154,11 @@ namespace Luban.Job.Cfg.Defs
throw new Exception($"service:'{targetService.Name}' ref:'{refType}' 重复引用");
}
}
foreach ((var fullTypeName, var type) in this.Types)
foreach (var e in this.Types)
{
if (!refTypes.ContainsKey(fullTypeName) && (type is DefEnum))
if (!refTypes.ContainsKey(e.Key) && (e.Value is DefEnum))
{
refTypes.Add(fullTypeName, type);
refTypes.Add(e.Key, e.Value);
}
}

View File

@ -31,6 +31,7 @@ namespace Luban.Job.Cfg.Defs
return DeepCompareTypeDefine.Ins.Compare(this, b, new Dictionary<DefTypeBase, bool>(), new HashSet<DefTypeBase>());
}
#if !LUBAN_ASSISTANT
public string GoBinImport
{
get
@ -129,6 +130,7 @@ namespace Luban.Job.Cfg.Defs
throw new NotImplementedException();
}
}
#endif
public DefBean(CfgBean b) : base(b)
{

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Job.Cfg.RawDefs;
using Luban.Job.Common.Types;
using Luban.Job.Common.Utils;

View File

@ -73,7 +73,7 @@ namespace Luban.Job.Cfg.Generate
postContent?.Invoke(fileContent);
var file = outputFile;
var md5 = CacheFileUtil.GenMd5AndAddCache(file, string.Join('\n', fileContent));
var md5 = CacheFileUtil.GenMd5AndAddCache(file, string.Join("\n", fileContent));
ctx.GenCodeFilesInOutputCodeDir.Add(new FileInfo() { FilePath = file, MD5 = md5 });
}));
}

View File

@ -104,7 +104,9 @@ namespace Luban.Job.Cfg.TypeVisitors
|| f1.NeedExport != f2.NeedExport
|| f1.Index != f2.Index
|| f1.Sep != f2.Sep
#if !LUBAN_ASSISTANT
|| f1.ResourceTag != f2.ResourceTag
#endif
|| f1.IsMultiRow != f2.IsMultiRow
|| f1.CType.IsNullable != f2.CType.IsNullable
|| f1.CType.GetType() != f2.CType.GetType()

View File

@ -1,3 +1,4 @@
using Bright.Collections;
using Luban.Job.Cfg.Defs;
using Luban.Job.Common.Defs;
using Luban.Job.Common.Types;

View File

@ -32,12 +32,12 @@ namespace Luban.Job.Cfg.Utils
public string SheetName { get; set; }
}
public static async Task<List<InputFileInfo>> CollectInputFilesAsync(RemoteAgent agent, IEnumerable<string> files, string dataDir)
public static async Task<List<InputFileInfo>> CollectInputFilesAsync(IAgent agent, IEnumerable<string> files, string dataDir)
{
var collectTasks = new List<Task<List<InputFileInfo>>>();
foreach (var file in files)
{
(var actualFile, var sheetName) = RenderFileUtil.SplitFileAndSheetName(FileUtil.Standardize(file));
(var actualFile, var sheetName) = FileUtil.SplitFileAndSheetName(FileUtil.Standardize(file));
var actualFullPath = FileUtil.Combine(dataDir, actualFile);
var originFullPath = FileUtil.Combine(dataDir, file);
//s_logger.Info("== get input file:{file} actualFile:{actual}", file, actualFile);
@ -71,7 +71,7 @@ namespace Luban.Job.Cfg.Utils
// return CollectInputFilesAsync(agent, table.InputFiles, dataDir)
//}
public static async Task GenerateLoadRecordFromFileTasksAsync(RemoteAgent agent, DefTable table, string dataDir, List<string> inputFiles2, List<Task<List<Record>>> tasks)
public static async Task GenerateLoadRecordFromFileTasksAsync(IAgent agent, DefTable table, string dataDir, List<string> inputFiles2, List<Task<List<Record>>> tasks)
{
var inputFileInfos = await CollectInputFilesAsync(agent, inputFiles2, dataDir);
@ -93,7 +93,7 @@ namespace Luban.Job.Cfg.Utils
file.OriginFile,
file.SheetName,
await agent.GetFromCacheOrReadAllBytesAsync(file.ActualFile, file.MD5),
RenderFileUtil.IsExcelFile(file.ActualFile));
FileUtil.IsExcelFile(file.ActualFile));
FileRecordCacheManager.Ins.AddCacheLoadedRecords(table, file.MD5, file.SheetName, res);
@ -102,7 +102,7 @@ namespace Luban.Job.Cfg.Utils
}
}
public static async Task LoadTableAsync(RemoteAgent agent, DefTable table, string dataDir, string patchName, string patchDataDir)
public static async Task LoadTableAsync(IAgent agent, DefTable table, string dataDir, string patchName, string patchDataDir)
{
var mainLoadTasks = new List<Task<List<Record>>>();
var mainGenerateTask = GenerateLoadRecordFromFileTasksAsync(agent, table, dataDir, table.InputFiles, mainLoadTasks);
@ -145,7 +145,7 @@ namespace Luban.Job.Cfg.Utils
s_logger.Trace("table:{name} record num:{num}", table.FullName, mainRecords.Count);
}
public static async Task LoadCfgDataAsync(RemoteAgent agent, DefAssembly ass, string dataDir, string patchName, string patchDataDir)
public static async Task LoadCfgDataAsync(IAgent agent, DefAssembly ass, string dataDir, string patchName, string patchDataDir)
{
var ctx = agent;
List<DefTable> exportTables = ass.Types.Values.Where(t => t is DefTable ct && ct.NeedExport).Select(t => (DefTable)t).ToList();
@ -200,7 +200,7 @@ namespace Luban.Job.Cfg.Utils
}
}
public static async Task LoadTextTablesAsync(RemoteAgent agent, DefAssembly ass, string baseDir, string textTableFiles)
public static async Task LoadTextTablesAsync(IAgent agent, DefAssembly ass, string baseDir, string textTableFiles)
{
var tasks = new List<Task<byte[]>>();
var files = textTableFiles.Split(',');

View File

@ -58,7 +58,7 @@ namespace Luban.Job.Cfg.Utils
};
public static DDateTime CreateDateTime(string x)
{
DateTime dateTime = DateTime.ParseExact(x, dateTimeFormats, System.Globalization.CultureInfo.InvariantCulture);
DateTime dateTime = DateTime.ParseExact(x, dateTimeFormats, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);
return new DDateTime(dateTime);
}

View File

@ -16,7 +16,11 @@ namespace Luban.Job.Cfg.Validators
public static string GetActualTableName(string table)
{
#if !LUBAN_ASSISTANT
return table.EndsWith("?") ? table[0..^1] : table;
#else
return table.EndsWith("?") ? table.Substring(0, table.Length - 1) : table;
#endif
}
public RefValidator(List<string> tables)
@ -40,7 +44,11 @@ namespace Luban.Job.Cfg.Validators
if (table.EndsWith("?"))
{
zeroAble = true;
#if !LUBAN_ASSISTANT
actualTable = table[0..^1];
#else
actualTable = table.Substring(0, table.Length - 1);
#endif
}
else
{
@ -64,7 +72,11 @@ namespace Luban.Job.Cfg.Validators
string actualTable;
if (table.EndsWith("?"))
{
#if !LUBAN_ASSISTANT
actualTable = table[0..^1];
#else
actualTable = table.Substring(0, table.Length - 1);
#endif
}
else
{
@ -85,7 +97,11 @@ namespace Luban.Job.Cfg.Validators
foreach (var table in Tables)
{
#if !LUBAN_ASSISTANT
string actualTable = table.EndsWith("?") ? table[0..^1] : table;
#else
string actualTable = table.EndsWith("?") ? table.Substring(0, table.Length - 1) : table;
#endif
var ct = def.Assembly.GetCfgTable(actualTable);
if (ct == null)
{

View File

@ -32,7 +32,7 @@ namespace Luban.Job.Common.Defs
{
private static readonly NLog.Logger s_logger = NLog.LogManager.GetCurrentClassLogger();
protected RemoteAgent Agent { get; }
protected IAgent Agent { get; }
public string RootDir { get; private set; }
@ -50,7 +50,7 @@ namespace Luban.Job.Common.Defs
protected readonly List<PEnum> _enums = new List<PEnum>();
protected readonly List<Bean> _beans = new List<Bean>();
protected CommonDefLoader(RemoteAgent agent)
protected CommonDefLoader(IAgent agent)
{
Agent = agent;

View File

@ -32,7 +32,7 @@ namespace Luban.Job.Common.Defs
public Dictionary<string, DefTypeBase> Types { get; } = new Dictionary<string, DefTypeBase>();
public RemoteAgent Agent { get; protected set; }
public IAgent Agent { get; protected set; }
public string TopModule { get; protected set; }
@ -117,7 +117,11 @@ namespace Luban.Job.Common.Defs
public TType CreateType(string module, string type)
{
#if LUBAN_ASSISTANT
int sepIndex = type.IndexOf(',');
#else
int sepIndex = type.IndexOf(',', System.StringComparison.Ordinal);
#endif
if (sepIndex > 0)
{
string containerType = type.Substring(0, sepIndex).Trim();
@ -134,14 +138,22 @@ namespace Luban.Job.Common.Defs
bool nullable;
var (type, tags) = DefUtil.ParseType(rawType);
#if !LUBAN_ASSISTANT
if (type.EndsWith('?'))
#else
if (type.EndsWith("?"))
#endif
{
if (!SupportNullable)
{
throw new Exception($"not support nullable type:'{module}.{type}'");
}
nullable = true;
#if !LUBAN_ASSISTANT
type = type[0..^1];
#else
type = type.Substring(0, type.Length - 1);
#endif
}
else
{

View File

@ -3,6 +3,7 @@ using Luban.Job.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using Bright.Collections;
namespace Luban.Job.Common.Defs
{
@ -11,19 +12,19 @@ namespace Luban.Job.Common.Defs
{
public class Item
{
public string Name { get; init; }
public string Name { get; set; }
public string Value { get; set; }
public string Alias { get; init; }
public string Alias { get; set; }
public string AliasOrName => string.IsNullOrWhiteSpace(Alias) ? Name : Alias;
public int IntValue { get; set; }
public string Comment { get; init; }
public string Comment { get; set; }
public Dictionary<string, string> Tags { get; init; }
public Dictionary<string, string> Tags { get; set; }
public bool HasTag(string attrName)
{

View File

@ -12,7 +12,7 @@ namespace Luban.Job.Common.Defs
public string TopModule => AssemblyBase.TopModule;
public RemoteAgent Agent => AssemblyBase.Agent;
public IAgent Agent => AssemblyBase.Agent;
public string Name { get; set; }

View File

@ -21,7 +21,11 @@ namespace Luban.Job.Common.Utils
int sepIndex = pair.IndexOfAny(s_attrKeyValueSep);
if (sepIndex >= 0)
{
#if !LUBAN_ASSISTANT
am.Add(pair[..sepIndex].Trim(), pair[(sepIndex + 1)..].Trim());
#else
am.Add(pair.Substring(0, sepIndex).Trim(), pair.Substring(sepIndex + 1).Trim());
#endif
}
else
{
@ -40,7 +44,11 @@ namespace Luban.Job.Common.Utils
}
else
{
#if !LUBAN_ASSISTANT
return (s[..sepIndex], ParseAttrs(s[(sepIndex + 1)..]));
#else
return (s.Substring(0, sepIndex), ParseAttrs(s.Substring(sepIndex + 1)));
#endif
}
}

View File

@ -59,34 +59,6 @@ namespace Luban.Job.Common.Utils
return fullName + ".bin";
}
public static bool IsExcelFile(string fullName)
{
return fullName.EndsWith(".csv", StringComparison.Ordinal)
|| fullName.EndsWith(".xls", StringComparison.Ordinal)
|| fullName.EndsWith(".xlsx", StringComparison.Ordinal);
}
public static (string, string) SplitFileAndSheetName(string url)
{
int sheetSepIndex = url.IndexOf('@');
if (sheetSepIndex < 0)
{
return (url, null);
}
else
{
int lastPathSep = url.LastIndexOf('/', sheetSepIndex);
if (lastPathSep >= 0)
{
return (url[0..(lastPathSep + 1)] + url[(sheetSepIndex + 1)..], url[(lastPathSep + 1)..sheetSepIndex]);
}
else
{
return (url[(sheetSepIndex + 1)..], url[(lastPathSep + 1)..sheetSepIndex]);
}
}
}
private readonly static Dictionary<string, ELanguage> s_name2Lans = new()
{
{ "cs", ELanguage.CS },

View File

@ -126,7 +126,7 @@ namespace Luban.Server.Common
break;
}
_caches.Remove(c.MD5, out var _);
_caches.Remove(c.MD5);
TotalBytes -= c.Content.Length;
s_logger.Info("remove cache. file:{file} md5:{md5} size:{size}, total bytes:{bytes} after remove.", c.FileName, c.MD5, c.Content.Length, TotalBytes);
}