-
-
Notifications
You must be signed in to change notification settings - Fork 373
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
Add safe config updating #7224
Open
Efnilite
wants to merge
29
commits into
dev/feature
Choose a base branch
from
feature/safe-config-update
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add safe config updating #7224
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ef27c83
add key updating
Efnilite f9bcc6c
update VoidNode
Efnilite e80baf3
add getIndex to Node
Efnilite 49a6bfa
replace insert with add at index to match List
Efnilite 802db39
ensure correct order when adding entries
Efnilite ea09b63
update InvalidNode
Efnilite ef91bf5
update SimpleNode
Efnilite e7d2501
update EntryNode
Efnilite 777861d
add iterator for VoidNodes for comments, update SectionNode
Efnilite 4b0ed3c
add getting a node from full idx
Efnilite c6c20a4
added comparisons between diff lines, getting path, reformatted
Efnilite 830e758
update to make it actually work
Efnilite a2b87a6
add formatted debug method
Efnilite 2c09b12
fix error message
Efnilite ce406eb
reformatted, reduced visibility of getAt
Efnilite 5156672
update node comparisons
Efnilite 9bba911
finalize
Efnilite e04340b
update docs
Efnilite 5cd0756
backup before updating, lol
Efnilite cec1734
Merge branch 'dev/feature' into feature/safe-config-update
Efnilite f894839
Update src/main/java/ch/njol/skript/config/Config.java
Efnilite 20d7fdf
requested changes
Efnilite 5f883ee
change to map
Efnilite 4a16762
fix paths containing dots causing problems for getPath
Efnilite 5a0c42c
Merge branch 'dev/feature' into feature/safe-config-update
Efnilite 081c8c8
update
Efnilite 2c3b70c
revert reformatting of node classes
Efnilite bc40b24
add missing methods to Node
Efnilite 937b7a4
Merge branch 'dev/feature' into feature/safe-config-update
Efnilite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,6 @@ | |
import ch.njol.skript.config.EnumParser; | ||
import ch.njol.skript.config.Option; | ||
import ch.njol.skript.config.OptionSection; | ||
import ch.njol.skript.config.SectionNode; | ||
import ch.njol.skript.hooks.Hook; | ||
import ch.njol.skript.hooks.VaultHook; | ||
import ch.njol.skript.hooks.regions.GriefPreventionHook; | ||
|
@@ -332,111 +331,72 @@ private static void userDisableHooks(Class<? extends Hook<?>> hookClass, boolean | |
/** | ||
* This should only be used in special cases | ||
*/ | ||
@Nullable | ||
public static Config getConfig() { | ||
public static @Nullable Config getConfig() { | ||
return mainConfig; | ||
} | ||
|
||
// also used for reloading | ||
static boolean load() { | ||
|
||
/** | ||
* Reloads the main config file. | ||
*/ | ||
static void load() { | ||
try { | ||
final File oldConfigFile = new File(Skript.getInstance().getDataFolder(), "config.cfg"); | ||
final File configFile = new File(Skript.getInstance().getDataFolder(), "config.sk"); | ||
if (oldConfigFile.exists()) { | ||
if (!configFile.exists()) { | ||
oldConfigFile.renameTo(configFile); | ||
Skript.info("[1.3] Renamed your 'config.cfg' to 'config.sk' to match the new format"); | ||
} else { | ||
Skript.error("Found both a new and an old config, ignoring the old one"); | ||
} | ||
} | ||
File configFile = new File(Skript.getInstance().getDataFolder(), "config.sk"); | ||
|
||
if (!configFile.exists()) { | ||
Skript.error("Config file 'config.sk' does not exist!"); | ||
return false; | ||
return; | ||
} | ||
if (!configFile.canRead()) { | ||
Skript.error("Config file 'config.sk' cannot be read!"); | ||
return false; | ||
return; | ||
} | ||
Config mc; | ||
|
||
Config mainConfig; | ||
try { | ||
mc = new Config(configFile, false, false, ":"); | ||
} catch (final IOException e) { | ||
Skript.error("Could not load the main config: " + e.getLocalizedMessage()); | ||
return false; | ||
mainConfig = new Config(configFile, false, false, ":"); | ||
} catch (IOException ex) { | ||
Skript.exception(ex, "Could not load the main config"); | ||
return; | ||
} | ||
mainConfig = mc; | ||
SkriptConfig.mainConfig = mainConfig; | ||
|
||
String configVersion = mc.get(version.key); | ||
String configVersion = mainConfig.get(version.key); | ||
if (configVersion == null || Skript.getVersion().compareTo(new Version(configVersion)) != 0) { | ||
try { | ||
final InputStream in = Skript.getInstance().getResource("config.sk"); | ||
if (in == null) { | ||
if (!mainConfig.getMainNode().isValid()) { | ||
Skript.error("Your config is outdated, but cannot be updated because it contains errors."); | ||
return; | ||
} | ||
|
||
try (InputStream stream = Skript.getInstance().getResource("config.sk")) { | ||
if (stream == null) { | ||
Skript.error("Your config is outdated, but Skript couldn't find the newest config in its jar."); | ||
return false; | ||
} | ||
final Config newConfig = new Config(in, "Skript.jar/config.sk", false, false, ":"); | ||
in.close(); | ||
|
||
boolean forceUpdate = false; | ||
|
||
if (mc.getMainNode().get("database") != null) { // old database layout | ||
forceUpdate = true; | ||
try { | ||
final SectionNode oldDB = (SectionNode) mc.getMainNode().get("database"); | ||
assert oldDB != null; | ||
final SectionNode newDBs = (SectionNode) newConfig.getMainNode().get(databases.key); | ||
assert newDBs != null; | ||
final SectionNode newDB = (SectionNode) newDBs.get("database 1"); | ||
assert newDB != null; | ||
|
||
newDB.setValues(oldDB); | ||
|
||
// '.db' was dynamically added before | ||
final String file = newDB.getValue("file"); | ||
assert file != null; | ||
if (!file.endsWith(".db")) | ||
newDB.set("file", file + ".db"); | ||
|
||
final SectionNode def = (SectionNode) newDBs.get("default"); | ||
assert def != null; | ||
def.set("backup interval", "" + mc.get("variables backup interval")); | ||
} catch (final Exception e) { | ||
Skript.error("An error occurred while trying to update the config's database section."); | ||
Skript.error("You'll have to update the config yourself:"); | ||
Skript.error("Open the new config.sk as well as the created backup, and move the 'database' section from the backup to the start of the 'databases' section"); | ||
Skript.error("of the new config (i.e. the line 'databases:' should be directly above 'database:'), and add a tab in front of every line that you just copied."); | ||
return false; | ||
} | ||
return; | ||
} | ||
|
||
if (newConfig.setValues(mc, version.key, databases.key) || forceUpdate) { // new config is different | ||
final File bu = FileUtils.backup(configFile); | ||
newConfig.getMainNode().set(version.key, Skript.getVersion().toString()); | ||
if (mc.getMainNode().get(databases.key) != null) | ||
newConfig.getMainNode().set(databases.key, mc.getMainNode().get(databases.key)); | ||
mc = mainConfig = newConfig; | ||
mc.save(configFile); | ||
Skript.info("Your configuration has been updated to the latest version. A backup of your old config file has been created as " + bu.getName()); | ||
} else { // only the version changed | ||
mc.getMainNode().set(version.key, Skript.getVersion().toString()); | ||
mc.save(configFile); | ||
Config newConfig = new Config(stream, "Skript.jar/config.sk", false, false, ":"); | ||
|
||
File backup = FileUtils.backup(configFile); | ||
boolean updated = mainConfig.updateNodes(newConfig); | ||
// mainConfig.getMainNode().set(version.key, Skript.getVersion().toString()); TODO FOR TESTING! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just so i dont forget |
||
mainConfig.save(configFile); | ||
SkriptConfig.mainConfig = mainConfig; | ||
|
||
if (updated) { | ||
Skript.info("Your configuration has been updated to the latest version. " + | ||
"A backup of your old config file has been created as " + backup.getName()); | ||
} else { | ||
Skript.info("Your configuration is outdated, but no changes were performed. " + | ||
"A backup of your config file has been created as " + backup.getName()); | ||
} | ||
} catch (final IOException e) { | ||
Skript.error("Could not load the new config from the jar file: " + e.getLocalizedMessage()); | ||
} catch (IOException ex) { | ||
Skript.exception(ex, "Could not update the main config"); | ||
return; | ||
} | ||
} | ||
|
||
mc.load(SkriptConfig.class); | ||
|
||
// if (!keepConfigsLoaded.value()) | ||
// mainConfig = null; | ||
} catch (final RuntimeException e) { | ||
Skript.exception(e, "An error occurred while loading the config"); | ||
return false; | ||
|
||
mainConfig.load(SkriptConfig.class); | ||
} catch (RuntimeException ex) { | ||
Skript.exception(ex, "An error occurred while loading the config"); | ||
} | ||
return true; | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried this behavior isn't maintained. Can you show the results of some tests with a config file updating from like, 2.4 -> 2.9.5 and likewise with 2.4->2.10? I'm not entirely sure how it worked tbh.