| 1 |
{ |
| 2 |
Copyright 2021-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 |
{ RTTI utilities. } |
| 17 |
unit CastleInternalRttiUtils; |
| 18 |
|
| 19 |
{$I castleconf.inc} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses TypInfo; |
| 24 |
|
| 25 |
{ Get the property name and value as Strings (e.g. to display in Object Inspector). } |
| 26 |
function PropertyGet(const PropObject: TObject; const PropInfo: PPropInfo; |
| 27 |
out Name, Value: String): Boolean; |
| 28 |
|
| 29 |
{ Does the property have a default value now. |
| 30 |
This is 100% reliable to use to avoid serializing things that have default value. |
| 31 |
Their state is equal to the state when object is created. |
| 32 |
|
| 33 |
Note that this is not a complete check to determine |
| 34 |
should the property be serialized or not. |
| 35 |
You should also check @code(IsStoredProp(PropObject, PropInfo)) |
| 36 |
in case the property has a "stored" method. |
| 37 |
IsStoredProp works in all cases (whether "stored" indicates a constant, |
| 38 |
field or some method) and it is @true if there was no "stored" value. |
| 39 |
So the full check is |
| 40 |
|
| 41 |
@longCode(# |
| 42 |
PropertyHasDefaultValue(PropObject, PropInfo) and |
| 43 |
IsStoredProp(PropObject, PropInfo) |
| 44 |
#) |
| 45 |
|
| 46 |
@param(TreatSubComponentAsDefault |
| 47 |
Determines how to treat subcomponents, |
| 48 |
i.e. instances with csSubComponent placed in properties |
| 49 |
(with the exception of subcomponents with |
| 50 |
TCastleComponent.ValueIsStreamed overridden, like TCastleVectorXxPersistent). |
| 51 |
|
| 52 |
The idea of subcomponent is that these subcomponents are automatically |
| 53 |
created and owned by parent. So their existence is by definition |
| 54 |
"default state". But their contents may have |
| 55 |
been modified compared to a default state. |
| 56 |
|
| 57 |
By default (when TreatSubComponentAsDefault is @false) |
| 58 |
we return @false for such subcomponents. |
| 59 |
In case of TCastleComponent, this means we return "not PropObject.ValueIsStreamed". |
| 60 |
This is safer and better for serialization system: |
| 61 |
serialization should store the contents of such subcomponent. |
| 62 |
|
| 63 |
When TreatSubComponentAsDefault is @true, |
| 64 |
we return @true for them. Even though their contents may be non-default. |
| 65 |
This is useful for object inspector display, |
| 66 |
that may display subcomponents expanded, |
| 67 |
so particular subcomponent's non-default properties would be emphasized anyway. |
| 68 |
|
| 69 |
Note: LCL object inspector avoids the need for this by |
| 70 |
implementing TClassPropertyEditor.ValueIsStreamed that scans |
| 71 |
all property editors in the property of subcomponent. |
| 72 |
|
| 73 |
Note that TreatSubComponentAsDefault doesn't matter |
| 74 |
when PropObject is TCastleComponent with |
| 75 |
TCastleComponent.ValueIsStreamed overridden. |
| 76 |
In this case we always return "not PropObject.ValueIsStreamed", |
| 77 |
regardless of TreatSubComponentAsDefault value. |
| 78 |
This means TreatSubComponentAsDefault doesn't affect display |
| 79 |
of components like TCastleVectorXxPersistent. |
| 80 |
) } |
| 81 |
function PropertyHasDefaultValue(const PropObject: TObject; |
| 82 |
const PropInfo: PPropInfo; const TreatSubComponentAsDefault: Boolean = false): Boolean; |
| 83 |
|
| 84 |
{ Is property of type Boolean (and any similar type, like LongBool, |
| 85 |
that for user presentation is also just a boolean, |
| 86 |
i.e. 2 values shown as checkbox). } |
| 87 |
function PropertyIsBool(const PropInfo: PPropInfo): Boolean; |
| 88 |
|
| 89 |
{ Get a property of type Boolean. |
| 90 |
Use only when PropertyIsBool is @true, undefined what happens otherwise. } |
| 91 |
function PropertyBoolGet(const PropObject: TObject; const PropInfo: PPropInfo): Boolean; |
| 92 |
|
| 93 |
{ Set a property of type Boolean. |
| 94 |
Use only when PropertyIsBool is @true, undefined what happens otherwise. } |
| 95 |
procedure PropertyBoolSet(const PropObject: TObject; const PropInfo: PPropInfo; const NewValue: Boolean); |
| 96 |
|
| 97 |
implementation |
| 98 |
|
| 99 |
uses SysUtils, RtlConsts, Classes, |
| 100 |
CastleUtils, CastleStringUtils, CastleClassUtils; |
| 101 |
|
| 102 |
{$ifndef FPC} |
| 103 |
resourcestring |
| 104 |
SErrNoVariantSupport = 'No variant support for properties. Please use the variants unit in your project and recompile'; |
| 105 |
{$endif} |
| 106 |
|
| 107 |
var |
| 108 |
InternalComponent: TCastleComponent; |
| 109 |
|
| 110 |
{ C is TCastleComponent and it overriddes TCastleComponent.ValueIsStreamed. |
| 111 |
|
| 112 |
Note: In general checking for such "is this method overridden" is dirty, |
| 113 |
as not expected by developers. But in this case it's used only for internal |
| 114 |
functionality in this unit, and only for display of properties |
| 115 |
in CGE inspector. |
| 116 |
|
| 117 |
For now this is OK. } |
| 118 |
function CustomizedValueIsStreamed(const C: TObject): Boolean; |
| 119 |
type |
| 120 |
TValueIsStreamed = function: Boolean of object; |
| 121 |
var |
| 122 |
CurMethod, NotOverriddedMethod: TValueIsStreamed; |
| 123 |
begin |
| 124 |
Result := false; |
| 125 |
if C is TCastleComponent then |
| 126 |
begin |
| 127 |
CurMethod := {$ifdef FPC}@{$endif} TCastleComponent(C).ValueIsStreamed; |
| 128 |
NotOverriddedMethod := {$ifdef FPC}@{$endif} InternalComponent.ValueIsStreamed; |
| 129 |
Result := TMethod(CurMethod).Code <> TMethod(NotOverriddedMethod).Code; |
| 130 |
end; |
| 131 |
end; |
| 132 |
|
| 133 |
function PropertyGet(const PropObject: TObject; const PropInfo: PPropInfo; out Name, Value: String): Boolean; |
| 134 |
|
| 135 |
function ObjectToString(const O: TObject): String; |
| 136 |
begin |
| 137 |
if O = nil then |
| 138 |
Result := 'nil' |
| 139 |
else |
| 140 |
if CustomizedValueIsStreamed(O) then |
| 141 |
begin |
| 142 |
{ We rely here on conventions about objects that implement ValueIsStreamed: |
| 143 |
- Their Name may end with "Persistent" suffix which we want to hide, |
| 144 |
to show in inspector e.g. 'Translation' instead of 'TranslationPersistent'. |
| 145 |
- They implement ToString to show something useful. } |
| 146 |
Result := O.ToString; |
| 147 |
Name := SuffixRemove('persistent', Name, true); |
| 148 |
end else |
| 149 |
if O is TComponent then |
| 150 |
Result := TComponent(O).Name + ': ' + O.ClassName |
| 151 |
else |
| 152 |
Result := O.ClassName; |
| 153 |
end; |
| 154 |
|
| 155 |
var |
| 156 |
PropType: PTypeInfo; |
| 157 |
begin |
| 158 |
Name := PropInfo^.Name; |
| 159 |
PropType := PropInfo^.PropType{$ifndef FPC}^{$endif}; |
| 160 |
|
| 161 |
Result := true; |
| 162 |
case PropType^.Kind of |
| 163 |
tkInteger: |
| 164 |
Value := IntToStr(GetOrdProp(PropObject, PropInfo)); |
| 165 |
tkEnumeration: |
| 166 |
Value := GetEnumName(PropType, GetOrdProp(PropObject, PropInfo)); |
| 167 |
{$ifndef FPUNONE} |
| 168 |
tkFloat: |
| 169 |
Value := FloatToStrDot(GetFloatProp(PropObject, PropInfo)); |
| 170 |
{$endif} |
| 171 |
//tkSet: TODO |
| 172 |
tkChar: |
| 173 |
Value := Char(GetOrdProp(PropObject, PropInfo)); |
| 174 |
{$ifdef FPC} |
| 175 |
tkSString, tkLString, tkAString: |
| 176 |
{$else} |
| 177 |
tkString, tkLString: |
| 178 |
{$endif} |
| 179 |
Value := GetStrProp(PropObject, PropInfo); |
| 180 |
tkWString: |
| 181 |
Value := UTF8Encode(GetWideStrProp(PropObject, PropInfo)); |
| 182 |
tkVariant: |
| 183 |
Value := GetVariantProp(PropObject, PropInfo); |
| 184 |
tkClass: |
| 185 |
Value := ObjectToString(GetObjectProp(PropObject, PropInfo)); |
| 186 |
tkWChar: |
| 187 |
Value := UTF8Encode(WideChar(GetOrdProp(PropObject, PropInfo))); |
| 188 |
{$ifdef FPC} |
| 189 |
tkBool: |
| 190 |
Value := BoolToStr(GetOrdProp(PropObject, PropInfo) <> 0, true); |
| 191 |
{$endif} |
| 192 |
tkInt64: |
| 193 |
Value := IntToStr(GetOrdProp(PropObject, PropInfo)); |
| 194 |
{$ifdef FPC} |
| 195 |
tkQWord: |
| 196 |
Value := IntToStr(GetOrdProp(PropObject, PropInfo)); |
| 197 |
{$endif} |
| 198 |
//tkObject: |
| 199 |
// tkArray, |
| 200 |
// tkRecord, |
| 201 |
// tkInterface, |
| 202 |
// tkDynArray, |
| 203 |
// tkInterfaceRaw, |
| 204 |
// tkProcVar, |
| 205 |
// tkMethod: |
| 206 |
tkUString : |
| 207 |
Value := UTF8Encode(GetWideStrProp(PropObject, PropInfo)); |
| 208 |
{$ifdef FPC} |
| 209 |
tkUChar: |
| 210 |
Value := UTF8Encode(UnicodeChar(GetOrdProp(PropObject, PropInfo))); |
| 211 |
{$endif} |
| 212 |
else |
| 213 |
Result := false; |
| 214 |
end; |
| 215 |
end; |
| 216 |
|
| 217 |
function PropertyHasDefaultValue(const PropObject: TObject; |
| 218 |
const PropInfo: PPropInfo; const TreatSubComponentAsDefault: Boolean): Boolean; |
| 219 |
|
| 220 |
{ Implemented looking closely at what standard FPC writer does, |
| 221 |
3.0.4/fpcsrc/rtl/objpas/classes/writer.inc , |
| 222 |
and simplified a lot for CGE purposes. } |
| 223 |
|
| 224 |
var |
| 225 |
PropType: PTypeInfo; |
| 226 |
Value, DefValue: Int32; |
| 227 |
{$ifndef FPUNONE} |
| 228 |
FloatValue, DefFloatValue: Extended; |
| 229 |
{$endif} |
| 230 |
MethodValue, DefMethodValue: TMethod; |
| 231 |
VarValue, DefVarValue: tvardata; |
| 232 |
BoolValue, DefBoolValue, DefValueUse: Boolean; |
| 233 |
C: TObject; |
| 234 |
begin |
| 235 |
Result := false; // for unknown types, assume false |
| 236 |
|
| 237 |
PropType := PropInfo^.PropType{$ifndef FPC}^{$endif}; |
| 238 |
DefValue := PropInfo^.Default; |
| 239 |
|
| 240 |
{ $80000000 means that there's no default value (in case of Single or String |
| 241 |
or Int64, you need to specify it by "nodefault") } |
| 242 |
DefValueUse := DefValue <> Int32($80000000); |
| 243 |
if not DefValueUse then |
| 244 |
Exit; |
| 245 |
|
| 246 |
case PropType^.Kind of |
| 247 |
tkInteger, tkChar, tkEnumeration, tkSet, tkWChar: |
| 248 |
begin |
| 249 |
Value := GetOrdProp(PropObject, PropInfo); |
| 250 |
Result := Value = DefValue; |
| 251 |
end; |
| 252 |
{$ifndef FPUNONE} |
| 253 |
tkFloat: |
| 254 |
begin |
| 255 |
FloatValue := GetFloatProp(PropObject, PropInfo); |
| 256 |
DefFloatValue := PSingle(@PropInfo^.Default)^; |
| 257 |
Result := FloatValue = DefFloatValue; |
| 258 |
end; |
| 259 |
{$endif} |
| 260 |
tkMethod: |
| 261 |
begin |
| 262 |
MethodValue := GetMethodProp(PropObject, PropInfo); |
| 263 |
DefMethodValue.Data := nil; |
| 264 |
DefMethodValue.Code := nil; |
| 265 |
Result := SameMethods(MethodValue, DefMethodValue); |
| 266 |
end; |
| 267 |
{$ifdef FPC} |
| 268 |
tkSString, tkLString, tkAString: |
| 269 |
begin |
| 270 |
Result := GetStrProp(PropObject, PropInfo) = ''; |
| 271 |
end; |
| 272 |
{$else} |
| 273 |
tkString, tkLString: |
| 274 |
begin |
| 275 |
Result := GetAnsiStrProp(PropObject, PropInfo) = ''; |
| 276 |
end; |
| 277 |
{$endif} |
| 278 |
tkWString: |
| 279 |
begin |
| 280 |
Result := GetWideStrProp(PropObject, PropInfo) = ''; |
| 281 |
end; |
| 282 |
tkUString: |
| 283 |
begin |
| 284 |
Result := GetUnicodeStrProp(PropObject, PropInfo) = ''; |
| 285 |
end; |
| 286 |
tkVariant: |
| 287 |
begin |
| 288 |
{ Ensure that a Variant manager is installed } |
| 289 |
if not Assigned(VarClearProc) then |
| 290 |
raise EWriteError.Create(SErrNoVariantSupport); |
| 291 |
VarValue := tvardata(GetVariantProp(PropObject, PropInfo)); |
| 292 |
FillChar(DefVarValue,sizeof(DefVarValue),0); |
| 293 |
{$ifdef FPC} |
| 294 |
Result := CompareByte(VarValue,DefVarValue,sizeof(VarValue)) = 0; |
| 295 |
{$else} |
| 296 |
Result := CompareMem(@VarValue, @DefVarValue, sizeof(VarValue)); |
| 297 |
{$endif} |
| 298 |
end; |
| 299 |
tkClass: |
| 300 |
begin |
| 301 |
C := GetObjectProp(PropObject, PropInfo); |
| 302 |
Result := |
| 303 |
(C = nil) or |
| 304 |
( |
| 305 |
{ When TreatSubComponentAsDefault: |
| 306 |
Treat subcomponents |
| 307 |
(except the ones overriding TCastleComponent.ValueIsStreamed) |
| 308 |
as having "default value", without analyzing the contents. } |
| 309 |
TreatSubComponentAsDefault and |
| 310 |
(C is TComponent) and |
| 311 |
(csSubComponent in TComponent(C).ComponentStyle) and |
| 312 |
(not CustomizedValueIsStreamed(C)) |
| 313 |
) or |
| 314 |
( |
| 315 |
{ Avoid saving common subcomponents with all values left as default. |
| 316 |
This makes .castle-user-interface files smaller, which makes also their |
| 317 |
diffs easier to read (useful when commiting them, reviewing etc.) } |
| 318 |
(C is TCastleComponent) and |
| 319 |
(not TCastleComponent(C).ValueIsStreamed) |
| 320 |
); |
| 321 |
end; |
| 322 |
tkInt64{$ifdef FPC}, tkQWord{$endif}: |
| 323 |
begin |
| 324 |
Result := GetInt64Prop(PropObject, PropInfo) = 0; |
| 325 |
end; |
| 326 |
{$ifdef FPC} |
| 327 |
tkBool: |
| 328 |
begin |
| 329 |
BoolValue := GetOrdProp(PropObject, PropInfo)<>0; |
| 330 |
DefBoolValue := DefValue <> 0; |
| 331 |
Result := BoolValue = DefBoolValue; |
| 332 |
end; |
| 333 |
{$endif} |
| 334 |
else ; |
| 335 |
end; |
| 336 |
end; |
| 337 |
|
| 338 |
function PropertyIsBool(const PropInfo: PPropInfo): Boolean; |
| 339 |
var |
| 340 |
PropType: PTypeInfo; |
| 341 |
begin |
| 342 |
PropType := PropInfo^.PropType{$ifndef FPC}^{$endif}; |
| 343 |
{$ifdef FPC} |
| 344 |
Result := PropType^.Kind = tkBool; |
| 345 |
{$else} |
| 346 |
{ Delphi makes things harder, using tkEnumeration for Boolean. |
| 347 |
See http://blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm , |
| 348 |
https://stackoverflow.com/questions/10188459/how-to-loop-all-properties-in-a-class , |
| 349 |
https://en.delphipraxis.net/topic/11756-safegetenumname-a-safer-implementation-of-typinfogetenumname/ , |
| 350 |
https://blog.dummzeuch.de/2024/06/21/safegetenumname-a-safer-implementation-of-typinfo-getenumname/ |
| 351 |
} |
| 352 |
Result := (PropType^.Kind = tkEnumeration) and |
| 353 |
(GetTypeData(PropType)^.BaseType^ = TypeInfo(Boolean)); |
| 354 |
{$endif} |
| 355 |
end; |
| 356 |
|
| 357 |
function PropertyBoolGet(const PropObject: TObject; const PropInfo: PPropInfo): Boolean; |
| 358 |
begin |
| 359 |
Result := GetOrdProp(PropObject, PropInfo) <> 0; |
| 360 |
end; |
| 361 |
|
| 362 |
procedure PropertyBoolSet(const PropObject: TObject; const PropInfo: PPropInfo; const NewValue: Boolean); |
| 363 |
begin |
| 364 |
SetOrdProp(PropObject, PropInfo, Iff(NewValue, 1, 0)); |
| 365 |
end; |
| 366 |
|
| 367 |
initialization |
| 368 |
InternalComponent := TCastleComponent.Create(nil); |
| 369 |
finalization |
| 370 |
FreeAndNil(InternalComponent); |
| 371 |
end. |