Skip to content

Commit

Permalink
Merge pull request #8 from kelnishi/unity-package
Browse files Browse the repository at this point in the history
Unity package setup
  • Loading branch information
kelnishi authored Nov 16, 2024
2 parents 5b85e0d + a5e17d4 commit 197bafd
Show file tree
Hide file tree
Showing 21 changed files with 522 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .run/Package Unity.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Package Unity" type="CompoundRunConfigurationType">
<toRun name="Wacs.Core Publish Wacs.Core.dll" type="ShConfigurationType" />
<toRun name="Wacs.Core Publish Wacs.WASIp1.dll" type="ShConfigurationType" />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/Wacs.Core Publish Wacs.Core.dll.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Wacs.Core Publish Wacs.Core.dll" type="ShConfigurationType" folderName="Unity Publish">
<option name="SCRIPT_TEXT" value="dotnet publish -c Release -o ../unity/Runtime/Plugins/" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/Wacs.Core" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/zsh" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
17 changes: 17 additions & 0 deletions .run/Wacs.Core Publish Wacs.WASIp1.dll.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Wacs.Core Publish Wacs.WASIp1.dll" type="ShConfigurationType" folderName="Unity Publish">
<option name="SCRIPT_TEXT" value="dotnet publish -c Release -o ../unity/Runtime/Plugins/" />
<option name="INDEPENDENT_SCRIPT_PATH" value="true" />
<option name="SCRIPT_PATH" value="" />
<option name="SCRIPT_OPTIONS" value="" />
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" />
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$/Wacs.WASIp1" />
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" />
<option name="INTERPRETER_PATH" value="/bin/zsh" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="EXECUTE_IN_TERMINAL" value="true" />
<option name="EXECUTE_SCRIPT_FILE" value="false" />
<envs />
<method v="2" />
</configuration>
</component>
102 changes: 102 additions & 0 deletions .run/sync_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/bin/bash

# --------------------------------------------
# Script to Sync .NET Assembly Version to package.json
# --------------------------------------------

# Exit immediately if a command exits with a non-zero status
set -e

# Function to display usage
usage() {
echo "Usage: $0 [path/to/project.csproj] [path/to/package.json]"
echo "If no arguments are provided, it searches for the first .csproj file and assumes package.json is in the current directory."
exit 1
}

# Check for help flag
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
usage
fi

# Assign arguments or set defaults
CSPROJ_FILE="${1:-}"
PACKAGE_JSON_FILE="${2:-package.json}"

# Function to find the first .csproj file if not provided
find_csproj() {
local csproj
csproj=$(find . -maxdepth 1 -name "*.csproj" | head -n 1)
echo "$csproj"
}

# Determine the .csproj file
if [[ -z "$CSPROJ_FILE" ]]; then
CSPROJ_FILE=$(find_csproj)
if [[ -z "$CSPROJ_FILE" ]]; then
echo "Error: No .csproj file found in the current directory."
exit 1
fi
fi

# Check if the .csproj file exists
if [[ ! -f "$CSPROJ_FILE" ]]; then
echo "Error: .csproj file '$CSPROJ_FILE' does not exist."
exit 1
fi

# Check if package.json exists
if [[ ! -f "$PACKAGE_JSON_FILE" ]]; then
echo "Error: package.json file '$PACKAGE_JSON_FILE' does not exist."
exit 1
fi

# Extract the version from the .csproj file using sed
# This sed command captures the content between <Version> and </Version>
VERSION=$(sed -n 's:.*<Version>\(.*\)</Version>.*:\1:p' "$CSPROJ_FILE")

# If VERSION is empty, try to extract from AssemblyInfo.cs
if [[ -z "$VERSION" ]]; then
# Assuming AssemblyInfo.cs is located in Properties directory
ASSEMBLY_INFO=$(find . -path "*/Properties/AssemblyInfo.cs" | head -n 1)
if [[ -n "$ASSEMBLY_INFO" ]]; then
VERSION=$(sed -n 's/.*AssemblyVersion("\([^"]*\)").*/\1/p' "$ASSEMBLY_INFO")
fi
fi

# If VERSION is still empty, exit with error
if [[ -z "$VERSION" ]]; then
echo "Error: Could not find the version in $CSPROJ_FILE or AssemblyInfo.cs."
exit 1
fi

echo "Detected .NET project version: $VERSION"

# Function to update package.json using jq
update_package_json_jq() {
local ver="$1"
local pkg="$2"
jq --arg ver "$ver" '.version = $ver' "$pkg" > "${pkg}.tmp" && mv "${pkg}.tmp" "$pkg"
}

# Function to update package.json using sed
update_package_json_sed() {
local ver="$1"
local pkg="$2"
# This regex matches the "version": "x.y.z" pattern and replaces it
sed -i.bak -E 's/("version"\s*:\s*")[^"]+(")/\1'"$ver"'\2/' "$pkg"
}

# Update package.json
if command -v jq > /dev/null 2>&1; then
echo "Updating package.json using jq..."
update_package_json_jq "$VERSION" "$PACKAGE_JSON_FILE"
echo "Successfully updated 'version' in $PACKAGE_JSON_FILE to $VERSION."
else
echo "jq is not installed. Falling back to sed."
update_package_json_sed "$VERSION" "$PACKAGE_JSON_FILE"
echo "Successfully updated 'version' in $PACKAGE_JSON_FILE to $VERSION."
echo "A backup of the original package.json is saved as package.json.bak."
fi

exit 0
7 changes: 7 additions & 0 deletions global.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"sdk": {
"version": "6.0.0",
"rollForward": "latestMajor",
"allowPrerelease": false
}
}
10 changes: 10 additions & 0 deletions unity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Changelog

All notable changes to the unity package will be documented in this file.

## [0.1.4]
### Added
- Initial project setup for Unity.

### Changed
- Updated project structure to allow installation as a Unity package from git.
7 changes: 7 additions & 0 deletions unity/Documentation~/manual.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## Documentation...

I build and maintain WACS as a solo dev.

I hope someday I'll have time for some great documentation.

Until then, you can browse the [github repo](https://github.com/kelnishi/WACS/discussions) and connect with me there.
22 changes: 22 additions & 0 deletions unity/Editor/AssetImporters/WasmImporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using UnityEngine;
using UnityEditor;
using UnityEditor.AssetImporters;
using System.IO;

[ScriptedImporter(1, "wasm")]
public class WasmImporter : ScriptedImporter
{
public override void OnImportAsset(AssetImportContext ctx)
{
// Load the binary data from the .kelvin file
byte[] data = File.ReadAllBytes(ctx.assetPath);

// Create an instance of KelvinAsset and assign the data
WasmAsset wasmAsset = ScriptableObject.CreateInstance<WasmAsset>();
wasmAsset.data = data;

// Add the asset to the import context
ctx.AddObjectToAsset("Wasm Asset", wasmAsset);
ctx.SetMainObject(wasmAsset);
}
}
55 changes: 55 additions & 0 deletions unity/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# WACS - WebAssembly Interpreter

**WACS** is a pure C# WebAssembly Interpreter designed for .NET environments, including Unity's IL2CPP.

## Features

- **Execute WebAssembly**: Load and run .wasm files or data streams.
- **Interpreter-Only**: No JIT compilation, works in AOT modes like IL2CPP on iOS.
- **Magic Interop**: Reflection based interop allows for easy function binding with little boilerplate code.
- **Open Source**: Library is fully open source, free to inspect, change, and improve.

## Unity Installation

- Window>Package Manager
- Click + Add package from git URL...
- Enter the repo URL: ```https://github.com/kelnishi/WACS```
- Click Add

## Usage

To use Wacs, you'll need to instantiate a runtime and a module.

```csharp
using Wacs.Core;
using Wacs.Core.Runtime;
using Wacs.Core.Runtime.Types;

public class ExampleClass : MonoBehaviour
{
[SerializeField] private WasmAsset wasmAsset;
public string moduleName = "_";

void Start()
{
var stream = new MemoryStream(wasmAsset.data);
var module = BinaryModuleParser.ParseWasm(stream);

_runtime = new WasmRuntime();
_output = new();
_runtime.BindHostFunction<Action<char>>(("env", "sayc"), c =>
{
_output.Append(c);
});

_moduleInstance = _runtime.InstantiateModule(module, new RuntimeOptions { SkipModuleValidation = true});
_runtime.RegisterModule(moduleName, _moduleInstance);

var fa = runtime.GetExportedFunction((moduleName, "main"));
var main = runtime.CreateInvoker<Func<int>>(fa);

//Execute the module
int result = main();
}
}
```
Binary file added unity/Runtime/Plugins/FluentValidation.dll
Binary file not shown.
Binary file not shown.
58 changes: 58 additions & 0 deletions unity/Runtime/Plugins/Wacs.Core.deps.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"runtimeTarget": {
"name": ".NETStandard,Version=v2.1/",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETStandard,Version=v2.1": {},
".NETStandard,Version=v2.1/": {
"Wacs.Core/0.1.4": {
"dependencies": {
"FluentValidation": "11.10.0",
"Microsoft.Extensions.ObjectPool": "9.0.0"
},
"runtime": {
"Wacs.Core.dll": {}
}
},
"FluentValidation/11.10.0": {
"runtime": {
"lib/netstandard2.1/FluentValidation.dll": {
"assemblyVersion": "11.0.0.0",
"fileVersion": "11.10.0.0"
}
}
},
"Microsoft.Extensions.ObjectPool/9.0.0": {
"runtime": {
"lib/netstandard2.0/Microsoft.Extensions.ObjectPool.dll": {
"assemblyVersion": "9.0.0.0",
"fileVersion": "9.0.24.52903"
}
}
}
}
},
"libraries": {
"Wacs.Core/0.1.4": {
"type": "project",
"serviceable": false,
"sha512": ""
},
"FluentValidation/11.10.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-qsJGSJDdZ8qiG+lVJ70PZfJHcEdq8UQZ/tZDXoj78/iHKG6lVKtMJsD11zyyv/IPc7rwqGqnFoFLTNzpo3IPYg==",
"path": "fluentvalidation/11.10.0",
"hashPath": "fluentvalidation.11.10.0.nupkg.sha512"
},
"Microsoft.Extensions.ObjectPool/9.0.0": {
"type": "package",
"serviceable": true,
"sha512": "sha512-UbsU/gYe4nv1DeqMXIVzDfNNek7Sk2kKuAOXL/Y+sLcAR0HwFUqzg1EPiU88jeHNe0g81aPvvHbvHarQr3r9IA==",
"path": "microsoft.extensions.objectpool/9.0.0",
"hashPath": "microsoft.extensions.objectpool.9.0.0.nupkg.sha512"
}
}
}
Binary file added unity/Runtime/Plugins/Wacs.Core.dll
Binary file not shown.
Binary file added unity/Runtime/Plugins/Wacs.Core.pdb
Binary file not shown.
Loading

0 comments on commit 197bafd

Please sign in to comment.