I'm developing a Heatmap tool for CS:GO. It decrypts .dem (custom demo) files, then serializes them into binary for later use.
The data which gets serialized looks like this:
namespace HeatScatterToolv2
{
class LoadAndVisualize
{
public void LoadFileFromBinary (string fileName)
{
MatchInfo matchInfo = BinarySerialization.ReadFromBinaryFile<MatchInfo>(fileName);
List<PlayerInfo> listOfPlayers = BinarySerialization.ReadFromBinaryFile<List<PlayerInfo>>(fileName);
List<RoundEndEvent> listOfRoundEndEvents = BinarySerialization.ReadFromBinaryFile<List<RoundEndEvent>>(fileName);
List<BombPlantEvent> listOfBombPlants = BinarySerialization.ReadFromBinaryFile<List<BombPlantEvent>>(fileName);
List<BombDefuseEvent> listOfBombDefuses = BinarySerialization.ReadFromBinaryFile<List<BombDefuseEvent>>(fileName);
List<BombExplodeEvent> listOfBombExplosions = BinarySerialization.ReadFromBinaryFile<List<BombExplodeEvent>>(fileName);
List<SmokeEvent> listOfSmokes = BinarySerialization.ReadFromBinaryFile<List<SmokeEvent>>(fileName);
List<MolotovEvent> listOfMolotovs = BinarySerialization.ReadFromBinaryFile<List<MolotovEvent>>(fileName);
List<FlashEvent> listOfFlashes = BinarySerialization.ReadFromBinaryFile<List<FlashEvent>>(fileName);
List<GrenadeEvent> listOfGrenades = BinarySerialization.ReadFromBinaryFile<List<GrenadeEvent>>(fileName);
List<KillEvent> listOfKills = BinarySerialization.ReadFromBinaryFile<List<KillEvent>>(fileName);
}
}
These are lists of [Serializable]structs, each of which is having a few variables of types int/float/long/string/char only.
When this code is executed however, I get either an InvalistCastException : InvalidCastException warning
Hovering over the Stream instance shows me this also give sme Read/Write timeout.
If I just continue running the program, I get the SerializationException :
This is the code where all of this occurs:
public static T ReadFromBinaryFile<T>(string filePath)
{
using (Stream stream = File.Open(filePath, FileMode.Open))
{
bool canItTimeout = stream.CanTimeout;
var binaryFormatter = new BinaryFormatter();
return (T)binaryFormatter.Deserialize(stream);
}
}
I'm new to programming and new to c#. I read other questions asked about similar problems, but couldn't get any of their solutions to work or I didn't really understand them much.
Since I don't have rating I cant post more than 2 links, but here are the classes concerned in this problem (as far as i can tell).
My serializer class (copied from another person): https:/./pastebin.com/XHUqgq8y
My save binary file class: https:/./pastebin.com/xqBw9YCY
The structs that I use: https:/./pastebin.com/gDUuvAdh
My load class with: https:/./pastebin.com/NktKzFfD
The program class: https:/./pastebin.com/eTxFmHbn
Apologies for linking half my code, but I really dont have a clue as to what might be causing the error.
Any help is much appreciated, but if possible explain it for newbies like myself. THANKS!
You have a few design issues 1) First you can't keep on opening the same file over again. So pass a stream the serialization method 2) With binary data you do not know the number if items in the list. So you must pass a count to the serialization method. I assume that the matchInfo will contain the counts of the other list objects.
public void LoadFileFromBinary(string fileName)
{
using (Stream stream = File.Open(filePath, FileMode.Open))
{
MatchInfo matchInfo = BinarySerialization.ReadFromBinaryFile<MatchInfo>(stream);
int count = 1;
List<PlayerInfo> listOfPlayers = BinarySerialization.ReadFromBinaryFile<List<PlayerInfo>>(stream,count);
List<RoundEndEvent> listOfRoundEndEvents = BinarySerialization.ReadFromBinaryFile<List<RoundEndEvent>>(stream, count);
List<BombPlantEvent> listOfBombPlants = BinarySerialization.ReadFromBinaryFile<List<BombPlantEvent>>(stream, count);
List<BombDefuseEvent> listOfBombDefuses = BinarySerialization.ReadFromBinaryFile<List<BombDefuseEvent>>(stream, count);
List<BombExplodeEvent> listOfBombExplosions = BinarySerialization.ReadFromBinaryFile<List<BombExplodeEvent>>(stream, count);
List<SmokeEvent> listOfSmokes = BinarySerialization.ReadFromBinaryFile<List<SmokeEvent>>(stream, count);
List<MolotovEvent> listOfMolotovs = BinarySerialization.ReadFromBinaryFile<List<MolotovEvent>>(stream, count);
List<FlashEvent> listOfFlashes = BinarySerialization.ReadFromBinaryFile<List<FlashEvent>>(stream, count);
List<GrenadeEvent> listOfGrenades = BinarySerialization.ReadFromBinaryFile<List<GrenadeEvent>>(stream, count);
List<KillEvent> listOfKills = BinarySerialization.ReadFromBinaryFile<List<KillEvent>>(stream, count);
}
}
public static List<T> ReadFromBinaryFile<T>(Stream stream, int count)
{
List<T> data = new List<T>();
var binaryFormatter = new BinaryFormatter();
for (int i = 0; i < count; i++)
{
data.Add((T)binaryFormatter.Deserialize(stream));
}
return data;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.