OSP Code Optimisation Module Version Checker

Currently viewing this thread:

Module Version Checker

Originally developed for the Redwall Vermin Resurgence module, I decided to make this OSP, since it's a rather useful tool and there currently is nothing of the sort in OSP format.

In short, this allows the player to do a simple check, by using little MS code and a PHP file and .txt file somewhere stored on the internet. This can be used in single-player and multi-player modules. Only requirement is that the player is connected to the internet.

More detailed on how this works, a script is called that sends a message to a PHP file on your server. In this message we will send the current version number of the player's module, and compare this number with the most recent version number that is stored in a .txt file on your server. The result is then send back to the player and will display a message with information, showing if the module is either up to date, or needs to be updated.

Here are some screenshots of how it works, on the left we have the result when the module is up to date, in the middle when it's not and on the right when something is not working correctly, for example no internet connection can be made or with the server.

       
(Click on the thumbmails to see the larger version)


If you are already familiar with webhosting and FTP, you can safely ignore the spoiler below. You will need an FTP client though and some webhosting space in order to make this system work.

As you have read here, you will need some internet storage to make this work. Now you can acquire this by paying a yearly sum of $ 50 for a host, but there are plenty of free alternatives. Maybe your clan has some free space on their server and you can make use of it, or a friend hosts a website or something of the sort.

If this is not the case, you can register a free account for some free hosting space. Use Google, 'free webhosting' usually gives results, or use this site to find a suitable hoster: http://www.free-webhosts.com.

Once you got your hosting up and running, you will need an FTP client. You can use FileZilla for this. I mention FileZila specifically because it's one of the most popular clients, and it's free.

When you have registered with a web hoster and installed FileZilla successfully, you should try to connect to the FTP of your host. Often the hoster will send you an email with the FTP information. Once you got that, connect with FileZilla to the host.

May you experience problems with connecting, please see the FileZilla documentation.

Once you have your webhosting and FileZilla set up, we can start uploading our PHP and text file. Open your favourite text editor, Notepad ++, Notepad, Sublime, Gedit, everything will work.

Paste the following codes into a new file, and save it as read.php.
Code:
<?php
header_remove();
function set_content_length($output)
{
  header("Content-Length: ".strlen($output));
  return $output;
}
ob_start("set_content_length");

$mrv_file		= 'mrv.txt';

$number			= $_GET['number'];

$fh 			= fopen		($mrv_file, 'r');
$mrv 			= fread		($fh, filesize($mrv_file));
fclose($fh);

if 		 ( $mrv == 	$number )	{
$vc	=1;
echo $vc;
echo "|";
echo $mrv;
} elseif 	( $mrv > 	$number)	{
$vc	=2;
echo $vc;
echo "|";
echo $mrv;
} else						{
$vc	=3;
echo $vc;
echo "|";
echo $mrv;
}

?>
Now make the mrv.txt file, we will be using this file from where the most recent version number will be gotten from. Keep a thing in mind though, the M&B module system can only compare registers (reg) and not strings! This means that you will always need to compare numbers with each other. That means you can only use numbers in your version number! So no dots, comma's, letters or whatsoever. Later I will explain how we can apply a work-around.

For this example we will use version 1:
Code:
1
The only thing you need to put in the mrv.txt is the number, so in this case 1.

Now you can upload both files to your webhost.

Now we will implant the MS codes in order to make this work.

module_constants
The number that will be send to the PHP file, will be defined in module_constants. You can place this at the end of module_constants file.
Code:
version_number = 1
module_scripts
We will be also needing a script that will send the message to the PHP file. Place this script at the end of module_scripts.
Code:
# script_version_check
	("version_check",
	[
	(assign, reg0, version_number),
	
	(str_store_string, s1, "@http://*your location of the file*/read.php?number={reg0}"),
	(send_message_to_url, s1),
	(display_log_message, s1),		# So to see if you really send your number
	]),
Also you will need to edit the script_game_receive_url_response. Find it in module_scripts and place this block in the script.
Code:
	(assign, reg20, build_number),	
	
	(display_message, "@Your version: {reg20}"),
	(display_message, "@Newest version: {reg1}"),
	
	(try_begin),
	  (eq, reg0, 1),
	  (display_message, "@Your version is up to date!", 0x01DF3A),
	(else_try),
	  (eq, reg0, 2),
	  (display_message, "@Your module is out of date, we recommend you to update.", 0xDF0101),	
	(else_try),
	  (eq, reg0, 3),
	  (display_message, "@Something is wrong...", 0xFFBF00),
	(try_end),
module_game_menus
Find the camp menu in module_game_menus, under the camp menu. Searching for "camp" also tends to work. Then paste this block in the menu.
Code:
       ("send_test_message",[],"Do a version check.",
       [         
         (call_script, "script_version_check"),         
       ]), 
A script can be called from practically everywhere in the MS. For example when the module starts up, or when the player joins a server and so on.

And theeeeeeeeeeen, when you have compiled the module and have everything right, you should get the result of this screenshot:



So, whenever your module has a new release, you can simply edit mrv.txt with your FTP and edit that number, and when the player checks for the new version via his camp menu, he/she will get a message that a new version has been released, with the result you can see here:



The Workaround

If your module version is for example looking like this, 1.134, or 0.02a, you can still make use of this. If we would be using 1.134, you will take the digits 134 as the ones you will be sending to your PHP file and the 1. later in displaying the message. This can be easily done by editing your version_number in module_constants to 134. After that you go to the script_game_receive_url_response and add this command:
(str_store_string, s20, "@1."),

Then edit the display_messages,
(display_message, "@Your version: {s20}.{reg20}"),
(display_message, "@Newest version: {s20}.{reg1}"),

Then your output is:
Your version: 1.134
Newest version: 1.{whatever number you defined in mrv.text}

If you would be using 0.02a for example, you would be using the 2 as your version check number. Then it can look like this:
(str_store_string, s20, "@0.0"),
(str_store_string, s21, "@a"),

(display_message, "@Your version: {s20}{reg20}{s21}"),
(display_message, "@Newest version: {s20}{reg1}{s21}"),


Some credit is here in place,
- First of I like to thank Jerke for his help, I am no hero with PHP, but thanks to him I could make this work.
- A credit goes to the team of c-RPG, for their method of reading data from Warband in PHP files. The header is their work.
 

Lumos

Archduke
WBWF&SNWM&BVC
Very nice idea! I, however, won't be making use of this, as I've put automatic version checks into the TBS Launcher, along with some other stuff like updating mechanics.
Awesome, anyways. Keep up the good work!
 
Top Bottom