aboutsummaryrefslogtreecommitdiffstats
path: root/Tools/ResourceExtractor/ResourceExtractor.pas
blob: aa9cc3bc07069a03cf00db161f388d246e3b4ea1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
program ResourceExtractor;

{$ifdef FPC}
  {$mode delphi}{$H+}
{$endif}

uses
  Classes,
  SysUtils,
  StrUtils;
  

var
  ResCount: integer;
  OutStream: TStringList;


procedure Init();
begin
  OutStream := TStringList.Create();

  OutStream.Add('const');
  // placeholder for array-header (will be filled on file-saving)
  OutStream.Add('');
end;

procedure SaveToFile(const OutFileName: string);
begin
  // add array-header
  OutStream[1] := '  resources: array[0..'+IntToStr(ResCount-1)+', 0..2] of string = (';
  // add trailer
  OutStream.Add('  );');

  // save file
  try
    OutStream.SaveToFile(OutFileName);
  except
    OutStream.Free();
    raise Exception.Create('Could not save to file: "' + OutFileName + '"');
  end;

  OutStream.Free();
end;

function AddResource(Fields: TStringList; const RCFileDir, ResDir: string): boolean;
var
  ResName, ResType, ResFile: string;
begin
  if (Fields.Count < 3) or
     (AnsiStartsStr('//', Fields[0])) or
     (Length(Fields[2]) < 3) then
  begin
    Result := false;
    Exit;
  end;

  // add a trailing comma to the last line
  if (ResCount > 0) then
    OutStream[OutStream.Count-1] := OutStream[OutStream.Count-1] + ',';

  ResName := Fields[0];
  ResType := Fields[1];
  ResFile := Fields[2];
  
  Writeln('ADD:  [' + ResType + ':' + ResName + ' = ' +ResFile + ']');

  // quote fields
  ResName := AnsiQuotedStr(ResName, '''')+',';
  ResType := AnsiQuotedStr(ResType, '''')+',';
  // strip surrounding quotes of filename
  ResFile := AnsiMidStr(ResFile, 2, Length(Fields[2])-2);
  // now translate the resource filename (relative to the RC-file) to be relative to the resource-dir
  // 1st step: get absolute path of the resource
  ResFile := ExpandFileName(RCFileDir + ResFile);
  // 2nd step: get path of the resource relative to the resource-dir
  // Note: both paths must be absolute and the base-path must have a trailing '/' or '\'
  ResFile := ExtractRelativepath(ResDir, ResFile);
  // 3rd step: ... and quote
  ResFile := AnsiQuotedStr(ResFile, '''');

  // compose line
  OutStream.Add(Format('    (%-20S%-8S%S)', [ResName, ResType, ResFile]));

  Inc(ResCount);
  
  Result := true;
end;

procedure ExtractResources(const InFileName, ResDir: string);
var
  Fields: TStringList;
  LineIndex: integer;
  Lines: TStringList;
  RCFileDirAbs, ResDirAbs: string;
begin
  // get absolute paths
  RCFileDirAbs := ExtractFilePath(ExpandFileName(InFileName));
  ResDirAbs := ExpandFileName(ResDir) + '/';

  Lines := TStringList.Create();
  try
    Lines.LoadFromFile(InFileName);
  except
    raise Exception.Create('Failed to open file: "' + InFileName + '"');
  end;

  Fields := TStringList.Create();
  for LineIndex := 0 to Lines.Count-1 do
  begin
    Fields.Clear();
    // split line into [Name, Type, File]
    ExtractStrings([' ', #9], [], PChar(Lines[LineIndex]), Fields);
    if (not AddResource(Fields, RCFileDirAbs, ResDirAbs)) then
      Writeln( 'SKIP: "'+Lines[LineIndex]+'"');
  end;

  Lines.Free();
  Fields.Free();
end;

var
  ProgName: string;
begin
  if (ParamCount <> 3) then
  begin
    ProgName := ExtractFileName(ParamStr(0));
    WriteLn('Usage: ' + ProgName + ' RC-File Resource-Dir Output-File');
    Exit;
  end;

  WriteLn('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');
  WriteLn('Converting "' + ParamStr(1) + '" to "' + ParamStr(3) + '"');
  WriteLn('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>');

  try
    Init();
    ExtractResources(ParamStr(1), ParamStr(2));
    SaveToFile(ParamStr(3));
  except on E:Exception do
  begin
    WriteLn('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    WriteLn('Conversion failed: ' + E.Message + '!');
    WriteLn('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!');
    Exit;
  end;
  end;

  WriteLn('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');
  WriteLn('Conversion finished!');
  WriteLn('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<');
end.