| 1 |
// -*- compile-command: "castle-engine compile && castle-engine run" -*- |
| 2 |
{ |
| 3 |
Copyright 2018-2022 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{ Generate Pascal code to instantiate persistent vectors/colors classes. |
| 18 |
See README.md for detailed description. } |
| 19 |
|
| 20 |
uses SysUtils, Generics.Collections, StrUtils, |
| 21 |
CastleUtils, CastleStringUtils, CastleFilesUtils, CastleDownload; |
| 22 |
|
| 23 |
type |
| 24 |
TAutoGenerateProperty = class |
| 25 |
PropertyName, PropertyType: String; |
| 26 |
NoStore, Rotation: Boolean; |
| 27 |
StoreCheckFunction: String; |
| 28 |
function PersistentPropertyClass: String; |
| 29 |
procedure ProcessToken(const Token: String); |
| 30 |
end; |
| 31 |
|
| 32 |
TAutoGeneratePropertyList = class(specialize TObjectList<TAutoGenerateProperty>) |
| 33 |
end; |
| 34 |
|
| 35 |
TAutoGenerateClass = class |
| 36 |
Properties: TAutoGeneratePropertyList; |
| 37 |
Path, OutputClassName: String; |
| 38 |
constructor Create; |
| 39 |
destructor Destroy; override; |
| 40 |
procedure GenerateCode; |
| 41 |
end; |
| 42 |
|
| 43 |
TAutoGenerateClasses = class(specialize TObjectDictionary<String, TAutoGenerateClass>) |
| 44 |
procedure Add(const Path, OutputClassName: String; |
| 45 |
const Prop: TAutoGenerateProperty); |
| 46 |
procedure GenerateAll; |
| 47 |
end; |
| 48 |
|
| 49 |
{ TAutoGenerateProperty ------------------------------------------------------ } |
| 50 |
|
| 51 |
function TAutoGenerateProperty.PersistentPropertyClass: String; |
| 52 |
begin |
| 53 |
Result := StringReplace(PropertyType, 'TVector', 'TCastleVector', [rfReplaceAll]); |
| 54 |
if Rotation then |
| 55 |
Result += 'RotationPersistent' |
| 56 |
else |
| 57 |
Result += 'Persistent'; |
| 58 |
end; |
| 59 |
|
| 60 |
procedure TAutoGenerateProperty.ProcessToken(const Token: String); |
| 61 |
begin |
| 62 |
if Token = 'no-store' then |
| 63 |
NoStore := true |
| 64 |
else |
| 65 |
if IsPrefix('store=', Token, false) then |
| 66 |
StoreCheckFunction := PrefixRemove('store=', Token, false) |
| 67 |
else |
| 68 |
if Token = 'rotation' then |
| 69 |
Rotation := true |
| 70 |
else |
| 71 |
raise Exception.CreateFmt('Invalid token "%s" at property "%s"', [Token, PropertyName]); |
| 72 |
end; |
| 73 |
|
| 74 |
{ TAutoGenerateClass --------------------------------------------------------- } |
| 75 |
|
| 76 |
constructor TAutoGenerateClass.Create; |
| 77 |
begin |
| 78 |
inherited; |
| 79 |
Properties := TAutoGeneratePropertyList.Create(true); |
| 80 |
end; |
| 81 |
|
| 82 |
destructor TAutoGenerateClass.Destroy; |
| 83 |
begin |
| 84 |
FreeAndNil(Properties); |
| 85 |
inherited; |
| 86 |
end; |
| 87 |
|
| 88 |
procedure TAutoGenerateClass.GenerateCode; |
| 89 |
var |
| 90 |
InputTemplate, OutputPath, OutputFileName, OutputCode: String; |
| 91 |
Macros: TStringStringMap; |
| 92 |
P: TAutoGenerateProperty; |
| 93 |
begin |
| 94 |
OutputPath := Path + 'auto_generated_persistent_vectors' + PathDelim; |
| 95 |
ForceDirectories(OutputPath); |
| 96 |
OutputFileName := OutputPath + |
| 97 |
LowerCase(OutputClassName) + '_persistent_vectors.inc'; |
| 98 |
|
| 99 |
InputTemplate := FileToString('persistent_vectors_template.inc'); |
| 100 |
OutputCode := ''; |
| 101 |
|
| 102 |
for P in Properties do |
| 103 |
begin |
| 104 |
Macros := TStringStringMap.Create; |
| 105 |
try |
| 106 |
Macros.Add('${CLASS_NAME}', OutputClassName); |
| 107 |
Macros.Add('${PROPERTY_NAME}', P.PropertyName); |
| 108 |
Macros.Add('${PROPERTY_TYPE}', P.PropertyType); |
| 109 |
if P.StoreCheckFunction <> '' then |
| 110 |
Macros.Add('${PROPERTY_STORED}', 'stored ' + P.StoreCheckFunction) |
| 111 |
else |
| 112 |
Macros.Add('${PROPERTY_STORED}', IfThen(P.NoStore, 'stored false', '')); |
| 113 |
Macros.Add('${PERSISTENT_PROPERTY_CLASS}', P.PersistentPropertyClass); |
| 114 |
OutputCode := OutputCode + SReplacePatterns(InputTemplate, Macros, false) + NL; |
| 115 |
finally FreeAndNil(Macros) end; |
| 116 |
end; |
| 117 |
|
| 118 |
StringToFile(OutputFileName, OutputCode); |
| 119 |
Writeln('Generated ', OutputFileName, ' with ', Properties.Count, ' persistent vectors/colors properties'); |
| 120 |
end; |
| 121 |
|
| 122 |
{ TAutoGenerateClasses ------------------------------------------------------- } |
| 123 |
|
| 124 |
procedure TAutoGenerateClasses.Add( |
| 125 |
const Path, OutputClassName: String; |
| 126 |
const Prop: TAutoGenerateProperty); |
| 127 |
var |
| 128 |
C: TAutoGenerateClass; |
| 129 |
begin |
| 130 |
if not TryGetValue(Path + OutputClassName, C) then |
| 131 |
begin |
| 132 |
C := TAutoGenerateClass.Create; |
| 133 |
C.Path := Path; |
| 134 |
C.OutputClassName := OutputClassName; |
| 135 |
inherited Add(Path + OutputClassName, C); |
| 136 |
end; |
| 137 |
|
| 138 |
C.Properties.Add(Prop); |
| 139 |
end; |
| 140 |
|
| 141 |
procedure TAutoGenerateClasses.GenerateAll; |
| 142 |
var |
| 143 |
C: TAutoGenerateClass; |
| 144 |
begin |
| 145 |
for C in Values do |
| 146 |
C.GenerateCode; |
| 147 |
end; |
| 148 |
|
| 149 |
{ main program --------------------------------------------------------------- } |
| 150 |
|
| 151 |
var |
| 152 |
AutoGenerateClasses: TAutoGenerateClasses; |
| 153 |
InputFile: TTextReader; |
| 154 |
Line, Path, OutputClassName: String; |
| 155 |
LineSplit: TCastleStringList; |
| 156 |
Prop: TAutoGenerateProperty; |
| 157 |
I: Integer; |
| 158 |
begin |
| 159 |
AutoGenerateClasses := TAutoGenerateClasses.Create([doOwnsValues]); |
| 160 |
try |
| 161 |
InputFile := TTextReader.Create('persistent_vectors_input.txt'); |
| 162 |
try |
| 163 |
while not InputFile.Eof do |
| 164 |
begin |
| 165 |
Line := InputFile.Readln; |
| 166 |
|
| 167 |
if Trim(Line) = '' then Continue; // skip empty lines |
| 168 |
|
| 169 |
LineSplit := CreateTokens(Line); |
| 170 |
try |
| 171 |
if LineSplit.Count < 4 then |
| 172 |
raise Exception.CreateFmt('Expected at least 4 tokens at line "%s"', [Line]); |
| 173 |
|
| 174 |
Path := InclPathDelim(LineSplit[0]); |
| 175 |
OutputClassName := LineSplit[1]; |
| 176 |
|
| 177 |
Prop := TAutoGenerateProperty.Create; |
| 178 |
Prop.PropertyName := LineSplit[2]; |
| 179 |
Prop.PropertyType := LineSplit[3]; |
| 180 |
for I := 4 to LineSplit.Count - 1 do |
| 181 |
Prop.ProcessToken(LineSplit[I]); |
| 182 |
|
| 183 |
AutoGenerateClasses.Add(Path, OutputClassName, Prop); |
| 184 |
finally FreeAndNil(LineSplit) end; |
| 185 |
end; |
| 186 |
finally FreeAndNil(InputFile) end; |
| 187 |
|
| 188 |
AutoGenerateClasses.GenerateAll; |
| 189 |
finally FreeAndNil(AutoGenerateClasses) end; |
| 190 |
end. |