-
-
Notifications
You must be signed in to change notification settings - Fork 9
Polyscript config #110
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
Merged
Merged
Polyscript config #110
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fef548b
Add Config
joukepouke dc3a10f
Actually load the mod
joukepouke 5e8328b
Slight refactor and fix of warning
joukepouke 442a47f
Use System.Text.json instead of newtonsoft
joukepouke 782ff77
add missing using statement
joukepouke 0a90f23
Fix 2 warnings
joukepouke a5d4c2e
Fix bug of game crashing on assembly load...
joukepouke e8181eb
other languages in localization
joukepouke fcc0da7
rename polyscriptmod to polymod
joukepouke 9ceb5bc
Add gld config
joukepouke 2b34285
Actually load config
joukepouke af3fcf2
Revert "rename polyscriptmod to polymod"
joukepouke 7a60848
Fix bug related to game crashing on launch
joukepouke 9e4827b
On error, set mod to errrror instead of crashing
joukepouke b8c9889
Change naming for @jkdev
joukepouke 1788148
change name of polyScriptMod to PolyScript
joukepouke 9260014
fix
joukepouke 7a51f79
minor fix
joukepouke 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||
| <Costura> | ||
| <IncludeAssemblies> | ||
| Scriban | ||
| </IncludeAssemblies> | ||
| </Costura> | ||
| </Weavers> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
|
|
||
| namespace PolyMod.Managers; | ||
|
|
||
| /// <summary> | ||
| /// Allows mods to save config. | ||
| /// </summary> | ||
| public class Config<T> where T : class | ||
| { | ||
| private T? currentConfig; | ||
| private readonly string modName; | ||
| private readonly ConfigTypes configType; | ||
| private static readonly string ExposedConfigPath = Path.Combine(Plugin.BASE_PATH, "mods.json"); | ||
| private readonly string perModConfigPath; | ||
| private T? defaultConfig; | ||
| public Config(string modName, ConfigTypes configType) | ||
| { | ||
| this.modName = modName; | ||
| this.configType = configType; | ||
| perModConfigPath = Path.Combine(Plugin.MODS_PATH, $"{modName}.json"); | ||
| Load(); | ||
| } | ||
|
|
||
| internal void Load() // can be called internally if config changes; gui config not implemented yet | ||
| { | ||
| switch (configType) | ||
| { | ||
| case ConfigTypes.PerMod: | ||
| { | ||
| if (!File.Exists(perModConfigPath)) | ||
| { | ||
| return; | ||
| } | ||
| var jsonText = File.ReadAllText(perModConfigPath); | ||
| currentConfig = JsonSerializer.Deserialize<T>(jsonText); | ||
| break; | ||
| } | ||
| case ConfigTypes.Exposed: | ||
| { | ||
| if (!File.Exists(ExposedConfigPath)) | ||
| { | ||
| return; | ||
| } | ||
| var jsonText = File.ReadAllText(ExposedConfigPath); | ||
| currentConfig = JsonNode.Parse(jsonText)![modName]?.Deserialize<T>(); | ||
| break; | ||
| } | ||
| default: | ||
| throw new ArgumentOutOfRangeException(); | ||
| } | ||
| } | ||
| /// <summary> | ||
| /// Sets the default if the config does not exist yet. Always call this before reading from the config. | ||
| /// </summary> | ||
| public void SetDefaultConfig(T defaultValue) | ||
| { | ||
| defaultConfig = defaultValue; | ||
| if (currentConfig is not null) return; | ||
| Write(defaultConfig); | ||
| SaveChanges(); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Writes the **entire** config. Usage not recommended, use Edit() instead | ||
| /// </summary> | ||
| public void Write(T config) | ||
| { | ||
| currentConfig = config; | ||
| } | ||
| /// <summary> | ||
| /// Gets the config. Should only be called after setting a default. | ||
| /// </summary> | ||
| public T Get() | ||
| { | ||
| return currentConfig ?? throw new InvalidOperationException("Must set default before reading config."); | ||
| } | ||
| /// <summary> | ||
| /// Edits the config. Should only be called after setting a default. | ||
| /// </summary> | ||
| /// <remarks>Call SaveChanges after editing</remarks> | ||
| public void Edit(Action<T> editor) | ||
| { | ||
| editor(currentConfig ?? throw new InvalidOperationException("Must set default before reading config.")); | ||
| } | ||
| /// <summary> | ||
| /// Gets part of the config. Should only be called after setting a default | ||
| /// </summary> | ||
| public TResult Get<TResult>(Func<T, TResult> getter) | ||
| { | ||
| return getter(currentConfig ?? throw new InvalidOperationException("Must set default before reading config.")); | ||
| } | ||
| /// <summary> | ||
| /// Writes the config to disk | ||
| /// </summary> | ||
| public void SaveChanges() | ||
| { | ||
| switch (configType) | ||
| { | ||
| case ConfigTypes.PerMod: | ||
| var perModJson = JsonSerializer.Serialize(currentConfig, new JsonSerializerOptions { WriteIndented = true }); | ||
| File.WriteAllText(perModConfigPath, perModJson); | ||
| break; | ||
| case ConfigTypes.Exposed: | ||
| var modsConfigText = File.ReadAllText(ExposedConfigPath); | ||
| var modsConfigJson = JsonNode.Parse(modsConfigText)!.AsObject(); | ||
| modsConfigJson[modName] = JsonSerializer.SerializeToNode(currentConfig!); | ||
| File.WriteAllText(ExposedConfigPath, modsConfigJson.ToJsonString(new JsonSerializerOptions { WriteIndented = true })); | ||
| break; | ||
| default: | ||
| throw new ArgumentOutOfRangeException(); | ||
| } | ||
| } | ||
|
|
||
| public enum ConfigTypes | ||
| { | ||
| PerMod, | ||
| Exposed | ||
| } | ||
| } |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using Scriban; | ||
| using Scriban.Runtime; | ||
|
|
||
| namespace PolyMod.Managers; | ||
|
|
||
| public class GldConfigTemplate | ||
| { | ||
| private static readonly string ConfigPath = Path.Combine(Plugin.BASE_PATH, "mods.json"); | ||
|
|
||
| private readonly string templateText; | ||
| private JsonObject currentConfig = new(); | ||
| private string modName; | ||
|
|
||
| public GldConfigTemplate(string templateText, string modName) | ||
| { | ||
| this.templateText = templateText; | ||
| this.modName = modName; | ||
| Load(); | ||
| } | ||
| private void Load() | ||
| { | ||
| if (File.Exists(ConfigPath)) | ||
| { | ||
| var json = File.ReadAllText(ConfigPath); | ||
| if (JsonNode.Parse(json) is JsonObject modsConfig | ||
| && modsConfig.TryGetPropertyValue(modName, out var modConfigNode) | ||
| && modConfigNode is JsonObject modConfig) | ||
| { | ||
| currentConfig = modConfig; | ||
| return; | ||
| } | ||
| } | ||
| currentConfig = new JsonObject(); | ||
| } | ||
|
|
||
| public string? Render() | ||
| { | ||
| if (!templateText.Contains("{{")) return templateText; | ||
| var template = Template.Parse(templateText); | ||
| var context = new TemplateContext(); | ||
| var scriptObject = new ScriptObject(); | ||
|
|
||
| bool changedConfig = false; | ||
| scriptObject.Import("config", | ||
| new Func<string, string, string>((key, defaultValue) => | ||
| { | ||
| if (currentConfig.TryGetPropertyValue(key, out var token) && token != null) | ||
| { | ||
| return token.ToString(); | ||
| } | ||
|
|
||
| changedConfig = true; | ||
| currentConfig[key] = defaultValue; | ||
|
|
||
| return defaultValue; | ||
| }) | ||
| ); | ||
| context.PushGlobal(scriptObject); | ||
| string? result; | ||
| try | ||
| { | ||
| result = template.Render(context); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| Plugin.logger.LogError("error during parse of gld patch template: " + e.ToString()); | ||
| result = null; | ||
| } | ||
| if (changedConfig) | ||
| { | ||
| SaveChanges(); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| public void SaveChanges() | ||
| { | ||
| JsonObject modsConfigJson; | ||
| if (File.Exists(ConfigPath)) | ||
| { | ||
| var modsConfigText = File.ReadAllText(ConfigPath); | ||
| modsConfigJson = (JsonNode.Parse(modsConfigText) as JsonObject) ?? new JsonObject(); | ||
| } | ||
| else | ||
| { | ||
| modsConfigJson = new JsonObject(); | ||
| } | ||
|
|
||
| modsConfigJson[modName] = currentConfig; | ||
| File.WriteAllText(ConfigPath, modsConfigJson.ToJsonString(new JsonSerializerOptions { WriteIndented = true })); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.