Skip to content

Commit

Permalink
It actually works
Browse files Browse the repository at this point in the history
  • Loading branch information
StellarWitch7 committed Aug 19, 2024
1 parent dd9ab10 commit d3c77f4
Show file tree
Hide file tree
Showing 17 changed files with 195 additions and 26 deletions.
7 changes: 7 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id 'fabric-loom' version '1.7-SNAPSHOT'
id 'maven-publish'
id 'me.fallenbreath.yamlang' version '1.3.1'
id "org.jetbrains.kotlin.jvm" version "2.0.10"
}

Expand Down Expand Up @@ -90,6 +91,7 @@ dependencies {
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-base:${property('deps.cardinal-components-api')}")
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-entity:${property('deps.cardinal-components-api')}")
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-chunk:${property('deps.cardinal-components-api')}")
include modImplementation("org.ladysnake.cardinal-components-api:cardinal-components-world:${property('deps.cardinal-components-api')}")

modImplementation "maven.modrinth:trickster:${property('deps.trickster')}"
modImplementation "io.wispforest:accessories-fabric:${property('deps.accessories')}"
Expand Down Expand Up @@ -132,6 +134,11 @@ jar {
}
}

yamlang {
targetSourceSets = [sourceSets.main]
inputDir = "assets/ram/lang"
}

// configure the maven publication
publishing {
publications {
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ deps.owo-lib=0.12.10+1.21
deps.lavender=0.1.11+1.21
deps.cardinal-components-api=6.1.1
deps.cicada=0.8.3+1.21-and-above
deps.trickster=2.0.0-alpha.21
deps.trickster=2.0.0-alpha.22
deps.accessories=1.0.0-beta.11+1.21
deps.modmenu=11.0.1

Expand Down
3 changes: 3 additions & 0 deletions src/main/kotlin/stellarwitch7/ram/RandomAccessMind.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stellarwitch7.ram
import net.fabricmc.api.ModInitializer
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import stellarwitch7.ram.spell.trick.ModTricks

object RandomAccessMind : ModInitializer {
const val MOD_ID: String = "ram"
Expand All @@ -13,5 +14,7 @@ object RandomAccessMind : ModInitializer {
// However, some things (like resources) may still be uninitialized.
// Proceed with mild caution.
logger.info("SIGSEGV")

ModTricks.register()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package stellarwitch7.ram.cca.block

object ModBlockComponents {
//TODO
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import stellarwitch7.libstellar.registry.cca.entity.CCAEntityComponentRegistrar
import stellarwitch7.ram.RandomAccessMind
import stellarwitch7.ram.cca.RAMComponent

//TODO: something something private can't access Kotlin whatnot
object ModEntityComponents : CCAEntityComponentRegistrar() {
override val modID: String = RandomAccessMind.MOD_ID

Expand Down
27 changes: 19 additions & 8 deletions src/main/kotlin/stellarwitch7/ram/spell/mind/DefaultRAM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import dev.enjarai.trickster.spell.fragment.VoidFragment
import dev.enjarai.trickster.spell.trick.Trick
import stellarwitch7.libstellar.registry.codec.CodecType
import stellarwitch7.libstellar.utils.KCodecUtils
import stellarwitch7.ram.spell.trick.blunder.NotWithinRAMBoundsBlunder
import stellarwitch7.ram.spell.trick.blunder.OutOfRAMBlunder

open class DefaultRAM(val size: UInt) : RAM {
Expand All @@ -17,26 +18,36 @@ open class DefaultRAM(val size: UInt) : RAM {
this.slots = slots.toTypedArray()
}

override fun malloc(source: Trick): UInt {
override fun alloc(source: Trick): UInt {
for ((i, slot) in slots.withIndex()) {
if (slot.free)
if (slot.free) {
slot.free = false
return i.toUInt()
}
}

throw OutOfRAMBlunder(source)
}

override fun free(address: UInt) {
slots[address.toInt()].free = true
override fun free(source: Trick, address: Int) {
if (address < 0 || address >= slots.size)
throw NotWithinRAMBoundsBlunder(source, slots.size.toUInt(), address.toDouble())

slots[address].free = true
}

override fun write(address: UInt, value: Fragment) {
slots[address.toInt()].value = value
override fun write(source: Trick, address: Int, value: Fragment) {
if (address < 0 || address >= slots.size)
throw NotWithinRAMBoundsBlunder(source, slots.size.toUInt(), address.toDouble())

slots[address].value = value
}

override fun read(address: UInt): Fragment {
return slots[address.toInt()].value
override fun read(source: Trick, address: Int): Fragment {
if (address < 0 || address >= slots.size)
throw NotWithinRAMBoundsBlunder(source, slots.size.toUInt(), address.toDouble())

return slots[address].value
}

companion object {
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/stellarwitch7/ram/spell/mind/RAM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import stellarwitch7.libstellar.registry.codec.CodecTypeProvider
import stellarwitch7.ram.RandomAccessMind

interface RAM : CodecTypeProvider<RAM> {
fun malloc(source: Trick): UInt
fun alloc(source: Trick): UInt

fun free(address: UInt)
fun free(source: Trick, address: Int)

fun write(address: UInt, value: Fragment)
fun write(source: Trick, address: Int, value: Fragment)

fun read(address: UInt): Fragment
fun read(source: Trick, address: Int): Fragment

companion object : CodecRegistrar<RAM> {
override val modID: String = RandomAccessMind.MOD_ID
Expand Down
19 changes: 18 additions & 1 deletion src/main/kotlin/stellarwitch7/ram/spell/trick/ModTricks.kt
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package stellarwitch7.ram.spell.trick

object ModTricks {
import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.Tricks
import net.minecraft.registry.Registry
import stellarwitch7.libstellar.registry.Registrar
import stellarwitch7.ram.RandomAccessMind
import stellarwitch7.ram.spell.trick.mind.RAMAllocTrick
import stellarwitch7.ram.spell.trick.mind.RAMFreeTrick
import stellarwitch7.ram.spell.trick.mind.RAMReadTrick
import stellarwitch7.ram.spell.trick.mind.RAMWriteTrick

object ModTricks : Registrar<Trick> {
override val modID: String = RandomAccessMind.MOD_ID
override val registry: Registry<Trick> = Tricks.REGISTRY

val alloc = register("alloc", RAMAllocTrick())
val free = register("free", RAMFreeTrick())
val read = register("read", RAMReadTrick())
val write = register("write", RAMWriteTrick())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package stellarwitch7.ram.spell.trick.blunder

import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.blunder.TrickBlunderException
import net.minecraft.text.MutableText

class NotWithinRAMBoundsBlunder(source: Trick, private val max: UInt, private val given: Double) : TrickBlunderException(source) {
override fun createMessage(): MutableText {
return super.createMessage()
.append("Invalid RAM slot address. Got ")
.append("{}".format(given))
.append(", but must be greater or equal to zero and lesser than ")
.append("{}".format(max))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.blunder.NoPlayerBlunder
import stellarwitch7.ram.cca.entity.ModEntityComponents

class RAMAllocTrick : Trick(Pattern.of(/*TODO*/)) {
class RAMAllocTrick : Trick(Pattern.of(1, 4, 0, 6, 4, 8, 2, 4)) {
override fun activate(ctx: SpellContext, fragments: List<Fragment>): Fragment {
return NumberFragment(ModEntityComponents.ram.get(ctx.source.player.orElseThrow {
NoPlayerBlunder(this)
}).ram.malloc(this).toDouble())
}).ram.alloc(this).toDouble())
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
package stellarwitch7.ram.spell.trick.mind

class RAMFreeTrick {
import dev.enjarai.trickster.spell.Fragment
import dev.enjarai.trickster.spell.Pattern
import dev.enjarai.trickster.spell.SpellContext
import dev.enjarai.trickster.spell.fragment.FragmentType
import dev.enjarai.trickster.spell.fragment.VoidFragment
import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.blunder.NoPlayerBlunder
import stellarwitch7.ram.cca.entity.ModEntityComponents

class RAMFreeTrick : Trick(Pattern.of(1, 4, 6, 8, 4)) {
override fun activate(ctx: SpellContext, fragments: List<Fragment>): Fragment {
val number = expectInput(fragments, FragmentType.NUMBER, 0).number()

ModEntityComponents.ram.get(ctx.source.player.orElseThrow {
NoPlayerBlunder(this)
}).ram.free(this, number.toInt())

return VoidFragment.INSTANCE
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package stellarwitch7.ram.spell.trick.mind

class RAMReadTrick {
import dev.enjarai.trickster.spell.Fragment
import dev.enjarai.trickster.spell.Pattern
import dev.enjarai.trickster.spell.SpellContext
import dev.enjarai.trickster.spell.fragment.FragmentType
import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.blunder.NoPlayerBlunder
import stellarwitch7.ram.cca.entity.ModEntityComponents

class RAMReadTrick : Trick(Pattern.of(1, 4, 3, 6, 8, 5)) {
override fun activate(ctx: SpellContext, fragments: List<Fragment>): Fragment {
val number = expectInput(fragments, FragmentType.NUMBER, 0).number()

return ModEntityComponents.ram.get(ctx.source.player.orElseThrow {
NoPlayerBlunder(this)
}).ram.read(this, number.toInt())
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package stellarwitch7.ram.spell.trick.mind

import dev.enjarai.trickster.spell.Fragment
import dev.enjarai.trickster.spell.Pattern
import dev.enjarai.trickster.spell.SpellContext
import dev.enjarai.trickster.spell.fragment.FragmentType
import dev.enjarai.trickster.spell.fragment.VoidFragment
import dev.enjarai.trickster.spell.trick.Trick
import dev.enjarai.trickster.spell.trick.blunder.NoPlayerBlunder
import stellarwitch7.ram.cca.entity.ModEntityComponents

class RAMWriteTrick : Trick(Pattern.of(1, 4, 5, 8, 6, 3)) {
override fun activate(ctx: SpellContext, fragments: List<Fragment>): Fragment {
val number = expectInput(fragments, FragmentType.NUMBER, 0).number()
val any = expectInput(fragments, 1)

ModEntityComponents.ram.get(ctx.source.player.orElseThrow {
NoPlayerBlunder(this)
}).ram.write(this, number.toInt(), any)

return VoidFragment.INSTANCE
}
}
6 changes: 6 additions & 0 deletions src/main/resources/assets/ram/lang/en_us.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
trickster:
trick.ram:
alloc: Allocator's Ploy
free: Concierge's Ploy
read: Retrieval Ingress
write: Ploy of Caching
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
```json
{
"title": "Random Access Mind",
"icon": "minecraft:barrel",
"category": "trickster:basics"
}
```

The Random Access Mind, or RAM for short,
is a way of storing arbitrary semi-persistent data alongside other unrelated arbitrary data,
through the use of an allocation system.

;;;;;

<|glyph@trickster:templates|trick-id=ram:alloc,title=Allocator's Ploy|>

-> number

---

Attempts to allocate a RAM slot, blundering if there are none free. Returns the address of the allocated slot.

;;;;;

<|glyph@trickster:templates|trick-id=ram:free,title=Concierge's Ploy|>

number ->

---

Frees the RAM slot at the given address, permitting later re-allocation.

;;;;;

<|glyph@trickster:templates|trick-id=ram:read,title=Retrieval Ingress|>

number -> any

---

Returns the fragment stored within the RAM slot at the given address.

;;;;;

<|glyph@trickster:templates|trick-id=ram:write,title=Ploy of Caching|>

number, any ->

---

Stores the given fragment in the RAM slot at the given address, overwriting what was there previously.
9 changes: 6 additions & 3 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,11 @@
"adapter": "kotlin"
}
],
"cardinal-components": [
"stellarwitch7.ram.cca.entity.ModEntityComponents"
"cardinal-components-entity": [
{
"value": "stellarwitch7.ram.cca.entity.ModEntityComponents",
"adapter": "kotlin"
}
]
},
"custom": {
Expand All @@ -50,7 +53,7 @@
"fabric-api": "*",
"fabric-language-kotlin": "*",
"libstellar": "1.0.0",
"trickster": "2.0.0-alpha.21"
"trickster": ">=2.0.0-alpha.22"
},
"suggests": {
}
Expand Down

0 comments on commit d3c77f4

Please sign in to comment.