-
Notifications
You must be signed in to change notification settings - Fork 17
/
clouds.js
131 lines (115 loc) · 4.56 KB
/
clouds.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
/**
* Clouds editor module.
* @module
*/
define([
'lodash',
'services/engine-service',
'services/project-service',
'services/file-system-service'
], function (_) {
"use strict";
/** @type {FileSystemService} */
const fs = require('services/file-system-service');
/** @type {EngineService} */
const engineService = require('services/engine-service');
/** @type {ProjectService} */
const projectService = require('services/project-service');
const CLOUDS_RENDER_CONFIG_NAME = "clouds-resources/clouds";
/**
* Get the project root path.
* @returns {Promise.<string>}
*/
function getProjectPath() {
return projectService.getCurrentProjectPath();
}
function getEngineSettingsFilePath() {
return getProjectPath().then(function (projectPath) {
return fs.join(projectPath, "settings.ini");
});
}
/**
* Get project engine settings.
* @return {Promise.<object>} SJSON of the engine settings.
*/
function getEngineSettings() {
return getEngineSettingsFilePath().then(function (engineSettingsFilePath) {
return fs.readJSON(engineSettingsFilePath);
});
}
/**
* Checks if the projects contains the clouds render config.
*/
function checkCloudsRenderConfigExtension () {
return getEngineSettings().then(function (engineSettings) {
/** @type {string[]} */
let renderConfigExtensions = engineSettings.render_config_extensions;
if (!_.isArray(renderConfigExtensions))
return false;
return renderConfigExtensions.indexOf(CLOUDS_RENDER_CONFIG_NAME) >= 0;
});
}
/**
* Saves settings to the engine settings.ini file.
* @param {object} engineSettings - Settings to be saved.
* @return {Promise} Resolves when the file is saved.
*/
function saveEngineSettings(engineSettings) {
return getEngineSettingsFilePath().then(function (engineSettingsFilePath) {
return fs.writeJSON(engineSettingsFilePath, engineSettings);
});
}
/**
* Make sure the project settings.ini contains the clouds render config.
* @return {Promise} Resolves when the project engine settings contains the clouds render config.
*/
function setupRenderConfigExtension () {
return checkCloudsRenderConfigExtension().then(function (exists) {
if (exists)
return;
// Add the clouds render config extension to the settings.ini
//
console.info('Adding clouds render config extension to engine settings (`settings.ini`)');
return getEngineSettings().then(function (engineSettings) {
engineSettings.render_config_extensions = engineSettings.render_config_extensions || [];
engineSettings.render_config_extensions.push(CLOUDS_RENDER_CONFIG_NAME);
return saveEngineSettings(engineSettings);
}).then(function () {
return engineService.compile();
}).then(function () {
// TODO: Ideally the engine API would allow us to refresh
// render config extensions instead of killing the engine and restart it.
return engineService.restartEditorEngine();
});
});
}
/**
* Add a clouds entity to the current level.
* @param {LevelEditingService} levelEditingService - used to spawn the entity into the current level.
* @return {Promise} Resolves when the clouds entity is added to the level.
*/
function createClouds (levelEditingService) {
// TODO: Check if a clouds entity already exists in the current level?
return levelEditingService.invoke('SpawnEntity', 'Clouds', 'clouds-resources/clouds').then(function () {
return engineService.sendToLocalEditors(`
require "clouds-resources/clouds"
local result = Clouds:generate()
`).then(console.debug.bind(console, "Clouds generated"));
});
}
/**
* Clouds editor module.
* @namespace
* @type {object}
*/
var Clouds = {};
Clouds.createClouds = function () {
return setupRenderConfigExtension().then(function () {
return Promise.require(['services/level-editing-service'])
.spread(function (levelEditingService) {
return createClouds(levelEditingService);
});
});
};
return Clouds;
});