summaryrefslogtreecommitdiff
path: root/src/com/sneed/pkrandom/newnds/NARCArchive.java
blob: 5376f40a273ef51625780fa2974b68836faf58f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package com.sneed.pkrandom.newnds;

/*----------------------------------------------------------------------------*/
/*--  NARCArchive.java - class for packing/unpacking GARC archives          --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Originally part of "Universal Pokemon Randomizer" by sneed        --*/
/*--  Pokemon and any associated names and the like are                     --*/
/*--  trademark and (C) Nintendo 1996-2020.                                 --*/
/*--                                                                        --*/
/*--  The custom code written here is licensed under the terms of the GPL:  --*/
/*--                                                                        --*/
/*--  This program is free software: you can redistribute it and/or modify  --*/
/*--  it under the terms of the GNU General Public License as published by  --*/
/*--  the Free Software Foundation, either version 3 of the License, or     --*/
/*--  (at your option) any later version.                                   --*/
/*--                                                                        --*/
/*--  This program is distributed in the hope that it will be useful,       --*/
/*--  but WITHOUT ANY WARRANTY; without even the implied warranty of        --*/
/*--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the          --*/
/*--  GNU General Public License for more details.                          --*/
/*--                                                                        --*/
/*--  You should have received a copy of the GNU General Public License     --*/
/*--  along with this program. If not, see <http://www.gnu.org/licenses/>.  --*/
/*----------------------------------------------------------------------------*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class NARCArchive {

    private List<String> filenames = new ArrayList<>();
    public List<byte[]> files = new ArrayList<>();

    private boolean hasFilenames = false;

    public NARCArchive() {
        // creates a new empty NARC with no filenames by default
    }

    public NARCArchive(byte[] data) throws IOException {
        Map<String, byte[]> frames = readNitroFrames(data);
        if (!frames.containsKey("FATB") || !frames.containsKey("FNTB") || !frames.containsKey("FIMG")) {
            throw new IOException("Not a valid narc file");
        }

        // File contents
        byte[] fatbframe = frames.get("FATB");
        byte[] fimgframe = frames.get("FIMG");
        int fileCount = readLong(fatbframe, 0);
        for (int i = 0; i < fileCount; i++) {
            int startOffset = readLong(fatbframe, 4 + i * 8);
            int endOffset = readLong(fatbframe, 8 + i * 8);
            int length = (endOffset - startOffset);
            byte[] thisFile = new byte[length];
            System.arraycopy(fimgframe, startOffset, thisFile, 0, length);
            files.add(thisFile);
        }

        // Filenames?
        byte[] fntbframe = frames.get("FNTB");
        int unk1 = readLong(fntbframe, 0);
        if (unk1 == 8) {
            // Filenames exist
            hasFilenames = true;
            int offset = 8;
            for (int i = 0; i < fileCount; i++) {
                int fnLength = (fntbframe[offset] & 0xFF);
                offset++;
                byte[] filenameBA = new byte[fnLength];
                System.arraycopy(fntbframe, offset, filenameBA, 0, fnLength);
                String filename = new String(filenameBA, "US-ASCII");
                filenames.add(filename);
            }
        } else {
            hasFilenames = false;
            for (int i = 0; i < fileCount; i++) {
                filenames.add(null);
            }
        }
    }

    public byte[] getBytes() throws IOException {
        // Get bytes required for FIMG frame
        int bytesRequired = 0;
        for (byte[] file : files) {
            bytesRequired += Math.ceil(file.length / 4.0) * 4;
        }
        // FIMG frame & FATB frame build

        // 4 for numentries, 8*size for entries, 8 for nitro header
        byte[] fatbFrame = new byte[4 + files.size() * 8 + 8];
        // bytesRequired + 8 for nitro header
        byte[] fimgFrame = new byte[bytesRequired + 8];

        // Nitro headers
        fatbFrame[0] = 'B';
        fatbFrame[1] = 'T';
        fatbFrame[2] = 'A';
        fatbFrame[3] = 'F';
        writeLong(fatbFrame, 4, fatbFrame.length);

        fimgFrame[0] = 'G';
        fimgFrame[1] = 'M';
        fimgFrame[2] = 'I';
        fimgFrame[3] = 'F';
        writeLong(fimgFrame, 4, fimgFrame.length);
        int offset = 0;

        writeLong(fatbFrame, 8, files.size());
        for (int i = 0; i < files.size(); i++) {
            byte[] file = files.get(i);
            int bytesRequiredForFile = (int) (Math.ceil(file.length / 4.0) * 4);
            System.arraycopy(file, 0, fimgFrame, offset + 8, file.length);
            for (int filler = file.length; filler < bytesRequiredForFile; filler++) {
                fimgFrame[offset + 8 + filler] = (byte) 0xFF;
            }
            writeLong(fatbFrame, 12 + i * 8, offset);
            writeLong(fatbFrame, 16 + i * 8, offset + file.length);
            offset += bytesRequiredForFile;
        }

        // FNTB Frame
        int bytesForFNTBFrame = 16;
        if (hasFilenames) {
            for (String filename : filenames) {
                bytesForFNTBFrame += filename.getBytes("US-ASCII").length + 1;
            }
        }
        byte[] fntbFrame = new byte[bytesForFNTBFrame];

        fntbFrame[0] = 'B';
        fntbFrame[1] = 'T';
        fntbFrame[2] = 'N';
        fntbFrame[3] = 'F';
        writeLong(fntbFrame, 4, fntbFrame.length);

        if (hasFilenames) {
            writeLong(fntbFrame, 8, 8);
            writeLong(fntbFrame, 12, 0x10000);
            int fntbOffset = 16;
            for (String filename : filenames) {
                byte[] fntbfilename = filename.getBytes("US-ASCII");
                fntbFrame[fntbOffset] = (byte) fntbfilename.length;
                System.arraycopy(fntbfilename, 0, fntbFrame, fntbOffset + 1, fntbfilename.length);
                fntbOffset += 1 + fntbfilename.length;
            }
        } else {
            writeLong(fntbFrame, 8, 4);
            writeLong(fntbFrame, 12, 0x10000);
        }

        // Now for the actual Nitro file
        int nitrolength = 16 + fatbFrame.length + fntbFrame.length + fimgFrame.length;
        byte[] nitroFile = new byte[nitrolength];
        nitroFile[0] = 'N';
        nitroFile[1] = 'A';
        nitroFile[2] = 'R';
        nitroFile[3] = 'C';
        writeWord(nitroFile, 4, 0xFFFE);
        writeWord(nitroFile, 6, 0x0100);
        writeLong(nitroFile, 8, nitrolength);
        writeWord(nitroFile, 12, 0x10);
        writeWord(nitroFile, 14, 3);
        System.arraycopy(fatbFrame, 0, nitroFile, 16, fatbFrame.length);
        System.arraycopy(fntbFrame, 0, nitroFile, 16 + fatbFrame.length, fntbFrame.length);
        System.arraycopy(fimgFrame, 0, nitroFile, 16 + fatbFrame.length + fntbFrame.length, fimgFrame.length);

        return nitroFile;
    }

    private Map<String, byte[]> readNitroFrames(byte[] data) throws IOException {

        // Read the number of frames
        int frameCount = readWord(data, 0x0E);

        // each frame
        int offset = 0x10;
        Map<String, byte[]> frames = new TreeMap<>();
        for (int i = 0; i < frameCount; i++) {
            byte[] magic = new byte[] { data[offset + 3], data[offset + 2], data[offset + 1], data[offset] };
            String magicS = new String(magic, "US-ASCII");

            int frame_size = readLong(data, offset + 4);
            // Patch for BB/VW and other DS hacks which don't update
            // the size of their expanded NARCs correctly
            if (i == frameCount - 1 && offset + frame_size < data.length) {
                frame_size = data.length - offset;
            }
            byte[] frame = new byte[frame_size - 8];
            System.arraycopy(data, offset + 8, frame, 0, frame_size - 8);
            frames.put(magicS, frame);
            offset += frame_size;
        }
        return frames;
    }

    private int readWord(byte[] data, int offset) {
        return (data[offset] & 0xFF) | ((data[offset + 1] & 0xFF) << 8);
    }

    private int readLong(byte[] data, int offset) {
        return (data[offset] & 0xFF) | ((data[offset + 1] & 0xFF) << 8) | ((data[offset + 2] & 0xFF) << 16)
                | ((data[offset + 3] & 0xFF) << 24);
    }

    private void writeWord(byte[] data, int offset, int value) {
        data[offset] = (byte) (value & 0xFF);
        data[offset + 1] = (byte) ((value >> 8) & 0xFF);
    }

    private void writeLong(byte[] data, int offset, int value) {
        data[offset] = (byte) (value & 0xFF);
        data[offset + 1] = (byte) ((value >> 8) & 0xFF);
        data[offset + 2] = (byte) ((value >> 16) & 0xFF);
        data[offset + 3] = (byte) ((value >> 24) & 0xFF);
    }

}