|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const ts2ResourceInfo = {}; |
| 4 | + |
| 5 | +/** Parses a tsv/csv file with data into an array and a map. |
| 6 | + * |
| 7 | + * The file must have column names in its first row. |
| 8 | + * The column 'id' must be a hex number. It will be used in the map as the key. */ |
| 9 | +ts2ResourceInfo.parseToArrayAndMap = async function (fileUrl, valueSeparator, entryLoadedCallback = entry => {}) { |
| 10 | + let fileFetchResponse = await fetch(fileUrl); |
| 11 | + let fileContents = await fileFetchResponse.text(); |
| 12 | + |
| 13 | + let fileLines = fileContents.split(/\r?\n/g); |
| 14 | + let columnNames = fileLines.shift().split(valueSeparator); // the first line |
| 15 | + |
| 16 | + let valueArray = []; |
| 17 | + let valueMap = new Map(); |
| 18 | + |
| 19 | + fileLines.forEach(line => { |
| 20 | + line = line.trim(); |
| 21 | + if (line.length == 0) |
| 22 | + return; |
| 23 | + |
| 24 | + let values = line.split(valueSeparator).map(value => value.trim()); |
| 25 | + let entry = {}; |
| 26 | + for (let i = 0; i < columnNames.length; i++) { |
| 27 | + entry[columnNames[i]] = |
| 28 | + values[i] === undefined || values[i].length == 0 |
| 29 | + ? null |
| 30 | + : values[i]; |
| 31 | + } |
| 32 | + valueArray.push(entry); |
| 33 | + entryLoadedCallback(entry); |
| 34 | + |
| 35 | + let entryId = parseInt(entry.id, 16); |
| 36 | + valueMap.set(entryId, entry); |
| 37 | + }); |
| 38 | + |
| 39 | + return { |
| 40 | + array: valueArray, |
| 41 | + map: valueMap |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +/* Groups */ |
| 46 | + |
| 47 | +ts2ResourceInfo.initializeGroups = async function () { |
| 48 | + let groupInfos = await ts2ResourceInfo.parseToArrayAndMap('database/ts2-group-info.txt', '\t'); |
| 49 | + |
| 50 | + groupInfos.array.forEach(groupInfo => |
| 51 | + groupInfo.parent = groupInfos.map.get(parseInt(groupInfo.parentId, 16)) ?? null |
| 52 | + ); |
| 53 | + |
| 54 | + ts2ResourceInfo.groupInfoArray = groupInfos.array; |
| 55 | + ts2ResourceInfo.groupInfoMap = groupInfos.map; |
| 56 | +} |
| 57 | + |
| 58 | +ts2ResourceInfo.getAllGroups = function () { |
| 59 | + if (ts2ResourceInfo.groupInfoArray === undefined) |
| 60 | + throw Error('Group info must be initialized first with initializeGroups()'); |
| 61 | + |
| 62 | + return ts2ResourceInfo.groupInfoArray; |
| 63 | +} |
| 64 | + |
| 65 | +ts2ResourceInfo.getGroupById = function (id) { |
| 66 | + if (ts2ResourceInfo.groupInfoMap === undefined) |
| 67 | + throw Error('Group info must be initialized first with initializeGroups()'); |
| 68 | + |
| 69 | + return ts2ResourceInfo.groupInfoMap.get(Number(id)); |
| 70 | +} |
| 71 | + |
| 72 | +/* Types |
| 73 | + ts2-type-info.txt is based on https://github.com/sims-hacker-zone/SimPE/blob/autinerd/avalonia/SimPe/SimPe/Data/FileTypes.cs */ |
| 74 | + |
| 75 | +ts2ResourceInfo.initializeTypes = async function () { |
| 76 | + let typeInfos = await ts2ResourceInfo.parseToArrayAndMap( |
| 77 | + 'database/ts2-type-info.txt', |
| 78 | + '\t', |
| 79 | + entry => entry.canHaveName = !!entry.canHaveName // convert to bool |
| 80 | + ); |
| 81 | + |
| 82 | + ts2ResourceInfo.typeInfoArray = typeInfos.array; |
| 83 | + ts2ResourceInfo.typeInfoMap = typeInfos.map; |
| 84 | +} |
| 85 | + |
| 86 | +ts2ResourceInfo.getAllTypes = function () { |
| 87 | + if (ts2ResourceInfo.typeInfoArray === undefined) |
| 88 | + throw Error('Type info must be initialized first with initializeTypes()'); |
| 89 | + |
| 90 | + return ts2ResourceInfo.typeInfoArray; |
| 91 | +} |
| 92 | + |
| 93 | +ts2ResourceInfo.getTypeById = function (id) { |
| 94 | + if (ts2ResourceInfo.typeInfoMap === undefined) |
| 95 | + throw Error('Type info must be initialized first with initializeTypes()'); |
| 96 | + |
| 97 | + return ts2ResourceInfo.typeInfoMap.get(Number(id)); |
| 98 | +} |
0 commit comments