| 1 |
{ |
| 2 |
Copyright 2013-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Reading Android asset files as streams. } |
| 17 |
unit CastleAndroidInternalAssetStream; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Classes, CastleAndroidInternalAssetManager; |
| 24 |
|
| 25 |
type |
| 26 |
EAssetReadError = class(EReadError); |
| 27 |
EAssetNotFound = class(EAssetReadError); |
| 28 |
|
| 29 |
TReadAssetStream = class(TStream) |
| 30 |
private |
| 31 |
Asset: PAAsset; |
| 32 |
FPosition: Int64; |
| 33 |
protected |
| 34 |
function GetSize: Int64; override; |
| 35 |
function GetPosition: Int64; override; |
| 36 |
|
| 37 |
{ This stream doesn't support setting size. |
| 38 |
(All other versions of SetSize also call this.) |
| 39 |
@raises(EStreamNotImplementedSetSize Always.) } |
| 40 |
procedure SetSize(NewSize: Longint); override; |
| 41 |
public |
| 42 |
{ Open a stream for an asset on given path. |
| 43 |
The path should be a valid Android asset path, |
| 44 |
like @code(images/my_texture.png). } |
| 45 |
constructor Create(Path: string); |
| 46 |
destructor Destroy; override; |
| 47 |
|
| 48 |
{ This stream doesn't support seeking. |
| 49 |
(SetPosition and all other versions of Seek also call this.) |
| 50 |
@raises(EStreamNotImplementedSeek Always.) } |
| 51 |
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; override; |
| 52 |
|
| 53 |
{ This stream doesn't support writing. |
| 54 |
(WriteBuffer also calls this.) |
| 55 |
@raises(EStreamNotImplementedWrite Always.) } |
| 56 |
function Write(const Buffer; Count: Longint): Longint; override; |
| 57 |
|
| 58 |
function Read(var Buffer; Count: Longint): Longint; override; |
| 59 |
end; |
| 60 |
|
| 61 |
{ Assuming that this is @code(castle-android-assets:/xxx/yyy) URL, convert it to an asset path |
| 62 |
@code(xxx/yyy). Does percent-decoding along the way. } |
| 63 |
function URIToAssetPath(const URI: string): string; |
| 64 |
|
| 65 |
function AssetPathToURI(const AssetPath: string): string; |
| 66 |
|
| 67 |
implementation |
| 68 |
|
| 69 |
uses CastleAndroidInternalLog, CastleAndroidNativeAppGlue, |
| 70 |
CastleClassUtils, CastleLog, CastleStringUtils, URIParser; |
| 71 |
|
| 72 |
constructor TReadAssetStream.Create(Path: string); |
| 73 |
var |
| 74 |
AssetManager: PAAssetManager; |
| 75 |
begin |
| 76 |
inherited Create; |
| 77 |
if ExtractFileExt(Path) = '.gz' then |
| 78 |
begin |
| 79 |
// WritelnLog('Assets', 'Trying to access Android asset with .gz extension, stripping the .gz (because Android tools strip them too when packing the .apk file): %s', |
| 80 |
// [Path]); |
| 81 |
Path := ChangeFileExt(Path, ''); |
| 82 |
end; |
| 83 |
AssetManager := AndroidMainApp^.Activity^.AssetManager; |
| 84 |
Asset := AAssetManager_open(AssetManager, PChar(Path), AASSET_MODE_STREAMING); |
| 85 |
if Asset = nil then |
| 86 |
raise EAssetNotFound.CreateFmt('Android asset "%s" not found', [Path]); |
| 87 |
end; |
| 88 |
|
| 89 |
destructor TReadAssetStream.Destroy; |
| 90 |
begin |
| 91 |
if Asset <> nil then |
| 92 |
AAsset_close(Asset); |
| 93 |
inherited; |
| 94 |
end; |
| 95 |
|
| 96 |
function TReadAssetStream.Read(var Buffer; Count: Longint): Longint; |
| 97 |
begin |
| 98 |
Result := AAsset_read(Asset, @Buffer, Count); |
| 99 |
if Result < 0 then |
| 100 |
raise EAssetReadError.Create('Error when reading asset data stream'); |
| 101 |
FPosition += Result; |
| 102 |
end; |
| 103 |
|
| 104 |
function TReadAssetStream.GetSize: Int64; |
| 105 |
begin |
| 106 |
Result := AAsset_getLength(Asset); |
| 107 |
{ Take only the least-significant 32 bits of result, because |
| 108 |
on some Androids the higher 32-bits are nonsense (Sony Ericsson, |
| 109 |
Android 2.3.4, WT191l). } |
| 110 |
Result := Result and Int64(High(UInt32)); |
| 111 |
end; |
| 112 |
|
| 113 |
function TReadAssetStream.GetPosition: Int64; |
| 114 |
begin |
| 115 |
Result := FPosition; |
| 116 |
end; |
| 117 |
|
| 118 |
procedure TReadAssetStream.SetSize(NewSize: Longint); |
| 119 |
begin |
| 120 |
raise EStreamNotImplementedSetSize.Create( |
| 121 |
'TReadAssetStream.SetSize not supported'); |
| 122 |
end; |
| 123 |
|
| 124 |
function TReadAssetStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; |
| 125 |
begin |
| 126 |
if ( (Origin = soBeginning) and (Offset = FPosition) ) or |
| 127 |
( (Origin = soCurrent ) and (Offset = 0) ) then |
| 128 |
{ nothing needs to be done, ok } |
| 129 |
Exit; |
| 130 |
|
| 131 |
raise EStreamNotImplementedSeek.Create('TReadAssetStream.Seek not supported'); |
| 132 |
Result := 0; // just to get rid of warning |
| 133 |
end; |
| 134 |
|
| 135 |
function TReadAssetStream.Write(const Buffer; Count: Longint): Longint; |
| 136 |
begin |
| 137 |
raise EStreamNotImplementedWrite.Create('TReadAssetStream.Write not supported'); |
| 138 |
Result := 0; // just to get rid of warning |
| 139 |
end; |
| 140 |
|
| 141 |
{ global routines ------------------------------------------------------------ } |
| 142 |
|
| 143 |
function URIToAssetPath(const URI: string): string; |
| 144 |
var |
| 145 |
U: TURI; |
| 146 |
FixedURI: String; |
| 147 |
begin |
| 148 |
U := ParseURI(URI); |
| 149 |
if SameText(U.Protocol, 'assets') or |
| 150 |
SameText(U.Protocol, 'castle-android-assets') then |
| 151 |
begin |
| 152 |
// Try to work (but with warning) even in case of some invalid URI |
| 153 |
if IsPrefix('castle-android-assets://', URI, true) then |
| 154 |
begin |
| 155 |
WritelnWarning('Too many / at the beginning of URL "%s". This usually means you used ''castle-data://xxx'', while you should use ''castle-data:/xxx''.', |
| 156 |
[URI]); |
| 157 |
FixedURI := 'castle-android-assets:/' + PrefixRemove('castle-android-assets://', URI, true); |
| 158 |
U := ParseURI(FixedURI); |
| 159 |
end; |
| 160 |
|
| 161 |
Result := PrefixRemove('/', U.Path + U.Document, false) |
| 162 |
end else |
| 163 |
raise Exception.CreateFmt('URI does not have protocol "castle-android-assets:" or "assets:", cannot convert to Android asset path: %s, protocol %s', |
| 164 |
[URI, U.Protocol]); |
| 165 |
end; |
| 166 |
|
| 167 |
function AssetPathToURI(const AssetPath: string): string; |
| 168 |
var |
| 169 |
U: TURI; |
| 170 |
begin |
| 171 |
FillByte(U, SizeOf(U), 0); |
| 172 |
U.Protocol := 'castle-android-assets'; |
| 173 |
U.Path := '/' + AssetPath; // AssetPath does not start with slash |
| 174 |
Result := EncodeURI(U); |
| 175 |
end; |
| 176 |
|
| 177 |
end. |