This repository has been archived by the owner on Oct 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upackconfigurations.pas
359 lines (313 loc) · 11.6 KB
/
upackconfigurations.pas
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
unit uPackConfigurations;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
Buttons, ExtCtrls, EditBtn;
type
{ TfrmPackConfigs }
TfrmPackConfigs = class(TForm)
btnClose: TBitBtn;
btnNewConf: TBitBtn;
btnDelConf: TBitBtn;
btnPack: TBitBtn;
btnSave: TBitBtn;
btnAddTag: TButton;
btnRemoveTag: TButton;
cbDestination: TComboBox;
chkCollision: TCheckBox;
chkRemoveTag: TCheckBox;
chkArchiveSameName: TCheckBox;
chkRemoveComments: TCheckBox;
chkRemoveLeadingSpaces: TCheckBox;
fedtFile: TFileNameEdit;
fedtArchive: TFileNameEdit;
Label2: TLabel;
Label3: TLabel;
lbxConfigs: TListBox;
lbxTags: TListBox;
pnlConfiguration: TPanel;
procedure btnAddTagClick(Sender: TObject);
procedure btnDelConfClick(Sender: TObject);
procedure btnNewConfClick(Sender: TObject);
procedure btnPackClick(Sender: TObject);
procedure btnRemoveTagClick(Sender: TObject);
procedure btnSaveClick(Sender: TObject);
procedure cbDestinationChange(Sender: TObject);
procedure chkArchiveSameNameChange(Sender: TObject);
procedure chkCollisionChange(Sender: TObject);
procedure chkRefactorVariablesChange(Sender: TObject);
procedure chkRemoveCommentsChange(Sender: TObject);
procedure chkRemoveLeadingSpacesChange(Sender: TObject);
procedure chkRemoveTagChange(Sender: TObject);
procedure fedtArchiveChange(Sender: TObject);
procedure fedtFileChange(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure lbxConfigsSelectionChange(Sender: TObject; {%H-}User: boolean);
procedure lbxTagsSelectionChange(Sender: TObject; {%H-}User: boolean);
private
procedure ShowResults(const ProgramName, Destination: string; OriginalSize, PackedSize: integer; Collisions: boolean;
const CollisionText, SyntaxCheckText: string);
public
procedure LoadConfigValues(const ConfigName: string);
procedure LoadConfigs;
end;
var
frmPackConfigs: TfrmPackConfigs;
implementation
{$R *.lfm}
{ TfrmPackConfigs }
uses uSettings, uPacker, uMain, uEditorFile, strutils, uObjects, uVariableFetcher, uPackerResults;
procedure TfrmPackConfigs.btnSaveClick(Sender: TObject);
var
Idx: integer;
begin
Settings.OpenKey('packerconfigs/' + lbxConfigs.GetSelectedText, True);
Settings.SetValue('destination/type', cbDestination.Caption);
if cbDestination.Text = 'Archive' then
begin
Settings.SetValue('destination/Archive/samename', chkArchiveSameName.Checked);
Settings.SetValue('destination/Archive/filename', fedtArchive.FileName);
end
else if cbDestination.Text = 'File' then
Settings.SetValue('destination/File/filename', fedtFile.FileName);
Settings.SetValue('removecomments', chkRemoveComments.Checked);
Settings.SetValue('removetrailingspaces', chkRemoveLeadingSpaces.Checked);
Settings.DeletePath('removetags');
Settings.SetValue('removetags/enabled', chkRemoveTag.Checked);
for Idx := 0 to lbxTags.Items.Count - 1 do
Settings.SetValue('removetags/tags/' + IntToStr(Idx), lbxTags.Items[Idx]);
Settings.SetValue('collisioncheck', chkCollision.Checked);
Settings.CloseKey;
btnSave.Enabled := False;
end;
procedure TfrmPackConfigs.cbDestinationChange(Sender: TObject);
begin
btnSave.Enabled := True;
fedtFile.Visible := cbDestination.Text = 'File';
chkArchiveSameName.Visible := cbDestination.Text = 'Archive';
fedtArchive.Visible := cbDestination.Text = 'Archive';
fedtArchive.Enabled := not chkArchiveSameName.Checked;
end;
procedure TfrmPackConfigs.chkArchiveSameNameChange(Sender: TObject);
begin
fedtArchive.Enabled := not chkArchiveSameName.Checked;
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.chkCollisionChange(Sender: TObject);
begin
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.chkRefactorVariablesChange(Sender: TObject);
begin
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.chkRemoveCommentsChange(Sender: TObject);
begin
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.chkRemoveLeadingSpacesChange(Sender: TObject);
begin
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.chkRemoveTagChange(Sender: TObject);
begin
lbxTags.Enabled := chkRemoveTag.Checked;
btnAddTag.Enabled := chkRemoveTag.Checked;
btnRemoveTag.Enabled := chkRemoveTag.Checked;
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.fedtArchiveChange(Sender: TObject);
begin
fedtArchive.FileName := ExtractFileNameOnly(fedtArchive.FileName);
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.fedtFileChange(Sender: TObject);
begin
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.FormShow(Sender: TObject);
begin
btnSave.Enabled := False;
end;
procedure TfrmPackConfigs.lbxConfigsSelectionChange(Sender: TObject; User: boolean);
begin
pnlConfiguration.Enabled := lbxConfigs.ItemIndex >= 0;
btnPack.Enabled := lbxConfigs.ItemIndex >= 0;
LoadConfigValues(lbxConfigs.GetSelectedText);
btnDelConf.Enabled := lbxConfigs.ItemIndex >= 0;
btnSave.Enabled := False;
end;
procedure TfrmPackConfigs.lbxTagsSelectionChange(Sender: TObject; User: boolean);
begin
btnRemoveTag.Enabled := lbxTags.ItemIndex >= 0;
end;
procedure TfrmPackConfigs.ShowResults(const ProgramName, Destination: string; OriginalSize, PackedSize: integer; Collisions: boolean;
const CollisionText, SyntaxCheckText: string);
begin
frmPackerResults := TfrmPackerResults.Create(nil);
try
frmPackerResults.Label1.Caption := 'Packed ' + ProgramName + ' into ' + Destination + '. Original size: ' + IntToStr(OriginalSize) +
', packed: ' + IntToStr(PackedSize) + '.';
if Collisions then
frmPackerResults.Memo1.Text := CollisionText;
if Length(SyntaxCheckText) > 0 then
frmPackerResults.Memo1.Text := frmPackerResults.Memo1.Text + SyntaxCheckText;
if Length(frmPackerResults.Memo1.Text) = 0 then
frmPackerResults.AdjustNoCollisions;
frmPackerResults.ShowModal;
finally
frmPackerResults.Free;
end;
end;
procedure TfrmPackConfigs.btnNewConfClick(Sender: TObject);
var
New: string;
begin
New := '';
if InputQuery('New configuration', 'Name:', New) then
if lbxConfigs.Items.IndexOf(New) >= 0 then
MessageDlg('New configuration', 'A configuration by that name already exists.', mtError, [mbOK], 0)
else
begin
lbxConfigs.AddItem(New, nil);
lbxConfigs.ItemIndex := lbxConfigs.Items.IndexOf(New);
lbxConfigsSelectionChange(lbxConfigs, False);
btnSaveClick(btnSave);
btnPack.Enabled := True;
end;
end;
procedure TfrmPackConfigs.btnDelConfClick(Sender: TObject);
begin
if MessageDlg('Program packer', 'Are you sure you want to delete this configuration?',
mtConfirmation, [mbYes, mbNo], 0) = mrYes then
begin
Settings.DeleteValue('packerconfigs/' + lbxConfigs.GetSelectedText);
lbxConfigs.Items.Delete(lbxConfigs.ItemIndex);
lbxConfigs.ItemIndex := lbxConfigs.Items.Count - 1;
lbxConfigsSelectionChange(lbxConfigs, False);
end;
end;
procedure TfrmPackConfigs.btnAddTagClick(Sender: TObject);
var
TagName: string;
begin
TagName := '';
if InputQuery('Remove lines after', 'Tag name', TagName) then
if AnsiStartsStr('//', TagName) then
lbxTags.AddItem(RightStr(TagName, length(TagName) - 2), nil)
else
lbxTags.AddItem(TagName, nil);
lbxTagsSelectionChange(lbxTags, False);
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.btnPackClick(Sender: TObject);
var
Code: TStrings;
rc, rts: boolean;
OriginalSize, PackedSize: integer;
DestinationStr, Aux: string;
CollisionCheckText, SyntaxCheckText: string;
begin
Cursor := crHourGlass;
try
try
Code := TEditorFile(frmMain.pcMDIFaker.ActivePage).Editor.Lines;
rc := chkRemoveComments.Checked;
rts := chkRemoveLeadingSpaces.Checked;
if cbDestination.Text = 'Archive' then
begin
if chkArchiveSameName.Checked then
Aux := ExtractFileNameOnly(TEditorFile(frmMain.pcMDIFaker.ActivePage).FileName)
else
Aux := ExtractFileNameOnly(fedtArchive.FileName);
PackToArchive(Code, Aux, rc, rts, lbxTags.Items, OriginalSize, PackedSize);
DestinationStr := 'archive (' + Aux + ')';
end
else if cbDestination.Text = 'Clipboard' then
begin
PackToClipboard(Code, rc, rts, lbxTags.Items, OriginalSize, PackedSize);
DestinationStr := 'clipboard';
end
else if cbDestination.Text = 'File' then
begin
PackToFile(Code, fedtFile.Text, rc, rts, lbxTags.Items, OriginalSize, PackedSize);
DestinationStr := fedtFile.Text;
end
else if cbDestination.Text = 'Sub-assembly' then
begin
end
else if cbDestination.Text = 'Vessel' then
begin
end;
CollisionCheckText := '';
if chkCollision.Checked then
RunDeepCollisionScan(Code, TEditorFile(frmMain.pcMDIFaker.ActivePage).FileName, CollisionCheckText);
SyntaxCheck(SyntaxCheckText);
ShowResults(ExtractFileNameOnly(TEditorFile(frmMain.pcMDIFaker.ActivePage).FileName), DestinationStr,
OriginalSize, PackedSize, chkCollision.Checked, CollisionCheckText, SyntaxCheckText);
except
on e: Exception do
MessageDlg('Program Packer', E.Message, mtError, [mbOk], 0);
end;
finally
Cursor := crDefault;
end;
end;
procedure TfrmPackConfigs.btnRemoveTagClick(Sender: TObject);
begin
lbxTags.Items.Delete(lbxTags.ItemIndex);
lbxTagsSelectionChange(lbxTags, False);
btnSave.Enabled := True;
end;
procedure TfrmPackConfigs.LoadConfigValues(const ConfigName: string);
var
Tags: TStringList;
xTag: string;
begin
Settings.OpenKey('packerconfigs/' + ConfigName, True);
cbDestination.ItemIndex := cbDestination.Items.IndexOf(Settings.GetValue('destination/type', 'Archive'));
cbDestinationChange(cbDestination);
chkRemoveComments.Checked := Settings.GetValue('removecomments', False);
chkRemoveLeadingSpaces.Checked := Settings.GetValue('removetrailingspaces', True);
if Settings.GetValue('destination/type', 'Archive') = 'Archive' then
begin
chkArchiveSameName.Checked := Settings.GetValue('destination/Archive/samename', False);
fedtArchive.FileName := Settings.GetValue('destination/Archive/filename', '');
fedtArchive.Enabled := not chkArchiveSameName.Checked;
fedtArchive.Visible := True;
end
else if Settings.GetValue('destination/type', 'Archive') = 'File' then
begin
fedtFile.FileName := Settings.GetValue('destination/File/filename', '');
fedtFile.Visible := True;
fedtFile.Enabled := True;
end;
chkRemoveTag.Checked := Settings.GetValue('removetags/enabled', False);
Tags := TStringList.Create;
Settings.EnumValues('removetags/tags', Tags);
for xTag in Tags do
lbxTags.Items.Add(Settings.GetValue('removetags/tags/' + xTag, ''));
chkRemoveTagChange(chkRemoveTag);
chkCollision.Checked := Settings.GetValue('collisioncheck', False);
Settings.CloseKey;
end;
procedure TfrmPackConfigs.LoadConfigs;
var
Configs: TStrings;
Config: string;
begin
Configs := TStringList.Create;
Settings.EnumSubKeys('/packerconfigs/', Configs);
for Config in Configs do
lbxConfigs.AddItem(Config, nil);
if Configs.Count > 0 then
begin
lbxConfigs.ItemIndex := 0;
LoadConfigValues(lbxConfigs.Items[0]);
btnSave.Enabled := False;
end;
fedtArchive.InitialDir := Settings.GetValue('kspfolder', '') + PathDelim + 'Plugins' + PathDelim +
'PluginData' + PathDelim + 'Archive';
end;
end.