Skip to content

Commit

Permalink
Adds some funcs to Slot
Browse files Browse the repository at this point in the history
children_count, get_child, find_child_by_name, find_child_by_tag
  • Loading branch information
RobertBaruch committed Jan 9, 2024
1 parent 3a145c7 commit 75141bc
Show file tree
Hide file tree
Showing 15 changed files with 457 additions and 135 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Dergwasm includes a WASM binary, `firmware.wasm`, which implements a [MicroPytho
6:38:54 PM.614 ( -1 FPS) [INFO] [ResoniteModLoader/Dergwasm] Dergwasm patches applied
```

6. Copy this URL, and paste it into your world in Resonite: `resrec:///U-Xekri/R-a6ddab70-db2d-41cf-be0e-4f7a356afe69`. This is a Dergwasm slot hierarchy, already set up.
6. Copy this URL, and paste it into your world in Resonite: `resrec:///U-Xekri/R-c67b56f1-8b83-4108-8ac7-0846ed8ce8b9`. This is a Dergwasm slot hierarchy, already set up.
![The Dergwasm slot](dergwasm_slot.jpg)
7. Re-initialize Dergwasm by using the ProtofluxTool to create a Dynamic Impulse Trigger node. Set its `Tag` to `_dergwasm_init` and its `TargetHierarchy` to the top-level Dergwasm slot. Now trigger the node. There will be a slight hitch as the WASM file is read in and parsed.

Expand Down
1 change: 1 addition & 0 deletions dergwasm_mod/Dergwasm/Dergwasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static void Init(World world, string filename)
if (consoleSlot != null)
consoleSlot.GetComponent<FrooxEngine.UIX.Text>().Content.Value = "";

Msg($"Dergwasm v{typeof(Dergwasm).Assembly.GetName().Version}");
Msg("Init called");
machine = new Machine();
// machine.Debug = true;
Expand Down
112 changes: 56 additions & 56 deletions dergwasm_mod/Dergwasm/Dergwasm.csproj
Original file line number Diff line number Diff line change
@@ -1,64 +1,64 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>0.1.1.0</Version>
</PropertyGroup>
<PropertyGroup>
<TargetFramework>net472</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Version>0.1.3.0</Version>
</PropertyGroup>

<PropertyGroup>
<ResonitePath>$(MSBuildThisFileDirectory)Resonite</ResonitePath>
<ResonitePath Condition="Exists('C:\Program Files (x86)\Steam\steamapps\common\Resonite\')">C:\Program Files (x86)\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('D:\Program Files (x86)\Steam\steamapps\common\Resonite\')">D:\Program Files (x86)\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('C:\SteamLibrary\steamapps\common\Resonite\')">C:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('D:\SteamLibrary\steamapps\common\Resonite\')">D:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('$(HOME)/.steam/steam/steamapps/common/Resonite/')">$(HOME)/.steam/steam/steamapps/common/Resonite/</ResonitePath>
<ResonitePath Condition="Exists('E:\Programs\Steam\steamapps\common\Resonite')">E:\Programs\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('F:\Steam\steamapps\common\Resonite')">F:\Steam\steamapps\common\Resonite\</ResonitePath>
<ResoniteCoreLibs>$(ResonitePath)\Resonite_Data\Managed</ResoniteCoreLibs>
<ResoniteNmlLibs>$(ResonitePath)\rml_libs</ResoniteNmlLibs>
<ResoniteNmlMods>$(ResonitePath)\rml_mods</ResoniteNmlMods>
<ResoniteLibs>$(ResonitePath)\Libraries</ResoniteLibs>
</PropertyGroup>
<PropertyGroup>
<ResonitePath>$(MSBuildThisFileDirectory)Resonite</ResonitePath>
<ResonitePath Condition="Exists('C:\Program Files (x86)\Steam\steamapps\common\Resonite\')">C:\Program Files (x86)\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('D:\Program Files (x86)\Steam\steamapps\common\Resonite\')">D:\Program Files (x86)\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('C:\SteamLibrary\steamapps\common\Resonite\')">C:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('D:\SteamLibrary\steamapps\common\Resonite\')">D:\SteamLibrary\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('$(HOME)/.steam/steam/steamapps/common/Resonite/')">$(HOME)/.steam/steam/steamapps/common/Resonite/</ResonitePath>
<ResonitePath Condition="Exists('E:\Programs\Steam\steamapps\common\Resonite')">E:\Programs\Steam\steamapps\common\Resonite\</ResonitePath>
<ResonitePath Condition="Exists('F:\Steam\steamapps\common\Resonite')">F:\Steam\steamapps\common\Resonite\</ResonitePath>
<ResoniteCoreLibs>$(ResonitePath)\Resonite_Data\Managed</ResoniteCoreLibs>
<ResoniteNmlLibs>$(ResonitePath)\rml_libs</ResoniteNmlLibs>
<ResoniteNmlMods>$(ResonitePath)\rml_mods</ResoniteNmlMods>
<ResoniteLibs>$(ResonitePath)\Libraries</ResoniteLibs>
</PropertyGroup>

<ItemGroup>
<Reference Include="0Harmony">
<HintPath>$(ResoniteNmlLibs)\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Elements.Assets">
<HintPath>$(ResoniteCoreLibs)\Elements.Assets.dll</HintPath>
</Reference>
<Reference Include="Elements.Core">
<HintPath>$(ResoniteCoreLibs)\Elements.Core.dll</HintPath>
</Reference>
<Reference Include="FrooxEngine">
<HintPath>$(ResoniteCoreLibs)\FrooxEngine.dll</HintPath>
</Reference>
<Reference Include="ProtoFlux.Core">
<HintPath>$(ResoniteCoreLibs)\ProtoFlux.Core.dll</HintPath>
</Reference>
<Reference Include="ProtoFlux.Nodes.FrooxEngine">
<HintPath>$(ResoniteCoreLibs)\ProtoFlux.Nodes.FrooxEngine.dll</HintPath>
</Reference>
<Reference Include="ResoniteModLoader">
<HintPath>$(ResoniteLibs)\ResoniteModLoader.dll</HintPath>
</Reference>
<Reference Include="SkyFrost.Base">
<HintPath>$(ResoniteCoreLibs)\SkyFrost.Base.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Reference Include="0Harmony">
<HintPath>$(ResoniteNmlLibs)\0Harmony.dll</HintPath>
</Reference>
<Reference Include="Elements.Assets">
<HintPath>$(ResoniteCoreLibs)\Elements.Assets.dll</HintPath>
</Reference>
<Reference Include="Elements.Core">
<HintPath>$(ResoniteCoreLibs)\Elements.Core.dll</HintPath>
</Reference>
<Reference Include="FrooxEngine">
<HintPath>$(ResoniteCoreLibs)\FrooxEngine.dll</HintPath>
</Reference>
<Reference Include="ProtoFlux.Core">
<HintPath>$(ResoniteCoreLibs)\ProtoFlux.Core.dll</HintPath>
</Reference>
<Reference Include="ProtoFlux.Nodes.FrooxEngine">
<HintPath>$(ResoniteCoreLibs)\ProtoFlux.Nodes.FrooxEngine.dll</HintPath>
</Reference>
<Reference Include="ResoniteModLoader">
<HintPath>$(ResoniteLibs)\ResoniteModLoader.dll</HintPath>
</Reference>
<Reference Include="SkyFrost.Base">
<HintPath>$(ResoniteCoreLibs)\SkyFrost.Base.dll</HintPath>
</Reference>
</ItemGroup>

<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="System.Memory" Version="4.5.5" />
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
<PackageReference Include="System.Threading.Tasks.Extensions" Version="4.5.4" />
<PackageReference Include="System.ValueTuple" Version="4.5.0" />
</ItemGroup>

<Target Name="CopyPluginLibrary" AfterTargets="PostBuildEvent"
Condition="Exists($(ResoniteNmlMods))">
<Exec
Command="copy &quot;$(TargetDir)\$(TargetName).dll&quot; &quot;$(ResoniteNmlMods)\&quot;" />
</Target>
<Target Name="CopyPluginLibrary" AfterTargets="PostBuildEvent"
Condition="Exists($(ResoniteNmlMods))">
<Exec
Command="copy &quot;$(TargetDir)\$(TargetName).dll&quot; &quot;$(ResoniteNmlMods)\&quot;" />
</Target>

</Project>
73 changes: 56 additions & 17 deletions dergwasm_mod/Dergwasm/ResoniteEnv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,23 @@ public void RegisterHostFuncs()
machine.RegisterVoidHostFunc<ulong, int>("env", "slot__set_name", slot__set_name);
machine.RegisterReturningHostFunc<ulong, int>(
"env",
"value_field__bool__get_value",
value_field__bool__get_value
"slot__get_num_children",
slot__get_num_children
);
machine.RegisterVoidHostFunc<ulong, int>(
machine.RegisterReturningHostFunc<ulong, int, ulong>(
"env",
"slot__get_child",
slot__get_child
);
machine.RegisterReturningHostFunc<ulong, int, int, int, int, ulong>(
"env",
"slot__find_child_by_name",
slot__find_child_by_name
);
machine.RegisterReturningHostFunc<ulong, int, int, ulong>(
"env",
"value_field__bool__set_value",
value_field__bool__set_value
"slot__find_child_by_tag",
slot__find_child_by_tag
);
}

Expand Down Expand Up @@ -241,22 +251,51 @@ public void slot__set_name(Frame frame, ulong slot_id, int ptr)
slot.Name = emscriptenEnv.GetUTF8StringFromMem(ptr);
}

public void value_field__bool__set_value(Frame frame, ulong value_field_id, int value)
public int slot__get_num_children(Frame frame, ulong slot_id)
{
ValueField<bool> component =
world.ReferenceController.GetObjectOrNull(new RefID(value_field_id))
as ValueField<bool>;
if (component == null)
return;
component.Value.Value = value != 0;
Slot slot = SlotFromRefID(slot_id);
return slot?.ChildrenCount ?? 0;
}

public ulong slot__get_child(Frame frame, ulong slot_id, int index)
{
Slot slot = SlotFromRefID(slot_id);
return ((ulong?)slot?[index]?.ReferenceID) ?? 0;
}

public ulong slot__find_child_by_name(
Frame frame,
ulong slot_id,
int namePtr,
int match_substring,
int ignore_case,
int max_depth
)
{
Slot slot = SlotFromRefID(slot_id);
string name = emscriptenEnv.GetUTF8StringFromMem(namePtr);
return (
(ulong?)
slot?.FindChild(
name,
match_substring != 0,
ignore_case != 0,
max_depth
)?.ReferenceID
) ?? 0;
}

public int value_field__bool__get_value(Frame frame, ulong value_field_id)
public ulong slot__find_child_by_tag(Frame frame, ulong slot_id, int tagPtr, int max_depth)
{
ValueField<bool> component =
world.ReferenceController.GetObjectOrNull(new RefID(value_field_id))
as ValueField<bool>;
return (component?.Value?.Value ?? false) ? 1 : 0;
Slot slot = SlotFromRefID(slot_id);
string tag = emscriptenEnv.GetUTF8StringFromMem(tagPtr);
return (
(ulong?)
slot?.FindChild(
(Predicate<Slot>)(s => s.Tag == tag),
max_depth
)?.ReferenceID
) ?? 0;
}
}
}
Binary file modified firmware.wasm
Binary file not shown.
3 changes: 2 additions & 1 deletion usercmodule/resonite/micropython.mk
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
USERMODULES_DIR := $(USERMOD_DIR)

# Add all C files to SRC_USERMOD.
# SRC_USERMOD += $(USERMODULES_DIR)/resonite.c
SRC_USERMOD += $(USERMODULES_DIR)/mp_resonite.c
SRC_USERMOD += $(USERMODULES_DIR)/mp_resonite_slot_impl.c
SRC_USERMOD += $(USERMODULES_DIR)/mp_resonite_slot.c
SRC_USERMOD += $(USERMODULES_DIR)/mp_resonite_utils.c

CFLAGS_USERMOD += -I$(USERMODULES_DIR)

Expand Down
26 changes: 26 additions & 0 deletions usercmodule/resonite/mp_resonite.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "py/obj.h"
#include "py/runtime.h"

#include "mp_resonite_slot.h"

// Define all attributes of the module.
// Table entries are key/value pairs of the attribute name (a string)
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
STATIC const mp_rom_map_elem_t resonite_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_resonite) },
{ MP_ROM_QSTR(MP_QSTR_Slot), MP_ROM_PTR(&resonite_Slot_type) },
};
// Create a const dict named resonite_module_globals, with content resonite_module_globals_table.
STATIC MP_DEFINE_CONST_DICT(resonite_module_globals, resonite_module_globals_table);


// Define module object.
const mp_obj_module_t resonite_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&resonite_module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_resonite, resonite_user_cmodule);
30 changes: 8 additions & 22 deletions usercmodule/resonite/mp_resonite_slot.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(resonite_Slot_get_parent_obj, resonite_Slot_get
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(resonite_Slot_get_object_root_obj, 1, resonite_Slot_get_object_root);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(resonite_Slot_get_name_obj, resonite_Slot_get_name);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(resonite_Slot_set_name_obj, resonite_Slot_set_name);
STATIC MP_DEFINE_CONST_FUN_OBJ_1(resonite_Slot_children_count_obj, resonite_Slot_children_count);
STATIC MP_DEFINE_CONST_FUN_OBJ_2(resonite_Slot_get_child_obj, resonite_Slot_get_child);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(resonite_Slot_find_child_by_name_obj, 2, resonite_Slot_find_child_by_name);
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(resonite_Slot_find_child_by_tag_obj, 2, resonite_Slot_find_child_by_tag);

// This collects all methods and other static class attributes of Slot.
// The table structure is similar to the module table, as detailed below.
Expand All @@ -20,6 +24,10 @@ STATIC const mp_rom_map_elem_t resonite_Slot_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_get_object_root), MP_ROM_PTR(&resonite_Slot_get_object_root_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_name), MP_ROM_PTR(&resonite_Slot_get_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_set_name), MP_ROM_PTR(&resonite_Slot_set_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_children_count), MP_ROM_PTR(&resonite_Slot_children_count_obj) },
{ MP_ROM_QSTR(MP_QSTR_get_child), MP_ROM_PTR(&resonite_Slot_get_child_obj) },
{ MP_ROM_QSTR(MP_QSTR_find_child_by_name), MP_ROM_PTR(&resonite_Slot_find_child_by_name_obj) },
{ MP_ROM_QSTR(MP_QSTR_find_child_by_tag), MP_ROM_PTR(&resonite_Slot_find_child_by_tag_obj) },
};
// Create a const dict named resonite_Slot_locals_dict, with content
// resonite_Slot_locals_dict_table.
Expand All @@ -34,25 +42,3 @@ MP_DEFINE_CONST_OBJ_TYPE(
print, resonite_Slot_print,
locals_dict, &resonite_Slot_locals_dict
);

// Define all attributes of the module.
// Table entries are key/value pairs of the attribute name (a string)
// and the MicroPython object reference.
// All identifiers and strings are written as MP_QSTR_xxx and will be
// optimized to word-sized integers by the build system (interned strings).
STATIC const mp_rom_map_elem_t resonite_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_resonite) },
{ MP_ROM_QSTR(MP_QSTR_Slot), MP_ROM_PTR(&resonite_Slot_type) },
};
// Create a const dict named resonite_module_globals, with content resonite_module_globals_table.
STATIC MP_DEFINE_CONST_DICT(resonite_module_globals, resonite_module_globals_table);


// Define module object.
const mp_obj_module_t resonite_user_cmodule = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t *)&resonite_module_globals,
};

// Register the module to make it available in Python.
MP_REGISTER_MODULE(MP_QSTR_resonite, resonite_user_cmodule);
16 changes: 11 additions & 5 deletions usercmodule/resonite/mp_resonite_slot.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,22 @@ typedef struct _resonite_Slot_obj_t {

extern const mp_obj_type_t resonite_Slot_type;

// Utility function to create a Slot object from a reference ID.
extern mp_obj_t resonite_create_slot(resonite_slot_refid_t reference_id);
// Creates a Slot object from a reference ID.
extern mp_obj_t resonite_new_Slot(resonite_slot_refid_t reference_id);
// Creates a Slot object from a reference ID, or None if the reference ID is 0.
extern mp_obj_t resonite_new_Slot_or_none(resonite_slot_refid_t reference_id);

// This represents Slot.__init__.
extern mp_obj_t resonite_Slot_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
extern void resonite_Slot_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind);
extern mp_obj_t resonite_Slot_make_new(const mp_obj_type_t* type, size_t n_args, size_t n_kw, const mp_obj_t* args);
extern void resonite_Slot_print(const mp_print_t* print, mp_obj_t self_in, mp_print_kind_t kind);
extern mp_obj_t resonite_Slot_root_slot(mp_obj_t cls_in);
extern mp_obj_t resonite_Slot_get_parent(mp_obj_t self_in);
extern mp_obj_t resonite_Slot_get_object_root(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
extern mp_obj_t resonite_Slot_get_object_root(size_t n_args, const mp_obj_t* pos_args, mp_map_t* kw_args);
extern mp_obj_t resonite_Slot_get_name(mp_obj_t self_in);
extern mp_obj_t resonite_Slot_set_name(mp_obj_t self_in, mp_obj_t name);
extern mp_obj_t resonite_Slot_children_count(mp_obj_t self_in);
extern mp_obj_t resonite_Slot_get_child(mp_obj_t self_in, mp_obj_t index);
extern mp_obj_t resonite_Slot_find_child_by_name(size_t n_args, const mp_obj_t* pos_args, mp_map_t* kw_args);
extern mp_obj_t resonite_Slot_find_child_by_tag(size_t n_args, const mp_obj_t* pos_args, mp_map_t* kw_args);

#endif // __RESONITE_RESONITE_SLOT_H__
Loading

0 comments on commit 75141bc

Please sign in to comment.