Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the behavior of config file savings (makes it not truncate exiting file) #11

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions Injector/Main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ func loadConfig():
config.autoUpdateDisalbedMods = obj["autoUpdateDisalbedMods"]
SettingsPage.onLoaded()

# This patch will allow for externally made changes (e.g., by mods)
# to stay persistent. Before this patch this function would always
# truncate the existing config file and then store its content. Now
# it is loading the existing file, updates known keys and then
# storing all of its new content to file.

# Maybe this can be flashed out by offering some interface that would
# allow mods to read/store config data in their own section:
# { "mods": [ {"mod_id": "allowSomething: true} ] }

# It would be kinda better to have each mod create its own config file
# in a subfolder called 'config' next to the 'mods' directory (similar
# to Minecraft modpacks). By offering an interface this could help form
# a standard. - Fabian, 26 Jan 2025

func saveConfig():
var jarr = {
"customModDir": config.customModDir,
Expand All @@ -54,9 +69,38 @@ func saveConfig():
"allowModAutoUpdates": config.allowModAutoUpdates,
"autoUpdateDisalbedMods": config.autoUpdateDisalbedMods
}
var jstr = JSON.stringify(jarr)

var config_indent = " "
var config_sort_keys = false
var config_json : String = ""

if not FileAccess.file_exists(configPath):
config_json = JSON.stringify(jarr, config_indent, config_sort_keys)

else:
# read original
var file_old = FileAccess.open(configPath, FileAccess.READ)
var config_obj = JSON.new()

# parse
var err = config_obj.parse(file_old.get_as_text())
file_old.close()
if err != OK:
OS.alert(
"Failed while parsing existing config file. "+ \
"Fix the error or delete the file to restore orginal config then try again:\n\n" + \
"[Line %d]:\n\n%s\n\n%s" % [config_obj.get_error_line(), config_obj.get_error_message(), configPath],
"VostokMods - Config could not be saved")
return

# update values
for key in jarr:
config_obj.data[key] = jarr.get(key)

config_json = JSON.stringify(config_obj.data, config_indent, config_sort_keys)

var f = FileAccess.open(configPath, FileAccess.WRITE)
f.store_string(jstr)
f.store_string(config_json)
f.flush()
f.close()

Expand Down