using Spark;
using Spark.Packets;
class Program
{
InSim _insim;
Program()
{
using (_insim = new InSim())
{
_insim.Bind<IS_MSO>(MessageReceived);
_insim.Connect("127.0.0.1", 29999);
_insim.Send(new IS_ISI { Prefix = '!', IName = "^3Spark" });
_insim.Run();
}
}
void MessageReceived(IS_MSO mso)
{
string[] args;
if (TryParseCommand(mso, out args))
{
var command = args[0].ToLower();
switch (command)
{
case "!buy":
// Process !buy command.
break;
case "!sell":
// Process !sell command.
break;
}
}
}
bool TryParseCommand(IS_MSO mso, out string[] args)
{
if (mso.UserType == UserType.MSO_PREFIX)
{
var message = mso.Msg.Substring(mso.TextStart);
args = message.Split();
return args.Length > 0;
}
args = null;
return false;
}
static void Main()
{
new Program();
}
}
using System;
using Spark;
using Spark.Packets;
namespace SimpleSniffer
{
class Program
{
Program()
{
using (var _insim = new InSim())
{
_insim.PacketReceived += new EventHandler<PacketEventArgs>(_insim_PacketReceived);
_insim.Connect("127.0.0.1", 29999);
_insim.Send(new IS_ISI { ReqI = 255, IName = "^3Sniffer", Flags = InSimFlags.ISF_MCI, Interval = 2000 });
_insim.Run();
}
}
void _insim_PacketReceived(object sender, PacketEventArgs e)
{
SniffPacket(e.Packet);
Console.WriteLine();
}
void SniffPacket<T>(T packet)
{
var properties = packet.GetType().GetProperties();
foreach (var property in properties)
{
var value = property.GetValue(packet, null);
if (value.GetType().IsArray)
{
foreach (var item in (Array)value)
{
if (property.Name == "CompCars" || property.Name == "NodeLaps")
SniffPacket(item); // Recursion!
else
Console.WriteLine("{0}: {1}", property.Name, item);
}
}
else
{
Console.WriteLine("{0}: {1}", property.Name, value);
}
}
}
static void Main()
{
new Program();
}
}
}
using System.Timers;
using Spark;
using Spark.Packets;
namespace CountDown
{
class Program
{
InSim _insim;
Timer _timer;
int _count;
string[] _countdown = new string[] { "3", 2", "1", "GO!" };
public Program()
{
using (_insim = new InSim())
{
// Connect to InSim
_insim.Bind<IS_MSO>(MessageReceived);
_insim.Connect("127.0.0.1", 29999);
_insim.Send(new IS_ISI { IName = "^3CountDown", Prefix = '!' }); // Set prefix to '!'
// Initialize countdown timer.
_timer = new Timer();
_timer.AutoReset = true;
_timer.Interval = 1000; // Milliseconds
_timer.Elapsed += new ElapsedEventHandler(_timer_Elapsed);
// Go!
_insim.Run();
}
}
void MessageReceived(IS_MSO mso)
{
if (mso.UserType == UserType.MSO_PREFIX)
{
var message = mso.Msg.Substring(mso.TextStart);
if (message.StartsWith("!cd") || message.StartsWith("!countdown"))
{
// Start countdown!
_timer.Start();
}
}
}
void _timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (_count < _countdown.Length)
{
_insim.Send("/rcm {0}", _countdown[_count++]);
_insim.Send("/rcm_all");
}
else
{
_count = 0;
_timer.Stop();
_insim.Send("/rcc_all");
}
}
static void Main()
{
new Program();
}
}
}
void _insim_PacketReceived(object sender, PacketEventArgs e)
{
IS_VER ver;
if (e.TryCast(out ver))
{
Console.WriteLine("LFS: {0} {1} InSim: {2}", ver.Product, ver.Version, ver.InSimVer);
}
}
using System;
using Spark;
using Spark.Packets;
using Spark.Helpers;
class Program
{
static void Main()
{
using (var insim = new InSim())
{
// Relay packets begin with IR_*.
insim.Bind<IR_HOS>(HostListReceived);
// Connect to the relay host.
insim.Connect("isrelay.lfs.net", 47474);
// Request for the host list to be sent.
insim.Send(new IR_HLR { ReqI = 255 });
insim.Run();
}
}
static void HostListReceived(IR_HOS hos)
{
// Loop through each host in the list.
foreach (var host in hos.Info)
{
// Print out the host name.
Console.WriteLine("Host: {0}", StringHelper.StripColors(host.HName));
// If this is the last host, print a blank line after it.
if (host.Flags.HasFlag(InfoFlags.HOS_LAST))
Console.WriteLine();
}
}
}
using System;
using System.Collections.Generic;
using Spark;
using Spark.Packets;
using Spark.Helpers;
namespace Spark.Example5
{
class Program
{
static InSim _insim;
// We store the players in a dictionary.
static Dictionary<int, IS_NPL> _players = new Dictionary<int, IS_NPL>();
static void Main()
{
// Create new InSim object.
using (_insim = new InSim())
{
// Bind handlers.
_insim.Bind<IS_ISM>(MultiPlayerInfo);
_insim.Bind<IS_NPL>(NewPlayer);
_insim.Bind<IS_PLL>(PlayerLeft);
_insim.Bind<IS_MCI>(MultiCarInfo);
// Establish the InSim connection.
_insim.Connect("127.0.0.1", 29999);
// Initialize InSim.
_insim.Send(new IS_ISI { IName = "^3Example", Flags = InSimFlags.ISF_MCI, Interval = 500, Admin = string.Empty });
// Request for multiplayer info packet to be sent.
_insim.Send(new IS_TINY { SubT = TinyType.TINY_ISM, ReqI = 255 });
// Prevent program from exiting.
_insim.Run();
}
}
static void MultiPlayerInfo(IS_ISM ism)
{
// When joining multiplayer request for all connections and players to be sent.
_insim.Send(new IS_TINY { SubT = TinyType.TINY_NCN, ReqI = 255 });
_insim.Send(new IS_TINY { SubT = TinyType.TINY_NPL, ReqI = 255 });
}
static void NewPlayer(IS_NPL npl)
{
if (_players.ContainsKey(npl.PLID))
{
// Leaving pits, just update NPL object.
_players[npl.PLID] = npl;
}
else
{
// Add new player.
_players.Add(npl.PLID, npl);
}
}
static void PlayerLeft(IS_PLL pll)
{
// Remove player.
_players.Remove(pll.PLID);
}
static void MultiCarInfo(IS_MCI mci)
{
// Loop through each car on track.
foreach (var car in mci.CompCars)
{
// Get the player driving this car.
IS_NPL npl;
if (_players.TryGetValue(car.PLID, out npl))
{
// Check cars speed.
var kph = MathHelper.SpeedToKph(car.Speed);
if (kph > 80)
{
// Spectate player and send chat message.
_insim.Send("/spec {0}", npl.PName);
_insim.Send("{0} ^3spectated for speeding", npl.PName);
}
}
}
}
}
}
public static double SpeedToMph(int speed)
{
return speed / 146.486067;
}
using System;
using System.Collections.Generic;
using Spark;
using Spark.Packets;
using Spark.Helpers;
class Program
{
// We store the players in a dictionary.
static Dictionary<int, IS_NPL> _players = new Dictionary<int, IS_NPL>();
static void Main()
{
// Create new InSim object.
using (var insim = new InSim())
{
// Bind handlers.
insim.Bind<IS_NPL>(NewPlayer);
insim.Bind<IS_PLL>(PlayerLeft);
insim.Bind<IS_MCI>(MultiCarInfo);
// Establish the InSim connection.
insim.Connect("127.0.0.1", 29999);
// Initialize InSim.
insim.Send(new IS_ISI { IName = "^3Example", Flags = InSimFlags.ISF_MCI, Interval = 500 });
// Request players.
insim.Send(new IS_TINY { SubT = TinyType.TINY_NPL, ReqI = 255 });
// Prevent program from exiting.
insim.Run();
}
}
static void NewPlayer(IS_NPL npl)
{
if (_players.ContainsKey(npl.PLID))
{
// Leaving pits, just update NPL object.
_players[npl.PLID] = npl;
}
else
{
// Add new player.
_players.Add(npl.PLID, npl);
}
}
static void PlayerLeft(IS_PLL pll)
{
// Remove player.
_players.Remove(pll.PLID);
}
static void MultiCarInfo(IS_MCI mci)
{
// Loop through each car on track.
foreach (var car in mci.CompCars)
{
IS_NPL npl;
if (_players.TryGetValue(car.PLID, out npl))
{
// Convert LFS speed into Mph.
var mph = MathHelper.SpeedToMph(car.Speed);
// Print nicely formatted string to console.
Console.WriteLine("Speed: {0} {1:F2}", npl.PName, mph);
}
}
}
}
PM> Install-Package InSimDotNet