diff --git a/src/Luban.ClientServer/Program.cs b/src/Luban.ClientServer/Program.cs index ef24075..467548c 100644 --- a/src/Luban.ClientServer/Program.cs +++ b/src/Luban.ClientServer/Program.cs @@ -4,6 +4,7 @@ using Luban.Client.Common.Utils; using Luban.Client.Utils; using Luban.Common.Protos; using Luban.Common.Utils; +using Luban.Job.Common.Utils; using Luban.Server; using System; using System.Collections.Generic; @@ -34,7 +35,8 @@ namespace Luban.ClientServer public string[] WatchDir { get; set; } - public string StringTemplateDir { get; set; } + [Option('t', "template_search_path", Required = false, HelpText = "string template search path.")] + public string TemplateSearchPath { get; set; } } private static void PrintUsage(string err) @@ -54,7 +56,7 @@ Options: -l --loglevel log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF -c --cachemetafile cache meta file name. default is '.cache.meta' -w --watch watch data change and regenerate. - -t --templatedirectory string templates directory. default is 'Templates' + -t --template_search_path additional template search path -h --help show usage "); } @@ -122,9 +124,9 @@ Options: break; } case "-t": - case "--templatedirectory": + case "--template_search_path": { - ops.StringTemplateDir = args[++i]; + ops.TemplateSearchPath = args[++i]; break; } case "--": @@ -154,7 +156,11 @@ Options: private static void StartServer(AllCommandLineOptions options) { - Job.Common.Utils.StringTemplateUtil.TemplateDir = options.StringTemplateDir; + if (!string.IsNullOrEmpty(options.TemplateSearchPath)) + { + StringTemplateUtil.AddTemplateSearchPath(options.TemplateSearchPath); + } + StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); GenServer.Ins.Start(options.Port, ProtocolStub.Factories); @@ -206,9 +212,9 @@ Options: ThreadPool.SetMinThreads(Math.Max(4, processorCount), 5); ThreadPool.SetMaxThreads(Math.Max(16, processorCount * 4), 10); - if (string.IsNullOrEmpty(options.StringTemplateDir)) + if (string.IsNullOrEmpty(options.TemplateSearchPath)) { - options.StringTemplateDir = FileUtil.GetPathRelateApplicationDirectory("Templates"); + options.TemplateSearchPath = FileUtil.GetPathRelateApplicationDirectory("Templates"); } if (string.IsNullOrWhiteSpace(options.Host)) { diff --git a/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs b/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs index 9d8f44c..6235303 100644 --- a/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs +++ b/src/Luban.Job.Common/Source/Utils/StringTemplateUtil.cs @@ -11,11 +11,24 @@ namespace Luban.Job.Common.Utils { public static class StringTemplateUtil { - public static string TemplateDir { get; set; } + private static List TemplateSearchPaths { get; } = new List(); + + public static void AddTemplateSearchPath(string path) + { + TemplateSearchPaths.Add(path); + } public static string GetTemplateString(string templateName) { - return File.ReadAllText($"{TemplateDir}/{templateName}.tpl", Encoding.UTF8); + foreach (var searchPath in TemplateSearchPaths) + { + var fullPath = $"{searchPath}/{templateName}.tpl"; + if (File.Exists(fullPath)) + { + return File.ReadAllText(fullPath, Encoding.UTF8); + } + } + throw new FileNotFoundException($"can't find {templateName}.tpl in paths:{string.Join(';', TemplateSearchPaths)}"); } private static readonly ConcurrentDictionary s_templates = new(); diff --git a/src/Luban.Server/Source/Program.cs b/src/Luban.Server/Source/Program.cs index 94187e5..1690f4c 100644 --- a/src/Luban.Server/Source/Program.cs +++ b/src/Luban.Server/Source/Program.cs @@ -2,6 +2,7 @@ using Bright.Common; using CommandLine; using Luban.Common.Protos; using Luban.Common.Utils; +using Luban.Job.Common.Utils; using Luban.Server.Common; using System; using System.IO; @@ -20,8 +21,8 @@ namespace Luban.Server [Option('l', "loglevel", Required = false, HelpText = "log level. default INFO. avaliable value: TRACE,DEBUG,INFO,WARN,ERROR,FATAL,OFF")] public string LogLevel { get; set; } = "INFO"; - [Option('t', "string template directory", Required = false, HelpText = "string template directory.")] - public string StringTemplateDir { get; set; } + [Option('t', "template_search_path", Required = false, HelpText = "additional template search path")] + public string TemplateSearchPath { get; set; } } private static CommandLineOptions ParseOptions(String[] args) @@ -48,11 +49,11 @@ namespace Luban.Server var options = ParseOptions(args); - if (string.IsNullOrEmpty(options.StringTemplateDir)) + if (!string.IsNullOrEmpty(options.TemplateSearchPath)) { - options.StringTemplateDir = FileUtil.GetPathRelateApplicationDirectory("Templates"); + StringTemplateUtil.AddTemplateSearchPath(options.TemplateSearchPath); } - Job.Common.Utils.StringTemplateUtil.TemplateDir = options.StringTemplateDir; + StringTemplateUtil.AddTemplateSearchPath(FileUtil.GetPathRelateApplicationDirectory("Templates")); Luban.Common.Utils.LogUtil.InitSimpleNLogConfigure(NLog.LogLevel.FromString(options.LogLevel));