CleanFiles.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import codecs
  2. import os
  3. import glob
  4. standard_sections = [
  5. "Core",
  6. "EmuState",
  7. "OnLoad",
  8. "OnFrame",
  9. "ActionReplay",
  10. "Video",
  11. "Video_Settings",
  12. "Video_Enhancements",
  13. "Video_Hacks",
  14. "Speedhacks",
  15. ]
  16. standard_comments = {
  17. "Core": "Values set here will override the main dolphin settings.",
  18. "EmuState": "The Emulation State. 1 is worst, 5 is best, 0 is not set.",
  19. "OnLoad": "Add memory patches to be loaded once on boot here.",
  20. "OnFrame": "Add memory patches to be applied every frame here.",
  21. "ActionReplay": "Add action replay cheats here.",
  22. "Video": "",
  23. "Video_Settings": "",
  24. "Video_Enhancements": "",
  25. "Video_Hacks": "",
  26. "Speedhacks": "",
  27. }
  28. def normalize_comment(line):
  29. line = line.strip().lstrip('#').lstrip()
  30. if line:
  31. return "# %s" % (line,)
  32. else:
  33. return ""
  34. def normalize_ini_file(in_, out):
  35. sections = {}
  36. current_section = None
  37. toplevel_comment = ""
  38. wants_comment = False
  39. for line in in_:
  40. line = line.strip()
  41. # strip utf8 bom
  42. line = line.lstrip(u'\ufeff')
  43. if line.startswith('#'):
  44. line = normalize_comment(line)
  45. if current_section is None:
  46. toplevel_comment += line
  47. continue
  48. if line.startswith('['):
  49. end = line.find(']')
  50. section_name = line[1:end]
  51. if section_name not in standard_sections:
  52. continue
  53. current_section = []
  54. sections[section_name] = current_section
  55. wants_comment = False
  56. continue
  57. if current_section is None and line:
  58. raise ValueError("invalid junk")
  59. if current_section is None:
  60. continue
  61. if line.startswith('#') and not wants_comment:
  62. continue
  63. current_section.append(line)
  64. if line:
  65. wants_comment = True
  66. out.write(toplevel_comment.strip() + "\n\n")
  67. for section in standard_sections:
  68. lines = '\n'.join(sections.get(section, "")).strip()
  69. comments = standard_comments[section]
  70. if not lines and not comments:
  71. continue
  72. out.write("[%s]\n" % (section,))
  73. if comments:
  74. out.write("# %s\n" % (comments,))
  75. if lines:
  76. out.write(lines)
  77. out.write('\n')
  78. out.write('\n')
  79. def main():
  80. base_path = os.path.dirname(__file__)
  81. pattern = os.path.join(base_path, "../Data/User/GameConfig/??????.ini")
  82. for name in glob.glob(pattern):
  83. in__name = name
  84. out_name = name + '.new'
  85. in_ = codecs.open(in__name, 'r', 'utf8')
  86. out = codecs.open(out_name, 'w', 'utf8')
  87. normalize_ini_file(in_, out)
  88. os.rename(out_name, in__name)
  89. if __name__ == "__main__":
  90. main()