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. /**
  55. * 写入配置文件
  56. * @param {string} key - 要写入的配置项键名
  57. * @param {*} value - 要写入的配置项值
  58. */
  59. function writeConfigFile(key, value) {
  60. try {
  61. let config = {};
  62. // 如果配置文件存在,则读取现有内容
  63. if (fs.existsSync(configPath)) {
  64. const data = fs.readFileSync(configPath, 'utf8');
  65. config = {
  66. ...defaultConfig,
  67. ...JSON.parse(data),
  68. };
  69. }else {
  70. console.log('配置文件不存在');
  71. // 创建空白JSON文件
  72. fs.writeFileSync(configPath, '{}', 'utf8');
  73. config = {
  74. ...defaultConfig,
  75. }
  76. }
  77. // 更新配置项
  78. config[key] = value;
  79. // 将更新后的配置写回文件
  80. fs.writeFileSync(configPath, JSON.stringify(config, null, 2), 'utf8');
  81. console.log(`成功写入配置项: ${key}`);
  82. } catch (error) {
  83. console.error('写入配置文件出错:', error);
  84. }
  85. }
  86. module.exports = {
  87. readConfigFile,
  88. writeConfigFile
  89. };