BN# Developer Guide

Notice: this documentation was retrieved from an archive of the JinxBot source files, and the original site where this information was hosted is no longer available. Additionally, several updates have been made to the Battle.net servers since this was developed, and it may no longer be fully compatible on official servers.

BN# is a library designed so that developers can quickly create a functioning binary client for classic Battle.net. It does this by providing a neatly-packaged programming interface that hides most of the mundane implementation details from the developer.

While the library itself is targeted to the .NET Framework 2.0, the examples below use C# 3.0 only features, which requires the .NET Framework 3.5 compiler.

How to Develop a Battle.net Client in 10 Minutes

  1. Open Visual Studio and create a new Console Application project.
  2. In the project explorer, right click on the references folder and choose “Add Reference”
  3. In the “Browse” tab, navigate to the directory where you have BNSharp.dll and choose it.
  4. Next you must implement the settings interface – right-click on your project and choose “Add Class” – give it a name. An example implementation is below.
using System;
using System.Collections.Generic;
using System.Text;
using BNSharp;
using BNSharp.BattleNet;


namespace BnsDemoClient
{
    class ClientSettings : IBattleNetSettings
    {
        public string CdKey1
        {
            get;
            set;
        }

        public string CdKey2
        {
            get;
            set;
        }

        public string CdKeyOwner
        {
            get { return "BNSharp"; }
            set { }
        }

        public string Client
        {
            get { return Product.StarcraftRetail.ProductCode; }
            set { }
        }

        public string GameExe
        {
            get { return @"c:\GameFiles\STAR\Starcraft.exe"; }
            set { }
        }

        public string GameFile2
        {
            get { return @"c:\GameFiles\STAR\storm.dll"; }
            set { }
        }

        public string GameFile3
        {
            get { return @"c:\GameFiles\STAR\battle.snp"; }
            set { }
        }

        public string HomeChannel
        {
            get;
            set;
        }

        public string ImageFile
        {
            get { return @"c:\GameFiles\STAR\star.bin"; }
            set { }
        }

        public string Password
        {
            get;
            set;
        }

        public BNSharp.BattleNet.PingType PingMethod
        {
            get { return PingType.Normal; }
            set { }
        }

        public int Port
        {
            get { return 6112; }
            set { }
        }

        public string Server
        {
            get { return "useast.battle.net"; }
            set { }
        }

        public string Username
        {
            get;
            set;
        }

        public int VersionByte
        {
            get { return 0xd1; }
            set { }
        }
    }
}

Note: StarCraft is no longer supported on official servers, so you will need to change the “Client” property’s get method to return a different game’s product code. You will also need to update the game file paths to point to the files for the different game.

  1. Go to the project’s entry point and create a new instance of the client using your custom settings class. You can either prompt the user for the values, or hard code them (just don’t publish your code if you do the latter!). The example below prompts the user:
static void Main(string[] args)
{
      string userName, password, key;
      Console.WriteLine("Specify username: ");
      userName = Console.ReadLine();
      Console.WriteLine("Specify password: ");
      password = Console.ReadLine();
      Console.WriteLine("Specify CD key for Starcraft:");
      key = Console.ReadLine();

      ClientSettings settings = new ClientSettings
      {
           CdKey1 = key,
           Username = userName,
           Password = password
      };

      BattleNetClient client = new BattleNetClient(settings);
}
  1. Next you will need to hook up event handlers. This is done at the bottom of the above example, while still in the brackets. You can tweak the strings to change how events are displayed.
client.Connected += delegate
{
       Console.WriteLine("Connected.");
};
client.EnteredChat += delegate(object sender, EnteredChatEventArgs e)
{
      Console.WriteLine("Entered chat as {0}", e.UniqueUsername);
};
client.JoinedChannel += delegate(object sender, ServerChatEventArgs e)
{
      Console.WriteLine("Joined Channel: {0}", e.Text);
};
client.UserJoined += delegate(object sender, UserEventArgs e)
{
      Console.WriteLine("User Joined: {0} ({1})", e.User.Username, e.User.Stats.Product.Name);
};
client.UserLeft += delegate(object sender, UserEventArgs e)
{
      Console.WriteLine("User left: {0}", e.User.Username);
};
client.UserShown += delegate(object sender, UserEventArgs e)
{
      Console.WriteLine("User Joined: {0} ({1})", e.User.Username, e.User.Stats.Product.Name);
};
client.UserSpoke += delegate(object sender, ChatMessageEventArgs e)
{
      Console.WriteLine("<{0}>: {1}", e.Username, e.Text);
};
client.UserEmoted += delegate(object sender, ChatMessageEventArgs e)
{
      Console.WriteLine("<{0} {1}>", e.Username, e.Text);
};
client.WhisperSent += delegate(object sender, ChatMessageEventArgs e)
{
      Console.WriteLine("You whisper to {0}: {1}", e.Username, e.Text);
};
client.WhisperReceived += delegate(object sender, ChatMessageEventArgs e)
{
      Console.WriteLine("{0} whispers: {1}", e.Username, e.Text);
};
client.LoginFailed += delegate(object sender, LoginFailedEventArgs e)
{
      Console.WriteLine("Login failed: {0}", e.Reason);
};
  1. Finally, create the main chat loop, which will keep the program running and waiting for user input, disconnecting when the user inputs the “/quit” command.
Console.WriteLine("Press <enter> to connect or Ctrl+C to quit.");
Console.ReadLine();

client.Connect();

string input = null;
do
{
       input = Console.ReadLine();
       if (input != "/quit")
            client.SendMessage(input);
} while (input != "/quit");

client.Close();
Console.WriteLine("Disconnected.  Press <enter> to quit.");
Console.ReadLine();
  1. You’re done! Press F5 to build and run the project. Sample output below.
Entered chat as DarkTemplar~AoA
Joined Channel: StarCraft USA-1
User Joined: mclarenf1 (Starcraft (Retail))
User Joined: JESUS_is_LORD (Starcraft (Retail))
User Joined: destroyer-x1 (Starcraft (Retail))
User Joined: pnut (Starcraft (Retail))
User Joined: dravenxx (Starcraft (Retail))
User Joined: Suspect45 (Starcraft (Retail))
User Joined: IAMAHAZARD (Starcraft (Retail))
User Joined: bh12345 (Starcraft (Retail))
User Joined: Olgarn (Starcraft (Retail))
User Joined: T.J.T.E.R.R.A.N (Starcraft (Retail))
User Joined: chaneyy (Starcraft (Retail))
User Joined: lukeduke152 (Starcraft (Retail))
User Joined: 843ilikeeggs (Starcraft (Retail))
User Joined: MagicMurder (Starcraft (Retail))
User Joined: DDKush (Starcraft (Retail))
User Joined: CLAW(GG) (Starcraft (Retail))
User Joined: soccerballa77 (Starcraft (Retail))
User Joined: HRZ1 (Starcraft (Retail))
User Joined: sbatten2 (Starcraft (Retail))
User Joined: HandofArtanis (Starcraft (Retail))
User Joined: ulostme[fun] (Starcraft (Retail))
User Joined: ModdedLawnMower (Starcraft (Retail))
User Joined: tyler_howe (Starcraft (Retail))
User Joined: RODIMUS (Starcraft (Retail))
User Joined: sidler (Starcraft (Retail))
User Joined: staal18 (Starcraft (Retail))
User Joined: m3driven (Starcraft (Retail))
User Joined: devinlacross (Starcraft (Retail))
User Joined: hbsblows (Starcraft (Retail))
User Joined: Dazzler007 (Starcraft (Retail))
User Joined: nano0 (Starcraft (Retail))
User Joined: Robomarine (Starcraft (Retail))
Hi everyone
User Joined: fkuc (Starcraft (Retail))
User Joined: E3S (Starcraft (Retail))
User Joined: ShRoOmS_NukkA (Starcraft (Retail))
User Joined: rushrushrush2 (Starcraft (Retail))
User Joined: ChaosDivElite (Starcraft (Retail))
User Joined: DarkTemplar~AoA (Starcraft (Retail))
User Joined: dirkacat (Starcraft (Retail))
User left: E3S <ulostme[fun]>: go to clan op xfun-
User Joined: dimwitt (Starcraft (Retail))
User left: m3driven
User left: fkuc <ulostme[fun]>: on west
User Joined: ogima (Starcraft (Retail))
User left: tyler_howe
User Joined: E3S (Starcraft (Retail))
User Joined: m3driven (Starcraft (Retail)) 
<ulostme[fun]>: hi
User left: Robomarine
User Joined: fkuc (Starcraft (Retail))
User left: hbsblows
/quit
Disconnected. Press to quit.