Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

klopsquark/MonoScript

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MonoScript

MonoScript is a library for .NET that allows you to use C# as if it were a scripting language like Lua, Squirrel, AngelScript, etc.

It achieves this by embedding the Mono C# compiler (MCS), and providing an API to work with it.

MonoScript is designed with sandboxing in mind, in order to make it more approachable for usage within games (its intended use-case). As a result, you have complete control over what types and namespaces are made accessible to scripts.

Compiling

In order to compile MonoScript, you will first need to compile jay. It should be as simple as building the included Premake project, refer to the Premake documentation for information on how to do so.

Once jay has been compiled, simply open the MonoScript solution and build it.

Note on VS2017 + Windows 10:

  1. Install Windows 8.1 SDK and UCRT SDK (installing only Windows 8.1 SDK may cause errors like: "xyz.h": No such file or directory
  2. Execute premake5 vs2017 in repo root folder (it will execute the script premake5.lua by default)
  3. Open and compile jay.sln
  4. Open and compile MonoScript.sln
  5. Run demo application
  6. Happy coding...

Usage

Using MonoScript is very simple.

  1. Create an instance of the ScriptBuilder class and call Start() on it.
  2. Import any types, references and namespaces you wish to make accessible to scripts.
  3. Build the module with var success = builder.Build(out var module);
  4. Use module.GetTypes() to retrieve the script types.
using System;

namespace MonoScript.Sample
{
    public struct MyStruct
    {
        public string Message;

        public override string ToString()
        {
            return Message;
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var builder = new ScriptBuilder();
            builder.Start();

            builder.ImportType(typeof(Console));
            builder.ImportType<MyStruct>();
            
            builder.AddFromString("Example.cs", @"
                using System;
                using MonoScript.Sample;
                
                public class MyClass
                {
                    public MyClass()
                    {
                        var myStruct = new MyStruct
                        {
                            Message = ""Hello, world!""
                        };
                
                        Console.WriteLine(myStruct);
                    }
                }");

            if (!builder.Build(out var module))
                Console.WriteLine("Failed. {0} error(s). {1} warning(s).", module.ErrorCount, module.WarningCount);
            else
            {
                Console.WriteLine("Succeeded. {0} warning(s).", module.WarningCount);

                foreach (var type in module.GetTypes())
                    Activator.CreateInstance(type);
            }
            
            Console.WriteLine("Press any key to exit . . .");
            Console.ReadKey();
        }
    }
}

Unity

MonoScript has been tested under and confirmed to work with the experimental .NET 4.6 target for Unity projects found in Unity 2017.1 and above.

License

MonoScript is licensed under the MIT license.

About

C# Scripting via MCS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 95.3%
  • C 4.6%
  • Other 0.1%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.