XE6 Publish file Add files to be published in Deployment Manager, Remote Path write
assets\internal\或
assets\就可以
其中
assets\internal\会把文件发布到
Tpath.getdocumentspath (aka/data/data/.../files) directory
assets\会把文件发布到
Tpath.getshareddocumentspath (aka/mnt/sdcard/android/data/.../files) directory
In addition, the System.startupcopy unit is modified, and the support setting assets\sdcard\xxx will publish the file to the/mnt/sdcard/xxx directory.
{ ******************************************************* }
{ }
{codegear Delphi Runtime Library}
{Copyright (c) 2013-2014 Embarcadero Technologies, Inc.}
{ }
{ ******************************************************* }
Instructions for use
In Deployment Manager, set assets\internal\, the file will be published to Tpath.getdocumentspath (i.e./data/data/.../files)
Setting assets\ will publish the file to the Tpath.getshareddocumentspath (i.e./mnt/sdcard/android/data/.../files) directory
Setting assets\sdcard\xxx will publish the file to the/mnt/sdcard/xxx directory
{$HPPEMIT Linkunit}
Unit system.startupcopy;
Interface
Uses
System.sysutils;
Type
Estartupcopyexception = Class (Exception);
Implementation
Uses
{$IFDEF ANDROID}
Androidapi.assetmanager,
Androidapi.nativeactivity,
Androidapi.ioutils,
POSIX.UNISTD,
System.rtlconsts,
Androidapi.externalsdcardpath,
{$ENDIF ANDROID}
{$IFDEF IOS}
Iosapi.foundation,
{$ENDIF}
System.ioutils;
{$IFDEF ANDROID}
Type
Tassets_type = (atexternal, atinternal, Atsdcard);
Const
Max_buff_len = 65536;
Assets_root = ' ASSETS ';
Assets_root_d = ' ASSETS ' + pathdelim;
Assets_root_d_length = LENGTH (assets_root_d);
assets_internal = ' INTERNAL ';
assets_internal_d = ' INTERNAL ' + pathdelim;
Assets_deploy_dir = ' Deployinfo ';
Assets_filename = ' deployedassets.txt ';
Assets_filename_path = assets_deploy_dir + Pathdelim + assets_filename;
Assets_sdcard = ' SDcard ';
Assets_sdcard_d = ' sdcard ' + pathdelim;
Assets_sdcard_d_length = LENGTH (assets_sdcard_d);
{$ENDIF ANDROID}
{$IFDEF ANDROID}
function Copyassettofile (Lassetmanager:paassetmanager;
Const Assetfolder, assetname:string; Const Destinationroot, Destfolder,
filename:string): Boolean;
Var
Origfilename, destFileName, destinationpath:string;
Readcount, Writecount:integer;
Lassetfile:paasset;
Filehandle:thandle;
Buffer:tbytes;
M:tmarshaller;
Begin
Result: = True;
If Assetfolder = "Then
Origfilename: = Assetname
Else
Origfilename: = Includetrailingpathdelimiter (assetfolder) + assetname;
If Destfolder <> ' then
Begin
DestinationPath: = Includetrailingpathdelimiter (destinationroot) + Destfolder;
destFileName: = Includetrailingpathdelimiter (destinationroot) +
Includetrailingpathdelimiter (Destfolder) + FileName;
End
Else
Begin
DestinationPath: = Destinationroot;
destFileName: = Includetrailingpathdelimiter (destinationroot) + FileName
End
If not fileexists (destfilename) then//does not overwrite files
Begin
Second Create an intermediate buffer.
SetLength (Buffer, Max_buff_len);
Lassetfile: = nil;
Try
If not directoryexists (DestinationPath) Then
Begin
If not forcedirectories (DestinationPath) Then
Begin
Exit (False);
End
End
We have a valid Assetmanager. Start
Lassetfile: = Aassetmanager_open (Lassetmanager, M.asutf8 (origfilename). ToPointer,
Aasset_mode_buffer);
If Lassetfile <> Nil Then
Begin
FileHandle: = Filecreate (destFileName);
Try
If FileHandle = Thandle ( -1) Then
Begin
Exit (False);
End
Repeat
Readcount: = Aasset_read (lassetfile, @Buffer [0], Max_buff_len);
Writecount: = FileWrite (FileHandle, Buffer, 0, Readcount);
Until (readcount <= 0) or (readcount <> writecount);
Finally
FileClose (FileHandle);
End
End
Else
Raise Estartupcopyexception.createfmt (Sassetfilenotfound, [origfilename]);
Finally
if (lassetfile <> nil) Then
Aasset_close (Lassetfile);
SetLength (Buffer, 0);
End
End
End
function Readassetsdeployfile (Assetmanager:paassetmanager;
var filecontent:string): Boolean;
Var
Buffer:array [0.. Max_buff_len-1] of Char;
Lassetfile:paasset;
Readcount:integer;
M:tmarshaller;
Begin
Result: = False;
Lassetfile: = Aassetmanager_open (Assetmanager, M.asutf8 (Assets_filename_path). ToPointer,
Aasset_mode_buffer);
If Assigned (lassetfile) Then
Begin
Try
Repeat
Readcount: = Aasset_read (Lassetfile, @Buffer, Max_buff_len);
If Readcount > 0 Then
Filecontent: = filecontent + utf8tostring (@Buffer);
Until (readcount <= 0);
Result: = True;
Finally
Aasset_close (Lassetfile);
End
End
End
Procedure Copyassetstofiles;
Var
Assetmanager:paassetmanager;
rootdir:string;
internalpath:string;
externalpath:string;
sdcardpath:string;
{$REGION ' Copyassetfolder '}
Procedure Copyassetfolder (const Lassetmanager:paassetmanager;
Const Fromfolder, tofolder:string; Isinternal:tassets_type = atexternal);
Var
Lassetdir:paassetdir;
lfile:marshaledastring;
filename:string;
M:tmarshaller;
Begin
Listing the files on root directory
Lassetdir: = Aassetmanager_opendir (Lassetmanager, M.asutf8 (Fromfolder). ToPointer);
If Lassetdir <> Nil Then
Begin
Try
Lfile: = Aassetdir_getnextfilename (Lassetdir);
While Lfile <> Nil do
Begin
FileName: = utf8tostring (lfile);
Case IsInternal of
Atinternal:
Begin
Copyassettofile (Lassetmanager, Fromfolder, FileName, Internalpath,
Tofolder, FileName);
End
Atsdcard:
Begin
If Sdcardpath = "Then
Raise Estartupcopyexception.create (sexternalextoragenotavailable);
Copyassettofile (Lassetmanager, Fromfolder, FileName, Sdcardpath, Tofolder,
FileName);
End
Else
Begin
If Externalpath = "Then
Raise Estartupcopyexception.create (sexternalextoragenotavailable);
Copyassettofile (Lassetmanager, Fromfolder, FileName, Externalpath, Tofolder,
FileName);
End
End
Lfile: = Aassetdir_getnextfilename (Lassetdir);
End
Finally
Aassetdir_close (Lassetdir);
End
End
End
{$ENDREGION}
{$REGION ' Copyassetfile '}
Procedure Copyassetfile (const Lassetmanager:paassetmanager;
Const FromFile, tofile:string; Isinternal:tassets_type = atexternal);
Var
filename:string;
fromfolder:string;
tofolder:string;
Begin
Fromfolder: = Extractfilepath (FromFile);
Tofolder: = Extractfilepath (ToFile);
FileName: = Extractfilename (FromFile);
Case IsInternal of
Atinternal:
Begin
Copyassettofile (Lassetmanager, Fromfolder, FileName, Internalpath,
Tofolder, FileName)
End
Atsdcard:
Begin
If Sdcardpath = "Then
Raise Estartupcopyexception.create (sexternalextoragenotavailable);
Copyassettofile (Lassetmanager, Fromfolder, FileName, Sdcardpath, Tofolder,
FileName);
End
Else
Begin
If Externalpath = "Then
Raise Estartupcopyexception.create (sexternalextoragenotavailable);
Copyassettofile (Lassetmanager, Fromfolder, FileName, Externalpath, Tofolder,
FileName);
End
End
End
{$ENDREGION}
{$REGION ' Processdeployedfiles '}
Procedure Processdeployedfiles (const Lassetmanager:paassetmanager; lfilesstr:string);
Var
I:integer;
filename:string;
afiles:tarray<string>;
Begin
Afiles: = Lfilesstr.split ([String (#13 # #)], excludeempty);
For I: = Low (Afiles) to High (afiles) does
Begin
FileName: = Afiles[i]. Replace (' \ ', '/'). Replace ('./', ');
If Filename.startswith (assets_root_d) Then
Begin
FileName: = filename.substring (assets_root_d_length);
If Filename.startswith (assets_internal_d) Then
Begin
Copyassetfile (Assetmanager, FileName,
Filename.substring (Length (assets_internal_d)), atinternal);
End
else if Filename.startswith (Assets_sdcard) Then
Begin
Copyassetfile (Assetmanager, FileName,
Filename.substring (assets_sdcard_d_length), Atsdcard);
End
Else
Begin
Copyassetfile (assetmanager, filename, filename, atexternal);
End
End
End
End
{$ENDREGION}
Var
deployedfiles:string;
Begin
Internalpath: = Getfilesdir;
Externalpath: = Getexternalfilesdir;
Sdcardpath: = Getexternalsdcardpath ();
Assetmanager: = Anativeactivity (system.delphiactivity^). Assetmanager;
if (Assetmanager <> nil) Then
Begin
If Readassetsdeployfile (Assetmanager, deployedfiles) Then
Processdeployedfiles (Assetmanager, Deployedfiles)
Else
Begin
RootDir: = ";
Copyassetfolder (Assetmanager, RootDir, RootDir, atexternal);
RootDir: = assets_internal;
Copyassetfolder (Assetmanager, RootDir, ", atinternal);
RootDir: = ' StartUp ';
Copyassetfolder (Assetmanager, RootDir, RootDir, atexternal);
RootDir: = Assets_sdcard;
Copyassetfolder (Assetmanager, RootDir, RootDir, Atsdcard);
End
End
End
Procedure Copystartupfiles;
Begin
Copyassetstofiles;
End
{$ELSE! ANDROID}
Procedure Copystartupfiles;
Var
Source, destination:string;
Procedure Docopyfiles (const src:string; const dst:string);
Var
Searchrec:tsearchrec;
Res:integer;
Begin
Res: = FindFirst (SRC + ' * ', faanyfile, Searchrec);
While Res = 0 do
Begin
if (searchrec.attr and fadirectory) = Fadirectory Then
Begin
if (Searchrec.name <> ') and (Searchrec.name <> ' ... ') then
Begin
If Forcedirectories (Dst + searchrec.name) Then
Do the recurse thing ...
Docopyfiles (SRC + searchrec.name + Pathdelim,
Dst + searchrec.name + pathdelim);
End
End
Else
Begin
If not fileexists (Dst + searchrec.name) Then
Begin
Tfile.copy (SRC + searchrec.name, Dst + searchrec.name, False);
Copy without overwriting.
End
End
Res: = FindNext (SEARCHREC);
End
End
{$IFDEF IOS}
Var
Bundle:nsbundle;
{$ENDIF}
Begin
{$IFDEF IOS}
Bundle: = Tnsbundle.wrap (TNSBundle.OCClass.mainBundle);
Source: = utf8tostring (Bundle.bundlePath.UTF8String) + Pathdelim + ' StartUp ' +
Pathdelim;
{$ELSE}
Source: = Extractfilepath (paramstr (0)) + ' StartUp ' + pathdelim;
{$ENDIF}
If DirectoryExists (Source) Then
Begin
Destination: = Gethomepath + Pathdelim;
Docopyfiles (Source, Destination);
End
End
{$ENDIF ANDROID}
Initialization
Begin
Copystartupfiles;
End
End.
XE6 Publish file Add files to be published in Deployment Manager, Remote path write assets\internal\ or assets\ can