| 1 |
{ |
| 2 |
Copyright 2021-2021 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 |
{ Services (on Android and iOS) operations, |
| 17 |
to be done when actually generating project using them. } |
| 18 |
unit ToolServicesOperations; |
| 19 |
|
| 20 |
interface |
| 21 |
|
| 22 |
uses SysUtils, Generics.Collections, DOM, |
| 23 |
CastleUtils, CastleStringUtils, |
| 24 |
ToolServices, ToolProject; |
| 25 |
|
| 26 |
type |
| 27 |
TServiceManifest = class |
| 28 |
strict private |
| 29 |
FService: TService; |
| 30 |
public |
| 31 |
type |
| 32 |
TServiceCopy = class |
| 33 |
Source, Target: String; |
| 34 |
end; |
| 35 |
TServiceCopyList = specialize TObjectList<TServiceCopy>; |
| 36 |
var |
| 37 |
PackageOperations: TServiceCopyList; |
| 38 |
|
| 39 |
constructor Create(const AService: TService); |
| 40 |
destructor Destroy; override; |
| 41 |
procedure Load(const UrlServicePath: String); |
| 42 |
|
| 43 |
{ Call all package operations for this service. |
| 44 |
OutputPath is passed from PackageServices. } |
| 45 |
procedure Package(const Project: TCastleProject; const OutputPath: String); |
| 46 |
end; |
| 47 |
|
| 48 |
{ Call all package operations for all services, |
| 49 |
realizing the instructions from service CastleEngineService.xml. |
| 50 |
|
| 51 |
UrlServicesPath must be an absolute URL (may, but doesn't have to, end with /) |
| 52 |
pointing to the CGE read-only files of services. |
| 53 |
|
| 54 |
OutputPath must be an absolute path (may, but doesn't have to, end with PathDelim) |
| 55 |
pointing to the writeable files where service templates are unpacked now. } |
| 56 |
procedure PackageServices(const Project: TCastleProject; const Services: TServiceList; |
| 57 |
const UrlServicesPath, OutputPath: String); |
| 58 |
|
| 59 |
implementation |
| 60 |
|
| 61 |
uses Classes, XMLRead, |
| 62 |
CastleXmlUtils, CastleUriUtils, CastleFilesUtils, |
| 63 |
ToolUtils, ToolCommonUtils; |
| 64 |
|
| 65 |
{ TServiceManifest ----------------------------------------------------------- } |
| 66 |
|
| 67 |
constructor TServiceManifest.Create(const AService: TService); |
| 68 |
begin |
| 69 |
inherited Create; |
| 70 |
FService := AService; |
| 71 |
PackageOperations := TServiceCopyList.Create; |
| 72 |
end; |
| 73 |
|
| 74 |
destructor TServiceManifest.Destroy; |
| 75 |
begin |
| 76 |
FreeAndNil(PackageOperations); |
| 77 |
inherited; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TServiceManifest.Load(const UrlServicePath: String); |
| 81 |
var |
| 82 |
UrlServiceManifest: String; |
| 83 |
Doc: TXMLDocument; |
| 84 |
PackageElement: TDOMElement; |
| 85 |
I: TXMLElementIterator; |
| 86 |
CopyOperation: TServiceCopy; |
| 87 |
begin |
| 88 |
PackageOperations.Clear; |
| 89 |
|
| 90 |
UrlServiceManifest := CombineURI(URIIncludeSlash(UrlServicePath), 'CastleEngineService.xml'); |
| 91 |
if URIFileExists(UrlServiceManifest) then |
| 92 |
begin |
| 93 |
Doc := UrlReadXml(UrlServiceManifest); |
| 94 |
try |
| 95 |
if Doc.DocumentElement.TagName8 <> 'service' then |
| 96 |
raise Exception.CreateFmt('Root element of CastleEngineService.xml must be <service>, not "%s"', [ |
| 97 |
Doc.DocumentElement.TagName8 |
| 98 |
]); |
| 99 |
PackageElement := Doc.DocumentElement.Child('package', false); |
| 100 |
if PackageElement <> nil then |
| 101 |
begin |
| 102 |
I := PackageElement.ChildrenIterator; |
| 103 |
try |
| 104 |
while I.GetNext do |
| 105 |
begin |
| 106 |
if I.Current.TagName8 <> 'copy' then |
| 107 |
raise Exception.CreateFmt('Children of <package> must be <copy>, not "%s"', [ |
| 108 |
I.Current.TagName8 |
| 109 |
]); |
| 110 |
CopyOperation := TServiceCopy.Create; |
| 111 |
CopyOperation.Source := I.Current.AttributeString('source'); |
| 112 |
CopyOperation.Target := I.Current.AttributeString('target'); |
| 113 |
PackageOperations.Add(CopyOperation); |
| 114 |
end; |
| 115 |
finally FreeAndNil(I) end; |
| 116 |
end; |
| 117 |
finally FreeAndNil(Doc) end; |
| 118 |
end; |
| 119 |
end; |
| 120 |
|
| 121 |
procedure TServiceManifest.Package(const Project: TCastleProject; const OutputPath: String); |
| 122 |
var |
| 123 |
C: TServiceCopy; |
| 124 |
Source, Target: String; |
| 125 |
begin |
| 126 |
for C in PackageOperations do |
| 127 |
begin |
| 128 |
(* Copy.Source may be something like '${IOS.FMOD.LIBRARY_PATH}' and the macro |
| 129 |
may expand to an absolute path, or a path relative to the project. *) |
| 130 |
Source := CombinePaths(Project.Path, Project.ReplaceMacros(C.Source)); |
| 131 |
|
| 132 |
if IsPathAbsolute(C.Target) then |
| 133 |
raise Exception.CreateFmt('Target to copy package file in CastleEngineService.xml is an absolute path, it should be relative to output: "%s"', [ |
| 134 |
C.Target |
| 135 |
]); |
| 136 |
Target := CombinePaths(OutputPath, C.Target); |
| 137 |
if not IsPathAbsolute(Target) then |
| 138 |
raise Exception.CreateFmt('Target to copy package file is not an absolute path: "%s"', [ |
| 139 |
Target |
| 140 |
]); |
| 141 |
if RegularFileExists(Target) then |
| 142 |
raise Exception.CreateFmt('Service file "%s" already exists, specified by multiple services: %s', [ |
| 143 |
Target |
| 144 |
]); |
| 145 |
|
| 146 |
// all checks passed, do the actual file operations (create dir, copy file) |
| 147 |
SmartCopyFile(Source, Target); |
| 148 |
WritelnVerbose(Format('Packaging service %s: Copied %s => %s', [ |
| 149 |
FService.Name, |
| 150 |
Source, |
| 151 |
Target |
| 152 |
])); |
| 153 |
end; |
| 154 |
end; |
| 155 |
|
| 156 |
{ routines ------------------------------------------------------------------- } |
| 157 |
|
| 158 |
procedure PackageServices(const Project: TCastleProject; const Services: TServiceList; |
| 159 |
const UrlServicesPath, OutputPath: String); |
| 160 |
var |
| 161 |
Service: TService; |
| 162 |
ServiceManifest: TServiceManifest; |
| 163 |
begin |
| 164 |
for Service in Services do |
| 165 |
begin |
| 166 |
ServiceManifest := TServiceManifest.Create(Service); |
| 167 |
try |
| 168 |
ServiceManifest.Load(CombineURI(URIIncludeSlash(UrlServicesPath), Service.Name)); |
| 169 |
ServiceManifest.Package(Project, OutputPath); |
| 170 |
finally FreeAndNil(ServiceManifest) end; |
| 171 |
end; |
| 172 |
end; |
| 173 |
|
| 174 |
end. |