BrawlCrate v0.41
Wii File Editor
Loading...
Searching...
No Matches
Static Public Member Functions | List of all members
BrawlLib.Wii.MSBinDecoder Class Reference

Static Public Member Functions

static int EncodeString (string s, byte *dPtr)
 
static int GetStringSize (string s)
 

Member Function Documentation

◆ EncodeString()

static int BrawlLib.Wii.MSBinDecoder.EncodeString ( string  s,
byte *  dPtr 
)
inlinestatic
90 {
91 if (s.Length == 0)
92 {
93 return 0;
94 }
95
96 int bLen;
97 char c;
98
99 int colorIndex, sizeIndex;
100 byte* buffer = stackalloc byte[1024];
101 byte* first = dPtr;
102
103 byte[] utf8;
104 fixed (char* p = s)
105 {
106 utf8 = new byte[Encoding.UTF8.GetEncoder().GetByteCount(p, s.Length, true)];
107 bool completed;
108 fixed (byte* b = utf8)
109 {
110 Encoding.UTF8.GetEncoder().Convert(p, s.Length, b, utf8.Length, true,
111 out int charsUsed, out int bytesUsed, out completed);
112 }
113
114 if (!completed)
115 {
116 throw new Exception("Could not encode the MSBin file.");
117 }
118 }
119
120 // The loop below is ignorant of UTF8, but it looks for certain ASCII characters and writes each character as 1 byte, so hopefully this kludge will make it work
121 char[] utf8_to_char_array = new char[utf8.Length];
122 for (int i = 0; i < utf8.Length; i++)
123 {
124 utf8_to_char_array[i] = (char) utf8[i];
125 }
126
127 fixed (char* p = utf8_to_char_array)
128 {
129 char* sPtr = p, ceil = sPtr + utf8_to_char_array.Length;
130
131 while (sPtr < ceil)
132 {
133 c = *sPtr++;
134 if (c == '<')
135 {
136 char* last = sPtr;
137 byte* tPtr = buffer;
138 while (last < ceil && (c = *last++) != '>')
139 {
140 if (c != ' ')
141 {
142 *tPtr++ = (byte) c;
143 }
144 }
145
146 if (c != '>')
147 {
148 *dPtr++ = (byte) '<';
149 }
150 else
151 {
152 sPtr = last;
153 bLen = (int) tPtr - (int) buffer;
154 ToLower(buffer, bLen);
155
156 byte* control = dPtr;
157 byte* flag = dPtr + 1;
158 byte* data = dPtr + 2;
159
160 if ((sizeIndex = IndexOf(buffer, bLen, "size=")) >= 0)
161 {
162 *control = 0x12;
163 *flag |= 1;
164 *data++ = 0x0D;
165
166 tPtr = buffer + sizeIndex + 5;
167 ReadHex(ref data, ref tPtr, 2);
168
169 dPtr = data;
170 }
171
172 if ((colorIndex = IndexOf(buffer, bLen, "color=")) >= 0)
173 {
174 *control = 0x12;
175 *flag |= 2;
176 *data++ = 0x0C;
177
178 tPtr = buffer + colorIndex + 6;
179 ReadHex(ref data, ref tPtr, 4);
180
181 dPtr = data;
182 }
183
184 if (StrContains(buffer, bLen, "end"))
185 {
186 *dPtr++ = 0x13;
187 }
188 }
189 }
190 else if (c == '[')
191 {
192 char* last = sPtr;
193 byte* tPtr = buffer;
194 while (last < ceil && (c = *last++) != ']')
195 {
196 if (c != ' ')
197 {
198 *tPtr++ = (byte) c;
199 }
200 }
201
202 if (c != ']')
203 {
204 *dPtr++ = (byte) ']';
205 }
206 else
207 {
208 sPtr = last;
209 bLen = (int) tPtr - (int) buffer;
210 ToLower(buffer, bLen);
211
212 if ((colorIndex = IndexOf(buffer, bLen, "border=")) >= 0)
213 {
214 tPtr = buffer + colorIndex + 7;
215
216 *dPtr++ = 0x11;
217 ReadHex(ref dPtr, ref tPtr, 4);
218 }
219 }
220 }
221 else if (c == '\\')
222 {
223 if (sPtr < ceil)
224 {
225 *dPtr++ = (byte) *sPtr++;
226 }
227 }
228 else if (c == '\r')
229 {
230 } //do nothing
231 else
232 {
233 *dPtr++ = (byte) c;
234 }
235 }
236 }
237
238 return (int) dPtr - (int) first;
239 }

◆ GetStringSize()

static int BrawlLib.Wii.MSBinDecoder.GetStringSize ( string  s)
inlinestatic
242 {
243 if (s.Length == 0)
244 {
245 return 0;
246 }
247
248 int len = 0, bLen;
249 int strlen = s.Length;
250 char c;
251
252 bool hasColor, hasSize;
253 byte* buffer = stackalloc byte[1024];
254
255 byte[] utf8;
256 fixed (char* p = s)
257 {
258 utf8 = new byte[Encoding.UTF8.GetEncoder().GetByteCount(p, s.Length, true)];
259 bool completed;
260 fixed (byte* b = utf8)
261 {
262 Encoding.UTF8.GetEncoder().Convert(p, s.Length, b, utf8.Length, true,
263 out int charsUsed, out int bytesUsed, out completed);
264 }
265
266 if (!completed)
267 {
268 throw new Exception("Could not encode the MSBin file.");
269 }
270 }
271
272 // The loop below is ignorant of UTF8, but it looks for certain ASCII characters and writes each character as 1 byte, so hopefully this kludge will make it work
273 char[] utf8_to_char_array = new char[utf8.Length];
274 for (int i = 0; i < utf8.Length; i++)
275 {
276 utf8_to_char_array[i] = (char) utf8[i];
277 }
278
279 fixed (char* p = utf8_to_char_array)
280 {
281 char* sPtr = p, ceil = sPtr + utf8_to_char_array.Length;
282
283 while (sPtr < ceil)
284 {
285 c = *sPtr++;
286 if (c == '<')
287 {
288 char* last = sPtr;
289 byte* tPtr = buffer;
290 while (last < ceil && (c = *last++) != '>')
291 {
292 if (c != ' ')
293 {
294 *tPtr++ = (byte) c;
295 }
296 }
297
298 if (c != '>')
299 {
300 len++;
301 }
302 else
303 {
304 sPtr = last;
305 bLen = (int) tPtr - (int) buffer;
306 ToLower(buffer, bLen);
307
308 if (hasColor = StrContains(buffer, bLen, "color="))
309 {
310 len += 5;
311 }
312
313 if (hasSize = StrContains(buffer, bLen, "size="))
314 {
315 len += 3;
316 }
317
318 if (hasSize || hasColor)
319 {
320 len += 2;
321 }
322
323 if (StrContains(buffer, bLen, "end"))
324 {
325 len++;
326 }
327 }
328 }
329 else if (c == '[')
330 {
331 char* last = sPtr;
332 byte* tPtr = buffer;
333 while (last < ceil && (c = *last++) != ']')
334 {
335 if (c != ' ')
336 {
337 *tPtr++ = (byte) c;
338 }
339 }
340
341 if (c != ']')
342 {
343 len++;
344 }
345 else
346 {
347 sPtr = last;
348 bLen = (int) tPtr - (int) buffer;
349 ToLower(buffer, bLen);
350
351 if (StrContains(buffer, bLen, "border="))
352 {
353 len += 5;
354 }
355 }
356 }
357 else if (c == '\\')
358 {
359 if (sPtr < ceil)
360 {
361 sPtr++;
362 len++;
363 }
364 }
365 else if (c == '\r')
366 {
367 } //do nothing
368 else
369 {
370 len++;
371 }
372 }
373 }
374
375 return len;
376 }

The documentation for this class was generated from the following file: