public sealed override HasherSearchResult FindLongestMatch(int ip, int maxLength, int maxDistance, int dictionaryStart, int lastDistance, int bestLenIn)
        {
            var result = FindLongestBackReference(ip, maxLength, maxDistance, lastDistance, bestLenIn);

            if (dictionary != null && !result.FoundAnything && dictionaryMatches >= (dictionaryLookups >> 7))
            {
                DictionaryIndexEntry?bestEntry = null;
                int bestScore = int.MinValue;

                foreach (var entry in dictionary.Index.Find(new ArraySegment <byte>(input, ip, input.Length - ip), minLength: 4, maxLength))
                {
                    var copyLength = entry.CopyLength;
                    var wordIndex  = dictionary.Format.UnpackWordIndex(copyLength, entry.Packed);

                    if (!LimitedDictionary.CanUseWord(copyLength, wordIndex, shallow: true))
                    {
                        continue;
                    }

                    var transform = dictionary.Transforms[dictionary.Format.UnpackTransformIndex(copyLength, entry.Packed)];

                    if (!LimitedDictionary.CanUseTransform(transform))
                    {
                        continue;
                    }

                    int distance = dictionaryStart + entry.Packed;
                    int score    = HasherSearchResult.BackwardReferenceScore(entry.OutputLength, distance);

                    if (score > bestScore)  // TODO check distance to make sure it doesn't go beyond what can be represented?
                    {
                        bestEntry = entry;
                        bestScore = score;
                    }
                }

                ++dictionaryLookups;

                if (bestEntry != null)
                {
                    ++dictionaryMatches;
                    return(new HasherSearchResult(bestEntry.Value, bestScore));
                }
            }

            return(result.Build());
        }
Ejemplo n.º 2
0
    // Use this for initialization
    public BinManager()
    {
        // Create default palette
        if (activePalette == null)
        {
            CreatePalette(Consts.DEF_PAL);
        }

        // Open real.bin
        fsReal = File.OpenRead(Application.dataPath + Consts.REAL);
        if (fsReal == null)
        {
            Debug.Log("Open real.bin failed.");
            Application.Quit();
        }

        // Read adrn.bin and store information
        FileStream fsAdrn = File.OpenRead(Application.dataPath + Consts.ADRN);

        if (fsAdrn == null)
        {
            Debug.Log("Open adrn.bin failed.");
            Application.Quit();
        }
        int adrnSize = 80;
        int adrnCnt  = (int)fsAdrn.Length / adrnSize;

        bitmapTable = new uint[0xffff];         // 0xffff = Max Map_BitmapNo
        adrnData    = new List <AdrnData>();
        for (int i = 0; i < adrnCnt; i++)
        {
            byte[] bufferAdrn = new byte[adrnSize];
            fsAdrn.Read(bufferAdrn, 0, adrnSize);
            if (adrnData.Count <= i)
            {
                adrnData.Add(new AdrnData());
            }
            adrnData[i] = ByteToStruct <AdrnData>(bufferAdrn);
            AdrnData adrnBlock   = adrnData[i];
            int      mapbitmapno = (int)adrnBlock.Map_BitmapNo;
            if (mapbitmapno != 0)
            {
                bitmapTable[(int)mapbitmapno] = adrnBlock.BitmapNo;
                // Some hot fixes
                if ((12082 <= mapbitmapno && mapbitmapno <= 12811) ||
                    (10132 <= mapbitmapno && mapbitmapno <= 10136))
                {
                    adrnBlock.Map_Hit = (ushort)(300 + adrnBlock.Map_Hit % 100);
                }
            }
            else
            {
                bitmapTable[(int)mapbitmapno] = 0;
            }
        }
        fsAdrn.Close();

        // Read spr.bin and spradrn.bin, and store sprite information
        FileStream fsSpr     = File.OpenRead(Application.dataPath + Consts.SPR);
        FileStream fsSpradrn = File.OpenRead(Application.dataPath + Consts.SPRADRN);

        if (fsSpr == null || fsSpradrn == null)
        {
            Debug.Log("Open spr.bin and spradrn.bin failed.");
            Application.Quit();
        }
        int spradrnSize = 12;
        int index       = 0;

        spradrnData = new Dictionary <uint, SpradrnData>();
        while (true)
        {
            byte[] bufferSpradrn = new byte[spradrnSize];
            int    bytesread     = fsSpradrn.Read(bufferSpradrn, 0, spradrnSize);
            if (bytesread <= 0)
            {
                break;                             // End of file
            }
            SpradrnData spradrnBlock = new SpradrnData();
            spradrnBlock       = ByteToStruct <SpradrnData>(bufferSpradrn);
            spradrnBlock.sprNo = spradrnBlock.sprNo - Consts.SPRSTART;
            spradrnData.Add(spradrnBlock.sprNo, spradrnBlock);
            index++;
        }

        index = 0;
        int keyCnt = 0;

        sprData = new Dictionary <uint, SprData>();
        while (true)
        {
            // If all keys are found, stop loop
            if (keyCnt >= spradrnData.Count)
            {
                break;
            }

            SpradrnData spradrnBlock;
            bool        ret = spradrnData.TryGetValue((uint)index, out spradrnBlock);

            // If sprNo used
            if (ret == true)
            {
                // Key found counter ++
                keyCnt++;
                // New sprData block
                SprData sprBlock = new SprData();
                // Get animList array size
                sprBlock.animSize = spradrnBlock.animSize;
                // New animList array by size
                sprBlock.ptAnimList = new AnimList[(int)sprBlock.animSize];
                // Seek file to offset indicated by sprAdrn info
                fsSpr.Seek((long)spradrnBlock.offset, SeekOrigin.Begin);
                for (int i = 0; i < (int)sprBlock.animSize; i++)
                {
                    byte[] buf = new byte[12];                     // 12 = Size per AnimList
                    fsSpr.Read(buf, 0, 12);

                    // Can't use ByteToStruct, size not matching
                    // Read pre-frame info
                    int indexBuf = 0;
                    sprBlock.ptAnimList[i].dir = BitConverter.ToUInt16(buf, indexBuf);
                    indexBuf += sizeof(ushort);
                    sprBlock.ptAnimList[i].no = BitConverter.ToUInt16(buf, indexBuf);
                    indexBuf += sizeof(ushort);
                    sprBlock.ptAnimList[i].dtAnim = BitConverter.ToUInt32(buf, indexBuf);
                    indexBuf += sizeof(uint);
                    sprBlock.ptAnimList[i].frameCnt = BitConverter.ToUInt32(buf, indexBuf);
                    indexBuf += sizeof(uint);

                    if (sprBlock.ptAnimList[i].frameCnt != 0)
                    {
                        sprBlock.ptAnimList[i].dtAnim = sprBlock.ptAnimList[i].dtAnim / (sprBlock.ptAnimList[i].frameCnt << 4);
                        if (sprBlock.ptAnimList[i].dtAnim < 1)
                        {
                            sprBlock.ptAnimList[i].dtAnim = 1;
                        }
                    }

                    // Read info per frame
                    // Actual size per frame block is 10 = 4 + 2 + 2 + 2
                    int size = 5 * (int)sprBlock.ptAnimList[i].frameCnt;
                    buf = new byte[size * sizeof(ushort)];
                    fsSpr.Read(buf, 0, size * sizeof(ushort));
                    indexBuf = 0;
                    // New frameList by frameCnt
                    sprBlock.ptAnimList[i].ptFrameList = new FrameList[(int)sprBlock.ptAnimList[i].frameCnt];
                    for (int j = 0; j < (int)sprBlock.ptAnimList[i].frameCnt; j++)
                    {
                        sprBlock.ptAnimList[i].ptFrameList[j].BmpNo = BitConverter.ToUInt32(buf, indexBuf);
                        indexBuf += sizeof(uint);
                        sprBlock.ptAnimList[i].ptFrameList[j].PosX = BitConverter.ToInt16(buf, indexBuf);
                        indexBuf += sizeof(short);
                        sprBlock.ptAnimList[i].ptFrameList[j].PosY = BitConverter.ToInt16(buf, indexBuf);
                        indexBuf += sizeof(short);
                        sprBlock.ptAnimList[i].ptFrameList[j].SoundNo = BitConverter.ToUInt16(buf, indexBuf);
                        indexBuf += sizeof(ushort);
                    }
                }
                // Done reading 1 SprData
                sprData.Add((uint)index, sprBlock);
            }
            index++;
        }
        // Read bin files all done

        // Initialize texture dictionary
        textureData = new LimitedDictionary <int, Texture2D>();
        textureData.MaxItemsToHold = Consts.MAX_TEXTUREDATA;
    }