Welcome to mirror list, hosted at ThFree Co, Russian Federation.

runtime.c « intern « blenloader « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ca9f2faf998f5917a5e119054f5453204916bb0a (plain)
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
/*
 * ***** 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.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 */

/**
 * \file runtime.c
 * \brief This file handles the loading of .blend files embedded in runtimes
 * \ingroup blenloader
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>

#ifdef WIN32
#  include <io.h>       // read, open
#  include "BLI_winstuff.h"
#else // ! WIN32
#  include <unistd.h>       // read
#endif

#include "BLI_blenlib.h"
#include "BLI_utildefines.h"

#include "BLO_readfile.h"
#include "BLO_runtime.h"

#include "BKE_blender.h"
#include "BKE_report.h"

/* Runtime reading */

static int handle_read_msb_int(int handle)
{
	unsigned char buf[4];

	if (read(handle, buf, 4) != 4)
		return -1;

	return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0);
}

int BLO_is_a_runtime(const char *path)
{
	int res = 0, fd = BLI_open(path, O_BINARY | O_RDONLY, 0);
	int datastart;
	char buf[8];

	if (fd < 0)
		goto cleanup;
	
	lseek(fd, -12, SEEK_END);
	
	datastart = handle_read_msb_int(fd);

	if (datastart == -1)
		goto cleanup;
	else if (read(fd, buf, 8) != 8)
		goto cleanup;
	else if (memcmp(buf, "BRUNTIME", 8) != 0)
		goto cleanup;
	else
		res = 1;

cleanup:
	if (fd != -1)
		close(fd);

	return res;
}

BlendFileData *BLO_read_runtime(const char *path, ReportList *reports)
{
	BlendFileData *bfd = NULL;
	size_t actualsize;
	int fd, datastart;
	char buf[8];

	fd = BLI_open(path, O_BINARY | O_RDONLY, 0);

	if (fd < 0) {
		BKE_reportf(reports, RPT_ERROR, "Unable to open '%s': %s", path, strerror(errno));
		goto cleanup;
	}
	
	actualsize = BLI_file_descriptor_size(fd);

	lseek(fd, -12, SEEK_END);

	datastart = handle_read_msb_int(fd);

	if (datastart == -1) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (problem seeking)", path);
		goto cleanup;
	}
	else if (read(fd, buf, 8) != 8) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (truncated header)", path);
		goto cleanup;
	}
	else if (memcmp(buf, "BRUNTIME", 8) != 0) {
		BKE_reportf(reports, RPT_ERROR, "Unable to read '%s' (not a blend file)", path);
		goto cleanup;
	}
	else {
		//printf("starting to read runtime from %s at datastart %d\n", path, datastart);
		lseek(fd, datastart, SEEK_SET);
		bfd = blo_read_blendafterruntime(fd, path, actualsize - datastart, reports);
		fd = -1; // file was closed in blo_read_blendafterruntime()
	}
	
cleanup:
	if (fd != -1)
		close(fd);
	
	return bfd;
}