SRL Archive - RS3 Help

Cant create file


rj

2015-03-14 16:54:23

I'm trying to create a file while a form is open - apparently not possible with lape. So I tried to work around it by closing and freeing the form, and it still does not work. Is there a way to create a file while the form is still open? Why does it not write when I have closed the form (through code)?


{$I SRL-6/SRL.Simba}
{$I TFile.Simba}

const
paintColor = 65280;

var
mainForm:TForm;
clickEveryButton:TEdit;
startButton:TButton;
stallOptions:TComboBox;
labels:array of TLabel;
pressed:boolean;

procedure createForm();forward;

function isLoggedIn():boolean;override;
begin
result := true;
end;

procedure reShow();native;
begin
mainForm.showModal();
end;

procedure startScript(sender:TObject); native;
var
error, st:string;
f:Tfile;
begin
try
mainForm.close();
mainForm.Free();
f.create('C:\Simba\', 'passwords', 'txt');
createForm();
mainForm.showModal();
except
writeln('Unable to start GUI');
finally
mainForm.Free();
end;
//sync(reShow);
exit;
st := stallOptions.getCaption;
case st of
'Jewelry' : writeln('1');
'Banana' : writeln('2');
'Crafting': writeln('3');
'Magic' : writeln('4');
'Scimitar': writeln('5');
else
error := 'You must select a stall to steal from';
end;
try
writeln('test');
except
error := 'You must enter a number in the click every box';
end;
if (length(error) > 1) then
begin
messageBox(error, 'Error', 8);
exit();
end else
begin
mainForm.close();
pressed := true;
end;
end;

procedure createForm();
var
i:integer;
pos:TPointArray;
captions:TStringArray;
begin
mainForm.Init(nil);
with (mainForm) do
begin
setWidth(400);
setHeight(300);
setPosition(poScreenCenter);
setBorderStyle(bsSingle);
setShowHint(true);
setCaption('Stall theiver');
end;
clickEveryButton.init(mainForm);
with (clickEveryButton) do
begin
setParent(mainForm);
setBounds(69, 8, 140, 20);
setCaption('C:\Simba\passwords.txt');
setHint('Time (ms)');
end;
startButton.init(mainForm);
with (startButton) do
begin
setParent(mainForm);
setBounds(8, 100, 235, 30);
setCaption('Start Script');
setHint('Start script');
setOnClick(startScript);
end;
stallOptions.init(mainForm);
with (stallOptions) do
begin
setParent(mainForm);
setBounds(69, 48, 100, 30);
setCaption('Jewelry');
setHint('Steal from');
addItem('Jewelry', stallOptions);
addItem('Banana', stallOptions);
addItem('Crafting', stallOptions);
addItem('Magic', stallOptions);
addItem('Scimitar', stallOptions);
end;
captions := ['Path', 'Steal from'];
pos := [Point(5, 10), Point(5, 50)];
setLength(labels, length(captions));
for i := 0 to high(captions) do
begin
labels[i].Init(mainForm);
with labels[i] do
begin
setParent(mainForm);
setBounds(pos[i].x, pos[i].y, 10, 10);
setCaption(captions[i]);
end;
end;
end;

procedure initForm();native;
begin
createForm();
try
mainForm.showModal();
except
writeln('Unable to start GUI');
finally
mainForm.Free();
end;
end;

procedure showForm();
begin
sync(initForm);
end;

begin
setUpsrl;

showForm();
if (not pressed) then
writeln('did not press start');
end.

Brandon

2015-03-14 19:38:57

From: rj
I'm trying to create a file while a form is open - apparently not possible with lape.


Not entirely true..


Change your code to use the below.. It uses getTClient().getMFiles().. See: https://github.com/MerlijnWajer/Simba/blob/master/Units/MMLAddon/PSInc/Wrappers/file.inc

[SPOILER=TFile]


type
TFile = record
path, name, extension, data, realPath:string;
end;

function TFile.exists():boolean;
var
s:TStringArray;
i:Integer;
begin
s := getTClient().getMFiles().GetFiles(self.path, self.extension);
for i := 0 to high(s) do
if (s[i] = (self.name +'.' + self.extension)) then
exit(true);
end;

procedure TFile.recall(const filePath, fileName, fileExtension:string);
begin
self.path := path;
self.name := name;
self.extension := fileExtension;
self.realPath := filePath + fileName + '.' + fileExtension;
end;

function TFile.open():integer;
begin
result := getTClient().getMFiles().openFile(self.realPath, true);
end;

function TFile.readString(const strLength:integer):string;
var
tmpFile:integer;
str:string;
begin
tmpFile := self.open();
try
getTClient().getMFiles().ReadFileString(tmpFile, str, strLength);
finally
result := str;
getTClient().getMFiles().closeFile(tmpFile);
end;
end;

function TFile.readString():string; overload;
begin
result := self.readString(50000);
end;

function TFile.rewrite():integer;
begin
try
result := getTClient().getMFiles().rewriteFile(self.realPath, true);
except
end;
end;

function TFile.setText(const str:string):boolean;
var
tmpFile:integer;
begin
if (not self.exists()) then
exit();
try
tmpFile := self.rewrite();
getTClient().getMFiles().writeFileString(tmpFile, str);
getTClient().getMFiles().closeFile(tmpFile);
except
end;
end;

function TFile.parseData(const str1, str2:string):string;
var
str:string;
begin
result := between(str1, str2, self.readString());
end;

function TFile.delete():boolean;
begin
result := getTClient().getMFiles().deleteFile(self.realPath);
end;

procedure TFile.create(const filePath, fileName, fileExtension:string);
var
tmpFile:integer;
begin
if self.exists() then
begin
self.recall(filePath, fileName, fileExtension);
exit();
end;
try
self.path := filePath;
self.name := fileName;
self.extension := fileExtension;
self.realPath := filePath + fileName + '.' + fileExtension;
tmpFile := getTClient().getMFiles().createFile(self.realPath);
getTClient().getMFiles().closeFile(tmpFile);
except
end;
end;

[/SPOILER]

Same answer as discussed here: https://villavu.com/forum/showthread.php?t=112012

Olly

2015-03-14 20:09:17

Or use a TStringList. They're quite nice. :)

rj

2015-03-14 20:33:48

From: Brandon
…



TY


From: Olly
Or use a TStringList. They're quite nice. :)



Explain never heard of that

Brandon

2015-03-14 22:07:28

From: rj
TY

Explain never heard of that



I think he means:


var
TSL: TStringList;
begin
TSL.Init();
TSL.Add('meh');
TSL.Add('foo');
TSL.SaveToFile('blah');
TSL.Free();


TSL.Init();
TSL.LoadFromFile('blah');
TSL.getValues(0);
TSL.Free();
end.

rj

2015-03-17 21:56:15

From: Brandon
I think he means:


var
TSL: TStringList;
begin
TSL.Init();
TSL.Add('meh');
TSL.Add('foo');
TSL.SaveToFile('blah');
TSL.Free();


TSL.Init();
TSL.LoadFromFile('blah');
TSL.getValues(0);
TSL.Free();
end.


these work with a form open right