Created
December 31, 2022 01:04
-
-
Save Varriount/161beabe461936fb580e10d14c2b3e64 to your computer and use it in GitHub Desktop.
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
// language=Handlebars | |
const RAW_CLASS_TEMPLATE = ` | |
class SettingsClass extends {{baseClassName}} { | |
namespace = {{toStringLiteral namespace}} | |
//// Private Methods //// | |
static _register() { | |
{{#each settings as | setting |}} | |
game.settings.register( | |
this.namespace, | |
{{toStringLiteral setting.keyName}}, | |
{ | |
...{{toObjectLiteral 16 setting.config}}, | |
callback: function(value) { | |
{{setting.fieldName}} = value; | |
} | |
}, | |
); | |
{{/each}} | |
} | |
//// Property Logic //// | |
{{#each settings as | setting |}} | |
// Logic for {{setting.keyName}}/{{setting.fieldName}} | |
_{{setting.fieldName}} = null; | |
get {{setting.fieldName}}() { | |
{{toThisProperty setting.fieldName}}; | |
} | |
set {{setting.fieldName}}(value) { | |
{{toThisProperty setting.fieldName}} = value; | |
game.settings.set(this.namespace, {{toStringLiteral setting.keyName}}, value); | |
} | |
{{/each}} | |
} | |
return SettingsClass | |
` | |
// Constants | |
const Handlebars = Handlebars.create(); | |
const CLASS_TEMPLATE = Handlebars.compile(RAW_CLASS_TEMPLATE, {noEscape: true}) | |
// Helpers | |
Handlebars.registerHelper('toStringLiteral', function (value) { | |
return '"' + value + '"'; | |
}) | |
Handlebars.registerHelper('toThisProperty', function (value) { | |
return 'this._' + value; | |
}) | |
Handlebars.registerHelper('toObjectLiteral', function (count, value) { | |
const lead = '\n' + ' '.repeat(count) | |
return JSON | |
.stringify(value, null, ' ') | |
.replaceAll('\n', lead); | |
}) | |
// API | |
function createSettingsClass(settings) { | |
return Function(CLASS_TEMPLATE(settings))() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment