LocalVersionLoader.qml 841 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import QtQuick 2.0
  2. import FileIO 1.0
  3. QtObject {
  4. property string iniFilePath: ""
  5. property string version: ""
  6. property string config: ""
  7. function load() {
  8. config = FileIO.readTextFile(iniFilePath)
  9. if (!config) {
  10. version = ""
  11. return
  12. }
  13. let groups = config.match(/game_version=([^\s]+)/)
  14. version = (groups && groups[1]) || ""
  15. }
  16. function save() {
  17. if (!version) {
  18. console.error("Version for save is not set")
  19. return
  20. }
  21. if (!config) {
  22. config = `[General]\r\nchannel=1\r\ncps=mihoyo\r\ngame_version=${version}\r\nsub_channel=0\r\n`
  23. } else {
  24. config = config.replace(/(game_version)=[^\s]+/, "$1=" + version)
  25. }
  26. FileIO.writeTextFile(iniFilePath, config)
  27. }
  28. }