-
Notifications
You must be signed in to change notification settings - Fork 2
/
lovedist.lpr
332 lines (279 loc) · 10.1 KB
/
lovedist.lpr
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
program lovedist;
{$mode objfpc}{$H+}
uses
{$IFDEF UNIX}{$IFDEF UseCThreads}
cthreads,
{$ENDIF}{$ENDIF}
Classes, SysUtils, CustApp, zipper, ShellApi, shellit
{ you can add units after this };
type
{ TMyApplication }
TMyApplication = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
procedure WriteHelp; virtual;
procedure ParseDir(path, luapath : string; fileString : TStringList);
end;
procedure CopyFile(const inFile : string; const outFile : string);
var
inpF : TFileStream;
outF : TFileStream;
buffer : array [0..15359] of Byte;
bRead : LongInt;
begin
inpF := TFileStream.Create(inFile, fmOpenRead);
outF := TFileStream.Create(outFile, fmCreate);
try
bRead := inpF.Read(buffer, 15360);
while bRead <> 0 do begin
outF.Write(buffer, bRead);
bRead := inpF.Read(buffer, 15360);
end;
finally
inpF.Free;
outF.Free;
end;
end;
procedure CleanWorkDir(path : string);
var
fRec : TSearchRec;
begin
if FindFirst(path + DirectorySeparator + '*.*', faAnyFile and faDirectory, fRec) = 0 then
begin
repeat
if (fRec.Attr and faDirectory) <> faDirectory then
DeleteFile(path + DirectorySeparator + fRec.Name)
else
if (fRec.Name <> '..') and (fRec.Name <> '.') then
begin
CleanWorkDir(path + DirectorySeparator + fRec.Name);
end;
until FindNext(fRec) <> 0;
FindClose(fRec);
RemoveDir(path);
end;
end;
{ TMyApplication }
procedure TMyApplication.DoRun;
var
zip : TZipper;
files : TStringList;
baseDir : string;
fname : string;
gamefolder : string;
distfolder : string;
outfile : string;
ft : TFileStream;
gr : TFileStream;
buffer : array [0..15359] of Byte;
bRead : LongInt;
i : Integer;
embedded : Boolean;
skipzip : Boolean;
begin
WriteLn('LoveDist - Distribution Utility');
Writeln('Version: 1.1.3');
Writeln('Made by Aleksandar Panic, [email protected]');
// parse parameters
if HasOption('h','help') then begin
WriteHelp;
Terminate;
Exit;
end;
baseDir := ExtractFileDir(ParamStr(0));
gamefolder := ParamStr(1);
outfile := ParamStr(2);
embedded := false;
skipzip := false;
if (ExtractFileExt(ParamStr(1)) = '.love') then begin
skipzip := true;
outfile := StringReplace(ExtractFileName(ParamStr(1)), '.love', '', [rfIgnoreCase]);
Writeln('Using: ' + outfile + '.love');
gamefolder := outfile;
end;
if ParamCount = 0 then begin
WriteHelp;
Terminate;
Exit;
end;
if skipzip = false then begin
if DirectoryExists(baseDir + DirectorySeparator + gamefolder) = false then begin
WriteLn('Error: Game folder does not exist or not specified. LoveDist must be in the same root folder as the game directory.');
WriteHelp;
Terminate;
Exit;
end;
if DirectoryExists(baseDir + DirectorySeparator + gamefolder) = false then begin
WriteLn('Error: Output filename must be specified, without extension.');
WriteHelp;
Terminate;
Exit;
end;
Writeln('');
WriteLn(' [ LOVE File ] ');
Writeln('');
zip := TZipper.Create;
files := TStringList.Create;
try
writeln('Project Directory: ' + baseDir + DirectorySeparator + gamefolder);
if DirectoryExists(baseDir + DirectorySeparator + gamefolder + '_work') then
CleanWorkDir(baseDir + DirectorySeparator + gamefolder + '_work');
MkDir(baseDir + DirectorySeparator + gamefolder + '_work');
ParseDir(baseDir + DirectorySeparator + gamefolder, baseDir + DirectorySeparator + gamefolder + '_work', files);
writeln('Found: ' + IntToStr(files.Count) + ' ' + gamefolder + ' files.');
writeln('Creating LOVE file...');
zip.FileName := baseDir + DirectorySeparator + outfile + '.zip';
if HasOption('c', 'compile') then begin
Writeln('');
WriteLn(' [ Compilation ] ');
Writeln('');
Writeln('Compile initiated.');
for i := 0 to files.Count - 1 do
begin
fname := files.Strings[i];
if ExtractFileExt(fname) = '.lua' then begin
writeln('Compiling: ' + StringReplace(fname, baseDir, '', [rfIgnoreCase]));
ExecAndWait('"' + baseDir + DirectorySeparator + 'luac.exe' + '"', '-s -o "' + fname + '" "' + fname + '"', baseDir);
end;
end;
end;
for i := 0 to files.Count - 1 do
begin
if ExtractFileExt(files.Strings[i]) = '.lua' then
begin
zip.Entries.AddFileEntry(files.Strings[i], StringReplace(files.Strings[i], baseDir + DirectorySeparator + gamefolder + '_work' + DirectorySeparator, '', [rfIgnoreCase]));
end
else
zip.Entries.AddFileEntry(files.Strings[i], StringReplace(files.Strings[i], baseDir + DirectorySeparator + gamefolder + DirectorySeparator, '', [rfIgnoreCase]));
end;
zip.ZipAllFiles;
finally
zip.Free;
files.Free;
end;
RenameFile(baseDir + DirectorySeparator + outfile + '.zip', baseDir + DirectorySeparator + outfile + '.love');
writeln('LOVE file created successfully.');
end;
if HasOption('e', 'embed') then begin
Writeln('');
WriteLn(' [ Embedding ] ');
Writeln('');
Writeln('Embed initiated.');
gr := TFileStream.Create(baseDir + DirectorySeparator + 'love.exe', fmOpenRead);
ft := TFileStream.Create(baseDir + DirectorySeparator + outfile + '.exe', fmCreate);
bRead := gr.Read(buffer, 15360);
while bRead <> 0 do begin
ft.Write(buffer, bRead);
bRead := gr.Read(buffer, 15360);
end;
gr.Free;
gr := TFileStream.Create(baseDir + DirectorySeparator + outfile + '.love', fmOpenRead);
bRead := gr.Read(buffer, 15360);
while bRead <> 0 do begin
ft.Write(buffer, bRead);
bRead := gr.Read(buffer, 15360);
end;
gr.Free;
ft.Free;
Writeln('Embedding successfull.');
DeleteFile(baseDir + DirectorySeparator + outfile + '.love');
embedded := true;
end;
if HasOption('d', 'dist') then begin
Writeln('');
WriteLn(' [ Distribution ] ');
Writeln('');
Writeln('Packaging initiated.');
distfolder := outfile + '_dist';
if DirectoryExists(baseDir + DirectorySeparator + distfolder) then
CleanWorkDir(baseDir + DirectorySeparator + distfolder);
MkDir(baseDir + DirectorySeparator + distfolder);
// Copy Main File
if embedded then begin
CopyFile(baseDir + DirectorySeparator + outfile + '.exe', baseDir + DirectorySeparator + distfolder + DirectorySeparator + outfile + '.exe');
DeleteFile(baseDir + DirectorySeparator + outfile + '.exe');
end
else begin
CopyFile(baseDir + DirectorySeparator + 'love.exe', baseDir + DirectorySeparator + distfolder + DirectorySeparator + 'love.exe');
CopyFile(baseDir + DirectorySeparator + outfile + '.love', baseDir + DirectorySeparator + distfolder + DirectorySeparator + outfile + '.love');
DeleteFile(baseDir + DirectorySeparator + outfile + '.love');
end;
// Copy required files
CopyFile(baseDir + DirectorySeparator + 'DevIL.dll', baseDir + DirectorySeparator + distfolder + DirectorySeparator + 'DevIL.dll');
CopyFile(baseDir + DirectorySeparator + 'OpenAL32.dll', baseDir + DirectorySeparator + distfolder + DirectorySeparator + 'OpenAL32.dll');
CopyFile(baseDir + DirectorySeparator + 'SDL.dll', baseDir + DirectorySeparator + distfolder + DirectorySeparator + 'SDL.dll');
writeln('');
Writeln('Distribution packagaging finished.');
end;
if DirectoryExists(baseDir + DirectorySeparator + gamefolder + '_work') then
CleanWorkDir(baseDir + DirectorySeparator + gamefolder + '_work');
writeln('');
writeln('');
Writeln('All operations completed. Press any key to end program.');
ReadLn;
// stop program loop
Terminate;
end;
procedure TMyApplication.ParseDir(path, luapath : string; fileString : TStringList);
var
fRec : TSearchRec;
begin
if not (DirectoryExists(luapath)) then MkDir(luapath);
if FindFirst(path + DirectorySeparator + '*.*', faAnyFile and faDirectory, fRec) = 0 then
begin
repeat
if (fRec.Attr and faDirectory) <> faDirectory then
begin
// Move LUA files into work dir
if ExtractFileExt(fRec.Name) = '.lua' then
begin
CopyFile(path + DirectorySeparator + fRec.Name, luapath + DirectorySeparator + fRec.Name);
fileString.Add(luapath + DirectorySeparator + fRec.Name);
end
else
fileString.Add(path + DirectorySeparator + fRec.Name);
end
else
if (fRec.Name <> '..') and (fRec.Name <> '.') then
begin
ParseDir(path + DirectorySeparator + fRec.Name, luapath + DirectorySeparator + fRec.Name, fileString);
end;
until FindNext(fRec) <> 0;
FindClose(fRec);
end;
end;
constructor TMyApplication.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;
destructor TMyApplication.Destroy;
begin
inherited Destroy;
end;
procedure TMyApplication.WriteHelp;
begin
{ add your help code here }
writeln('');
writeln('Usage: ', ExtractFileName(ExeName),' input_folder output_filename [options]');
writeln('');
writeln('-e, -embed - Embeds LOVE file in application.');
writeln('-c, -compile - Compiles all .lua files using lua compiler.');
writeln('-d, -dist - Creates a distribution folder for the game and copies all required files to it.');
writeln('-h, -help - Displays this information.');
writeln('');
writeln('Press any key to end the program.');
readln;
end;
var
Application: TMyApplication;
{$R *.res}
begin
Application := TMyApplication.Create(nil);
Application.Title := 'Love Dist';
Application.Run;
Application.Free;
end.