-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Container.kt
893 lines (804 loc) · 37.8 KB
/
Container.kt
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
package polyrhythmmania.container
import com.badlogic.gdx.files.FileHandle
import com.badlogic.gdx.graphics.PixmapIO
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.utils.Disposable
import com.eclipsesource.json.Json
import com.eclipsesource.json.JsonObject
import com.eclipsesource.json.JsonValue
import com.eclipsesource.json.WriterConfig
import net.beadsproject.beads.ugens.SamplePlayer
import net.lingala.zip4j.ZipFile
import paintbox.Paintbox
import paintbox.binding.FloatVar
import paintbox.binding.ReadOnlyFloatVar
import paintbox.binding.Var
import paintbox.util.Version
import paintbox.util.WindowSize
import paintbox.util.closeQuietly
import paintbox.util.gdxutils.disposeQuietly
import polyrhythmmania.PRMania
import polyrhythmmania.container.manifest.LibraryRelevantData
import polyrhythmmania.container.manifest.ResourceTag
import polyrhythmmania.container.manifest.SaveOptions
import polyrhythmmania.editor.Editor
import polyrhythmmania.editor.EditorSpecialFlags
import polyrhythmmania.editor.TrackID
import polyrhythmmania.editor.block.Block
import polyrhythmmania.editor.block.BlockEndState
import polyrhythmmania.editor.block.Instantiator
import polyrhythmmania.editor.block.Instantiators
import polyrhythmmania.engine.Engine
import polyrhythmmania.engine.input.ResultsText
import polyrhythmmania.engine.music.MusicVolume
import polyrhythmmania.engine.tempo.Swing
import polyrhythmmania.engine.tempo.TempoChange
import polyrhythmmania.engine.timesignature.TimeSignature
import polyrhythmmania.soundsystem.BeadsMusic
import polyrhythmmania.soundsystem.SoundSystem
import polyrhythmmania.soundsystem.TimingProvider
import polyrhythmmania.soundsystem.sample.GdxAudioReader
import polyrhythmmania.soundsystem.sample.LoopParams
import polyrhythmmania.util.TempFileUtils
import polyrhythmmania.world.World
import polyrhythmmania.world.WorldSettings
import polyrhythmmania.world.entity.EntityInputFeedback
import polyrhythmmania.world.render.ForceTexturePack
import polyrhythmmania.world.render.WorldRenderer
import polyrhythmmania.world.render.WorldRendererWithUI
import polyrhythmmania.world.texturepack.*
import polyrhythmmania.world.tileset.Tileset
import java.io.File
import java.time.Instant
import java.time.LocalDateTime
import java.time.ZoneOffset
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArrayList
import java.util.zip.Deflater
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.math.min
/**
* A [Container] holds together the pieces for a game: the [World], [WorldRenderer], optional [SoundSystem],
* [TimingProvider], and [Engine].
*
* It also contains the external resources that have to be tracked for loading/unloading.
*
* There are also pre-defined external resources as a utility.
*/
class Container(
val soundSystem: SoundSystem?, timingProvider: TimingProvider,
val globalSettings: GlobalContainerSettings,
) : Disposable {
companion object {
const val LEVEL_FILE_EXTENSION: String = "prmania"
const val PROJECT_FILE_EXTENSION: String = "prmproj"
const val CONTAINER_VERSION: Int = 13
const val RES_KEY_COMPRESSED_MUSIC: String = "compressed_music"
val DEFAULT_TRACKS_BEFORE_V7: List<String> = listOf(
"input_0",
"input_1",
"input_2",
"fx_0",
"fx_1"
) // Default tracks indexes for container version 6 and below
const val VERSION_LEVEL_METADATA_ADDED: Int = 9
const val VERSION_EXPORT_STATISTICS_ADDED: Int = 10
const val VERSION_MULTIPLE_TEX_PACK_ADDED: Int = 12
val MIN_BANNER_SIZE: WindowSize = WindowSize(256, 80)
val MAX_BANNER_SIZE: WindowSize = WindowSize(512, 160)
fun isBannerTextureWithinSize(tex: Texture): Boolean {
return tex.width in MIN_BANNER_SIZE.width..MAX_BANNER_SIZE.width
&& tex.height in MIN_BANNER_SIZE.height..MAX_BANNER_SIZE.height
}
}
val world: World = World(globalSettings.numberOfSpotlightsOverride)
val timing: TimingProvider = timingProvider // Could also be the SoundSystem in theory
val engine: Engine = Engine(timing, world, soundSystem, this)
val texturePack: Var<TexturePack> by lazy { Var(StockTexturePacks.gba) } // Lazy due to late init in StockTexturePacks
val customTexturePacks: Array<Var<CustomTexturePack?>> = Array(TexturePackSource.CUSTOM_RANGE.last) { Var(null) }
val texturePackSource: Var<TexturePackSource> = Var(TexturePackSource.StockGBA)
val renderer: WorldRendererWithUI by lazy {
WorldRendererWithUI(world, Tileset(
when (globalSettings.forceTexturePack) {
ForceTexturePack.NO_FORCE -> this.texturePack
ForceTexturePack.FORCE_GBA -> Var(StockTexturePacks.gba)
ForceTexturePack.FORCE_HD -> Var(StockTexturePacks.hd)
ForceTexturePack.FORCE_ARCADE -> Var(StockTexturePacks.arcade)
}
).apply {
world.tilesetPalette.applyTo(this)
}, engine
)
}
private val _blocks: MutableList<Block> = CopyOnWriteArrayList()
val blocks: List<Block> get() = _blocks
var resultsText: ResultsText = ResultsText.DEFAULT
var levelMetadata: LevelMetadata = LevelMetadata.DEFAULT_METADATA.copy(
initialCreationDate = LocalDateTime.ofInstant(
Instant.now(),
ZoneOffset.UTC
)
)
var wasLevelMetadataLoaded: Boolean = false
private set
val trackIDs: List<TrackID> = Editor.DEFAULT_TRACKS.map { it.id } // Intentionally doesn't use editor.tracks
val storyModeMetadata: Var<StoryModeContainerMetadata> = Var(StoryModeContainerMetadata.BLANK)
private val _resources: MutableMap<String, ExternalResource> = ConcurrentHashMap()
val resources: Map<String, ExternalResource> get() = _resources
var compressedMusic: ExternalResource? = null
private set
var bannerTexture: Var<Texture?> = Var(null)
var lastBlockPosition: FloatVar = FloatVar(0f) // Position of very last block
private set
var endBlockPosition: FloatVar = FloatVar(Float.POSITIVE_INFINITY) // Position of first End State block
private set
val stopPosition: ReadOnlyFloatVar = FloatVar {
// endPosition if < Infinity, otherwise lastBlockPosition
val endBlockPos = endBlockPosition.use()
if (endBlockPos < Float.POSITIVE_INFINITY) endBlockPos else lastBlockPosition.use()
}
init {
engine.inputter.skillStarGotten.addListener {
if (it.getOrCompute()) {
renderer.fireSkillStar()
}
}
storyModeMetadata.addListener {
val metadata = it.getOrCompute()
engine.inputter.inputChallenge.restriction = metadata.inputTimingRestriction
val livesMode = engine.modifiers.livesMode
val lives = metadata.lives
livesMode.enabled.set(lives > 0)
livesMode.maxLives.set(lives)
livesMode.resetState()
val defectiveRodsMode = engine.modifiers.defectiveRodsMode
val defectiveThreshold = metadata.defectiveRodsThreshold
defectiveRodsMode.enabled.set(defectiveThreshold > 0)
defectiveRodsMode.maxLives.set(defectiveThreshold)
defectiveRodsMode.resetState()
val monsterGoal = engine.modifiers.monsterGoal
val monsterEnabled = metadata.monsterEnabled
monsterGoal.enabled.set(monsterEnabled)
monsterGoal.difficulty.set(metadata.monsterDifficulty)
monsterGoal.recoveryPenalty.set(metadata.monsterRecoveryPenalty)
monsterGoal.resetState()
world.resetWorld()
}
}
/**
* Resets all mutable state within this [Container].
*/
fun resetMutableState() {
engine.resetMutableState()
world.resetWorld()
this.globalSettings.applyForcedTilesetPaletteSettings(this)
this.globalSettings.applyForcedSignLanguageSettings(this.world)
this.setTexturePackFromSource()
val blocks = this.blocks.toList()
engine.addEvents(blocks.flatMap { it.compileIntoEvents() })
resetInputFeedbackEntities()
}
/**
* Refreshes the feedback entity's base color if the input timing restriction is different
*/
fun resetInputFeedbackEntities() {
world.entities.filterIsInstance<EntityInputFeedback>().forEach { ent ->
ent.updateCurrentColor(engine)
}
}
fun getTexturePackFromSource(source: TexturePackSource): TexturePack? {
return when (source) {
TexturePackSource.StockGBA -> StockTexturePacks.gba
TexturePackSource.StockHD -> StockTexturePacks.hd
TexturePackSource.StockArcade -> StockTexturePacks.arcade
is TexturePackSource.Custom -> getCustomTexturePackAsCascading(source.id - 1)
}
}
fun setTexturePackFromSource(source: TexturePackSource = texturePackSource.getOrCompute()): TexturePack {
val chosen = getTexturePackFromSource(source) ?: StockTexturePacks.gba
texturePack.set(chosen)
return chosen
}
fun getCustomTexturePackAsCascading(index: Int): CascadingTexturePack? {
val ctp = customTexturePacks[index].getOrCompute()
if (ctp != null) {
return CascadingTexturePack(
"cascading_custom", emptySet(),
listOf(
ctp,
StockTexturePacks.allPacksByIDWithDeprecations[ctp.fallbackID.getOrCompute()]
?: StockTexturePacks.gba
)
)
}
return null
}
fun setCompressedMusic(res: ExternalResource?) {
val oldCompressedMusic = this.compressedMusic
if (oldCompressedMusic != res) {
if (res != null) {
addResource(res)
} else {
if (oldCompressedMusic != null) {
removeResource(oldCompressedMusic.key)
}
}
this.compressedMusic = res
}
}
fun addResource(res: ExternalResource) {
val key = res.key
val existing = _resources[key]
if (existing != res) { // Don't dispose and add if it is the same resource
existing?.dispose()
_resources[key] = res
}
}
fun removeResource(key: String) {
val removed = _resources.remove(key)
removed?.dispose()
}
fun updateLastPoints() {
val blocks = this.blocks.sortedBy { it.beat }
val firstEndBlock: BlockEndState? = blocks.firstOrNull { it is BlockEndState } as? BlockEndState?
lastBlockPosition.set(blocks.lastOrNull()?.beat ?: 0f)
endBlockPosition.set(firstEndBlock?.beat ?: Float.POSITIVE_INFINITY)
}
fun addBlock(block: Block) {
val blocks = this._blocks
if (block !in blocks) {
blocks.add(block)
updateLastPoints()
}
}
fun addBlocks(blocksToAdd: List<Block>) {
val blocks = this._blocks
blocksToAdd.forEach { block ->
if (block !in blocks) {
blocks.add(block)
}
}
updateLastPoints()
}
fun removeBlock(block: Block) {
val blocks = this._blocks
blocks.remove(block)
updateLastPoints()
}
fun removeBlocks(blocksToAdd: List<Block>) {
val blocks = this._blocks
blocks.removeAll(blocksToAdd)
updateLastPoints()
}
override fun dispose() {
if (engine.metricsEnabled) {
engine.metricsReporter.report()
}
soundSystem?.dispose()
customTexturePacks.forEach { pack ->
(pack.getOrCompute() as? Disposable)?.disposeQuietly()
}
resources.values.toList().forEach { it.disposeQuietly() }
_resources.clear()
renderer.disposeQuietly()
}
/*
Container file format:
- Compressed zip archive with file extension .prmania
- /manifest.json
- /res/
- Contains the ExternalResources
*/
/**
* Writes the [Container] to a file.
*/
fun writeToFile(file: File, saveOptions: SaveOptions) {
if (!file.exists()) {
file.createNewFile()
} else {
if (!file.isFile) throw ContainerException("File input given was not a file: ${file.absolutePath}")
}
val extRes: List<ExternalResource> = this.resources.values.toList()
val extResMap: Map<ExternalResource, String /* UUID */> = extRes.associateWith { UUID.randomUUID().toString() }
// Create manifest
val jsonObj: JsonObject = Json.`object`()
val libraryRelevantData = LibraryRelevantData(
CONTAINER_VERSION,
PRMania.VERSION,
saveOptions.isAutosave,
saveOptions.exportStatistics,
if (!saveOptions.isProject) UUID.randomUUID() else null,
levelMetadata.truncateWithLimits()
)
libraryRelevantData.writeToManifestJson(jsonObj)
jsonObj.add("resources", Json.`object`().also { obj ->
obj.add("list", Json.array().also { array ->
extResMap.forEach { (res, uuid) ->
array.add(Json.`object`().also { resObj ->
resObj.add("key", res.key)
resObj.add("uuid", uuid)
resObj.add("ext", res.file.extension)
})
}
})
})
jsonObj.add("editor", Json.`object`().also { editorObj ->
editorObj.add("trackIndexes", Json.`object`().also { trackIndexesObj ->
val trackIDs = this.trackIDs
trackIndexesObj.add("count", trackIDs.size)
trackIndexesObj.add("ids", Json.array(*trackIDs.map { it.id }.toTypedArray()))
})
})
jsonObj.add("engine", Json.`object`().also { engineObj ->
engineObj.add("tempo", Json.`object`().also { tempoObj ->
fun TempoChange.encode(): JsonValue {
return Json.`object`().also { o ->
o.add("beat", this.beat)
o.add("tempo", this.newTempo)
if (this.newSwing != Swing.STRAIGHT) {
o.add("swing", Json.`object`().also { so ->
so.add("ratio", this.newSwing.ratio)
so.add("div", this.newSwing.division)
})
}
}
}
val globalTempo = engine.tempos.getGlobalTempo()
tempoObj.add("startingTempo", globalTempo.encode())
tempoObj.add("changes", Json.array().also { array ->
(engine.tempos.getAllTempoChanges().toList() - globalTempo).sortedBy { it.beat }.forEach { tc ->
array.add(tc.encode())
}
})
})
engineObj.add("music", Json.`object`().also { musicObj ->
val musicData = engine.musicData
musicObj.add("volumes", Json.array().also { array ->
fun MusicVolume.encode(): JsonValue {
return Json.`object`().also { o ->
o.add("beat", this.beat)
o.add("width", this.width)
o.add("vol", this.newVolume)
}
}
(musicData.volumeMap.getAllMusicVolumes().toList()).forEach { mv ->
array.add(mv.encode())
}
})
musicObj.add("firstBeatSec", musicData.firstBeatSec)
musicObj.add("musicFirstBeat", musicData.musicSyncPointBeat)
musicObj.add("rate", musicData.rate) // As of container version 6
val loopParams = musicData.loopParams
musicObj.add("looping", loopParams.loopType == SamplePlayer.LoopType.LOOP_FORWARDS)
musicObj.add("loopStartMs", loopParams.startPointMs)
musicObj.add("loopEndMs", loopParams.endPointMs)
})
// As of container version 2:
engineObj.add("timeSignatures", Json.`object`().also { timeSigObj ->
timeSigObj.add("list", Json.array().also { array ->
engine.timeSignatures.map.values.forEach {
val node = Json.`object`()
node.set("beat", it.beat)
node.set("divisions", it.beatsPerMeasure)
node.set("beatUnit", it.beatUnit)
node.set("measure", it.measure)
array.add(node)
}
})
})
})
jsonObj.add("blocks", Json.array().also { blocksArray ->
val classMapping: Map<Class<*>, Instantiator<*>> = Instantiators.classMapping
for (block in blocks.toList()) {
val o = Json.`object`()
val javaClass = block.javaClass
val inst = classMapping[javaClass] ?: continue
o.add("inst", inst.id)
block.writeToJson(o)
blocksArray.add(o)
}
})
jsonObj.add("tilesetConfig", Json.`object`().also { tilesetConfigObj ->
tilesetConfigObj.add("palette", this.world.tilesetPalette.toJson())
tilesetConfigObj.add("texturePack", Json.`object`().also { texturePackObj ->
if (customTexturePacks.any { it.getOrCompute() != null }) {
texturePackObj.add("hasCustom", true)
// As of container version 12:
texturePackObj.add("slotCount", customTexturePacks.size)
texturePackObj.add("presentIndices", Json.array().also { arr ->
customTexturePacks.forEachIndexed { index, varr ->
if (varr.getOrCompute() != null) {
arr.add(index)
}
}
})
}
val currentTexturePack = texturePack.getOrCompute()
val src = texturePackSource.getOrCompute()
if (src is TexturePackSource.Custom) {
texturePackObj.add("source", "custom")
texturePackObj.add("srcIndex", src.id - 1)
} else {
texturePackObj.add("source", "stock")
texturePackObj.add("stockID", currentTexturePack.id)
}
})
})
val resultsText = this.resultsText
if (resultsText != ResultsText.DEFAULT) {
jsonObj.add("resultsText", resultsText.toJson())
}
val worldSettings = this.world.worldSettings
if (worldSettings != WorldSettings.DEFAULT) {
jsonObj.add("worldSettings", worldSettings.toJson())
}
val storyModeMetadata = this.storyModeMetadata.getOrCompute()
if (storyModeMetadata != StoryModeContainerMetadata.BLANK) {
jsonObj.add("storyModeMetadata", storyModeMetadata.toJson())
}
// Pack
file.outputStream().use { fos ->
ZipOutputStream(fos).use { zip ->
zip.setComment("Polyrhythm Mania level file - ${PRMania.VERSION}")
zip.putNextEntry(ZipEntry("manifest.json"))
val jsonWriter = zip.bufferedWriter()
jsonObj.writeTo(jsonWriter, WriterConfig.PRETTY_PRINT)
jsonWriter.flush()
zip.closeEntry()
// Resources
val resDir = "res/"
zip.putNextEntry(ZipEntry(resDir))
zip.closeEntry()
extResMap.forEach { (res, uuid) ->
zip.putNextEntry(ZipEntry("${resDir}${uuid}"))
res.file.inputStream().use { input ->
input.copyTo(zip)
}
zip.closeEntry()
}
customTexturePacks.map { it.getOrCompute() }.forEachIndexed { index, pack ->
if (pack != null) {
val tmp = TempFileUtils.createTempFile("savingtexpack")
tmp.outputStream().use { tmpOutputStream ->
ZipOutputStream(tmpOutputStream).use { texPackZip ->
texPackZip.setLevel(Deflater.NO_COMPRESSION)
pack.writeToOutputStream(texPackZip)
}
}
zip.putNextEntry(ZipEntry("${resDir}texture_pack_${index}.zip"))
tmp.inputStream().use { input ->
input.copyTo(zip)
}
zip.closeEntry()
tmp.delete()
}
}
val bannerTexture = this.bannerTexture.getOrCompute()
if (bannerTexture != null) {
try {
val tmp = TempFileUtils.createTempFile("savinglvlbanner")
val texData = bannerTexture.textureData
if (!texData.isPrepared) {
texData.prepare()
}
val pixmap = bannerTexture.textureData.consumePixmap()
PixmapIO.writePNG(FileHandle(tmp), pixmap)
if (texData.disposePixmap()) {
pixmap.disposeQuietly()
}
zip.putNextEntry(ZipEntry("banner.png"))
tmp.inputStream().use { input ->
input.copyTo(zip)
}
zip.closeEntry()
tmp.delete()
} catch (e: Exception) {
Paintbox.LOGGER.error("Failed to save level banner!")
e.printStackTrace()
}
}
}
}
}
/**
* Reads container info from a file. This should only be called on a NEW [Container] object!
*/
fun readFromFile(file: File, editorFlags: EnumSet<EditorSpecialFlags>): LoadMetadata {
val zipFile = ZipFile(file)
val json: JsonObject
zipFile.getInputStream(zipFile.getFileHeader("manifest.json")).use { zipInputStream ->
val reader = zipInputStream.reader()
json = Json.parse(reader).asObject()
}
val libraryRelevantDataLoad = LibraryRelevantData.fromManifestJson(json, file.lastModified())
val libraryRelevantData: LibraryRelevantData = libraryRelevantDataLoad.first
val containerVersion: Int = libraryRelevantData.containerVersion
this.wasLevelMetadataLoaded = libraryRelevantDataLoad.second.wasLevelMetadataLoaded
if (libraryRelevantData.levelMetadata != null) {
this.levelMetadata = libraryRelevantData.levelMetadata
}
val resourcesMap: Map<String, ResourceTag> =
json.get("resources").asObject().get("list").asArray().associate { value ->
value as JsonObject
val res = ResourceTag(
value.getString("key", null),
value.getString("uuid", null)!!,
value.getString("ext", "tmp")
)
Pair(res.key, res)
}
val engineObj = json.get("engine").asObject()
val tempoObj = engineObj.get("tempo").asObject()
fun JsonObject.decodeTempoChange(): TempoChange {
val swingObj = this.get("swing")
val swing: Swing =
if (swingObj != null && swingObj.isObject && EditorSpecialFlags.STORY_MODE in editorFlags) {
swingObj as JsonObject
Swing(
swingObj.getInt("ratio", Swing.STRAIGHT.ratio),
swingObj.getFloat("div", Swing.STRAIGHT.division)
)
} else Swing.STRAIGHT
return TempoChange(this.getFloat("beat", 0f), this.getFloat("tempo", 1f), swing)
}
engine.tempos.addTempoChange(tempoObj.get("startingTempo").asObject().decodeTempoChange().copy(beat = 0f))
engine.tempos.addTempoChangesBulk(
tempoObj.get("changes").asArray().map { value ->
value.asObject().decodeTempoChange()
}
)
val musicObj = engineObj.get("music").asObject()
val volumesObj = musicObj.get("volumes").asArray()
fun JsonObject.decodeMusicVolume(): MusicVolume {
return MusicVolume(this.getFloat("beat", 0f), this.getFloat("width", 0f), this.getInt("vol", 100))
}
engine.musicData.volumeMap.addMusicVolumesBulk(
volumesObj.asArray().map { value ->
value.asObject().decodeMusicVolume()
}
)
engine.musicData.also { musicData ->
musicData.firstBeatSec = musicObj.getFloat("firstBeatSec", 0f)
musicData.musicSyncPointBeat = musicObj.getFloat("musicFirstBeat", 0f)
musicData.loopParams = LoopParams(
if (musicObj.getBoolean(
"looping",
false
)
) SamplePlayer.LoopType.LOOP_FORWARDS else SamplePlayer.LoopType.NO_LOOP_FORWARDS,
musicObj.getDouble("loopStartMs", 0.0),
musicObj.getDouble("loopEndMs", 0.0)
)
if (containerVersion >= 6) {
val rateField = musicObj.get("rate")?.asFloat()
if (rateField != null) {
musicData.rate = rateField.coerceAtLeast(0f)
}
}
}
if (containerVersion >= 2) {
val timeSigObj = engineObj.get("timeSignatures").asObject()
val list = timeSigObj.get("list").asArray()
list.forEach {
val obj = it.asObject()
engine.timeSignatures.add(
TimeSignature(
obj.getFloat("beat", 0f),
obj.getInt("divisions", 4),
obj.getInt("beatUnit", 4)
)
)
}
}
val customTexturePacksRead: Array<CustomTexturePack.ReadResult?> = Array(this.customTexturePacks.size) { null }
if (containerVersion >= 3) {
val tilesetObj = json.get("tilesetConfig")?.asObject()
if (tilesetObj != null) {
if (containerVersion <= 7) {
// Container version [3, 7]: tilesetConfig is the actual tilesetPalette object.
val tilesetPalette = this.world.tilesetPalette
tilesetPalette.fromJson(tilesetObj)
tilesetPalette.allMappings.forEach { it.enabled.set(true) }
} else {
// Container version [8, ): tilesetConfig is a larger obj. Palette is in own object "palette" now.
val paletteObj = tilesetObj.get("palette")?.asObject()
if (paletteObj != null) {
val tilesetPalette = this.world.tilesetPalette
tilesetPalette.fromJson(paletteObj)
tilesetPalette.allMappings.forEach { it.enabled.set(true) }
}
val texturePackObj = tilesetObj.get("texturePack")?.asObject()
if (texturePackObj != null) {
when (val source: String = texturePackObj.getString("source", "")) {
"stock" -> {
val stockID: String = texturePackObj.getString("stockID", "")
val pack = StockTexturePacks.allPacksByIDWithDeprecations[stockID]
if (pack != null) {
texturePack.set(pack)
val sourceFromPack = StockTexturePacks.getTexturePackSource(pack)
if (sourceFromPack != null) {
texturePackSource.set(sourceFromPack)
} else {
texturePackSource.set(TexturePackSource.StockGBA)
Paintbox.LOGGER.warn("[Container] TexturePackSource was not mapped for stock texture pack ${pack.id}, setting to GBA")
}
} else {
Paintbox.LOGGER.warn("[Container] Unknown tilesetConfig.texturePack.stockID '${stockID}', skipping stock texture pack")
texturePack.set(StockTexturePacks.gba)
texturePackSource.set(TexturePackSource.StockGBA)
}
}
"custom" -> {
// index is present as of container version 12
if (containerVersion >= VERSION_MULTIPLE_TEX_PACK_ADDED) {
texturePackSource.set(
TexturePackSource.Custom(
(texturePackObj.get("srcIndex")
.asInt() + 1).coerceIn(TexturePackSource.CUSTOM_RANGE)
)
)
} else {
texturePackSource.set(TexturePackSource.Custom(1)) // Default to first pack for older levels
}
}
else -> {
// Ignore texture packs. Just use default GBA
Paintbox.LOGGER.warn("[Container] Unknown tilesetConfig.texturePack.source '${source}', skipping")
texturePackSource.set(TexturePackSource.StockGBA)
}
}
val hasCustomPack = texturePackObj.get("hasCustom")?.asBoolean() == true
if (containerVersion < VERSION_MULTIPLE_TEX_PACK_ADDED && hasCustomPack) {
zipFile.getInputStream(zipFile.getFileHeader("res/texture_pack.zip"))
.use { zipInputStream ->
val tempFile = TempFileUtils.createTempFile("extres", ".zip")
val out = tempFile.outputStream()
zipInputStream.copyTo(out)
val f = ZipFile(tempFile)
val readResult = CustomTexturePack.readFromStream(f)
customTexturePacksRead[0] = readResult
tempFile.delete()
}
} else if (containerVersion >= VERSION_MULTIPLE_TEX_PACK_ADDED && hasCustomPack) {
val slotCount = texturePackObj.get("slotCount").asInt()
val presentIndicesArr = texturePackObj.get("presentIndices").asArray()
val presentIndices: Set<Int> =
presentIndicesArr.filter { it.isNumber }.map { it.asInt() }.toSet()
for (i in 0..<min(slotCount, this.customTexturePacks.size)) {
if (i in presentIndices) {
zipFile.getInputStream(zipFile.getFileHeader("res/texture_pack_${i}.zip"))
.use { zipInputStream ->
val tempFile = TempFileUtils.createTempFile("extres", ".zip")
val out = tempFile.outputStream()
zipInputStream.copyTo(out)
val f = ZipFile(tempFile)
val readResult = CustomTexturePack.readFromStream(f)
customTexturePacksRead[i] = readResult
tempFile.delete()
}
}
}
}
}
}
world.tilesetPalette.applyTo(renderer.tileset)
}
}
if (containerVersion >= 4) {
val resultsObj = json.get("resultsText")?.asObject()
if (resultsObj != null) {
this.resultsText = ResultsText.fromJson(resultsObj)
}
}
if (containerVersion >= 5) {
val worldSettingsObj = json.get("worldSettings")?.asObject()
if (worldSettingsObj != null) {
this.world.worldSettings = WorldSettings.fromJson(worldSettingsObj)
}
}
if (containerVersion >= 12 && EditorSpecialFlags.STORY_MODE in editorFlags) {
val storyModeMetadataObj = json.get("storyModeMetadata")?.asObject()
if (storyModeMetadataObj != null) {
this.storyModeMetadata.set(StoryModeContainerMetadata.fromJson(storyModeMetadataObj))
}
}
val blocksObj = json.get("blocks").asArray()
val instantiators = Instantiators.instantiatorMap
val blocks: MutableList<Block> = mutableListOf()
for (value in blocksObj) {
val obj = value.asObject()
val instID = obj.getString("inst", null)
@Suppress("UNCHECKED_CAST")
val inst = (instantiators[instID] as? Instantiator<Block>?)
if (inst == null) {
if (instID != null) {
Paintbox.LOGGER.warn("[Container] Missing instantiator ID '$instID', skipping")
}
continue
} else if (containerVersion >= 11) {
if (!inst.canBeShown(editorFlags)) {
if (instID != null) {
Paintbox.LOGGER.warn("[Container] Instantiator '$instID' is not compatible with current editor flags $editorFlags, skipping")
}
continue
}
}
val block: Block = inst.factory.invoke(inst, engine)
block.readFromJson(obj, editorFlags)
blocks.add(block)
}
blocks.sortWith(Block.getComparator())
this.addBlocks(blocks)
engine.addEvents(blocks.flatMap { it.compileIntoEvents() })
resourcesMap.forEach { (key, res) ->
zipFile.getInputStream(zipFile.getFileHeader("res/${res.uuid}")).use { zipInputStream ->
val tempFile = TempFileUtils.createTempFile("extres", ".${res.ext}")
val out = tempFile.outputStream()
zipInputStream.copyTo(out)
addResource(ExternalResource(key, tempFile, true))
}
}
val compressedMusicRes = resources[RES_KEY_COMPRESSED_MUSIC]
this.compressedMusic = compressedMusicRes
// Set up music and other resources
if (compressedMusicRes != null) {
// Music reader decompressed to another file, so the original compressedMusic file is not a dependency after
val newMusic: BeadsMusic = GdxAudioReader.newMusic(FileHandle(compressedMusicRes.file), null)
engine.musicData.beadsMusic = newMusic
engine.musicData.update()
}
var levelBannerFile: File? = null
val bannerHeader = zipFile.getFileHeader("banner.png")
if (bannerHeader != null) {
zipFile.getInputStream(bannerHeader).use { zipInputStream ->
val tempFile = TempFileUtils.createTempFile("banner", ".png")
val out = tempFile.outputStream()
zipInputStream.copyTo(out)
levelBannerFile = tempFile
}
}
zipFile.closeQuietly()
return LoadMetadata(this, libraryRelevantData, customTexturePacksRead, levelBannerFile)
}
class LoadMetadata(
val container: Container, val libraryRelevantData: LibraryRelevantData,
val customTexturePacksRead: Array<CustomTexturePack.ReadResult?>,
val levelBannerFile: File?,
) {
val containerVersion: Int = libraryRelevantData.containerVersion
val programVersion: Version = libraryRelevantData.programVersion
val isFutureVersion: Boolean = (programVersion > PRMania.VERSION) || (containerVersion > CONTAINER_VERSION)
/**
* Must be called on the GL thread.
*/
fun loadOnGLThread() {
customTexturePacksRead.forEachIndexed { index, readResult ->
if (readResult != null) {
val ctp = readResult.createAndLoadTextures()
container.customTexturePacks[index].set(ctp)
}
}
container.setTexturePackFromSource()
if (levelBannerFile != null) {
try {
val tex = Texture(FileHandle(levelBannerFile))
tex.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
if (!isBannerTextureWithinSize(tex)) {
Paintbox.LOGGER.warn("Ignoring banner texture because it is not the right size (${tex.width}x${tex.height})")
tex.disposeQuietly()
} else {
val old = container.bannerTexture.getOrCompute()
container.bannerTexture.set(null)
old?.disposeQuietly()
container.bannerTexture.set(tex)
}
} catch (e: Exception) {
Paintbox.LOGGER.error("Failed to load banner texture!")
e.printStackTrace()
}
}
}
}
}