Skip to content

Commit

Permalink
Rare: enforce utf-8 encoding in some places, should fix subtle issues…
Browse files Browse the repository at this point in the history
… on Windows

Fixes: #463
  • Loading branch information
loathingKernel committed Oct 10, 2024
1 parent e55c44f commit 3f99773
Show file tree
Hide file tree
Showing 9 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion rare/commands/launcher/console_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def save(self):
if ok:
if "." not in file:
file += ".log"
with open(file, "w") as f:
with open(file, "w", encoding="utf-8") as f:
f.write(self.console_edit.toPlainText())
f.close()
self.save_button.setText(self.tr("Saved"))
Expand Down
4 changes: 2 additions & 2 deletions rare/models/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def __load_metadata_json() -> Dict:
metadata = {}
file = os.path.join(data_dir(), "game_meta.json")
try:
with open(file, "r") as f:
with open(file, "r", encoding="utf-8") as f:
metadata = json.load(f)
except FileNotFoundError:
logger.info("%s does not exist", file)
Expand All @@ -175,7 +175,7 @@ def __save_metadata(self):
metadata: Dict = self.__load_metadata_json()
# pylint: disable=unsupported-assignment-operation
metadata[self.app_name] = vars(self.metadata)
with open(os.path.join(data_dir(), "game_meta.json"), "w+") as file:
with open(os.path.join(data_dir(), "game_meta.json"), "w+", encoding="utf-8") as file:
json.dump(metadata, file, indent=2)

def update_game(self):
Expand Down
2 changes: 1 addition & 1 deletion rare/resources/static_css/stylesheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def css_name(widget: Union[wrappertype, QObject, Type], subwidget: str = ""):


if __name__ == "__main__":
with open("stylesheet.qss", "w") as qss:
with open("stylesheet.qss", "w", encoding="utf-8") as qss:
qss.write(f'\n/* This file is auto-generated from "{os.path.basename(__file__)}". DO NOT EDIT!!! */\n\n')
qss.write(css.toString())

Expand Down
2 changes: 1 addition & 1 deletion rare/shared/image_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def __download(self, updates: List, json_data: Dict, game: Game, use_async: bool
json_data["size"] = {"w": ImageSize.Tall.size.width(), "h": ImageSize.Tall.size.height()}

# write image.json
with open(self.__img_json(game.app_name), "w") as file:
with open(self.__img_json(game.app_name), "w", encoding="utf-8") as file:
json.dump(json_data, file)

return bool(updates)
Expand Down
2 changes: 1 addition & 1 deletion rare/shared/rare_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def core(self, init: bool = False) -> LegendaryCore:
else:
path = os.path.expanduser('~/.config/legendary')
logger.info("Creating config in path: %s", config_path)
with open(os.path.join(path, "config.ini"), "w") as config_file:
with open(os.path.join(path, "config.ini"), "w", encoding="utf-8") as config_file:
config_file.write("[Legendary]")
self.__core = LegendaryCore()

Expand Down
4 changes: 2 additions & 2 deletions rare/shared/wrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def __init__(self):
self.__file = os.path.join(config_dir(), "wrappers.json")
self.__wrappers_dict = {}
try:
with open(self.__file) as f:
with open(self.__file, "r", encoding="utf-8") as f:
self.__wrappers_dict = json.load(f)
except FileNotFoundError:
logger.info("%s does not exist", self.__file)
Expand Down Expand Up @@ -114,7 +114,7 @@ def __save_wrappers(self):
self.__wrappers_dict["wrappers"] = self.__wrappers
self.__wrappers_dict["applists"] = self.__applists

with open(os.path.join(self.__file), "w+") as f:
with open(os.path.join(self.__file), "w+", encoding="utf-8") as f:
json.dump(self.__wrappers_dict, f, default=lambda o: vars(o), indent=2)


Expand Down
12 changes: 6 additions & 6 deletions rare/utils/compat/steam.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def find_steam() -> Optional[str]:

def find_libraries(steam_path: str) -> Set[str]:
vdf_path = os.path.join(steam_path, "config", "libraryfolders.vdf")
with open(vdf_path, "r") as f:
with open(vdf_path, "r", encoding="utf-8") as f:
libraryfolders = vdf.load(f)["libraryfolders"]
# libraries = [os.path.join(folder["path"], "steamapps") for key, folder in libraryfolders.items()]
libraries = {os.path.join(folder["path"], "steamapps") for key, folder in libraryfolders.items()}
Expand Down Expand Up @@ -165,7 +165,7 @@ def find_appmanifests(library: str) -> List[dict]:
appmanifests = []
for entry in os.scandir(library):
if entry.is_file() and entry.name.endswith(".acf"):
with open(os.path.join(library, entry.name), "r") as f:
with open(os.path.join(library, entry.name), "r", encoding="utf-8") as f:
appmanifest = vdf.load(f)
appmanifests.append(appmanifest)
return appmanifests
Expand Down Expand Up @@ -206,7 +206,7 @@ def find_runtimes(steam_path: str, library: str) -> Dict[str, SteamRuntime]:
folder = appmanifest["AppState"]["installdir"]
tool_path = os.path.join(common, folder)
if os.path.isfile(vdf_file := os.path.join(tool_path, "toolmanifest.vdf")):
with open(vdf_file, "r") as f:
with open(vdf_file, "r", encoding="utf-8") as f:
toolmanifest = vdf.load(f)
if toolmanifest["manifest"]["compatmanager_layer_name"] == "container-runtime":
runtimes.update(
Expand All @@ -231,7 +231,7 @@ def find_protons(steam_path: str, library: str) -> List[ProtonTool]:
folder = appmanifest["AppState"]["installdir"]
tool_path = os.path.join(common, folder)
if os.path.isfile(vdf_file := os.path.join(tool_path, "toolmanifest.vdf")):
with open(vdf_file, "r") as f:
with open(vdf_file, "r", encoding="utf-8") as f:
toolmanifest = vdf.load(f)
if toolmanifest["manifest"]["compatmanager_layer_name"] == "proton":
protons.append(
Expand Down Expand Up @@ -270,7 +270,7 @@ def find_compatibility_tools(steam_path: str) -> List[CompatibilityTool]:
if not os.path.isfile(tool_vdf):
continue

with open(tool_vdf, "r") as f:
with open(tool_vdf, "r", encoding="utf-8") as f:
compatibilitytool = vdf.load(f)

entry_tools = compatibilitytool["compatibilitytools"]["compat_tools"]
Expand All @@ -285,7 +285,7 @@ def find_compatibility_tools(steam_path: str) -> List[CompatibilityTool]:
if not os.path.isfile(manifest_vdf):
continue

with open(manifest_vdf, "r") as f:
with open(manifest_vdf, "r", encoding="utf-8") as f:
manifest = vdf.load(f)

tools.append(
Expand Down
2 changes: 1 addition & 1 deletion rare/utils/steam_grades.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def load_json() -> dict:
ids = {}
for app in apps:
ids[app["name"]] = app["appid"]
with open(file, "w") as f:
with open(file, "w", encoding="utf-8") as f:
f.write(orjson.dumps(ids).decode("utf-8"))
return ids
else:
Expand Down
6 changes: 3 additions & 3 deletions rare/utils/steam_shortcuts.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def find_steam_users(steam_path: str) -> List[SteamUser]:
vdf_path = os.path.join(steam_path, "config", "loginusers.vdf")
if not os.path.exists(vdf_path):
return _users
with open(vdf_path, 'r') as f:
with open(vdf_path, "r", encoding="utf-8") as f:
users = vdf.load(f).get("users", {})
for long_id, user in users.items():
_users.append(SteamUser(long_id, user))
Expand All @@ -56,7 +56,7 @@ def _load_shortcuts(steam_path: str, user: SteamUser) -> Dict[str, SteamShortcut
vdf_path = os.path.join(steam_path, "userdata", str(user.short_id), "config", "shortcuts.vdf")
if not os.path.exists(vdf_path):
return _shortcuts
with open(vdf_path, 'rb') as f:
with open(vdf_path, "rb") as f:
shortcuts = vdf.binary_load(f).get("shortcuts", {})
for idx, shortcut in shortcuts.items():
_shortcuts[idx] = SteamShortcut.from_dict(shortcut)
Expand All @@ -66,7 +66,7 @@ def _load_shortcuts(steam_path: str, user: SteamUser) -> Dict[str, SteamShortcut
def _save_shortcuts(steam_path: str, user: SteamUser, shortcuts: Dict[str, SteamShortcut]) -> None:
_shortcuts = {k: asdict(v) for k, v in shortcuts.items()}
vdf_path = os.path.join(steam_path, "userdata", str(user.short_id), "config", "shortcuts.vdf")
with open(vdf_path, 'wb') as f:
with open(vdf_path, "wb") as f:
vdf.binary_dump({"shortcuts": _shortcuts}, f)


Expand Down

0 comments on commit 3f99773

Please sign in to comment.