Ejemplo n.º 1
0
        private bool LoadLadderFile()
        {
            if (!File.Exists(LadderFile))
            {
                return(false);
            }
            try
            {
                TLadderHeader  Header       = new TLadderHeader();
                TLadderIndex[] LadderIndex  = new TLadderIndex[35];
                TLadderInfo    LadderInfo   = new TLadderInfo();
                FileStream     LadderStream = new FileStream(LadderFile, FileMode.Open, FileAccess.Read);
                BinaryReader   LadderReader = new BinaryReader(LadderStream);

                byte[] buf = new Byte[Marshal.SizeOf(Header)];
                int    i, j, count = 0;
                LadderReader.Read(buf, 0, buf.Length);
                Header = (TLadderHeader)D2DBS.core.BytesToStruct(buf, Header.GetType());
                for (i = 0; i < MAX_TYPE; i++)
                {
                    buf = new Byte[Marshal.SizeOf(LadderIndex[i])];
                    LadderReader.Read(buf, 0, buf.Length);
                    LadderIndex[i] = (TLadderIndex)D2DBS.core.BytesToStruct(buf, LadderIndex[i].GetType());
                }
                for (i = 0; i < MAX_TYPE; i++)
                {
                    Ladder[i].Clear();
                    CharIndex[i].Clear();
                    for (j = 0; j < LadderIndex[i].number; j++)
                    {
                        buf = new Byte[Marshal.SizeOf(LadderInfo)];
                        LadderReader.Read(buf, 0, buf.Length);
                        LadderInfo = (TLadderInfo)D2DBS.core.BytesToStruct(buf, LadderInfo.GetType());
                        CharIndex[i][LadderInfo.charname] = Ladder[i].Count;
                        Ladder[i].Add(LadderInfo);
                        count++;
                    }
                }
                LadderStream.Close();
                LadderStream.Dispose();
                D2DBS.log.Write("info", "Ladder loaded successfully, " + count.ToString() + " char(s) in total.");
                return(true);
            }
            catch
            {
                D2DBS.log.Write("error", "Failed to load ladder file, rebuild it.");
            }
            return(false);
        }
Ejemplo n.º 2
0
        private bool SaveLadderFile()
        {
            lock (LadderLock)
            {
                TLadderIndex[] LadderIndex = new TLadderIndex[35];
                TLadderHeader  Header = new TLadderHeader();
                TLadderInfo    emptydata = new TLadderInfo(true);
                int            i, j;
                int            count = 0;
                int            start = Marshal.SizeOf(Header) + Marshal.SizeOf(LadderIndex[0]) * 35;
                for (i = 0; i < MAX_TYPE; i++)
                {
                    LadderIndex[i].type   = i;
                    LadderIndex[i].offset = start;
                    LadderIndex[i].number = GetNumberByType(i);
                    start += LadderIndex[i].number * 24;
                }
                byte[]       buf;
                MemoryStream MSLadderBuffer = new MemoryStream();
                BinaryWriter LadderBuffer   = new BinaryWriter(MSLadderBuffer);
                Header.maxtype = MAX_TYPE;
                LadderBuffer.Write(D2DBS.core.StructToBytes(Header));
                for (i = 0; i < MAX_TYPE; i++)
                {
                    LadderBuffer.Write(D2DBS.core.StructToBytes(LadderIndex[i]));
                }
                for (i = 0; i < MAX_TYPE; i++)
                {
                    for (j = 0; j < LadderIndex[i].number; j++)
                    {
                        if (j >= Ladder[i].Count)
                        {
                            LadderBuffer.Write(D2DBS.core.StructToBytes(emptydata));
                        }
                        else
                        {
                            LadderBuffer.Write(D2DBS.core.StructToBytes(Ladder[i][j]));
                            count++;
                        }
                    }
                }
                LadderBuffer.Close();

                buf = MSLadderBuffer.ToArray();
                MSLadderBuffer.Close();
                MSLadderBuffer.Dispose();

                int Checksum = CalcChecksum(buf);
                Header.checksum = Checksum;
                buf[4]          = (byte)(Checksum & 0xFF);
                buf[5]          = (byte)((Checksum >> 8) & 0xFF);
                buf[6]          = (byte)((Checksum >> 16) & 0xFF);
                buf[7]          = (byte)((Checksum >> 24) & 0xFF);

                if (D2DBS.config["enablebackup"] != "0" && File.Exists(LadderFile))
                {
                    try
                    {
                        if (File.Exists(LadderBakFile))
                        {
                            File.Delete(LadderBakFile);
                        }
                        File.Move(LadderFile, LadderBakFile);
                        D2DBS.log.Write("info", "Ladder backup executed.");
                    }
                    catch (Exception e)
                    {
                        D2DBS.log.Write("error", "Backup failed: " + e.Message);
                    }
                }

                FileStream LadderStream = new FileStream(LadderFile, FileMode.Create, FileAccess.Write);
                LadderStream.Write(buf, 0, buf.Length);
                LadderStream.Close();
                LadderStream.Dispose();
                D2DBS.log.Write("info", "Ladder saved successfully, " + count.ToString() + " char(s) in total.");
                return(true);
            }
        }