How to add Auto Updating to Your Script by putonajonny
This is going to be a very quick nice little tutorial on how to add Auto Updating to your script.
Step 1.
Step 1 is to add something to check whether the person is using the most up to date version of your script, if you already have this move on to step 2. The best way is just to have a procedure called "CheckVersion" or something similar and call it at the start of your script. First of all you will need to create a page that stores the most up to date version number of your script. The best site for this I think is pastehtml.com it involves no sign up and allows very quick editing later on.
When you open the page you will be presented with this:
Enter the version in the big box, select "Plain Text" and click "Publish Page", on the next page click publish again and then you will be presented with something like:
Keep a note somewhere of that link, you will need it in a second.
Now for the coding part, you need to tell your script what version it is so that it can compare the downloaded page with something, so at the top of your script have something like:
Then we need to download the page we made before, for comparison we will save it to a variable first of all. Therefore have something like:
To download the page we are going to use
Simba Code:
function GetPage(url : String) : String;
so therefore you would put:
Simba Code:
Ver := GetPage('http://pastehtml.com/view/birjk6g28.txt');
Remember to have the URL in 's because it is a string.
You have now stored the most up to date version into "Ver" so if you did:
you should output:
Code:
Compiled successfully in 22 ms.
5
Successfully executed.
Now we have our constant and our variable, we will now use if statements to choose what to do next.
First of all the page may not download properly, so you might want to write something like:
Simba Code:
If(Ver = '') then
begin
WriteLn('There was an error connecting to the Update Server so I will assume that you have the most up to date Version')
Exit;
end;
That way if the site you are using has server issues the script usage isn't interrupted.
The next case is if they are using the most up to date version:
Simba Code:
If IntToStr(Version) = Ver then
begin
WriteLn('You are using the most up to date version -Thank you');
Exit;
end;
This is what happens if they are using the most up to date version, they just carry on using the script.
What about if you are making the next version of the script, you don't want to have to disable the version checker, so the next case is if the version written in the script is greater than the version stored on the computer:
Simba Code:
If Version > StrToInt(Ver) then
begin
ClearDebug;
WriteLn('**You seem to be useing some sort of beta version, be careful')
end else
begin
UpdateScript;
end;
This used an "end else" function, which you should know about by now so I won't go into. It then calls the "UpdateScript;" procedure if none of the above were true, so Step 2 is writing that Procedure.
Step 2.
This step is where we write what we want the script to do if we have detected that the person is not using the most up to date version. For this I use a new procedure:
Simba Code:
Procedure UpdateScript;
begin
end;
First thing we want to do is to check whether the new version of the script is already saved on their computer, and they are just not using it. For this we use the "FileExists" function with something like:
Simba Code:
If(FileExists('C:\Simba\Scripts\YourScript v'+Ver+'.simba')) then
begin
ClearDebug;
WriteLn('You are trying to run an older version of this script even though you have the newer version')
WriteLn('It is stored at "C:\Simba\Scripts\YourScript v'+Ver+'.simba"')
TerminateScript;
end;
This checks whether there is a file with the name YourScript v'+Ver+'.simba (e.g. YourScript v5.simba) stored at C:\Simba\Scripts\. If there is it doesn't need to download the script so it ends and instructs the user where the file is.
Next I always check that that directory exists, if they have Simba it probably should but just as a fail safe I do:
Simba Code:
If(Not DirectoryExists('C:\Simba\Scripts')) then
CreateDirectory('C:\Simba\Scripts');
Next we need to download the new script, it therefore needs to be hosted on a website somewhere, see my tutorial in step 1 about http://pastehtml.com. You just need a text file hosted somewhere which you can edit later to add the new version of the script to. For the sake of this tutorial I am going to use http://mysite.com/myscript.txt.
You will need to temporarily save the script to a Variable so have something like:
This can be a local variable.
Then save it to the string using:
Simba Code:
NewScript := GetPage('http://mysite.com/myscript.txt')
We are then going to use 2 functions for creating and writing the file:
Simba Code:
function CreateFile(path : string) : longint
function WriteFileString(FileNum : LongInt; s : String) : Boolean
I then use them as a part of an If statement:
Simba Code:
If ((CreateFile('C:\Simba\Scripts\Merchenting v'+Ver+'.simba') = -1) or (not WriteFileString(0, NewScript))) then
(CreateFile returns -1 if it fails) It asks for the file number not the file path, since this is the first file we opened it is file 0.
If this happens I tell it to:
Simba Code:
begin
ClearDebug;
WriteLn('Sorry I was not able to auto update this script, you can update it manually')
WriteLn('Opening the web page for you to update')
OpenWebPage('http://villavu.com/yourthread');
TerminateScript;
end;
You must remember to close the file using File number 0 for the reasons I explained above.
One final check I always do:
Simba Code:
If((FileExists('C:\Simba\Scripts\YourScrupt v'+Ver+'.simba')) and Suc) then
begin
ClearDebug;
WriteLn('Updated The Script, new version stored at "C:\Simba\Scripts\Merchenting v'+Ver+'.simba"')
TerminateScript;
end
I shouldn't need to explain what that does.
If you have any suggestions for this tutorial then let me know, you can see it in action at my Merchenting Script
-putonajonny
exampled from my script:
Simba Code:
Procedure UpdateScript;
Var NewScript : String; Suc : Boolean;
begin
If(FileExists('C:\Simba\Scripts\Merchenting v'+Ver+'.simba')) then
begin
ClearDebug;
WriteLn('You are trying to run an older version of this script even though you have the newer version')
WriteLn('It is stored at "C:\Simba\Scripts\Merchenting v'+Ver+'.simba"')
TerminateScript;
end;
If(Not DirectoryExists('C:\Simba\Scripts')) then
CreateDirectory('C:\Simba\Scripts');
NewScript := GetPage('http://pastehtml.com/view/bimye4g26.txt')
If ((CreateFile('C:\Simba\Scripts\Merchenting v'+Ver+'.simba') = -1) or (not WriteFileString(0, NewScript))) then
Suc := False
else
Suc := True;
WriteLn(FileSize(0));
CloseFile(0);
If((FileExists('C:\Simba\Scripts\Merchenting v'+Ver+'.simba')) and Suc) then
begin
ClearDebug;
WriteLn('There is a new version of the script, updating...')
Wait(500+Random(4000));
WriteLn('Updated The Script, new version stored at "C:\Simba\Scripts\Merchenting v'+Ver+'.simba"')
TerminateScript;
end else
begin
ClearDebug;
WriteLn('Sorry I was not able to auto update this script, you can update it manually')
WriteLn('Opening the web page for you to update')
OpenWebPage('http://pastehtml.com/view/biaj2biir.html');
TerminateScript;
end;
end;
Procedure CheckVersion;
Begin
If(WriteLines) then
WriteLn('Checking if you have the most up to date version');
Ver := GetPage('http://pastehtml.com/view/biaj1s82r.txt');
If(Ver = '') then
begin
WriteLn('There was an error connecting to the Update Server so I will assume that you have the most up to date Version')
Exit;
end;
If IntToStr(Version) = Ver then
begin
If(WriteLines) then
WriteLn('You are using the most up to date version -Thank you');
end else
begin
If Version > StrToInt(Ver) then
begin
ClearDebug;
WriteLn('**You seem to be useing some sort of beta version, be careful')
end else
begin
UpdateScript;
end;
end;
end;
Step 3
When you have updated your script you just need to edit your two hosted pages: to do this using pastehtml just click on: