svn merge ^/trunk/blender -r43294:43338
[blender.git] / source / blender / blenloader / intern / readfile.c
index 6479c3ee04ddb8b42952777fcb03e1996d9af025..0328be060cc4832920f2d185d6114faf6f040da1 100644 (file)
@@ -39,6 +39,7 @@
 #include <fcntl.h> // for open
 #include <string.h> // for strrchr strncmp strstr
 #include <math.h> // for fabs
 #include <fcntl.h> // for open
 #include <string.h> // for strrchr strncmp strstr
 #include <math.h> // for fabs
+#include <stdarg.h> /* for va_start/end */
 
 #ifndef WIN32
        #include <unistd.h> // for read close
 
 #ifndef WIN32
        #include <unistd.h> // for read close
 
 #include "MEM_guardedalloc.h"
 
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_utildefines.h"
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
 #include "BLI_math.h"
-#include "BLI_utildefines.h"
+#include "BLI_cellalloc.h"
+#include "BLI_edgehash.h"
 
 #include "BKE_anim.h"
 #include "BKE_action.h"
 
 #include "BKE_anim.h"
 #include "BKE_action.h"
 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
 #include "BKE_sound.h"
 
 #include "BKE_utildefines.h" // SWITCH_INT DATA ENDB DNA1 O_BINARY GLOB USER TEST REND
 #include "BKE_sound.h"
 
+#include "IMB_imbuf.h"  // for proxy / timecode versioning stuff
+
 #include "NOD_socket.h"
 
 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
 #include "NOD_socket.h"
 
 //XXX #include "BIF_butspace.h" // badlevel, for do_versions, patching event codes
@@ -247,6 +252,31 @@ static void *read_struct(FileData *fd, BHead *bh, const char *blockname);
 static void direct_link_modifiers(FileData *fd, ListBase *lb);
 static void convert_tface_mt(FileData *fd, Main *main);
 
 static void direct_link_modifiers(FileData *fd, ListBase *lb);
 static void convert_tface_mt(FileData *fd, Main *main);
 
+/* this function ensures that reports are printed,
+ * in the case of libraray linking errors this is important!
+ *
+ * bit kludge but better then doubling up on prints,
+ * we could alternatively have a versions of a report function which foces printing - campbell
+ */
+static void BKE_reportf_wrap(ReportList *reports, ReportType type, const char *format, ...)
+{
+       char fixed_buf[1024]; /* should be long enough */
+
+       va_list args;
+
+       va_start(args, format);
+       vsnprintf(fixed_buf, sizeof(fixed_buf), format, args);
+       va_end(args);
+
+       fixed_buf[sizeof(fixed_buf) - 1] = '\0';
+
+       BKE_report(reports, type, fixed_buf);
+
+       if(G.background==0) {
+               printf("%s\n", fixed_buf);
+       }
+}
+
 static OldNewMap *oldnewmap_new(void) 
 {
        OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
 static OldNewMap *oldnewmap_new(void) 
 {
        OldNewMap *onm= MEM_callocN(sizeof(*onm), "OldNewMap");
@@ -2674,6 +2704,16 @@ static void lib_link_key(FileData *fd, Main *main)
 
        key= main->key.first;
        while(key) {
 
        key= main->key.first;
        while(key) {
+               /*check if we need to generate unique ids for the shapekeys*/
+               if (!key->uidgen) {
+                       KeyBlock *block;
+
+                       key->uidgen = 1;
+                       for (block=key->block.first; block; block=block->next) {
+                               block->uid = key->uidgen++;
+                       }
+               }
+
                if(key->id.flag & LIB_NEEDLINK) {
                        if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
                        
                if(key->id.flag & LIB_NEEDLINK) {
                        if(key->adt) lib_link_animdata(fd, &key->id, key->adt);
                        
@@ -3596,6 +3636,26 @@ static void lib_link_customdata_mtface(FileData *fd, Mesh *me, CustomData *fdata
 
 }
 
 
 }
 
+static void lib_link_customdata_mtpoly(FileData *fd, Mesh *me, CustomData *pdata, int totface)
+{
+       int i;
+
+       for(i=0; i<pdata->totlayer; i++) {
+               CustomDataLayer *layer = &pdata->layers[i];
+               
+               if(layer->type == CD_MTEXPOLY) {
+                       MTexPoly *tf= layer->data;
+                       int i;
+
+                       for (i=0; i<totface; i++, tf++) {
+                               tf->tpage= newlibadr(fd, me->id.lib, tf->tpage);
+                               if(tf->tpage && tf->tpage->id.us==0)
+                                       tf->tpage->id.us= 1;
+                       }
+               }
+       }
+}
+
 static void lib_link_mesh(FileData *fd, Main *main)
 {
        Mesh *me;
 static void lib_link_mesh(FileData *fd, Main *main)
 {
        Mesh *me;
@@ -3623,10 +3683,32 @@ static void lib_link_mesh(FileData *fd, Main *main)
                        me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
 
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
                        me->texcomesh= newlibadr_us(fd, me->id.lib, me->texcomesh);
 
                        lib_link_customdata_mtface(fd, me, &me->fdata, me->totface);
+                       lib_link_customdata_mtpoly(fd, me, &me->pdata, me->totpoly);
                        if(me->mr && me->mr->levels.first)
                                lib_link_customdata_mtface(fd, me, &me->mr->fdata,
                                                           ((MultiresLevel*)me->mr->levels.first)->totface);
 
                        if(me->mr && me->mr->levels.first)
                                lib_link_customdata_mtface(fd, me, &me->mr->fdata,
                                                           ((MultiresLevel*)me->mr->levels.first)->totface);
 
+                       /*check if we need to convert mfaces to mpolys*/
+                       if (me->totface && !me->totpoly) {
+                               convert_mfaces_to_mpolys(me);
+                       }
+                       
+                       /*
+                        * Re-tesselate, even if the polys were just created from tessfaces, this
+                        * is important because it:
+                        *  - fill the CD_POLYINDEX layer
+                        *  - gives consistency of tessface between loading from a file and
+                        *    converting an edited BMesh back into a mesh (i.e. it replaces
+                        *    quad tessfaces in a loaded mesh immediately, instead of lazily
+                        *    waiting until edit mode has been entered/exited, making it easier
+                        *    to recognize problems that would otherwise only show up after edits).
+                        */
+                       me->totface = mesh_recalcTesselation(
+                               &me->fdata, &me->ldata, &me->pdata,
+                               me->mvert, me->totface, me->totloop, me->totpoly);
+
+                       mesh_update_customdata_pointers(me, TRUE);
+
                        me->id.flag -= LIB_NEEDLINK;
                }
                me= me->id.next;
                        me->id.flag -= LIB_NEEDLINK;
                }
                me= me->id.next;
@@ -3645,10 +3727,17 @@ static void direct_link_dverts(FileData *fd, int count, MDeformVert *mdverts)
        }
 
        for (i= count; i > 0; i--, mdverts++) {
        }
 
        for (i= count; i > 0; i--, mdverts++) {
-               if(mdverts->dw) {
-                       mdverts->dw= newdataadr(fd, mdverts->dw);
+               /*convert to vgroup allocation system*/
+               MDeformWeight *dw;
+               if(mdverts->dw && (dw= newdataadr(fd, mdverts->dw))) {
+                       const ssize_t dw_len= mdverts->totweight * sizeof(MDeformWeight);
+                       void *dw_tmp= BLI_cellalloc_malloc(dw_len, "direct_link_dverts");
+                       memcpy(dw_tmp, dw, dw_len);
+                       mdverts->dw= dw_tmp;
+                       MEM_freeN(dw);
                }
                }
-               if (mdverts->dw == NULL) {
+               else {
+                       mdverts->dw= NULL;
                        mdverts->totweight= 0;
                }
        }
                        mdverts->totweight= 0;
                }
        }
@@ -3661,7 +3750,18 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
 
                for(i = 0; i < count; ++i) {
                        mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
 
                for(i = 0; i < count; ++i) {
                        mdisps[i].disps = newdataadr(fd, mdisps[i].disps);
-
+                       
+                       /*put .disps into cellalloc system*/
+                       if (mdisps[i].disps) {
+                               float *disp2;
+                               
+                               disp2 = BLI_cellalloc_malloc(MEM_allocN_len(mdisps[i].disps), "cellalloc .disps copy");
+                               memcpy(disp2, mdisps[i].disps, MEM_allocN_len(mdisps[i].disps));
+                               
+                               MEM_freeN(mdisps[i].disps);
+                               mdisps[i].disps = (float (*)[3])disp2;
+                       }
+                       
                        if( (fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps) ) {
                                /* DNA_struct_switch_endian doesn't do endian swap for (*disps)[] */
                                /* this does swap for data written at write_mdisps() - readfile.c */
                        if( (fd->flags & FD_FLAGS_SWITCH_ENDIAN) && (mdisps[i].disps) ) {
                                /* DNA_struct_switch_endian doesn't do endian swap for (*disps)[] */
                                /* this does swap for data written at write_mdisps() - readfile.c */
@@ -3678,6 +3778,7 @@ static void direct_link_mdisps(FileData *fd, int count, MDisps *mdisps, int exte
        }
 }
 
        }
 }
 
+/*this isn't really a public api function, so prototyped here*/
 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 {
        int i = 0;
 static void direct_link_customdata(FileData *fd, CustomData *data, int count)
 {
        int i = 0;
@@ -3698,6 +3799,8 @@ static void direct_link_customdata(FileData *fd, CustomData *data, int count)
                        i++;
                }
        }
                        i++;
                }
        }
+
+       CustomData_update_typemap(data);
 }
 
 static void direct_link_mesh(FileData *fd, Mesh *mesh)
 }
 
 static void direct_link_mesh(FileData *fd, Mesh *mesh)
@@ -3708,12 +3811,17 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        mesh->mvert= newdataadr(fd, mesh->mvert);
        mesh->medge= newdataadr(fd, mesh->medge);
        mesh->mface= newdataadr(fd, mesh->mface);
        mesh->mvert= newdataadr(fd, mesh->mvert);
        mesh->medge= newdataadr(fd, mesh->medge);
        mesh->mface= newdataadr(fd, mesh->mface);
+       mesh->mloop= newdataadr(fd, mesh->mloop);
+       mesh->mpoly= newdataadr(fd, mesh->mpoly);
        mesh->tface= newdataadr(fd, mesh->tface);
        mesh->mtface= newdataadr(fd, mesh->mtface);
        mesh->mcol= newdataadr(fd, mesh->mcol);
        mesh->msticky= newdataadr(fd, mesh->msticky);
        mesh->dvert= newdataadr(fd, mesh->dvert);
        mesh->tface= newdataadr(fd, mesh->tface);
        mesh->mtface= newdataadr(fd, mesh->mtface);
        mesh->mcol= newdataadr(fd, mesh->mcol);
        mesh->msticky= newdataadr(fd, mesh->msticky);
        mesh->dvert= newdataadr(fd, mesh->dvert);
-       
+       mesh->mloopcol= newdataadr(fd, mesh->mloopcol);
+       mesh->mloopuv= newdataadr(fd, mesh->mloopuv);
+       mesh->mtpoly= newdataadr(fd, mesh->mtpoly);
+
        /* animdata */
        mesh->adt= newdataadr(fd, mesh->adt);
        direct_link_animdata(fd, mesh->adt);
        /* animdata */
        mesh->adt= newdataadr(fd, mesh->adt);
        direct_link_animdata(fd, mesh->adt);
@@ -3725,10 +3833,42 @@ static void direct_link_mesh(FileData *fd, Mesh *mesh)
        direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
        direct_link_customdata(fd, &mesh->edata, mesh->totedge);
        direct_link_customdata(fd, &mesh->fdata, mesh->totface);
        direct_link_customdata(fd, &mesh->vdata, mesh->totvert);
        direct_link_customdata(fd, &mesh->edata, mesh->totedge);
        direct_link_customdata(fd, &mesh->fdata, mesh->totface);
+       direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
+       direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
+       
+
+#ifdef USE_BMESH_FORWARD_COMPAT
+       /* NEVER ENABLE THIS CODE INTO BMESH!
+        * THIS IS FOR LOADING BMESH INTO OLDER FILES ONLY */
+       mesh->mpoly= newdataadr(fd, mesh->mpoly);
+       mesh->mloop= newdataadr(fd, mesh->mloop);
+
+       direct_link_customdata(fd, &mesh->pdata, mesh->totpoly);
+       direct_link_customdata(fd, &mesh->ldata, mesh->totloop);
+
+       if (mesh->mpoly) {
+               /* be clever and load polygons as mfaces */
+
+               mesh->totface= mesh_mpoly_to_mface(&mesh->fdata, &mesh->ldata, &mesh->pdata,
+                                                  mesh->totface, mesh->totloop, mesh->totpoly);
+
+               CustomData_free(&mesh->pdata, mesh->totpoly);
+               memset(&mesh->pdata, 0, sizeof(CustomData));
+               mesh->totpoly = 0;
+
+               CustomData_free(&mesh->ldata, mesh->totloop);
+               memset(&mesh->ldata, 0, sizeof(CustomData));
+               mesh->totloop = 0;
+
+               mesh_update_customdata_pointers(mesh);
+       }
+
+#endif
+
 
        mesh->bb= NULL;
        mesh->mselect = NULL;
 
        mesh->bb= NULL;
        mesh->mselect = NULL;
-       mesh->edit_mesh= NULL;
+       mesh->edit_btmesh= NULL;
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
        
        /* Multires data */
        mesh->mr= newdataadr(fd, mesh->mr);
@@ -4080,8 +4220,9 @@ static void lib_link_object(FileData *fd, Main *main)
                ob= ob->id.next;
        }
 
                ob= ob->id.next;
        }
 
-       if(warn)
+       if(warn) {
                BKE_report(fd->reports, RPT_WARNING, "Warning in console");
                BKE_report(fd->reports, RPT_WARNING, "Warning in console");
+       }
 }
 
 
 }
 
 
@@ -4662,8 +4803,9 @@ static void lib_link_scene(FileData *fd, Main *main)
                                base->object= newlibadr_us(fd, sce->id.lib, base->object);
                                
                                if(base->object==NULL) {
                                base->object= newlibadr_us(fd, sce->id.lib, base->object);
                                
                                if(base->object==NULL) {
-                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: Object lost from scene:'%s\'\n", sce->id.name+2);
-                                       if(G.background==0) printf("LIB ERROR: base removed from scene:'%s\'\n", sce->id.name+2);
+                                       BKE_reportf_wrap(fd->reports, RPT_ERROR,
+                                                        "LIB ERROR: Object lost from scene:'%s\'\n",
+                                                        sce->id.name+2);
                                        BLI_remlink(&sce->base, base);
                                        if(base==sce->basact) sce->basact= NULL;
                                        MEM_freeN(base);
                                        BLI_remlink(&sce->base, base);
                                        if(base==sce->basact) sce->basact= NULL;
                                        MEM_freeN(base);
@@ -4676,7 +4818,7 @@ static void lib_link_scene(FileData *fd, Main *main)
                                if(seq->scene) {
                                        seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
                                        if(seq->scene) {
                                if(seq->scene) {
                                        seq->scene= newlibadr(fd, sce->id.lib, seq->scene);
                                        if(seq->scene) {
-                                               seq->scene_sound = sound_scene_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                               seq->scene_sound = sound_scene_add_scene_sound_defaults(sce, seq);
                                        }
                                }
                                if(seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
                                        }
                                }
                                if(seq->scene_camera) seq->scene_camera= newlibadr(fd, sce->id.lib, seq->scene_camera);
@@ -4688,7 +4830,7 @@ static void lib_link_scene(FileData *fd, Main *main)
                                                seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
                                        if (seq->sound) {
                                                seq->sound->id.us++;
                                                seq->sound= newlibadr(fd, sce->id.lib, seq->sound);
                                        if (seq->sound) {
                                                seq->sound->id.us++;
-                                               seq->scene_sound = sound_add_scene_sound(sce, seq, seq->startdisp, seq->enddisp, seq->startofs + seq->anim_startofs);
+                                               seq->scene_sound = sound_add_scene_sound_defaults(sce, seq);
                                        }
                                }
                                seq->anim= NULL;
                                        }
                                }
                                seq->anim= NULL;
@@ -5763,8 +5905,9 @@ static void direct_link_library(FileData *fd, Library *lib, Main *main)
        for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
                if(newmain->curlib) {
                        if(BLI_path_cmp(newmain->curlib->filepath, lib->filepath) == 0) {
        for(newmain= fd->mainlist.first; newmain; newmain= newmain->next) {
                if(newmain->curlib) {
                        if(BLI_path_cmp(newmain->curlib->filepath, lib->filepath) == 0) {
-                               printf("Fixed error in file; multiple instances of lib:\n %s\n", lib->filepath);
-                               BKE_reportf(fd->reports, RPT_WARNING, "Library '%s', '%s' had multiple instances, save and reload!", lib->name, lib->filepath);
+                               BKE_reportf_wrap(fd->reports, RPT_WARNING,
+                                                "Library '%s', '%s' had multiple instances, save and reload!",
+                                                lib->name, lib->filepath);
 
                                change_idid_adr(&fd->mainlist, fd, lib, newmain->curlib);
 //                             change_idid_adr_fd(fd, lib, newmain->curlib);
 
                                change_idid_adr(&fd->mainlist, fd, lib, newmain->curlib);
 //                             change_idid_adr_fd(fd, lib, newmain->curlib);
@@ -6496,20 +6639,20 @@ static void customdata_version_242(Mesh *me)
                if (layer->type == CD_MTFACE) {
                        if (layer->name[0] == 0) {
                                if (mtfacen == 0) strcpy(layer->name, "UVMap");
                if (layer->type == CD_MTFACE) {
                        if (layer->name[0] == 0) {
                                if (mtfacen == 0) strcpy(layer->name, "UVMap");
-                               else sprintf(layer->name, "UVMap.%.3d", mtfacen);
+                               else BLI_snprintf(layer->name, sizeof(layer->name), "UVMap.%.3d", mtfacen);
                        }
                        mtfacen++;
                }
                else if (layer->type == CD_MCOL) {
                        if (layer->name[0] == 0) {
                                if (mcoln == 0) strcpy(layer->name, "Col");
                        }
                        mtfacen++;
                }
                else if (layer->type == CD_MCOL) {
                        if (layer->name[0] == 0) {
                                if (mcoln == 0) strcpy(layer->name, "Col");
-                               else sprintf(layer->name, "Col.%.3d", mcoln);
+                               else BLI_snprintf(layer->name, sizeof(layer->name), "Col.%.3d", mcoln);
                        }
                        mcoln++;
                }
        }
 
                        }
                        mcoln++;
                }
        }
 
-       mesh_update_customdata_pointers(me);
+       mesh_update_customdata_pointers(me, TRUE);
 }
 
 /*only copy render texface layer from active*/
 }
 
 /*only copy render texface layer from active*/
@@ -7459,6 +7602,56 @@ void do_versions_image_settings_2_60(Scene *sce)
 
 }
 
 
 }
 
+/* socket use flags were only temporary before */
+static void do_versions_nodetree_socket_use_flags_2_62(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       bNodeLink *link;
+       
+       for (node=ntree->nodes.first; node; node=node->next) {
+               for (sock=node->inputs.first; sock; sock=sock->next)
+                       sock->flag &= ~SOCK_IN_USE;
+               for (sock=node->outputs.first; sock; sock=sock->next)
+                       sock->flag &= ~SOCK_IN_USE;
+       }
+       for (sock=ntree->inputs.first; sock; sock=sock->next)
+               sock->flag &= ~SOCK_IN_USE;
+       for (sock=ntree->outputs.first; sock; sock=sock->next)
+               sock->flag &= ~SOCK_IN_USE;
+       
+       for (link=ntree->links.first; link; link=link->next) {
+               link->fromsock->flag |= SOCK_IN_USE;
+               link->tosock->flag |= SOCK_IN_USE;
+       }
+}
+
+/* set the SOCK_AUTO_HIDDEN flag on collapsed nodes */
+static void do_versions_nodetree_socket_auto_hidden_flags_2_62(bNodeTree *ntree)
+{
+       bNode *node;
+       bNodeSocket *sock;
+       
+       for (node=ntree->nodes.first; node; node=node->next) {
+               if (node->flag & NODE_HIDDEN) {
+                       for (sock=node->inputs.first; sock; sock=sock->next) {
+                               if (sock->link==NULL)
+                                       sock->flag |= SOCK_AUTO_HIDDEN;
+                       }
+                       for(sock=node->outputs.first; sock; sock= sock->next) {
+                               if(nodeCountSocketLinks(ntree, sock)==0)
+                                       sock->flag |= SOCK_AUTO_HIDDEN;
+                       }
+               }
+               else {
+                       for(sock=node->inputs.first; sock; sock= sock->next)
+                               sock->flag &= ~SOCK_AUTO_HIDDEN;
+                       for(sock=node->outputs.first; sock; sock= sock->next)
+                               sock->flag &= ~SOCK_AUTO_HIDDEN;
+               }
+       }
+}
+
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
 static void do_versions(FileData *fd, Library *lib, Main *main)
 {
        /* WATCH IT!!!: pointers from libdata have not been converted */
@@ -8746,9 +8939,9 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        }
                        if(ob->soft && ob->soft->vertgroup==0) {
                                bDeformGroup *locGroup = defgroup_find_name(ob, "SOFTGOAL");
                        }
                        if(ob->soft && ob->soft->vertgroup==0) {
                                bDeformGroup *locGroup = defgroup_find_name(ob, "SOFTGOAL");
-                               if(locGroup){
+                               if (locGroup) {
                                        /* retrieve index for that group */
                                        /* retrieve index for that group */
-                                       ob->soft->vertgroup =  1 + defgroup_find_index(ob, locGroup); 
+                                       ob->soft->vertgroup =  1 + BLI_findindex(&ob->defbase, locGroup);
                                }
                        }
                }
                                }
                        }
                }
@@ -8948,8 +9141,9 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                                        strcpy(kb->name, "Basis");
                                        }
                                        else {
                                                        strcpy(kb->name, "Basis");
                                        }
                                        else {
-                                               if(kb->name[0]==0)
-                                                       sprintf(kb->name, "Key %d", index);
+                                               if (kb->name[0]==0) {
+                                                       BLI_snprintf(kb->name, sizeof(kb->name), "Key %d", index);
+                                               }
                                                kb->adrcode= index++;
                                        }
                                }
                                                kb->adrcode= index++;
                                        }
                                }
@@ -9942,7 +10136,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                BLI_addtail(&ob->particlesystem, psys);
 
                                md= modifier_new(eModifierType_ParticleSystem);
                                BLI_addtail(&ob->particlesystem, psys);
 
                                md= modifier_new(eModifierType_ParticleSystem);
-                               sprintf(md->name, "ParticleSystem %i", BLI_countlist(&ob->particlesystem));
+                               BLI_snprintf(md->name, sizeof(md->name), "ParticleSystem %i", BLI_countlist(&ob->particlesystem));
                                psmd= (ParticleSystemModifierData*) md;
                                psmd->psys=psys;
                                BLI_addtail(&ob->modifiers, md);
                                psmd= (ParticleSystemModifierData*) md;
                                psmd->psys=psys;
                                BLI_addtail(&ob->modifiers, md);
@@ -10686,8 +10880,11 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                void *olddata = ob->data;
                                ob->data = me;
 
                                void *olddata = ob->data;
                                ob->data = me;
 
-                               if(me && me->id.lib==NULL && me->mr && me->mr->level_count > 1) /* XXX - library meshes crash on loading most yoFrankie levels, the multires pointer gets invalid -  Campbell */
+                               /* XXX - library meshes crash on loading most yoFrankie levels,
+                                * the multires pointer gets invalid -  Campbell */
+                               if(me && me->id.lib==NULL && me->mr && me->mr->level_count > 1) {
                                        multires_load_old(ob, me);
                                        multires_load_old(ob, me);
+                               }
 
                                ob->data = olddata;
                        }
 
                                ob->data = olddata;
                        }
@@ -10786,7 +10983,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                        sce->gm.dome.warptext = sce->r.dometext;
 
                        //Stand Alone
                        sce->gm.dome.warptext = sce->r.dometext;
 
                        //Stand Alone
-                       sce->gm.fullscreen = sce->r.fullscreen;
+                       sce->gm.playerflag |= (sce->r.fullscreen?GAME_PLAYER_FULLSCREEN:0);
                        sce->gm.xplay = sce->r.xplay;
                        sce->gm.yplay = sce->r.yplay;
                        sce->gm.freqplay = sce->r.freqplay;
                        sce->gm.xplay = sce->r.xplay;
                        sce->gm.yplay = sce->r.yplay;
                        sce->gm.freqplay = sce->r.freqplay;
@@ -12157,7 +12354,7 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                Mesh *me;
 
                for(me= main->mesh.first; me; me= me->id.next)
                Mesh *me;
 
                for(me= main->mesh.first; me; me= me->id.next)
-                       mesh_calc_normals(me->mvert, me->totvert, me->mface, me->totface, NULL);
+                       mesh_calc_normals_tessface(me->mvert, me->totvert, me->mface, me->totface, NULL);
        }
 
        if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)){
        }
 
        if (main->versionfile < 256 || (main->versionfile == 256 && main->subversionfile < 2)){
@@ -12585,10 +12782,12 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                                        clip->aspy= 1.0f;
                                }
 
                                        clip->aspy= 1.0f;
                                }
 
-                               /* XXX: a bit hacky, probably include imbuf and use real constants are nicer */
-                               clip->proxy.build_tc_flag= 7;
+                               clip->proxy.build_tc_flag= IMB_TC_RECORD_RUN |
+                                                          IMB_TC_FREE_RUN |
+                                                          IMB_TC_INTERPOLATED_REC_DATE_FREE_RUN;
+
                                if(clip->proxy.build_size_flag==0)
                                if(clip->proxy.build_size_flag==0)
-                                       clip->proxy.build_size_flag= 1;
+                                       clip->proxy.build_size_flag= IMB_PROXY_25;
 
                                if(clip->proxy.quality==0)
                                        clip->proxy.quality= 90;
 
                                if(clip->proxy.quality==0)
                                        clip->proxy.quality= 90;
@@ -12676,17 +12875,188 @@ static void do_versions(FileData *fd, Library *lib, Main *main)
                }
        }
 
                }
        }
 
-       /* put compatibility code here until next subversion bump */
+       if (main->versionfile < 260 || (main->versionfile == 260 && main->subversionfile < 8))
        {
        {
-               MovieClip *clip;
+               Brush *brush;
 
 
-               for (clip= main->movieclip.first; clip; clip= clip->id.next) {
-                       MovieTracking *tracking= &clip->tracking;
+               for (brush= main->brush.first; brush; brush= brush->id.next) {
+                       if (brush->sculpt_tool == SCULPT_TOOL_ROTATE)
+                               brush->alpha= 1.0f;
+               }
+       }
+
+       if (main->versionfile < 261 || (main->versionfile == 261 && main->subversionfile < 1))
+       {
+               {
+                       /* update use flags for node sockets (was only temporary before) */
+                       Scene *sce;
+                       Material *mat;
+                       Tex *tex;
+                       Lamp *lamp;
+                       World *world;
+                       bNodeTree *ntree;
+
+                       for (sce=main->scene.first; sce; sce=sce->id.next)
+                               if (sce->nodetree)
+                                       do_versions_nodetree_socket_use_flags_2_62(sce->nodetree);
+
+                       for (mat=main->mat.first; mat; mat=mat->id.next)
+                               if (mat->nodetree)
+                                       do_versions_nodetree_socket_use_flags_2_62(mat->nodetree);
+
+                       for (tex=main->tex.first; tex; tex=tex->id.next)
+                               if (tex->nodetree)
+                                       do_versions_nodetree_socket_use_flags_2_62(tex->nodetree);
+
+                       for (lamp=main->lamp.first; lamp; lamp=lamp->id.next)
+                               if (lamp->nodetree)
+                                       do_versions_nodetree_socket_use_flags_2_62(lamp->nodetree);
+
+                       for (world=main->world.first; world; world=world->id.next)
+                               if (world->nodetree)
+                                       do_versions_nodetree_socket_use_flags_2_62(world->nodetree);
+
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
+                               do_versions_nodetree_socket_use_flags_2_62(ntree);
+               }
+               {
+                       /* Initialize BGE exit key to esc key */
+                       Scene *scene;
+                       for(scene= main->scene.first; scene; scene= scene->id.next) {
+                               if (!scene->gm.exitkey)
+                                       scene->gm.exitkey = 218; // Blender key code for ESC
+                       }
+               }
+               {
+                       MovieClip *clip;
+                       Object *ob;
+
+                       for (clip= main->movieclip.first; clip; clip= clip->id.next) {
+                               MovieTracking *tracking= &clip->tracking;
+                               MovieTrackingObject *tracking_object= tracking->objects.first;
+
+                               clip->proxy.build_tc_flag|= IMB_TC_RECORD_RUN_NO_GAPS;
+
+                               if(!tracking->settings.object_distance)
+                                       tracking->settings.object_distance= 1.0f;
+
+                               if(tracking->objects.first == NULL)
+                                       BKE_tracking_new_object(tracking, "Camera");
+
+                               while(tracking_object) {
+                                       if(!tracking_object->scale)
+                                               tracking_object->scale= 1.0f;
+
+                                       tracking_object= tracking_object->next;
+                               }
+                       }
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               bConstraint *con;
+                               for (con= ob->constraints.first; con; con=con->next) {
+                                       bConstraintTypeInfo *cti= constraint_get_typeinfo(con);
+
+                                       if(!cti)
+                                               continue;
+
+                                       if(cti->type==CONSTRAINT_TYPE_OBJECTSOLVER) {
+                                               bObjectSolverConstraint *data= (bObjectSolverConstraint *)con->data;
+
+                                               if(data->invmat[3][3]==0.0f)
+                                                       unit_m4(data->invmat);
+                                       }
+                               }
+                       }
+               }
+               {
+               /* Warn the user if he is using ["Text"] properties for Font objects */
+                       Object *ob;
+                       bProperty *prop;
+
+                       for (ob= main->object.first; ob; ob= ob->id.next) {
+                               if (ob->type == OB_FONT) {
+                                       prop = get_ob_property(ob, "Text");
+                                       if (prop) {
+                                               BKE_reportf_wrap(fd->reports, RPT_WARNING,
+                                                                "Game property name conflict in object: \"%s\".\nText objects reserve the "
+                                                                "[\"Text\"] game property to change their content through Logic Bricks.\n",
+                                                                ob->id.name+2);
+                                       }
+                               }
+                       }
+               }
+               {
+                       /* set the SOCK_AUTO_HIDDEN flag on collapsed nodes */
+                       Scene *sce;
+                       Material *mat;
+                       Tex *tex;
+                       Lamp *lamp;
+                       World *world;
+                       bNodeTree *ntree;
+
+                       for (sce=main->scene.first; sce; sce=sce->id.next)
+                               if (sce->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(sce->nodetree);
+
+                       for (mat=main->mat.first; mat; mat=mat->id.next)
+                               if (mat->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(mat->nodetree);
+
+                       for (tex=main->tex.first; tex; tex=tex->id.next)
+                               if (tex->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(tex->nodetree);
+
+                       for (lamp=main->lamp.first; lamp; lamp=lamp->id.next)
+                               if (lamp->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(lamp->nodetree);
+
+                       for (world=main->world.first; world; world=world->id.next)
+                               if (world->nodetree)
+                                       do_versions_nodetree_socket_auto_hidden_flags_2_62(world->nodetree);
+
+                       for (ntree=main->nodetree.first; ntree; ntree=ntree->id.next)
+                               do_versions_nodetree_socket_auto_hidden_flags_2_62(ntree);
+               }
+       }
+
+       if (main->versionfile < 261 || (main->versionfile == 261 && main->subversionfile < 2))
+       {
+               {
+                       /* convert Camera Actuator values to defines */
+                       Object *ob;
+                       bActuator *act;
+                       for(ob = main->object.first; ob; ob= ob->id.next) {
+                               for(act= ob->actuators.first; act; act= act->next) {
+                                       if (act->type == ACT_CAMERA) {
+                                               bCameraActuator *ba= act->data;
 
 
-                       if(tracking->objects.first == NULL)
-                               BKE_tracking_new_object(tracking, "Camera");
+                                               if(ba->axis==(float) 'x') ba->axis=OB_POSX;
+                                               else if (ba->axis==(float)'y') ba->axis=OB_POSY;
+                                               /* don't do an if/else to avoid imediate subversion bump*/
+//                                     ba->axis=((ba->axis == (float) 'x')?OB_POSX_X:OB_POSY);
+                                       }
+                               }
+                       }
+               }
+
+               {
+                       /* convert deprecated sculpt_paint_unified_* fields to
+                          UnifiedPaintSettings */
+                       Scene *scene;
+                       for(scene= main->scene.first; scene; scene= scene->id.next) {
+                               ToolSettings *ts= scene->toolsettings;
+                               UnifiedPaintSettings *ups= &ts->unified_paint_settings;
+                               ups->size= ts->sculpt_paint_unified_size;
+                               ups->unprojected_radius= ts->sculpt_paint_unified_unprojected_radius;
+                               ups->alpha= ts->sculpt_paint_unified_alpha;
+                               ups->flag= ts->sculpt_paint_settings;
+                       }
                }
        }
                }
        }
+       
+       /* put compatibility code here until next subversion bump */
+       {
+       }
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
 
        /* WATCH IT!!!: pointers from libdata have not been converted yet here! */
        /* WATCH IT 2!: Userdef struct init has to be in editors/interface/resources.c! */
@@ -14138,8 +14508,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                if(fd==NULL) {
 
                                        /* printf and reports for now... its important users know this */
                                if(fd==NULL) {
 
                                        /* printf and reports for now... its important users know this */
-                                       BKE_reportf(basefd->reports, RPT_INFO, "read library:  '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
-                                       if(!G.background && basefd->reports) printf("read library: '%s', '%s'\n", mainptr->curlib->filepath, mainptr->curlib->name);
+                                       BKE_reportf_wrap(basefd->reports, RPT_INFO,
+                                                        "read library:  '%s', '%s'\n",
+                                                        mainptr->curlib->filepath, mainptr->curlib->name);
 
                                        fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
                                        
 
                                        fd= blo_openblenderfile(mainptr->curlib->filepath, basefd->reports);
                                        
@@ -14184,8 +14555,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                        else mainptr->curlib->filedata= NULL;
 
                                        if (fd==NULL) {
                                        else mainptr->curlib->filedata= NULL;
 
                                        if (fd==NULL) {
-                                               BKE_reportf(basefd->reports, RPT_ERROR, "Can't find lib '%s'\n", mainptr->curlib->filepath);
-                                               if(!G.background && basefd->reports) printf("ERROR: can't find lib %s \n", mainptr->curlib->filepath);
+                                               BKE_reportf_wrap(basefd->reports, RPT_ERROR,
+                                                                "Can't find lib '%s'\n",
+                                                                mainptr->curlib->filepath);
                                        }
                                }
                                if(fd) {
                                        }
                                }
                                if(fd) {
@@ -14202,8 +14574,10 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
                                                                append_id_part(fd, mainptr, id, &realid);
                                                                if (!realid) {
 
                                                                append_id_part(fd, mainptr, id, &realid);
                                                                if (!realid) {
-                                                                       BKE_reportf(fd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
-                                                                       if(!G.background && basefd->reports) printf("LIB ERROR: %s:'%s' missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
+                                                                       BKE_reportf_wrap(fd->reports, RPT_ERROR,
+                                                                                        "LIB ERROR: %s:'%s' missing from '%s'\n",
+                                                                                        BKE_idcode_to_name(GS(id->name)),
+                                                                                        id->name+2, mainptr->curlib->filepath);
                                                                }
                                                                
                                                                change_idid_adr(mainlist, basefd, id, realid);
                                                                }
                                                                
                                                                change_idid_adr(mainlist, basefd, id, realid);
@@ -14216,7 +14590,8 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
 
                                        expand_main(fd, mainptr);
                                        
 
                                        expand_main(fd, mainptr);
                                        
-                                       /* dang FileData... now new libraries need to be appended to original filedata, it is not a good replacement for the old global (ton) */
+                                       /* dang FileData... now new libraries need to be appended to original filedata,
+                                        * it is not a good replacement for the old global (ton) */
                                        while( fd->mainlist.first ) {
                                                Main *mp= fd->mainlist.first;
                                                BLI_remlink(&fd->mainlist, mp);
                                        while( fd->mainlist.first ) {
                                                Main *mp= fd->mainlist.first;
                                                BLI_remlink(&fd->mainlist, mp);
@@ -14238,8 +14613,9 @@ static void read_libraries(FileData *basefd, ListBase *mainlist)
                                ID *idn= id->next;
                                if(id->flag & LIB_READ) {
                                        BLI_remlink(lbarray[a], id);
                                ID *idn= id->next;
                                if(id->flag & LIB_READ) {
                                        BLI_remlink(lbarray[a], id);
-                                       BKE_reportf(basefd->reports, RPT_ERROR, "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
-                                       if(!G.background && basefd->reports)printf("LIB ERROR: %s:'%s' unread libblock missing from '%s'\n", BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
+                                       BKE_reportf_wrap(basefd->reports, RPT_ERROR,
+                                                        "LIB ERROR: %s:'%s' unread libblock missing from '%s'\n",
+                                                        BKE_idcode_to_name(GS(id->name)), id->name+2, mainptr->curlib->filepath);
                                        change_idid_adr(mainlist, basefd, id, NULL);
 
                                        MEM_freeN(id);
                                        change_idid_adr(mainlist, basefd, id, NULL);
 
                                        MEM_freeN(id);