Jump to content

[WIP] Konsole


Recommended Posts

I'm working on a plugin to give a developer style console to KSP. Primary uses would be developers of plugins and users who want to explore the internals of KSP.

The idea is to use the Mono.csharp.eval to allow real time execution of C# statements. Like:


>print(2+2)
4

or more to the point.


>GameObject.Find("Minmus").destroy(); //Take that Minmus!

Cobbling from the internet I came up with this proof of concept.

using System;
using KSP.IO;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Mono.CSharp;

public class PartlessLoader : KSP.Testing.UnitTest
{
public PartlessLoader()
{
//Called at the first loading screen
//When you start the game.
MyPlugin.Initialize();
}
}

public static class MyPlugin
{
public static UnityEngine.GameObject CmdConsole;
public static void Initialize()
{
CmdConsole = new UnityEngine.GameObject(
"No Parts!",
new Type[] { typeof(CmdConsole) });
UnityEngine.GameObject.DontDestroyOnLoad(CmdConsole);
}
}

public class CmdConsole : MonoBehaviour/*Singleton<CmdConsole>*/
{
public KeyCode[] m_ShortcutKeys = new KeyCode[]{KeyCode.LeftAlt, KeyCode.F12}; //the keys used to open Console;
public bool m_IsConsoleOpen = false;

private string m_editstr = "";
private string m_result = "";

private int m_cmdId = 0; //used to identify cmd

// Use this for initialization
void Start () {
Mono.CSharp.Evaluator.Init(new string[] { });
foreach (System.Reflection.Assembly assembly in AppDomain.CurrentDomain.GetAssemblies())
{
//Dbg.Log("refer: {0}", assembly.FullName);
if( assembly.FullName.Contains("Cecil") || assembly.FullName.Contains("UnityEditor") )
continue;
Mono.CSharp.Evaluator.ReferenceAssembly(assembly);
}
Evaluator.Run ("using UnityEngine;\n" +
"using System;"
);
}

void Update() {
//check if the console should be opened
if (m_IsConsoleOpen)
return;

bool bAllKeysDown = true;
foreach ( KeyCode kc in m_ShortcutKeys )
{
if( !Input.GetKey(kc) )
{
bAllKeysDown = false;
break;
}
}

if( bAllKeysDown )
{
m_IsConsoleOpen = true;
}
}

void OnGUI()
{
//if (!m_IsConsoleOpen)
// return;

m_editstr = GUI.TextArea(new Rect(10, 10, 400, 100), m_editstr);

if( GUI.Button(new Rect(420, 60, 100, 40), "Execute") )
{
++m_cmdId;
bool bSuccess = Run(m_editstr);
m_result = string.Format("{0}: {1}", m_cmdId, bSuccess ? "OK" : "Fail");
m_editstr = ""; //clear the script
}

if( GUI.Button(new Rect(530, 60, 100, 40), "Close") )
{
m_IsConsoleOpen = false;
}

if (m_result.Length > 0)
{
GUI.TextArea(new Rect(420, 10, 200, 30), m_result);
}
}


public bool Run(string cmd) {
return Evaluator.Run(cmd);
}

}

Which _almost_ works. But it throws an exception.

TypeLoadException: Could not load type 'System.Runtime.InteropServices.GuidAttribute' from assembly 'mscorlib,

Google says I am compiling to the wrong c# version. But that doesn't seem to be the problem. Any ideas?

Link to comment
Share on other sites

Which version are you compiling against? C# 3.0/ .NET 3.5?

R4m0n made one a few months ago but I don't know if he kept it up to date or posted the source anywhere:

Wow! That is exactly what I was trying for. I wish R4m0n was still around. I'd like to know how he made it work.

Link to comment
Share on other sites

Nuts! I got home tonight and was able to try r4m0ns version and it throws the same exception on mine. But then I tried it with 0.18.1 and it works. So they broke it with 0.19. Argh. so close....

Nevermind. It was a plugin conflict. Works great! Thanks EndlessWaves! And r4m0n wherever you are. :)

Link to comment
Share on other sites

This thread is quite old. Please consider starting a new thread rather than reviving this one.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...