mirrored from https://projects.blender.org/blender/blender.git
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
wm_files.c
3361 lines (2782 loc) · 101 KB
/
wm_files.c
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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
* All rights reserved.
*/
/** \file
* \ingroup wm
*
* User level access for blend file read/write, file-history and user-preferences
* (including relevant operators).
*/
/* placed up here because of crappy
* winsock stuff.
*/
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include "zlib.h" /* wm_read_exotic() */
#ifdef WIN32
/* Need to include windows.h so _WIN32_IE is defined. */
# include <windows.h>
# ifndef _WIN32_IE
/* Minimal requirements for SHGetSpecialFolderPath on MINGW MSVC has this defined already. */
# define _WIN32_IE 0x0400
# endif
/* For SHGetSpecialFolderPath, has to be done before BLI_winstuff
* because 'near' is disabled through BLI_windstuff */
# include "BLI_winstuff.h"
# include <shlobj.h>
#endif
#include "MEM_CacheLimiterC-Api.h"
#include "MEM_guardedalloc.h"
#include "BLI_blenlib.h"
#include "BLI_fileops_types.h"
#include "BLI_linklist.h"
#include "BLI_system.h"
#include "BLI_threads.h"
#include "BLI_timer.h"
#include "BLI_utildefines.h"
#include BLI_SYSTEM_PID_H
#include "BLT_translation.h"
#include "BLF_api.h"
#include "DNA_object_types.h"
#include "DNA_scene_types.h"
#include "DNA_screen_types.h"
#include "DNA_space_types.h"
#include "DNA_userdef_types.h"
#include "DNA_windowmanager_types.h"
#include "DNA_workspace_types.h"
#include "BKE_addon.h"
#include "BKE_appdir.h"
#include "BKE_autoexec.h"
#include "BKE_blender.h"
#include "BKE_blendfile.h"
#include "BKE_callbacks.h"
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_idprop.h"
#include "BKE_lib_id.h"
#include "BKE_lib_override.h"
#include "BKE_main.h"
#include "BKE_packedFile.h"
#include "BKE_report.h"
#include "BKE_scene.h"
#include "BKE_screen.h"
#include "BKE_sound.h"
#include "BKE_undo_system.h"
#include "BKE_workspace.h"
#include "BLO_readfile.h"
#include "BLO_undofile.h" /* to save from an undo memfile */
#include "BLO_writefile.h"
#include "RNA_access.h"
#include "RNA_define.h"
#include "IMB_imbuf.h"
#include "IMB_imbuf_types.h"
#include "IMB_thumbs.h"
#include "ED_datafiles.h"
#include "ED_fileselect.h"
#include "ED_image.h"
#include "ED_outliner.h"
#include "ED_screen.h"
#include "ED_undo.h"
#include "ED_util.h"
#include "ED_view3d.h"
#include "ED_view3d_offscreen.h"
#include "GHOST_C-api.h"
#include "GHOST_Path-api.h"
#include "UI_interface.h"
#include "UI_interface_icons.h"
#include "UI_resources.h"
#include "UI_view2d.h"
/* only to report a missing engine */
#include "RE_engine.h"
#ifdef WITH_PYTHON
# include "BPY_extern_python.h"
# include "BPY_extern_run.h"
#endif
#include "DEG_depsgraph.h"
#include "WM_api.h"
#include "WM_message.h"
#include "WM_toolsystem.h"
#include "WM_types.h"
#include "wm.h"
#include "wm_event_system.h"
#include "wm_files.h"
#include "wm_window.h"
static RecentFile *wm_file_history_find(const char *filepath);
static void wm_history_file_free(RecentFile *recent);
static void wm_history_file_update(void);
static void wm_history_file_write(void);
/* -------------------------------------------------------------------- */
/** \name Misc Utility Functions
* \{ */
void WM_file_tag_modified(void)
{
wmWindowManager *wm = G_MAIN->wm.first;
if (wm->file_saved) {
wm->file_saved = 0;
/* notifier that data changed, for save-over warning or header */
WM_main_add_notifier(NC_WM | ND_DATACHANGED, NULL);
}
}
bool wm_file_or_image_is_modified(const Main *bmain, const wmWindowManager *wm)
{
return !wm->file_saved || ED_image_should_save_modified(bmain);
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Window Matching for File Reading
* \{ */
/**
* To be able to read files without windows closing, opening, moving
* we try to prepare for worst case:
* - active window gets active screen from file
* - restoring the screens from non-active windows
* Best case is all screens match, in that case they get assigned to proper window.
*/
static void wm_window_match_init(bContext *C, ListBase *wmlist)
{
wmWindowManager *wm;
wmWindow *win, *active_win;
*wmlist = G_MAIN->wm;
BLI_listbase_clear(&G_MAIN->wm);
active_win = CTX_wm_window(C);
/* first wrap up running stuff */
/* code copied from wm_init_exit.c */
for (wm = wmlist->first; wm; wm = wm->id.next) {
WM_jobs_kill_all(wm);
for (win = wm->windows.first; win; win = win->next) {
CTX_wm_window_set(C, win); /* needed by operator close callbacks */
WM_event_remove_handlers(C, &win->handlers);
WM_event_remove_handlers(C, &win->modalhandlers);
ED_screen_exit(C, win, WM_window_get_active_screen(win));
}
}
/* reset active window */
CTX_wm_window_set(C, active_win);
/* XXX Hack! We have to clear context menu here, because removing all modalhandlers
* above frees the active menu (at least, in the 'startup splash' case),
* causing use-after-free error in later handling of the button callbacks in UI code
* (see ui_apply_but_funcs_after()).
* Tried solving this by always NULL-ing context's menu when setting wm/win/etc.,
* but it broke popups refreshing (see T47632),
* so for now just handling this specific case here. */
CTX_wm_menu_set(C, NULL);
ED_editors_exit(G_MAIN, true);
}
static void wm_window_substitute_old(wmWindowManager *oldwm,
wmWindowManager *wm,
wmWindow *oldwin,
wmWindow *win)
{
win->ghostwin = oldwin->ghostwin;
win->gpuctx = oldwin->gpuctx;
win->active = oldwin->active;
if (win->active) {
wm->winactive = win;
}
if (oldwm->windrawable == oldwin) {
oldwm->windrawable = NULL;
wm->windrawable = win;
}
/* File loading in background mode still calls this. */
if (!G.background) {
/* Pointer back. */
GHOST_SetWindowUserData(win->ghostwin, win);
}
oldwin->ghostwin = NULL;
oldwin->gpuctx = NULL;
win->eventstate = oldwin->eventstate;
oldwin->eventstate = NULL;
/* ensure proper screen rescaling */
win->sizex = oldwin->sizex;
win->sizey = oldwin->sizey;
win->posx = oldwin->posx;
win->posy = oldwin->posy;
}
static void wm_window_match_keep_current_wm(const bContext *C,
ListBase *current_wm_list,
const bool load_ui,
ListBase *r_new_wm_list)
{
Main *bmain = CTX_data_main(C);
wmWindowManager *wm = current_wm_list->first;
bScreen *screen = NULL;
/* match oldwm to new dbase, only old files */
wm->initialized &= ~WM_WINDOW_IS_INIT;
/* when loading without UI, no matching needed */
if (load_ui && (screen = CTX_wm_screen(C))) {
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
WorkSpace *workspace;
BKE_workspace_layout_find_global(bmain, screen, &workspace);
BKE_workspace_active_set(win->workspace_hook, workspace);
win->scene = CTX_data_scene(C);
/* all windows get active screen from file */
if (screen->winid == 0) {
WM_window_set_active_screen(win, workspace, screen);
}
else {
WorkSpaceLayout *layout_old = WM_window_get_active_layout(win);
WorkSpaceLayout *layout_new = ED_workspace_layout_duplicate(
bmain, workspace, layout_old, win);
WM_window_set_active_layout(win, workspace, layout_new);
}
bScreen *win_screen = WM_window_get_active_screen(win);
win_screen->winid = win->winid;
}
}
*r_new_wm_list = *current_wm_list;
}
static void wm_window_match_replace_by_file_wm(bContext *C,
ListBase *current_wm_list,
ListBase *readfile_wm_list,
ListBase *r_new_wm_list)
{
wmWindowManager *oldwm = current_wm_list->first;
wmWindowManager *wm = readfile_wm_list->first; /* will become our new WM */
bool has_match = false;
/* this code could move to setup_appdata */
/* preserve key configurations in new wm, to preserve their keymaps */
wm->keyconfigs = oldwm->keyconfigs;
wm->addonconf = oldwm->addonconf;
wm->defaultconf = oldwm->defaultconf;
wm->userconf = oldwm->userconf;
BLI_listbase_clear(&oldwm->keyconfigs);
oldwm->addonconf = NULL;
oldwm->defaultconf = NULL;
oldwm->userconf = NULL;
/* ensure making new keymaps and set space types */
wm->initialized = 0;
wm->winactive = NULL;
/* Clearing drawable of before deleting any context
* to avoid clearing the wrong wm. */
wm_window_clear_drawable(oldwm);
/* only first wm in list has ghostwins */
LISTBASE_FOREACH (wmWindow *, win, &wm->windows) {
LISTBASE_FOREACH (wmWindow *, oldwin, &oldwm->windows) {
if (oldwin->winid == win->winid) {
has_match = true;
wm_window_substitute_old(oldwm, wm, oldwin, win);
}
}
}
/* make sure at least one window is kept open so we don't lose the context, check T42303 */
if (!has_match) {
wm_window_substitute_old(oldwm, wm, oldwm->windows.first, wm->windows.first);
}
wm_close_and_free_all(C, current_wm_list);
*r_new_wm_list = *readfile_wm_list;
}
/**
* Match old WM with new, 4 cases:
* 1) No current WM, no WM in file: Make new default.
* 2) No current WM, but WM in file: Keep current WM, do nothing else.
* 3) Current WM, but not in file: Keep current WM, update windows with screens from file.
* 4) Current WM, and WM in file: Try to keep current GHOST windows, use WM from file.
*
* \param r_new_wm_list: Return argument for the wm list to be used from now on.
*/
static void wm_window_match_do(bContext *C,
ListBase *current_wm_list,
ListBase *readfile_wm_list,
ListBase *r_new_wm_list)
{
if (BLI_listbase_is_empty(current_wm_list)) {
/* case 1 */
if (BLI_listbase_is_empty(readfile_wm_list)) {
Main *bmain = CTX_data_main(C);
/* Neither current, no newly read file have a WM -> add the default one. */
wm_add_default(bmain, C);
*r_new_wm_list = bmain->wm;
}
/* case 2 */
else {
*r_new_wm_list = *readfile_wm_list;
}
}
else {
/* case 3 */
if (BLI_listbase_is_empty(readfile_wm_list)) {
/* We've read file without wm, keep current one entirely alive.
* Happens when reading pre 2.5 files (no WM back then) */
wm_window_match_keep_current_wm(
C, current_wm_list, (G.fileflags & G_FILE_NO_UI) == 0, r_new_wm_list);
}
/* case 4 */
else {
wm_window_match_replace_by_file_wm(C, current_wm_list, readfile_wm_list, r_new_wm_list);
}
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Preferences Initialization & Versioning
* \{ */
/**
* In case #UserDef was read, re-initialize values that depend on it.
*/
static void wm_init_userdef(Main *bmain)
{
/* Not versioning, just avoid errors. */
#ifndef WITH_CYCLES
BKE_addon_remove_safe(&U.addons, "cycles");
#else
UNUSED_VARS(BKE_addon_remove_safe);
#endif
UI_init_userdef();
/* needed so loading a file from the command line respects user-pref T26156. */
SET_FLAG_FROM_TEST(G.fileflags, U.flag & USER_FILENOUI, G_FILE_NO_UI);
/* set the python auto-execute setting from user prefs */
/* enabled by default, unless explicitly enabled in the command line which overrides */
if ((G.f & G_FLAG_SCRIPT_OVERRIDE_PREF) == 0) {
SET_FLAG_FROM_TEST(G.f, (U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0, G_FLAG_SCRIPT_AUTOEXEC);
}
MEM_CacheLimiter_set_maximum(((size_t)U.memcachelimit) * 1024 * 1024);
BKE_sound_init(bmain);
/* Update the temporary directory from the preferences or fallback to the system default. */
BKE_tempdir_init(U.tempdir);
/* Update tablet API preference. */
WM_init_tablet_api();
BLO_sanitize_experimental_features_userpref_blend(&U);
}
/* return codes */
#define BKE_READ_EXOTIC_FAIL_PATH -3 /* file format is not supported */
#define BKE_READ_EXOTIC_FAIL_FORMAT -2 /* file format is not supported */
#define BKE_READ_EXOTIC_FAIL_OPEN -1 /* Can't open the file */
#define BKE_READ_EXOTIC_OK_BLEND 0 /* .blend file */
#if 0
# define BKE_READ_EXOTIC_OK_OTHER 1 /* other supported formats */
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Read Exotic File Formats
*
* Currently only supports '.blend' files,
* we could support registering other file formats and their loaders.
* \{ */
/* intended to check for non-blender formats but for now it only reads blends */
static int wm_read_exotic(const char *name)
{
int len;
gzFile gzfile;
char header[7];
int retval;
/* make sure we're not trying to read a directory.... */
len = strlen(name);
if (len > 0 && ELEM(name[len - 1], '/', '\\')) {
retval = BKE_READ_EXOTIC_FAIL_PATH;
}
else {
gzfile = BLI_gzopen(name, "rb");
if (gzfile == NULL) {
retval = BKE_READ_EXOTIC_FAIL_OPEN;
}
else {
len = gzread(gzfile, header, sizeof(header));
gzclose(gzfile);
if (len == sizeof(header) && STREQLEN(header, "BLENDER", 7)) {
retval = BKE_READ_EXOTIC_OK_BLEND;
}
else {
/* We may want to support loading other file formats
* from their header bytes or file extension.
* This used to be supported in the code below and may be added
* back at some point. */
#if 0
WM_cursor_wait(true);
if (is_foo_format(name)) {
read_foo(name);
retval = BKE_READ_EXOTIC_OK_OTHER;
}
else
#endif
{
retval = BKE_READ_EXOTIC_FAIL_FORMAT;
}
#if 0
WM_cursor_wait(false);
#endif
}
}
}
return retval;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Read Blend-File Shared Utilities
* \{ */
void WM_file_autoexec_init(const char *filepath)
{
if (G.f & G_FLAG_SCRIPT_OVERRIDE_PREF) {
return;
}
if (G.f & G_FLAG_SCRIPT_AUTOEXEC) {
char path[FILE_MAX];
BLI_split_dir_part(filepath, path, sizeof(path));
if (BKE_autoexec_match(path)) {
G.f &= ~G_FLAG_SCRIPT_AUTOEXEC;
}
}
}
void wm_file_read_report(bContext *C, Main *bmain)
{
ReportList *reports = NULL;
Scene *sce;
for (sce = bmain->scenes.first; sce; sce = sce->id.next) {
if (sce->r.engine[0] &&
BLI_findstring(&R_engines, sce->r.engine, offsetof(RenderEngineType, idname)) == NULL) {
if (reports == NULL) {
reports = CTX_wm_reports(C);
}
BKE_reportf(reports,
RPT_ERROR,
"Engine '%s' not available for scene '%s' (an add-on may need to be installed "
"or enabled)",
sce->r.engine,
sce->id.name + 2);
}
}
if (reports) {
if (!G.background) {
WM_report_banner_show();
}
}
}
/**
* Logic shared between #WM_file_read & #wm_homefile_read,
* call before loading a file.
* \note In the case of #WM_file_read the file may fail to load.
* Change here shouldn't cause user-visible changes in that case.
*/
static void wm_file_read_pre(bContext *C, bool use_data, bool UNUSED(use_userdef))
{
if (use_data) {
BKE_callback_exec_null(CTX_data_main(C), BKE_CB_EVT_LOAD_PRE);
BLI_timer_on_file_load();
}
/* Always do this as both startup and preferences may have loaded in many font's
* at a different zoom level to the file being loaded. */
UI_view2d_zoom_cache_reset();
}
/**
* Logic shared between #WM_file_read & #wm_homefile_read,
* updates to make after reading a file.
*/
static void wm_file_read_post(bContext *C,
const bool is_startup_file,
const bool is_factory_startup,
const bool use_data,
const bool use_userdef,
const bool reset_app_template)
{
bool addons_loaded = false;
wmWindowManager *wm = CTX_wm_manager(C);
if (use_data) {
if (!G.background) {
/* remove windows which failed to be added via WM_check */
wm_window_ghostwindows_remove_invalid(C, wm);
}
CTX_wm_window_set(C, wm->windows.first);
}
#ifdef WITH_PYTHON
if (is_startup_file) {
/* possible python hasn't been initialized */
if (CTX_py_init_get(C)) {
bool reset_all = use_userdef;
if (use_userdef || reset_app_template) {
/* Only run when we have a template path found. */
if (BKE_appdir_app_template_any()) {
BPY_run_string_eval(
C, (const char *[]){"bl_app_template_utils", NULL}, "bl_app_template_utils.reset()");
reset_all = true;
}
}
if (reset_all) {
/* sync addons, these may have changed from the defaults */
BPY_run_string_eval(C, (const char *[]){"addon_utils", NULL}, "addon_utils.reset_all()");
}
if (use_data) {
BPY_python_reset(C);
}
addons_loaded = true;
}
}
else {
/* run any texts that were loaded in and flagged as modules */
if (use_data) {
BPY_python_reset(C);
}
addons_loaded = true;
}
#else
UNUSED_VARS(is_startup_file, reset_app_template);
#endif /* WITH_PYTHON */
Main *bmain = CTX_data_main(C);
if (use_userdef) {
if (is_factory_startup) {
BKE_callback_exec_null(bmain, BKE_CB_EVT_LOAD_FACTORY_USERDEF_POST);
}
}
if (use_data) {
/* important to do before NULL'ing the context */
BKE_callback_exec_null(bmain, BKE_CB_EVT_VERSION_UPDATE);
BKE_callback_exec_null(bmain, BKE_CB_EVT_LOAD_POST);
if (is_factory_startup) {
BKE_callback_exec_null(bmain, BKE_CB_EVT_LOAD_FACTORY_STARTUP_POST);
}
}
if (use_data) {
WM_operatortype_last_properties_clear_all();
/* After load post, so for example the driver namespace can be filled
* before evaluating the depsgraph. */
wm_event_do_depsgraph(C, true);
ED_editors_init(C);
#if 1
WM_event_add_notifier(C, NC_WM | ND_FILEREAD, NULL);
#else
WM_msg_publish_static(CTX_wm_message_bus(C), WM_MSG_STATICTYPE_FILE_READ);
#endif
}
/* report any errors.
* currently disabled if addons aren't yet loaded */
if (addons_loaded) {
wm_file_read_report(C, bmain);
}
if (use_data) {
if (!G.background) {
if (wm->undo_stack == NULL) {
wm->undo_stack = BKE_undosys_stack_create();
}
else {
BKE_undosys_stack_clear(wm->undo_stack);
}
BKE_undosys_stack_init_from_main(wm->undo_stack, bmain);
BKE_undosys_stack_init_from_context(wm->undo_stack, C);
}
}
if (use_data) {
if (!G.background) {
/* in background mode this makes it hard to load
* a blend file and do anything since the screen
* won't be set to a valid value again */
CTX_wm_window_set(C, NULL); /* exits queues */
/* Ensure tools are registered. */
WM_toolsystem_init(C);
}
}
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Read Main Blend-File API
* \{ */
bool WM_file_read(bContext *C, const char *filepath, ReportList *reports)
{
/* assume automated tasks with background, don't write recent file list */
const bool do_history_file_update = (G.background == false) &&
(CTX_wm_manager(C)->op_undo_depth == 0);
bool success = false;
const bool use_data = true;
const bool use_userdef = false;
/* so we can get the error message */
errno = 0;
WM_cursor_wait(1);
wm_file_read_pre(C, use_data, use_userdef);
/* first try to append data from exotic file formats... */
/* it throws error box when file doesn't exist and returns -1 */
/* note; it should set some error message somewhere... (ton) */
const int retval = wm_read_exotic(filepath);
/* we didn't succeed, now try to read Blender file */
if (retval == BKE_READ_EXOTIC_OK_BLEND) {
const int G_f_orig = G.f;
ListBase wmbase;
/* put aside screens to match with persistent windows later */
/* also exit screens and editors */
wm_window_match_init(C, &wmbase);
/* confusing this global... */
G.relbase_valid = 1;
success = BKE_blendfile_read(
C,
filepath,
/* Loading preferences when the user intended to load a regular file is a security risk,
* because the excluded path list is also loaded.
* Further it's just confusing if a user loads a file and various preferences change. */
&(const struct BlendFileReadParams){
.is_startup = false,
.skip_flags = BLO_READ_SKIP_USERDEF,
},
reports);
/* BKE_file_read sets new Main into context. */
Main *bmain = CTX_data_main(C);
/* when loading startup.blend's, we can be left with a blank path */
if (BKE_main_blendfile_path(bmain)[0] != '\0') {
G.save_over = 1;
}
else {
G.save_over = 0;
G.relbase_valid = 0;
}
/* this flag is initialized by the operator but overwritten on read.
* need to re-enable it here else drivers + registered scripts wont work. */
if (G.f != G_f_orig) {
const int flags_keep = G_FLAG_ALL_RUNTIME;
G.f &= G_FLAG_ALL_READFILE;
G.f = (G.f & ~flags_keep) | (G_f_orig & flags_keep);
}
/* match the read WM with current WM */
wm_window_match_do(C, &wmbase, &bmain->wm, &bmain->wm);
WM_check(C); /* opens window(s), checks keymaps */
if (success) {
if (do_history_file_update) {
wm_history_file_update();
}
}
wm_file_read_post(C, false, false, use_data, use_userdef, false);
}
#if 0
else if (retval == BKE_READ_EXOTIC_OK_OTHER) {
BKE_undo_write(C, "Import file");
}
#endif
else if (retval == BKE_READ_EXOTIC_FAIL_OPEN) {
BKE_reportf(reports,
RPT_ERROR,
"Cannot read file '%s': %s",
filepath,
errno ? strerror(errno) : TIP_("unable to open the file"));
}
else if (retval == BKE_READ_EXOTIC_FAIL_FORMAT) {
BKE_reportf(reports, RPT_ERROR, "File format is not supported in file '%s'", filepath);
}
else if (retval == BKE_READ_EXOTIC_FAIL_PATH) {
BKE_reportf(reports, RPT_ERROR, "File path '%s' invalid", filepath);
}
else {
BKE_reportf(reports, RPT_ERROR, "Unknown error loading '%s'", filepath);
BLI_assert(!"invalid 'retval'");
}
if (success == false) {
/* remove from recent files list */
if (do_history_file_update) {
RecentFile *recent = wm_file_history_find(filepath);
if (recent) {
wm_history_file_free(recent);
wm_history_file_write();
}
}
}
WM_cursor_wait(0);
return success;
}
static struct {
char app_template[64];
bool override;
} wm_init_state_app_template = {{0}};
/**
* Used for setting app-template from the command line:
* - non-empty string: overrides.
* - empty string: override, using no app template.
* - NULL: clears override.
*/
void WM_init_state_app_template_set(const char *app_template)
{
if (app_template) {
STRNCPY(wm_init_state_app_template.app_template, app_template);
wm_init_state_app_template.override = true;
}
else {
wm_init_state_app_template.app_template[0] = '\0';
wm_init_state_app_template.override = false;
}
}
const char *WM_init_state_app_template_get(void)
{
return wm_init_state_app_template.override ? wm_init_state_app_template.app_template : NULL;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Read Startup & Preferences Blend-File API
* \{ */
/**
* Called on startup, (context entirely filled with NULLs)
* or called for 'New File' both startup.blend and userpref.blend are checked.
*
* \param use_factory_settings:
* Ignore on-disk startup file, use bundled ``datatoc_startup_blend`` instead.
* Used for "Restore Factory Settings".
*
* \param use_userdef: Load factory settings as well as startup file.
* Disabled for "File New" we don't want to reload preferences.
*
* \param filepath_startup_override:
* Optional path pointing to an alternative blend file (may be NULL).
*
* \param app_template_override:
* Template to use instead of the template defined in user-preferences.
* When not-null, this is written into the user preferences.
*/
void wm_homefile_read(bContext *C,
ReportList *reports,
bool use_factory_settings,
bool use_empty_data,
bool use_data,
bool use_userdef,
const char *filepath_startup_override,
const char *app_template_override,
bool *r_is_factory_startup)
{
Main *bmain = G_MAIN; /* Context does not always have valid main pointer here... */
ListBase wmbase;
bool success = false;
bool filepath_startup_is_factory = true;
char filepath_startup[FILE_MAX];
char filepath_userdef[FILE_MAX];
/* When 'app_template' is set:
* '{BLENDER_USER_CONFIG}/{app_template}' */
char app_template_system[FILE_MAX];
/* When 'app_template' is set:
* '{BLENDER_SYSTEM_SCRIPTS}/startup/bl_app_templates_system/{app_template}' */
char app_template_config[FILE_MAX];
eBLOReadSkip skip_flags = 0;
if (use_data == false) {
skip_flags |= BLO_READ_SKIP_DATA;
}
if (use_userdef == false) {
skip_flags |= BLO_READ_SKIP_USERDEF;
}
/* True if we load startup.blend from memory
* or use app-template startup.blend which the user hasn't saved. */
bool is_factory_startup = true;
const char *app_template = NULL;
bool update_defaults = false;
if (filepath_startup_override != NULL) {
/* pass */
}
else if (app_template_override) {
/* This may be clearing the current template by setting to an empty string. */
app_template = app_template_override;
}
else if (!use_factory_settings && U.app_template[0]) {
app_template = U.app_template;
}
const bool reset_app_template = ((!app_template && U.app_template[0]) ||
(app_template && !STREQ(app_template, U.app_template)));
/* options exclude eachother */
BLI_assert((use_factory_settings && filepath_startup_override) == 0);
if ((G.f & G_FLAG_SCRIPT_OVERRIDE_PREF) == 0) {
SET_FLAG_FROM_TEST(G.f, (U.flag & USER_SCRIPT_AUTOEXEC_DISABLE) == 0, G_FLAG_SCRIPT_AUTOEXEC);
}
if (use_data) {
if (reset_app_template) {
/* Always load UI when switching to another template. */
G.fileflags &= ~G_FILE_NO_UI;
}
}
if (use_userdef || reset_app_template) {
#ifdef WITH_PYTHON
/* This only runs once Blender has already started. */
if (CTX_py_init_get(C)) {
/* This is restored by 'wm_file_read_post', disable before loading any preferences
* so an add-on can read their own preferences when un-registering,
* and use new preferences if/when re-registering, see T67577.
*
* Note that this fits into 'wm_file_read_pre' function but gets messy
* since we need to know if 'reset_app_template' is true. */
BPY_run_string_eval(C, (const char *[]){"addon_utils", NULL}, "addon_utils.disable_all()");
}
#endif /* WITH_PYTHON */
}
wm_file_read_pre(C, use_data, use_userdef);
if (use_data) {
G.relbase_valid = 0;
/* put aside screens to match with persistent windows later */
wm_window_match_init(C, &wmbase);
}
filepath_startup[0] = '\0';
filepath_userdef[0] = '\0';
app_template_system[0] = '\0';
app_template_config[0] = '\0';
const char *const cfgdir = BKE_appdir_folder_id(BLENDER_USER_CONFIG, NULL);
if (!use_factory_settings) {
if (cfgdir) {
BLI_path_join(
filepath_startup, sizeof(filepath_startup), cfgdir, BLENDER_STARTUP_FILE, NULL);
filepath_startup_is_factory = false;
if (use_userdef) {
BLI_path_join(
filepath_userdef, sizeof(filepath_startup), cfgdir, BLENDER_USERPREF_FILE, NULL);
}
}
else {
use_factory_settings = true;
}
if (filepath_startup_override) {
BLI_strncpy(filepath_startup, filepath_startup_override, FILE_MAX);
filepath_startup_is_factory = false;
}
}
/* load preferences before startup.blend */
if (use_userdef) {
if (!use_factory_settings && BLI_exists(filepath_userdef)) {
UserDef *userdef = BKE_blendfile_userdef_read(filepath_userdef, NULL);
if (userdef != NULL) {
BKE_blender_userdef_data_set_and_free(userdef);
userdef = NULL;
skip_flags |= BLO_READ_SKIP_USERDEF;
printf("Read prefs: %s\n", filepath_userdef);
}
}
}
if ((app_template != NULL) && (app_template[0] != '\0')) {
if (!BKE_appdir_app_template_id_search(
app_template, app_template_system, sizeof(app_template_system))) {
/* Can safely continue with code below, just warn it's not found. */
BKE_reportf(reports, RPT_WARNING, "Application Template '%s' not found", app_template);
}
/* Insert template name into startup file. */