@cabin-icarus/tooltip_generator 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,362 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path'
3
+ import { KVObject, serialize } from 'valve-kv'
4
+ import { LocalizationData, AbilityLocalization, ModifierLocalization, StandardLocalization } from "./localizationInterfaces";
5
+ import { Language } from "~resource/languages"
6
+
7
+ export class LocalizationCompiler
8
+ {
9
+ addon_filepath: string = path.join("node_modules/~resource", "addon_");
10
+ filepath_format: string = ".txt";
11
+ currentWrites: Set<string> = new Set();
12
+ writeTimeout?: NodeJS.Timeout;
13
+
14
+ // Helper functions
15
+ TransformForLocalization(text: string, modifier: boolean): string
16
+ {
17
+ if (modifier)
18
+ {
19
+ text = text.replace(/\{([^f]\w+)\}($|[^%])/g, "%d$1%$2")
20
+ text = text.replace(/\{([^f]\w+)\}%/g, "%d$1%%%")
21
+ text = text.replace(/\{f(\w+)\}($|[^%])/g, "%f$1%$2")
22
+ text = text.replace(/\{f(\w+)\}%/g, "%f$1%%%");
23
+ text = text.replace(/%\{([^f]\w+)\}/g, "%%%d$1%")
24
+ text = text.replace(/%\{f(\w+)\}/g, "%%%f$1%");
25
+
26
+ return text;
27
+ }
28
+ else
29
+ {
30
+ text = text.replace(/\{(\w*)}($|[^%])/g, "%$1%$2")
31
+ text = text.replace(/\{(\w*)}%/g, "%$1%%%");
32
+ text = text.replace(/%\{(\w*)}/g, "%%%$1%");
33
+
34
+ return text;
35
+ }
36
+ }
37
+
38
+ OnLocalizationDataChanged(allData: {[path: string]: LocalizationData})
39
+ {
40
+ // console.log("Localization event fired");
41
+ let Abilities: Array<AbilityLocalization> = new Array<AbilityLocalization>();
42
+ let Modifiers: Array<ModifierLocalization> = new Array<ModifierLocalization>();
43
+ let StandardTooltips: Array<StandardLocalization> = new Array<StandardLocalization>();
44
+ //let Talents: Array<HeroTalents> = new Array<HeroTalents>();
45
+
46
+ const localization_info: LocalizationData =
47
+ {
48
+ AbilityArray: Abilities,
49
+ ModifierArray: Modifiers,
50
+ StandardArray: StandardTooltips,
51
+ // TalentArray: Talents,
52
+ };
53
+
54
+ for (const [key, data] of Object.entries(allData)) {
55
+ if (data.AbilityArray) {
56
+ Array.prototype.push.apply(Abilities, data.AbilityArray);
57
+ }
58
+ if (data.ModifierArray) {
59
+ Array.prototype.push.apply(Modifiers, data.ModifierArray);
60
+ }
61
+ if (data.StandardArray) {
62
+ Array.prototype.push.apply(StandardTooltips, data.StandardArray);
63
+ }
64
+ }
65
+
66
+ // Generate information for every language
67
+ const languages = Object.values(Language).filter(v => typeof v !== "number");
68
+ for (const language of languages)
69
+ {
70
+ if (language !== Language.None)
71
+ {
72
+ const tokens: KVObject = this.GenerateContentStringForLanguage(language, localization_info);
73
+ this.WriteContentToAddonFile(language, tokens);
74
+ }
75
+ }
76
+ }
77
+
78
+ GenerateContentStringForLanguage(language: string, localized_data: LocalizationData): KVObject
79
+ {
80
+ let tokens: KVObject = {}
81
+
82
+ // Go over standard tooltips
83
+ if (localized_data.StandardArray) {
84
+ for (const standardLocalization of localized_data.StandardArray)
85
+ {
86
+ // Check for name override for the language we're checking
87
+ let standard_tooltip_string = standardLocalization.name;
88
+
89
+ if (standardLocalization.language_overrides && standardLocalization.language_overrides.length > 0)
90
+ {
91
+ for (const language_override of standardLocalization.language_overrides)
92
+ {
93
+ if (language_override.language === language)
94
+ {
95
+ standard_tooltip_string = language_override.name_override;
96
+ }
97
+ }
98
+ }
99
+
100
+ tokens[standardLocalization.classname] = standard_tooltip_string;
101
+ }
102
+ }
103
+
104
+ // Go over abilities for this language
105
+ if (localized_data.AbilityArray) {
106
+ for (const ability of localized_data.AbilityArray)
107
+ {
108
+ // Class name is identical for all languages, so we would always use it
109
+ const ability_string = `DOTA_Tooltip_Ability_${ability.ability_classname}`;
110
+
111
+ // Name
112
+ let ability_name = ability.name;
113
+ let ability_description = ability.description;
114
+ //let reimagined_effects = ability.reimagined_effects;
115
+ let ability_lore = ability.lore;
116
+ let ability_notes = ability.notes;
117
+ let scepter_description = ability.scepter_description;
118
+ let shard_description = ability.shard_description;
119
+ let ability_specials = ability.ability_specials;
120
+
121
+ if (ability.language_overrides)
122
+ {
123
+ for (const language_override of ability.language_overrides)
124
+ {
125
+ if (language_override.language === language)
126
+ {
127
+ // Check for name override
128
+ if (language_override.name_override)
129
+ {
130
+ ability_name = language_override.name_override;
131
+ }
132
+
133
+ // Check for description overrides
134
+ if (language_override.description_override)
135
+ {
136
+ ability_description = language_override.description_override;
137
+ }
138
+
139
+ // Check for reimagined effect overrides
140
+ //if (language_override.reimagined_effects_override)
141
+ //{
142
+ //reimagined_effects = language_override.reimagined_effects_override;
143
+ //}
144
+
145
+ // Check for lore override
146
+ if (language_override.lore_override)
147
+ {
148
+ ability_lore = language_override.lore_override;
149
+ }
150
+
151
+ // Check for note override
152
+ if (language_override.notes_override)
153
+ {
154
+ ability_notes = language_override.notes_override;
155
+ }
156
+
157
+ // Check for scepter override
158
+ if (language_override.scepter_description_override)
159
+ {
160
+ scepter_description = language_override.scepter_description_override;
161
+ }
162
+
163
+ // Check for shard override
164
+ if (language_override.shard_description_override)
165
+ {
166
+ shard_description = language_override.shard_description_override;
167
+ }
168
+
169
+ // Check for ability specials override, if any
170
+ if (language_override.ability_specials_override)
171
+ {
172
+ ability_specials = language_override.ability_specials_override;
173
+ }
174
+ }
175
+ }
176
+ }
177
+
178
+ // Add name localization
179
+ if (ability_name)
180
+ {
181
+ tokens[ability_string] = ability_name;
182
+ }
183
+
184
+ // Add description localization
185
+ if (ability_description)
186
+ {
187
+ ability_description = this.TransformForLocalization(ability_description, false);
188
+ tokens[`${ability_string}_description`] = ability_description;
189
+ }
190
+
191
+ // Lore, if any
192
+ if (ability_lore)
193
+ {
194
+ const transformed_lore = this.TransformForLocalization(ability_lore, false);
195
+ tokens[`${ability_string}_Lore`] = transformed_lore;
196
+ }
197
+
198
+ // Notes, if any
199
+ if (ability_notes)
200
+ {
201
+ let counter = 0;
202
+ for (const note of ability_notes)
203
+ {
204
+ const transformed_note = this.TransformForLocalization(note, false);
205
+ tokens[`${ability_string}_Note${counter}`] = transformed_note;
206
+
207
+ counter++;
208
+ }
209
+ }
210
+
211
+ // Scepter, if any
212
+ if (scepter_description)
213
+ {
214
+ const ability_scepter_description = this.TransformForLocalization(scepter_description, false);
215
+ tokens[`${ability_string}_scepter_description`] = ability_scepter_description;
216
+ }
217
+
218
+ // Shard, if any
219
+ if (shard_description)
220
+ {
221
+ const ability_shard_description = this.TransformForLocalization(shard_description, false);
222
+ tokens[`${ability_string}_shard_description`] = ability_shard_description;
223
+ }
224
+
225
+ // Ability specials, if any
226
+ if (ability_specials)
227
+ {
228
+ for (const ability_special of ability_specials)
229
+ {
230
+ // Construct the ability special
231
+ let ability_special_text = "";
232
+
233
+ if (ability_special.percentage)
234
+ {
235
+ ability_special_text = "%";
236
+ }
237
+ else if (ability_special.item_stat)
238
+ {
239
+ ability_special_text = "+$"
240
+ }
241
+
242
+ ability_special_text += ability_special.text;
243
+
244
+ tokens[`${ability_string}_${ability_special.ability_special}`] = ability_special_text;
245
+ }
246
+ }
247
+ }
248
+ }
249
+
250
+ // Go over modifiers
251
+ if (localized_data.ModifierArray) {
252
+ for (const modifier of localized_data.ModifierArray)
253
+ {
254
+ const modifier_string = `DOTA_Tooltip_${modifier.modifier_classname}`;
255
+
256
+ // Name
257
+ let modifier_name = modifier.name;
258
+ let modifier_description = modifier.description;
259
+
260
+ if (modifier.language_overrides)
261
+ {
262
+ for (const language_override of modifier.language_overrides)
263
+ {
264
+ if (language_override.language === language)
265
+ {
266
+ // Name overrides for a specific language, if necessary
267
+ if (language_override.name_override)
268
+ {
269
+ modifier_name = language_override.name_override;
270
+ }
271
+
272
+ // Description overrides for a specific language, if necessary
273
+ if (language_override.description_override)
274
+ {
275
+ modifier_description = language_override.description_override;
276
+ }
277
+ }
278
+ }
279
+ }
280
+
281
+ // Add name to localization string
282
+ if (modifier_name)
283
+ {
284
+ tokens[modifier_string] = modifier_name;
285
+ }
286
+
287
+ // Add description to localization string
288
+ if (modifier_description)
289
+ {
290
+ modifier_description = this.TransformForLocalization(modifier_description, true);
291
+ tokens[`${modifier_string}_description`] = modifier_description;
292
+ }
293
+ }
294
+ }
295
+
296
+ return tokens;
297
+ }
298
+
299
+ WriteContentToAddonFile(language: string, tokens: KVObject)
300
+ {
301
+ // Set based on language
302
+ const filepath = this.addon_filepath + language.toString() + this.filepath_format;
303
+
304
+ // Add the opening tokens
305
+ const kv = {lang: { Language: language, Tokens: tokens}}
306
+
307
+ // Serialize!
308
+ let write_string = serialize(kv);
309
+
310
+ // Try writing if the output is not busy
311
+ this.TryWriting(filepath, write_string, () => {
312
+
313
+ // Prepare the output
314
+ this.currentWrites.add(language);
315
+ if (this.writeTimeout) {
316
+ clearTimeout(this.writeTimeout);
317
+ }
318
+ this.writeTimeout = setTimeout(() => this.FinishWrite(), 500);
319
+ });
320
+ }
321
+
322
+ TryWriting(filepath: string, write_string: string, success: () => void, tries: number = 0) {
323
+ if (tries > 5) {
324
+ console.log("\x1b[31m%s\x1b[0m", "Tried too many times to write without success!");
325
+ return;
326
+ }
327
+ try {
328
+ // Remove file contents, or create a fresh one if it doesn't exists yet.
329
+ const fd = fs.openSync(filepath, 'w');
330
+ fs.closeSync(fd);
331
+
332
+ // Write to the file
333
+ fs.writeFileSync(filepath, write_string);
334
+ } catch {
335
+ // try again after a short time
336
+ setTimeout(() => this.TryWriting(filepath, write_string, success, tries + 1), 1000);
337
+ return;
338
+ }
339
+ success();
340
+ }
341
+
342
+ // Print the output
343
+ FinishWrite() {
344
+ if (this.currentWrites.size === 0) return;
345
+ let languages: string[] = [];
346
+ for (const language of this.currentWrites.values()) {
347
+ languages.push(language);
348
+ }
349
+ this.currentWrites.clear();
350
+ let languageText = languages[0];
351
+ if (languages.length > 1) {
352
+ let lastLang = languages.splice(-1)[0];
353
+ let otherLangs = languages.join(", ");
354
+ if (otherLangs !== "") {
355
+ languageText = otherLangs;
356
+ }
357
+ languageText += " and " + lastLang;
358
+ }
359
+ console.log(`Finished writing tooltips for ${languageText}`);
360
+ console.log("");
361
+ }
362
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,100 @@
1
+ import { Language } from "./languages";
2
+
3
+ export interface AbilityLocalization
4
+ {
5
+ ability_classname: string;
6
+ name?: string;
7
+ description?: string;
8
+ scepter_description?: string;
9
+ shard_description?: string;
10
+ lore?: string;
11
+ notes?: Array<string>;
12
+ //reimagined_effects?: Array<ReimaginedEffect>;
13
+ ability_specials?: Array<AbilitySpecialLocalization>;
14
+ language_overrides?: Array<AbilityLocalizationContent>;
15
+ }
16
+
17
+ export interface AbilityLocalizationContent
18
+ {
19
+ language: Language;
20
+ name_override?: string;
21
+ description_override?: string;
22
+ scepter_description_override?: string;
23
+ shard_description_override?: string;
24
+ lore_override?: string;
25
+ notes_override?: Array<string>;
26
+ //reimagined_effects_override?: Array<ReimaginedEffect>;
27
+ ability_specials_override?: Array<AbilitySpecialLocalization>;
28
+ }
29
+
30
+ export interface ModifierLocalization
31
+ {
32
+ modifier_classname: string;
33
+ name?: string;
34
+ description?: string;
35
+ language_overrides?: Array<ModifierLocalizationContent>;
36
+ }
37
+
38
+ export interface ModifierLocalizationContent
39
+ {
40
+ language: Language;
41
+ name_override?: string
42
+ description_override?: string;
43
+ }
44
+
45
+ export interface StandardLocalization
46
+ {
47
+ classname: string;
48
+ name: string;
49
+ language_overrides?: StandardLocalizationNameOverride[]
50
+ }
51
+
52
+ export interface StandardLocalizationNameOverride
53
+ {
54
+ language: Language;
55
+ name_override: string
56
+ }
57
+
58
+ // export interface ReimaginedEffect
59
+ // {
60
+ // title: string;
61
+ // description: string;
62
+ // }
63
+
64
+ export interface AbilitySpecialLocalization
65
+ {
66
+ ability_special: string;
67
+ text: string;
68
+ percentage?: boolean; // false by default if omitted
69
+ item_stat?: boolean // false by default if omitted
70
+ }
71
+
72
+ // export interface HeroTalents
73
+ // {
74
+ // talent_classname: string;
75
+ // talents: Array<HeroTalentLocalization>;
76
+ // }
77
+
78
+ // export interface HeroTalentLocalization
79
+ // {
80
+ // name: string;
81
+ // description: string;
82
+ // lore: string;
83
+ // language_overrides?: Array<TalentLocalizationOverrides>
84
+ // }
85
+
86
+ // export interface TalentLocalizationOverrides
87
+ // {
88
+ // language: Language;
89
+ // name_override?: string;
90
+ // description_override?: string;
91
+ // lore_override?: string;
92
+ // }
93
+
94
+ export interface LocalizationData
95
+ {
96
+ AbilityArray?: Array<AbilityLocalization>;
97
+ ModifierArray?: Array<ModifierLocalization>;
98
+ StandardArray?: Array<StandardLocalization>;
99
+ //TalentArray?: Array<HeroTalents>;
100
+ }
package/package.json ADDED
@@ -0,0 +1,49 @@
1
+ {
2
+ "author": {
3
+ "name": "Shush"
4
+ },
5
+ "bugs": {
6
+ "url": "https://github.com/CabinIkaros/tooltip_generator/issues"
7
+ },
8
+ "bundleDependencies": false,
9
+ "dependencies": {
10
+ "link-module-alias": "^1.2.0",
11
+ "node-watch": "^0.7.1",
12
+ "npm-run-all": "^4.1.5",
13
+ "typescript": "^4.1.3",
14
+ "valve-kv": "^1.1.0"
15
+ },
16
+ "deprecated": false,
17
+ "description": "Code generator that makes creating tooltips for Dota 2 custom games much easier.",
18
+ "devDependencies": {
19
+ "@types/node": "^14.14.22",
20
+ "fs-extra": "^9.1.0",
21
+ "ts-node": "^10.9.2",
22
+ "node-xlsx": "^0.23.0"
23
+ },
24
+ "homepage": "https://github.com/CabinIkaros/tooltip_generator#readme",
25
+ "keywords": [
26
+ "Dota",
27
+ "Custom",
28
+ "Game",
29
+ "Tooltip",
30
+ "Generator"
31
+ ],
32
+ "license": "ISC",
33
+ "name": "@cabin-icarus/tooltip_generator",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/CabinIkaros/tooltip_generator.git"
37
+ },
38
+ "_moduleAliases": {
39
+ "~resource": "resource",
40
+ "~generator": "node_modules/@cabin-icarus/tooltip_generator"
41
+ },
42
+ "scripts": {
43
+ "postinstall": "ts-node install.ts",
44
+ "dev": "run-p dev:*",
45
+ "dev:runlocalizationWatcher": "ts-node node_modules/~generator/fsWatcher.ts",
46
+ "init": "link-module-alias && ts-node node_modules/~generator/init.ts"
47
+ },
48
+ "version": "1.7.0"
49
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "compilerOptions": {
3
+ /* Visit https://aka.ms/tsconfig.json to read more about this file */
4
+
5
+ /* Basic Options */
6
+ // "incremental": true, /* Enable incremental compilation */
7
+ "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
8
+ "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
9
+ // "lib": [], /* Specify library files to be included in the compilation. */
10
+ // "allowJs": true, /* Allow javascript files to be compiled. */
11
+ // "checkJs": true, /* Report errors in .js files. */
12
+ // "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
13
+ // "declaration": true, /* Generates corresponding '.d.ts' file. */
14
+ // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */
15
+ // "sourceMap": true, /* Generates corresponding '.map' file. */
16
+ // "outFile": "./", /* Concatenate and emit output to single file. */
17
+ // "outDir": "./", /* Redirect output structure to the directory. */
18
+ // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
19
+ // "composite": true, /* Enable project compilation */
20
+ // "tsBuildInfoFile": "./", /* Specify file to store incremental compilation information */
21
+ // "removeComments": true, /* Do not emit comments to output. */
22
+ // "noEmit": true, /* Do not emit outputs. */
23
+ // "importHelpers": true, /* Import emit helpers from 'tslib'. */
24
+ "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
25
+ // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
26
+
27
+ /* Strict Type-Checking Options */
28
+ "strict": true, /* Enable all strict type-checking options. */
29
+ // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
30
+ // "strictNullChecks": true, /* Enable strict null checks. */
31
+ // "strictFunctionTypes": true, /* Enable strict checking of function types. */
32
+ // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
33
+ // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
34
+ // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
35
+ // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
36
+
37
+ /* Additional Checks */
38
+ // "noUnusedLocals": true, /* Report errors on unused locals. */
39
+ // "noUnusedParameters": true, /* Report errors on unused parameters. */
40
+ // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
41
+ // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
42
+
43
+ /* Module Resolution Options */
44
+ // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
45
+ // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
46
+ // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
47
+ // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
48
+ // "typeRoots": [], /* List of folders to include type definitions from. */
49
+ // "types": [], /* Type declaration files to be included in compilation. */
50
+ // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
51
+ "esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
52
+ // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
53
+ // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
54
+
55
+ /* Source Map Options */
56
+ // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
57
+ // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
58
+ // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
59
+ // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
60
+
61
+ /* Experimental Options */
62
+ // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
63
+ // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
64
+
65
+ /* Advanced Options */
66
+ "skipLibCheck": true, /* Skip type checking of declaration files. */
67
+ "forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
68
+ "types": [
69
+ "@types/node"
70
+ ]
71
+ },
72
+ }