The online racing simulator
Hello...
I'm making Cruise InSim and building on "InSim.NETSampleByDK" and i thank him so much because it's very simple and easy to understand...but i had problem....because too many things missed from his InSim as cruise .... so i decided to switch to "Spark Cruise" but the problem is that InSim builded on old Library so my question is if i changed to InSim.NET Library (last version) would that work on "Spark Cruise" InSim ?

Thanks...Honza
Quote from HonzaQ4 :Hello...
I'm making Cruise InSim and building on "InSim.NETSampleByDK" and i thank him so much because it's very simple and easy to understand...but i had problem....because too many things missed from his InSim as cruise .... so i decided to switch to "Spark Cruise" but the problem is that InSim builded on old Library so my question is if i changed to InSim.NET Library (last version) would that work on "Spark Cruise" InSim ?

Thanks...Honza

It won't work just by changing the library, you'll have to use the new methods that InSim.NET uses to initialise connection, bind events and even control each Callback(packet). If you have time then i'd suggest you to start copying part by part to a clean project, so you can convert it.

Also i'd like to thank you for using it and yes, this was exactly the reason i released it. To show people how to use and understand each part of InSim.NET. And of course that's the reason it doesn't include any race/drift/cruise features.
hmmmm....i think i will choose your InSim because it's clear one......but need hard work
also i need hard work to change the Library at Spark Cruise so it's same

and Thanks DarkKostas
i got problem :/ ...... iam suing InSimDotNetSampleByDk so if you can help me

when i start my InSim it kicks me out and cant get it till period of time......

why that happen and how to stop it??
and Finally i finished my save system at .NET InSim but i need small check because if there's something wrong to avoide it and also anyone can use it for free
As i said before I'm biulding from on InSim.NETSampleByDK just to let you know and here is the codes ...

here is save users and getuser(stats) and others

#region ' File Info '

private void CheckUserAdded(string Username)
{
#region ' Stream Readers&Writers '

StreamReader CashFile = new StreamReader(DataFolder + "/Cash.ini");
StreamWriter CashFileWriter = new StreamWriter(DataFolder + "/Cash.ini");

StreamReader BankCashFile = new StreamReader(DataFolder + "/BankCash.ini");
StreamWriter BankCashFileWriter = new StreamWriter(DataFolder + "/BankCash.ini");

StreamReader TotalJobsDoneFile = new StreamReader(DataFolder + "/TotalJobsDone.ini");
StreamWriter TotalJobsDoneFileWriter = new StreamWriter(DataFolder + "/TotalJobsDone.ini");

StreamReader TotalHealthFile = new StreamReader(DataFolder + "/TotalHealth.ini");
StreamWriter TotalHealthFileWriter = new StreamWriter(DataFolder + "/TotalHealth.ini");

StreamReader TotalJoinsFile = new StreamReader(DataFolder + "/TotalJoins.ini");
StreamWriter TotalJoinsFileWriter = new StreamWriter(DataFolder + "/TotalJoins.ini");

StreamReader TotalDistanceFile = new StreamReader(DataFolder + "/TotalDistance.ini");
StreamWriter TotalDistanceFileWriter = new StreamWriter(DataFolder + "/TotalDistance.ini");

StreamReader CarsFile = new StreamReader(DataFolder + "/Cars.ini");
StreamWriter CarsFileWriter = new StreamWriter(DataFolder + "/Cars.ini");

#endregion

#region ' Class's '

string line = null;

bool FoundUsernameCash = false;
bool FoundUsernameBankCash = false;
bool FoundUsernameTotalJobsDone = false;
bool FoundUsernameTotalHealth = false;
bool FoundUsernameTotalJoins = false;
bool FoundUsernameTotalDistance = false;
bool FoundUsernameCars = false;

#endregion

#region ' Check Cash '

while ((line = CashFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameCash = true;
}
if (!FoundUsernameCash)
{
CashFileWriter.WriteLine(Username + " = 3000");
}
}

#endregion

#region ' Check Bank Cash '

while ((line = BankCashFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameBankCash = true;
}
if (!FoundUsernameBankCash)
{
BankCashFileWriter.WriteLine(Username + " = 10000");
}
}

#endregion

#region ' Check Total Jobs Done '

while ((line = TotalJobsDoneFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameTotalJobsDone = true;
}
if (!FoundUsernameTotalJobsDone)
{
TotalJobsDoneFileWriter.WriteLine(Username + " = 0");
}
}

#endregion

#region ' Check Total Health '

while ((line = TotalHealthFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameTotalHealth = true;
}
if (!FoundUsernameTotalHealth)
{
TotalHealthFileWriter.WriteLine(Username + " = 50");
}
}

#endregion

#region ' Check Total Joins '

while ((line = TotalJoinsFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameTotalJoins = true;
}
if (!FoundUsernameTotalJoins)
{
TotalJoinsFileWriter.WriteLine(Username + " = 0");
}
}

#endregion

#region ' Check Total Distance '

while ((line = TotalDistanceFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameTotalDistance = true;
}
if (!FoundUsernameTotalDistance)
{
TotalDistanceFileWriter.WriteLine(Username + " = 0");
}
}

#endregion

#region ' Check Cars '

while ((line = CarsFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
FoundUsernameCars = true;
}
if (!FoundUsernameCars)
{
CarsFileWriter.WriteLine(Username + " = UF1");
}
}

#endregion
}

#region ' Player Status '

private bool GetUserAdmin(string Username)
{//reading admins.ini when connecting to server for InSim admin
StreamReader CurrentFile = new StreamReader(DataFolder + "/admins.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private bool GetUserPermBan(string Username)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/banlist.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private bool CanBeOfficer(string Username)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/officers.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private bool CanBeCadet(string Username)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/cadets.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private bool CanBeTowTruck(string Username)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/towtrucks.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

private bool CanBeMember(string Username)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/members.ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
CurrentFile.Close();
return true;
}
}
CurrentFile.Close();
return false;
}

#endregion

#region ' Player Stats '

private long GetUserCash(string Username)
{
StreamReader CashFile = new StreamReader(DataFolder + "/Cash.ini");

string line = null;
while ((line = CashFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return long.Parse(LineData[1]); }
catch { }
}
}
CashFile.Close();
return 0;
}

private long GetUserBankCash(string Username)
{
StreamReader BankCashFile = new StreamReader(DataFolder + "/BankCash.ini");

string line = null;
while ((line = BankCashFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return long.Parse(LineData[1]); }
catch { }
}
}
BankCashFile.Close();
return 0;
}

private string GetUserCars(string Username)
{
StreamReader CarsFile = new StreamReader(DataFolder + "/Cars.ini");

string line = null;
while ((line = CarsFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return LineData[1].Trim(); }
catch { }
}
}
CarsFile.Close();
return "";
}

private long GetUserTotalDistance(string Username)
{
StreamReader TotalDistanceFile = new StreamReader(DataFolder + "/TotalDistance.ini");

string line = null;
while ((line = TotalDistanceFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return long.Parse(LineData[1]); }
catch { }
}
}
TotalDistanceFile.Close();
return 0;
}

private long GetUserTotalJoins(string Username)
{
StreamReader TotalJoinsFile = new StreamReader(DataFolder + "/TotalJoins.ini");

string line = null;
while ((line = TotalJoinsFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return long.Parse(LineData[1]); }
catch { }
}
}
TotalJoinsFile.Close();
return 0;
}

private byte GetUserTotalHealth(string Username)
{
StreamReader TotalHealthFile = new StreamReader(DataFolder + "/TotalHealth.ini");

string line = null;
while ((line = TotalHealthFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return byte.Parse(LineData[1]); }
catch { }
}
}
TotalHealthFile.Close();
return 0;
}

private int GetUserTotalJobsDone(string Username)
{
StreamReader TotalJobsDoneFile = new StreamReader(DataFolder + "/TotalJobsDone.ini");

string line = null;
while ((line = TotalJobsDoneFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return int.Parse(LineData[1]); }
catch { }
}
}
TotalJobsDoneFile.Close();
return 0;
}

#endregion

#endregion


and here is how to use it at OnConnectionJoin (NCN)

CheckUserAdded(NCN.UName);
connections.Add(NCN.UCID, new Connections
{
UniqueID = NCN.UCID,
Username = NCN.UName,
Playername = NCN.PName,
IsServerAdmin = NCN.Admin,

IsInSimAdmin = GetUserAdmin(NCN.UName),
IsPermBan = GetUserPermBan(NCN.UName),
IsCadet = CanBeCadet(NCN.UName),
IsMember = CanBeMember(NCN.UName),
IsOfficer = CanBeOfficer(NCN.UName),
IsTowTruck = CanBeTowTruck(NCN.UName),

Cash = GetUserCash(NCN.UName),
BankCash = GetUserBankCash(NCN.UName),
Cars = GetUserCars(NCN.UName),
TotalDistance = GetUserTotalDistance(NCN.UName),
TotalHealth = GetUserTotalHealth(NCN.UName),
TotalJobsDone = GetUserTotalJobsDone(NCN.UName),
TotalJoins = GetUserTotalJoins(NCN.UName)
});//make sure your code is AFTER this one

i hope that are my codes right and thanks
Quote from HonzaQ4 :and Finally i finished my save system at .NET InSim but i need small check because if there's something wrong to avoide it and also anyone can use it for free
As i said before I'm biulding from on InSim.NETSampleByDK just to let you know and here is the codes ...

here is save users and getuser(stats) and others
...

i hope that are my codes right and thanks


#region ' Player Status ' <--- all these Player Status functions into 1

private bool IsUserInGroup(string Username, string GroupName)
{//reading 'GroupName'.ini when connecting to server
bool IsInGroup = false;
StreamReader CurrentFile = new StreamReader(DataFolder + "/groups/" + GroupName + ".ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
if (line == Username)
{
IsInGroup = true;
break;
}
}
CurrentFile.Close();
return IsInGroup;
}
#endregion

How to use?
IsCadet = IsUserInGroup(NCN.UName, "cadets")//This will look in cadets.ini

Same with the player stats.

private string GetUserStat(string Username, string StatName)
{
StreamReader CurrentFile = new StreamReader(DataFolder + "/" + StatName + ".ini");

string line = null;
while ((line = CurrentFile.ReadLine()) != null)
{
string[] LineData = line.Split('=');
if (LineData[0] == Username)
{
try { return LineData[1]; }
catch { }
}
}
CurrentFile.Close();
return "0";
}

How to use? (A little attention here. As it returns String, for supporting string, you need to Convert it into Int32 or whatever you use)
Cash = (long)GetUserStat(NCN.UName, "Cash")

uhmmm....you more clever than me ..... and you use better and shorter codes ....

but i still dont understand what's the difference if i use long code and you use short codes....would that make bugs or something??

but i still dont understand does my codes right or not??

and thank you very much man!
Quote from HonzaQ4 :uhmmm....you more clever than me ..... and you use better and shorter codes ....

but i still dont understand what's the difference if i use long code and you use short codes....would that make bugs or something??

but i still dont understand does my codes right or not??

and thank you very much man!

Both are correct. But why use 500 lines of code when it can become just 50! And also easier for later upgrade. For example if you want to add something new, like hmmm, if user is a banana you can use IsUserInGroup(NCN.Username, "banana") without adding any extra code Also it makes your code faster to be read and understood by somebody else.
Yeah shorter codes are better for update .. you on right!.....

now i have got another problem with GetUserStat(string Username, string StatName) and didnt work like what you wrote so i used another way and it didnt give me any error but want your opinion if it right or not...

between to let you know .... i use
byte > health
int > TotalJobsDone
string > Cars
long > all others


connections.Add(NCN.UCID, new Connections
{
UniqueID = NCN.UCID,
Username = NCN.UName,
Playername = NCN.PName,
IsServerAdmin = NCN.Admin,

IsInSimAdmin = IsUserInGroup(NCN.UName, "admins"),
IsPermBan = IsUserInGroup(NCN.UName, "banlist"),
IsCadet = IsUserInGroup(NCN.UName, "cadets"),
IsMember = IsUserInGroup(NCN.UName, "members"),
IsOfficer = IsUserInGroup(NCN.UName, "officers"),
IsTowTruck = IsUserInGroup(NCN.UName, "towtrucks"),

Cash = Convert.ToInt64(GetUserStats(NCN.UName, "Cash")),
BankCash = Convert.ToInt64(GetUserStats(NCN.UName, "BankCash")),
Cars = (GetUserStats(NCN.UName, "Cars")),
TotalDistance = Convert.ToInt64(GetUserStats(NCN.UName, "TotalDistance")),
TotalHealth = Convert.ToByte(GetUserStats(NCN.UName, "TotalHealth")),
TotalJobsDone = Convert.ToInt32(GetUserStats(NCN.UName, "TotalJobsDone")),
TotalJoins = Convert.ToInt64(GetUserStats(NCN.UName, "TotalJoins"))
});//make sure your code is AFTER this one

i want to ask you another question....

now i want to make "Conn" as variable for "Connections.cs" i mean .... i want to use it like this..


if (Conn.TotalJoins == 0)
{
MsgAll(Conn.Playername + " Is new player to the community");
}

so the question is .... which is the right sentence for variable "Conn" in IS_NCN Packet...

var Conn = connections[NCN.UCID];


Connections Conn = connections[NCN.UCID];

and Thanks
That's the correct way. I usually use name as CurrentConnection but doesn't matter. It's up to you.
Quote from HonzaQ4 :

Connections Conn = connections[NCN.UCID];


Also if you want to get it from PLID, for example on MCI packet NPL packet, you got to convert PLID into UCID.


//Function
Connections GetConnection(byte PLID)
{
Players TotalPlayers;
if (_players.TryGetValue(PLID, out TotalPlayers)) return _connections[TotalPlayers.UCID];
return null;
}

//And use
Connections CurrentConnection = GetConnection(NPL.PLID);

Yeah i saw that.....and i knew how to use it..but i didnt know which code is right....

any way thank you very much you're pro!
Hello. Can anyone write an example of creating and using button, where after pressed need to enter text. InSim packet called IS_BTT, to get what user was writing.
EDIT: I created it myself, using InsimDotNET source code. That's the code for someone, who needs or in future need code:

insim.Send(new IS_BTN { Text = "Your text", BStyle = ButtonStyles.ISB_CLICK | ButtonStyles.ISB_LIGHT, UCID = C.UCID, H = 6, W = 30, T = 30, L = 80, ClickID = 6, ReqI = 6, Caption = "Text when pressed", TypeIn = 10});

Caption is text, which is written when user click the button. TypeIn is max symbols, which can be written in box.
Hi.
Сompiled dll from source code by codeplex. Changes affected only IS_NCN. But through random intervals (some minuts) i get an exception "Object reference not set to an instance of the object" and insim die.


System.NullReferenceException: Ссылка на объект не указывает на экземпляр объекта.
in InSimDotNet.BindingManager.PacketBinding`1.Execute(InSim insim, IPacket packet)
in InSimDotNet.BindingManager.Execute(InSim insim, IPacket packet)
in InSimDotNet.InSim.RaisePacketEvent(IPacket packet)
in InSimDotNet.InSim.tcpSocket_PacketDataReceived(Object sender, PacketDataEventArgs e)
in InSimDotNet.TcpSocket.OnPacketDataReceived(PacketDataEventArgs e)
in InSimDotNet.TcpSocket.HandlePackets(SocketState state)
in InSimDotNet.TcpSocket.ReceiveCallback(IAsyncResult asyncResult)

Project is clean, no action is not conducted, the program only connects and waits, but binded many packets (emty void). If remove most of, the exception appears on a random remaining packet

-------
Upd.
From IDA and HEX editor has made changes to the downloded dll. A nail hammered microscope, but the problem is solved.
I have problem with asian chars. App is crashing when someone wrote with asian chars. Is it library bug?
Don't think so. Seems to work fine for me. What does the error message(s) say when it crashes (in debug mode)?
I solved it with installing asian fonts.

If I remember true error was: Unable to convert [F6][BC] bytes to string bla bla bla
Continuing saga: Crash on kyril/korean/china alphabet
Every char on kyril korean china crashes insim instance :F
can i prevent shutting down instance or i must fix the issue with chars?

http://prntscr.com/37blcf
Now i have problems with IS_MCI packets. I can't read 'em. I want to apply speed limits to users.

private void speedChecked(InSim insim, IS_MCI mci)
{
foreach (CompCar car in mci.Info)
{
var speed = MathHelper.SpeedToKph(car.Speed);
if (speed > 131)
{
_players.TryGetValue(car.PLID, out npl);
insim.Send("/kick " + npl.PName);
}
}}

Have you bind it?

insim.Bind<IS_MCI>(OnCarInformation);

Quote from david989898 :Have you bind it?

insim.Bind<IS_MCI>([B]speedChecked[/B]);


Should be this.

You also have to use UName to kick instead of PName.
I binded it. And used UName.No luck. I debugged it with writeline, it couldn't read values. speed gives 0 always.

Edit; Wow, now i can read values but i can't pair user with speed. I found problem is in this function, what is the wrong?;


private Connection GetUser(byte plid)
{
IS_NPL npl;
if (_players.TryGetValue(plid, out npl))
{
return _users[npl.UCID];
}
return null;
}

Today, I got hundreds of this error:



EDIT: I found it when. It's giving error when someone joined to server.



as well as on Airio PROS:



Looks like it's unsolvable but Airio recovered itself and everything is fine. But other insim (I developed it) crashes. Is there a way to recover on this?

Second question is:

InSim Error: The given key was not present in the dictionary.

This happens if someone does connect-disconnect-connect-disconnect. Time doesn't matter. It can be like this:
10:00-connected
10:10-disconnected
23:00-connected
23:10-disconnected

I debugged with IntelliTrace:

I took it:



on foreach:




private void time_Elapsed(object sender, ElapsedEventArgs e)
{

foreach (var user in conns)
{

UpdateUser(user);
showWindows(user);
}


}

Other things:

List<Connection> conns = new List<Connection>();


private void TimeOneSec() //It's called on initiliazing insim
{
time = new System.Timers.Timer();
time.Interval = 1000; // Milliseconds.
time.AutoReset = true;
time.Elapsed += new ElapsedEventHandler(time_Elapsed);
time.Start();
}



This is how "conns" works:


addConnToList(ncn);


int ucidToIdx(byte ucid) {
for (int i = 0; i < conns.Count; i++)
if (conns[i].UCID == ucid)
return i;
return 0;
}


void addConnToList(IS_NCN ncn) {
Connection conn = new Connection();

conn.UCID = ncn.UCID;
conn.UName = ncn.UName;
conn.PName = ncn.PName;
conn.Admin = ncn.Admin;
conn.Health = Convert.ToInt32(Database.loadHealth(conn.UName));
conn.Money = Convert.ToInt32(Database.loadMoney(conn.UName));
conn.Distance = Convert.ToInt32(Database.loadDistance(conn.UName));
conns.Add(conn);
}

void removeConnFromList(byte ucid) {
conns.Remove(conns[ucidToIdx(ucid)]);

}


void newConnection(InSim insim, IS_NCN pack) {

// Add connection to list
addConnToList(pack);
var conn = conns[ucidToIdx(pack.UCID)];
}

Thanks for all help
Is there anyway to disable encoding conversion to unicode?

At the moment I want to resend a message received from IS_MSO but the problem is that encoding tags ^L, ^B and others are removed.

They are also removed from PName in IS_NCN.

Thanks.
When i try use
insim.Send( UCID, "Message" )
i catch Exception
WideCharToMultiByte

using Linux + Mono

FGED GREDG RDFGDR GSFDG