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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
|
/*
* $Id$
*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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.
*
*
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/blenloader/intern/writefile.c
* \ingroup blenloader
*/
/*
FILEFORMAT: IFF-style structure (but not IFF compatible!)
start file:
BLENDER_V100 12 bytes (versie 1.00)
V = big endian, v = little endian
_ = 4 byte pointer, - = 8 byte pointer
datablocks: also see struct BHead
<bh.code> 4 chars
<bh.len> int, len data after BHead
<bh.old> void, old pointer
<bh.SDNAnr> int
<bh.nr> int, in case of array: amount of structs
data
...
...
Almost all data in Blender are structures. Each struct saved
gets a BHead header. With BHead the struct can be linked again
and compared with StructDNA .
WRITE
Preferred writing order: (not really a must, but why would you do it random?)
Any case: direct data is ALWAYS after the lib block
(Local file data)
- for each LibBlock
- write LibBlock
- write associated direct data
(External file data)
- per library
- write library block
- per LibBlock
- write the ID of LibBlock
- write TEST (128x128, blend file preview, optional)
- write FileGlobal (some global vars)
- write SDNA
- write USER if filename is ~/X.XX/config/startup.blend
*/
#include <math.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "zlib.h"
#ifndef WIN32
#include <unistd.h>
#else
#include "winsock2.h"
#include <io.h>
#include <process.h> // for getpid
#include "BLI_winstuff.h"
#endif
#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
#include "DNA_actuator_types.h"
#include "DNA_brush_types.h"
#include "DNA_camera_types.h"
#include "DNA_cloth_types.h"
#include "DNA_constraint_types.h"
#include "DNA_controller_types.h"
#include "DNA_dynamicpaint_types.h"
#include "DNA_genfile.h"
#include "DNA_group_types.h"
#include "DNA_gpencil_types.h"
#include "DNA_fileglobal_types.h"
#include "DNA_key_types.h"
#include "DNA_lattice_types.h"
#include "DNA_lamp_types.h"
#include "DNA_meta_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_material_types.h"
#include "DNA_node_types.h"
#include "DNA_object_types.h"
#include "DNA_object_force.h"
#include "DNA_packedFile_types.h"
#include "DNA_particle_types.h"
#include "DNA_property_types.h"
#include "DNA_scene_types.h"
#include "DNA_sdna_types.h"
#include "DNA_sequence_types.h"
#include "DNA_sensor_types.h"
#include "DNA_smoke_types.h"
#include "DNA_space_types.h"
#include "DNA_screen_types.h"
#include "DNA_sound_types.h"
#include "DNA_text_types.h"
#include "DNA_view3d_types.h"
#include "DNA_vfont_types.h"
#include "DNA_world_types.h"
#include "DNA_windowmanager_types.h"
#include "MEM_guardedalloc.h" // MEM_freeN
#include "BLI_blenlib.h"
#include "BLI_linklist.h"
#include "BLI_bpath.h"
#include "BLI_utildefines.h"
#include "BKE_action.h"
#include "BKE_blender.h"
#include "BKE_curve.h"
#include "BKE_constraint.h"
#include "BKE_global.h" // for G
#include "BKE_library.h" // for set_listbasepointers
#include "BKE_main.h"
#include "BKE_node.h"
#include "BKE_report.h"
#include "BKE_sequencer.h"
#include "BKE_utildefines.h"
#include "BKE_modifier.h"
#include "BKE_fcurve.h"
#include "BKE_pointcache.h"
#include "BLO_writefile.h"
#include "BLO_readfile.h"
#include "BLO_undofile.h"
#include "readfile.h"
#include <errno.h>
/* ********* my write, buffered writing with minimum size chunks ************ */
#define MYWRITE_BUFFER_SIZE 100000
#define MYWRITE_MAX_CHUNK 32768
typedef struct {
struct SDNA *sdna;
int file;
unsigned char *buf;
MemFile *compare, *current;
int tot, count, error, memsize;
} WriteData;
static WriteData *writedata_new(int file)
{
WriteData *wd= MEM_callocN(sizeof(*wd), "writedata");
/* XXX, see note about this in readfile.c, remove
* once we have an xp lock - zr
*/
if (wd == NULL) return NULL;
wd->sdna= DNA_sdna_from_data(DNAstr, DNAlen, 0);
wd->file= file;
wd->buf= MEM_mallocN(MYWRITE_BUFFER_SIZE, "wd->buf");
return wd;
}
static void writedata_do_write(WriteData *wd, void *mem, int memlen)
{
if ((wd == NULL) || wd->error || (mem == NULL) || memlen < 1) return;
if (wd->error) return;
/* memory based save */
if(wd->current) {
add_memfilechunk(NULL, wd->current, mem, memlen);
}
else {
if (write(wd->file, mem, memlen) != memlen)
wd->error= 1;
}
}
static void writedata_free(WriteData *wd)
{
DNA_sdna_free(wd->sdna);
MEM_freeN(wd->buf);
MEM_freeN(wd);
}
/***/
/**
* Low level WRITE(2) wrapper that buffers data
* @param adr Pointer to new chunk of data
* @param len Length of new chunk of data
* @warning Talks to other functions with global parameters
*/
#define MYWRITE_FLUSH NULL
static void mywrite( WriteData *wd, void *adr, int len)
{
if (wd->error) return;
/* flush helps compression for undo-save */
if(adr==MYWRITE_FLUSH) {
if(wd->count) {
writedata_do_write(wd, wd->buf, wd->count);
wd->count= 0;
}
return;
}
wd->tot+= len;
/* if we have a single big chunk, write existing data in
* buffer and write out big chunk in smaller pieces */
if(len>MYWRITE_MAX_CHUNK) {
if(wd->count) {
writedata_do_write(wd, wd->buf, wd->count);
wd->count= 0;
}
do {
int writelen= MIN2(len, MYWRITE_MAX_CHUNK);
writedata_do_write(wd, adr, writelen);
adr = (char*)adr + writelen;
len -= writelen;
} while(len > 0);
return;
}
/* if data would overflow buffer, write out the buffer */
if(len+wd->count>MYWRITE_BUFFER_SIZE-1) {
writedata_do_write(wd, wd->buf, wd->count);
wd->count= 0;
}
/* append data at end of buffer */
memcpy(&wd->buf[wd->count], adr, len);
wd->count+= len;
}
/**
* BeGiN initializer for mywrite
* @param file File descriptor
* @param write_flags Write parameters
* @warning Talks to other functions with global parameters
*/
static WriteData *bgnwrite(int file, MemFile *compare, MemFile *current)
{
WriteData *wd= writedata_new(file);
if (wd == NULL) return NULL;
wd->compare= compare;
wd->current= current;
/* this inits comparing */
add_memfilechunk(compare, NULL, NULL, 0);
return wd;
}
/**
* END the mywrite wrapper
* @return 1 if write failed
* @return unknown global variable otherwise
* @warning Talks to other functions with global parameters
*/
static int endwrite(WriteData *wd)
{
int err;
if (wd->count) {
writedata_do_write(wd, wd->buf, wd->count);
wd->count= 0;
}
err= wd->error;
writedata_free(wd);
return err;
}
/* ********** WRITE FILE ****************** */
static void writestruct(WriteData *wd, int filecode, const char *structname, int nr, void *adr)
{
BHead bh;
short *sp;
if(adr==NULL || nr==0) return;
/* init BHead */
bh.code= filecode;
bh.old= adr;
bh.nr= nr;
bh.SDNAnr= DNA_struct_find_nr(wd->sdna, structname);
if(bh.SDNAnr== -1) {
printf("error: can't find SDNA code <%s>\n", structname);
return;
}
sp= wd->sdna->structs[bh.SDNAnr];
bh.len= nr*wd->sdna->typelens[sp[0]];
if(bh.len==0) return;
mywrite(wd, &bh, sizeof(BHead));
mywrite(wd, adr, bh.len);
}
static void writedata(WriteData *wd, int filecode, int len, void *adr) /* do not use for structs */
{
BHead bh;
if(adr==NULL) return;
if(len==0) return;
len+= 3;
len-= ( len % 4);
/* init BHead */
bh.code= filecode;
bh.old= adr;
bh.nr= 1;
bh.SDNAnr= 0;
bh.len= len;
mywrite(wd, &bh, sizeof(BHead));
if(len) mywrite(wd, adr, len);
}
/* *************** writing some direct data structs used in more code parts **************** */
/*These functions are used by blender's .blend system for file saving/loading.*/
void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd);
void IDP_WriteProperty(IDProperty *prop, void *wd);
static void IDP_WriteArray(IDProperty *prop, void *wd)
{
/*REMEMBER to set totalen to len in the linking code!!*/
if (prop->data.pointer) {
writedata(wd, DATA, MEM_allocN_len(prop->data.pointer), prop->data.pointer);
if(prop->subtype == IDP_GROUP) {
IDProperty **array= prop->data.pointer;
int a;
for(a=0; a<prop->len; a++)
IDP_WriteProperty(array[a], wd);
}
}
}
static void IDP_WriteIDPArray(IDProperty *prop, void *wd)
{
/*REMEMBER to set totalen to len in the linking code!!*/
if (prop->data.pointer) {
IDProperty *array = prop->data.pointer;
int a;
writestruct(wd, DATA, "IDProperty", prop->len, array);
for(a=0; a<prop->len; a++)
IDP_WriteProperty_OnlyData(&array[a], wd);
}
}
static void IDP_WriteString(IDProperty *prop, void *wd)
{
/*REMEMBER to set totalen to len in the linking code!!*/
writedata(wd, DATA, prop->len+1, prop->data.pointer);
}
static void IDP_WriteGroup(IDProperty *prop, void *wd)
{
IDProperty *loop;
for (loop=prop->data.group.first; loop; loop=loop->next) {
IDP_WriteProperty(loop, wd);
}
}
/* Functions to read/write ID Properties */
void IDP_WriteProperty_OnlyData(IDProperty *prop, void *wd)
{
switch (prop->type) {
case IDP_GROUP:
IDP_WriteGroup(prop, wd);
break;
case IDP_STRING:
IDP_WriteString(prop, wd);
break;
case IDP_ARRAY:
IDP_WriteArray(prop, wd);
break;
case IDP_IDPARRAY:
IDP_WriteIDPArray(prop, wd);
break;
}
}
void IDP_WriteProperty(IDProperty *prop, void *wd)
{
writestruct(wd, DATA, "IDProperty", 1, prop);
IDP_WriteProperty_OnlyData(prop, wd);
}
static void write_fmodifiers(WriteData *wd, ListBase *fmodifiers)
{
FModifier *fcm;
/* Modifiers */
for (fcm= fmodifiers->first; fcm; fcm= fcm->next) {
FModifierTypeInfo *fmi= fmodifier_get_typeinfo(fcm);
/* Write the specific data */
if (fmi && fcm->data) {
/* firstly, just write the plain fmi->data struct */
writestruct(wd, DATA, fmi->structName, 1, fcm->data);
/* do any modifier specific stuff */
switch (fcm->type) {
case FMODIFIER_TYPE_GENERATOR:
{
FMod_Generator *data= (FMod_Generator *)fcm->data;
/* write coefficients array */
if (data->coefficients)
writedata(wd, DATA, sizeof(float)*(data->arraysize), data->coefficients);
}
break;
case FMODIFIER_TYPE_ENVELOPE:
{
FMod_Envelope *data= (FMod_Envelope *)fcm->data;
/* write envelope data */
if (data->data)
writestruct(wd, DATA, "FCM_EnvelopeData", data->totvert, data->data);
}
break;
case FMODIFIER_TYPE_PYTHON:
{
FMod_Python *data = (FMod_Python *)fcm->data;
/* Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
IDP_WriteProperty(data->prop, wd);
}
break;
}
}
/* Write the modifier */
writestruct(wd, DATA, "FModifier", 1, fcm);
}
}
static void write_fcurves(WriteData *wd, ListBase *fcurves)
{
FCurve *fcu;
for (fcu=fcurves->first; fcu; fcu=fcu->next) {
/* F-Curve */
writestruct(wd, DATA, "FCurve", 1, fcu);
/* curve data */
if (fcu->bezt)
writestruct(wd, DATA, "BezTriple", fcu->totvert, fcu->bezt);
if (fcu->fpt)
writestruct(wd, DATA, "FPoint", fcu->totvert, fcu->fpt);
if (fcu->rna_path)
writedata(wd, DATA, strlen(fcu->rna_path)+1, fcu->rna_path);
/* driver data */
if (fcu->driver) {
ChannelDriver *driver= fcu->driver;
DriverVar *dvar;
writestruct(wd, DATA, "ChannelDriver", 1, driver);
/* variables */
for (dvar= driver->variables.first; dvar; dvar= dvar->next) {
writestruct(wd, DATA, "DriverVar", 1, dvar);
DRIVER_TARGETS_USED_LOOPER(dvar)
{
if (dtar->rna_path)
writedata(wd, DATA, strlen(dtar->rna_path)+1, dtar->rna_path);
}
DRIVER_TARGETS_LOOPER_END
}
}
/* write F-Modifiers */
write_fmodifiers(wd, &fcu->modifiers);
}
}
static void write_actions(WriteData *wd, ListBase *idbase)
{
bAction *act;
bActionGroup *grp;
TimeMarker *marker;
for(act=idbase->first; act; act= act->id.next) {
if (act->id.us>0 || wd->current) {
writestruct(wd, ID_AC, "bAction", 1, act);
if (act->id.properties) IDP_WriteProperty(act->id.properties, wd);
write_fcurves(wd, &act->curves);
for (grp=act->groups.first; grp; grp=grp->next) {
writestruct(wd, DATA, "bActionGroup", 1, grp);
}
for (marker=act->markers.first; marker; marker=marker->next) {
writestruct(wd, DATA, "TimeMarker", 1, marker);
}
}
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_keyingsets(WriteData *wd, ListBase *list)
{
KeyingSet *ks;
KS_Path *ksp;
for (ks= list->first; ks; ks= ks->next) {
/* KeyingSet */
writestruct(wd, DATA, "KeyingSet", 1, ks);
/* Paths */
for (ksp= ks->paths.first; ksp; ksp= ksp->next) {
/* Path */
writestruct(wd, DATA, "KS_Path", 1, ksp);
if (ksp->rna_path)
writedata(wd, DATA, strlen(ksp->rna_path)+1, ksp->rna_path);
}
}
}
static void write_nlastrips(WriteData *wd, ListBase *strips)
{
NlaStrip *strip;
for (strip= strips->first; strip; strip= strip->next) {
/* write the strip first */
writestruct(wd, DATA, "NlaStrip", 1, strip);
/* write the strip's F-Curves and modifiers */
write_fcurves(wd, &strip->fcurves);
write_fmodifiers(wd, &strip->modifiers);
/* write the strip's children */
write_nlastrips(wd, &strip->strips);
}
}
static void write_nladata(WriteData *wd, ListBase *nlabase)
{
NlaTrack *nlt;
/* write all the tracks */
for (nlt= nlabase->first; nlt; nlt= nlt->next) {
/* write the track first */
writestruct(wd, DATA, "NlaTrack", 1, nlt);
/* write the track's strips */
write_nlastrips(wd, &nlt->strips);
}
}
static void write_animdata(WriteData *wd, AnimData *adt)
{
AnimOverride *aor;
/* firstly, just write the AnimData block */
writestruct(wd, DATA, "AnimData", 1, adt);
/* write drivers */
write_fcurves(wd, &adt->drivers);
/* write overrides */
// FIXME: are these needed?
for (aor= adt->overrides.first; aor; aor= aor->next) {
/* overrides consist of base data + rna_path */
writestruct(wd, DATA, "AnimOverride", 1, aor);
writedata(wd, DATA, strlen(aor->rna_path)+1, aor->rna_path);
}
// TODO write the remaps (if they are needed)
/* write NLA data */
write_nladata(wd, &adt->nla_tracks);
}
static void write_curvemapping(WriteData *wd, CurveMapping *cumap)
{
int a;
writestruct(wd, DATA, "CurveMapping", 1, cumap);
for(a=0; a<CM_TOT; a++)
writestruct(wd, DATA, "CurveMapPoint", cumap->cm[a].totpoint, cumap->cm[a].curve);
}
/* this is only direct data, tree itself should have been written */
static void write_nodetree(WriteData *wd, bNodeTree *ntree)
{
bNode *node;
bNodeSocket *sock;
bNodeLink *link;
/* for link_list() speed, we write per list */
if(ntree->adt) write_animdata(wd, ntree->adt);
for(node= ntree->nodes.first; node; node= node->next)
writestruct(wd, DATA, "bNode", 1, node);
for(node= ntree->nodes.first; node; node= node->next) {
if(node->storage && node->type!=NODE_DYNAMIC) {
/* could be handlerized at some point, now only 1 exception still */
if(ntree->type==NTREE_SHADER && (node->type==SH_NODE_CURVE_VEC || node->type==SH_NODE_CURVE_RGB))
write_curvemapping(wd, node->storage);
else if(ntree->type==NTREE_COMPOSIT && ELEM4(node->type, CMP_NODE_TIME, CMP_NODE_CURVE_VEC, CMP_NODE_CURVE_RGB, CMP_NODE_HUECORRECT))
write_curvemapping(wd, node->storage);
else if(ntree->type==NTREE_TEXTURE && (node->type==TEX_NODE_CURVE_RGB || node->type==TEX_NODE_CURVE_TIME) )
write_curvemapping(wd, node->storage);
else
writestruct(wd, DATA, node->typeinfo->storagename, 1, node->storage);
}
for(sock= node->inputs.first; sock; sock= sock->next)
writestruct(wd, DATA, "bNodeSocket", 1, sock);
for(sock= node->outputs.first; sock; sock= sock->next)
writestruct(wd, DATA, "bNodeSocket", 1, sock);
}
for(link= ntree->links.first; link; link= link->next)
writestruct(wd, DATA, "bNodeLink", 1, link);
/* external sockets */
for(sock= ntree->inputs.first; sock; sock= sock->next)
writestruct(wd, DATA, "bNodeSocket", 1, sock);
for(sock= ntree->outputs.first; sock; sock= sock->next)
writestruct(wd, DATA, "bNodeSocket", 1, sock);
}
static void current_screen_compat(Main *mainvar, bScreen **screen)
{
wmWindowManager *wm;
wmWindow *window;
/* find a global current screen in the first open window, to have
* a reasonable default for reading in older versions */
wm= mainvar->wm.first;
window= (wm)? wm->windows.first: NULL;
*screen= (window)? window->screen: NULL;
}
static void write_renderinfo(WriteData *wd, Main *mainvar) /* for renderdeamon */
{
bScreen *curscreen;
Scene *sce;
int data[8];
/* XXX in future, handle multiple windows with multiple screnes? */
current_screen_compat(mainvar, &curscreen);
for(sce= mainvar->scene.first; sce; sce= sce->id.next) {
if(sce->id.lib==NULL && ( sce==curscreen->scene || (sce->r.scemode & R_BG_RENDER)) ) {
data[0]= sce->r.sfra;
data[1]= sce->r.efra;
memset(data+2, 0, sizeof(int)*6);
BLI_strncpy((char *)(data+2), sce->id.name+2, sizeof(sce->id.name)-2);
writedata(wd, REND, 32, data);
}
}
}
static void write_userdef(WriteData *wd)
{
bTheme *btheme;
wmKeyMap *keymap;
wmKeyMapItem *kmi;
bAddon *bext;
writestruct(wd, USER, "UserDef", 1, &U);
for(btheme= U.themes.first; btheme; btheme=btheme->next)
writestruct(wd, DATA, "bTheme", 1, btheme);
for(keymap= U.keymaps.first; keymap; keymap=keymap->next) {
writestruct(wd, DATA, "wmKeyMap", 1, keymap);
for(kmi=keymap->items.first; kmi; kmi=kmi->next) {
writestruct(wd, DATA, "wmKeyMapItem", 1, kmi);
if(kmi->properties)
IDP_WriteProperty(kmi->properties, wd);
}
}
for(bext= U.addons.first; bext; bext=bext->next)
writestruct(wd, DATA, "bAddon", 1, bext);
}
static void write_boid_state(WriteData *wd, BoidState *state)
{
BoidRule *rule = state->rules.first;
//BoidCondition *cond = state->conditions.first;
writestruct(wd, DATA, "BoidState", 1, state);
for(; rule; rule=rule->next) {
switch(rule->type) {
case eBoidRuleType_Goal:
case eBoidRuleType_Avoid:
writestruct(wd, DATA, "BoidRuleGoalAvoid", 1, rule);
break;
case eBoidRuleType_AvoidCollision:
writestruct(wd, DATA, "BoidRuleAvoidCollision", 1, rule);
break;
case eBoidRuleType_FollowLeader:
writestruct(wd, DATA, "BoidRuleFollowLeader", 1, rule);
break;
case eBoidRuleType_AverageSpeed:
writestruct(wd, DATA, "BoidRuleAverageSpeed", 1, rule);
break;
case eBoidRuleType_Fight:
writestruct(wd, DATA, "BoidRuleFight", 1, rule);
break;
default:
writestruct(wd, DATA, "BoidRule", 1, rule);
break;
}
}
//for(; cond; cond=cond->next)
// writestruct(wd, DATA, "BoidCondition", 1, cond);
}
/* update this also to readfile.c */
static const char *ptcache_data_struct[] = {
"", // BPHYS_DATA_INDEX
"", // BPHYS_DATA_LOCATION
"", // BPHYS_DATA_VELOCITY
"", // BPHYS_DATA_ROTATION
"", // BPHYS_DATA_AVELOCITY / BPHYS_DATA_XCONST */
"", // BPHYS_DATA_SIZE:
"", // BPHYS_DATA_TIMES:
"BoidData" // case BPHYS_DATA_BOIDS:
};
static const char *ptcache_extra_struct[] = {
"",
"ParticleSpring"
};
static void write_pointcaches(WriteData *wd, ListBase *ptcaches)
{
PointCache *cache = ptcaches->first;
int i;
for(; cache; cache=cache->next) {
writestruct(wd, DATA, "PointCache", 1, cache);
if((cache->flag & PTCACHE_DISK_CACHE)==0) {
PTCacheMem *pm = cache->mem_cache.first;
for(; pm; pm=pm->next) {
PTCacheExtra *extra = pm->extradata.first;
writestruct(wd, DATA, "PTCacheMem", 1, pm);
for(i=0; i<BPHYS_TOT_DATA; i++) {
if(pm->data[i] && pm->data_types & (1<<i)) {
if(strcmp(ptcache_data_struct[i], "")==0)
writedata(wd, DATA, MEM_allocN_len(pm->data[i]), pm->data[i]);
else
writestruct(wd, DATA, ptcache_data_struct[i], pm->totpoint, pm->data[i]);
}
}
for(; extra; extra=extra->next) {
if(strcmp(ptcache_extra_struct[extra->type], "")==0)
continue;
writestruct(wd, DATA, "PTCacheExtra", 1, extra);
writestruct(wd, DATA, ptcache_extra_struct[extra->type], extra->totdata, extra->data);
}
}
}
}
}
static void write_particlesettings(WriteData *wd, ListBase *idbase)
{
ParticleSettings *part;
ParticleDupliWeight *dw;
int a;
part= idbase->first;
while(part) {
if(part->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_PA, "ParticleSettings", 1, part);
if (part->id.properties) IDP_WriteProperty(part->id.properties, wd);
if (part->adt) write_animdata(wd, part->adt);
writestruct(wd, DATA, "PartDeflect", 1, part->pd);
writestruct(wd, DATA, "PartDeflect", 1, part->pd2);
writestruct(wd, DATA, "EffectorWeights", 1, part->effector_weights);
dw = part->dupliweights.first;
for(; dw; dw=dw->next)
writestruct(wd, DATA, "ParticleDupliWeight", 1, dw);
if(part->boids && part->phystype == PART_PHYS_BOIDS) {
BoidState *state = part->boids->states.first;
writestruct(wd, DATA, "BoidSettings", 1, part->boids);
for(; state; state=state->next)
write_boid_state(wd, state);
}
if(part->fluid && part->phystype == PART_PHYS_FLUID){
writestruct(wd, DATA, "SPHFluidSettings", 1, part->fluid);
}
for(a=0; a<MAX_MTEX; a++) {
if(part->mtex[a]) writestruct(wd, DATA, "MTex", 1, part->mtex[a]);
}
}
part= part->id.next;
}
}
static void write_particlesystems(WriteData *wd, ListBase *particles)
{
ParticleSystem *psys= particles->first;
ParticleTarget *pt;
int a;
for(; psys; psys=psys->next) {
writestruct(wd, DATA, "ParticleSystem", 1, psys);
if(psys->particles) {
writestruct(wd, DATA, "ParticleData", psys->totpart ,psys->particles);
if(psys->particles->hair) {
ParticleData *pa = psys->particles;
for(a=0; a<psys->totpart; a++, pa++)
writestruct(wd, DATA, "HairKey", pa->totkey, pa->hair);
}
if(psys->particles->boid && psys->part->phystype == PART_PHYS_BOIDS)
writestruct(wd, DATA, "BoidParticle", psys->totpart, psys->particles->boid);
if(psys->part->fluid && psys->part->phystype == PART_PHYS_FLUID && (psys->part->fluid->flag & SPH_VISCOELASTIC_SPRINGS))
writestruct(wd, DATA, "ParticleSpring", psys->tot_fluidsprings, psys->fluid_springs);
}
pt = psys->targets.first;
for(; pt; pt=pt->next)
writestruct(wd, DATA, "ParticleTarget", 1, pt);
if(psys->child) writestruct(wd, DATA, "ChildParticle", psys->totchild ,psys->child);
if(psys->clmd) {
writestruct(wd, DATA, "ClothModifierData", 1, psys->clmd);
writestruct(wd, DATA, "ClothSimSettings", 1, psys->clmd->sim_parms);
writestruct(wd, DATA, "ClothCollSettings", 1, psys->clmd->coll_parms);
}
write_pointcaches(wd, &psys->ptcaches);
}
}
static void write_properties(WriteData *wd, ListBase *lb)
{
bProperty *prop;
prop= lb->first;
while(prop) {
writestruct(wd, DATA, "bProperty", 1, prop);
if(prop->poin && prop->poin != &prop->data)
writedata(wd, DATA, MEM_allocN_len(prop->poin), prop->poin);
prop= prop->next;
}
}
static void write_sensors(WriteData *wd, ListBase *lb)
{
bSensor *sens;
sens= lb->first;
while(sens) {
writestruct(wd, DATA, "bSensor", 1, sens);
writedata(wd, DATA, sizeof(void *)*sens->totlinks, sens->links);
switch(sens->type) {
case SENS_NEAR:
writestruct(wd, DATA, "bNearSensor", 1, sens->data);
break;
case SENS_MOUSE:
writestruct(wd, DATA, "bMouseSensor", 1, sens->data);
break;
case SENS_TOUCH:
writestruct(wd, DATA, "bTouchSensor", 1, sens->data);
break;
case SENS_KEYBOARD:
writestruct(wd, DATA, "bKeyboardSensor", 1, sens->data);
break;
case SENS_PROPERTY:
writestruct(wd, DATA, "bPropertySensor", 1, sens->data);
break;
case SENS_ARMATURE:
writestruct(wd, DATA, "bArmatureSensor", 1, sens->data);
break;
case SENS_ACTUATOR:
writestruct(wd, DATA, "bActuatorSensor", 1, sens->data);
break;
case SENS_DELAY:
writestruct(wd, DATA, "bDelaySensor", 1, sens->data);
break;
case SENS_COLLISION:
writestruct(wd, DATA, "bCollisionSensor", 1, sens->data);
break;
case SENS_RADAR:
writestruct(wd, DATA, "bRadarSensor", 1, sens->data);
break;
case SENS_RANDOM:
writestruct(wd, DATA, "bRandomSensor", 1, sens->data);
break;
case SENS_RAY:
writestruct(wd, DATA, "bRaySensor", 1, sens->data);
break;
case SENS_MESSAGE:
writestruct(wd, DATA, "bMessageSensor", 1, sens->data);
break;
case SENS_JOYSTICK:
writestruct(wd, DATA, "bJoystickSensor", 1, sens->data);
break;
default:
; /* error: don't know how to write this file */
}
sens= sens->next;
}
}
static void write_controllers(WriteData *wd, ListBase *lb)
{
bController *cont;
cont= lb->first;
while(cont) {
writestruct(wd, DATA, "bController", 1, cont);
writedata(wd, DATA, sizeof(void *)*cont->totlinks, cont->links);
switch(cont->type) {
case CONT_EXPRESSION:
writestruct(wd, DATA, "bExpressionCont", 1, cont->data);
break;
case CONT_PYTHON:
writestruct(wd, DATA, "bPythonCont", 1, cont->data);
break;
default:
; /* error: don't know how to write this file */
}
cont= cont->next;
}
}
static void write_actuators(WriteData *wd, ListBase *lb)
{
bActuator *act;
act= lb->first;
while(act) {
writestruct(wd, DATA, "bActuator", 1, act);
switch(act->type) {
case ACT_ACTION:
case ACT_SHAPEACTION:
writestruct(wd, DATA, "bActionActuator", 1, act->data);
break;
case ACT_SOUND:
writestruct(wd, DATA, "bSoundActuator", 1, act->data);
break;
case ACT_OBJECT:
writestruct(wd, DATA, "bObjectActuator", 1, act->data);
break;
case ACT_IPO:
writestruct(wd, DATA, "bIpoActuator", 1, act->data);
break;
case ACT_PROPERTY:
writestruct(wd, DATA, "bPropertyActuator", 1, act->data);
break;
case ACT_CAMERA:
writestruct(wd, DATA, "bCameraActuator", 1, act->data);
break;
case ACT_CONSTRAINT:
writestruct(wd, DATA, "bConstraintActuator", 1, act->data);
break;
case ACT_EDIT_OBJECT:
writestruct(wd, DATA, "bEditObjectActuator", 1, act->data);
break;
case ACT_SCENE:
writestruct(wd, DATA, "bSceneActuator", 1, act->data);
break;
case ACT_GROUP:
writestruct(wd, DATA, "bGroupActuator", 1, act->data);
break;
case ACT_RANDOM:
writestruct(wd, DATA, "bRandomActuator", 1, act->data);
break;
case ACT_MESSAGE:
writestruct(wd, DATA, "bMessageActuator", 1, act->data);
break;
case ACT_GAME:
writestruct(wd, DATA, "bGameActuator", 1, act->data);
break;
case ACT_VISIBILITY:
writestruct(wd, DATA, "bVisibilityActuator", 1, act->data);
break;
case ACT_2DFILTER:
writestruct(wd, DATA, "bTwoDFilterActuator", 1, act->data);
break;
case ACT_PARENT:
writestruct(wd, DATA, "bParentActuator", 1, act->data);
break;
case ACT_STATE:
writestruct(wd, DATA, "bStateActuator", 1, act->data);
break;
case ACT_ARMATURE:
writestruct(wd, DATA, "bArmatureActuator", 1, act->data);
break;
default:
; /* error: don't know how to write this file */
}
act= act->next;
}
}
static void write_motionpath(WriteData *wd, bMotionPath *mpath)
{
/* sanity checks */
if (mpath == NULL)
return;
/* firstly, just write the motionpath struct */
writestruct(wd, DATA, "bMotionPath", 1, mpath);
/* now write the array of data */
writestruct(wd, DATA, "bMotionPathVert", mpath->length, mpath->points);
}
static void write_constraints(WriteData *wd, ListBase *conlist)
{
bConstraint *con;
for (con=conlist->first; con; con=con->next) {
bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
/* Write the specific data */
if (cti && con->data) {
/* firstly, just write the plain con->data struct */
writestruct(wd, DATA, cti->structName, 1, con->data);
/* do any constraint specific stuff */
switch (con->type) {
case CONSTRAINT_TYPE_PYTHON:
{
bPythonConstraint *data = (bPythonConstraint *)con->data;
bConstraintTarget *ct;
/* write targets */
for (ct= data->targets.first; ct; ct= ct->next)
writestruct(wd, DATA, "bConstraintTarget", 1, ct);
/* Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
IDP_WriteProperty(data->prop, wd);
}
break;
case CONSTRAINT_TYPE_SPLINEIK:
{
bSplineIKConstraint *data= (bSplineIKConstraint*)con->data;
/* write points array */
writedata(wd, DATA, sizeof(float)*(data->numpoints), data->points);
}
break;
}
}
/* Write the constraint */
writestruct(wd, DATA, "bConstraint", 1, con);
}
}
static void write_pose(WriteData *wd, bPose *pose)
{
bPoseChannel *chan;
bActionGroup *grp;
/* Write each channel */
if (!pose)
return;
/* Write channels */
for (chan=pose->chanbase.first; chan; chan=chan->next) {
/* Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
if (chan->prop)
IDP_WriteProperty(chan->prop, wd);
write_constraints(wd, &chan->constraints);
write_motionpath(wd, chan->mpath);
/* prevent crashes with autosave, when a bone duplicated in editmode has not yet been assigned to its posechannel */
if (chan->bone)
chan->selectflag= chan->bone->flag & BONE_SELECTED; /* gets restored on read, for library armatures */
writestruct(wd, DATA, "bPoseChannel", 1, chan);
}
/* Write groups */
for (grp=pose->agroups.first; grp; grp=grp->next)
writestruct(wd, DATA, "bActionGroup", 1, grp);
/* write IK param */
if (pose->ikparam) {
char *structname = (char *)get_ikparam_name(pose);
if (structname)
writestruct(wd, DATA, structname, 1, pose->ikparam);
}
/* Write this pose */
writestruct(wd, DATA, "bPose", 1, pose);
}
static void write_defgroups(WriteData *wd, ListBase *defbase)
{
bDeformGroup *defgroup;
for (defgroup=defbase->first; defgroup; defgroup=defgroup->next)
writestruct(wd, DATA, "bDeformGroup", 1, defgroup);
}
static void write_modifiers(WriteData *wd, ListBase *modbase)
{
ModifierData *md;
if (modbase == NULL) return;
for (md=modbase->first; md; md= md->next) {
ModifierTypeInfo *mti = modifierType_getInfo(md->type);
if (mti == NULL) return;
writestruct(wd, DATA, mti->structName, 1, md);
if (md->type==eModifierType_Hook) {
HookModifierData *hmd = (HookModifierData*) md;
writedata(wd, DATA, sizeof(int)*hmd->totindex, hmd->indexar);
}
else if(md->type==eModifierType_Cloth) {
ClothModifierData *clmd = (ClothModifierData*) md;
writestruct(wd, DATA, "ClothSimSettings", 1, clmd->sim_parms);
writestruct(wd, DATA, "ClothCollSettings", 1, clmd->coll_parms);
writestruct(wd, DATA, "EffectorWeights", 1, clmd->sim_parms->effector_weights);
write_pointcaches(wd, &clmd->ptcaches);
}
else if(md->type==eModifierType_Smoke) {
SmokeModifierData *smd = (SmokeModifierData*) md;
if(smd->type & MOD_SMOKE_TYPE_DOMAIN)
{
if(smd->domain)
{
write_pointcaches(wd, &(smd->domain->ptcaches[0]));
/* create fake pointcache so that old blender versions can read it */
smd->domain->point_cache[1] = BKE_ptcache_add(&smd->domain->ptcaches[1]);
smd->domain->point_cache[1]->flag |= PTCACHE_DISK_CACHE|PTCACHE_FAKE_SMOKE;
smd->domain->point_cache[1]->step = 1;
write_pointcaches(wd, &(smd->domain->ptcaches[1]));
}
writestruct(wd, DATA, "SmokeDomainSettings", 1, smd->domain);
if(smd->domain) {
/* cleanup the fake pointcache */
BKE_ptcache_free_list(&smd->domain->ptcaches[1]);
smd->domain->point_cache[1] = NULL;
writestruct(wd, DATA, "EffectorWeights", 1, smd->domain->effector_weights);
}
}
else if(smd->type & MOD_SMOKE_TYPE_FLOW)
writestruct(wd, DATA, "SmokeFlowSettings", 1, smd->flow);
else if(smd->type & MOD_SMOKE_TYPE_COLL)
writestruct(wd, DATA, "SmokeCollSettings", 1, smd->coll);
}
else if(md->type==eModifierType_Fluidsim) {
FluidsimModifierData *fluidmd = (FluidsimModifierData*) md;
writestruct(wd, DATA, "FluidsimSettings", 1, fluidmd->fss);
}
else if(md->type==eModifierType_DynamicPaint) {
DynamicPaintModifierData *pmd = (DynamicPaintModifierData*) md;
if(pmd->type & MOD_DYNAMICPAINT_TYPE_CANVAS)
{
DynamicPaintSurface *surface;
writestruct(wd, DATA, "DynamicPaintCanvasSettings", 1, pmd->canvas);
/* write surfaces */
for (surface=pmd->canvas->surfaces.first; surface; surface=surface->next)
writestruct(wd, DATA, "DynamicPaintSurface", 1, surface);
/* write caches */
for (surface=pmd->canvas->surfaces.first; surface; surface=surface->next)
write_pointcaches(wd, &(surface->ptcaches));
}
else if(pmd->type & MOD_DYNAMICPAINT_TYPE_BRUSH && pmd->brush)
{
writestruct(wd, DATA, "DynamicPaintBrushSettings", 1, pmd->brush);
writestruct(wd, DATA, "Material", 1, pmd->brush->mat);
writestruct(wd, DATA, "ColorBand", 1, pmd->brush->paint_ramp);
}
}
else if (md->type==eModifierType_Collision) {
/*
CollisionModifierData *collmd = (CollisionModifierData*) md;
// TODO: CollisionModifier should use pointcache
// + have proper reset events before enabling this
writestruct(wd, DATA, "MVert", collmd->numverts, collmd->x);
writestruct(wd, DATA, "MVert", collmd->numverts, collmd->xnew);
writestruct(wd, DATA, "MFace", collmd->numfaces, collmd->mfaces);
*/
}
else if (md->type==eModifierType_MeshDeform) {
MeshDeformModifierData *mmd = (MeshDeformModifierData*) md;
int size = mmd->dyngridsize;
writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->bindinfluences);
writedata(wd, DATA, sizeof(int)*(mmd->totvert+1), mmd->bindoffsets);
writedata(wd, DATA, sizeof(float)*3*mmd->totcagevert,
mmd->bindcagecos);
writestruct(wd, DATA, "MDefCell", size*size*size, mmd->dyngrid);
writestruct(wd, DATA, "MDefInfluence", mmd->totinfluence, mmd->dyninfluences);
writedata(wd, DATA, sizeof(int)*mmd->totvert, mmd->dynverts);
}
else if (md->type==eModifierType_Warp) {
WarpModifierData *tmd = (WarpModifierData*) md;
if(tmd->curfalloff) {
write_curvemapping(wd, tmd->curfalloff);
}
}
}
}
static void write_objects(WriteData *wd, ListBase *idbase)
{
Object *ob;
ob= idbase->first;
while(ob) {
if(ob->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_OB, "Object", 1, ob);
/*Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
if (ob->id.properties) IDP_WriteProperty(ob->id.properties, wd);
if (ob->adt) write_animdata(wd, ob->adt);
/* direct data */
writedata(wd, DATA, sizeof(void *)*ob->totcol, ob->mat);
writedata(wd, DATA, sizeof(char)*ob->totcol, ob->matbits);
/* write_effects(wd, &ob->effect); */ /* not used anymore */
write_properties(wd, &ob->prop);
write_sensors(wd, &ob->sensors);
write_controllers(wd, &ob->controllers);
write_actuators(wd, &ob->actuators);
if (ob->type == OB_ARMATURE) {
bArmature *arm = ob->data;
if (arm && ob->pose && arm->act_bone) {
BLI_strncpy(ob->pose->proxy_act_bone, arm->act_bone->name, sizeof(ob->pose->proxy_act_bone));
}
}
write_pose(wd, ob->pose);
write_defgroups(wd, &ob->defbase);
write_constraints(wd, &ob->constraints);
write_motionpath(wd, ob->mpath);
writestruct(wd, DATA, "PartDeflect", 1, ob->pd);
writestruct(wd, DATA, "SoftBody", 1, ob->soft);
if(ob->soft) {
write_pointcaches(wd, &ob->soft->ptcaches);
writestruct(wd, DATA, "EffectorWeights", 1, ob->soft->effector_weights);
}
writestruct(wd, DATA, "BulletSoftBody", 1, ob->bsoft);
write_particlesystems(wd, &ob->particlesystem);
write_modifiers(wd, &ob->modifiers);
}
ob= ob->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_vfonts(WriteData *wd, ListBase *idbase)
{
VFont *vf;
PackedFile * pf;
vf= idbase->first;
while(vf) {
if(vf->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_VF, "VFont", 1, vf);
if (vf->id.properties) IDP_WriteProperty(vf->id.properties, wd);
/* direct data */
if (vf->packedfile) {
pf = vf->packedfile;
writestruct(wd, DATA, "PackedFile", 1, pf);
writedata(wd, DATA, pf->size, pf->data);
}
}
vf= vf->id.next;
}
}
static void write_keys(WriteData *wd, ListBase *idbase)
{
Key *key;
KeyBlock *kb;
key= idbase->first;
while(key) {
if(key->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_KE, "Key", 1, key);
if (key->id.properties) IDP_WriteProperty(key->id.properties, wd);
if (key->adt) write_animdata(wd, key->adt);
/* direct data */
kb= key->block.first;
while(kb) {
writestruct(wd, DATA, "KeyBlock", 1, kb);
if(kb->data) writedata(wd, DATA, kb->totelem*key->elemsize, kb->data);
kb= kb->next;
}
}
key= key->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_cameras(WriteData *wd, ListBase *idbase)
{
Camera *cam;
cam= idbase->first;
while(cam) {
if(cam->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_CA, "Camera", 1, cam);
if (cam->id.properties) IDP_WriteProperty(cam->id.properties, wd);
if (cam->adt) write_animdata(wd, cam->adt);
}
cam= cam->id.next;
}
}
static void write_mballs(WriteData *wd, ListBase *idbase)
{
MetaBall *mb;
MetaElem *ml;
mb= idbase->first;
while(mb) {
if(mb->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_MB, "MetaBall", 1, mb);
if (mb->id.properties) IDP_WriteProperty(mb->id.properties, wd);
/* direct data */
writedata(wd, DATA, sizeof(void *)*mb->totcol, mb->mat);
if (mb->adt) write_animdata(wd, mb->adt);
ml= mb->elems.first;
while(ml) {
writestruct(wd, DATA, "MetaElem", 1, ml);
ml= ml->next;
}
}
mb= mb->id.next;
}
}
static int amount_of_chars(char *str)
{
// Since the data is saved as UTF-8 to the cu->str
// The cu->len is not same as the strlen(cu->str)
return strlen(str);
}
static void write_curves(WriteData *wd, ListBase *idbase)
{
Curve *cu;
Nurb *nu;
cu= idbase->first;
while(cu) {
if(cu->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_CU, "Curve", 1, cu);
/* direct data */
writedata(wd, DATA, sizeof(void *)*cu->totcol, cu->mat);
if (cu->id.properties) IDP_WriteProperty(cu->id.properties, wd);
if (cu->adt) write_animdata(wd, cu->adt);
if(cu->vfont) {
writedata(wd, DATA, amount_of_chars(cu->str)+1, cu->str);
writestruct(wd, DATA, "CharInfo", cu->len+1, cu->strinfo);
writestruct(wd, DATA, "TextBox", cu->totbox, cu->tb);
}
else {
/* is also the order of reading */
nu= cu->nurb.first;
while(nu) {
writestruct(wd, DATA, "Nurb", 1, nu);
nu= nu->next;
}
nu= cu->nurb.first;
while(nu) {
if(nu->type == CU_BEZIER)
writestruct(wd, DATA, "BezTriple", nu->pntsu, nu->bezt);
else {
writestruct(wd, DATA, "BPoint", nu->pntsu*nu->pntsv, nu->bp);
if(nu->knotsu) writedata(wd, DATA, KNOTSU(nu)*sizeof(float), nu->knotsu);
if(nu->knotsv) writedata(wd, DATA, KNOTSV(nu)*sizeof(float), nu->knotsv);
}
nu= nu->next;
}
}
}
cu= cu->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_dverts(WriteData *wd, int count, MDeformVert *dvlist)
{
if (dvlist) {
int i;
/* Write the dvert list */
writestruct(wd, DATA, "MDeformVert", count, dvlist);
/* Write deformation data for each dvert */
for (i=0; i<count; i++) {
if (dvlist[i].dw)
writestruct(wd, DATA, "MDeformWeight", dvlist[i].totweight, dvlist[i].dw);
}
}
}
static void write_mdisps(WriteData *wd, int count, MDisps *mdlist, int external)
{
if(mdlist) {
int i;
writestruct(wd, DATA, "MDisps", count, mdlist);
if(!external) {
for(i = 0; i < count; ++i) {
if(mdlist[i].disps)
writedata(wd, DATA, sizeof(float)*3*mdlist[i].totdisp, mdlist[i].disps);
}
}
}
}
static void write_customdata(WriteData *wd, ID *id, int count, CustomData *data, int partial_type, int partial_count)
{
int i;
/* write external customdata (not for undo) */
if(data->external && !wd->current)
CustomData_external_write(data, id, CD_MASK_MESH, count, 0);
writestruct(wd, DATA, "CustomDataLayer", data->maxlayer, data->layers);
for (i=0; i<data->totlayer; i++) {
CustomDataLayer *layer= &data->layers[i];
const char *structname;
int structnum, datasize;
if (layer->type == CD_MDEFORMVERT) {
/* layer types that allocate own memory need special handling */
write_dverts(wd, count, layer->data);
}
else if (layer->type == CD_MDISPS) {
write_mdisps(wd, count, layer->data, layer->flag & CD_FLAG_EXTERNAL);
}
else {
CustomData_file_write_info(layer->type, &structname, &structnum);
if (structnum) {
/* when using partial visibility, the MEdge and MFace layers
are smaller than the original, so their type and count is
passed to make this work */
if (layer->type != partial_type) datasize= structnum*count;
else datasize= structnum*partial_count;
writestruct(wd, DATA, structname, datasize, layer->data);
}
else
printf("error: this CustomDataLayer must not be written to file\n");
}
}
if(data->external)
writestruct(wd, DATA, "CustomDataExternal", 1, data->external);
}
static void write_meshs(WriteData *wd, ListBase *idbase)
{
Mesh *mesh;
mesh= idbase->first;
while(mesh) {
if(mesh->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_ME, "Mesh", 1, mesh);
/* direct data */
if (mesh->id.properties) IDP_WriteProperty(mesh->id.properties, wd);
if (mesh->adt) write_animdata(wd, mesh->adt);
writedata(wd, DATA, sizeof(void *)*mesh->totcol, mesh->mat);
if(mesh->pv) {
write_customdata(wd, &mesh->id, mesh->pv->totvert, &mesh->vdata, -1, 0);
write_customdata(wd, &mesh->id, mesh->pv->totedge, &mesh->edata,
CD_MEDGE, mesh->totedge);
write_customdata(wd, &mesh->id, mesh->pv->totface, &mesh->fdata,
CD_MFACE, mesh->totface);
}
else {
write_customdata(wd, &mesh->id, mesh->totvert, &mesh->vdata, -1, 0);
write_customdata(wd, &mesh->id, mesh->totedge, &mesh->edata, -1, 0);
write_customdata(wd, &mesh->id, mesh->totface, &mesh->fdata, -1, 0);
}
/* PMV data */
if(mesh->pv) {
writestruct(wd, DATA, "PartialVisibility", 1, mesh->pv);
writedata(wd, DATA, sizeof(unsigned int)*mesh->pv->totvert, mesh->pv->vert_map);
writedata(wd, DATA, sizeof(int)*mesh->pv->totedge, mesh->pv->edge_map);
writestruct(wd, DATA, "MFace", mesh->pv->totface, mesh->pv->old_faces);
writestruct(wd, DATA, "MEdge", mesh->pv->totedge, mesh->pv->old_edges);
}
}
mesh= mesh->id.next;
}
}
static void write_lattices(WriteData *wd, ListBase *idbase)
{
Lattice *lt;
lt= idbase->first;
while(lt) {
if(lt->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_LT, "Lattice", 1, lt);
if (lt->id.properties) IDP_WriteProperty(lt->id.properties, wd);
/* write animdata */
if (lt->adt) write_animdata(wd, lt->adt);
/* direct data */
writestruct(wd, DATA, "BPoint", lt->pntsu*lt->pntsv*lt->pntsw, lt->def);
write_dverts(wd, lt->pntsu*lt->pntsv*lt->pntsw, lt->dvert);
}
lt= lt->id.next;
}
}
static void write_previews(WriteData *wd, PreviewImage *prv)
{
if (prv) {
short w = prv->w[1];
short h = prv->h[1];
unsigned int *rect = prv->rect[1];
/* don't write out large previews if not requested */
if (!(U.flag & USER_SAVE_PREVIEWS) ) {
prv->w[1] = 0;
prv->h[1] = 0;
prv->rect[1] = NULL;
}
writestruct(wd, DATA, "PreviewImage", 1, prv);
if (prv->rect[0]) writedata(wd, DATA, prv->w[0]*prv->h[0]*sizeof(unsigned int), prv->rect[0]);
if (prv->rect[1]) writedata(wd, DATA, prv->w[1]*prv->h[1]*sizeof(unsigned int), prv->rect[1]);
/* restore preview, we still want to keep it in memory even if not saved to file */
if (!(U.flag & USER_SAVE_PREVIEWS) ) {
prv->w[1] = w;
prv->h[1] = h;
prv->rect[1] = rect;
}
}
}
static void write_images(WriteData *wd, ListBase *idbase)
{
Image *ima;
PackedFile * pf;
ima= idbase->first;
while(ima) {
if(ima->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_IM, "Image", 1, ima);
if (ima->id.properties) IDP_WriteProperty(ima->id.properties, wd);
if (ima->packedfile) {
pf = ima->packedfile;
writestruct(wd, DATA, "PackedFile", 1, pf);
writedata(wd, DATA, pf->size, pf->data);
}
write_previews(wd, ima->preview);
}
ima= ima->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_textures(WriteData *wd, ListBase *idbase)
{
Tex *tex;
tex= idbase->first;
while(tex) {
if(tex->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_TE, "Tex", 1, tex);
if (tex->id.properties) IDP_WriteProperty(tex->id.properties, wd);
if (tex->adt) write_animdata(wd, tex->adt);
/* direct data */
if(tex->type == TEX_PLUGIN && tex->plugin) writestruct(wd, DATA, "PluginTex", 1, tex->plugin);
if(tex->coba) writestruct(wd, DATA, "ColorBand", 1, tex->coba);
if(tex->type == TEX_ENVMAP && tex->env) writestruct(wd, DATA, "EnvMap", 1, tex->env);
if(tex->type == TEX_POINTDENSITY && tex->pd) {
writestruct(wd, DATA, "PointDensity", 1, tex->pd);
if(tex->pd->coba) writestruct(wd, DATA, "ColorBand", 1, tex->pd->coba);
if(tex->pd->falloff_curve) write_curvemapping(wd, tex->pd->falloff_curve);
}
if(tex->type == TEX_VOXELDATA && tex->vd) writestruct(wd, DATA, "VoxelData", 1, tex->vd);
/* nodetree is integral part of texture, no libdata */
if(tex->nodetree) {
writestruct(wd, DATA, "bNodeTree", 1, tex->nodetree);
write_nodetree(wd, tex->nodetree);
}
write_previews(wd, tex->preview);
}
tex= tex->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_materials(WriteData *wd, ListBase *idbase)
{
Material *ma;
int a;
ma= idbase->first;
while(ma) {
if(ma->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_MA, "Material", 1, ma);
/*Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
/*manually set head group property to IDP_GROUP, just in case it hadn't been
set yet :) */
if (ma->id.properties) IDP_WriteProperty(ma->id.properties, wd);
if (ma->adt) write_animdata(wd, ma->adt);
for(a=0; a<MAX_MTEX; a++) {
if(ma->mtex[a]) writestruct(wd, DATA, "MTex", 1, ma->mtex[a]);
}
if(ma->ramp_col) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_col);
if(ma->ramp_spec) writestruct(wd, DATA, "ColorBand", 1, ma->ramp_spec);
/* nodetree is integral part of material, no libdata */
if(ma->nodetree) {
writestruct(wd, DATA, "bNodeTree", 1, ma->nodetree);
write_nodetree(wd, ma->nodetree);
}
write_previews(wd, ma->preview);
}
ma= ma->id.next;
}
}
static void write_worlds(WriteData *wd, ListBase *idbase)
{
World *wrld;
int a;
wrld= idbase->first;
while(wrld) {
if(wrld->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_WO, "World", 1, wrld);
if (wrld->id.properties) IDP_WriteProperty(wrld->id.properties, wd);
if (wrld->adt) write_animdata(wd, wrld->adt);
for(a=0; a<MAX_MTEX; a++) {
if(wrld->mtex[a]) writestruct(wd, DATA, "MTex", 1, wrld->mtex[a]);
}
write_previews(wd, wrld->preview);
}
wrld= wrld->id.next;
}
}
static void write_lamps(WriteData *wd, ListBase *idbase)
{
Lamp *la;
int a;
la= idbase->first;
while(la) {
if(la->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_LA, "Lamp", 1, la);
if (la->id.properties) IDP_WriteProperty(la->id.properties, wd);
if (la->adt) write_animdata(wd, la->adt);
/* direct data */
for(a=0; a<MAX_MTEX; a++) {
if(la->mtex[a]) writestruct(wd, DATA, "MTex", 1, la->mtex[a]);
}
if(la->curfalloff)
write_curvemapping(wd, la->curfalloff);
write_previews(wd, la->preview);
}
la= la->id.next;
}
}
static void write_scenes(WriteData *wd, ListBase *scebase)
{
Scene *sce;
Base *base;
Editing *ed;
Sequence *seq;
MetaStack *ms;
Strip *strip;
TimeMarker *marker;
TransformOrientation *ts;
SceneRenderLayer *srl;
ToolSettings *tos;
sce= scebase->first;
while(sce) {
/* write LibData */
writestruct(wd, ID_SCE, "Scene", 1, sce);
if (sce->id.properties) IDP_WriteProperty(sce->id.properties, wd);
if (sce->adt) write_animdata(wd, sce->adt);
write_keyingsets(wd, &sce->keyingsets);
/* direct data */
base= sce->base.first;
while(base) {
writestruct(wd, DATA, "Base", 1, base);
base= base->next;
}
tos = sce->toolsettings;
writestruct(wd, DATA, "ToolSettings", 1, tos);
if(tos->vpaint) {
writestruct(wd, DATA, "VPaint", 1, tos->vpaint);
}
if(tos->wpaint) {
writestruct(wd, DATA, "VPaint", 1, tos->wpaint);
}
if(tos->sculpt) {
writestruct(wd, DATA, "Sculpt", 1, tos->sculpt);
}
// write_paint(wd, &tos->imapaint.paint);
ed= sce->ed;
if(ed) {
writestruct(wd, DATA, "Editing", 1, ed);
/* reset write flags too */
SEQ_BEGIN(ed, seq) {
if(seq->strip) seq->strip->done= 0;
writestruct(wd, DATA, "Sequence", 1, seq);
}
SEQ_END
SEQ_BEGIN(ed, seq) {
if(seq->strip && seq->strip->done==0) {
/* write strip with 'done' at 0 because readfile */
if(seq->plugin) writestruct(wd, DATA, "PluginSeq", 1, seq->plugin);
if(seq->effectdata) {
switch(seq->type){
case SEQ_COLOR:
writestruct(wd, DATA, "SolidColorVars", 1, seq->effectdata);
break;
case SEQ_SPEED:
writestruct(wd, DATA, "SpeedControlVars", 1, seq->effectdata);
break;
case SEQ_WIPE:
writestruct(wd, DATA, "WipeVars", 1, seq->effectdata);
break;
case SEQ_GLOW:
writestruct(wd, DATA, "GlowVars", 1, seq->effectdata);
break;
case SEQ_TRANSFORM:
writestruct(wd, DATA, "TransformVars", 1, seq->effectdata);
break;
}
}
strip= seq->strip;
writestruct(wd, DATA, "Strip", 1, strip);
if(seq->flag & SEQ_USE_CROP && strip->crop) {
writestruct(wd, DATA, "StripCrop", 1, strip->crop);
}
if(seq->flag & SEQ_USE_TRANSFORM && strip->transform) {
writestruct(wd, DATA, "StripTransform", 1, strip->transform);
}
if(seq->flag & SEQ_USE_PROXY && strip->proxy) {
writestruct(wd, DATA, "StripProxy", 1, strip->proxy);
}
if(seq->flag & SEQ_USE_COLOR_BALANCE && strip->color_balance) {
writestruct(wd, DATA, "StripColorBalance", 1, strip->color_balance);
}
if(seq->type==SEQ_IMAGE)
writestruct(wd, DATA, "StripElem", MEM_allocN_len(strip->stripdata) / sizeof(struct StripElem), strip->stripdata);
else if(seq->type==SEQ_MOVIE || seq->type==SEQ_RAM_SOUND || seq->type == SEQ_HD_SOUND)
writestruct(wd, DATA, "StripElem", 1, strip->stripdata);
strip->done= 1;
}
}
SEQ_END
/* new; meta stack too, even when its nasty restore code */
for(ms= ed->metastack.first; ms; ms= ms->next) {
writestruct(wd, DATA, "MetaStack", 1, ms);
}
}
if (sce->r.avicodecdata) {
writestruct(wd, DATA, "AviCodecData", 1, sce->r.avicodecdata);
if (sce->r.avicodecdata->lpFormat) writedata(wd, DATA, sce->r.avicodecdata->cbFormat, sce->r.avicodecdata->lpFormat);
if (sce->r.avicodecdata->lpParms) writedata(wd, DATA, sce->r.avicodecdata->cbParms, sce->r.avicodecdata->lpParms);
}
if (sce->r.qtcodecdata) {
writestruct(wd, DATA, "QuicktimeCodecData", 1, sce->r.qtcodecdata);
if (sce->r.qtcodecdata->cdParms) writedata(wd, DATA, sce->r.qtcodecdata->cdSize, sce->r.qtcodecdata->cdParms);
}
if (sce->r.ffcodecdata.properties) {
IDP_WriteProperty(sce->r.ffcodecdata.properties, wd);
}
/* writing dynamic list of TimeMarkers to the blend file */
for(marker= sce->markers.first; marker; marker= marker->next)
writestruct(wd, DATA, "TimeMarker", 1, marker);
/* writing dynamic list of TransformOrientations to the blend file */
for(ts = sce->transform_spaces.first; ts; ts = ts->next)
writestruct(wd, DATA, "TransformOrientation", 1, ts);
for(srl= sce->r.layers.first; srl; srl= srl->next)
writestruct(wd, DATA, "SceneRenderLayer", 1, srl);
if(sce->nodetree) {
writestruct(wd, DATA, "bNodeTree", 1, sce->nodetree);
write_nodetree(wd, sce->nodetree);
}
sce= sce->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_gpencils(WriteData *wd, ListBase *lb)
{
bGPdata *gpd;
bGPDlayer *gpl;
bGPDframe *gpf;
bGPDstroke *gps;
for (gpd= lb->first; gpd; gpd= gpd->id.next) {
if (gpd->id.us>0 || wd->current) {
/* write gpd data block to file */
writestruct(wd, ID_GD, "bGPdata", 1, gpd);
/* write grease-pencil layers to file */
for (gpl= gpd->layers.first; gpl; gpl= gpl->next) {
writestruct(wd, DATA, "bGPDlayer", 1, gpl);
/* write this layer's frames to file */
for (gpf= gpl->frames.first; gpf; gpf= gpf->next) {
writestruct(wd, DATA, "bGPDframe", 1, gpf);
/* write strokes */
for (gps= gpf->strokes.first; gps; gps= gps->next) {
writestruct(wd, DATA, "bGPDstroke", 1, gps);
writestruct(wd, DATA, "bGPDspoint", gps->totpoints, gps->points);
}
}
}
}
}
}
static void write_windowmanagers(WriteData *wd, ListBase *lb)
{
wmWindowManager *wm;
wmWindow *win;
for(wm= lb->first; wm; wm= wm->id.next) {
writestruct(wd, ID_WM, "wmWindowManager", 1, wm);
for(win= wm->windows.first; win; win= win->next)
writestruct(wd, DATA, "wmWindow", 1, win);
}
}
static void write_region(WriteData *wd, ARegion *ar, int spacetype)
{
writestruct(wd, DATA, "ARegion", 1, ar);
if(ar->regiondata) {
switch(spacetype) {
case SPACE_VIEW3D:
if(ar->regiontype==RGN_TYPE_WINDOW) {
RegionView3D *rv3d= ar->regiondata;
writestruct(wd, DATA, "RegionView3D", 1, rv3d);
if(rv3d->localvd)
writestruct(wd, DATA, "RegionView3D", 1, rv3d->localvd);
if(rv3d->clipbb)
writestruct(wd, DATA, "BoundBox", 1, rv3d->clipbb);
}
else
printf("regiondata write missing!\n");
break;
default:
printf("regiondata write missing!\n");
}
}
}
static void write_screens(WriteData *wd, ListBase *scrbase)
{
bScreen *sc;
ScrArea *sa;
ScrVert *sv;
ScrEdge *se;
sc= scrbase->first;
while(sc) {
/* write LibData */
/* in 2.50+ files, the file identifier for screens is patched, forward compatibility */
writestruct(wd, ID_SCRN, "Screen", 1, sc);
if (sc->id.properties)
IDP_WriteProperty(sc->id.properties, wd);
/* direct data */
for(sv= sc->vertbase.first; sv; sv= sv->next)
writestruct(wd, DATA, "ScrVert", 1, sv);
for(se= sc->edgebase.first; se; se= se->next)
writestruct(wd, DATA, "ScrEdge", 1, se);
for(sa= sc->areabase.first; sa; sa= sa->next) {
SpaceLink *sl;
Panel *pa;
ARegion *ar;
writestruct(wd, DATA, "ScrArea", 1, sa);
for(ar= sa->regionbase.first; ar; ar= ar->next) {
write_region(wd, ar, sa->spacetype);
for(pa= ar->panels.first; pa; pa= pa->next)
writestruct(wd, DATA, "Panel", 1, pa);
}
sl= sa->spacedata.first;
while(sl) {
for(ar= sl->regionbase.first; ar; ar= ar->next)
write_region(wd, ar, sl->spacetype);
if(sl->spacetype==SPACE_VIEW3D) {
View3D *v3d= (View3D *) sl;
BGpic *bgpic;
writestruct(wd, DATA, "View3D", 1, v3d);
for (bgpic= v3d->bgpicbase.first; bgpic; bgpic= bgpic->next)
writestruct(wd, DATA, "BGpic", 1, bgpic);
if(v3d->localvd) writestruct(wd, DATA, "View3D", 1, v3d->localvd);
}
else if(sl->spacetype==SPACE_IPO) {
SpaceIpo *sipo= (SpaceIpo *)sl;
ListBase tmpGhosts = sipo->ghostCurves;
/* temporarily disable ghost curves when saving */
sipo->ghostCurves.first= sipo->ghostCurves.last= NULL;
writestruct(wd, DATA, "SpaceIpo", 1, sl);
if(sipo->ads) writestruct(wd, DATA, "bDopeSheet", 1, sipo->ads);
/* reenable ghost curves */
sipo->ghostCurves= tmpGhosts;
}
else if(sl->spacetype==SPACE_BUTS) {
writestruct(wd, DATA, "SpaceButs", 1, sl);
}
else if(sl->spacetype==SPACE_FILE) {
writestruct(wd, DATA, "SpaceFile", 1, sl);
}
else if(sl->spacetype==SPACE_SEQ) {
writestruct(wd, DATA, "SpaceSeq", 1, sl);
}
else if(sl->spacetype==SPACE_OUTLINER) {
SpaceOops *so= (SpaceOops *)sl;
writestruct(wd, DATA, "SpaceOops", 1, so);
/* outliner */
if(so->treestore) {
writestruct(wd, DATA, "TreeStore", 1, so->treestore);
if(so->treestore->data)
writestruct(wd, DATA, "TreeStoreElem", so->treestore->usedelem, so->treestore->data);
}
}
else if(sl->spacetype==SPACE_IMAGE) {
SpaceImage *sima= (SpaceImage *)sl;
writestruct(wd, DATA, "SpaceImage", 1, sl);
if(sima->cumap)
write_curvemapping(wd, sima->cumap);
}
else if(sl->spacetype==SPACE_IMASEL) {
// XXX: depreceated... do we still want to keep this?
writestruct(wd, DATA, "SpaceImaSel", 1, sl);
}
else if(sl->spacetype==SPACE_TEXT) {
writestruct(wd, DATA, "SpaceText", 1, sl);
}
else if(sl->spacetype==SPACE_SCRIPT) {
SpaceScript *scr = (SpaceScript*)sl;
scr->but_refs = NULL;
writestruct(wd, DATA, "SpaceScript", 1, sl);
}
else if(sl->spacetype==SPACE_ACTION) {
writestruct(wd, DATA, "SpaceAction", 1, sl);
}
else if(sl->spacetype==SPACE_SOUND) {
writestruct(wd, DATA, "SpaceSound", 1, sl);
}
else if(sl->spacetype==SPACE_NLA){
SpaceNla *snla= (SpaceNla *)sl;
writestruct(wd, DATA, "SpaceNla", 1, snla);
if(snla->ads) writestruct(wd, DATA, "bDopeSheet", 1, snla->ads);
}
else if(sl->spacetype==SPACE_TIME){
writestruct(wd, DATA, "SpaceTime", 1, sl);
}
else if(sl->spacetype==SPACE_NODE){
writestruct(wd, DATA, "SpaceNode", 1, sl);
}
else if(sl->spacetype==SPACE_LOGIC){
writestruct(wd, DATA, "SpaceLogic", 1, sl);
}
else if(sl->spacetype==SPACE_CONSOLE) {
SpaceConsole *con = (SpaceConsole*)sl;
ConsoleLine *cl;
for (cl=con->history.first; cl; cl=cl->next) {
/* 'len_alloc' is invalid on write, set from 'len' on read */
writestruct(wd, DATA, "ConsoleLine", 1, cl);
writedata(wd, DATA, cl->len+1, cl->line);
}
writestruct(wd, DATA, "SpaceConsole", 1, sl);
}
else if(sl->spacetype==SPACE_USERPREF) {
writestruct(wd, DATA, "SpaceUserPref", 1, sl);
}
sl= sl->next;
}
}
sc= sc->id.next;
}
}
static void write_libraries(WriteData *wd, Main *main)
{
ListBase *lbarray[30];
ID *id;
int a, tot, foundone;
for(; main; main= main->next) {
a=tot= set_listbasepointers(main, lbarray);
/* test: is lib being used */
foundone= 0;
while(tot--) {
for(id= lbarray[tot]->first; id; id= id->next) {
if(id->us>0 && (id->flag & LIB_EXTERN)) {
foundone= 1;
break;
}
}
if(foundone) break;
}
if(foundone) {
writestruct(wd, ID_LI, "Library", 1, main->curlib);
while(a--) {
for(id= lbarray[a]->first; id; id= id->next) {
if(id->us>0 && (id->flag & LIB_EXTERN)) {
writestruct(wd, ID_ID, "ID", 1, id);
}
}
}
}
}
}
static void write_bone(WriteData *wd, Bone* bone)
{
Bone* cbone;
// PATCH for upward compatibility after 2.37+ armature recode
bone->size[0]= bone->size[1]= bone->size[2]= 1.0f;
// Write this bone
writestruct(wd, DATA, "Bone", 1, bone);
/* Write ID Properties -- and copy this comment EXACTLY for easy finding
of library blocks that implement this.*/
if (bone->prop)
IDP_WriteProperty(bone->prop, wd);
// Write Children
cbone= bone->childbase.first;
while(cbone) {
write_bone(wd, cbone);
cbone= cbone->next;
}
}
static void write_armatures(WriteData *wd, ListBase *idbase)
{
bArmature *arm;
Bone *bone;
arm=idbase->first;
while (arm) {
if (arm->id.us>0 || wd->current) {
writestruct(wd, ID_AR, "bArmature", 1, arm);
if (arm->id.properties) IDP_WriteProperty(arm->id.properties, wd);
if (arm->adt) write_animdata(wd, arm->adt);
/* Direct data */
bone= arm->bonebase.first;
while(bone) {
write_bone(wd, bone);
bone=bone->next;
}
}
arm=arm->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_texts(WriteData *wd, ListBase *idbase)
{
Text *text;
TextLine *tmp;
TextMarker *mrk;
text= idbase->first;
while(text) {
if ( (text->flags & TXT_ISMEM) && (text->flags & TXT_ISEXT)) text->flags &= ~TXT_ISEXT;
/* write LibData */
writestruct(wd, ID_TXT, "Text", 1, text);
if(text->name) writedata(wd, DATA, strlen(text->name)+1, text->name);
if (text->id.properties) IDP_WriteProperty(text->id.properties, wd);
if(!(text->flags & TXT_ISEXT)) {
/* now write the text data, in two steps for optimization in the readfunction */
tmp= text->lines.first;
while (tmp) {
writestruct(wd, DATA, "TextLine", 1, tmp);
tmp= tmp->next;
}
tmp= text->lines.first;
while (tmp) {
writedata(wd, DATA, tmp->len+1, tmp->line);
tmp= tmp->next;
}
/* write markers */
mrk= text->markers.first;
while (mrk) {
writestruct(wd, DATA, "TextMarker", 1, mrk);
mrk= mrk->next;
}
}
text= text->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_sounds(WriteData *wd, ListBase *idbase)
{
bSound *sound;
PackedFile * pf;
sound= idbase->first;
while(sound) {
if(sound->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_SO, "bSound", 1, sound);
if (sound->id.properties) IDP_WriteProperty(sound->id.properties, wd);
if (sound->packedfile) {
pf = sound->packedfile;
writestruct(wd, DATA, "PackedFile", 1, pf);
writedata(wd, DATA, pf->size, pf->data);
}
}
sound= sound->id.next;
}
/* flush helps the compression for undo-save */
mywrite(wd, MYWRITE_FLUSH, 0);
}
static void write_groups(WriteData *wd, ListBase *idbase)
{
Group *group;
GroupObject *go;
for(group= idbase->first; group; group= group->id.next) {
if(group->id.us>0 || wd->current) {
/* write LibData */
writestruct(wd, ID_GR, "Group", 1, group);
if (group->id.properties) IDP_WriteProperty(group->id.properties, wd);
go= group->gobject.first;
while(go) {
writestruct(wd, DATA, "GroupObject", 1, go);
go= go->next;
}
}
}
}
static void write_nodetrees(WriteData *wd, ListBase *idbase)
{
bNodeTree *ntree;
for(ntree=idbase->first; ntree; ntree= ntree->id.next) {
if (ntree->id.us>0 || wd->current) {
writestruct(wd, ID_NT, "bNodeTree", 1, ntree);
write_nodetree(wd, ntree);
if (ntree->id.properties) IDP_WriteProperty(ntree->id.properties, wd);
if (ntree->adt) write_animdata(wd, ntree->adt);
}
}
}
static void write_brushes(WriteData *wd, ListBase *idbase)
{
Brush *brush;
for(brush=idbase->first; brush; brush= brush->id.next) {
if(brush->id.us>0 || wd->current) {
writestruct(wd, ID_BR, "Brush", 1, brush);
if (brush->id.properties) IDP_WriteProperty(brush->id.properties, wd);
writestruct(wd, DATA, "MTex", 1, &brush->mtex);
if(brush->curve)
write_curvemapping(wd, brush->curve);
}
}
}
static void write_scripts(WriteData *wd, ListBase *idbase)
{
Script *script;
for(script=idbase->first; script; script= script->id.next) {
if(script->id.us>0 || wd->current) {
writestruct(wd, ID_SCRIPT, "Script", 1, script);
if (script->id.properties) IDP_WriteProperty(script->id.properties, wd);
}
}
}
/* context is usually defined by WM, two cases where no WM is available:
* - for forward compatibility, curscreen has to be saved
* - for undofile, curscene needs to be saved */
static void write_global(WriteData *wd, int fileflags, Main *mainvar)
{
FileGlobal fg;
bScreen *screen;
char subvstr[8];
/* prevent mem checkers from complaining */
fg.pads= fg.pad= 0;
memset(fg.filename, 0, sizeof(fg.filename));
current_screen_compat(mainvar, &screen);
/* XXX still remap G */
fg.curscreen= screen;
fg.curscene= screen->scene;
fg.displaymode= G.displaymode;
fg.winpos= G.winpos;
fg.fileflags= (fileflags & ~(G_FILE_NO_UI|G_FILE_RELATIVE_REMAP)); // prevent to save this, is not good convention, and feature with concerns...
fg.globalf= G.f;
BLI_strncpy(fg.filename, mainvar->name, sizeof(fg.filename));
sprintf(subvstr, "%4d", BLENDER_SUBVERSION);
memcpy(fg.subvstr, subvstr, 4);
fg.subversion= BLENDER_SUBVERSION;
fg.minversion= BLENDER_MINVERSION;
fg.minsubversion= BLENDER_MINSUBVERSION;
#ifdef NAN_BUILDINFO
{
extern char build_rev[];
fg.revision= atoi(build_rev);
}
#else
fg.revision= 0;
#endif
writestruct(wd, GLOB, "FileGlobal", 1, &fg);
}
/* preview image, first 2 values are width and height
* second are an RGBA image (unsigned char)
* note, this uses 'TEST' since new types will segfault on file load for older blender versions.
*/
static void write_thumb(WriteData *wd, int *img)
{
if(img)
writedata(wd, TEST, (2 + img[0] * img[1]) * sizeof(int), img);
}
/* if MemFile * there's filesave to memory */
static int write_file_handle(Main *mainvar, int handle, MemFile *compare, MemFile *current,
int write_user_block, int write_flags, int *thumb)
{
BHead bhead;
ListBase mainlist;
char buf[16];
WriteData *wd;
blo_split_main(&mainlist, mainvar);
wd= bgnwrite(handle, compare, current);
sprintf(buf, "BLENDER%c%c%.3d", (sizeof(void*)==8)?'-':'_', (ENDIAN_ORDER==B_ENDIAN)?'V':'v', BLENDER_VERSION);
mywrite(wd, buf, 12);
write_renderinfo(wd, mainvar);
write_thumb(wd, thumb);
write_global(wd, write_flags, mainvar);
/* no UI save in undo */
if(current==NULL) {
write_windowmanagers(wd, &mainvar->wm);
write_screens (wd, &mainvar->screen);
}
write_scenes (wd, &mainvar->scene);
write_curves (wd, &mainvar->curve);
write_mballs (wd, &mainvar->mball);
write_images (wd, &mainvar->image);
write_cameras (wd, &mainvar->camera);
write_lamps (wd, &mainvar->lamp);
write_lattices (wd, &mainvar->latt);
write_vfonts (wd, &mainvar->vfont);
write_keys (wd, &mainvar->key);
write_worlds (wd, &mainvar->world);
write_texts (wd, &mainvar->text);
write_sounds (wd, &mainvar->sound);
write_groups (wd, &mainvar->group);
write_armatures(wd, &mainvar->armature);
write_actions (wd, &mainvar->action);
write_objects (wd, &mainvar->object);
write_materials(wd, &mainvar->mat);
write_textures (wd, &mainvar->tex);
write_meshs (wd, &mainvar->mesh);
write_particlesettings(wd, &mainvar->particle);
write_nodetrees(wd, &mainvar->nodetree);
write_brushes (wd, &mainvar->brush);
write_scripts (wd, &mainvar->script);
write_gpencils (wd, &mainvar->gpencil);
write_libraries(wd, mainvar->next);
if (write_user_block) {
write_userdef(wd);
}
/* dna as last, because (to be implemented) test for which structs are written */
writedata(wd, DNA1, wd->sdna->datalen, wd->sdna->data);
/* end of file */
memset(&bhead, 0, sizeof(BHead));
bhead.code= ENDB;
mywrite(wd, &bhead, sizeof(BHead));
blo_join_main(&mainlist);
return endwrite(wd);
}
/* return: success (1) */
int BLO_write_file(Main *mainvar, const char *filepath, int write_flags, ReportList *reports, int *thumb)
{
char userfilename[FILE_MAXDIR+FILE_MAXFILE];
char tempname[FILE_MAXDIR+FILE_MAXFILE+1];
int file, err, write_user_block;
/* open temporary file, so we preserve the original in case we crash */
BLI_snprintf(tempname, sizeof(tempname), "%s@", filepath);
file = open(tempname,O_BINARY+O_WRONLY+O_CREAT+O_TRUNC, 0666);
if(file == -1) {
BKE_reportf(reports, RPT_ERROR, "Can't open file %s for writing: %s.", tempname, strerror(errno));
return 0;
}
/* remapping of relative paths to new file location */
if(write_flags & G_FILE_RELATIVE_REMAP) {
char dir1[FILE_MAXDIR+FILE_MAXFILE];
char dir2[FILE_MAXDIR+FILE_MAXFILE];
BLI_split_dirfile(filepath, dir1, NULL);
BLI_split_dirfile(mainvar->name, dir2, NULL);
/* just incase there is some subtle difference */
BLI_cleanup_dir(mainvar->name, dir1);
BLI_cleanup_dir(mainvar->name, dir2);
if(BLI_path_cmp(dir1, dir2)==0) {
write_flags &= ~G_FILE_RELATIVE_REMAP;
}
else {
if(G.relbase_valid) {
/* blend may not have been saved before. Tn this case
* we should not have any relative paths, but if there
* is somehow, an invalid or empty G.main->name it will
* print an error, dont try make the absolute in this case. */
makeFilesAbsolute(mainvar, G.main->name, NULL);
}
}
}
BLI_make_file_string(G.main->name, userfilename, BLI_get_folder_create(BLENDER_USER_CONFIG, NULL), BLENDER_STARTUP_FILE);
write_user_block= (BLI_path_cmp(filepath, userfilename) == 0);
if(write_flags & G_FILE_RELATIVE_REMAP)
makeFilesRelative(mainvar, filepath, NULL); /* note, making relative to something OTHER then G.main->name */
/* actual file writing */
err= write_file_handle(mainvar, file, NULL,NULL, write_user_block, write_flags, thumb);
close(file);
/* rename/compress */
if(!err) {
if(write_flags & G_FILE_COMPRESS) {
/* compressed files have the same ending as regular files... only from 2.4!!! */
char gzname[FILE_MAXDIR+FILE_MAXFILE+4];
int ret;
/* first write compressed to separate @.gz */
BLI_snprintf(gzname, sizeof(gzname), "%s@.gz", filepath);
ret = BLI_gzip(tempname, gzname);
if(0==ret) {
/* now rename to real file name, and delete temp @ file too */
if(BLI_rename(gzname, filepath) != 0) {
BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @.");
return 0;
}
BLI_delete(tempname, 0, 0);
}
else if(-1==ret) {
BKE_report(reports, RPT_ERROR, "Failed opening .gz file.");
return 0;
}
else if(-2==ret) {
BKE_report(reports, RPT_ERROR, "Failed opening .blend file for compression.");
return 0;
}
}
else if(BLI_rename(tempname, filepath) != 0) {
BKE_report(reports, RPT_ERROR, "Can't change old file. File saved with @");
return 0;
}
}
else {
BKE_report(reports, RPT_ERROR, strerror(errno));
remove(tempname);
return 0;
}
return 1;
}
/* return: success (1) */
int BLO_write_file_mem(Main *mainvar, MemFile *compare, MemFile *current, int write_flags)
{
int err;
err= write_file_handle(mainvar, 0, compare, current, 0, write_flags, NULL);
if(err==0) return 1;
return 0;
}
|