Archive for the ‘Snippets’ tag

Opening files and URLs

URL

URL

I don’t know about you, but I really hate those little bits of code that you will use in every single game but not during the actual development of it. As far as I’m concerned, even if not the most noble usage of the “reuse your code” rule, getting rid of these pesky little pieces of code is the single most useful usage of object oriented programming, not because I read it somewhere but because I’ll never have to worry about it again!

Opening files and URLs are two examples those pesky bits of code and to me both are perfect candidates to our framework Toolkit class! So here’s the code of the two static functions that were created to deal with it.

/*Opens an external file. Upon completing the task, callbacks to the
 *given function.*/
public static function OpenFile(filename:String, callback:Function)
{
	var ldr:URLLoader = new URLLoader();
 
	ldr.load(new URLRequest(filename));
	ldr.addEventListener(Event.COMPLETE, callback);
}
 
/*Opens a URL on a new window.*/
public static function OpenURL(url:String)
{
	var urlReq:URLRequest = new URLRequest(url);
	navigateToURL(urlReq, "_blank");
}

Posted: March 11th, 2009
at 12:00am by Vlad

Tagged with ,


Categories: The code of VGS

Comments: No comments


Site lock and domain name

If you are selling licenses of your games, site locking is something that you just have to know. One of the mistakes I did early on was to simply use some piece of code I found on the web. It was something like this:

if(this.root.loaderInfo.url.indexOf("domain.com") != -1)
    trace("Good!"); // It's in the right domain
else
    trace("Bad!"); // It's in the wrong domain

What this does is to get the index (aka position) of the string “domain.com” from the string that is returned by the loaderInfo.url. If it returns -1 then the string “domain.com” is not present in the loaderInfo.url.

This solution is fine, but it presentes two problems.

First, if someone really wants to use your site-locked game, they can! They just have to find a way of putting the “domain.com” string somewhere in the path to your swf since this code does not compare the domain name but rather the presence of a string on another string.

The second problem is that you don’t know the domain where the game is present. I need that information for other evil schemes of mine, so this was the perfect reason to write a small tool that would fetch me the domain name.

So I wrote this simple static function that lies inside a class Toolkit -name I shamelessly stolen from a chat with Andrew from Epic Shadow- and that simply returns the root domain and the top level domain, separated by a dot from the loaderInfo.url.

/*Returns the name of the domain without subdomain, e.g. vortixgames.com
 * instead of www.vortixgames.com.*/
public static function Domain(root:MovieClip):String
{
	/* Splits the full path into an array of strings separated by "/"
	 * and gets the index 2 value which is the full domain name.*/
	var currentDomain:String = root.loaderInfo.url.split("/")[2];
 
	/* Splits the result into an array of strings separated by "." and
	 * checks the length of the array. If the array has a length of 3
	 * the domain contains a subdomain, for instance "www" and it must
	 * be discarded. If the length is 2 then, there's no subdomain
	 * present. */
 
	var fqdn:Array = currentDomain.split("."); // FQDN in array form
 
	var rdi:int = 1; // Root Domain Index defaults to 1
	var tli:int = 2; // Top Level Index defaults to 2
 
	if (fqdn.length == 2)
	{
		rdi--;
		tli--;
	}
 
	return fqdn[rdi] + "." + fqdn[tli];
}

The only issue I have with this function is that I have to pass the root as a parameter to the function. Any idea on how I could solve this otherwise is most welcome.

Posted: March 2nd, 2009
at 12:00am by Vlad

Tagged with , ,


Categories: The code of VGS

Comments: 3 comments