-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacnl_convert_csharp_pseudo.cs
203 lines (183 loc) · 7.12 KB
/
acnl_convert_csharp_pseudo.cs
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
public class ACNLConvertExample
{
public byte[] ConvertACPattern()
{
byte[] ACNHpattern = File.ReadAllBytes(ACNH_Pattern_Example.nhd); // Read your pattern
byte[] ACNLpattern = null;
int pixelDataLength = 0;
switch(ACNHpattern.Length) // Check Pattern Type
{
case 0x2A8:
ACNLpattern = new byte[0x26C]; // Create new bytes for ACNL Normal
pixelDataLength = 512;
break;
case 0x8A8:
ACNLpattern = new byte[0x870]; // Create new bytes for ACNL Pro
pixelDataLength = 2048;
break;
default:
// Malformed file
return;
}
switch(ACNHpattern[0x77]) // Check Pattern Type
{
case 0x00:
ACNLpattern[0x69] = 0x09;
break;
case 0x12:
ACNLpattern[0x69] = 0x01;
break;
case 0x13:
ACNLpattern[0x69] = 0x00;
break;
case 0x14:
ACNLpattern[0x69] = 0x02;
break;
case 0x15:
ACNLpattern[0x69] = 0x04;
break;
case 0x16:
ACNLpattern[0x69] = 0x03;
break;
case 0x17:
ACNLpattern[0x69] = 0x05;
break;
case 0x18:
ACNLpattern[0x69] = 0x07;
break;
case 0x19:
ACNLpattern[0x69] = 0x06;
break;
case 0x1E:
ACNLpattern[0x69] = 0x08;
break;
default:
// Not a default interoperable type
return;
break;
}
Array.Copy(ACNHpattern, 0x10, ACNLpattern, 0x00, 40); // Pattern Name
Array.Copy(ACNHpattern, 0x58, ACNLpattern, 0x2C, 18); // Player Name (Trimmed)
Array.Copy(ACNHpattern, 0x3C, ACNLpattern, 0x42, 18); // Island Name (Trimmed)
Array.Copy(ACNHpattern, 0x54, ACNLpattern, 0x2A, 2); // Player ID Hash (or reset to 0xFF, 0xFF)
Array.Copy(ACNHpattern, 0x38, ACNLpattern, 0x40, 2); // Town ID Hash
byte[] ACNHpalette = new byte[45]; // Create bytes to store ACNH palette
Array.Copy(ACNHpattern, 0x78, ACNHpalette, 0, 45); // Store them
byte[] ACNLpalette = new byte[15]; // Create bytes to store ACNL palette
for (int i = 0; i < ACNLpalette.Length; i++)
{
ACNLpalette[i] = GetNearestColor(ACNHpalette[i*3], ACNHpalette[i*3 + 1], ACNHpalette[i*3 + 2]);
}
Array.Copy(ACNLpalette, 0x00, ACNLpattern, 0x58, 15); // Store them
Array.Copy(ACNHpattern, 0xA5, ACNLpattern, 0x6C, pixelDataLength); // Pixel Data
if(ACNLpattern.Length == 0x870)
ACNLpattern = DataSortACNL(ACNLpattern);
return ACNLpattern;
}
public byte GetNearestColor(byte R, byte G, byte B)
{
var convVal = -1;
var convIndex = -1;
for (int i = 0; i < 256; i++) // Run against the color index
{
if (ACNLColors[i] != Color.Empty) // Check we don't pick one of the unused colors
{
// Get differences in color value
var diff = Math.Abs(ACNLColors[i].R - R) + Math.Abs(ACNLColors[i].G - G) + Math.Abs(ACNLColors[i].B - B);
if (convVal == -1 || convVal > diff) // If close enough, pick it
{
convVal = diff;
convIndex = i;
}
}
}
return (byte)convIndex;
}
public Color[] ACNLColors = new Color[256]
{
Color.FromArgb ( 0xFF, 0xEE, 0xFF ), // 0x00, refer color table in info above
// ...and on and on...
Color.Empty, // 0xFF, refer color table in info above
};
public static byte[] DataSortACNL(byte[] data)
{
var dataSorted = new byte[0x870];
Array.Copy(data, 0x00, dataSorted, 0x00, 0x870);
Dictionary<(int, int), int> OffsetsProTexChunk = new Dictionary<(int, int), int>()
{
// Front Chunk
{ ( 0x000, 0x200), 0x200 },
// Back Chunk
{ ( 0x200, 0x400), 0x000 },
// Front Bottom Chunk
{ ( 0x600, 0x700), 0x600 },
// Back Bottom Chunk
{ ( 0x700, 0x800), 0x400 },
// Left Sleeve Chunk
{ ( 0x400, 0x500), 0x500 },
// Right Sleeve Chunk
{ ( 0x500, 0x600), 0x700 },
};
Dictionary<(int, int), int> OffsetsProStandeeTexChunk = new Dictionary<(int, int), int>()
{
// Top Left Chunk
{ ( 0x000, 0x200), 0x000 },
// Bottom Left Chunk
{ ( 0x200, 0x400), 0x400 },
// Top Right Chunk
{ ( 0x400, 0x600), 0x200 },
// Bottom Right Chunk
{ ( 0x600, 0x800), 0x600 }
};
// Check data type
if (data[0x69] != 0x06 && data[0x69] != 0x07)
{
// Iterate pixels
for (var y = 0; y < 64; y++)
{
for (var x = 0; x < 64 / 2; x++)
{
// Ain't a hat? Off we hack!
if (data[0x69] != 0x06 && data[0x69] != 0x07)
{
// Grab offsets for chunks
var offset = (x >= 64 / 4 ? 0x200 : 0x0) + (y >= 64 / 2 ? 0x400 : 0x0);
int index = offset + x % (64 / 4) + (y % (64 / 2)) * (64 / 4);
if (data[0x69] == 0x08) // If it's a standee
{
var newIndex = index;
foreach (var kvp in OffsetsProStandeeTexChunk)
{
// Match up chunks
if (index >= kvp.Value && index < kvp.Value + (kvp.Key.Item2 - kvp.Key.Item1))
{
newIndex = (index - kvp.Value) + kvp.Key.Item1;
}
}
dataSorted[0x6C + newIndex] = data[0x6C + index]; // Place chunks
}
else
{
var newIndex = index;
foreach (var kvp in OffsetsProTexChunk)
{
// Match up chunks
if (index >= kvp.Value && index < kvp.Value + (kvp.Key.Item2 - kvp.Key.Item1))
{
newIndex = (index - kvp.Value) + kvp.Key.Item1;
}
}
dataSorted[0x6C + newIndex] = data[0x6C + index]; // Place chunks
}
}
}
}
}
else
{
// It's a hat? Cut it back!
Array.Copy(data, 0x6C, dataSorted, 0x6C, 0x200);
}
return dataSorted;
}
}