using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace DEServer { struct Creature { public const int VERSION = 1; public string name; public byte[] pass; public string code; public string owner; public int version; public int HP; public int ATK; public int DEF; public int SPC; public int AGI; public int baseHP; public int baseATK; public int baseDEF; public int baseSPC; public int baseAGI; public int baseEXP; public int totalEXP; public int element; public List traits; public List moves; public List evolvedPasses; public Creature(byte[] serialization, bool full = false) { this.moves = new List(); this.traits = new List(); MemoryStream ms = new MemoryStream(serialization); this.version = ms.ReadByte(); //Length of pass (1 byte) int passlength = ms.ReadByte(); //Length of name (1 byte) int namelength = ms.ReadByte(); //Length of code (1 byte) int codelength = ms.ReadByte(); //Length of owner (1 byte) int ownerlength = ms.ReadByte(); //Number of moves (1 byte) int numMoves = ms.ReadByte(); //Number of traits (2 bytes) int numTraits = TwoBytes(ms.ReadByte(), ms.ReadByte()); //Pass pass = new byte[passlength]; ms.Read(pass, 0, passlength); //Name byte[] buffer = new byte[namelength]; ms.Read(buffer, 0, namelength); this.name = Encoding.ASCII.GetString(buffer); //Code buffer = new byte[codelength]; ms.Read(buffer, 0, codelength); this.code = Encoding.ASCII.GetString(buffer); //Owner buffer = new byte[ownerlength]; ms.Read(buffer, 0, ownerlength); this.owner = Encoding.ASCII.GetString(buffer); //Moves (2 bytes each) for (int i = 0; i < numMoves; i++) { this.moves.Add(TwoBytes(ms.ReadByte(), ms.ReadByte())); } //Traits (2 bytes each) for (int i = 0; i < numTraits; i++) { this.traits.Add(TwoBytes(ms.ReadByte(), ms.ReadByte())); } //Base HP (2 bytes) this.baseHP = TwoBytes(ms.ReadByte(), ms.ReadByte()); //Base ATK (2 bytes) this.baseATK = TwoBytes(ms.ReadByte(), ms.ReadByte()); //Base DEF (2 bytes) this.baseDEF = TwoBytes(ms.ReadByte(), ms.ReadByte()); //Base SPC (2 bytes) this.baseSPC = TwoBytes(ms.ReadByte(), ms.ReadByte()); //Base AGI (2 bytes) this.baseAGI = TwoBytes(ms.ReadByte(), ms.ReadByte()); //EXP (4 bytes) this.baseEXP = FourBytes(ms.ReadByte(), ms.ReadByte(), ms.ReadByte(), ms.ReadByte()); //TOTAL EXP (4 bytes) this.totalEXP = FourBytes(ms.ReadByte(), ms.ReadByte(), ms.ReadByte(), ms.ReadByte()); //Element (1 byte) this.element = ms.ReadByte(); //HP (2 bytes) this.HP = TwoBytes(ms.ReadByte(), ms.ReadByte()); //ATK (2 bytes) this.ATK = TwoBytes(ms.ReadByte(), ms.ReadByte()); //DEF (2 bytes) this.DEF = TwoBytes(ms.ReadByte(), ms.ReadByte()); //SPC (2 bytes) this.SPC = TwoBytes(ms.ReadByte(), ms.ReadByte()); //AGI (2 bytes) this.AGI = TwoBytes(ms.ReadByte(), ms.ReadByte()); evolvedPasses = new List(); if (full) { evolvedPasses = new List(); int numberOfEvolveds = TwoBytes(ms.ReadByte(), ms.ReadByte()); for (int i = 0; i < numberOfEvolveds; i++) { int passLength = ms.ReadByte(); List thePass = new List(); for (int ii = 0; ii < passlength; ii++) { thePass.Add((byte)ms.ReadByte()); } evolvedPasses.Add(thePass.ToArray()); } } //PrintStats(); } public void PrintStats() { string passPhrase = ""; for (int i = 0; i < pass.Length; i++) passPhrase += Convert.ToString(pass[i] + " "); Console.Out.WriteLine("Received character:\n" + this.name + "\n" + passPhrase + "\n" + this.code); Console.Out.WriteLine("Owner: " + this.owner); string movestring = "Moves: "; for (int i = 0; i < this.moves.Count; i++) movestring += this.moves[i] + " "; Program.Log(movestring); string traitstring = "Traits: "; for (int i = 0; i < this.traits.Count; i++) traitstring += this.traits[i] + " "; Program.Log(traitstring); Program.Log("STATS:"); Program.Log("HP: " + baseHP + " / " + HP); Program.Log("ATK: " + baseATK + " / " + ATK); Program.Log("DEF: " + baseDEF + " / " + DEF); Program.Log("SPC: " + baseSPC + " / " + SPC); Program.Log("AGI: " + baseAGI + " / " + AGI); Program.Log("EXP: " + baseEXP + " / " + totalEXP); } public static int TwoBytes(int byte1, int byte2) { byte1 = (byte)byte1; byte2 = (byte)byte2; return byte1 * 256 + byte2; } public static int FourBytes(int byte1, int byte2, int byte3, int byte4) { byte1 = (byte)byte1; byte2 = (byte)byte2; byte4 = (byte)byte4; byte3 = (byte)byte3; return byte1 * 256 * 256 * 256 + byte2 * 256 * 256 + byte3 * 256 + byte4; } public byte[] Serialize(bool full = false) { List toReturn = new List(); toReturn.Add((byte)VERSION); if (full) { //Length of pass (1 byte) toReturn.Add((byte)pass.Length); } //Length of name (1 byte) toReturn.Add((byte)name.Length); //Length of code (1 byte) toReturn.Add((byte)code.Length); //Lenght of owner (1 byte) toReturn.Add((byte)owner.Length); //Number of moves (1 byte) toReturn.Add((byte)moves.Count); //Number of traits (2 bytes) byte[] tr = toTwoBytes(traits.Count); toReturn.Add(tr[0]); toReturn.Add(tr[1]); if (full) { //Pass for (int i = 0; i < pass.Length; i++) { toReturn.Add(pass[i]); } } //Name tr = Encoding.ASCII.GetBytes(name); for (int i = 0; i < name.Length; i++) { toReturn.Add(tr[i]); } //Code tr = Encoding.ASCII.GetBytes(code); for (int i = 0; i < code.Length; i++) { toReturn.Add(tr[i]); } //Owner tr = Encoding.ASCII.GetBytes(owner); for (int i = 0; i < owner.Length; i++) { toReturn.Add(tr[i]); } //Moves (2 bytes each) for (int i = 0; i < moves.Count; i++) { tr = toTwoBytes(moves[i]); toReturn.Add(tr[0]); toReturn.Add(tr[1]); } //Traits (2 bytes each) for (int i = 0; i < traits.Count; i++) { tr = toTwoBytes(traits[i]); toReturn.Add(tr[0]); toReturn.Add(tr[1]); } //Base HP (2 bytes) tr = toTwoBytes(this.baseHP); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //Base ATK (2 bytes) tr = toTwoBytes(baseATK); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //Base DEF (2 bytes) tr = toTwoBytes(baseDEF); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //Base SPC (2 bytes) tr = toTwoBytes(baseSPC); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //Base AGI (2 bytes) tr = toTwoBytes(baseAGI); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //EXP (4 bytes) tr = toFourBytes(baseEXP); toReturn.Add(tr[0]); toReturn.Add(tr[1]); toReturn.Add(tr[2]); toReturn.Add(tr[3]); //TOTAL EXP (4 bytes) tr = toFourBytes(totalEXP); toReturn.Add(tr[0]); toReturn.Add(tr[1]); toReturn.Add(tr[2]); toReturn.Add(tr[3]); //Element (1 byte) toReturn.Add((byte)this.element); //HP (2 bytes) tr = toTwoBytes(this.HP); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //ATK (2 bytes) tr = toTwoBytes(this.ATK); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //DEF (2 bytes) tr = toTwoBytes(this.DEF); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //SPC (2 bytes) tr = toTwoBytes(this.SPC); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //AGI (2 bytes) tr = toTwoBytes(this.AGI); toReturn.Add(tr[0]); toReturn.Add(tr[1]); //Add known passes if (full) { tr = toTwoBytes(this.evolvedPasses.Count); toReturn.Add(tr[0]); toReturn.Add(tr[1]); for(int i = 0; i < this.evolvedPasses.Count; i++) { toReturn.Add((byte)this.evolvedPasses[i].Length); for (int ii = 0; ii < this.evolvedPasses[i].Length; ii++) { toReturn.Add(this.evolvedPasses[i][ii]); } } } return toReturn.ToArray(); } public byte[] toTwoBytes(int source) { byte[] toReturn = new byte[2]; toReturn[0] = (byte)Math.Floor((float)source / 256f); toReturn[1] = (byte)(source - toReturn[0] * 256); return toReturn; } public byte[] toFourBytes(int source) { byte[] toReturn = new byte[4]; toReturn[0] = (byte)Math.Floor((float)source / (float)(256 * 256 * 256)); source -= toReturn[0] * 256 * 256 * 256; toReturn[1] = (byte)Math.Floor((float)source / (float)(256 * 256)); source -= toReturn[1] * 256 * 256; toReturn[2] = (byte)Math.Floor((float)source / 256f); toReturn[3] = (byte)(source - toReturn[0] * 256); return toReturn; } } }