Thursday, June 2, 2011

Character Select Part 1 - Unity3d

So I took a small break because of family issues, but I'm back today to inform everyone that I have completed the Character Select code. You can find it through the github link on the right. Today I'll go ahead and explain it.

There is at least one problem we will be looking to prevent. The character ID that is sent actually needs to belong to the player attempting to log in. This will prevent hackers from looking into the packet sent and modifying it to access any character in the game. We will cover this in the server side, but it is a heads up.

So lets get started into the Unity3d side. Again I'm doing some really simple GUI stuff. My intention isn't to do GUI but to make it generic so you can see how its done and apply it to your own game.

If you pull down my code, you will see that I renamed the CharacterSelectHandler in unity3d to CharacterListHandler. I made this change to keep the functionality of the handler consistent with its name.

When we left off last time we were able to create a character and get back a list of our characters. I decided to make a simple and ugly UI that will allow us to select a character and pass that message to the extension. In the OnGUI function of CharacterSelectGUI I replaced the empty foreach loop with the following:


            GUI.Box(new Rect(300, 10, 100, 300), "Classes");


            int yPos = 50;


            foreach (Character character in CharacterList.characterList)
            {


                if (GUI.Button(new Rect(310, yPos, 80, 50), character.Name))
                {
                    ISFSObject data = new SFSObject();
                    data.PutLong("characterID", character.ID);
                    Debug.Log("Logging in as character ID: " + character.ID);
                    ExtensionRequest request = new ExtensionRequest("selectCharacter", data);
                    smartFox.Send(request);
                }


                yPos += 60;
            }


This basically creates a simple box with the text "classe"s and then creates a button for each character we received from the server. When you click on the button, it takes the character id and passes it as a request using the name selectCharacter. This is the code where a hacker could insert any id. A further change to consider when this gets rewritten is to change the ID from a long to a GUID. A GUID would help because then an attacker doesn't know what other IDs are in the system and data farmers can't figure out how many characters are in the game.

Next we need to build a new handler for receiving the response from the server on whether they can log in or if they chose a bad character. We will call it CharacterSelectedHandler:


using System;
using Sfs2X.Entities.Data;
using System.Collections.Generic;
using UnityEngine;


public class CharacterSelectedHandler : IMessageHandler
{

    public override void OnHandleMessage(ISFSObject data)
    {
        Debug.Log("Character selected successfully, entering the game");
    }
}


Simple. Now lets plug it in to the CharacterSelectGUI code. We need to create an instance of it, instantiate it, and set it to receive the characterSelected message we will get back from the server and then add the callback that will push us into the game scene. At the same time we will use another copy of our error handler to handle any error messages passed back from the server.


    CharacterSelectedHandler CharacterSelected;
    ErrorHandler errorHandler;


Inside the Awake function:


            CharacterSelected = new CharacterSelectedHandler();
            errorHandler = new ErrorHandler();


            handlers.Add("characterSelected", CharacterSelected.HandleMessage);
            handlers.Add("error", errorHandler.HandleMessage);

            CharacterSelected.afterMessageRecieved += AfterCharacterSelected;

And lastly we will add the AfterCharacterSelected function:
    public void AfterCharacterSelected()
    {
        Debug.Log("going to main game");
        UnregisterSFSSceneCallbacks();
        Application.LoadLevel("Game");
    }

And there we have it. Remember you need to create a scene called Game and add it to the build options so the game can load the scene. This completes the Unity3d portion of our Character Select code and brings us one step closer to the end of this tutorial series. Next I will cover the Java portion and how we keep players from loading other players' characters and send us into our game.

No comments: