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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
|
/*************************************************************************/
/* editor_settings.cpp */
/*************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* http://www.godotengine.org */
/*************************************************************************/
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include "editor_settings.h"
#include "os/os.h"
#include "os/dir_access.h"
#include "os/file_access.h"
#include "version.h"
#include "scene/main/scene_main_loop.h"
#include "os/os.h"
#include "scene/main/node.h"
#include "io/resource_loader.h"
#include "io/resource_saver.h"
#include "scene/main/viewport.h"
#include "io/config_file.h"
#include "editor_node.h"
#include "globals.h"
Ref<EditorSettings> EditorSettings::singleton=NULL;
EditorSettings *EditorSettings::get_singleton() {
return singleton.ptr();
}
bool EditorSettings::_set(const StringName& p_name, const Variant& p_value) {
_THREAD_SAFE_METHOD_
if (p_value.get_type()==Variant::NIL)
props.erase(p_name);
else {
if (props.has(p_name))
props[p_name].variant=p_value;
else
props[p_name]=VariantContainer(p_value,last_order++);
}
emit_signal("settings_changed");
return true;
}
bool EditorSettings::_get(const StringName& p_name,Variant &r_ret) const {
_THREAD_SAFE_METHOD_
const VariantContainer *v=props.getptr(p_name);
if (!v)
return false;
r_ret = v->variant;
return true;
}
struct _EVCSort {
String name;
Variant::Type type;
int order;
bool operator<(const _EVCSort& p_vcs) const{ return order< p_vcs.order; }
};
void EditorSettings::_get_property_list(List<PropertyInfo> *p_list) const {
_THREAD_SAFE_METHOD_
const String *k=NULL;
Set<_EVCSort> vclist;
while ((k=props.next(k))) {
const VariantContainer *v=props.getptr(*k);
if (v->hide_from_editor)
continue;
_EVCSort vc;
vc.name=*k;
vc.order=v->order;
vc.type=v->variant.get_type();
vclist.insert(vc);
}
for(Set<_EVCSort>::Element *E=vclist.front();E;E=E->next()) {
int pinfo = PROPERTY_USAGE_STORAGE;
if (!E->get().name.begins_with("_"))
pinfo|=PROPERTY_USAGE_EDITOR;
PropertyInfo pi(E->get().type, E->get().name);
pi.usage=pinfo;
if (hints.has(E->get().name))
pi=hints[E->get().name];
p_list->push_back( pi );
}
}
bool EditorSettings::has(String p_var) const {
_THREAD_SAFE_METHOD_
return props.has(p_var);
}
void EditorSettings::erase(String p_var) {
_THREAD_SAFE_METHOD_
props.erase(p_var);
}
void EditorSettings::raise_order(const String& p_name) {
_THREAD_SAFE_METHOD_
ERR_FAIL_COND(!props.has(p_name));
props[p_name].order=++last_order;
}
Variant _EDITOR_DEF( const String& p_var, const Variant& p_default) {
if (EditorSettings::get_singleton()->has(p_var))
return EditorSettings::get_singleton()->get(p_var);
EditorSettings::get_singleton()->set(p_var,p_default);
return p_default;
}
void EditorSettings::create() {
if (singleton.ptr())
return; //pointless
DirAccess *dir=NULL;
Object *object;
Variant meta;
String config_path;
String config_dir;
String config_file="editor_settings.xml";
if (OS::get_singleton()->has_environment("APPDATA")) {
// Most likely under windows, save here
config_path=OS::get_singleton()->get_environment("APPDATA");
config_dir=String(_MKSTR(VERSION_SHORT_NAME)).capitalize();
} else if (OS::get_singleton()->has_environment("HOME")) {
config_path=OS::get_singleton()->get_environment("HOME");
config_dir="."+String(_MKSTR(VERSION_SHORT_NAME)).to_lower();
}
ObjectTypeDB::register_type<EditorSettings>(); //otherwise it can't be unserialized
String config_file_path;
if (config_path!=""){
dir = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
if (dir->change_dir(config_path)!=OK) {
ERR_PRINT("Cannot find path for config directory!");
memdelete(dir);
goto fail;
}
if (dir->change_dir(config_dir)!=OK) {
dir->make_dir(config_dir);
if (dir->change_dir(config_dir)!=OK) {
ERR_PRINT("Cannot create config directory!");
memdelete(dir);
goto fail;
}
}
if (dir->change_dir("templates")!=OK) {
dir->make_dir("templates");
} else {
dir->change_dir("..");
}
if (dir->change_dir("tmp")!=OK) {
dir->make_dir("tmp");
} else {
dir->change_dir("..");
}
if (dir->change_dir("plugins")!=OK) {
dir->make_dir("plugins");
} else {
dir->change_dir("..");
}
// path at least is validated, so validate config file
config_file_path = config_path+"/"+config_dir+"/"+config_file;
if (!dir->file_exists(config_file)) {
memdelete(dir);
WARN_PRINT("Config file does not exist, creating.")
goto fail;
}
memdelete(dir);
singleton = ResourceLoader::load(config_file_path,"EditorSettings");
if (singleton.is_null()) {
WARN_PRINT("Could not open config file.");
goto fail;
}
singleton->config_file_path=config_file_path;
singleton->settings_path=config_path+"/"+config_dir;
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("EditorSettings: Load OK!");
}
singleton->scan_plugins();
return;
}
fail:
singleton = Ref<EditorSettings>( memnew( EditorSettings ) );
singleton->config_file_path=config_file_path;
singleton->_load_defaults();
singleton->scan_plugins();
}
String EditorSettings::get_settings_path() const {
return settings_path;
}
Error EditorSettings::_load_plugin(const String& p_path, Plugin &plugin) {
if (!FileAccess::exists(p_path))
return ERR_FILE_NOT_FOUND;
Ref<ConfigFile> cf = memnew(ConfigFile);
Error err = cf->load(p_path);
ERR_EXPLAIN("Error loading plugin description for: "+p_path);
ERR_FAIL_COND_V(err!=OK,ERR_CANT_OPEN);
plugin.instance=NULL;
ERR_FAIL_COND_V(!cf->has_section_key("plugin","name"),ERR_INVALID_DATA);
ERR_FAIL_COND_V(!cf->has_section_key("plugin","installs"),ERR_INVALID_DATA);
ERR_FAIL_COND_V(!cf->has_section_key("plugin","author"),ERR_INVALID_DATA);
ERR_FAIL_COND_V(!cf->has_section_key("plugin","version"),ERR_INVALID_DATA);
ERR_FAIL_COND_V(!cf->has_section_key("plugin","script"),ERR_INVALID_DATA);
plugin.name=cf->get_value("plugin","name");
plugin.author=cf->get_value("plugin","author");
plugin.version=cf->get_value("plugin","version");
plugin.script=cf->get_value("plugin","script");
if (cf->has_section_key("plugin","description"))
plugin.description=cf->get_value("plugin","description");
plugin.installs=cf->get_value("plugin","installs");
if (cf->has_section_key("plugin","install_files"))
plugin.install_files=cf->get_value("plugin","install_files");
return OK;
}
void EditorSettings::scan_plugins() {
Map<String,Plugin> new_plugins;
new_plugins.clear();
DirAccess *d = DirAccess::create(DirAccess::ACCESS_FILESYSTEM);
Error err = d->change_dir(get_settings_path().plus_file("plugins"));
if (err!=OK) {
memdelete(d);
ERR_EXPLAIN("Plugin dir does not exist!")
ERR_FAIL_COND(err!=OK);
}
d->list_dir_begin();
String base = d->get_current_dir();
//print_line("list diring on: "+base);
while(true) {
String p = d->get_next();
if (p=="")
break;
if (!d->current_is_dir() || p.begins_with("."))
continue;
String cfpath=d->get_current_dir().plus_file(p+"/plugin.cfg");
Plugin plugin;
Error err = _load_plugin(cfpath,plugin);
ERR_CONTINUE(err!=OK);
if (plugins.has(p))
plugin.instance=plugins[p].instance;
new_plugins[p]=plugin;
}
plugins=new_plugins;
memdelete(d);
}
void EditorSettings::save() {
//_THREAD_SAFE_METHOD_
if (!singleton.ptr())
return;
if (singleton->config_file_path=="") {
ERR_PRINT("Cannot save EditorSettings config, no valid path");
return;
}
Error err = ResourceSaver::save(singleton->config_file_path,singleton);
if (err!=OK) {
ERR_PRINT("Can't Save!");
return;
}
if (OS::get_singleton()->is_stdout_verbose()) {
print_line("EditorSettings Save OK!");
}
}
void EditorSettings::destroy() {
if (!singleton.ptr())
return;
save();
singleton=Ref<EditorSettings>();
}
void EditorSettings::_load_defaults() {
_THREAD_SAFE_METHOD_
set("global/font","");
hints["global/font"]=PropertyInfo(Variant::STRING,"global/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");
set("global/autoscan_project_path","");
hints["global/autoscan_project_path"]=PropertyInfo(Variant::STRING,"global/autoscan_project_path",PROPERTY_HINT_GLOBAL_DIR);
set("global/default_project_path","");
hints["global/default_project_path"]=PropertyInfo(Variant::STRING,"global/default_project_path",PROPERTY_HINT_GLOBAL_DIR);
set("global/default_project_export_path","");
hints["global/default_project_export_path"]=PropertyInfo(Variant::STRING,"global/default_project_export_path",PROPERTY_HINT_GLOBAL_DIR);
set("text_editor/background_color",Color::html("3b000000"));
set("text_editor/text_color",Color::html("aaaaaa"));
set("text_editor/text_selected_color",Color::html("000000"));
set("text_editor/keyword_color",Color::html("ffffb3"));
set("text_editor/base_type_color",Color::html("a4ffd4"));
set("text_editor/engine_type_color",Color::html("83d3ff"));
set("text_editor/comment_color",Color::html("983d1b"));
set("text_editor/string_color",Color::html("ef6ebe"));
set("text_editor/symbol_color",Color::html("badfff"));
set("text_editor/selection_color",Color::html("7b5dbe"));
set("text_editor/idle_parse_delay",2);
set("text_editor/create_signal_callbacks",true);
set("text_editor/autosave_interval_seconds",60);
set("text_editor/font","");
hints["text_editor/font"]=PropertyInfo(Variant::STRING,"text_editor/font",PROPERTY_HINT_GLOBAL_FILE,"*.fnt");
set("text_editor/auto_brace_complete", false);
set("3d_editor/default_fov",45.0);
set("3d_editor/default_z_near",0.1);
set("3d_editor/default_z_far",500.0);
set("3d_editor/navigation_scheme",0);
hints["3d_editor/navigation_scheme"]=PropertyInfo(Variant::INT,"3d_editor/navigation_scheme",PROPERTY_HINT_ENUM,"Godot,Maya,Modo");
set("3d_editor/orbit_modifier",0);
hints["3d_editor/orbit_modifier"]=PropertyInfo(Variant::INT,"3d_editor/orbit_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
set("3d_editor/pan_modifier",1);
hints["3d_editor/pan_modifier"]=PropertyInfo(Variant::INT,"3d_editor/pan_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
set("3d_editor/zoom_modifier",4);
hints["3d_editor/zoom_modifier"]=PropertyInfo(Variant::INT,"3d_editor/zoom_modifier",PROPERTY_HINT_ENUM,"None,Shift,Alt,Meta,Ctrl");
set("2d_editor/bone_width",5);
set("2d_editor/bone_color1",Color(1.0,1.0,1.0,0.9));
set("2d_editor/bone_color2",Color(0.75,0.75,0.75,0.9));
set("2d_editor/bone_selected_color",Color(0.9,0.45,0.45,0.9));
set("2d_editor/bone_ik_color",Color(0.9,0.9,0.45,0.9));
set("on_save/compress_binary_resources",true);
set("on_save/save_modified_external_resources",true);
set("on_save/save_paths_as_relative",false);
set("on_save/save_paths_without_extension",false);
set("text_editor/create_signal_callbacks",true);
set("animation/autorename_animation_tracks",true);
set("property_editor/texture_preview_width",48);
set("help/doc_path","");
set("import/ask_save_before_reimport",false);
set("import/pvrtc_texture_tool","");
#ifdef WINDOWS_ENABLED
hints["import/pvrtc_texture_tool"]=PropertyInfo(Variant::STRING,"import/pvrtc_texture_tool",PROPERTY_HINT_GLOBAL_FILE,"*.exe");
#else
hints["import/pvrtc_texture_tool"]=PropertyInfo(Variant::STRING,"import/pvrtc_texture_tool",PROPERTY_HINT_GLOBAL_FILE,"");
#endif
set("PVRTC/fast_conversion",false);
set("run/auto_save_before_running",true);
set("resources/save_compressed_resources",true);
set("resources/auto_reload_modified_images",true);
}
void EditorSettings::notify_changes() {
_THREAD_SAFE_METHOD_
SceneTree *sml=NULL;
if (OS::get_singleton()->get_main_loop())
sml = OS::get_singleton()->get_main_loop()->cast_to<SceneTree>();
if (!sml) {
print_line("not SML");
return;
}
Node *root = sml->get_root()->get_child(0);
if (!root) {
return;
}
root->propagate_notification(NOTIFICATION_EDITOR_SETTINGS_CHANGED);
}
void EditorSettings::add_property_hint(const PropertyInfo& p_hint) {
_THREAD_SAFE_METHOD_
hints[p_hint.name]=p_hint;
}
bool EditorSettings::is_plugin_enabled(const String& p_plugin) {
if (!has("_plugins/enabled"))
return false;
StringArray sa=get("_plugins/enabled");
for(int i=0;i<sa.size();i++) {
String plugin = sa[i];
if (!plugins.has(plugin))
continue;
if (plugin==p_plugin)
return true;
}
return false;
}
void EditorSettings::enable_plugins() {
// editor plugins
if (has("_plugins/enabled")) {
StringArray sa=get("_plugins/enabled");
for(int i=0;i<sa.size();i++) {
String plugin = sa[i];
if (!plugins.has(plugin))
continue;
if (plugins[plugin].installs)
continue; //not configured here
set_plugin_enabled(plugin,true);
}
}
// installed plugins
List<PropertyInfo> pi;
Globals::get_singleton()->get_property_list(&pi);
for (List<PropertyInfo>::Element *E=pi.front();E;E=E->next()) {
String p = E->get().name;
if (p.begins_with("plugins/")) {
load_installed_plugin(p.replace_first("plugins/",""));
}
}
}
void EditorSettings::load_installed_plugin(const String& p_plugin) {
ERR_FAIL_COND( !Globals::get_singleton()->has("plugins/"+p_plugin) );
String path = Globals::get_singleton()->get("plugins/"+p_plugin);
Plugin plugin;
Error err = _load_plugin(path.plus_file("plugin.cfg"),plugin);
if (err)
return;
print_line("installing plugin...");
EditorPlugin *ep=_load_plugin_editor(path.plus_file(plugin.script));
ERR_FAIL_COND(!ep);
print_line("load!");
EditorNode::add_editor_plugin(ep);
}
EditorPlugin *EditorSettings::_load_plugin_editor(const String& p_path) {
Ref<Script> script = ResourceLoader::load(p_path);
ERR_EXPLAIN("Invalid Script for plugin: "+p_path);
ERR_FAIL_COND_V(script.is_null(),NULL);
ERR_EXPLAIN("Script has errors: "+p_path);
ERR_FAIL_COND_V(!script->can_instance(),NULL);
ERR_EXPLAIN("Script does not inherit EditorPlugin: "+p_path);
ERR_FAIL_COND_V(script->get_instance_base_type().operator String()!="EditorPlugin",NULL);
EditorPlugin *ep = memnew( EditorPlugin );
ep->set_script(script.get_ref_ptr());
if (!ep->get_script_instance()) {
memdelete(ep);
ERR_EXPLAIN("Script could't load: "+p_path);
ERR_FAIL_V(NULL);
}
return ep;
}
void EditorSettings::set_plugin_enabled(const String& p_plugin, bool p_enabled) {
ERR_FAIL_COND(!plugins.has(p_plugin));
if (p_enabled == (plugins[p_plugin].instance!=NULL)) //already enabled or disabled
return;
print_line("REQUEST "+p_plugin+" to "+itos(p_enabled));
StringArray sa;
if (has("_plugins/enabled"))
sa=get("_plugins/enabled");
int idx=-1;
for(int i=0;i<sa.size();i++) {
if (sa[i]==p_plugin) {
idx=i;
break;
}
}
if (p_enabled) {
String ppath = get_settings_path().plus_file("plugins/"+p_plugin+"/"+plugins[p_plugin].script);
EditorPlugin *ep=_load_plugin_editor(ppath);
if (!ep)
return;
plugins[p_plugin].instance=ep;
EditorNode::add_editor_plugin(ep);
if (idx==-1)
sa.push_back(p_plugin);
} else {
print_line("DISABLING");
EditorNode::remove_editor_plugin(plugins[p_plugin].instance);
memdelete(plugins[p_plugin].instance);
plugins[p_plugin].instance=NULL;
if (idx!=-1)
sa.remove(idx);
}
if (sa.size()==0)
set("_plugins/enabled",Variant());
else
set("_plugins/enabled",sa);
}
void EditorSettings::_bind_methods() {
ADD_SIGNAL(MethodInfo("settings_changed"));
}
EditorSettings::EditorSettings() {
//singleton=this;
last_order=0;
_load_defaults();
}
EditorSettings::~EditorSettings() {
// singleton=NULL;
}
|