-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (123 loc) · 4.6 KB
/
index.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* for global variables see
https://z-edit.github.io/#/docs?t=Modules%2FStandard_Modules */
const fs = require('fs');
const path = require('path');
const textureSetCreator = require(`${modulePath}/src/textureSetCreator.js`);
ngapp.run(function (contextMenuFactory, nodeHelpers, settingsService) {
settingsService.registerSettings({
label: 'Texture Set Creator Settings',
templateUrl: `${modulePath}/partials/settings.html`,
controller: 'settingsController',
defaultSettings: {
textureSetCreator: {
variantNamePosition: 'suffix',
variantNameFormat: ' - $variant',
},
},
});
contextMenuFactory.treeViewItems.insertAfter(
(item) => item.id === 'Automate',
{
id: 'textureSetCreator',
visible: () => true,
build: (scope, items) => {
items.push({
label: 'Create texture sets',
hotkey: '',
callback: () =>
scope.$emit('openModal', 'configPicker', {
templateUrl: `${modulePath}/partials/configPicker.html`,
selectedRecords: scope.selectedNodes
.filter((node) =>
nodeHelpers.isRecordNode(node)
)
.map((node) => node.handle),
}),
});
},
}
);
});
ngapp.service('textureSetCreatorService', function (settingsService) {
this.run = (config, records) => {
let txCreator = new textureSetCreator(xelib, logger, {
variantNamePosition:
settingsService.settings.textureSetCreator.variantNamePosition,
variantNameFormat:
settingsService.settings.textureSetCreator.variantNameFormat,
});
txCreator.createAndSetTextureSets(config, records);
};
});
// this code makes the service accessible from
// zEdit scripts and UPF patchers
ngapp.run(function (textureSetCreatorService, interApiService) {
interApiService.register({
api: { textureSetCreatorService },
});
});
ngapp.controller('settingsController', function ($scope) {
$scope.variantPositions = ['prefix', 'suffix'];
});
ngapp.controller(
'configPickerModalController',
function (textureSetCreatorService, progressService, $scope) {
let configNames = fs.readdirSync(`${modulePath}/configs`);
let configOptions = configNames.map((item) => {
return {
id: path.join(`${modulePath}/configs`, item),
label: item,
};
});
$scope.configData = {
selected: configOptions[0],
options: configOptions,
};
$scope.selectFile = () => {
let selectedFile = fh.selectFile(
'Select config.',
`${modulePath}/configs`,
[{ name: 'JSON', extensions: ['json'] }]
);
if (selectedFile) {
$scope.configData.options.push({
id: selectedFile,
label: fh.getFileName(selectedFile),
});
$scope.configData.selected =
$scope.configData.options[
$scope.configData.options.length - 1
];
}
};
$scope.apply = () => {
logger.log('Running Texture Set Creator.');
try {
$scope.$emit('closeModal');
progressService.showProgress({ determinate: false });
let config = require($scope.configData.selected.id);
textureSetCreatorService.run(
config,
$scope.modalOptions.selectedRecords
);
logger.log('Texture Set Creator finished.');
progressService.hideProgress();
} catch (error) {
logger.log('Texture Set Creator failed.');
if (error.name === 'Validation Error') {
logger.log(
'Config ' +
$scope.configData.selected.label +
' is invalid:'
);
for (let err of error.errors) {
logger.log(err.property + ' ' + err.message);
}
} else {
logger.log(error.toString());
}
progressService.hideProgress();
}
};
}
);