| 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 |
type |
| 20 |
{ Options for the @link(UrlSaveStream) function. } |
| 21 |
TSaveStreamOption = ( |
| 22 |
{ Filter the contents through gzip compression. } |
| 23 |
ssoGzip |
| 24 |
); |
| 25 |
TSaveStreamOptions = set of TSaveStreamOption; |
| 26 |
|
| 27 |
{ Create a stream to save (write to) a given URL. |
| 28 |
|
| 29 |
For example, to save a file. |
| 30 |
When you use URLSaveStream with URL with a @code(file) protocol, |
| 31 |
or just with a regular filename, it will return a TFileStream instance |
| 32 |
to write this file. |
| 33 |
|
| 34 |
This function supports any CGE custom protocol registered by @link(RegisterUrlProtocol), |
| 35 |
if only you provided @code(WriteEvent: TUrlWriteEvent) when registering that |
| 36 |
protocol. |
| 37 |
|
| 38 |
Note: When saving to @code(castle-data) URL, remember that on some platforms |
| 39 |
(like Android) or installation methods (like system-wide installation |
| 40 |
on Linux or Windows) the game data is read-only. To be portable, |
| 41 |
you should never use URLSaveStream with the @code(castle-data) protocol. |
| 42 |
It is only useful in limited cases when you know that you distribute your |
| 43 |
game in such way (and users install it in such way) and on such platforms, |
| 44 |
that the "data" directory is writable. |
| 45 |
|
| 46 |
On Android, you should use the "write_external_storage" |
| 47 |
service to be able to write storage files (e.g. to SD card). |
| 48 |
This means files accessed by the 'file' protocol. |
| 49 |
See https://castle-engine.io/android-Services . |
| 50 |
|
| 51 |
Example usage: |
| 52 |
|
| 53 |
@longCode(# |
| 54 |
var |
| 55 |
Stream: TStream; |
| 56 |
I: Uint32; |
| 57 |
begin |
| 58 |
Stream := URLSaveStream('file:///tmp/foo.txt', []); |
| 59 |
try |
| 60 |
// write some strings |
| 61 |
WritelnStr(Stream, 'Some string'); |
| 62 |
WritelnStr(Stream, 'Another string'); |
| 63 |
// write some binary data |
| 64 |
I := 666; |
| 65 |
Stream.WriteBuffer(I, SizeOf(I)); |
| 66 |
finally FreeAndNil(Stream) end; |
| 67 |
end; |
| 68 |
#) |
| 69 |
|
| 70 |
@raises(ESaveError In case of problems saving this URL.) |
| 71 |
|
| 72 |
@raises(Exception Various TStream instances (used internally by this |
| 73 |
function) may raise exceptions in case the stream cannot be created |
| 74 |
for saving. |
| 75 |
Right now, we simply let these exceptions to "pass through" from this function |
| 76 |
(instead of catching and re-raising). |
| 77 |
So be ready that this function may raise @italic(any) Exception class.) |
| 78 |
} |
| 79 |
function UrlSaveStream(const Url: String; const Options: TSaveStreamOptions = []): TStream; |
| 80 |
|
| 81 |
{$endif} |
| 82 |
|
| 83 |
{$ifdef read_implementation} |
| 84 |
|
| 85 |
function UrlSaveStream(const Url: String; const Options: TSaveStreamOptions): TStream; |
| 86 |
var |
| 87 |
P: String; |
| 88 |
RegisteredProtocol: TRegisteredProtocol; |
| 89 |
UnderlyingStream: TStream; |
| 90 |
begin |
| 91 |
P := URIProtocol(Url); |
| 92 |
|
| 93 |
{ Handle early castle-data:, protocol to access application data, |
| 94 |
https://castle-engine.io/data } |
| 95 |
if P = 'castle-data' then |
| 96 |
begin |
| 97 |
Result := UrlSaveStream(ResolveCastleDataUrl(Url), Options); |
| 98 |
Exit; |
| 99 |
end; |
| 100 |
|
| 101 |
RegisteredProtocol := RegisteredProtocols.Find(P); |
| 102 |
if RegisteredProtocol <> nil then |
| 103 |
begin |
| 104 |
if Assigned(RegisteredProtocol.WriteEvent) then |
| 105 |
begin |
| 106 |
UnderlyingStream := RegisteredProtocol.WriteEvent(Url); |
| 107 |
// Compress using gzip, if requested in Options |
| 108 |
if ssoGzip in Options then |
| 109 |
Result := TGZFileStream.Create(UnderlyingStream, true) |
| 110 |
else |
| 111 |
Result := UnderlyingStream; |
| 112 |
end else |
| 113 |
raise EDownloadError.CreateFmt('Cannot write URLs with protocol "%s"', [P]); |
| 114 |
end else |
| 115 |
raise ESaveError.CreateFmt('Saving of URL with protocol "%s" not possible', [P]); |
| 116 |
end; |
| 117 |
|
| 118 |
{$endif} |