config.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // electron/utils/readConfig.js
  2. const fs = require('fs');
  3. const path = require('path');
  4. const { app } = require('electron');
  5. const defaultConfig = require('./config.default');
  6. const configPath = path.join(app.getPath("userData"),'config.default.json');
  7. // Smart Shooter 5 的历史默认安装目录。用户目录里的旧配置会盖掉代码默认值,
  8. // 只有仍停在这套出厂值时才自动切到 6;手动选过别的目录则保持不动。
  9. const LEGACY_SMARTSHOOTER5 = {
  10. controlType: "SmartShooter",
  11. controlPath: "C:\\Program Files\\Smart Shooter 5",
  12. }
  13. function migrateLegacySmartShooter(config) {
  14. if (!config || config.smartShooter6Migrated) return { config, changed: false }
  15. const stillFactoryDefault = config.controlType === LEGACY_SMARTSHOOTER5.controlType
  16. && config.controlPath === LEGACY_SMARTSHOOTER5.controlPath
  17. config.smartShooter6Migrated = true
  18. if (!stillFactoryDefault) return { config, changed: true }
  19. config.controlType = defaultConfig.controlType
  20. config.controlPath = defaultConfig.controlPath
  21. config.SmartShooterPath = config.SmartShooterPath || LEGACY_SMARTSHOOTER5.controlPath
  22. config.SmartShooter6Path = defaultConfig.SmartShooter6Path
  23. return { config, changed: true }
  24. }
  25. /**
  26. * 读取配置文件
  27. */
  28. function readConfigFile() {
  29. try {
  30. if (fs.existsSync(configPath)) {
  31. const data = fs.readFileSync(configPath, 'utf8');
  32. const parsed = JSON.parse(data);
  33. const { config: merged, changed } = migrateLegacySmartShooter({
  34. ...defaultConfig,
  35. ...parsed,
  36. });
  37. if (changed) {
  38. fs.writeFileSync(configPath, JSON.stringify(merged, null, 2), 'utf8');
  39. }
  40. return merged;
  41. } else {
  42. console.log('配置文件不存在');
  43. // 创建空白JSON文件
  44. fs.writeFileSync(configPath, '{}', 'utf8');
  45. return {
  46. ...defaultConfig,
  47. };
  48. }
  49. } catch (error) {
  50. console.error('读取配置文件出错:', error);
  51. return null;
  52. }
  53. }
  54. function loadConfigFile() {
  55. if (!fs.existsSync(configPath)) {
  56. return { ...defaultConfig }
  57. }
  58. const data = fs.readFileSync(configPath, 'utf8');
  59. return {
  60. ...defaultConfig,
  61. ...JSON.parse(data),
  62. }
  63. }
  64. /**
  65. * 一次写入多项配置,避免 controlType 和安装目录被分成几次写入后互相覆盖
  66. */
  67. function updateConfigFile(partial) {
  68. try {
  69. const config = {
  70. ...loadConfigFile(),
  71. ...partial,
  72. }
  73. fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
  74. console.log(`成功写入配置项: ${Object.keys(partial).join(', ')}`);
  75. } catch (error) {
  76. console.error('写入配置文件出错:', error);
  77. }
  78. }
  79. /**
  80. * 写入配置文件
  81. * @param {string} key - 要写入的配置项键名
  82. * @param {*} value - 要写入的配置项值
  83. */
  84. function writeConfigFile(key, value) {
  85. updateConfigFile({ [key]: value })
  86. }
  87. module.exports = {
  88. readConfigFile,
  89. writeConfigFile,
  90. updateConfigFile
  91. };