| 1 |
{ |
| 2 |
Copyright 2021-2022 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 |
{ Utilities to operate on Pascal source code. } |
| 17 |
unit EditorCodeTools; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses SysUtils, |
| 22 |
ToolManifest; |
| 23 |
|
| 24 |
{ Find unit where view initialization takes place. |
| 25 |
Returns '' if not found. |
| 26 |
Returns relative filename (relative to project path) if found. } |
| 27 |
function FindUnitToInitializeView(const Manifest: TCastleManifest): String; |
| 28 |
|
| 29 |
{ Add view initialization to given unit. |
| 30 |
Assumes that it was a unit returned by FindUnitToInitializeView, |
| 31 |
so it passed basic checks implemented there. |
| 32 |
Raises exception if unit cannot be modified for some reason |
| 33 |
(e.g. it was broken in the meantime, between |
| 34 |
FindUnitToInitializeView and AddInitializeView calls, |
| 35 |
or we have no permissions to write there). } |
| 36 |
procedure AddInitializeView(const UnitFileAbsolute: String; |
| 37 |
const ViewUnitName, ViewClassName, ViewVariableName: String); |
| 38 |
|
| 39 |
implementation |
| 40 |
|
| 41 |
uses CastleStringUtils, CastleFilesUtils, CastleUriUtils, CastleLog, CastleUtils; |
| 42 |
|
| 43 |
const |
| 44 |
CommentViewCreateBegin = '{$region ''Castle View Creation''}'; |
| 45 |
CommentViewCreateEnd = '{$endregion ''Castle View Creation''}'; |
| 46 |
CommentView2CreateBegin = '{$region ''Castle State Creation''}'; |
| 47 |
CommentView2CreateEnd = '{$endregion ''Castle State Creation''}'; |
| 48 |
CommentInitializationUsesBegin = '{$region ''Castle Initialization Uses''}'; |
| 49 |
CommentInitializationUsesEnd = '{$endregion ''Castle Initialization Uses''}'; |
| 50 |
|
| 51 |
function CheckUnitToInitializeView(const FileName: String): Boolean; |
| 52 |
var |
| 53 |
Content: String; |
| 54 |
begin |
| 55 |
try |
| 56 |
Content := FileToString(FilenameToUriSafe(FileName)); |
| 57 |
except |
| 58 |
on E: Exception do |
| 59 |
begin |
| 60 |
WritelnWarning('Could not open main unit to initialize views "%s": %s', [ |
| 61 |
FileName, |
| 62 |
E.Message |
| 63 |
]); |
| 64 |
Exit(false); |
| 65 |
end; |
| 66 |
end; |
| 67 |
|
| 68 |
Result := |
| 69 |
( (Pos(CommentViewCreateBegin, Content) <> 0) or (Pos(CommentView2CreateBegin, Content) <> 0) ) and |
| 70 |
( (Pos(CommentViewCreateEnd, Content) <> 0) or (Pos(CommentView2CreateEnd, Content) <> 0) ) and |
| 71 |
(Pos(CommentInitializationUsesBegin, Content) <> 0) and |
| 72 |
(Pos(CommentInitializationUsesEnd, Content) <> 0); |
| 73 |
end; |
| 74 |
|
| 75 |
function FindUnitToInitializeView(const Manifest: TCastleManifest): String; |
| 76 |
var |
| 77 |
UnitNames: TCastleStringList; |
| 78 |
AUnitName, UnitFileNameAbsolute, UnitFileNameRelative: String; |
| 79 |
begin |
| 80 |
UnitNames := CreateTokens(Manifest.GameUnits, WhiteSpaces + [',']); |
| 81 |
try |
| 82 |
for AUnitName in UnitNames do |
| 83 |
begin |
| 84 |
UnitFileNameAbsolute := Manifest.SearchPascalUnit(AUnitName); |
| 85 |
if (UnitFileNameAbsolute <> '') and |
| 86 |
CheckUnitToInitializeView(UnitFileNameAbsolute) then |
| 87 |
begin |
| 88 |
UnitFileNameRelative := ExtractRelativePath(Manifest.Path, UnitFileNameAbsolute); |
| 89 |
Exit(UnitFileNameRelative); |
| 90 |
end; |
| 91 |
end; |
| 92 |
|
| 93 |
Result := ''; // not found |
| 94 |
finally FreeAndNil(UnitNames) end; |
| 95 |
end; |
| 96 |
|
| 97 |
procedure AddInitializeView(const UnitFileAbsolute: String; |
| 98 |
const ViewUnitName, ViewClassName, ViewVariableName: String); |
| 99 |
var |
| 100 |
UnitFileUrl: String; |
| 101 |
Content: String; |
| 102 |
P: SizeInt; |
| 103 |
begin |
| 104 |
UnitFileUrl := FilenameToUriSafe(UnitFileAbsolute); |
| 105 |
Content := FileToString(UnitFileUrl); |
| 106 |
|
| 107 |
P := Pos(CommentViewCreateEnd, Content); |
| 108 |
if P = 0 then |
| 109 |
P := Pos(CommentView2CreateEnd, Content); |
| 110 |
if P = 0 then |
| 111 |
raise Exception.CreateFmt('Cannot modify unit file "%s", does not contain comment "%s".', [ |
| 112 |
UnitFileAbsolute, |
| 113 |
CommentViewCreateEnd |
| 114 |
]); |
| 115 |
Insert(Format('%s := %s.Create(Application);' + NL + ' ', [ |
| 116 |
ViewVariableName, |
| 117 |
ViewClassName |
| 118 |
]), Content, P); |
| 119 |
|
| 120 |
P := Pos(CommentInitializationUsesEnd, Content); |
| 121 |
if P = 0 then |
| 122 |
raise Exception.CreateFmt('Cannot modify unit file "%s", does not contain comment "%s".', [ |
| 123 |
UnitFileAbsolute, |
| 124 |
CommentInitializationUsesEnd |
| 125 |
]); |
| 126 |
Insert(Format(', %s' + NL + ' ', [ |
| 127 |
ViewUnitName |
| 128 |
]), Content, P); |
| 129 |
|
| 130 |
StringToFile(UnitFileUrl, Content); |
| 131 |
end; |
| 132 |
|
| 133 |
end. |