MainWindow.xaml.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. using AILicenseGenerator.Helper;
  2. using AILicenseGenerator.Models;
  3. using Microsoft.Win32;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text.Json;
  11. using System.Windows;
  12. using Vinno.AI.CommonSDK.Enums;
  13. namespace AILicenseGenerator
  14. {
  15. /// <summary>
  16. /// MainWindow.xaml 的交互逻辑
  17. /// </summary>
  18. public partial class MainWindow : Window
  19. {
  20. private const string _logFolderName = "AILicenseGeneratorLogs";
  21. private const string _generateFolderName = "GenerateFolder";
  22. private const string _functionSettingFile = "FunctionInfo.ini";
  23. private const string _licenseFileNameWithWildcard = "*****License.lic";
  24. private readonly string _version = Assembly.GetExecutingAssembly().GetName().Version.ToString();
  25. private readonly string _generateFolderPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _generateFolderName);
  26. private List<AIFunctionInfoDTO> _enabledFunctionList = new List<AIFunctionInfoDTO>();
  27. private List<AIFunctionInfoDTO> _functionList = new List<AIFunctionInfoDTO>();
  28. private List<AIFunctionInfoDTO> _functionListForAllEnginesVersion = new List<AIFunctionInfoDTO>();
  29. public MainWindow()
  30. {
  31. InitializeComponent();
  32. Title = "AILicenseGenerator_" + _version;
  33. Logger.LogDir = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, _logFolderName);
  34. TranslateHelper.Init(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Resources", "Chinese.json"));
  35. FunctionInit();
  36. AllFunctionListForAllEnginesVersionInit();
  37. if (AllFunctionEnableCheckBox.IsChecked == false)
  38. {
  39. FunctionDataGrid.ItemsSource = _functionList;
  40. }
  41. else
  42. {
  43. FunctionDataGrid.ItemsSource = _functionListForAllEnginesVersion;
  44. }
  45. this.DataContext = this;
  46. }
  47. private void FunctionInit()
  48. {
  49. var functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AIDiagnosisSDK.AIDiagnosis.AllFunctions);
  50. if (functions != null)
  51. {
  52. _functionList.AddRange(functions);
  53. }
  54. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(MyocardialDiagnosisSDK.MyocardialDiagnosis.AllFunctions);
  55. if (functions != null)
  56. {
  57. _functionList.AddRange(functions);
  58. }
  59. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(ThyroidClassificationSDK.ThyroidClassification.AllFunctions);
  60. if (functions != null)
  61. {
  62. _functionList.AddRange(functions);
  63. }
  64. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoEFDiagnosisSDK.AutoEFDiagnosis.AllFunctions);
  65. if (functions != null)
  66. {
  67. _functionList.AddRange(functions);
  68. }
  69. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoIVCDiagnosisSDK.AutoIVCDiagnosis.AllFunctions);
  70. if (functions != null)
  71. {
  72. _functionList.AddRange(functions);
  73. }
  74. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoVTIDiagnosisSDK.AutoVTIDiagnosis.AllFunctions);
  75. if (functions != null)
  76. {
  77. _functionList.AddRange(functions);
  78. }
  79. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(HepatoRenalRatioDiagnosisSDK.HepatoRenalRatioDiagnosis.AllFunctions);
  80. if (functions != null)
  81. {
  82. _functionList.AddRange(functions);
  83. }
  84. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(VetHeartDiagnosisSDK.VetHeartDiagnosis.AllFunctions);
  85. if(functions!=null)
  86. {
  87. _functionList.AddRange(functions);
  88. }
  89. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(CarotidClassificationSDK.CarotidClassification.AllFunctions);
  90. if (functions != null)
  91. {
  92. _functionList.AddRange(functions);
  93. }
  94. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AmnioticFluidDiagnosisSDK.AmnioticFluidDiagnosis.AllFunctions);
  95. if (functions != null)
  96. {
  97. _functionList.AddRange(functions);
  98. }
  99. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoDFRDiagnosisSDK.AutoDFRDiagnosis.AllFunctions);
  100. if (functions != null)
  101. {
  102. _functionList.AddRange(functions);
  103. }
  104. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(NerveIdentificationSDK.NerveIdentification.AllFunctions);
  105. if (functions != null)
  106. {
  107. _functionList.AddRange(functions);
  108. }
  109. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoBLineDiagnosisSDK.AutoBLineDiagnosis.AllFunctions);
  110. if (functions != null)
  111. {
  112. _functionList.AddRange(functions);
  113. }
  114. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(FatLayerIdentificationSDK.FatLayerIdentification.AllFunctions);
  115. if (functions != null)
  116. {
  117. _functionList.AddRange(functions);
  118. }
  119. }
  120. private void AllFunctionListForAllEnginesVersionInit()
  121. {
  122. var functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AIDiagnosisSDK.AIDiagnosis.AllFunctions);
  123. if(functions!=null)
  124. {
  125. _functionListForAllEnginesVersion.AddRange(functions);
  126. }
  127. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(MyocardialDiagnosisSDK.MyocardialDiagnosis.AllFunctions);
  128. if (functions != null)
  129. {
  130. _functionListForAllEnginesVersion.AddRange(functions);
  131. }
  132. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(ThyroidClassificationSDK.ThyroidClassification.AllFunctions);
  133. if (functions != null)
  134. {
  135. _functionListForAllEnginesVersion.AddRange(functions);
  136. }
  137. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoEFDiagnosisSDK.AutoEFDiagnosis.AllFunctions);
  138. if (functions != null)
  139. {
  140. _functionListForAllEnginesVersion.AddRange(functions);
  141. }
  142. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoIVCDiagnosisSDK.AutoIVCDiagnosis.AllFunctions);
  143. if (functions != null)
  144. {
  145. _functionListForAllEnginesVersion.AddRange(functions);
  146. }
  147. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoVTIDiagnosisSDK.AutoVTIDiagnosis.AllFunctions);
  148. if (functions != null)
  149. {
  150. _functionListForAllEnginesVersion.AddRange(functions);
  151. }
  152. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(HepatoRenalRatioDiagnosisSDK.HepatoRenalRatioDiagnosis.AllFunctions);
  153. if (functions != null)
  154. {
  155. _functionListForAllEnginesVersion.AddRange(functions);
  156. }
  157. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(VetHeartDiagnosisSDK.VetHeartDiagnosis.AllFunctions);
  158. if (functions != null)
  159. {
  160. _functionListForAllEnginesVersion.AddRange(functions);
  161. }
  162. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(CarotidClassificationSDK.CarotidClassification.AllFunctions);
  163. if (functions != null)
  164. {
  165. _functionListForAllEnginesVersion.AddRange(functions);
  166. }
  167. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AmnioticFluidDiagnosisSDK.AmnioticFluidDiagnosis.AllFunctions);
  168. if (functions != null)
  169. {
  170. _functionListForAllEnginesVersion.AddRange(functions);
  171. }
  172. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoDFRDiagnosisSDK.AutoDFRDiagnosis.AllFunctions);
  173. if (functions != null)
  174. {
  175. _functionListForAllEnginesVersion.AddRange(functions);
  176. }
  177. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(NerveIdentificationSDK.NerveIdentification.AllFunctions);
  178. if (functions != null)
  179. {
  180. _functionListForAllEnginesVersion.AddRange(functions);
  181. }
  182. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(AutoBLineDiagnosisSDK.AutoBLineDiagnosis.AllFunctions);
  183. if (functions != null)
  184. {
  185. _functionListForAllEnginesVersion.AddRange(functions);
  186. }
  187. functions = AIConvertHelper.ConvertListFunctionInfoToListAIFunctionInfoDTO(FatLayerIdentificationSDK.FatLayerIdentification.AllFunctions);
  188. if (functions != null)
  189. {
  190. _functionListForAllEnginesVersion.AddRange(functions);
  191. }
  192. foreach (var function in _functionListForAllEnginesVersion)
  193. {
  194. function.Version = "All";
  195. foreach (var enigne in function.Engines)
  196. {
  197. enigne.Version = "All";
  198. }
  199. }
  200. }
  201. private void FunctionSettingButton_Click(object sender, RoutedEventArgs e)
  202. {
  203. try
  204. {
  205. PreGeneration();
  206. var jsonString = JsonSerializer.Serialize(_enabledFunctionList, new JsonSerializerOptions
  207. {
  208. WriteIndented = true,
  209. });
  210. var filePath = Path.Combine(_generateFolderPath, _functionSettingFile);
  211. File.WriteAllText(filePath, jsonString);
  212. PostGeneration(_generateFolderPath);
  213. MessageBox.Show($"配置文件生成成功", "提示");
  214. }
  215. catch (Exception ex)
  216. {
  217. MessageBox.Show($"配置文件生成出错,{ex}", "提示");
  218. }
  219. }
  220. private void PreGeneration()
  221. {
  222. DirectoryHelper.DeleteDirectory(_generateFolderPath);
  223. DirectoryHelper.CreateDirectory(_generateFolderPath);
  224. GetEnabledFunctions();
  225. }
  226. private void GetEnabledFunctions()
  227. {
  228. _enabledFunctionList.Clear();
  229. if (AllFunctionEnableCheckBox.IsChecked == true)
  230. {
  231. var enableFunctions = _functionListForAllEnginesVersion.Where(x => x.IsEnabled).ToList();
  232. foreach (var function in enableFunctions)
  233. {
  234. var enabledEngines = function.Engines.Where(x => x.IsEnabled).ToList();
  235. _enabledFunctionList.Add(new AIFunctionInfoDTO(function.FunctionName, function.IsEnabled, function.Version, function.ExpiredTime, enabledEngines));
  236. }
  237. }
  238. else
  239. {
  240. var enableFunctions = _functionList.Where(x => x.IsEnabled).ToList();
  241. foreach (var function in enableFunctions)
  242. {
  243. var enabledEngines = function.Engines.Where(x => x.IsEnabled).ToList();
  244. _enabledFunctionList.Add(new AIFunctionInfoDTO(function.FunctionName, function.IsEnabled, function.Version, function.ExpiredTime, enabledEngines));
  245. }
  246. }
  247. }
  248. private void PostGeneration(string path)
  249. {
  250. using (Process process = new Process())
  251. {
  252. ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
  253. psi.Arguments = path;
  254. process.StartInfo = psi;
  255. process.Start();
  256. }
  257. }
  258. private void LicenseButton_Click(object sender, RoutedEventArgs e)
  259. {
  260. try
  261. {
  262. PreGeneration();
  263. if (_enabledFunctionList != null)
  264. {
  265. foreach (var function in _enabledFunctionList)
  266. {
  267. if (Enum.TryParse(function.FunctionName, out AIEnumType type))
  268. {
  269. var licenseFileName = _licenseFileNameWithWildcard.Replace("*****", type.ToString());
  270. var jsonString = JsonSerializer.Serialize(function, new JsonSerializerOptions
  271. {
  272. WriteIndented = true,
  273. });
  274. var desString = DesBuilder.Encrypt(jsonString);
  275. var filePath = Path.Combine(_generateFolderPath, licenseFileName);
  276. File.WriteAllText(filePath, desString);
  277. }
  278. }
  279. }
  280. PostGeneration(_generateFolderPath);
  281. MessageBox.Show($"License生成成功", "提示");
  282. }
  283. catch (Exception ex)
  284. {
  285. MessageBox.Show($"License生成出错,{ex}", "提示");
  286. }
  287. }
  288. private void GenerateButton_Click(object sender, RoutedEventArgs e)
  289. {
  290. try
  291. {
  292. PreGeneration();
  293. var jsonString = JsonSerializer.Serialize(_enabledFunctionList, new JsonSerializerOptions
  294. {
  295. WriteIndented = true,
  296. });
  297. var filePath = Path.Combine(_generateFolderPath, _functionSettingFile);
  298. File.WriteAllText(filePath, jsonString);
  299. if (_enabledFunctionList != null)
  300. {
  301. foreach (var function in _enabledFunctionList)
  302. {
  303. if (Enum.TryParse(function.FunctionName, out AIEnumType type))
  304. {
  305. var licenseFileName = _licenseFileNameWithWildcard.Replace("*****", type.ToString());
  306. jsonString = JsonSerializer.Serialize(function, new JsonSerializerOptions
  307. {
  308. WriteIndented = true,
  309. });
  310. var desString = DesBuilder.Encrypt(jsonString);
  311. filePath = Path.Combine(_generateFolderPath, licenseFileName);
  312. File.WriteAllText(filePath, desString);
  313. }
  314. }
  315. }
  316. PostGeneration(_generateFolderPath);
  317. MessageBox.Show($"全部生成成功", "提示");
  318. }
  319. catch (Exception ex)
  320. {
  321. MessageBox.Show($"全部生成出错,{ex}", "提示");
  322. }
  323. }
  324. private void AllFunctionEnable_Click(object sender, RoutedEventArgs e)
  325. {
  326. if (AllFunctionEnableCheckBox.IsChecked == false)
  327. {
  328. FunctionDataGrid.ItemsSource = _functionList;
  329. }
  330. else
  331. {
  332. FunctionDataGrid.ItemsSource = _functionListForAllEnginesVersion;
  333. }
  334. }
  335. private void LicenseDecryptButton_Click(object sender, RoutedEventArgs e)
  336. {
  337. OpenFileDialog openFileDialog = new OpenFileDialog
  338. {
  339. Filter = "License文件|*.lic",
  340. Multiselect = true,
  341. Title = "选择License文件"
  342. };
  343. if (openFileDialog.ShowDialog() ?? false)
  344. {
  345. var files = openFileDialog.FileNames;
  346. if (files != null && files.Length > 0)
  347. {
  348. var filePath = Path.GetDirectoryName(files[0]);
  349. foreach (var file in files)
  350. {
  351. try
  352. {
  353. var fileName = Path.GetFileNameWithoutExtension(file);
  354. var newFileName = $"Decrpt{fileName}.txt";
  355. var newFilePath = Path.Combine(filePath, newFileName);
  356. var licenseInfo = File.ReadAllText(file);
  357. var decryptInfo = DesBuilder.Decrypt(licenseInfo);
  358. File.WriteAllText(newFilePath, decryptInfo);
  359. }
  360. catch(Exception ex)
  361. {
  362. MessageBox.Show($"解密出错:{ex}", "提示");
  363. }
  364. }
  365. PostGeneration(filePath);
  366. MessageBox.Show($"解密完成!", "提示");
  367. }
  368. }
  369. }
  370. }
  371. }