Context on the client and server

Server is a custom 634 server that I wrote. I don't think the issue is there since the client can fetch cache data fine for a few archives when the cache is already downloaded by the client. The client is the 634 client from Displee's archive I think though I can't find it anymore. It's been deobfuscated further using Runelite deob, though I've also had this bug on the original client without the additional deob process done. No other modification have been made by me apart from IP/port change and a few switches for RSA/Isaac/etc. Not sure what the original owner did though..

The problem

I'm having trouble downloading the cache from my 634 client when I connect to my server. If I copy the cache files directly to the client's cache folder, the client can load it and access the data, but it's unable to download it from nothing. Right now the current behavior is as follow: Connection is established, archive #255.255 is fetched and received by the client (data is all correct, length of 2798 with the same data server/client), but after that, it just crashes at 2%.

Theorical cause

It seems to create a bunch of empty index files early after the client is initiated, even before archive #255.255 is fetched:

Code:
    Class234(File file, String string, long l) throws IOException {
        if ((l ^ 0xffffffffffffffffL) == 0L)
            l = 9223372036854775807L;
        if (l < file.length())
            file.delete();
        aRandomAccessFile3036 = new RandomAccessFile(file, string);
        aLong3037 = l;
        aLong3039 = 0L;
        aFile3046 = file;
        int i = aRandomAccessFile3036.read();
        if ((i ^ 0xffffffff) != 0 && !string.equals("r")) {
            aRandomAccessFile3036.seek(0L);
            aRandomAccessFile3036.write(i);
        }
        Logger.flash("init " + aFile3046.getName() + "(" + !string.equals("r") + ")");
        aRandomAccessFile3036.seek(0L);
    }
Files created (1 byte of data):

Code:
[08-22-20 11:42:28 PM] init random.dat(true)
[08-22-20 11:42:28 PM] init main_file_cache.dat2(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx255(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx0(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx1(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx2(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx3(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx4(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx5(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx6(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx7(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx8(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx9(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx10(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx11(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx12(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx13(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx14(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx15(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx16(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx17(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx18(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx19(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx20(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx21(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx22(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx23(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx24(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx25(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx26(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx27(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx28(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx29(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx30(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx31(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx32(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx33(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx34(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx35(true)
[08-22-20 11:42:28 PM] init main_file_cache.idx36(true)
[08-22-20 11:42:28 PM] init jagex_runescape_preferences.dat(true);
Then at some point it does this check when attempting to fetch the data from archive #255.255 (file with 1 byte length) for index #13/32/33:

Code:
if ((aClass78_1938.method787(0) ^ 0xffffffffffffffffL)
                        > ((long) (i_2_ * 6 - -6) ^ 0xffffffffffffffffL)) {
                    byte[] is_3_ = null;
                    return is_3_;
                }
Which essentially means, if the file length of index #255.255 is < the offset we're looking to load from the file (in this case 6*indexId + 6 for the header), return null. The length is always 1 since nothing is ever written to the file. So essentially, the problem is caused by the client not writing the data it receives from the server for archive #255.255 OR attempting to load the data too fast from the file before writing the data to it.

Edits

#1 -I deleted a part of the code that preloads data from #255.255 since I noticed it wasn't there on 718. It now crashes by giving a js5connect error, but at least I'm getting request for archive #255.32/33/13 on the server.

#2 - I'm getting packet #6 before the request for #255.255 and also before the requests before #255.13/32/33. I'm skipping 7 bytes and that length seems fine since it's decoding archive+file Id correctly for the following packet, but maybe I need to do something with the data? I don't think it's the encryption value since that's packet #4. Anyone know what that packet is used for? Here's the raw data from it:

Code:
[01:58:49 AM] unknown6 [0] 0
[01:58:49 AM] unknown6 [1] 0
[01:58:49 AM] unknown6 [2] 3 // 24- bit int with static value of 3
[01:58:49 AM] unknown6 [3] 3
[01:58:49 AM] unknown6 [4] 0
[01:58:49 AM] unknown6 [5] 0
[01:58:49 AM] unknown6 [6] 0 // idk about these 4
#3 - Found out I had reverse the condition for priority packet from packetId == 1 to packetId == 0. After fixing that and keeping the changes I made in edit #2, I'm now at 3%. It seems to fetch some the data for index #13-32-33... and then stop.

#4 - Fixed here's how I fixed it in the next paragraph:

The fix

Had to change this part on my server:

Code:
            if (packet == IncomingPacket.CACHE_REQUEST_0) {
                ((CacheRequestPacket) packetImpl).setPriority(true);
            }
To this;

Code:
            if (packet == IncomingPacket.CACHE_REQUEST_1) {
                ((CacheRequestPacket) packetImpl).setPriority(true);
            }
And modify this part on my client:

Code:
Class314_Sub1(int indexId, Class137 index, Class137 index255,
                  Class248 cacheRequest, Class112 class112, int i_24_, byte[] is,
                  int i_25_, boolean bool) {
        aClass356_6360 = new Class356(16);
        anInt6373 = 0;
        aClass262_6369 = new Class262();
        aLong6374 = 0L;
        this.indexId = indexId;
        this.index = index;
        if (this.index != null) {
            aBoolean6368 = true;
            aClass262_6372 = new Class262();
        } else
            aBoolean6368 = false;
        anInt6344 = i_24_;
        aByteArray6346 = is;
        anInt6352 = i_25_;
        this.index255 = index255;
        aBoolean6375 = bool;
        this.cacheRequest = cacheRequest;
        aClass112_6354 = class112;
        // FIX CRASH BUG
        //if (this.index255 != null)
        if (this.index255 != null && this.index255.method1161((byte) -4, this.indexId) != null) 
           aClass348_Sub42_Sub16_6355
                    = aClass112_6354.method1055(this.index255, this.indexId);
    }
This client part is just some weird data preload from archive #255.255 that still occurs even when the file is empty (only 1 empty byte in the file). It's disabled on matrix's 718 client.