Skip to content

Commit

Permalink
Feat: Show log in error window
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhxiao committed Jul 22, 2024
1 parent a003a84 commit ad2e5cb
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 39 deletions.
77 changes: 56 additions & 21 deletions MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
using System.Text;
using System.Windows;
using System.Windows;
using System.Collections.ObjectModel;
using NLog;
using SoulsModTranslator.core;
using DataFormats = System.Windows.DataFormats;
using DragDropEffects = System.Windows.DragDropEffects;
using DragEventArgs = System.Windows.DragEventArgs;
using MessageBox = System.Windows.Forms.MessageBox;
using System.IO;
using AdonisUI.Controls;
using Button = System.Windows.Controls.Button;
using MessageBoxButton = AdonisUI.Controls.MessageBoxButton;
using MessageBoxImage = AdonisUI.Controls.MessageBoxImage;
using System.Diagnostics;
using System.Windows.Navigation;
using Org.BouncyCastle.Bcpg.Sig;
using NLog.Targets;

namespace SoulsModTranslator
{
Expand All @@ -26,16 +22,43 @@ public partial class MainWindow : AdonisWindow
private static readonly string GlossaryPath = Path.Combine(Directory.GetCurrentDirectory(), "glossaries");
private static readonly string SoftwareName = "魂游MOD翻译工具 v2.7";

private static MemoryTarget MemoryTarget = new MemoryTarget
{
Name = "memory",
Layout = "[${level}] ${message}"
};

private static void ShowTaskResult(bool success, string succMsg, string failMsg)
{
var caption = success ? "提示" : "错误";
var icon = success ? MessageBoxImage.Information : MessageBoxImage.Error;
AdonisUI.Controls.MessageBox.Show(success ? succMsg : failMsg, caption, MessageBoxButton.OK, icon);
// var caption = success ? "提示" : "错误";
// var icon = success ? MessageBoxImage.Information : MessageBoxImage.Error;
if (success)
{
AdonisUI.Controls.MessageBox.Show(success ? succMsg : failMsg, "提示",
AdonisUI.Controls.MessageBoxButton.OK, MessageBoxImage.Information);
return;
}

var log = String.Join(Environment.NewLine, MemoryTarget.Logs.ToArray());
var messageBox = new MessageBoxModel
{
Text = failMsg + "\n\n" + log,
Caption = "错误",
Icon = MessageBoxImage.Information,
Buttons =
new[]
{
AdonisUI.Controls.MessageBoxButtons.Custom("复制日志并前往Github反馈", "github"),
AdonisUI.Controls.MessageBoxButtons.Custom("复制日志并前往b站反馈", "bilibili"),
AdonisUI.Controls.MessageBoxButtons.Custom("关闭", "close"),
},
};
AdonisUI.Controls.MessageBox.Show(messageBox);
}

private static List<string> LoadDbFiles()
{
Logger.Info($"数据库路径是{DbPath}");
Logger.Info($"数据库根目录{DbPath}");
if (!Directory.Exists(DbPath))
{
return new List<string>();
Expand All @@ -45,6 +68,13 @@ private static List<string> LoadDbFiles()
return (from file in files where Path.GetExtension(file).Equals(".json") select Path.GetFileName(file)).ToList();
}

public void CreateArrayLogger()
{
var config = LogManager.Configuration;
config.AddTarget(MemoryTarget);
config.AddRule(LogLevel.Info, LogLevel.Fatal, MemoryTarget);
LogManager.Configuration = config;
}

private void SwitchTab(string name)
{
Expand Down Expand Up @@ -72,7 +102,9 @@ private void SwitchTab(string name)

public MainWindow()
{
Logger.Info("\n\n\n===========================New Instance===================================");

Logger.Info("\n\n===========================New Instance===================================");
CreateArrayLogger();
InitializeComponent();
//
Glossaries = new ObservableCollection<string>();
Expand All @@ -87,7 +119,6 @@ public MainWindow()
SwitchTab("Translate");
this.Title = SoftwareName;
AboutTitleLabel.Content = SoftwareName + " By hhhxiao";

if (DbList.Count != 0) return;
ShowTaskResult(false, "", "找不到数据库文件,请检查软件完整性");
this.Close();
Expand Down Expand Up @@ -138,9 +169,11 @@ private async void MergeDbBtn_OnClick(object sender, RoutedEventArgs e)
if (result != System.Windows.Forms.DialogResult.OK) return;
//save path
var savePath = "";
var saveDialog = new SaveFileDialog();
saveDialog.Filter = "Json文件(*.json)|*";
saveDialog.FileName = "Untitled.json";
var saveDialog = new SaveFileDialog
{
Filter = "Json文件(*.json)|*",
FileName = "Untitled.json"
};
var saveResult = saveDialog.ShowDialog();
if (saveResult != System.Windows.Forms.DialogResult.OK) return;
savePath = saveDialog.FileName;
Expand All @@ -157,10 +190,12 @@ private void TranslateTab_OnClick(object sender, RoutedEventArgs e)

private void GlossaryAdd_onClick(object sender, RoutedEventArgs e)
{
var dialog = new System.Windows.Forms.OpenFileDialog();
dialog.InitialDirectory = GlossaryPath;
dialog.Filter = "Json 文件 (*.json)|*.json|所有文件|*.*";
dialog.Multiselect = true;
var dialog = new System.Windows.Forms.OpenFileDialog
{
InitialDirectory = GlossaryPath,
Filter = "Json 文件 (*.json)|*.json|所有文件|*.*",
Multiselect = true
};
var result = dialog.ShowDialog();
if (result != System.Windows.Forms.DialogResult.OK) return;
foreach (var item in dialog.FileNames)
Expand Down Expand Up @@ -255,7 +290,7 @@ private async void ExportBtn_onClick(object sender, RoutedEventArgs e)
dialog.FileName = exportAsExcel ? "text.xlsx" : "text.txt";
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK) return;
var result = await Task.Run(() => TextExporter.Export(dialog.FileName, res, exportAsExcel, resort, false));
Logger.Info($"成功导出未翻译文本 {dialog.FileName}");
Logger.Info($"成功导出未翻译文本{dialog.FileName}");
ShowTaskResult(result, "导出成功", "导出失败");
}

Expand Down
6 changes: 3 additions & 3 deletions core/DBTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public static bool CreateDb(string srcPath, string destPath, string savePath)

var sourceDict = ReadKeyValue(srcLang);
var destDict = ReadKeyValue(destLang);
Logger.Info($"从源语言中读取{sourceDict.Count}个文本,从目标语言中读取{destDict.Count}个文本");
Logger.Info($"从源语言中读取{sourceDict.Count}条文本,从目标语言中读取{destDict.Count}条文本");
var dbDict = new Dictionary<string, string>();
foreach (var (key, srcValue) in sourceDict)
{
Expand All @@ -68,7 +68,7 @@ public static bool MergeDB(string[] fileNames, string savePath)
foreach (var file in fileNames)
{
var db = Utils.LoadJsonToMap(file);
Logger.Info($" - {file}: {db.Count}");
Logger.Info($" - 文件:{file}: 大小:{db.Count}");
count += db.Count;
foreach (var kv in db)
{
Expand All @@ -79,7 +79,7 @@ public static bool MergeDB(string[] fileNames, string savePath)
}
}

Logger.Info($"{count}->{newDB.Count}");
Logger.Info($"新数据库的大小:{count}->{newDB.Count}");
Utils.SaveMapAsJson(newDB, savePath);
return true;
}
Expand Down
2 changes: 1 addition & 1 deletion core/Glossary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class RegexMatchRule
public Glossary(bool caseInsensitive)
{
_IgnoreCase = caseInsensitive;
Logger.Info($"忽视术语表大小写:{_IgnoreCase}");
Logger.Debug($"忽视术语表大小写:{_IgnoreCase}");
}

public bool Load(IEnumerable<string> glossaryFileNames)
Expand Down
8 changes: 4 additions & 4 deletions core/LangFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public bool Load(string langRootPath)
{
if (!Directory.Exists(langRootPath))
{
Logger.Error($"目录不存在: {langRootPath}");
Logger.Error($"目录不存在:{langRootPath}");
return false;
}

Expand All @@ -36,7 +36,7 @@ public bool Load(string langRootPath)
foreach (var bndFile in info.GetFiles())
{
if (!bndFile.Name.EndsWith(".msgbnd.dcx")) continue;
Logger.Info($"发现 msgbnd.dcx 文件: {bndFile.Name}");
Logger.Info($"发现msgbnd.dcx文件: {bndFile.Name}");
if (bndFile.Name == "ngword.msgbnd.dcx") continue; //跳过这个ng单词

try
Expand All @@ -46,15 +46,15 @@ public bool Load(string langRootPath)
{
Logger.Error($"发现重复的FMG语言文件,文件夹{langRootPath}中是否存在与{bndFile.Name}相同的dcx" +
$"文件(比如黑暗之魂III和艾尔登法环)请根据需求删除(或修改后缀名)其中一个");
Logger.Error($"推荐做法为新建文件夹并将除了item_dlc02.msgbnd.dcx以及item_dlc02.msgbnd.dcx之外的dcx文件移动到该文件夹内");
Logger.Error($"推荐做法为新建文件夹并将除了item_dlc02.msgbnd.dcx以及menu_dlc02.msgbnd.dcx之外的dcx文件移动到该文件夹内");
return false;
}

Bnds[bndFile.Name] = bnd;
}
catch (Exception e)
{
Logger.Error("不合法的dcx文件格式" + e.Message);
Logger.Error("不合法的dcx文件格式.\n\n" + e.Message);
return false;
}
}
Expand Down
12 changes: 6 additions & 6 deletions core/TextExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@ private static void CompressedExport(Stream file, ExportResult exportResult)
public static bool Export(string fileName, ExportResult exportResult, bool excel, bool resort, bool compressed)
{

Logger.Info("Exporter:" + fileName);
Logger.Info($" Phase list size: {exportResult.PhaseList.Count}");
Logger.Info($" 导出句子条数: {exportResult.SentenceList.Count}");
Logger.Info(" 是否导出为Excel: " + excel);
Logger.Info(" 是否重排: " + resort);
Logger.Info(" 是否压缩存储(未实装): " + compressed);
Logger.Info("开始导出:" + fileName);
Logger.Info($" - 导出短语条数: {exportResult.PhaseList.Count}");
Logger.Info($" - 导出句子条数: {exportResult.SentenceList.Count}");
Logger.Info(" - 是否导出为Excel: " + excel);
Logger.Info(" - 是否重排: " + resort);
Logger.Info(" - 是否压缩存储(未实装): " + compressed);
if (resort)
{
exportResult.SentenceList = exportResult.SentenceList.OrderByDescending(item => Utils.GetChineseCharacterRatio(item.Text))
Expand Down
9 changes: 6 additions & 3 deletions core/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ private static Action<string, int, string, int> CreateTraverser(DB db, Action<lo

public static ExportResult Export(string rootPath, string dbPath)
{
Logger.Info($"开始导出未翻译文本,msg根目录:{rootPath},数据库路径:{dbPath}");
Logger.Info($"开始导出未翻译文本,msg根目录{rootPath},数据库路径{dbPath}");
var result = new ExportResult
{
Success = false
Expand All @@ -159,7 +159,10 @@ public static ExportResult Export(string rootPath, string dbPath)

public static bool Translate(string rootPath, string dbPath, string translateFileName)
{
Logger.Info($"开始生成目标语言文件,msg根目录:{rootPath}\n - 数据库路径:{dbPath},翻译文件路径:{translateFileName}");
Logger.Info($"开始生成目标语言文件");
Logger.Info($"msg根目录:{rootPath}");
Logger.Info($"数据库路径:{dbPath}");
Logger.Info($"翻译文件路径:{translateFileName}");
var langFile = new LangFile();
var db = new DB();
if (!langFile.Load(Path.Combine(rootPath, Configuration.SrcLangPath)) || !db.Load(dbPath))
Expand Down Expand Up @@ -204,7 +207,7 @@ public static bool Translate(string rootPath, string dbPath, string translateFil
var zhocnPath = Path.Combine(rootPath, Configuration.DestLangPath);
foreach (var bnd in langFile.Bnds)
{
Logger.Info($"开始生成文件 {Path.Join(zhocnPath, bnd.Key)} ");
Logger.Info($"开始生成文件{Path.Join(zhocnPath, bnd.Key)} ");
foreach (var file in bnd.Value.Files)
{
//replace name form engus to zhocn
Expand Down
2 changes: 1 addition & 1 deletion core/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Dictionary<string, string> LoadJsonToMap(string fileName)
}
catch (Exception e)
{
Logger.Error($"无法读取Json文件{fileName}", e);
Logger.Error($"无法读取Json文件{fileName}", e);
}
return dict ?? new Dictionary<string, string>();
}
Expand Down

0 comments on commit ad2e5cb

Please sign in to comment.