Skip to content

Commit

Permalink
Merge branch 'v2' of private.github.com:0xobelisk/obelisk-engine into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
vladilen11 committed Sep 24, 2023
2 parents 6b5034d + b908b30 commit 995e51c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 52 deletions.
2 changes: 1 addition & 1 deletion examples/Move.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[move]
version = 0
manifest_digest = "29EBC8EEB6008703DCFCFC4C49D84765A6C3F555229A11B3917FD950A89CBD15"
manifest_digest = "CDB2CA0F065ECB73601AA770888CB620BD26EEF2D4C702F01BE21AF87435406F"
deps_digest = "112928C94A84031C09CD9B9D1D44B149B73FC0EEA5FA8D8B2D7CA4D91936335A"

dependencies = [
Expand Down
26 changes: 14 additions & 12 deletions examples/sources/codegen/components/multi_column.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module examples::multi_column_comp {
// Systems
friend examples::example_system;

const NAME: vector<u8> = b"multi_column";

public fun id(): address {
entity_key::from_bytes(b"Multi_column Comp")
entity_key::from_bytes(NAME)
}

// state
Expand All @@ -26,35 +28,35 @@ module examples::multi_column_comp {
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_component<Table<address,Field>>(
world::add_comp<Table<address,Field>>(
world,
id(),
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(state, last_update_time);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, state: vector<u8>, last_update_time: u64) {
let component = world::get_mut_component<Table<address, Field>>(world, id());
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(state, last_update_time);
field.data = data;
world::emit_update_event(id(), some(key), data)
}
public(friend) fun update_state(world: &mut World, key: address, state: vector<u8>) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
let data = encode(state, last_update_time);
Expand All @@ -63,7 +65,7 @@ module examples::multi_column_comp {
}

public(friend) fun update_last_update_time(world: &mut World, key: address, last_update_time: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let (state, _) = decode(field.data);
let data = encode(state, last_update_time);
Expand All @@ -72,27 +74,27 @@ module examples::multi_column_comp {
}

public fun get(world: &World, key: address): (vector<u8>,u64) {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun get_state(world: &World, key: address): vector<u8> {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (state, _) = decode(field.data);
state
}

public fun get_last_update_time(world: &World, key: address): u64 {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
let (_, last_update_time) = decode(field.data);
last_update_time
}

public fun contains(world: &World, key: address): bool {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

Expand Down
18 changes: 10 additions & 8 deletions examples/sources/codegen/components/single_column.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ module examples::single_column_comp {
// Systems
friend examples::example_system;

const NAME: vector<u8> = b"single_column";

public fun id(): address {
entity_key::from_bytes(b"Single_column Comp")
entity_key::from_bytes(NAME)
}

// value
Expand All @@ -25,42 +27,42 @@ module examples::single_column_comp {
}

public fun register(world: &mut World, ctx: &mut TxContext) {
world::add_component<Table<address,Field>>(
world::add_comp<Table<address,Field>>(
world,
id(),
NAME,
table::new<address,Field>(ctx)
);
}

public(friend) fun add(world: &mut World, key: address, value: u64) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
let data = encode(value);
table::add(component, key, Field { data });
world::emit_add_event(id(), key, data)
}

public(friend) fun remove(world: &mut World, key: address) {
let component = world::get_mut_component<Table<address,Field>>(world, id());
let component = world::get_mut_comp<Table<address,Field>>(world, id());
table::remove(component, key);
world::emit_remove_event(id(), key)
}

public(friend) fun update(world: &mut World, key: address, value: u64) {
let component = world::get_mut_component<Table<address, Field>>(world, id());
let component = world::get_mut_comp<Table<address, Field>>(world, id());
let field = table::borrow_mut<address, Field>(component, key);
let data = encode(value);
field.data = data;
world::emit_update_event(id(), some(key), data)
}

public fun get(world: &World, key: address): u64 {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
let field = table::borrow<address, Field>(component, key);
decode(field.data)
}

public fun contains(world: &World, key: address): bool {
let component = world::get_component<Table<address,Field>>(world, id());
let component = world::get_comp<Table<address,Field>>(world, id());
table::contains<address, Field>(component, key)
}

Expand Down
20 changes: 11 additions & 9 deletions examples/sources/codegen/components/single_struct.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module examples::single_struct_comp {
// Systems
friend examples::example_system;

const NAME: vector<u8> = b"single_struct";

public fun id(): address {
entity_key::from_bytes(b"Single_struct Comp")
entity_key::from_bytes(NAME)
}

// admin
Expand All @@ -24,45 +26,45 @@ module examples::single_struct_comp {
}

public fun register(world: &mut World) {
world::add_component<Field>(
world::add_comp<Field>(
world,
id(),
NAME,
Field { data: encode(@0x1, 100) }
);
}

public(friend) fun update(world: &mut World, admin: address, fee: u64) {
let data = encode(admin, fee);
world::get_mut_component<Field>(world, id()).data = data;
world::get_mut_comp<Field>(world, id()).data = data;
world::emit_update_event(id(), none(), data)
}
public(friend) fun update_admin(world: &mut World, admin: address) {
let field = world::get_mut_component<Field>(world, id());
let field = world::get_mut_comp<Field>(world, id());
let (_, fee) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public(friend) fun update_fee(world: &mut World, fee: u64) {
let field = world::get_mut_component<Field>(world, id());
let field = world::get_mut_comp<Field>(world, id());
let (admin, _) = decode(field.data);
field.data = encode(admin, fee);
world::emit_update_event(id(), none(), field.data)
}

public fun get(world: &World): (address,u64) {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
decode(data)
}

public fun get_admin(world: &World): address {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
let (admin, _) = decode(data);
admin
}

public fun get_fee(world: &World): u64 {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
let (_, fee) = decode(data);
fee
}
Expand Down
12 changes: 7 additions & 5 deletions examples/sources/codegen/components/single_value.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ module examples::single_value_comp {
// Systems
friend examples::example_system;

const NAME: vector<u8> = b"single_value";

public fun id(): address {
entity_key::from_bytes(b"Single_value Comp")
entity_key::from_bytes(NAME)
}

// value
Expand All @@ -23,21 +25,21 @@ module examples::single_value_comp {
}

public fun register(world: &mut World) {
world::add_component<Field>(
world::add_comp<Field>(
world,
id(),
NAME,
Field { data: encode(1000) }
);
}

public(friend) fun update(world: &mut World, value: u64) {
let data = encode(value);
world::get_mut_component<Field>(world, id()).data = data;
world::get_mut_comp<Field>(world, id()).data = data;
world::emit_update_event(id(), none(), data)
}

public fun get(world: &World): u64 {
let data = world::get_component<Field>(world, id()).data;
let data = world::get_comp<Field>(world, id()).data;
decode(data)
}

Expand Down
37 changes: 24 additions & 13 deletions examples/sources/codegen/eps/world.move
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
module examples::world {
use std::ascii::String;
use std::ascii::{String, string};
use std::option::Option;
use std::vector;
use examples::entity_key;
use sui::tx_context;
use sui::transfer;
use sui::event;
Expand Down Expand Up @@ -32,7 +34,9 @@ module examples::world {
/// Description of the world
description: String,
/// Components of the world
components: Bag,
comps: Bag,
/// Component names of the world
compnames: vector<String>,
/// admin of the world
admin: ID,
/// Components of the world
Expand Down Expand Up @@ -64,7 +68,8 @@ module examples::world {
id: object::new(ctx),
name,
description,
components: bag::new(ctx),
comps: bag::new(ctx),
compnames: vector::empty(),
admin: object::id(&admin),
version: VERSION
};
Expand All @@ -76,27 +81,33 @@ module examples::world {
(world.name, world.description, world.version)
}

public fun get_component<T : store>(world: &World, id: address): &T {
public fun compnames(world: &World): vector<String> {
world.compnames
}

public fun get_comp<T : store>(world: &World, id: address): &T {
assert!(world.version == VERSION, EWrongVersion);
assert!(bag::contains(&world.components, id), ECompDoesNotExist);
bag::borrow<address, T>(&world.components, id)
assert!(bag::contains(&world.comps, id), ECompDoesNotExist);
bag::borrow<address, T>(&world.comps, id)
}

public fun get_mut_component<T : store>(world: &mut World, id: address): &mut T {
public fun get_mut_comp<T : store>(world: &mut World, id: address): &mut T {
assert!(world.version == VERSION, EWrongVersion);
assert!(bag::contains(&world.components, id), ECompDoesNotExist);
bag::borrow_mut<address, T>(&mut world.components, id)
assert!(bag::contains(&world.comps, id), ECompDoesNotExist);
bag::borrow_mut<address, T>(&mut world.comps, id)
}

public fun add_component<T : store>(world: &mut World, id: address, component: T){
public fun add_comp<T : store>(world: &mut World, component_name: vector<u8>, component: T){
assert!(world.version == VERSION, EWrongVersion);
assert!(!bag::contains(&world.components, id), ECompAlreadyExists);
bag::add<address,T>(&mut world.components, id, component);
let id = entity_key::from_bytes(component_name);
assert!(!bag::contains(&world.comps, id), ECompAlreadyExists);
vector::push_back(&mut world.compnames, string(component_name));
bag::add<address,T>(&mut world.comps, id, component);
}

public fun contains(world: &mut World, id: address): bool {
assert!(world.version == VERSION, EWrongVersion);
bag::contains(&mut world.components, id)
bag::contains(&mut world.comps, id)
}

public fun emit_remove_event(comp: address, key: address) {
Expand Down
20 changes: 16 additions & 4 deletions examples/sources/system/example_system.move
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module examples::example_system {
use examples::single_value_comp;
use examples::world::World;
#[test_only]
use std::ascii::string;
#[test_only]
Expand All @@ -12,16 +14,18 @@ module examples::example_system {
#[test_only]
use examples::single_struct_comp;
#[test_only]
use examples::single_value_comp;
#[test_only]
use examples::world;
#[test_only]
use examples::world::World;
#[test_only]
use sui::test_scenario;
#[test_only]
use sui::test_scenario::Scenario;

public entry fun increase(world: &mut World) {
let old_number = single_value_comp::get(world);
let new_number = old_number + 10;
single_value_comp::update(world, new_number);
}

#[test_only]
public fun init_test(): Scenario {
let scenario_val = test_scenario::begin(@0x0001);
Expand All @@ -47,6 +51,14 @@ module examples::example_system {
assert!(description == string(b"Examples description"), 0);
assert!(version == 1, 0);

let names = world::compnames(&world);
assert!(names == vector[
string(b"single_column"),
string(b"multi_column"),
string(b"single_value"),
string(b"single_struct")
], 0);

test_scenario::return_shared<World>(world);
test_scenario::end(scenario_val);
}
Expand Down

0 comments on commit 995e51c

Please sign in to comment.