Silverlight Dependency Property Snippet

I created this snippet for Dependency Properties in Silverlight. If you are creating custom Silverlight controls you might find this snippet useful. I stored my code snippets here "My DocumentsVisual Studio 2008Code SnippetsVisual C#My Code Snippets" so all you need to do is copy the file to this location. Visual Studio should load the snippet the next time you start it up. If it does not then you can go into "Tools", "Code Snippets Manager" and load it up manually.

Now when you type "propsl" and hit tab you will get a stubbed out property like below, where tabbing through the items allows you to change the type and name of the property, set the owning class and provide a default value.

public int MyProperty
{
	get { return (int)GetValue(MyPropertyProperty); }
	set { SetValue(MyPropertyProperty, value); }
}
public static readonly DependencyProperty MyPropertyProperty = 
     DependencyProperty.Register("MyProperty", typeof(int), typeof(MyClass), 
     new PropertyMetadata(null, new PropertyChangedCallback(MyClass.OnMyPropertyPropertyChanged)));
private static void OnMyPropertyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{

Here is the code for the snippet and a link to the file:

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>propsl</Title>
            <Shortcut>propsl</Shortcut>
            <Description>Code snippet for an automatically implemented dependency property in Silverlight.</Description>
            <Author>Cameron Albert</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>type</ID>
                    <ToolTip>Property type</ToolTip>
                    <Default>int</Default>
                </Literal>
                <Literal>
                    <ID>property</ID>
                    <ToolTip>Property name</ToolTip>
                    <Default>MyProperty</Default>
                </Literal>
                <Literal>
                    <ID>ownerClass</ID>
                    <ToolTip>Owner Class</ToolTip>
                    <Default>MyClass</Default>
                </Literal>
                <Literal>
                    <ID>defaultValue</ID>
                    <ToolTip>Default Value</ToolTip>
                    <Default>null</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp">
                <![CDATA[public $type$ $property$
            {
                get { return ($type$)GetValue($property$Property); }
                set { SetValue($property$Property, value); }
            }
            public static readonly DependencyProperty $property$Property = DependencyProperty.Register("$property$", typeof($type$), typeof($ownerClass$), new PropertyMetadata($defaultValue$, new PropertyChangedCallback($ownerClass$.On$property$PropertyChanged)));
            private static void On$property$PropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
            {
            }
            $end$]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Silverlight Dependency Property Snippet

 

3/9/2009


Perenthia Armorial Version 1.0

I uploaded the Perenthia Armorial Version 1.0 to the Perenthia Alpha site tonight. The Perenthia Armorial is a way to provide external access to Perenthia information. The first version includes the Player Bar feature, where you pass a player name to the service and renders back HTML with player details.

The call to http://alpha.perenthia.com/Armorial/Armorial.ashx?n=Aldarian renders the following:

1/18/2009


Client/Server Communication Data Formats

While developing Perenthia I tried several different communication formats from sending JSON serialized objects back and forth to sending byte arrays containing mostly integers indicating what should be loaded and displayed, what commands to execute etc. What I eventually settled on was a bit of a hybrid. I've created a simple set of "tags" that can be sent to and from the server. The tags are nothing more than pipe delimited strings contained within curly braces and there are a limited number of tags that provide simplicity yet flexibility with the data that is transmitted. The tags are represented in C# by tag objects allow me to create them, query them, etc.

A simple command tag for the SAY command might look like this: {CMD|SAY|"Hello World!"}

I wrote a custom tag writer class that parses the tag objects into strings to be sent to the client and likewise a tag reader that reads the strings sent from the client and parses them into tag objects.

The client can only send commands to the server but the server sends commands, messages and objects to the client. The commands are all the same, the CMD text then the one word name of the command and then a list of arguments, messages are system, chat and tell messages but objects have a bit more information. For instance, an object tag encompasses several different types starting with a base ACTOR, a PLACE, a PLAYER and a PROP or property. The ACTOR, PLACE and PLAYER tags all define the ID, Name and Descriptions of the objects, with some additional data per type but the PROP tag defines the object id of its owner and a name/value pair. An example of a PLAYER tag with properties for x, y and z coordinates might be:

{OBJ|PLAYER|3756|"Aldarian"|"My character description..."}

{OBJ|PROP|3756|"X"|45}

{OBJ|PROP|3756|"Y"|26}

{OBJ|PROP|3756|"Z"|0}

The client can find and parse the player tag and then find the properties associated with that player instance. The way the server works now it will send the full player tag once logged in and a character is selected and then from then on out it just sends property updates.

Using this type of tag structure allows quite a bit of flexibility for the client. I could choose to write a very simple client that only displays MSG tags, much like a MUD, I could write a simple 2D interface client and display and move sprites about the map using the properties from the server, etc. Once Perenthia is up and running I will probably post some information on the tags and how to talk with the server should someone feel the need to write their own client. :)

1/5/2009


Silverlight 2 and Cross Domain Web Calls

Frank LaVigne has written a nice Silverlight 2 Cross Domain Web Proxy Utility for making any type of web calls from Silverlight 2. This a great utility if you want to serve images or outside content to your Silverlight 2 apps but do not have the ability to setup a domain policy file on the content hosts server.

3/18/2008


Silverlight 2.0

Silverlight 2.0 is available for download on silverlight.net!

3/5/2008


SQL 2005 XML Data Type, Stored Procedures and Lists

I've seen a lot of stuff out there regarding the SQL 2005 XML data type but most of it is just regurgitates the MSDN documentation. That's fine and all but what about practical uses of it? Well, I have a practical use sample. In building my persistent browser based game Perenthia I have a concept of a Place. A place is a virtual space in which objects are stored. For Perenthia the places represent the various rooms or tiles players move around on. The place or room has exits defined that allow the player to move from one place to the next. The exits are the typical directions; north, south, up, down, etc. In the database I have a Places table and a PlaceExits table. The Places table stores all the information regarding a place and the PlaceExits table stores the placeId along with a directionId and destinationId so I know what exits are available in any room and what rooms they lead to.

The simplified schema for the places would be:

 Places Tables

 In the stored procedure that retrieves the place information I use the following query snippet in the select clause:

    SELECT

        p.*,

    (
            SELECT
                e.DirectionId        AS "@directionId",
                e.DestinationId        AS "@destinationId"
            FROM
                dbo.PlaceExits e
            WHERE
                e.PlaceId = p.PlaceId
            FOR XML PATH('exit'), ROOT('exits')
        ) AS ExitsXml

    FROM dbo.Places p 

 This creates an XML fragment I can then parse in the application to fill a collection of Exits on the Place object.

When saving place information I pass XML generated from the Exits collection in a stored procedure like so:

CREATE PROCEDURE dbo.Places_SavePlace  (@PlaceId int, @ExitsXml xml)

From within the save procedure I perform an update or insert of the place data and then execute the following sql to insert and update the exits for the current place:

    -- Exits

    -- Process the existing exits first

    UPDATE
        dbo.PlaceExits
    SET
        DestinationId    = e.ex.value('(@destinationId)[1]', 'int')
    FROM
        @ExitsXml.nodes('/exits/exit') as e(ex)
    WHERE
        PlaceId = @PlaceId
        AND DirectionId = e.ex.value('(@directionId)[1]', 'tinyint')

    -- Process any new exits

    INSERT INTO dbo.PlaceExits
    (
        ObjectId, DirectionId, DestinationId
    )
    SELECT
        @PlaceId,
        e.ex.value('(@directionId)[1]', 'tinyint'),
        e.ex.value('(@destinationId)[1]', 'int')
    FROM
        @ExitsXml.nodes('/exits/exit') as e(ex)
    WHERE
        e.ex.value('(@directionId)[1]', 'tinyint') NOT IN
        (
            SELECT DirectionId FROM dbo.PlaceExits WHERE PlaceId = @PlaceId
        )

This is working pretty well and keeps me from having to loop through the exits in the application and make multiple database calls. 

11/14/2007


Engine Structure

I am structuring my PBBG game engine to be as flexible as possible in order to build various types of games. In order to do that I need to abstract out the components of the engine. Since persistent browser based games are, well, browser based, I decided to follow the normal n-tier model. I am creating a data tier, my actuall database, an application tier which is the engine and will handle client connections, authentication, commands and reading and writing to the database. On top of the application layer will reside the game layer which will be customizable libraries that will use and access the application layer. This follows along the MUD driver and MUD lib pattern where my engine will be the driver which will persist data and handle all communications and my MUD libs or games will be written in an OO fashion to take advantage of the game engine.

The engine is being written in C# and in such a way to take advantage of features of ASP.NET such as HttpModules and HttpHandlers. 

10/30/2007


.NET PBBG Engine

In between updating Perenthia and adding new features I have been pulling parts of the code base into a more generic PBBG Engine I am writing in C#. I started working on it when I upgraded the Knights of the Realm game and used parts of it for Perenthia. I am hoping to put Knights of the Realm Beta 2 on the new engine once I get it finished.

I am building the engine as generic as I can but it will incorporate a base rules set and some basic concepts. The base objects will be Avatars, Places and Things. These objects will contain the properties required to function within the rules set and all objects will derive from a base GameObject class that will provide a properties collection for creating custom properties on derived game objects.

The game will be driven by commands sent from the client. Some objects will handle the commands in the engine framework while other commands will cause events to be raised that deriving implementations can handle and provide custom execution or additional execution of the commands.

The egnine will basically be a commands/rules processor that I will hopefully be able to build a variety of games on. I have plans and ideas for several types of games and do not want to continually build the same thing over and over, hence the PBBG engine. 

10/17/2007


ASP.NET AJAX Server Controls and PBBGs

I ran into a major performance issue with the ASP.NET AJAX Server Controls while testing my persistent browser based game Perenthia. I had initially used update panels fro the various regions on the main game interface such as the player stats, map and chat window. Programming this was simple as I could do everything in the server side code and just send back the results. With the partial page rendering feature of the ASP.NET AJAX Extensions only the update panel html was sent back to the browser.

Once I moved the application into a production environment and had people on there playing and testing the web starting running out of memory, the application was consuming the server memory at an alarming rate. What was happening was that IIS would jump 1 to 2 MB of RAM for each request made by the client, that means every time someone moved on the game map 1 to 2 MB of RAM were being held and not released. I tried a bunch of different stuff from optimizing stored procedures to caching the map data but none of it helped.

I used Firebug to watch the AJAX request and response and to see if I could reduce that down some. The response was simply HTML fragments so I wasn't too worried about that but the request sent the entire ViewState up to the server with each post. The ViewState for the game page could be quite large since I was appending messages to the chat window which was a server control.

I was able to eliminate the ViewState issue with the chat window but the app was still consuming RAM and not releasing it.

I decided to try just a simple JavaScript only AJAX post using the Microsoft AJAX Client Libraries. After doing some basic tests on the chat window I decided to rebuild the game interface using only client side AJAX calls, no update panels. This has resolved the server memory issue, why, I am still not sure, must be something with the way the resources are handled in .NET. I have not encountered the slow downs or crashes from before and the app is actually running a little smoother on the front end as well.

What I settled on was a custom ASHX handler class that handles the commands from the client and returns JSON strings that the UI can then translate and use to update the interface components. I had to rebuild some of the interface components as JavaScript objects but overall it was the right choice. Not too mention that I can now use that same handler with other interfaces such as Flash or Silverlight.

In conclusion, while the AJAX Server Controls might work great for most web sites they are definitely not optimal for a persistent browser based game (PPBG).

10/1/2007


Adventures in Silverlight

I spent some time last night and today playing around in Silverlight to make a final decision on whether or not I will use it in my PBBG Perenthia. I had already started building out a Flash UI but just keep running into the same issues with Flash; the IDE sucks and I keep having to rebuild C# classes in ActionScript. So, I decided to dig a little more into Silverlight to see if using the 1.1 version would be feasible. The word from MS is that the beta 2 with a go live license should be available sometime later this year. That might work out pretty well for me and my current timeline. I have most of the server functionality for Perenthia complete but just can't find a UI combination that gives me what I want and keeps the overhead down. Anyway, I am going to spend the next few days playing around with a Silverlight UI and see if I can come up with something I can use in place of Flash.

8/29/2007


Perenthia PBBG Flash UI

I've decided to use Flash to provide the UI for my persistent browser based game Perenthia. I went back and forth between Flash and AJAX and even looked into Silverlight a little and Flash just has the maturity needed for a good PBBG interface.  I originally had the main game UI written using AJAX but found it to be a little cumbersome when a lot of activity was occuring on the back end, just too much traffic generated for one user. I looked into Silverlight a little but I am going to wait until the 1.1 version is released so I can program in C# on the backend. Flash seems to be able to provide me with what I need and I wrote custom ASHX handlers on the ASP.NET side to handle commands from the Flash UI. The UI is basically just an advanced MUD client, in that the primary output is text based. However, with Flash I will be able to provide a better map and add some additional graphical features later on down the road. Since the command handler is a custom ASHX handler in .NET I could really allow any type of client to connect, as long as that client can send XML as an HTTP POST and receive the and parse the XML response from the page.

I will post a screen shot of the UI in the next day or so. 

 

8/26/2007


Visual Studio 2008 Sivlerlight JS 1.0 Page ItemTemplate

I created a Visual Studio 2008 ItemTemplate for Silverlight 1.0 JS. The template creates a Page.xml and Page.js files for creating Silverlight 1.0 JS pages in an existing web project. I built this template for adding Silverlight 1.0 XAML pages to an existing ASP.NET project I am building using Visual Studio 2008. The web project is a 2.0 project so I can't reference the 3.5 JScriptSilverlightPage Item Template.

8/24/2007


Visual Studio 2008

I downloaded and installed the Visual Studio 2008 Beta 2 and I am really liking it. Aside from the new .NET 3.5 features the IDE itself is awesome. Visual Studio remains one of the best IDEs out there. If you want some good articles on VS 2008 check out Scott Gu's blog post on VS 2008.

8/2/2007


PBBG Ideas

I've been doing a lot of note taking lately, I've been trying to write out all the ideas that keep floating around in my head for various persistent browser based games and even a few that are more in the direction of casual games. I have a tablet PC so I tend to scribble my notes throughout the day, while working, at home while watching tv, etc. Now, if I could just find an army of free programmers to build the games. :)

8/1/2007


C# Socket Server for Flash/ActionScript

After doing some research on available socket servers for Flash I decided to write my own in C#. Most of the server out there are written in Java and while there are some open source implementations I prefer to stick with a code base I know and understand.

I got the Flash movie to connect to the C# server yesterday using JSON protocal instead of XML as my transport mechanism. I used a .NET JSON library and an ActionScript 2.0 JSON library to build the objects on both sides. I created a C# class for passing data, serialize as a JSON string and send it the Flash movie. On the ActionScript side I created an AS class that mimics my C# data gram class and use the AS JSON lib to serialize and send that same object structure to the C# server. Working pretty good so far, I am able to login to the server and send and receive messages.

My next steps will be creating a basic flash game that I can use to run around and do battle while sending and receiving messages from the server. 

I have plans to make a Flash based PBBG with real time combat, we'll see how it goes. :) 

7/27/2007


GridView Grouping and Summaries

If you are looking to group your grid view results and provide summaries such as totals, etc. I found a great utility for grouping GridView results. It is a set of classes that provide a helper for your grids to allow grouping. The component is super easy to use just remember that if you don't use a data source control you have to handle the GridView Sorting event. You don't actually have to have any code in the event handler, just handle the event.

7/18/2007


X, Y, Z Coordinate Struct for .NET PBBGs

I created a struct in C# for storing the X, Y and Z coordinates for characters, objects and rooms within my current PBBG project Perenthia. The struct, called Vector, is serializable and can be used as the key value in a sorted or generic dictionary. Here is the code for the class.

[Serializable]    public struct Vector : IEquatable<Vector>    {        public static readonly Vector Empty = new Vector(0, 0, 0);        public Vector(int x, int y, int z)        {            _x = x;            _y = y;            _z = z;        }        public void SetLocation(int x, int y)        {            this.SetLocation(x, y, this.Z);        }        public void SetLocation(int x, int y, int z)        {            _x = x;            _y = y;            _z = z;        }        public Vector Copy()        {            return new Vector(this.X, this.Y, this.Z);        }        public static Vector FromString(string value)        {            if (!String.IsNullOrEmpty(value))            {                string[] parts = value.Split(',');                if (parts != null && parts.Length == 3)                {                    int x, y, z;                    if (Int32.TryParse(parts[0], out x))                    {                        if (Int32.TryParse(parts[1], out y))                        {                            if (Int32.TryParse(parts[2], out z))                            {                                return new Vector(x, y, z);                            }                        }                    }                }            }            return Vector.Empty;        }        #region GetHashCode        public override int GetHashCode()        {            // The Y value should always come first in any kind of sorting, comparison or hashing operations            // followed by X and then Z because typical loops would start with the Y value.            return (this.Y.GetHashCode() + this.X.GetHashCode() + this.Z.GetHashCode());        }        #endregion        #region Equals        public bool Equals(Vector obj)        {            // The Y value should always come first in any kind of sorting, comparison or hashing operations            // followed by X and then Z because typical loops would start with the Y value.            if (obj != null)            {                if (obj.Y == this.Y)                {                    if (obj.X == this.X)                    {                        return (obj.Z == this.Z);                    }                }            }            return false;        }        public override bool Equals(object obj)        {            if (obj is Vector)            {                return this.Equals((Vector)obj);            }            return base.Equals(obj);        }        #endregion        #region ToString        public override string ToString()        {            return String.Format("{0},{1},{2}", _x, _y, _z);        }        public string ToString(bool forDisplay)        {            if (forDisplay)            {                return String.Format("X = {0}, Y = {1}, Z = {2}", _x, _y, _z);            }            return this.ToString();        }        #endregion        #region Operators        public static Vector operator +(Vector v1, Vector v2)        {            return new Vector(v1.X + v2.X, v1.Y + v2.Y, v1.Z + v2.Z);        }        public static Vector operator -(Vector v1, Vector v2)        {            return new Vector(v1.X - v2.X, v1.Y - v2.Y, v1.Z - v2.Z);        }        public static bool operator ==(Vector v1, Vector v2)        {            return v1.Equals(v2);        }        public static bool operator !=(Vector v1, Vector v2)        {            return (!v1.Equals(v2));        }        public static bool operator >=(Vector v1, Vector v2)        {            // The Y value should always come first in any kind of sorting, comparison or hashing operations            // followed by X and then Z because typical loops would start with the Y value.            if (v1.Y >= v2.Y)            {                if (v1.X >= v2.X)                {                    return (v1.Z >= v2.Z);                }            }            return false;        }        public static bool operator <=(Vector v1, Vector v2)        {            // The Y value should always come first in any kind of sorting, comparison or hashing operations            // followed by X and then Z because typical loops would start with the Y value.            if (v1.Y <= v2.Y)            {                if (v1.X <= v2.X)                {                    return (v1.Z <= v2.Z);                }            }            return false;        }        #endregion        #region Properties        private int _x;        public int X        {            get { return _x; }            set { _x = value; }        }        private int _y;        public int Y        {            get { return _y; }            set { _y = value; }        }        private int _z;        public int Z        {            get { return _z; }            set { _z = value; }        }        #endregion        #region IEquatable<Vector> Members        bool IEquatable<Vector>.Equals(Vector other)        {            return this.Equals(other);        }        #endregion    }

7/16/2007


JavaScript Libraries

I found some good javascript libraries for PBBG or any kind of web development.

This one is a graphics library useful for drawing vector graphics to the browser. Works great, just remember to set the position:relative value of the DIV you wish to draw in. :)

DHTML: Draw Line, Ellipse, Oval, Circle, Polyline, Polygon, Triangle with JavaScript

This site has a collection of useful libraries for all manner of web related activities.

Javascript Toolbox: Reusable Libraries And Scripts Plus Information

7/12/2007


PBBG Mapper

I've been working on a tile mapping tool I call PBBG Mapper here lately while working on my persistent browser based games. The tool is a Windows based application written in C# 2.0 and will include some plugin functionality so that other developers can extend it an customize it. The progam saves all tile maps as XML for easy portability and works pretty well. I will post some screenshots once I get it into a full working version.

7/10/2007