|
-
May 29th, 2011, 07:04 AM
#1
Thread Starter
Hyperactive Member
BinaryWriter Help Needed
I have some code which reads a binary file. But I don't have the code that writes the binary file. But I suppose you can kinda just reverse the process.
Here is the class I am working with:
PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
namespace CSX_Stats_Tools
{
public class StatsFile
{
public const short RANK_VERSION = 11;
public class StatsEntry
{
public StatsEntry()
{
bodyHits = new uint[9];
}
public uint[] bodyHits;
public string Name;
public string SteamID;
public long TeamKills;
public long Damage;
public long Deaths;
public long Kills;
public long Shots;
public long HeadShots;
public long Hits;
public long DefusionTries;
public long Defusions;
public long Plants;
public long Explosions;
/*
public long Generic;
public long Head;
public long Chest;
public long Stomach;
public long LeftArm;
public long RightArm;
public long LeftLeg;
public long RightLeg;
public long NetScore;
public long Rank;
public float Skill;
* */
}
public static ArrayList ReadEntriesToList(string file)
{
if (!File.Exists(file))
{
throw new FileNotFoundException();
}
System.IO.FileStream stream = File.Open(file, System.IO.FileMode.Open);
if (stream == null)
{
throw new FileLoadException();
}
BinaryReader br = new BinaryReader(stream);
ArrayList list;
try
{
short vers = br.ReadInt16();
if (vers != RANK_VERSION)
{
throw new Exception("Bad stats version");
}
ushort num = br.ReadUInt16();
list = new ArrayList();
while (num != 0)
{
StatsEntry entry = new StatsEntry();
byte[] name = br.ReadBytes(num);
num = br.ReadUInt16();
byte[] unique = br.ReadBytes(num);
entry.Name = Encoding.ASCII.GetString(name, 0, name.Length - 1);
entry.SteamID = Encoding.ASCII.GetString(unique, 0, unique.Length - 1);
entry.TeamKills = br.ReadUInt32();
entry.Damage = br.ReadUInt32();
entry.Deaths = br.ReadUInt32();
entry.Kills = br.ReadInt32();
entry.Shots = br.ReadUInt32();
entry.Hits = br.ReadUInt32();
entry.HeadShots = br.ReadUInt32();
entry.Defusions = br.ReadUInt32();
entry.DefusionTries = br.ReadUInt32();
entry.Plants = br.ReadUInt32();
entry.Explosions = br.ReadUInt32();
for (int i = 0; i < entry.bodyHits.Length; i++)
{
entry.bodyHits[i] = br.ReadUInt32();
}
num = br.ReadUInt16();
list.Add(entry);
}
}
catch
{
throw new FileLoadException("Error reading file");
}
finally
{
if (br != null)
{
br.Close();
br = null;
}
if (stream != null)
{
stream.Close();
stream = null;
}
}
return list;
}
public static void WriteStatsFile(ArrayList list)
{
UTF8Encoding utf8 = new UTF8Encoding();
BinaryWriter bin = new BinaryWriter(File.Open(@"csstatsx.dat", FileMode.Create));
bin.Write(RANK_VERSION);
foreach (StatsEntry player in list)
{
bin.Write(player.Name);
bin.Write(player.SteamID);
bin.Write(player.TeamKills);
bin.Write(player.Damage);
bin.Write(player.Deaths);
bin.Write(player.Kills);
bin.Write(player.Shots);
bin.Write(player.Hits);
bin.Write(player.HeadShots);
bin.Write(player.Defusions);
bin.Write(player.DefusionTries);
bin.Write(player.Plants);
bin.Write(player.Explosions);
for (int i = 0; i < player.bodyHits.Length; i++)
{
bin.Write(player.bodyHits[i]);
}
}
bin.Write(0);
bin.Flush();
bin.Close();
bin.Dispose();
}
}
}
The code at the bottom is my useless attempt in writing a binary file out of the data I have read from another. Simply just trying to duplicate the file for now, then I will modify its data,
I then have some lines which first reads a binary file into memory, and then it writes that data down with my method and then it tries to read the new file. I am stepping the code to see where it fails and it does that nearly directly. The only thing I know is working atm is the first write, the RANK_VERSION. Because it reads it as 11 (can you even fail on that step?).
But at the next step, the name part it fails.
Anyone that can have a look and tell me how to at least write the strings correctly?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|