Skip to content

Commit

Permalink
Fix validProperties when importing from debug file
Browse files Browse the repository at this point in the history
  • Loading branch information
Larkinabout committed Sep 24, 2024
1 parent e4ce655 commit e7ff0c0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"compatibility": [
{
"minimum": "3.3.1",
"verified": "4.0.1",
"verified": "4.0.2",
"maximum": "4"
}
]
Expand Down
2 changes: 1 addition & 1 deletion scripts/conditions.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function setConfig (data = null) {

config.statusEffects.push({
...(value.hud !== undefined && { hud: value.hud }),
_id: `dnd5e${key}00000000000`.substring(0, 16),
_id: dnd5e.utils.staticID(`dnd5e${key}`),
id: key,
img: value.icon,
...(value.levels !== undefined && { levels: value.levels }),
Expand Down
47 changes: 46 additions & 1 deletion scripts/debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export async function exportData () {
skills: CONFIG.DND5E.skills,
spellSchools: CONFIG.DND5E.spellSchools,
toolIds: CONFIG.DND5E.toolIds,
validProperties: CONFIG.DND5E.validProperties,
validProperties: convertSetsToArrays(CONFIG.DND5E.validProperties),
weaponIds: CONFIG.DND5E.weaponIds,
weaponProficiencies: CONFIG.DND5E.weaponProficiencies,
weaponProficienciesMap: CONFIG.DND5E.weaponProficienciesMap,
Expand Down Expand Up @@ -132,6 +132,7 @@ export async function importData () {
async function processImport (file) {
const json = await readTextFromFile(file)
const jsonData = JSON.parse(json)
jsonData.configDnd5e.validProperties = convertArraysToSets(jsonData.configDnd5e.validProperties)
const currentVersion = game.modules.get(MODULE.ID).version.split('.').slice(0, 2).join('.')
const fileVersion = jsonData.customDnd5eVersion.split('.').slice(0, 2).join('.')
if (fileVersion !== currentVersion) {
Expand Down Expand Up @@ -200,3 +201,47 @@ async function overwriteConfigDnd5e (configDnd5e) {

Logger.info(game.i18n.localize('CUSTOM_DND5E.dialog.importData.configImported'), true)
}

/**
* Convert sets to arrays for export to JSON
* @param {object} obj The object containing the sets
* @returns {object} The object with the sets converted to arrays
*/
function convertSetsToArrays (obj) {
if (obj instanceof Set) {
return [...obj] // Convert Set to array
}

if (Array.isArray(obj)) {
return obj.map(item => convertSetsToArrays(item)) // Recursively convert inside arrays
}

if (typeof obj === 'object' && obj !== null) {
// Recursively convert inside objects
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, convertSetsToArrays(value)])
)
}

return obj
}

/**
* Convert arrays to sets
* @param {object} obj The object containing the arrays
* @returns {object} The object with the arrays converted to sets
*/
function convertArraysToSets (obj) {
if (Array.isArray(obj)) {
return new Set(obj) // Convert arrayto Set
}

if (typeof obj === 'object' && obj !== null) {
// Recursively convert inside objects
return Object.fromEntries(
Object.entries(obj).map(([key, value]) => [key, convertArraysToSets(value)])
)
}

return obj
}

0 comments on commit e7ff0c0

Please sign in to comment.