1

I try to write a mod for a game. This is totally new territory for me, so I might be on the wrong track here. The game is written in Unity and you are able to add a .script file to your mod. The .script file can contain javascript that is parsed by Jint.

I tried to output a simple string from one of the game DLLs:

var UnityEngine = importNamespace("UnityEngine");
var IceEngine = importNamespace("IceEngine");
var GameMain = importNamespace("GameMain");

var output = GameMain.Game.ModPath;

UnityEngine.Debug.Log("----- Testmod Output Start-----");
UnityEngine.Debug.Log(output);
UnityEngine.Debug.Log("----- Testmod Output End-----");

In the GameMain.dll it says:

public class Game : MonoBehaviour, IUserManagerListener, IAccountMsg, IMsg, IRenderListener
{
  private static string modPath = Game.userPath + "/Mods";

  // lots of other code...

  public static string ModPath
  {
    get
    {
      return Game.modPath;
    }
  }

My understanding is that GameMain.Game.ModPath should give me a string. But instead the output in the log file is this:

----- Testmod Output Start-----
System.Dynamic.ExpandoObject
----- Testmod Output End-----

No matter wehat I try to output, I get a System.Dynamic.ExpandoObject and don't know what to do with it. Maybe someone can give me tips/resources to help. :)

Vencarii
  • 45
  • 6

1 Answers1

0

The ExpandoObject is the nearest equivalent that .Net has for a JavaScript Object - that is an object to which members can be added or removed on-the-fly.

This suggests to me that the underlying JavaScript method is actually returning an object rather than a string.

You can use Newtonsoft.Json to serialize the object:

var json = (string)JsonConvert.SerializeObject(GameMain.Game.ModPath);

Which will allow you to see if there's a property you should be using. So let's say this gives you {"modPath":"..."}, you should access that property directly like this:

var modPath = (string)GameMain.Game.ModPath.modPath;
Stewart Ritchie
  • 926
  • 7
  • 13
  • Thanks for your suggestion! But unfortunately I didn't get it to work. I tried `var Newtonsoft = importNamespace("Newtonsoft");` `var output = Newtonsoft.Json.JsonConvert.SerializeObject(GameMain.Game);` `UnityEngine.Debug.Log(output);` and `var output = UnityEngine.JSONSerializeModule.JsonUtility.ToJson(GameMain.Game);` `UnityEngine.Debug.Log(output);` But both times I got the error: JavaScriptException: Invalid generic type parameter – Vencarii Jun 11 '20 at 17:26
  • Ah... probably my mistake I failed to spot that your first sample was actually a script file. – Stewart Ritchie Jun 11 '20 at 18:15
  • Seems like there are a few steps to allowing CLR types to work in Jint https://github.com/sebastienros/jint#accessing-net-assemblies-and-classes – Stewart Ritchie Jun 11 '20 at 18:19