Git Product home page Git Product logo

bepinex.scriptloader's Introduction

ScriptLoader for BepInEx 5.4

This is a BepInEx 5 plugin that allows you to run C# script files without compiling them to a DLL.

This plugin uses a modified version of the Mono Compiler Service (mcs) that allows to use most of C# 7 features.
The compiler relies on System.Reflection.Emit being present! As such, Unity games using .NET Standard API (i.e. has netstandard.dll in the Managed folder) will not be able to run this plugin!

Now scripts ignore visibility checks! Thanks to custom MCS, you can now access private members (methods, fields) via scripts!

Why

Because sometimes I'm lazy to open Visual Studio and write proper plugins.

Installation

Download the latest plugin version from releases and place it into BepInEx/plugin folder.

Writing and installing scripts

To install scripts, create a scripts folder in the game's root folder and place raw .cs files (C# source code) into it.
To remove scripts, remove them from the scripts folder (or change file extension to .cs.off).

ScriptLoader will automatically load and compile all C# source code files it finds in the folder.
ScriptLoader will also automatically run any static void Main() methods it finds.

Example script:

using UnityEngine;

public static class MyScript {
    public static void Main() {
        Debug.Log("Hello, world!");
    }

    public static void Unload() {
        // Unload and unpatch everything before reloading the script
    }
}

Reloading scripts

ScriptLoader automatically detects changes in the scripts and reloads them.

In order to make your script reloadable, you must implement static void Unload() method that cleans up any used resources.
This is done because of Mono's limitation: you cannot actually unload any already loaded assemblies in Mono. Because of that, you should clean up your script so that the newly compiled script will not interfere with your game!

Specifying metadata

You can specify metadata at the very start of your script by using the following format:

// #name Short name of the script
// #author ghorsington
// #desc A longer description of the script. This still should be a one-liner.
// #ref ${Managed}/UnityEngine.UI.dll
// #ref ${BepInExRoot}/core/MyDependency.dll
// #proc_filter Game.exe

using UnityEngine;
...

The proc_filter tag acts like BepinProcess attribute in BepInEx: it allows you to specify which processes to run the script on.

The ref tag is special: ScriptLoader will automatically load any assemblies specified with the tag.
The path is relative to the scripts folder, but you can use ${Folder} to reference some special folders.

Currently the following special folders are available:

  • Managed -- path to the game's Managed folder with all the main DLLs
  • Scripts -- path to the scripts folder
  • BepInExRoot -- path to the BepInEx folder

WIP: At the moment, all tags but ref have no effect. A GUI is planned to allow to disable any scripts.

Compilation errors

At this moment the compilation errors are simply written to the BepInEx console.

Upgrading to 1.2.4.0

Starting 1.2.4.0, you might see the following error when loading a script:

Skipping loading `...` because it references outdated HarmonyWrapper and BepInEx.Harmony. To fix this, refer to github.com/denikson/BepInEx.ScriptLoader#upgrading-to-1240.

This error happens when using older ScriptLoader scripts with ScriptLoader 1.2.4.0 or newer.

In most cases, you can fix the script yourself. Open the script specified in the error into Notepad or some other text editor and do the following changes:

  • Remove using BepInEx.Harmony; line
  • Replace HarmonyWrapper.PatchAll with Harmony.CreateAndPatchAll

Then try to run the game again. If the error persists or you get some other error, the script is too complex to fix by this guide. In that case please conact the developer of the script and ask them to fix it.

bepinex.scriptloader's People

Contributors

ghorsington avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

bepinex.scriptloader's Issues

error CS0121

i have never learn about c#,dont know the c# grammar or the plugins principle ,but the same function name conflict when the script load .
this is the error message ,
[Info:C# Script Loader] more_mouse_sensitivity.cs(42,28): error CS0121: The call is ambiguous between the following methods or properties: ScaleTime.OnKeyDown(this System.Collections.Generic.List<AIProject.UI.ICommandData>, UnityEngine.KeyCode, UnityEngine.Events.UnityAction)' and SkipResources.OnKeyDown(this System.Collections.Generic.List<AIProject.UI.ICommandData>, UnityEngine.KeyCode, UnityEngine.Events.UnityAction)'

Enum Crash

// #name enum_test

using System;

public enum A
{
    B,
    C,
    D,
}

// crash if type of a is enum
public class Test
{
    public A a;

    static public Test aa = new Test(A.B);

    public Test(A a)
    {
        this.a = a;
    }
}

//public class Test2
//{
//    public int a;

//    static public Test2 aa = new Test2(0);

//    public Test2(int a)
//    {
//        this.a = a;
//    }
//}

public static class EnumTest
{
    public static void Main()
    {
    }
}

Unity Player [version: Unity 5.6.4p2_278fc6850ee0]

mono.dll caused an Access Violation (0xc0000005)
in module mono.dll at 0033:d7ad4f24.

Read from location 00000008 caused an access violation.

Initialize engine version: 5.6.4p2 (278fc6850ee0)

========== OUTPUTING STACK TRACE ==================

0x00007FFCD7AD0367 (mono) mono_class_is_valuetype
0x00007FFCD7B436F8 (mono) mono_type_get_object
0x00007FFCD7B3DCD9 (mono) mono_property_get_object
0x00000000079B2513 (Mono JIT Code) (wrapper managed-to-native) System.Reflection.Emit.TypeBuilder:create_runtime_class (System.Reflection.Emit.TypeBuilder)
0x00000000079B197C (Mono JIT Code) System.Reflection.Emit.TypeBuilder:CreateType ()
0x000000000BE3FB42 (Mono JIT Code) Mono.CSharp.TypeDefinition:CloseContainer ()
0x000000000BE3F7AC (Mono JIT Code) Mono.CSharp.TypeContainer:CloseContainer ()
0x000000000BE3F7AC (Mono JIT Code) Mono.CSharp.TypeContainer:CloseContainer ()
0x000000000BE3F60A (Mono JIT Code) Mono.CSharp.ModuleContainer:CloseContainer ()
0x00000000079F963A (Mono JIT Code) ScriptLoader.MonoCompiler:Compile (System.Collections.Generic.Dictionary`2<string, byte[]>,System.IO.TextWriter)
0x00000000079EC8C2 (Mono JIT Code) ScriptLoader.ScriptLoader:CompileScripts ()
0x00000000335493A8 (Mono JIT Code) ScriptLoader.ScriptLoader:Update ()
0x0000000004D92D72 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FFCD7BCADFC (mono) mono_walk_stack
0x00007FFCD7B28A9D (mono) mono_runtime_invoke
...<Game Call Stack>...
0x00007FFD6C907034 (KERNEL32) BaseThreadInitThunk
0x00007FFD6CC22651 (ntdll) RtlUserThreadStart

========== END OF STACKTRACE ===========

**** Crash! ****

Either a bug of compiler or incorrect parameters were feed into compiler.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.