Debian Codegraph @

src/files/castledownload_synchronous.inc

raw
1 {%MainUnit castledownload.pas}
2 {
3 Copyright 2013-2020 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 {$ifdef read_interface}
18
19 var
20 { Log (through CastleLog) all loading, that is: all calls to @link(Download).
21 This allows to easily check e.g. whether the engine is not loading something
22 during the game (which usually badly affects the performance). }
23 LogAllLoading: boolean = false;
24
25 var
26 { Does @link(Download) (synchronous downloading routine) support http and https protocol.
27 This is by default disabled, as it means that a call to @link(Download) may block
28 your application, for arbitrarily long time, and you have no way to interrupt it.
29 It is recommended to use asynchronous downloading, using @link(TCastleDownload),
30 to download resources over the network. }
31 EnableBlockingDownloads: boolean = false;
32
33 type
34 EDownloadError = class(Exception);
35 ESaveError = class(Exception);
36
37 { Options for the @link(Download) function. }
38 TStreamOption = (
39 { Force result to be a TCustomMemoryStream,
40 with contents fully loaded to the memory,
41 and freely seekable (you can move back and forth within).
42 Without this option, @link(Download) may return other streams,
43 for example TFileStream (that may not have good buffering, depending on OS)
44 or TBase64DecodingStream (that may not allow seeking).
45
46 Using TCustomMemoryStream means that reading is fast and comfortable,
47 but eats memory and doesn't allow to simultaneously read and process
48 the contents (the file must be fully loaded, e.g. downloaded from
49 the Internet, and ungzipped, before this function returns).
50 So use this option only for files that aren't too big.
51
52 For larger files, you usually don't want to use this option,
53 instead wrap result in TBufferedReadStream. }
54 soForceMemoryStream,
55
56 { Filter the contents through gzip decompression. }
57 soGzip
58 );
59 TStreamOptions = set of TStreamOption;
60
61 { Return a stream to read given URL.
62 Returned stream is suitable only for reading, and the initial position
63 is always at the beginning.
64 Overloaded version also returns a MIME type (or '' if unknown).
65
66 Any errors are reported by raising exceptions.
67
68 All the supported URL protocols are documented in our manual:
69 https://castle-engine.io/manual_network.php . They include:
70
71 @unorderedList(
72 @item(Local @code(file) URLs.
73 Moreover URL without any protocol is always
74 treated like a local filename (absolute or relative to current dir),
75 so this function can be a drop-in replacement for normal file reading.
76 The MIME type for local files is guessed based on their extension.
77 )
78
79 @item(Data URI scheme (http://en.wikipedia.org/wiki/Data_URI_scheme).)
80
81 @item(Protocols to embed script contents:
82 ecmascript, javascript (see X3D specifications),
83 castlescript, kambiscript (see https://castle-engine.io/castle_script.php),
84 compiled (https://castle-engine.io/x3d_extensions.php#section_ext_script_compiled).
85 )
86
87 @item(
88 Set EnableBlockingDownloads to @true to support http and https protocols.
89 The MIME type for such content is usually reported by the http server
90 (but if the server doesn't report MIME type, we still try to determine it,
91 looking at URL using UriMimeType).
92 )
93 )
94
95 This routine makes a @italic(synchronous (blocking) downloading).
96 Which means that if you use a network URL (like http://...) then your
97 application will wait until the data arrives from the Internet.
98 There may be a timeout of the request (so your application will not hang
99 indefinitely), but still your code (or user) have no way to cancel or watch
100 the progress of this operation.
101 This is why http/https support is disabled by default (see @link(EnableBlockingDownloads)).
102 This is sometimes acceptable (e.g. if you're
103 waiting during the "loading" process, and the data just @italic(has)
104 to be downloaded in order to continue), and it's really easy to use
105 (you just download data exactly the same way like you open a local file).
106
107 You can use @bold(asynchronous downloading) through the @link(TCastleDownload)
108 class instead.
109
110 @raises(EDownloadError In case of problems loading from this URL.
111
112 Any exceptions inside internal loading routines,
113 like EFOpenError or EStreamError, are internally caught and changed
114 to EDownloadError.)
115
116 @groupBegin
117 }
118 function Download(const Url: String; const Options: TStreamOptions = []): TStream; overload;
119 function Download(const Url: String; const Options: TStreamOptions;
120 out MimeType: string): TStream; overload;
121 { @groupEnd }
122
123 function GetEnableNetwork: Boolean; deprecated 'use EnableBlockingDownloads';
124 procedure SetEnableNetwork(const Value: Boolean); deprecated 'use EnableBlockingDownloads';
125 {$ifdef FPC}
126 property EnableNetwork: Boolean read GetEnableNetwork write SetEnableNetwork;
127 {$endif}
128
129 {$endif}
130
131 {$ifdef read_implementation}
132
133 function Download(const Url: String; const Options: TStreamOptions;
134 out MimeType: string): TStream;
135 var
136 D: TCastleDownload;
137 begin
138 D := TCastleDownload.Create(nil);
139 try
140 D.OwnsContents := false;
141 D.Url := Url;
142 D.Options := Options;
143 D.ForceSynchronous := true;
144 D.Start;
145 D.WaitForFinish;
146 Assert(D.Status in [dsError, dsSuccess]);
147 if D.Status = dsError then
148 begin
149 { D.Contents may be set non-nil even in case of error, testcase:
150 castle-model-viewer https://x3dgraphics.com/examples/X3dForAdvancedModeling/GltfSampleModels/AlphaBlendModeTest_not_existent.x3d
151 So make sure to free them.
152 }
153 D.OwnsContents := true;
154 raise EDownloadError.Create(D.ErrorMessage);
155 end else
156 begin
157 Assert(D.Status = dsSuccess);
158 Result := D.Contents;
159 MimeType := D.MimeType;
160 end;
161 finally FreeAndNil(D) end;
162 end;
163
164 function Download(const Url: String; const Options: TStreamOptions): TStream;
165 var
166 MimeType: string;
167 begin
168 Result := Download(Url, Options, MimeType { ignored });
169 end;
170
171 function GetEnableNetwork: Boolean;
172 begin
173 Result := EnableBlockingDownloads;
174 end;
175
176 procedure SetEnableNetwork(const Value: Boolean);
177 begin
178 EnableBlockingDownloads := Value;
179 end;
180
181 {$endif}