| 1 |
{ |
| 2 |
Copyright 2018-2019 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 CastleSettings.xml ( https://castle-engine.io/manual_castle_settings.php ). } |
| 17 |
unit CastleInternalSettings; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses SysUtils, Classes, Generics.Collections, DOM, Contnrs, |
| 24 |
CastleUtils, CastleClassUtils, CastleFonts, CastleRectangles, CastleTimeUtils, |
| 25 |
CastleUIControls; |
| 26 |
|
| 27 |
type |
| 28 |
TWarmupCache = class; |
| 29 |
TWarmupCacheFormat = class; |
| 30 |
|
| 31 |
EInvalidSettingsXml = class(Exception); |
| 32 |
|
| 33 |
TWarmupCacheFormatEvent = procedure (const Cache: TWarmupCache; |
| 34 |
const Element: TDOMElement; const ElementBaseUrl: String) of object; |
| 35 |
|
| 36 |
{ Anything that can be preloaded into the TWarmupCache. |
| 37 |
E.g. a texture, sound, model. } |
| 38 |
TWarmupCacheFormat = class |
| 39 |
{ XML element (in CastleSettings.xml) name indicating this format. } |
| 40 |
Name: String; |
| 41 |
Event: TWarmupCacheFormatEvent; |
| 42 |
end; |
| 43 |
|
| 44 |
TWarmupCacheFormatList = class({$ifdef FPC}specialize{$endif} TObjectList<TWarmupCacheFormat>) |
| 45 |
private |
| 46 |
function CallRegisteredFormat(const Cache: TWarmupCache; |
| 47 |
const Element: TDOMElement; const ElementBaseUrl: String): Boolean; |
| 48 |
public |
| 49 |
procedure RegisterFormat(const Name: String; const Event: TWarmupCacheFormatEvent); |
| 50 |
end; |
| 51 |
|
| 52 |
{ Used by SettingsLoad, an instance of this will be created and owner by container. |
| 53 |
In the future this may be available publicly (in non-internal unit) to have |
| 54 |
"warmpup cache" available for any period of time (right now, it is only for the |
| 55 |
lifetime of the container). } |
| 56 |
TWarmupCache = class(TComponent) |
| 57 |
strict private |
| 58 |
FOwnedObjects: TObjectList; |
| 59 |
private |
| 60 |
procedure ReadElement(const Element: TDOMElement; const ElementBaseUrl: String); |
| 61 |
public |
| 62 |
constructor Create(AOwner: TComponent); override; |
| 63 |
destructor Destroy; override; |
| 64 |
|
| 65 |
{ General-purpose container for objects that should be owned by this |
| 66 |
cache. May be used by TWarmupCacheFormatEvent implementation, |
| 67 |
if you create something that should be owned by cache. } |
| 68 |
property OwnedObjects: TObjectList read FOwnedObjects; |
| 69 |
end; |
| 70 |
|
| 71 |
{ Register new TWarmupCacheFormat using @code(WarmupCacheFormats.RegisterFormat). } |
| 72 |
function WarmupCacheFormats: TWarmupCacheFormatList; |
| 73 |
|
| 74 |
{ Load CastleSettings.xml ( https://castle-engine.io/manual_castle_settings.php ) |
| 75 |
into a container. |
| 76 |
This also creates a TWarmupCache that is owned by the container. } |
| 77 |
procedure SettingsLoad(const Container: TCastleContainer; const SettingsUrl: String); |
| 78 |
|
| 79 |
implementation |
| 80 |
|
| 81 |
uses Math, TypInfo, |
| 82 |
CastleLog, CastleXmlUtils, CastleStringUtils, CastleGLImages, |
| 83 |
CastleUnicode, CastleUriUtils{$ifdef FPC}, CastleLocalizationGetText{$endif}; |
| 84 |
|
| 85 |
{ TWarmupCacheFormatList ----------------------------------------------------- } |
| 86 |
|
| 87 |
function TWarmupCacheFormatList.CallRegisteredFormat(const Cache: TWarmupCache; |
| 88 |
const Element: TDOMElement; const ElementBaseUrl: String): Boolean; |
| 89 |
var |
| 90 |
Format: TWarmupCacheFormat; |
| 91 |
begin |
| 92 |
for Format in Self do |
| 93 |
if Format.Name = Element.TagName8 then |
| 94 |
begin |
| 95 |
Format.Event(Cache, Element, ElementBaseUrl); |
| 96 |
Exit(true); |
| 97 |
end; |
| 98 |
Result := false; |
| 99 |
end; |
| 100 |
|
| 101 |
procedure TWarmupCacheFormatList.RegisterFormat(const Name: String; |
| 102 |
const Event: TWarmupCacheFormatEvent); |
| 103 |
var |
| 104 |
Format: TWarmupCacheFormat; |
| 105 |
begin |
| 106 |
Assert(Assigned(Event)); |
| 107 |
|
| 108 |
Format := TWarmupCacheFormat.Create; |
| 109 |
Add(Format); |
| 110 |
Format.Name := Name; |
| 111 |
Format.Event := Event; |
| 112 |
end; |
| 113 |
|
| 114 |
{ TImageUiCache -------------------------------------------------------------- } |
| 115 |
|
| 116 |
type |
| 117 |
TImageUiCache = class |
| 118 |
class procedure Event(const Cache: TWarmupCache; |
| 119 |
const Element: TDOMElement; const ElementBaseUrl: String); |
| 120 |
end; |
| 121 |
|
| 122 |
class procedure TImageUiCache.Event(const Cache: TWarmupCache; |
| 123 |
const Element: TDOMElement; const ElementBaseUrl: String); |
| 124 |
var |
| 125 |
URL: String; |
| 126 |
Image: TCastleImagePersistent; |
| 127 |
begin |
| 128 |
URL := Element.AttributeURL('url', ElementBaseUrl); |
| 129 |
Image := TCastleImagePersistent.Create; |
| 130 |
Cache.OwnedObjects.Add(Image); |
| 131 |
Image.URL := URL; // loads the image |
| 132 |
end; |
| 133 |
|
| 134 |
{ TWarmupCache --------------------------------------------------------------- } |
| 135 |
|
| 136 |
constructor TWarmupCache.Create(AOwner: TComponent); |
| 137 |
begin |
| 138 |
inherited; |
| 139 |
FOwnedObjects := TObjectList.Create(true); |
| 140 |
end; |
| 141 |
|
| 142 |
destructor TWarmupCache.Destroy; |
| 143 |
begin |
| 144 |
FreeAndNil(FOwnedObjects); |
| 145 |
inherited; |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TWarmupCache.ReadElement(const Element: TDOMElement; const ElementBaseUrl: String); |
| 149 |
begin |
| 150 |
if not WarmupCacheFormats.CallRegisteredFormat(Self, Element, ElementBaseUrl) then |
| 151 |
raise EInvalidSettingsXml.CreateFmt('Not recognized warmup cache element "%s"', |
| 152 |
[Element.TagName8]); |
| 153 |
end; |
| 154 |
|
| 155 |
{ globals -------------------------------------------------------------------- } |
| 156 |
|
| 157 |
var |
| 158 |
FWarmupCacheFormats: TWarmupCacheFormatList; |
| 159 |
|
| 160 |
function WarmupCacheFormats: TWarmupCacheFormatList; |
| 161 |
begin |
| 162 |
if FWarmupCacheFormats = nil then |
| 163 |
begin |
| 164 |
FWarmupCacheFormats := TWarmupCacheFormatList.Create(true); |
| 165 |
// register formats implemented in this unit |
| 166 |
FWarmupCacheFormats.RegisterFormat('image_ui', {$ifdef FPC}@{$endif}TImageUiCache{$ifdef FPC}(nil){$endif}.Event); |
| 167 |
end; |
| 168 |
Result := FWarmupCacheFormats; |
| 169 |
end; |
| 170 |
|
| 171 |
procedure SettingsLoad(const Container: TCastleContainer; const SettingsUrl: String); |
| 172 |
|
| 173 |
function UIScalingToString(const UIScaling: TUIScaling): String; |
| 174 |
begin |
| 175 |
Result := SEnding(GetEnumName(TypeInfo(TUIScaling), Ord(UIScaling)), 3); |
| 176 |
end; |
| 177 |
|
| 178 |
function UIScalingFromString(const S: String): TUIScaling; |
| 179 |
begin |
| 180 |
for Result := Low(TUIScaling) to High(TUIScaling) do |
| 181 |
if S = UIScalingToString(Result) then |
| 182 |
Exit; |
| 183 |
raise EInvalidSettingsXml.CreateFmt('Not a valid value for UIScaling: %s', [S]); |
| 184 |
end; |
| 185 |
|
| 186 |
type |
| 187 |
TDynIntegerArray = array of Integer; |
| 188 |
|
| 189 |
function ParseIntegerList(const S: String): TDynIntegerArray; |
| 190 |
var |
| 191 |
IntegerList: TIntegerList; |
| 192 |
SeekPos: Integer; |
| 193 |
Token: String; |
| 194 |
begin |
| 195 |
IntegerList := TIntegerList.Create; |
| 196 |
try |
| 197 |
SeekPos := 1; |
| 198 |
repeat |
| 199 |
Token := NextToken(S, SeekPos); |
| 200 |
if Token = '' then Break; |
| 201 |
IntegerList.Add(StrToInt(Token)); |
| 202 |
until false; |
| 203 |
|
| 204 |
if IntegerList.Count = 0 then |
| 205 |
raise EInvalidSettingsXml.Create('sizes_at_load parameter is an empty list in CastleSettings.xml'); |
| 206 |
|
| 207 |
Result := TDynIntegerArray(IntegerList.ToArray); |
| 208 |
finally FreeAndNil(IntegerList) end; |
| 209 |
end; |
| 210 |
|
| 211 |
procedure ReadWarmupCache(E: TDOMElement); |
| 212 |
var |
| 213 |
Cache: TWarmupCache; |
| 214 |
I: TXMLElementIterator; |
| 215 |
TimeStart: TCastleProfilerTime; |
| 216 |
begin |
| 217 |
TimeStart := Profiler.Start('Warming up cache (following CastleSettings.xml contents)'); |
| 218 |
try |
| 219 |
Cache := TWarmupCache.Create(Container); |
| 220 |
I := E.ChildrenIterator; |
| 221 |
try |
| 222 |
while I.GetNext do |
| 223 |
Cache.ReadElement(I.Current, SettingsUrl); |
| 224 |
finally FreeAndNil(I) end; |
| 225 |
finally Profiler.Stop(TimeStart) end; |
| 226 |
end; |
| 227 |
|
| 228 |
function LoadFontSettings(const FontElement: TDOMElement): TCastleAbstractFont; |
| 229 |
var |
| 230 |
NewFontUrl: String; |
| 231 |
NewFontSize, NewFontOptimalSize: Cardinal; |
| 232 |
NewFontAntiAliased: Boolean; |
| 233 |
AllSizesAtLoadStr: String; |
| 234 |
AllSizesAtLoad: TDynIntegerArray; |
| 235 |
UnicodeCharList: TUnicodeCharList; |
| 236 |
RawString: String; |
| 237 |
S: String; |
| 238 |
StringList: TCastleStringList; |
| 239 |
begin |
| 240 |
if FontElement <> nil then |
| 241 |
begin |
| 242 |
NewFontUrl := FontElement.AttributeURL('url', SettingsUrl); |
| 243 |
NewFontSize := FontElement.AttributeCardinalDef('size', 20); |
| 244 |
NewFontAntiAliased := FontElement.AttributeBooleanDef('anti_aliased', true); |
| 245 |
|
| 246 |
{ Load additional character list for the font } |
| 247 |
UnicodeCharList := TUnicodeCharList.Create; |
| 248 |
{ providing only_sample_text="true" will force loading only characters |
| 249 |
explicitly specified, e.g. by sample_text="..." field or other means } |
| 250 |
if not FontElement.AttributeBooleanDef('only_sample_text', false) then |
| 251 |
UnicodeCharList.Add(SimpleAsciiCharacters); |
| 252 |
UnicodeCharList.Add(FontElement.AttributeStringDef('sample_text', '')); |
| 253 |
{ Load characters from one or several translation files } |
| 254 |
// TODO: Delphi Localization |
| 255 |
{$ifdef FPC} |
| 256 |
RawString := FontElement.AttributeStringDef('sample_get_text_mo', ''); |
| 257 |
if RawString <> '' then |
| 258 |
begin |
| 259 |
StringList := CreateTokens(RawString, WhiteSpaces + [',']); |
| 260 |
for S in StringList do |
| 261 |
AddTranslatedCharacters(CombineURI(SettingsUrl, S), UnicodeCharList); |
| 262 |
FreeAndNil(StringList); |
| 263 |
end; |
| 264 |
{$endif} |
| 265 |
{ Loads a comma/whitespace separated list of UTF8 characters decimal code } |
| 266 |
RawString := FontElement.AttributeStringDef('sample_code', ''); |
| 267 |
if RawString <> '' then |
| 268 |
begin |
| 269 |
StringList := CreateTokens(RawString, WhiteSpaces + [',']); |
| 270 |
for S in StringList do |
| 271 |
UnicodeCharList.Add(StrToInt(S)); |
| 272 |
FreeAndNil(StringList); |
| 273 |
end; |
| 274 |
|
| 275 |
if UnicodeCharList.Count = 0 then |
| 276 |
raise EInvalidSettingsXml.Create('No characters were loaded for font at ' + NewFontUrl); |
| 277 |
|
| 278 |
if FontElement.AttributeString('sizes_at_load', AllSizesAtLoadStr) then |
| 279 |
begin |
| 280 |
AllSizesAtLoad := ParseIntegerList(AllSizesAtLoadStr); |
| 281 |
Result := TCustomizedFont.Create(Container); |
| 282 |
TCustomizedFont(Result).Load(NewFontUrl, AllSizesAtLoad, NewFontAntiAliased, UnicodeCharList); |
| 283 |
end else |
| 284 |
begin |
| 285 |
NewFontOptimalSize := FontElement.AttributeCardinalDef('size_at_load', NewFontSize); |
| 286 |
Result := TCastleFont.Create(Container); |
| 287 |
TCastleFont(Result).OptimalSize := NewFontOptimalSize; |
| 288 |
TCastleFont(Result).AntiAliased := NewFontAntiAliased; |
| 289 |
TCastleFont(Result).LoadBasicCharacters := false; // if needed, they are included in UnicodeCharList |
| 290 |
TCastleFont(Result).LoadCharacters := UnicodeCharList.ToString; |
| 291 |
TCastleFont(Result).URL := NewFontUrl; |
| 292 |
end; |
| 293 |
Result.Size := NewFontSize; |
| 294 |
FreeAndNil(UnicodeCharList); |
| 295 |
end else |
| 296 |
Result := nil; |
| 297 |
end; |
| 298 |
|
| 299 |
const |
| 300 |
DefaultUIScaling = usNone; |
| 301 |
DefaultUIReferenceWidth = 0; |
| 302 |
DefaultUIReferenceHeight = 0; |
| 303 |
var |
| 304 |
SettingsDoc: TXMLDocument; |
| 305 |
E: TDOMElement; |
| 306 |
|
| 307 |
NewDefaultFont: TCastleAbstractFont; |
| 308 |
NewFontFamily: TCastleFontFamily; |
| 309 |
|
| 310 |
NewUIScaling: TUIScaling; |
| 311 |
NewUIReferenceWidth, NewUIReferenceHeight: Single; |
| 312 |
begin |
| 313 |
// initialize defaults |
| 314 |
NewUIScaling := DefaultUIScaling; |
| 315 |
NewUIReferenceWidth := DefaultUIReferenceWidth; |
| 316 |
NewUIReferenceHeight := DefaultUIReferenceHeight; |
| 317 |
NewDefaultFont := nil; |
| 318 |
|
| 319 |
SettingsDoc := URLReadXML(SettingsUrl); |
| 320 |
try |
| 321 |
if SettingsDoc.DocumentElement.TagName8 <> 'castle_settings' then |
| 322 |
raise EInvalidSettingsXml.Create('The root element must be <castle_settings>'); |
| 323 |
|
| 324 |
E := SettingsDoc.DocumentElement.Child('ui_scaling', false); |
| 325 |
if E <> nil then |
| 326 |
begin |
| 327 |
NewUIScaling := UIScalingFromString( |
| 328 |
E.AttributeStringDef('mode', UIScalingToString(DefaultUIScaling))); |
| 329 |
NewUIReferenceWidth := |
| 330 |
E.AttributeSingleDef('reference_width', DefaultUIReferenceWidth); |
| 331 |
NewUIReferenceHeight := |
| 332 |
E.AttributeSingleDef('reference_height', DefaultUIReferenceHeight); |
| 333 |
end; |
| 334 |
|
| 335 |
E := SettingsDoc.DocumentElement.Child('default_font', false); |
| 336 |
if E <> nil then |
| 337 |
begin |
| 338 |
if E.HasAttribute('url') then |
| 339 |
NewDefaultFont := LoadFontSettings(E) |
| 340 |
else |
| 341 |
begin |
| 342 |
NewFontFamily := TCastleFontFamily.Create(Container); |
| 343 |
{ Don't assign name, to enable users to call LoadSettings |
| 344 |
multiple times on the same container -- the names shouldn't clash. } |
| 345 |
// NewFontFamily.Name := 'CastleInternalDefaultFontFamily'; |
| 346 |
NewFontFamily.Regular := LoadFontSettings(E.Child('regular', false)); |
| 347 |
NewFontFamily.Bold := LoadFontSettings(E.Child('bold', false)); |
| 348 |
NewFontFamily.Italic := LoadFontSettings(E.Child('italic', false)); |
| 349 |
NewFontFamily.BoldItalic := LoadFontSettings(E.Child('bold_italic', false)); |
| 350 |
if NewFontFamily.Regular = nil then |
| 351 |
raise EInvalidSettingsXml.Create('The <default_font> specified in CastleSettings.xml does not have "url" attribute (for simple fonts, like TCastleFont, that are not TCastleFontFamily) nor a <regular> variant (for TCastleFontFamily fonts)'); |
| 352 |
NewDefaultFont := NewFontFamily; |
| 353 |
end; |
| 354 |
end; |
| 355 |
|
| 356 |
E := SettingsDoc.DocumentElement.Child('warmup_cache', false); |
| 357 |
if E <> nil then |
| 358 |
ReadWarmupCache(E); |
| 359 |
finally FreeAndNil(SettingsDoc) end; |
| 360 |
|
| 361 |
Container.UIScaling := NewUIScaling; |
| 362 |
Container.UIReferenceWidth := NewUIReferenceWidth; |
| 363 |
Container.UIReferenceHeight := NewUIReferenceHeight; |
| 364 |
Container.DefaultFont := NewDefaultFont; |
| 365 |
end; |
| 366 |
|
| 367 |
{$ifndef FPC}initialization{$endif} |
| 368 |
|
| 369 |
finalization |
| 370 |
FreeAndNil(FWarmupCacheFormats); |
| 371 |
end. |