/// <summary>
/// Performs initialization of this instance, which can be called at startup or in play mode when the Unity Editor rebuilds scripts.
/// </summary>
private bool Reinitialize()
{
if ((csharpEngine != null /* Has an interpreter */) &&
(CSI.Interpreter.Console != null /* Has a console */) &&
(!object.ReferenceEquals(CSI.Interpreter.Console, this) /* Console is another object */))
{
UnityEngine.Object otherUnityObject = null;
if ((!(CSI.Interpreter.Console is UnityEngine.Object) /* Not a Unity object */) ||
((bool)(otherUnityObject = CSI.Interpreter.Console as UnityEngine.Object) /* Another live Unity object */))
{
this.enabled = false;
if (otherUnityObject)
{
Debug.LogWarning(
"Only one C# Interpreter may be created per scene; " +
"use the one on the object named: " + otherUnityObject.name, this);
if (otherUnityObject is Behaviour)
{
((Behaviour)otherUnityObject).enabled = true;
}
}
else
{
Debug.LogWarning(
"Only one C# Interpreter console may be created!", this);
}
return false; // Not initialized
}
}
this.EnforceParameterLimits();
if (string.IsNullOrEmpty(this.outputText))
{
this.outputText = "CSI Simple C# Interpreter v." + Version + " from Tiaan.com in CLR v." + Environment.Version.ToString() + " on Unity v." + Application.unityVersion;
}
else
{
this.outputText += string.Format("{0}[CSI reloaded and reset some data @ {1}]", Environment.NewLine, DateTime.Now.ToLongTimeString());
}
this.outputStringBuilder = new StringBuilder(this.outputText + Environment.NewLine);
this.outputScrollPosition.y = Mathf.Infinity;
if (this.inputTextCache == null)
{
this.inputTextCache = new List<string>(this.maxHistorySize + 1);
}
if (this.inputTextHistory == null)
{
this.inputTextHistory = new List<string>(this.maxHistorySize + 1);
this.inputTextCache.Clear();
}
this.currentHistoryIndex = Math.Max(0, this.inputTextHistory.Count - 1);
InitializeCompilerForUnity3D.RunOnce();
csharpEngine = new CSI.Interpreter();
csharpEngine.OnGetUnknownItem += this.OnGetUnknownItem;
CSI.Interpreter.Console = this;
string libraryPath = null;
if (Application.isEditor)
{
// For Editor: Located in project's "Library\ScriptAssemblies" directory
libraryPath = Path.GetDirectoryName(
csharpEngine.FullExecutablePath());
}
else
{
// For Player: Located in "<ApplicationName>_Data\lib" directory
libraryPath = Application.dataPath;
}
try
{
if (!string.IsNullOrEmpty(libraryPath))
{
foreach (string reference in
Directory.GetFiles(libraryPath, "Assembly - *.dll"))
{
// Add DLLs from the project's "Library\ScriptAssemblies" directory:
// * "Assembly - CSharp.dll"
// * "Assembly - CSharp - Editor.dll"
// * "Assembly - CSharp - first pass.dll"
// * "Assembly - UnityScript - first pass.dll"
csharpEngine.AddReference(reference);
}
}
string includeFile = this.includeFile;
if (!string.IsNullOrEmpty(includeFile))
{
includeFile = ResolveFilename(includeFile);
if (!csharpEngine.ReadIncludeFile(includeFile))
{
Debug.LogWarning(
"CSI include-file not loaded: " + includeFile, this);
}
}
string includeAssetName;
string includeCode = GetAssetText(this.includeAsset, out includeAssetName);
if ((!string.IsNullOrEmpty(includeCode)) &&
(!csharpEngine.ReadIncludeCode(includeCode)))
{
Debug.LogWarning(
"CSI include-asset not loaded: " + (includeAssetName ?? string.Empty), this);
}
Assembly unityEngineAssembly = null;
string fullAssemblyPath = GetFullPathOfAssembly(
typeof(UnityEngine.GameObject).Assembly);
if (File.Exists(fullAssemblyPath))
{
// Adds "UnityEngine.dll", or rather "UnityEngine-Debug.dll", for the
// Editor, which is located in the "...\Unity\Editor\Data\lib" directory.
// However, this does not work for the Standalone Windows Player, which
// uses the same mechanism for UnityEngine as for UnityEditor (below).
unityEngineAssembly = typeof(UnityEngine.GameObject).Assembly;
}
// Add the Unity Editor's assembly only when available
this.unityEditorAssembly = null;
foreach (Assembly assembly in
AppDomain.CurrentDomain.GetAssemblies())
{
if (this.unityEditorAssembly == null)
{
try
{
if ((assembly.FullName.StartsWith("UnityEditor,", StringComparison.OrdinalIgnoreCase)) &&
(assembly.GetType("UnityEditor.EditorApplication") != null))
{
this.unityEditorAssembly = assembly;
}
}
catch
{
// Skip problematic assemblies
}
}
if (unityEngineAssembly == null)
{
try
{
if (((assembly.FullName.StartsWith("UnityEngine,", StringComparison.OrdinalIgnoreCase)) ||
(assembly.FullName.StartsWith("UnityEngine-Debug,", StringComparison.OrdinalIgnoreCase))) &&
(assembly.GetType("UnityEngine.GameObject") != null))
{
unityEngineAssembly = assembly;
}
}
catch
{
// Skip problematic assemblies
}
}
if ((this.unityEditorAssembly != null) &&
(unityEngineAssembly != null))
{
break;
}
}
if (unityEngineAssembly != null)
{
// Include "UnityEngine.dll" or "UnityEngine-Debug.dll"
string filename = GetFullPathOfAssembly(unityEngineAssembly);
if (File.Exists(filename))
{
csharpEngine.AddReference(filename);
csharpEngine.AddNamespace("UnityEngine");
}
}
if (this.unityEditorAssembly != null)
{
// Include "UnityEditor.dll"
string filename =
GetFullPathOfAssembly(this.unityEditorAssembly);
if (File.Exists(filename))
{
csharpEngine.AddReference(filename);
csharpEngine.AddNamespace("UnityEditor");
}
}
this.PromptForInput(PromptStart);
this.AddGlobal("csi", this);
return true; // Initialized successfully
}
catch (IOException exception)
{
// Probably running in the web player without required rights
Debug.LogError(
"CSI failed to initialize (web player not supported): " + exception.Message, this);
return false;
}
}