| 1 |
{ |
| 2 |
Copyright 2014-2023 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 |
Parts of this file are based on FPC packages/fpmkunit/src/fpmkunit.pp unit, |
| 9 |
which conveniently uses *exactly* the same license as Castle Game Engine. |
| 10 |
|
| 11 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 |
|
| 15 |
---------------------------------------------------------------------------- |
| 16 |
} |
| 17 |
|
| 18 |
{ Generating resources (res) files. } |
| 19 |
unit ToolResources; |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
uses CastleUtils, CastleStringUtils, |
| 24 |
ToolProject, ToolArchitectures, Classes; |
| 25 |
|
| 26 |
{ Maybe make castle-auto-generated-resources.res (depends on platform). |
| 27 |
Returns if resource file was created. } |
| 28 |
function MakeAutoGeneratedResources(const Project: TCastleProject; |
| 29 |
const FinalOutputResourcePath: string; |
| 30 |
const OS: TOS; const CPU: TCpu): Boolean; |
| 31 |
|
| 32 |
implementation |
| 33 |
|
| 34 |
uses SysUtils, |
| 35 |
Resource, ResWriter, VersionResource, VersionConsts, VersionTypes, |
| 36 |
GroupIconResource, |
| 37 |
CastleUriUtils, CastleLog, CastleFilesUtils, CastleDownload, |
| 38 |
ToolCommonUtils, ToolUtils, ToolCompilerInfo, ToolManifest; |
| 39 |
|
| 40 |
function MakeAutoGeneratedResources(const Project: TCastleProject; |
| 41 |
const FinalOutputResourcePath: string; |
| 42 |
const OS: TOS; const CPU: TCpu): Boolean; |
| 43 |
|
| 44 |
function MakeVersion(const V: TProjectVersion): TFileProductVersion; |
| 45 |
var |
| 46 |
I: Integer; |
| 47 |
begin |
| 48 |
for I := 0 to 3 do |
| 49 |
Result[I] := V.Items[I]; |
| 50 |
end; |
| 51 |
|
| 52 |
{ Add icon definition to resource Res. |
| 53 |
Returns TStream with icon, that has to remain allocated through Res lifetime. } |
| 54 |
function AddIcon(const Res: TResources; const FullIcoPath: String): TStream; |
| 55 |
var |
| 56 |
ResIconGroup: TGroupIconResource; |
| 57 |
ResIconGroupName: TResourceDesc; |
| 58 |
begin |
| 59 |
ResIconGroupName := TResourceDesc.Create('MAINICON'); |
| 60 |
ResIconGroup := TGroupIconResource.Create(nil, ResIconGroupName); |
| 61 |
FreeAndNil(ResIconGroupName); |
| 62 |
Result := Download(FilenameToUriSafe(FullIcoPath)); |
| 63 |
ResIconGroup.SetCustomItemDataStream(Result); |
| 64 |
Res.Add(ResIconGroup); |
| 65 |
end; |
| 66 |
|
| 67 |
const |
| 68 |
ManifestTemplate = {$I ../embedded_templates/windows/castle-automatic-windows.manifest.inc}; |
| 69 |
// Used as default in RC parser in fcl-res, with comment "MS RC starts up as en-US" |
| 70 |
DefaultRcLanguage = $0409; |
| 71 |
var |
| 72 |
IcoPath, FullIcoPath, OutputManifest, ResFilename: String; |
| 73 |
Res: TResources; |
| 74 |
VerRes: TVersionResource; |
| 75 |
VerStringTable: TVersionStringTable; |
| 76 |
VerTranslationInfo: TVerTranslationInfo; |
| 77 |
ResManifest: TGenericResource; |
| 78 |
ResManifestType, ResManifestName: TResourceDesc; |
| 79 |
ManifestStream, IconStream: TStream; |
| 80 |
begin |
| 81 |
// For now, the .res files are only used on Windows |
| 82 |
Result := OS in AllWindowsOSes; |
| 83 |
if not Result then Exit; |
| 84 |
|
| 85 |
OutputManifest := Project.ReplaceMacros(ManifestTemplate); |
| 86 |
|
| 87 |
{ nil local variables, to easily finalize them later in one finally..end clause } |
| 88 |
ManifestStream := nil; |
| 89 |
IconStream := nil; |
| 90 |
|
| 91 |
Res := TResources.Create; |
| 92 |
try |
| 93 |
{ Originally written as RC file, with resource contents |
| 94 |
(version info, icon, manifest) |
| 95 |
crafted looking at docs, what others are doing, and what Lazarus produces: |
| 96 |
|
| 97 |
http://www.osronline.com/article.cfm?article=588 |
| 98 |
http://msdn.microsoft.com/en-us/library/windows/desktop/aa381058%28v=vs.85%29.aspx |
| 99 |
http://stackoverflow.com/questions/12821369/vc-2012-how-to-include-version-info-from-version-inc-maintained-separately |
| 100 |
http://forum.lazarus.freepascal.org/index.php?topic=8979.0 |
| 101 |
|
| 102 |
This was then reworked RC -> into Pascal code using FPC TResources. |
| 103 |
This way we can construct this in a cross-platform way, |
| 104 |
without any additional intermediate RC files, |
| 105 |
and generate RES without any external tools (like windres). |
| 106 |
To understand how RC -> map into fcl-res classes |
| 107 |
|
| 108 |
- read fcl-res source code, esp. rcparser.y. |
| 109 |
|
| 110 |
- https://www.freepascal.org/docs-html/current/fclres/basic%20usage.html |
| 111 |
is very helpful to understand fcl-res classes. |
| 112 |
} |
| 113 |
|
| 114 |
VerRes := TVersionResource.Create; |
| 115 |
VerRes.LangID := DefaultRcLanguage; |
| 116 |
VerRes.FixedInfo.FileVersion := MakeVersion(Project.Version); |
| 117 |
VerRes.FixedInfo.ProductVersion := MakeVersion(Project.Version); |
| 118 |
VerRes.FixedInfo.FileOS := VOS_NT_WINDOWS32; |
| 119 |
VerRes.FixedInfo.FileType := VFT_APP; |
| 120 |
Res.Add(VerRes); |
| 121 |
|
| 122 |
VerStringTable := TVersionStringTable.Create('040904b0'); |
| 123 |
if Project.Author <> '' then |
| 124 |
begin |
| 125 |
VerStringTable.Add('CompanyName', Project.Author); |
| 126 |
VerStringTable.Add('LegalCopyright', 'Copyright ' + Project.Author); |
| 127 |
end; |
| 128 |
VerStringTable.Add('FileDescription', Project.Name); |
| 129 |
VerStringTable.Add('FileVersion', Project.Version.DisplayValue); |
| 130 |
VerStringTable.Add('InternalName', Project.Name); |
| 131 |
VerStringTable.Add('OriginalFilename', Project.ExecutableName + '.exe'); |
| 132 |
VerStringTable.Add('ProductName', Project.Name); |
| 133 |
VerStringTable.Add('ProductVersion', Project.Version.DisplayValue); |
| 134 |
VerRes.StringFileInfo.Add(VerStringTable); |
| 135 |
|
| 136 |
VerTranslationInfo.Language := $0409; // US English |
| 137 |
VerTranslationInfo.Codepage := 1200; // Unicode |
| 138 |
VerRes.VarFileInfo.Add(VerTranslationInfo); |
| 139 |
|
| 140 |
ResManifestType := TResourceDesc.Create(RT_MANIFEST); |
| 141 |
ResManifestName := TResourceDesc.Create(1); |
| 142 |
ResManifest := TGenericResource.Create(ResManifestType, ResManifestName); |
| 143 |
FreeAndNil(ResManifestType); |
| 144 |
FreeAndNil(ResManifestName); // the TResourceDesc can be freed right after usage |
| 145 |
ManifestStream := TStringStream.Create(OutputManifest); |
| 146 |
ResManifest.SetCustomRawDataStream(ManifestStream); |
| 147 |
Res.Add(ResManifest); |
| 148 |
|
| 149 |
IcoPath := Project.Icons.FindExtension(['.ico']); |
| 150 |
if IcoPath <> '' then |
| 151 |
begin |
| 152 |
FullIcoPath := CombinePaths(Project.Path, IcoPath); |
| 153 |
IconStream := AddIcon(Res, FullIcoPath); |
| 154 |
end else |
| 155 |
begin |
| 156 |
FullIcoPath := UriToFilenameSafe('castle-data:/windows/default_icon.ico'); |
| 157 |
if URIFileExists(FullIcoPath) then |
| 158 |
begin |
| 159 |
IconStream := AddIcon(Res, FullIcoPath); |
| 160 |
end else |
| 161 |
WritelnWarning('Windows Resources', 'Icon in format suitable for Windows (.ico) not found, and default CGE icon also not found. Exe file will not have icon.'); |
| 162 |
end; |
| 163 |
|
| 164 |
ResFilename := FinalOutputResourcePath + 'castle-auto-generated-resources.res'; |
| 165 |
Res.WriteToFile(ResFilename); |
| 166 |
finally |
| 167 |
FreeAndNil(Res); |
| 168 |
{ Free streams. |
| 169 |
We need to do this, passing them to SetCustomItemDataStream / SetCustomItemDataStream |
| 170 |
doesn't make them owned by Res. |
| 171 |
This is confirmed with testing with HeapTrc, and reading docs |
| 172 |
https://www.freepascal.org/docs-html/current/fclres/basic%20usage.html. } |
| 173 |
FreeAndNil(ManifestStream); |
| 174 |
FreeAndNil(IconStream); |
| 175 |
end; |
| 176 |
|
| 177 |
WritelnVerbose('Generated ' + ExtractFileName(ResFilename) + ', make sure you include it in your dpr/lpr source file like this:'); |
| 178 |
WritelnVerbose(' {$ifdef CASTLE_AUTO_GENERATED_RESOURCES} {$R ' + ExtractFileName(ResFilename) + '} {$endif}'); |
| 179 |
end; |
| 180 |
|
| 181 |
end. |