Microsoft ouvre certaines parties du code source de Minecraft édition Java :
Deux bibliothèques disponibles sous licence MIT

Le studio de développement Mojang (propriété de Microsoft) ouvre la cuisine interne de Minecraft aux programmeurs. L’équipe a passé l’annonce via son compte Twitter.

Nom : Minecraft open  source.png
Affichages : 4674
Taille : 271,9 Ko

Depuis peu, certaines parties du moteur du jeu vidéo sont disponibles sous licence MIT. Ce choix du studio de développement permet à chaque développeur de s’approprier le code, le modifier et le distribuer, ce, à la seule condition de publier les nouveaux contenus avec la note de copyright initiale. L’équipe Mojang devrait ainsi engranger des contributions de nature à améliorer le moteur de jeu.

Le studio de développement Mojang ouvre Brigadier et DataFixerUpper. Sous Minecraft, le gamer dispose d’une ligne de commandes – des instructions sous forme de texte qui débutent avec le caractère /. Brigadier est la bibliothèque qui assure la conversion de ce texte en une fonction que le jeu va exécuter. « Beaucoup de personnes pensent qu’il s’agit d’une fonctionnalité aisée à implémenter, mais il n’en est rien », expliquent les développeurs. Le code de la portion chargée de lire la chaîne de caractère illustre l’ardeur de la tâche.

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
 
package com.mojang.brigadier;
 
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import org.junit.Test;
 
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
 
public class StringReaderTest {
    @Test
    public void canRead() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.canRead(), is(true));
        reader.skip(); // 'a'
        assertThat(reader.canRead(), is(true));
        reader.skip(); // 'b'
        assertThat(reader.canRead(), is(true));
        reader.skip(); // 'c'
        assertThat(reader.canRead(), is(false));
    }
 
    @Test
    public void getRemainingLength() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.getRemainingLength(), is(3));
        reader.setCursor(1);
        assertThat(reader.getRemainingLength(), is(2));
        reader.setCursor(2);
        assertThat(reader.getRemainingLength(), is(1));
        reader.setCursor(3);
        assertThat(reader.getRemainingLength(), is(0));
    }
 
    @Test
    public void canRead_length() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.canRead(1), is(true));
        assertThat(reader.canRead(2), is(true));
        assertThat(reader.canRead(3), is(true));
        assertThat(reader.canRead(4), is(false));
        assertThat(reader.canRead(5), is(false));
    }
 
    @Test
    public void peek() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.peek(), is('a'));
        assertThat(reader.getCursor(), is(0));
        reader.setCursor(2);
        assertThat(reader.peek(), is('c'));
        assertThat(reader.getCursor(), is(2));
    }
 
    @Test
    public void peek_length() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.peek(0), is('a'));
        assertThat(reader.peek(2), is('c'));
        assertThat(reader.getCursor(), is(0));
        reader.setCursor(1);
        assertThat(reader.peek(1), is('c'));
        assertThat(reader.getCursor(), is(1));
    }
 
    @Test
    public void read() throws Exception {
        final StringReader reader = new StringReader("abc");
        assertThat(reader.read(), is('a'));
        assertThat(reader.read(), is('b'));
        assertThat(reader.read(), is('c'));
        assertThat(reader.getCursor(), is(3));
    }
 
    @Test
    public void skip() throws Exception {
        final StringReader reader = new StringReader("abc");
        reader.skip();
        assertThat(reader.getCursor(), is(1));
    }
 
    @Test
    public void getRemaining() throws Exception {
        final StringReader reader = new StringReader("Hello!");
        assertThat(reader.getRemaining(), equalTo("Hello!"));
        reader.setCursor(3);
        assertThat(reader.getRemaining(), equalTo("lo!"));
        reader.setCursor(6);
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void getRead() throws Exception {
        final StringReader reader = new StringReader("Hello!");
        assertThat(reader.getRead(), equalTo(""));
        reader.setCursor(3);
        assertThat(reader.getRead(), equalTo("Hel"));
        reader.setCursor(6);
        assertThat(reader.getRead(), equalTo("Hello!"));
    }
 
    @Test
    public void skipWhitespace_none() throws Exception {
        final StringReader reader = new StringReader("Hello!");
        reader.skipWhitespace();
        assertThat(reader.getCursor(), is(0));
    }
 
    @Test
    public void skipWhitespace_mixed() throws Exception {
        final StringReader reader = new StringReader(" \t \t\nHello!");
        reader.skipWhitespace();
        assertThat(reader.getCursor(), is(5));
    }
 
    @Test
    public void skipWhitespace_empty() throws Exception {
        final StringReader reader = new StringReader("");
        reader.skipWhitespace();
        assertThat(reader.getCursor(), is(0));
    }
 
    @Test
    public void readUnquotedString() throws Exception {
        final StringReader reader = new StringReader("hello world");
        assertThat(reader.readUnquotedString(), equalTo("hello"));
        assertThat(reader.getRead(), equalTo("hello"));
        assertThat(reader.getRemaining(), equalTo(" world"));
    }
 
    @Test
    public void readUnquotedString_empty() throws Exception {
        final StringReader reader = new StringReader("");
        assertThat(reader.readUnquotedString(), equalTo(""));
        assertThat(reader.getRead(), equalTo(""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readUnquotedString_empty_withRemaining() throws Exception {
        final StringReader reader = new StringReader(" hello world");
        assertThat(reader.readUnquotedString(), equalTo(""));
        assertThat(reader.getRead(), equalTo(""));
        assertThat(reader.getRemaining(), equalTo(" hello world"));
    }
 
    @Test
    public void readQuotedString() throws Exception {
        final StringReader reader = new StringReader("\"hello world\"");
        assertThat(reader.readQuotedString(), equalTo("hello world"));
        assertThat(reader.getRead(), equalTo("\"hello world\""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readQuotedString_empty() throws Exception {
        final StringReader reader = new StringReader("");
        assertThat(reader.readQuotedString(), equalTo(""));
        assertThat(reader.getRead(), equalTo(""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readQuotedString_emptyQuoted() throws Exception {
        final StringReader reader = new StringReader("\"\"");
        assertThat(reader.readQuotedString(), equalTo(""));
        assertThat(reader.getRead(), equalTo("\"\""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readQuotedString_emptyQuoted_withRemaining() throws Exception {
        final StringReader reader = new StringReader("\"\" hello world");
        assertThat(reader.readQuotedString(), equalTo(""));
        assertThat(reader.getRead(), equalTo("\"\""));
        assertThat(reader.getRemaining(), equalTo(" hello world"));
    }
 
    @Test
    public void readQuotedString_withEscapedQuote() throws Exception {
        final StringReader reader = new StringReader("\"hello \\\"world\\\"\"");
        assertThat(reader.readQuotedString(), equalTo("hello \"world\""));
        assertThat(reader.getRead(), equalTo("\"hello \\\"world\\\"\""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readQuotedString_withEscapedEscapes() throws Exception {
        final StringReader reader = new StringReader("\"\\\\o/\"");
        assertThat(reader.readQuotedString(), equalTo("\\o/"));
        assertThat(reader.getRead(), equalTo("\"\\\\o/\""));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readQuotedString_withRemaining() throws Exception {
        final StringReader reader = new StringReader("\"hello world\" foo bar");
        assertThat(reader.readQuotedString(), equalTo("hello world"));
        assertThat(reader.getRead(), equalTo("\"hello world\""));
        assertThat(reader.getRemaining(), equalTo(" foo bar"));
    }
 
    @Test
    public void readQuotedString_withImmediateRemaining() throws Exception {
        final StringReader reader = new StringReader("\"hello world\"foo bar");
        assertThat(reader.readQuotedString(), equalTo("hello world"));
        assertThat(reader.getRead(), equalTo("\"hello world\""));
        assertThat(reader.getRemaining(), equalTo("foo bar"));
    }
 
    @Test
    public void readQuotedString_noOpen() throws Exception {
        try {
            new StringReader("hello world\"").readQuotedString();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedStartOfQuote()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readQuotedString_noClose() throws Exception {
        try {
            new StringReader("\"hello world").readQuotedString();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedEndOfQuote()));
            assertThat(ex.getCursor(), is(12));
        }
    }
 
    @Test
    public void readQuotedString_invalidEscape() throws Exception {
        try {
            new StringReader("\"hello\\nworld\"").readQuotedString();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidEscape()));
            assertThat(ex.getCursor(), is(7));
        }
    }
 
    @Test
    public void readInt() throws Exception {
        final StringReader reader = new StringReader("1234567890");
        assertThat(reader.readInt(), is(1234567890));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readInt_negative() throws Exception {
        final StringReader reader = new StringReader("-1234567890");
        assertThat(reader.readInt(), is(-1234567890));
        assertThat(reader.getRead(), equalTo("-1234567890"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readInt_invalid() throws Exception {
        try {
            new StringReader("12.34").readInt();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidInt()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readInt_none() throws Exception {
        try {
            new StringReader("").readInt();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedInt()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readInt_withRemaining() throws Exception {
        final StringReader reader = new StringReader("1234567890 foo bar");
        assertThat(reader.readInt(), is(1234567890));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo(" foo bar"));
    }
 
    @Test
    public void readInt_withRemainingImmediate() throws Exception {
        final StringReader reader = new StringReader("1234567890foo bar");
        assertThat(reader.readInt(), is(1234567890));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo("foo bar"));
    }
 
    @Test
    public void readLong() throws Exception {
        final StringReader reader = new StringReader("1234567890");
        assertThat(reader.readLong(), is(1234567890L));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readLong_negative() throws Exception {
        final StringReader reader = new StringReader("-1234567890");
        assertThat(reader.readLong(), is(-1234567890L));
        assertThat(reader.getRead(), equalTo("-1234567890"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readLong_invalid() throws Exception {
        try {
            new StringReader("12.34").readLong();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidLong()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readLong_none() throws Exception {
        try {
            new StringReader("").readLong();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedLong()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readLong_withRemaining() throws Exception {
        final StringReader reader = new StringReader("1234567890 foo bar");
        assertThat(reader.readLong(), is(1234567890L));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo(" foo bar"));
    }
 
    @Test
    public void readLong_withRemainingImmediate() throws Exception {
        final StringReader reader = new StringReader("1234567890foo bar");
        assertThat(reader.readLong(), is(1234567890L));
        assertThat(reader.getRead(), equalTo("1234567890"));
        assertThat(reader.getRemaining(), equalTo("foo bar"));
    }
 
    @Test
    public void readDouble() throws Exception {
        final StringReader reader = new StringReader("123");
        assertThat(reader.readDouble(), is(123.0));
        assertThat(reader.getRead(), equalTo("123"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readDouble_withDecimal() throws Exception {
        final StringReader reader = new StringReader("12.34");
        assertThat(reader.readDouble(), is(12.34));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readDouble_negative() throws Exception {
        final StringReader reader = new StringReader("-123");
        assertThat(reader.readDouble(), is(-123.0));
        assertThat(reader.getRead(), equalTo("-123"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readDouble_invalid() throws Exception {
        try {
            new StringReader("12.34.56").readDouble();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidDouble()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readDouble_none() throws Exception {
        try {
            new StringReader("").readDouble();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedDouble()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readDouble_withRemaining() throws Exception {
        final StringReader reader = new StringReader("12.34 foo bar");
        assertThat(reader.readDouble(), is(12.34));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo(" foo bar"));
    }
 
    @Test
    public void readDouble_withRemainingImmediate() throws Exception {
        final StringReader reader = new StringReader("12.34foo bar");
        assertThat(reader.readDouble(), is(12.34));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo("foo bar"));
    }
 
    @Test
    public void readFloat() throws Exception {
        final StringReader reader = new StringReader("123");
        assertThat(reader.readFloat(), is(123.0f));
        assertThat(reader.getRead(), equalTo("123"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readFloat_withDecimal() throws Exception {
        final StringReader reader = new StringReader("12.34");
        assertThat(reader.readFloat(), is(12.34f));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readFloat_negative() throws Exception {
        final StringReader reader = new StringReader("-123");
        assertThat(reader.readFloat(), is(-123.0f));
        assertThat(reader.getRead(), equalTo("-123"));
        assertThat(reader.getRemaining(), equalTo(""));
    }
 
    @Test
    public void readFloat_invalid() throws Exception {
        try {
            new StringReader("12.34.56").readFloat();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidFloat()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readFloat_none() throws Exception {
        try {
            new StringReader("").readFloat();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedFloat()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readFloat_withRemaining() throws Exception {
        final StringReader reader = new StringReader("12.34 foo bar");
        assertThat(reader.readFloat(), is(12.34f));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo(" foo bar"));
    }
 
    @Test
    public void readFloat_withRemainingImmediate() throws Exception {
        final StringReader reader = new StringReader("12.34foo bar");
        assertThat(reader.readFloat(), is(12.34f));
        assertThat(reader.getRead(), equalTo("12.34"));
        assertThat(reader.getRemaining(), equalTo("foo bar"));
    }
 
    @Test
    public void expect_correct() throws Exception {
        final StringReader reader = new StringReader("abc");
        reader.expect('a');
        assertThat(reader.getCursor(), is(1));
    }
 
    @Test
    public void expect_incorrect() throws Exception {
        final StringReader reader = new StringReader("bcd");
        try {
            reader.expect('a');
            fail();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void expect_none() throws Exception {
        final StringReader reader = new StringReader("");
        try {
            reader.expect('a');
            fail();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedSymbol()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readBoolean_correct() throws Exception {
        final StringReader reader = new StringReader("true");
        assertThat(reader.readBoolean(), is(true));
        assertThat(reader.getRead(), equalTo("true"));
    }
 
    @Test
    public void readBoolean_incorrect() throws Exception {
        final StringReader reader = new StringReader("tuesday");
        try {
            reader.readBoolean();
            fail();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerInvalidBool()));
            assertThat(ex.getCursor(), is(0));
        }
    }
 
    @Test
    public void readBoolean_none() throws Exception {
        final StringReader reader = new StringReader("");
        try {
            reader.readBoolean();
            fail();
        } catch (final CommandSyntaxException ex) {
            assertThat(ex.getType(), is(CommandSyntaxException.BUILT_IN_EXCEPTIONS.readerExpectedBool()));
            assertThat(ex.getCursor(), is(0));
        }
    }
}


Nom : engine command.png
Affichages : 3028
Taille : 460,0 Ko
Le jeu génère une liste de suggestions pour chaque commande (ci-dessus, un exemple avec /give)



DataFixerUpper est la deuxième bibliothèque que Mojang a décidé d’ouvrir et l’une des parties les plus importantes du moteur du jeu. De façon brossée, il s’agit d’une bibliothèque de conversion des données (dont le jeu fait usage) au format requis pour que l’apparence des différents niveaux reste en phase avec les modifications introduites par l’équipe de développement. « Pour faire simple, avant que Minecraft ne charge les différents blocs, il fait appel à DataFixerUpper qui assure la mise à jour visuelle », explique l’équipe de développement.

Code java : Sélectionner tout - Visualiser dans une fenêtre à part
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
package com.mojang.datafixers;
 
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import it.unimi.dsi.fastutil.ints.Int2ObjectSortedMap;
import it.unimi.dsi.fastutil.ints.IntSortedSet;
import it.unimi.dsi.fastutil.longs.Long2ObjectMap;
import it.unimi.dsi.fastutil.longs.Long2ObjectOpenHashMap;
import com.mojang.datafixers.functions.PointFreeRule;
import com.mojang.datafixers.schemas.Schema;
import com.mojang.datafixers.types.Type;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import java.util.List;
import java.util.Optional;
 
/*
 * Optimizing functions
 *   Cunha, A., & Pinto, J. S. (2005). Point-free program transformation
 *   Lämmel, R., Visser, E., & Visser, J. (2002). The essence of strategic programming
 *
 * How to handle recursive types
 *   Cunha, A., & Pacheco, H. (2011). Algebraic specialization of generic functions for recursive types
 *   Yakushev, A. R., Holdermans, S., Löh, A., & Jeuring, J. (2009, August). Generic programming with fixed points for mutually recursive datatypes
 *   Magalhães, J. P., & Löh, A. (2012). A formal comparison of approaches to datatype-generic programming
 *
 * Optics
 *   Pickering, M., Gibbons, J., & Wu, N. (2017). Profunctor Optics: Modular Data Accessors
 *   Pacheco, H., & Cunha, A. (2010, June). Generic point-free lenses
 *
 * Tying it together
 *   Cunha, A., Oliveira, J. N., & Visser, J. (2006, August). Type-safe two-level data transformation
 *   Cunha, A., & Visser, J. (2011). Transformation of structure-shy programs with application to XPath queries and strategic functions
 *   Pacheco, H., & Cunha, A. (2011, January). Calculating with lenses: optimising bidirectional transformations
 */
public class DataFixerUpper implements DataFixer {
    public static boolean ERRORS_ARE_FATAL = false;
 
    private static final Logger LOGGER = LogManager.getLogger();
 
    protected static final PointFreeRule OPTIMIZATION_RULE = DataFixUtils.make(() -> {
        final PointFreeRule opSimple = PointFreeRule.orElse(
            PointFreeRule.orElse(
                PointFreeRule.CataFuseSame.INSTANCE,
                PointFreeRule.orElse(
                    PointFreeRule.CataFuseDifferent.INSTANCE,
                    PointFreeRule.LensAppId.INSTANCE
                )
            ),
            PointFreeRule.orElse(
                PointFreeRule.LensComp.INSTANCE,
                PointFreeRule.orElse(
                    PointFreeRule.AppNest.INSTANCE,
                    PointFreeRule.LensCompFunc.INSTANCE
                )
            )
        );
        final PointFreeRule opLeft = PointFreeRule.many(PointFreeRule.once(PointFreeRule.orElse(opSimple, PointFreeRule.CompAssocLeft.INSTANCE)));
        final PointFreeRule opComp = PointFreeRule.many(PointFreeRule.once(PointFreeRule.orElse(PointFreeRule.SortInj.INSTANCE, PointFreeRule.SortProj.INSTANCE)));
        final PointFreeRule opRight = PointFreeRule.many(PointFreeRule.once(PointFreeRule.orElse(opSimple, PointFreeRule.CompAssocRight.INSTANCE)));
        return PointFreeRule.seq(ImmutableList.of(() -> opLeft, () -> opComp, () -> opRight, () -> opLeft, () -> opRight));
    });
 
    private final Int2ObjectSortedMap<Schema> schemas;
    private final List<DataFix> globalList;
    private final IntSortedSet fixerVersions;
    private final Long2ObjectMap<TypeRewriteRule> rules = new Long2ObjectOpenHashMap<>();
 
    protected DataFixerUpper(final Int2ObjectSortedMap<Schema> schemas, final List<DataFix> globalList, final IntSortedSet fixerVersions) {
        this.schemas = schemas;
        this.globalList = globalList;
        this.fixerVersions = fixerVersions;
    }
 
    @Override
    public <T> Dynamic<T> update(final DSL.TypeReference type, final Dynamic<T> input, final int version, final int newVersion) {
        try {
            if (version < newVersion) {
                final Type<?> dataType = getType(type, version);
                final Optional<T> read = dataType.readAndWrite(input.getOps(), getType(type, newVersion), getRule(version, newVersion), OPTIMIZATION_RULE, input.getValue());
                if (!read.isPresent()) {
                    throw new IllegalStateException("Could not parse for fixing " + dataType);
                }
 
                return new Dynamic<>(input.getOps(), read.get());
            }
        } catch (final Throwable t) {
            LOGGER.error("Something went wrong upgrading!", t);
        }
        return input;
    }
 
    @Override
    public Schema getSchema(final int key) {
        return schemas.get(getLowestSchemaSameVersion(schemas, key));
    }
 
    protected Type<?> getType(final DSL.TypeReference type, final int version) {
        return getSchema(DataFixUtils.makeKey(version)).getType(type);
    }
 
    protected static int getLowestSchemaSameVersion(final Int2ObjectSortedMap<Schema> schemas, final int versionKey) {
        if (versionKey < schemas.firstIntKey()) {
            // can't have a data type before anything else
            return schemas.firstIntKey();
        }
        return schemas.subMap(0, versionKey + 1).lastIntKey();
    }
 
    private int getLowestFixSameVersion(final int versionKey) {
        if (versionKey < fixerVersions.firstInt()) {
            // can have a version before everything else
            return fixerVersions.firstInt() - 1;
        }
        return fixerVersions.subSet(0, versionKey + 1).lastInt();
    }
 
    protected TypeRewriteRule getRule(final int version, final int dataVersion) {
        if (version >= dataVersion) {
            return TypeRewriteRule.nop();
        }
 
        final int expandedVersion = getLowestFixSameVersion(DataFixUtils.makeKey(version));
        final int expandedDataVersion = DataFixUtils.makeKey(dataVersion);
 
        final long key = (long) expandedVersion << 32 | expandedDataVersion;
        if (!rules.containsKey(key)) {
            final List<TypeRewriteRule> rules = Lists.newArrayList();
            for (final DataFix fix : globalList) {
                final int fixVersion = fix.getVersionKey();
                if (fixVersion > expandedVersion && fixVersion <= expandedDataVersion) {
                    final TypeRewriteRule fixRule = fix.getRule();
                    if (fixRule == TypeRewriteRule.nop()) {
                        continue;
                    }
                    rules.add(fixRule);
                }
            }
            this.rules.put(key, TypeRewriteRule.seq(rules));
        }
        return rules.get(key);
    }
 
    protected IntSortedSet fixerVersions() {
        return fixerVersions;
    }
}

L’intérêt pour un développeur tiers est de pouvoir s’inspirer d’un travail de ce type pour le réutiliser dans des projets qui n’ont rien à voir avec le gaming. C’est en cela que l’open source est magique. De façon officielle, le jeu était jusqu’ici distribué sous licence propriétaire. La donne a changé et ce n’est qu’un début puisque l’équipe Mojang promet d’ouvrir d’autres pans du code source.

Source : Minecraft.net

Et vous ?

Que pensez-vous de cette initiative du studio de développement Mojang ?

Qu’avez-vous déniché de bon dans les packages mis à votre disposition ? Comment comptez-vous le réutiliser ?

Voir aussi :

Microsoft confirme le rachat de Minecraft pour 2,5 milliards de dollars

Microsoft utilise le jeu Minecraft comme espace d'apprentissage pour son logiciel d'intelligence artificielle qui sera disponible cet été

Microsoft mise sur C++ plutôt que Java pour écrire la nouvelle version de Minecraft Education Edition, gage de plus de rapidité ?