Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Modding game API? - using nLua

Discussion in 'Scripting' started by illuminatigaming, Feb 9, 2016.

  1. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Hey guys, i was thinking about creating a game where you can easily import and create mods. I want it to allow multiplayer.
    I thought about using asset bundles and the free .obj importer from the asset store combined with nLua, which seems quite good.
    I am a bit unsure. Should i Sandbox the lua code and only allow access to a API or just let it access the unity engine and let the script do what it wants and then leaving it to the users risk what the mod dies?
    What would you guys suggest? Any ideas how to create an API? Can it contain unity engine code or would this require giving the Lua Scripts acces to unity engine Methode?

    The mods are in the game folder at local low and will be dynamically loaded when the game is started.
     
  2. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    I have tried making a custom c# script that can act as API. I can then import its classes to lua.
    But can the script contain mono Methods to which nLua doesnt has access? (So to control its access)
    Or isnt that necessary?
    And what about networking the mods?
     
  3. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    NLua doesn't expose UnityEngine methods, so it's a relatively safe sandbox unless you register your own unsafe C# code. If you wrap them in your own safe C# methods, it'll be harder for the modder to write exploits or code that crashes the game. However, the modder could still cause problems with pure Lua, such as writing an infinite loop that hangs NLua.

    For networking, if you want to give the modder the ability to control network operations, wrap it in safe methods. The modder probably doesn't need to control network operations, though. Just make sure all players have the same mod and they're all in sync.
     
    SirNiklas likes this.
  4. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    How can i make safe C# code?
     
  5. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,670
    By "safe code," I just mean check the parameters first to make sure the modder is allowed to do it.

    For example, don't expose UnityObject.Destroy. A modder could use this to destroy critical GameObjects in the scene.

    Instead, write a new method such as "DestroyObject":
    Code (csharp):
    1. public static void DestroyObject(Object obj, float t = 0.0F) {
    2.     if (IsAllowedToDestroy(obj)) {
    3.         Destroy(obj, t);
    4.     }
    5. }
    (You need to define the function IsAllowedToDestroy according to the design of your game.)
     
  6. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Ah, thanks. Smart idea :D
     
  7. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    I was thinking, how can i stop exposing functions like the destroy one?
     
  8. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,513
    If you're asking that, I'm betting you're planning on using the unity specific nLua project that brings NLua into unity:
    https://github.com/Mervill/Unity3D-NLua

    I believe TonyLi is just talking about NLua vanilla:
    http://nlua.org/

    Note, using NLua vanilla, with out the extra library stuff that Unity3D-NLua has, means that NLua will only have access to what you allow it access.

    Scroll down on nlua.org to where it says: "Using the .Net objects". Here you can inject objects, assemblies, specific classes, etc.

    How you could uncover available methods, you could just inject objects as a global that have all the methods defined on it. This means there's leg work on your end to inject everything you want injected.

    Otherwise use Unity3D-NLua, which injects everything for you.
     
    TonyLi likes this.
  9. illuminatigaming

    illuminatigaming

    Joined:
    Jan 16, 2016
    Posts:
    17
    Currently i am using the Unity3D nLua.
    So i think i will replace it by the Standort nLua and then import and givr acces to custom classes etc/save Unity Classes.