summaryrefslogtreecommitdiff
path: root/src/com/rafa_99/pkrandom/ctr/SMDH.java
blob: 672be1c750256077e8c0bd8ae61b617822ca10da (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
package com.rafa_99.pkrandom.ctr;

/*----------------------------------------------------------------------------*/
/*--  NCCH.java - a base class for dealing with 3DS SMDH (icon.bin) files.  --*/
/*--                                                                        --*/
/*--  Part of "Universal Pokemon Randomizer ZX" by the UPR-ZX team          --*/
/*--  Pokemon and any associated names and the like are                     --*/
/*--  trademark and (C) Nintendo 1996-2020.                                 --*/
/*--                                                                        --*/
/*--  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 com.rafa_99.pkrandom.FileFunctions;

import java.nio.charset.StandardCharsets;

public class SMDH {

    private byte[] data;
    private String[] shortDescriptions = new String[12];
    private String[] longDescriptions = new String[12];
    private String[] publishers = new String[12];

    private static final int smdh_magic = 0x48444D53;
    private static final int length_of_title = 0x200;
    private static final int short_description_length = 0x80;
    private static final int long_description_length = 0x100;
    private static final int publisher_length = 0x80;

    public SMDH(byte[] smdhData) {
        data = smdhData;
        if (this.isValid()) {
            readDescriptionsAndPublishers();
        }
    }

    public byte[] getBytes() {
        return data;
    }

    public void setAllDescriptions(String newDescription) {
        byte[] newDescriptionBytes = newDescription.getBytes(StandardCharsets.UTF_16LE);
        if (newDescriptionBytes.length <= short_description_length) {
            for (int i = 0; i < 12; i++) {
                shortDescriptions[i] = newDescription;
                longDescriptions[i] = newDescription;
            }
            writeDescriptionsAndPublishers();
        }
    }

    public void setAllPublishers(String newPublisher) {
        byte[] newPublisherBytes = newPublisher.getBytes(StandardCharsets.UTF_16LE);
        if (newPublisherBytes.length <= publisher_length) {
            for (int i = 0; i < 12; i++) {
                publishers[i] = newPublisher;
            }
            writeDescriptionsAndPublishers();
        }
    }

    private boolean isValid() {
        int magic = FileFunctions.readFullInt(data, 0x0);
        return magic == smdh_magic;
    }

    private void readDescriptionsAndPublishers() {
        for (int i = 0; i < 12; i++) {
            int shortDescriptionOffset = 0x08 + (length_of_title * i);
            byte[] shortDescriptionBytes = new byte[short_description_length];
            System.arraycopy(data, shortDescriptionOffset, shortDescriptionBytes, 0, short_description_length);
            shortDescriptions[i] = new String(shortDescriptionBytes, StandardCharsets.UTF_16LE).trim();

            int longDescriptionOffset = 0x88 + (length_of_title * i);
            byte[] longDescriptionBytes = new byte[long_description_length];
            System.arraycopy(data, longDescriptionOffset, longDescriptionBytes, 0, long_description_length);
            longDescriptions[i] = new String(longDescriptionBytes, StandardCharsets.UTF_16LE).trim();

            int publisherOffset = 0x188 + (length_of_title * i);
            byte[] publisherBytes = new byte[publisher_length];
            System.arraycopy(data, publisherOffset, publisherBytes, 0, publisher_length);
            publishers[i] = new String(publisherBytes, StandardCharsets.UTF_16LE).trim();
        }
    }

    private void writeDescriptionsAndPublishers() {
        for (int i = 0; i < 12; i++) {
            byte[] emptyShortDescription = new byte[short_description_length];
            int shortDescriptionOffset = 0x08 + (length_of_title * i);
            byte[] shortDescriptionBytes = shortDescriptions[i].getBytes(StandardCharsets.UTF_16LE);
            System.arraycopy(emptyShortDescription, 0, data, shortDescriptionOffset, short_description_length);
            System.arraycopy(shortDescriptionBytes, 0, data, shortDescriptionOffset, shortDescriptionBytes.length);

            byte[] emptyLongDescription = new byte[long_description_length];
            int longDescriptionOffset = 0x88 + (length_of_title * i);
            byte[] longDescriptionBytes = longDescriptions[i].getBytes(StandardCharsets.UTF_16LE);
            System.arraycopy(emptyLongDescription, 0, data, longDescriptionOffset, long_description_length);
            System.arraycopy(longDescriptionBytes, 0, data, longDescriptionOffset, longDescriptionBytes.length);

            byte[] emptyPublisher = new byte[publisher_length];
            int publisherOffset = 0x188 + (length_of_title * i);
            byte[] publisherBytes = publishers[i].getBytes(StandardCharsets.UTF_16LE);
            System.arraycopy(emptyPublisher, 0, data, publisherOffset, publisher_length);
            System.arraycopy(publisherBytes, 0, data, publisherOffset, publisherBytes.length);
        }
    }
}