Magnet 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Magnet --version 1.0.1                
NuGet\Install-Package Magnet -Version 1.0.1                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Magnet" Version="1.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Magnet --version 1.0.1                
#r "nuget: Magnet, 1.0.1"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Magnet as a Cake Addin
#addin nuget:?package=Magnet&version=1.0.1

// Install Magnet as a Cake Tool
#tool nuget:?package=Magnet&version=1.0.1                

What is Magnet?


Magnet�ǻ��ڡ�Microsoft.CodeAnalysis.CSharp.Scripting������һ�������ܵ�c#��Ϸ�ű�����

Magnet is based on "Microsoft.CodeAnalysis.CSharp.Scripting" to develop a high performance c # game Script engine

��c#���Ժ�NET����£��ű����а�ȫ���ɿء���״̬�õ��ص�

On the basis of C# language and.NET framework, the script is safe, controllable, flexible and state

Features|����

  • Load from file | ���ļ�������ؽű�
  • Rewrite Type | �ṩ������д�����滻�ű���ʹ�õ�����
  • Unload | �ű�����ж��
  • Disable Namespace | ���������ռ�
  • Script state isolation | �ű�״̬����
  • Script dependency injection | �ű�����ע��
  • Debugger and braek; | ���ԺͶϵ�
  • Output assembly | �������
  • References assembly | �������ó���
  • Generation method delegate | ���ɷ���ί��
  • Illegal API detection | �Ƿ�API���
  • Global variable | ȫ�ֱ���
  • Expandability | ����չ��
  • Script inter call | �ű�֮�����
  • Custom Analysis | �Զ����������չ

Examples|����

    private static ScriptOptions Options(String name)
    {
        ScriptOptions options = ScriptOptions.Default;
        options.WithName(name);
        options.WithOutPutFile("123.dll");
        options.WithDebug(false);

        //options.WithRelease();
        options.WithAllowAsync(true);
        options.WithDirectory("../../../../Scripts");
        options.WithPreprocessorSymbols("USE_FILE");

        options.AddReferences<GameScript>();

        var timerProvider = new TimerProvider();
        options.AddAnalyzer(timerProvider);
        options.RegisterProvider(timerProvider);

        options.DisableNamespace(typeof(Thread));

        // Insecure
        //options.DisabledInsecureTypes();

        options.WithTypeRewriter(new TypeRewriter());
        options.UseDefaultSuppressDiagnostics();
        options.WithAssemblyLoadCallback(AssemblyLoad);
        options.RegisterProvider<ObjectKilledContext>(new ObjectKilledContext());
        options.RegisterProvider(GLOBAL);
        options.RegisterProvider<IObjectContext>(new HumContext(), "SELF");

        return options;
    }




    private static WeakReference<Action> TestSccriptUnload()
    {
        MagnetScript scriptManager = new MagnetScript(Options("Unload.Test"));
        var result = scriptManager.Compile();
        if (!result.Success)
        {
            foreach (var item in result.Diagnostics)
            {
                Console.WriteLine(item.ToString());
            }
            return null;
        }
        var state = scriptManager.CreateState();
        var weak = state.MethodDelegate<Action>("ScriptExample", "Hello");
        state.Dispose();
        scriptManager.Unload();
        return weak;
    }

    public static void Main()
    {
        MagnetScript scriptManager = new MagnetScript(Options("My.Script"));
        scriptManager.Unloading += ScriptManager_Unloading;
        scriptManager.Unloaded += ScriptManager_Unloaded;

        var result = scriptManager.Compile();
        foreach (var diagnostic in result.Diagnostics)
        {
            Console.WriteLine(diagnostic.ToString());
        }
        if (result.Success)
        {
            var stateOptions = StateOptions.Default;
            stateOptions.RegisterProvider(new TimerService());
            var stateTest = scriptManager.CreateState(stateOptions);
            var weakMain = stateTest.MethodDelegate<Action>("ScriptA", "Main");
            if (weakMain != null && weakMain.TryGetTarget(out var main))
            {
                using (new WatchTimer("With Call Main()")) main();
                main = null;
            }

            var weakPlayerLife = stateTest.ScriptAs<IPlayLifeEvent>();
            if (weakPlayerLife != null && weakPlayerLife.TryGetTarget(out var lifeEvent))
            {
                using (new WatchTimer("With Call OnOnline()")) lifeEvent.OnOnline(null);
                lifeEvent = null;
            }
            stateTest = null;
            scriptManager.Unload(true);
        }
        // wait gc unloaded assembly
        while (scriptManager.Status == ScrriptStatus.Unloading && scriptManager.IsAlive)
        {
            var obj = new byte[1024 * 1024];
            Thread.Sleep(10);
        }
    }

    private static void ScriptManager_Unloaded(MagnetScript obj)
    {
        Console.WriteLine($"�ű�[{obj.Name}:{obj.UniqueId}]ж�����.");
    }

    private static void ScriptManager_Unloading(MagnetScript obj)
    {
        Console.WriteLine($"�ű�[{obj.Name}:{obj.UniqueId}]ж������.");
    }

Script Examples|�ű�����

    using Magnet.Core;
    using System;



    // A usable script must meet three requirements.
    // 1. The access must be public
    // 2. The [ScriptAttribute] must be marked
    // 3. The AbstractScript class must be inherited

    [Script(nameof(ScriptExample))]
    public class ScriptExample : AbstractScript
    {
        [Autowired("SELF")]
        protected readonly IObjectContext? SELF;

        [Autowired]
        protected readonly GlobalVariableStore? GLOBAL;

        [Function("Hello")]
        public void Hello()
        {
            this.PRINT($"Hello Wrold!");

            // call script method
            Call("ScriptB", "Test", []);
            Call("ScriptB", "PrintMessage", ["Help"]);
            TryCall("ScriptB", "PrintMessage1", ["Help"]);
            Script<ScriptB>().PrintMessage("AAA");
            Script<ScriptB>((script) =>
            {
                script.PrintMessage("BBB");
            });

        }



        public Double Target
        {
            get
            {
                return 3.14;
            }
            set
            {
                this.PRINT(value);
            }
        }
    }
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.6 78 10/30/2024
1.0.5 88 10/26/2024
1.0.4 85 10/25/2024
1.0.3 80 10/25/2024
1.0.2 89 10/24/2024
1.0.1 82 10/24/2024
1.0.0 82 10/24/2024