forked from Miskatonic-Investigative-Society/CoC7-FoundryVTT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-compendiums.js
107 lines (103 loc) · 3.94 KB
/
generate-compendiums.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import * as crypto from 'crypto'
import * as fs from 'fs'
import glob from './node_modules/glob/glob.js'
import { loadAll } from './node_modules/js-yaml/index.js'
const lang = 'en'
let foundryVTTFile = ''
let check = ''
const collisions = {}
if (fs.existsSync(check = './system.json')) {
foundryVTTFile = check
} else if (fs.existsSync(check = './module.json')) {
foundryVTTFile = check
}
if (foundryVTTFile !== '') {
const packs = {}
const json = JSON.parse(fs.readFileSync(foundryVTTFile, 'utf8'))
for (const pack of json.packs) {
const match = pack.path.match(/([^\\/]+)\.db$/)
if (match) {
packs[match[1]] = pack.type
}
}
console.log('Known packs', packs)
const dbList = glob.sync('./compendiums/*.yaml', {})
for (const filename of dbList) {
const match = filename.match(/^\.\/compendiums\/([^/]+)\.yaml$/)
if (match) {
const fileslug = match[1]
if (typeof packs[fileslug] !== 'undefined') {
const type = packs[fileslug]
let yaml = loadAll(fs.readFileSync(filename, 'utf8'))
yaml = yaml.filter(doc => doc).map(doc => {
if (!doc._id) {
// Make sure we don't generate new ids everytime we rebuild
doc._id = generateBuildConsistentID(doc.name + lang + JSON.stringify(doc.flags.cocidFlag.eras))
}
doc.flags = {
CoC7: doc.flags
}
switch (type) {
case 'Item':
switch (doc.type) {
case 'skill': {
const match = doc.name.match(/^(.+)\s*\((.+)\)$/)
if (match) {
doc.system.skillName = match[2].trim()
doc.system.specialization = match[1].trim()
} else {
doc.system.skillName = doc.name
doc.system.specialization = ''
}
break
}
}
break
case 'RollTable':
if (typeof doc.results !== 'undefined') {
let range = 0
for (const offset in doc.results) {
if (!doc.results[offset]._id) {
doc.results[offset]._id = generateBuildConsistentID(doc.name + lang + JSON.stringify(doc.flags.CoC7.cocidFlag.eras) + offset)
}
if (!doc.results[offset].range) {
range++
doc.results[offset].range = [range, range]
} else {
range = doc.results[offset].range[1]
}
}
if (!doc.formula) {
doc.formula = '1d' + range
}
}
}
return doc
})
const packDir = './packs/'
const file = packDir + fileslug + '.db'
if (!fs.existsSync(packDir)) {
fs.mkdirSync(packDir, { recursive: true })
}
console.log('Generating: ' + file)
fs.writeFileSync(file, yaml.map(doc => JSON.stringify(doc)).join('\n'))
}
}
}
}
/**
* generateBuildConsistentID uses idSource to generate an id that will be consistent across builds.
*
* Note: This is called outside of foundry to build manual .db files and substitutes foundry.utils.randomID in a build consistent way.
* It creates a hash from the key so it is consistent between builds and the converts to base 64 so it uses the same range of characters as FoundryVTTs generator.
* @param {string} idSource
* @returns id, an string of 16 characters with the id consistently generated from idSource
*/
function generateBuildConsistentID (idSource) {
let id = crypto.createHash('md5').update(idSource).digest('base64').replace(/[\\+=\\/]/g, '').substring(0, 16)
while (typeof collisions[id] !== 'undefined') {
console.log('collision on ' + idSource)
id = crypto.createHash('md5').update(idSource + Math.random().toString()).digest('base64').replace(/[\\+=\\/]/g, '').substring(0, 16)
}
return id
}