PDA

View Full Version : How to add Auto Updating to Your Script



putonajonny
12-27-2011, 11:41 PM
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 (http://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:
http://putonajonnyscripts.hostoi.com/Updating%20Tutorial/1.png
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:
http://putonajonnyscripts.hostoi.com/Updating%20Tutorial/2.png
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:
Const
Version = 5;
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:
Var
Ver : String;
To download the page we are going to use function GetPage(url : String) : String;
so therefore you would put:
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:
WriteLn(Ver)
you should output:

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:
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:
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:
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:
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:
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:
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 (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:
Var
NewScript : String;This can be a local variable.
Then save it to the string using:
NewScript := GetPage('http://mysite.com/myscript.txt')
We are then going to use 2 functions for creating and writing the file:
function CreateFile(path : string) : longint
function WriteFileString(FileNum : LongInt; s : String) : Boolean
I then use them as a part of an If statement:
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: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 CloseFile(0);File number 0 for the reasons I explained above.
One final check I always do:
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 (http://villavu.com/forum/showthread.php?p=870151)

-putonajonny


exampled from my script:
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:
http://putonajonnyscripts.hostoi.com/Updating%20Tutorial/3.png
http://putonajonnyscripts.hostoi.com/Updating%20Tutorial/4.png

Kyle Undefined
12-27-2011, 11:48 PM
Awesome tutorial! I've always done this in my scripts.

putonajonny
12-27-2011, 11:54 PM
Awesome tutorial! I've always done this in my scripts.

What do you think about putting it in "Tutorial for Intermediates" I have no idea of the criteria for each, it isn't a very hard thing to do at all but it isn't the sort of thing that people should/would put on their scripts first of all, that's why I settled on this one.

Kyle Undefined
12-28-2011, 12:00 AM
Seems like a good place to put it to me.

tls
12-28-2011, 12:12 AM
Instead of using hard-coded directories, I suggest you use the Setting Constants:
ScriptPath
AppPath
FontPath
IncludePath
PluginPath

It will make it usable by people who install simba somewhere else.

Swatarianess
12-28-2011, 01:22 PM
Looks awesome :D maybe it should use same path as where the script is? or in Simba i dunno Function or procedures to do it but its somewhere i think its what mormonman said ^^

JuKKa
12-28-2011, 01:40 PM
If you overwrite the script that is ran in simba, if i remember correctly simba will ask to open the new one. Good tutorial, although i knew all this :P

Daniel
12-28-2011, 01:56 PM
C:\Simba\ isn't the only path people install Simba too. Also, give the user a choice to upgrade or not. Plus, it's better to just link to the actual script thread (for security concerns, etc). :)

Good tutorial though. :)

Main
12-28-2011, 02:06 PM
Holy fuck this is sexy!

Thanks for the tut!

Shuttleu
12-28-2011, 02:24 PM
Can I suggest using git? Much easier to manage and you can easily revert

Actually, I will make a tut for that

~shut

Flight
12-28-2011, 02:37 PM
Nice tutorial. :)

The Killer
01-06-2013, 01:22 PM
Ok, I'v been trying to follow this tutorial but I can't seem to grab the version. I'v done this:
program new;
Var
Ver: String;

begin
Ver := GetPage('https://raw.github.com/SRLTheKiller/Aio-Superheater/master/Version.txt');
Writeln(ver);
end.
to test but I just get a blank line. Why is this happening? the webpage isn't blank and it always loads for me in browser.

DannyRS
01-06-2013, 01:26 PM
Ok, I'v been trying to follow this tutorial but I can't seem to grab the version. I'v done this:
program new;
Var
Ver: String;

begin
Ver := GetPage('https://raw.github.com/SRLTheKiller/Aio-Superheater/master/Version.txt');
Writeln(ver);
end.
to test but I just get a blank line. Why is this happening? the webpage isn't blank and it always loads for me in browser.

No idea how this stuff even works, but i think if i remember the github site blocks you from coppying in "raw" pages, maybe check what other peopes URL's look like?

The Killer
01-06-2013, 01:38 PM
No idea how this stuff even works, but i think if i remember the github site blocks you from coppying in "raw" pages, maybe check what other peopes URL's look like?

o thanks :d, ashaman and superuser both use google code for their scripts so I'll try that