~mindsharing~ (C# snippets, Parse some lod files)
#1

hi Big Grin i wanna share an C# class maybe anyone can build some nice stuff with it

 

in this class are 3 Functions, and 3 structs

 

parse/struct for Itemname.lod, mobname.lod and options.lod

 

i think its already known how to read this files, ok here the class Smile

 

class ParseLod
{

//parse itemname.lod

public struct Item
{
public int ID;
public string Name;
public string Description;
public string ToString()
{
return ID + "\t" + Name + "\t" + Description;
}
}

public static Mob[] mobname(string path)
{
byte[] bytes = File.ReadAllBytes(path);
long len = bytes.Length;
long i = 0;
int i2 = 0;
long i3 = 0;
int namelen;
string mobname;
Mob[] parse = new Mob[0];
do
{
Array.Resize(ref parse, (int)i3 + 1);
parse[i3].ID = (bytes[i + 4] + (bytes[i + 5] * 256) + (bytes[i + 6] * 256) + (bytes[i + 7] * 256));
namelen = (bytes[i + 8] + bytes[i + 9] + bytes[i + 10] + bytes[i + 11]);
mobname = "";
i2 = 1;
while (i2 < namelen + 1)
{
i2++;
mobname = mobname + (char)(bytes[i + 10 + i2]);
}
parse[i3].Name = mobname;
i += 8 + namelen;
i3++;
} while (i < len - 4);
return parse;
}

//parse mobname.lod

public struct Mob
{
public int ID;
public string Name;
public string ToString()
{
return ID + "\t" + Name;
}
}

public static Item[] itemname(string path)
{
byte[] bytes = File.ReadAllBytes(path);
long len = bytes.Length;
long i = 0;
int i2 = 0;
long i3 = 0;
int namelen, deslen;
string Itemname, des;
Item[] parse = new Item[0];
do
{
Array.Resize(ref parse, (int)i3 + 1);
parse[i3].ID = (bytes[i + 4] + (bytes[i + 5] * 256) + (bytes[i + 6] * 256) + (bytes[i + 7] * 256));
namelen = (bytes[i + 8] + bytes[i + 9] + bytes[i + 10] + bytes[i + 11]);
Itemname = "";
i2 = 1;
while (i2 < namelen + 1)
{
i2++;
Itemname = Itemname + (char)(bytes[i + 10 + i2]);
}
parse[i3].Name = Itemname;
deslen = (bytes[i + 12 + namelen] + bytes[i + 13 + namelen] + bytes[i + 14 + namelen] + bytes[i + 15 + namelen]);
des = "";
i2 = 1;
while (i2 < deslen + 1)
{
i2++;
des = des + (char)(bytes[i + 14 + i2 + namelen]);
}
parse[i3].Description = des;
i += 12 + namelen + deslen;
i3++;
} while (i < len - 4);
return parse;
}

//parse option.lod

public struct Seal
{
public int ID;
public int SubID;
public string Name;
public int Value1;
public int Value2;
public int Value3;
public int Value4;
public int Value5;
public int Value6;
public int Value7;
public string ToString()
{
return ID + "\t" + SubID + "\t" + Name + "\t" + Value1 + " | " + Value2 + " | " + Value3 + " | " + Value4 + " | " + Value5 + " | " + Value6 + " | " + Value7;
}
}

public static Seal[] option(string path)
{
byte[] bytes = File.ReadAllBytes(path);
long len = bytes.Length;
long i = 0;
int i2 = 0;
long i3 = 0;
int namelen;
string sealname;
Seal[] parse = new Seal[0];
do
{
Array.Resize(ref parse, (int)i3 + 1);

parse[i3].ID = (bytes[i + 4] + (bytes[i + 5] * 256) + (bytes[i + 6] * 256) + (bytes[i + 7] * 256));
parse[i3].SubID = (bytes[i + 8] + (bytes[i + 9] * 256) + (bytes[i + 10] * 256) + (bytes[i + 11] * 256));
namelen = (bytes[i + 12] + (bytes[i + 13] * 256) + (bytes[i + 14] * 256) + (bytes[i + 15] * 256));
sealname = "";
i2 = 1;
while (i2 < namelen + 1)
{
i2++;
sealname = sealname + (char)(bytes[i + 14 + i2]);
}
parse[i3].Name = sealname;

parse[i3].Value1 = (bytes[i + 16 + namelen] + (bytes[i + 17 + namelen] * 256) + (bytes[i + 18 + namelen] * 256) + (bytes[i + 19 + namelen] * 256));
parse[i3].Value2 = (bytes[i + 20 + namelen] + (bytes[i + 21 + namelen] * 256) + (bytes[i + 22 + namelen] * 256) + (bytes[i + 23 + namelen] * 256));
parse[i3].Value3 = (bytes[i + 24 + namelen] + (bytes[i + 25 + namelen] * 256) + (bytes[i + 26 + namelen] * 256) + (bytes[i + 27 + namelen] * 256));
parse[i3].Value4 = (bytes[i + 28 + namelen] + (bytes[i + 29 + namelen] * 256) + (bytes[i + 30 + namelen] * 256) + (bytes[i + 31 + namelen] * 256));
parse[i3].Value5 = (bytes[i + 32 + namelen] + (bytes[i + 33 + namelen] * 256) + (bytes[i + 34 + namelen] * 256) + (bytes[i + 35 + namelen] * 256));
parse[i3].Value6 = (bytes[i + 36 + namelen] + (bytes[i + 37 + namelen] * 256) + (bytes[i + 38 + namelen] * 256) + (bytes[i + 39 + namelen] * 256));
parse[i3].Value7 = (bytes[i + 40 + namelen] + (bytes[i + 41 + namelen] * 256) + (bytes[i + 42 + namelen] * 256) + (bytes[i + 43 + namelen] * 256));
i += 40 + namelen;
i3++;
} while (i < len - 4);
return parse;
}
}
}

 

and an little example

 

ParseLod.Mob[] example = ParseLod.mobname(@"C:\ILC_DARK\Data\mobname.lod");

int i = 0;
while (i < example.Length)
{
Console.WriteLine(example[i].ToString());
i++;
}

 

if anywan se an way to optimize, tell me Big Grin if any one wanna usw this class, do it Big Grin

 

~ILAN12346

#2


struct tItem
{
public int ItemID;
public string Name;
public string Description;

public cItem(int ItemID, string Name, string Description )
{
this.ItemID = ItemID;
this.Name = Name;
this.Description = Description;
}
}

class ItemReader
{
public cItem[] ReadFile(string FileName)
{

List data = new List();

using( BinaryReader b = new BinaryReader( File.Open( FileName, FileMode.Open ) ) )
{
int lastID = b.ReadInt32();

while( b.BaseStream.Position data.Add( new cItem ( b.ReadInt32(),
new string( b.ReadChars( b.ReadInt32() ) ),
new string( b.ReadChars( b.ReadInt32() ) ) ) );

}

return data;
}
}

 

I think thats a little easier Wink

#3

o_O The terrifying moment where you realize YES... wizas code rly work, u wanna scroll down but... thats all

 

its like you make your Math homework... u need 1 page and an classmate next of you need ~8 lines WHITOUT ANY FKN NUMBER but it work and is much easier xD.....

 

nice 1 wiza Big Grin

 

~ILAn12346

#4

That the basic use of the Binary Reader, it can read most Streams.

 

But i like your example also, because it also works and is very creative Tongue

But i think to read and write binary files its better to use the BinaryReader and BinaryWriter.

 

In C++ its even more cool, u can read a struct from a file without having to read the members seperate

#5

i just wondered why i don´t use Binary reader :confused:

 

edit: ahhhhhhh cuz i wrote the mobnamelod parse function since 1 year @ 2 or 3 am

vs an friend, he sayed he can write an mobname.lod parser faster than me, i Use Autohotkey and he AutoIT, i found my source of it yesterday and port it directly to C# Big Grin

 

regards to Fabi202cool Big Grin

 

~ILAn12346

#6

In C++ its even more cool, u can read a struct from a file without having to read the members seperate

The problem with streaming a c++ struct to a file/network/database is that it only works on fix size struct but on dynamic size structs will not work correctly(you would need to create a method to stream each members)

this struct will work 2 bytes to id and 12 to name
struct data{
int id;
char name[12];
};
//this struct will not work 2 bytes to id and 4 bytes is the pointer address in memory.
struct data{
int id;
char* name;
}

#7
Ah yes i always wondered how to do it with strings that are not zero terminated

#8

why would anyone read a file like that ! ? ? ? ? this is not a mind i wish to know here man simple simple simple 2

 

datatypes like wizatek uses and like everyother sane person would when reading files!



Forum Jump:


Users browsing this thread: 2 Guest(s)