Christopher
8 years ago
66 changed files with 2353 additions and 2353 deletions
@ -1,399 +1,399 @@
|
||||
#if (!PCL) && ((!UNITY_5) || UNITY_STANDALONE) |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Net; |
||||
using System.Net.Sockets; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using MoonSharp.VsCodeDebugger.DebuggerLogic; |
||||
using MoonSharp.Interpreter; |
||||
using MoonSharp.Interpreter.Debugging; |
||||
using MoonSharp.VsCodeDebugger.SDK; |
||||
|
||||
namespace MoonSharp.VsCodeDebugger |
||||
{ |
||||
/// <summary> |
||||
/// Class implementing a debugger allowing attaching from a Visual Studio Code debugging session. |
||||
/// </summary> |
||||
public class MoonSharpVsCodeDebugServer : IDisposable |
||||
{ |
||||
object m_Lock = new object(); |
||||
List<AsyncDebugger> m_DebuggerList = new List<AsyncDebugger>(); |
||||
AsyncDebugger m_Current = null; |
||||
ManualResetEvent m_StopEvent = new ManualResetEvent(false); |
||||
bool m_Started = false; |
||||
int m_Port; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="MoonSharpVsCodeDebugServer" /> class. |
||||
/// </summary> |
||||
/// <param name="port">The port on which the debugger listens. It's recommended to use 41912.</param> |
||||
public MoonSharpVsCodeDebugServer(int port = 41912) |
||||
{ |
||||
m_Port = port; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="MoonSharpVsCodeDebugServer" /> class with a default script. |
||||
/// Note that for this specific script, it will NOT attach the debugger to the script. |
||||
/// </summary> |
||||
/// <param name="script">The script object to debug.</param> |
||||
/// <param name="port">The port on which the debugger listens. It's recommended to use 41912 unless you are going to keep more than one script object around.</param> |
||||
/// <param name="sourceFinder">A function which gets in input a source code and returns the path to |
||||
/// source file to use. It can return null and in that case (or if the file cannot be found) |
||||
/// a temporary file will be generated on the fly.</param> |
||||
[Obsolete("Use the constructor taking only a port, and the 'Attach' method instead.")] |
||||
public MoonSharpVsCodeDebugServer(Script script, int port, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
m_Port = port; |
||||
m_Current = new AsyncDebugger(script, sourceFinder ?? (s => s.Name), "Default script"); |
||||
m_DebuggerList.Add(m_Current); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Attaches the specified script to the debugger |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <param name="name">The name of the script.</param> |
||||
/// <param name="sourceFinder">A function which gets in input a source code and returns the path to |
||||
/// source file to use. It can return null and in that case (or if the file cannot be found) |
||||
/// a temporary file will be generated on the fly.</param> |
||||
/// <exception cref="ArgumentException">If the script has already been attached to this debugger.</exception> |
||||
public void AttachToScript(Script script, string name, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (m_DebuggerList.Any(d => d.Script == script)) |
||||
throw new ArgumentException("Script already attached to this debugger."); |
||||
|
||||
var debugger = new AsyncDebugger(script, sourceFinder ?? (s => s.Name), name); |
||||
script.AttachDebugger(debugger); |
||||
m_DebuggerList.Add(debugger); |
||||
|
||||
if (m_Current == null) |
||||
m_Current = debugger; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets a list of the attached debuggers by id and name |
||||
/// </summary> |
||||
public IEnumerable<KeyValuePair<int, string>> GetAttachedDebuggersByIdAndName() |
||||
{ |
||||
lock (m_Lock) |
||||
return m_DebuggerList |
||||
.OrderBy(d => d.Id) |
||||
.Select(d => new KeyValuePair<int, string>(d.Id, d.Name)) |
||||
.ToArray(); |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets or sets the current script by ID (see GetAttachedDebuggersByIdAndName). |
||||
/// New vscode connections will attach to this debugger ID. Changing the current ID does NOT disconnect |
||||
/// connected clients. |
||||
/// </summary> |
||||
public int? CurrentId |
||||
{ |
||||
get { lock (m_Lock) return m_Current != null ? m_Current.Id : (int?)null; } |
||||
set |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (value == null) |
||||
{ |
||||
m_Current = null; |
||||
return; |
||||
} |
||||
|
||||
var current = (m_DebuggerList.FirstOrDefault(d => d.Id == value)); |
||||
|
||||
if (current == null) |
||||
throw new ArgumentException("Cannot find debugger with given Id."); |
||||
|
||||
m_Current = current; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets or sets the current script. New vscode connections will attach to this script. Changing the current script does NOT disconnect |
||||
/// connected clients. |
||||
/// </summary> |
||||
public Script Current |
||||
{ |
||||
get { lock(m_Lock) return m_Current != null ? m_Current.Script : null; } |
||||
set |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (value == null) |
||||
{ |
||||
m_Current = null; |
||||
return; |
||||
} |
||||
|
||||
var current = (m_DebuggerList.FirstOrDefault(d => d.Script == value)); |
||||
|
||||
if (current == null) |
||||
throw new ArgumentException("Cannot find debugger with given script associated."); |
||||
|
||||
m_Current = current; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Detaches the specified script. The debugger attached to that script will get disconnected. |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <exception cref="ArgumentException">Thrown if the script cannot be found.</exception> |
||||
public void Detach(Script script) |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
var removed = m_DebuggerList.FirstOrDefault(d => d.Script == script); |
||||
|
||||
if (removed == null) |
||||
throw new ArgumentException("Cannot detach script - not found."); |
||||
|
||||
removed.Client = null; |
||||
|
||||
m_DebuggerList.Remove(removed); |
||||
|
||||
if (m_Current == removed) |
||||
{ |
||||
if (m_DebuggerList.Count > 0) |
||||
m_Current = m_DebuggerList[m_DebuggerList.Count - 1]; |
||||
else |
||||
m_Current = null; |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets a delegate which will be called when logging messages are generated |
||||
/// </summary> |
||||
public Action<string> Logger { get; set; } |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets the debugger object. Obsolete, use the new interface using the Attach method instead. |
||||
/// </summary> |
||||
[Obsolete("Use the Attach method instead.")] |
||||
public IDebugger GetDebugger() |
||||
{ |
||||
lock(m_Lock) |
||||
return m_Current; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Stops listening |
||||
/// </summary> |
||||
/// <exception cref="InvalidOperationException">Cannot stop; server was not started.</exception> |
||||
public void Dispose() |
||||
{ |
||||
m_StopEvent.Set(); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Starts listening on the localhost for incoming connections. |
||||
/// </summary> |
||||
public MoonSharpVsCodeDebugServer Start() |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (m_Started) |
||||
throw new InvalidOperationException("Cannot start; server has already been started."); |
||||
|
||||
m_StopEvent.Reset(); |
||||
|
||||
TcpListener serverSocket = null; |
||||
serverSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), m_Port); |
||||
serverSocket.Start(); |
||||
|
||||
SpawnThread("VsCodeDebugServer_" + m_Port.ToString(), () => ListenThread(serverSocket)); |
||||
|
||||
m_Started = true; |
||||
|
||||
return this; |
||||
} |
||||
} |
||||
|
||||
|
||||
private void ListenThread(TcpListener serverSocket) |
||||
{ |
||||
try |
||||
{ |
||||
while (!m_StopEvent.WaitOne(0)) |
||||
{ |
||||
#if DOTNET_CORE |
||||
var task = serverSocket.AcceptSocketAsync(); |
||||
task.Wait(); |
||||
var clientSocket = task.Result; |
||||
#else |
||||
var clientSocket = serverSocket.AcceptSocket(); |
||||
#endif |
||||
|
||||
if (clientSocket != null) |
||||
{ |
||||
string sessionId = Guid.NewGuid().ToString("N"); |
||||
Log("[{0}] : Accepted connection from client {1}", sessionId, clientSocket.RemoteEndPoint); |
||||
|
||||
SpawnThread("VsCodeDebugSession_" + sessionId, () => |
||||
{ |
||||
using (var networkStream = new NetworkStream(clientSocket)) |
||||
{ |
||||
try |
||||
{ |
||||
RunSession(sessionId, networkStream); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
Log("[{0}] : Error : {1}", ex.Message); |
||||
} |
||||
} |
||||
|
||||
#if DOTNET_CORE |
||||
clientSocket.Dispose(); |
||||
#else |
||||
clientSocket.Close(); |
||||
#endif |
||||
Log("[{0}] : Client connection closed", sessionId); |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
Log("Fatal error in listening thread : {0}", e.Message); |
||||
} |
||||
finally |
||||
{ |
||||
if (serverSocket != null) |
||||
serverSocket.Stop(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private void RunSession(string sessionId, NetworkStream stream) |
||||
{ |
||||
DebugSession debugSession = null; |
||||
|
||||
lock (m_Lock) |
||||
{ |
||||
if (m_Current != null) |
||||
debugSession = new MoonSharpDebugSession(this, m_Current); |
||||
else |
||||
debugSession = new EmptyDebugSession(this); |
||||
} |
||||
|
||||
debugSession.ProcessLoop(stream, stream); |
||||
} |
||||
|
||||
private void Log(string format, params object[] args) |
||||
{ |
||||
Action<string> logger = Logger; |
||||
|
||||
if (logger != null) |
||||
{ |
||||
string msg = string.Format(format, args); |
||||
logger(msg); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static void SpawnThread(string name, Action threadProc) |
||||
{ |
||||
#if DOTNET_CORE |
||||
System.Threading.Tasks.Task.Run(() => threadProc()); |
||||
#else |
||||
new System.Threading.Thread(() => threadProc()) |
||||
{ |
||||
IsBackground = true, |
||||
Name = name |
||||
} |
||||
.Start(); |
||||
#endif |
||||
} |
||||
} |
||||
} |
||||
|
||||
#else |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using MoonSharp.Interpreter; |
||||
using MoonSharp.Interpreter.Debugging; |
||||
|
||||
namespace MoonSharp.VsCodeDebugger |
||||
{ |
||||
public class MoonSharpVsCodeDebugServer : IDisposable |
||||
{ |
||||
public MoonSharpVsCodeDebugServer(int port = 41912) |
||||
{ |
||||
} |
||||
|
||||
[Obsolete("Use the constructor taking only a port, and the 'Attach' method instead.")] |
||||
public MoonSharpVsCodeDebugServer(Script script, int port, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
} |
||||
|
||||
public void AttachToScript(Script script, string name, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
} |
||||
|
||||
public IEnumerable<KeyValuePair<int, string>> GetAttachedDebuggersByIdAndName() |
||||
{ |
||||
yield break; |
||||
} |
||||
|
||||
|
||||
public int? CurrentId |
||||
{ |
||||
get { return null; } |
||||
set { } |
||||
} |
||||
|
||||
|
||||
public Script Current |
||||
{ |
||||
get { return null; } |
||||
set { } |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Detaches the specified script. The debugger attached to that script will get disconnected. |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <exception cref="ArgumentException">Thrown if the script cannot be found.</exception> |
||||
public void Detach(Script script) |
||||
{ |
||||
|
||||
} |
||||
|
||||
public Action<string> Logger { get; set; } |
||||
|
||||
|
||||
[Obsolete("Use the Attach method instead.")] |
||||
public IDebugger GetDebugger() |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
} |
||||
|
||||
public MoonSharpVsCodeDebugServer Start() |
||||
{ |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
} |
||||
#if (!PCL) && ((!UNITY_5) || UNITY_STANDALONE) |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.IO; |
||||
using System.Linq; |
||||
using System.Net; |
||||
using System.Net.Sockets; |
||||
using System.Text; |
||||
using System.Threading; |
||||
using MoonSharp.VsCodeDebugger.DebuggerLogic; |
||||
using MoonSharp.Interpreter; |
||||
using MoonSharp.Interpreter.Debugging; |
||||
using MoonSharp.VsCodeDebugger.SDK; |
||||
|
||||
namespace MoonSharp.VsCodeDebugger |
||||
{ |
||||
/// <summary> |
||||
/// Class implementing a debugger allowing attaching from a Visual Studio Code debugging session. |
||||
/// </summary> |
||||
public class MoonSharpVsCodeDebugServer : IDisposable |
||||
{ |
||||
object m_Lock = new object(); |
||||
List<AsyncDebugger> m_DebuggerList = new List<AsyncDebugger>(); |
||||
AsyncDebugger m_Current = null; |
||||
ManualResetEvent m_StopEvent = new ManualResetEvent(false); |
||||
bool m_Started = false; |
||||
int m_Port; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="MoonSharpVsCodeDebugServer" /> class. |
||||
/// </summary> |
||||
/// <param name="port">The port on which the debugger listens. It's recommended to use 41912.</param> |
||||
public MoonSharpVsCodeDebugServer(int port = 41912) |
||||
{ |
||||
m_Port = port; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="MoonSharpVsCodeDebugServer" /> class with a default script. |
||||
/// Note that for this specific script, it will NOT attach the debugger to the script. |
||||
/// </summary> |
||||
/// <param name="script">The script object to debug.</param> |
||||
/// <param name="port">The port on which the debugger listens. It's recommended to use 41912 unless you are going to keep more than one script object around.</param> |
||||
/// <param name="sourceFinder">A function which gets in input a source code and returns the path to |
||||
/// source file to use. It can return null and in that case (or if the file cannot be found) |
||||
/// a temporary file will be generated on the fly.</param> |
||||
[Obsolete("Use the constructor taking only a port, and the 'Attach' method instead.")] |
||||
public MoonSharpVsCodeDebugServer(Script script, int port, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
m_Port = port; |
||||
m_Current = new AsyncDebugger(script, sourceFinder ?? (s => s.Name), "Default script"); |
||||
m_DebuggerList.Add(m_Current); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Attaches the specified script to the debugger |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <param name="name">The name of the script.</param> |
||||
/// <param name="sourceFinder">A function which gets in input a source code and returns the path to |
||||
/// source file to use. It can return null and in that case (or if the file cannot be found) |
||||
/// a temporary file will be generated on the fly.</param> |
||||
/// <exception cref="ArgumentException">If the script has already been attached to this debugger.</exception> |
||||
public void AttachToScript(Script script, string name, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (m_DebuggerList.Any(d => d.Script == script)) |
||||
throw new ArgumentException("Script already attached to this debugger."); |
||||
|
||||
var debugger = new AsyncDebugger(script, sourceFinder ?? (s => s.Name), name); |
||||
script.AttachDebugger(debugger); |
||||
m_DebuggerList.Add(debugger); |
||||
|
||||
if (m_Current == null) |
||||
m_Current = debugger; |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets a list of the attached debuggers by id and name |
||||
/// </summary> |
||||
public IEnumerable<KeyValuePair<int, string>> GetAttachedDebuggersByIdAndName() |
||||
{ |
||||
lock (m_Lock) |
||||
return m_DebuggerList |
||||
.OrderBy(d => d.Id) |
||||
.Select(d => new KeyValuePair<int, string>(d.Id, d.Name)) |
||||
.ToArray(); |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets or sets the current script by ID (see GetAttachedDebuggersByIdAndName). |
||||
/// New vscode connections will attach to this debugger ID. Changing the current ID does NOT disconnect |
||||
/// connected clients. |
||||
/// </summary> |
||||
public int? CurrentId |
||||
{ |
||||
get { lock (m_Lock) return m_Current != null ? m_Current.Id : (int?)null; } |
||||
set |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (value == null) |
||||
{ |
||||
m_Current = null; |
||||
return; |
||||
} |
||||
|
||||
var current = (m_DebuggerList.FirstOrDefault(d => d.Id == value)); |
||||
|
||||
if (current == null) |
||||
throw new ArgumentException("Cannot find debugger with given Id."); |
||||
|
||||
m_Current = current; |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets or sets the current script. New vscode connections will attach to this script. Changing the current script does NOT disconnect |
||||
/// connected clients. |
||||
/// </summary> |
||||
public Script Current |
||||
{ |
||||
get { lock(m_Lock) return m_Current != null ? m_Current.Script : null; } |
||||
set |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (value == null) |
||||
{ |
||||
m_Current = null; |
||||
return; |
||||
} |
||||
|
||||
var current = (m_DebuggerList.FirstOrDefault(d => d.Script == value)); |
||||
|
||||
if (current == null) |
||||
throw new ArgumentException("Cannot find debugger with given script associated."); |
||||
|
||||
m_Current = current; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Detaches the specified script. The debugger attached to that script will get disconnected. |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <exception cref="ArgumentException">Thrown if the script cannot be found.</exception> |
||||
public void Detach(Script script) |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
var removed = m_DebuggerList.FirstOrDefault(d => d.Script == script); |
||||
|
||||
if (removed == null) |
||||
throw new ArgumentException("Cannot detach script - not found."); |
||||
|
||||
removed.Client = null; |
||||
|
||||
m_DebuggerList.Remove(removed); |
||||
|
||||
if (m_Current == removed) |
||||
{ |
||||
if (m_DebuggerList.Count > 0) |
||||
m_Current = m_DebuggerList[m_DebuggerList.Count - 1]; |
||||
else |
||||
m_Current = null; |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Gets or sets a delegate which will be called when logging messages are generated |
||||
/// </summary> |
||||
public Action<string> Logger { get; set; } |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets the debugger object. Obsolete, use the new interface using the Attach method instead. |
||||
/// </summary> |
||||
[Obsolete("Use the Attach method instead.")] |
||||
public IDebugger GetDebugger() |
||||
{ |
||||
lock(m_Lock) |
||||
return m_Current; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Stops listening |
||||
/// </summary> |
||||
/// <exception cref="InvalidOperationException">Cannot stop; server was not started.</exception> |
||||
public void Dispose() |
||||
{ |
||||
m_StopEvent.Set(); |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Starts listening on the localhost for incoming connections. |
||||
/// </summary> |
||||
public MoonSharpVsCodeDebugServer Start() |
||||
{ |
||||
lock (m_Lock) |
||||
{ |
||||
if (m_Started) |
||||
throw new InvalidOperationException("Cannot start; server has already been started."); |
||||
|
||||
m_StopEvent.Reset(); |
||||
|
||||
TcpListener serverSocket = null; |
||||
serverSocket = new TcpListener(IPAddress.Parse("127.0.0.1"), m_Port); |
||||
serverSocket.Start(); |
||||
|
||||
SpawnThread("VsCodeDebugServer_" + m_Port.ToString(), () => ListenThread(serverSocket)); |
||||
|
||||
m_Started = true; |
||||
|
||||
return this; |
||||
} |
||||
} |
||||
|
||||
|
||||
private void ListenThread(TcpListener serverSocket) |
||||
{ |
||||
try |
||||
{ |
||||
while (!m_StopEvent.WaitOne(0)) |
||||
{ |
||||
#if DOTNET_CORE |
||||
var task = serverSocket.AcceptSocketAsync(); |
||||
task.Wait(); |
||||
var clientSocket = task.Result; |
||||
#else |
||||
var clientSocket = serverSocket.AcceptSocket(); |
||||
#endif |
||||
|
||||
if (clientSocket != null) |
||||
{ |
||||
string sessionId = Guid.NewGuid().ToString("N"); |
||||
Log("[{0}] : Accepted connection from client {1}", sessionId, clientSocket.RemoteEndPoint); |
||||
|
||||
SpawnThread("VsCodeDebugSession_" + sessionId, () => |
||||
{ |
||||
using (var networkStream = new NetworkStream(clientSocket)) |
||||
{ |
||||
try |
||||
{ |
||||
RunSession(sessionId, networkStream); |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
Log("[{0}] : Error : {1}", ex.Message); |
||||
} |
||||
} |
||||
|
||||
#if DOTNET_CORE |
||||
clientSocket.Dispose(); |
||||
#else |
||||
clientSocket.Close(); |
||||
#endif |
||||
Log("[{0}] : Client connection closed", sessionId); |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
catch (Exception e) |
||||
{ |
||||
Log("Fatal error in listening thread : {0}", e.Message); |
||||
} |
||||
finally |
||||
{ |
||||
if (serverSocket != null) |
||||
serverSocket.Stop(); |
||||
} |
||||
} |
||||
|
||||
|
||||
private void RunSession(string sessionId, NetworkStream stream) |
||||
{ |
||||
DebugSession debugSession = null; |
||||
|
||||
lock (m_Lock) |
||||
{ |
||||
if (m_Current != null) |
||||
debugSession = new MoonSharpDebugSession(this, m_Current); |
||||
else |
||||
debugSession = new EmptyDebugSession(this); |
||||
} |
||||
|
||||
debugSession.ProcessLoop(stream, stream); |
||||
} |
||||
|
||||
private void Log(string format, params object[] args) |
||||
{ |
||||
Action<string> logger = Logger; |
||||
|
||||
if (logger != null) |
||||
{ |
||||
string msg = string.Format(format, args); |
||||
logger(msg); |
||||
} |
||||
} |
||||
|
||||
|
||||
private static void SpawnThread(string name, Action threadProc) |
||||
{ |
||||
#if DOTNET_CORE |
||||
System.Threading.Tasks.Task.Run(() => threadProc()); |
||||
#else |
||||
new System.Threading.Thread(() => threadProc()) |
||||
{ |
||||
IsBackground = true, |
||||
Name = name |
||||
} |
||||
.Start(); |
||||
#endif |
||||
} |
||||
} |
||||
} |
||||
|
||||
#else |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using MoonSharp.Interpreter; |
||||
using MoonSharp.Interpreter.Debugging; |
||||
|
||||
namespace MoonSharp.VsCodeDebugger |
||||
{ |
||||
public class MoonSharpVsCodeDebugServer : IDisposable |
||||
{ |
||||
public MoonSharpVsCodeDebugServer(int port = 41912) |
||||
{ |
||||
} |
||||
|
||||
[Obsolete("Use the constructor taking only a port, and the 'Attach' method instead.")] |
||||
public MoonSharpVsCodeDebugServer(Script script, int port, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
} |
||||
|
||||
public void AttachToScript(Script script, string name, Func<SourceCode, string> sourceFinder = null) |
||||
{ |
||||
} |
||||
|
||||
public IEnumerable<KeyValuePair<int, string>> GetAttachedDebuggersByIdAndName() |
||||
{ |
||||
yield break; |
||||
} |
||||
|
||||
|
||||
public int? CurrentId |
||||
{ |
||||
get { return null; } |
||||
set { } |
||||
} |
||||
|
||||
|
||||
public Script Current |
||||
{ |
||||
get { return null; } |
||||
set { } |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Detaches the specified script. The debugger attached to that script will get disconnected. |
||||
/// </summary> |
||||
/// <param name="script">The script.</param> |
||||
/// <exception cref="ArgumentException">Thrown if the script cannot be found.</exception> |
||||
public void Detach(Script script) |
||||
{ |
||||
|
||||
} |
||||
|
||||
public Action<string> Logger { get; set; } |
||||
|
||||
|
||||
[Obsolete("Use the Attach method instead.")] |
||||
public IDebugger GetDebugger() |
||||
{ |
||||
return null; |
||||
} |
||||
|
||||
public void Dispose() |
||||
{ |
||||
} |
||||
|
||||
public MoonSharpVsCodeDebugServer Start() |
||||
{ |
||||
return this; |
||||
} |
||||
|
||||
} |
||||
} |
||||
#endif |
@ -1,16 +1,16 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
using MoonSharp.Interpreter.Compatibility.Frameworks; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility |
||||
{ |
||||
public static class Framework |
||||
{ |
||||
static FrameworkCurrent s_FrameworkCurrent = new FrameworkCurrent(); |
||||
|
||||
public static FrameworkBase Do { get { return s_FrameworkCurrent; } } |
||||
} |
||||
} |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
using MoonSharp.Interpreter.Compatibility.Frameworks; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility |
||||
{ |
||||
public static class Framework |
||||
{ |
||||
static FrameworkCurrent s_FrameworkCurrent = new FrameworkCurrent(); |
||||
|
||||
public static FrameworkBase Do { get { return s_FrameworkCurrent; } } |
||||
} |
||||
} |
||||
|
@ -1,75 +1,75 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
public abstract class FrameworkBase |
||||
{ |
||||
public abstract bool StringContainsChar(string str, char chr); |
||||
|
||||
public abstract bool IsValueType(Type t); |
||||
|
||||
public abstract Assembly GetAssembly(Type t); |
||||
|
||||
public abstract Type GetBaseType(Type t); |
||||
|
||||
public abstract bool IsGenericType(Type t); |
||||
|
||||
public abstract bool IsGenericTypeDefinition(Type t); |
||||
|
||||
public abstract bool IsEnum(Type t); |
||||
|
||||
public abstract bool IsNestedPublic(Type t); |
||||
|
||||
public abstract bool IsAbstract(Type t); |
||||
|
||||
public abstract bool IsInterface(Type t); |
||||
|
||||
public abstract Attribute[] GetCustomAttributes(Type t, bool inherit); |
||||
|
||||
public abstract Attribute[] GetCustomAttributes(Type t, Type at, bool inherit); |
||||
|
||||
public abstract Type[] GetInterfaces(Type t); |
||||
|
||||
public abstract bool IsInstanceOfType(Type t, object o); |
||||
|
||||
public abstract MethodInfo GetAddMethod(EventInfo ei); |
||||
|
||||
public abstract MethodInfo GetRemoveMethod(EventInfo ei); |
||||
|
||||
public abstract MethodInfo GetGetMethod(PropertyInfo pi); |
||||
|
||||
public abstract MethodInfo GetSetMethod(PropertyInfo pi); |
||||
|
||||
public abstract Type GetInterface(Type type, string name); |
||||
|
||||
public abstract PropertyInfo[] GetProperties(Type type); |
||||
|
||||
public abstract PropertyInfo GetProperty(Type type, string name); |
||||
|
||||
public abstract Type[] GetNestedTypes(Type type); |
||||
|
||||
public abstract EventInfo[] GetEvents(Type type); |
||||
|
||||
public abstract ConstructorInfo[] GetConstructors(Type type); |
||||
|
||||
public abstract Type[] GetAssemblyTypes(Assembly asm); |
||||
|
||||
public abstract MethodInfo[] GetMethods(Type type); |
||||
|
||||
public abstract FieldInfo[] GetFields(Type t); |
||||
|
||||
public abstract MethodInfo GetMethod(Type type, string name); |
||||
|
||||
public abstract Type[] GetGenericArguments(Type t); |
||||
|
||||
public abstract bool IsAssignableFrom(Type current, Type toCompare); |
||||
|
||||
public abstract bool IsDbNull(object o); |
||||
|
||||
public abstract MethodInfo GetMethod(Type resourcesType, string v, Type[] type); |
||||
} |
||||
} |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
public abstract class FrameworkBase |
||||
{ |
||||
public abstract bool StringContainsChar(string str, char chr); |
||||
|
||||
public abstract bool IsValueType(Type t); |
||||
|
||||
public abstract Assembly GetAssembly(Type t); |
||||
|
||||
public abstract Type GetBaseType(Type t); |
||||
|
||||
public abstract bool IsGenericType(Type t); |
||||
|
||||
public abstract bool IsGenericTypeDefinition(Type t); |
||||
|
||||
public abstract bool IsEnum(Type t); |
||||
|
||||
public abstract bool IsNestedPublic(Type t); |
||||
|
||||
public abstract bool IsAbstract(Type t); |
||||
|
||||
public abstract bool IsInterface(Type t); |
||||
|
||||
public abstract Attribute[] GetCustomAttributes(Type t, bool inherit); |
||||
|
||||
public abstract Attribute[] GetCustomAttributes(Type t, Type at, bool inherit); |
||||
|
||||
public abstract Type[] GetInterfaces(Type t); |
||||
|
||||
public abstract bool IsInstanceOfType(Type t, object o); |
||||
|
||||
public abstract MethodInfo GetAddMethod(EventInfo ei); |
||||
|
||||
public abstract MethodInfo GetRemoveMethod(EventInfo ei); |
||||
|
||||
public abstract MethodInfo GetGetMethod(PropertyInfo pi); |
||||
|
||||
public abstract MethodInfo GetSetMethod(PropertyInfo pi); |
||||
|
||||
public abstract Type GetInterface(Type type, string name); |
||||
|
||||
public abstract PropertyInfo[] GetProperties(Type type); |
||||
|
||||
public abstract PropertyInfo GetProperty(Type type, string name); |
||||
|
||||
public abstract Type[] GetNestedTypes(Type type); |
||||
|
||||
public abstract EventInfo[] GetEvents(Type type); |
||||
|
||||
public abstract ConstructorInfo[] GetConstructors(Type type); |
||||
|
||||
public abstract Type[] GetAssemblyTypes(Assembly asm); |
||||
|
||||
public abstract MethodInfo[] GetMethods(Type type); |
||||
|
||||
public abstract FieldInfo[] GetFields(Type t); |
||||
|
||||
public abstract MethodInfo GetMethod(Type type, string name); |
||||
|
||||
public abstract Type[] GetGenericArguments(Type t); |
||||
|
||||
public abstract bool IsAssignableFrom(Type current, Type toCompare); |
||||
|
||||
public abstract bool IsDbNull(object o); |
||||
|
||||
public abstract MethodInfo GetMethod(Type resourcesType, string v, Type[] type); |
||||
} |
||||
} |
||||
|
@ -1,116 +1,116 @@
|
||||
#if !(DOTNET_CORE || NETFX_CORE) |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
abstract class FrameworkClrBase : FrameworkReflectionBase |
||||
{ |
||||
BindingFlags BINDINGFLAGS_MEMBER = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; |
||||
BindingFlags BINDINGFLAGS_INNERCLASS = BindingFlags.Public | BindingFlags.NonPublic; |
||||
|
||||
public override Type GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetAddMethod(true); |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return type.GetConstructors(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return type.GetEvents(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return type.GetFields(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetGenericArguments(); |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetGetMethod(true); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return t.GetInterfaces(); |
||||
} |
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return type.GetMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return type.GetMethods(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return type.GetNestedTypes(BINDINGFLAGS_INNERCLASS); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return type.GetProperties(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return type.GetProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetRemoveMethod(true); |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetSetMethod(true); |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.IsAssignableFrom(toCompare); |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
return t.IsInstanceOfType(o); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return resourcesType.GetMethod(name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return asm.GetTypes(); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
#if !(DOTNET_CORE || NETFX_CORE) |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
abstract class FrameworkClrBase : FrameworkReflectionBase |
||||
{ |
||||
BindingFlags BINDINGFLAGS_MEMBER = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; |
||||
BindingFlags BINDINGFLAGS_INNERCLASS = BindingFlags.Public | BindingFlags.NonPublic; |
||||
|
||||
public override Type GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetAddMethod(true); |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return type.GetConstructors(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return type.GetEvents(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return type.GetFields(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetGenericArguments(); |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetGetMethod(true); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return t.GetInterfaces(); |
||||
} |
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return type.GetMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return type.GetMethods(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return type.GetNestedTypes(BINDINGFLAGS_INNERCLASS); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return type.GetProperties(BINDINGFLAGS_MEMBER); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return type.GetProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetRemoveMethod(true); |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetSetMethod(true); |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.IsAssignableFrom(toCompare); |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
return t.IsInstanceOfType(o); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return resourcesType.GetMethod(name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return asm.GetTypes(); |
||||
} |
||||
|
||||
} |
||||
} |
||||
|
||||
#endif |
@ -1,76 +1,76 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
#if DOTNET_CORE || NETFX_CORE |
||||
using TTypeInfo = System.Reflection.TypeInfo; |
||||
#else |
||||
using TTypeInfo = System.Type; |
||||
#endif |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
abstract class FrameworkReflectionBase : FrameworkBase |
||||
{ |
||||
public abstract TTypeInfo GetTypeInfoFromType(Type t); |
||||
|
||||
public override Assembly GetAssembly(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).Assembly; |
||||
} |
||||
|
||||
public override Type GetBaseType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).BaseType; |
||||
} |
||||
|
||||
|
||||
public override bool IsValueType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsValueType; |
||||
} |
||||
|
||||
public override bool IsInterface(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsInterface; |
||||
} |
||||
|
||||
public override bool IsNestedPublic(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsNestedPublic; |
||||
} |
||||
public override bool IsAbstract(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsAbstract; |
||||
} |
||||
|
||||
public override bool IsEnum(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsEnum; |
||||
} |
||||
|
||||
public override bool IsGenericTypeDefinition(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsGenericTypeDefinition; |
||||
} |
||||
|
||||
public override bool IsGenericType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsGenericType; |
||||
} |
||||
|
||||
public override Attribute[] GetCustomAttributes(Type t, bool inherit) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetCustomAttributes(inherit).OfType<Attribute>().ToArray(); |
||||
} |
||||
|
||||
public override Attribute[] GetCustomAttributes(Type t, Type at, bool inherit) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetCustomAttributes(at, inherit).OfType<Attribute>().ToArray(); |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
#if DOTNET_CORE || NETFX_CORE |
||||
using TTypeInfo = System.Reflection.TypeInfo; |
||||
#else |
||||
using TTypeInfo = System.Type; |
||||
#endif |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
abstract class FrameworkReflectionBase : FrameworkBase |
||||
{ |
||||
public abstract TTypeInfo GetTypeInfoFromType(Type t); |
||||
|
||||
public override Assembly GetAssembly(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).Assembly; |
||||
} |
||||
|
||||
public override Type GetBaseType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).BaseType; |
||||
} |
||||
|
||||
|
||||
public override bool IsValueType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsValueType; |
||||
} |
||||
|
||||
public override bool IsInterface(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsInterface; |
||||
} |
||||
|
||||
public override bool IsNestedPublic(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsNestedPublic; |
||||
} |
||||
public override bool IsAbstract(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsAbstract; |
||||
} |
||||
|
||||
public override bool IsEnum(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsEnum; |
||||
} |
||||
|
||||
public override bool IsGenericTypeDefinition(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsGenericTypeDefinition; |
||||
} |
||||
|
||||
public override bool IsGenericType(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).IsGenericType; |
||||
} |
||||
|
||||
public override Attribute[] GetCustomAttributes(Type t, bool inherit) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetCustomAttributes(inherit).OfType<Attribute>().ToArray(); |
||||
} |
||||
|
||||
public override Attribute[] GetCustomAttributes(Type t, Type at, bool inherit) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetCustomAttributes(at, inherit).OfType<Attribute>().ToArray(); |
||||
} |
||||
|
||||
|
||||
} |
||||
} |
||||
|
@ -1,31 +1,31 @@
|
||||
#if !(DOTNET_CORE || NETFX_CORE) && !PCL |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkClrBase |
||||
{ |
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
return o != null && Convert.IsDBNull(o); |
||||
} |
||||
|
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetInterface(name); |
||||
} |
||||
} |
||||
} |
||||
|
||||
#endif |
||||
#if !(DOTNET_CORE || NETFX_CORE) && !PCL |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkClrBase |
||||
{ |
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
return o != null && Convert.IsDBNull(o); |
||||
} |
||||
|
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetInterface(name); |
||||
} |
||||
} |
||||
} |
||||
|
||||
#endif |
||||
|
@ -1,132 +1,132 @@
|
||||
#if DOTNET_CORE |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkReflectionBase |
||||
{ |
||||
public override TypeInfo GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t.GetTypeInfo(); |
||||
} |
||||
|
||||
private T[] SafeArray<T>(IEnumerable<T> prop) |
||||
{ |
||||
return prop != null ? prop.ToArray() : new T[0]; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetAddMethod(true); |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredConstructors); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredEvents); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredFields); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetTypeInfo().GetGenericArguments(); |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetGetMethod(true); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetInterface(name); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetInterfaces(); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredMethods); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredNestedTypes.Select(ti => ti.AsType())); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredProperties); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetRemoveMethod(true); |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetSetMethod(true); |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.GetTypeInfo().IsAssignableFrom(toCompare.GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
#if DOTNET_CORE |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkReflectionBase |
||||
{ |
||||
public override TypeInfo GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t.GetTypeInfo(); |
||||
} |
||||
|
||||
private T[] SafeArray<T>(IEnumerable<T> prop) |
||||
{ |
||||
return prop != null ? prop.ToArray() : new T[0]; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetAddMethod(true); |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredConstructors); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredEvents); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredFields); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetTypeInfo().GetGenericArguments(); |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetGetMethod(true); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetInterface(name); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return GetTypeInfoFromType(t).GetInterfaces(); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredMethods); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredNestedTypes.Select(ti => ti.AsType())); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredProperties); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.GetRemoveMethod(true); |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetSetMethod(true); |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.GetTypeInfo().IsAssignableFrom(toCompare.GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
return o != null && o.GetType().FullName.StartsWith("System.DBNull"); |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
return t.GetTypeInfo().IsInstanceOfType(o); |
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return resourcesType.GetTypeInfo().GetMethod(name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return asm.GetExportedTypes(); |
||||
} |
||||
|
||||
} |
||||
} |
||||
#endif |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
return t.GetTypeInfo().IsInstanceOfType(o); |
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return resourcesType.GetTypeInfo().GetMethod(name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return asm.GetExportedTypes(); |
||||
} |
||||
|
||||
} |
||||
} |
||||
#endif |
||||
|
@ -1,30 +1,30 @@
|
||||
#if !(DOTNET_CORE || NETFX_CORE) && PCL |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkClrBase |
||||
{ |
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
#if !(DOTNET_CORE || NETFX_CORE) && PCL |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkClrBase |
||||
{ |
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
return o != null && o.GetType().FullName.StartsWith("System.DBNull"); |
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr.ToString()); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetInterfaces(). |
||||
FirstOrDefault(t => t.Name == name); |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr.ToString()); |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetInterfaces(). |
||||
FirstOrDefault(t => t.Name == name); |
||||
} |
||||
} |
||||
} |
||||
|
||||
#endif |
@ -1,160 +1,160 @@
|
||||
#if NETFX_CORE && !DOTNET_CORE |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkReflectionBase |
||||
{ |
||||
public override TypeInfo GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t.GetTypeInfo(); |
||||
} |
||||
|
||||
private T[] SafeArray<T>(IEnumerable<T> prop) |
||||
{ |
||||
return prop != null ? prop.ToArray() : new T[0]; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.AddMethod; |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredConstructors); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredEvents); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredFields); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetTypeInfo().GenericTypeArguments; |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetMethod; |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(t => t.Name == name); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(t).ImplementedInterfaces); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetDeclaredMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredMethods); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredNestedTypes.Select(ti => ti.AsType())); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredProperties); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetDeclaredProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.RemoveMethod; |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.SetMethod; |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.GetTypeInfo().IsAssignableFrom(toCompare.GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
#if NETFX_CORE && !DOTNET_CORE |
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
|
||||
namespace MoonSharp.Interpreter.Compatibility.Frameworks |
||||
{ |
||||
class FrameworkCurrent : FrameworkReflectionBase |
||||
{ |
||||
public override TypeInfo GetTypeInfoFromType(Type t) |
||||
{ |
||||
return t.GetTypeInfo(); |
||||
} |
||||
|
||||
private T[] SafeArray<T>(IEnumerable<T> prop) |
||||
{ |
||||
return prop != null ? prop.ToArray() : new T[0]; |
||||
} |
||||
|
||||
public override MethodInfo GetAddMethod(EventInfo ei) |
||||
{ |
||||
return ei.AddMethod; |
||||
} |
||||
|
||||
public override ConstructorInfo[] GetConstructors(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredConstructors); |
||||
} |
||||
|
||||
public override EventInfo[] GetEvents(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredEvents); |
||||
} |
||||
|
||||
public override FieldInfo[] GetFields(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredFields); |
||||
} |
||||
|
||||
public override Type[] GetGenericArguments(Type type) |
||||
{ |
||||
return type.GetTypeInfo().GenericTypeArguments; |
||||
} |
||||
|
||||
public override MethodInfo GetGetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.GetMethod; |
||||
} |
||||
|
||||
public override Type GetInterface(Type type, string name) |
||||
{ |
||||
return type.GetTypeInfo().ImplementedInterfaces.FirstOrDefault(t => t.Name == name); |
||||
} |
||||
|
||||
public override Type[] GetInterfaces(Type t) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(t).ImplementedInterfaces); |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetDeclaredMethod(name); |
||||
} |
||||
|
||||
public override MethodInfo[] GetMethods(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredMethods); |
||||
} |
||||
|
||||
public override Type[] GetNestedTypes(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredNestedTypes.Select(ti => ti.AsType())); |
||||
} |
||||
|
||||
public override PropertyInfo[] GetProperties(Type type) |
||||
{ |
||||
return SafeArray(GetTypeInfoFromType(type).DeclaredProperties); |
||||
} |
||||
|
||||
public override PropertyInfo GetProperty(Type type, string name) |
||||
{ |
||||
return GetTypeInfoFromType(type).GetDeclaredProperty(name); |
||||
} |
||||
|
||||
public override MethodInfo GetRemoveMethod(EventInfo ei) |
||||
{ |
||||
return ei.RemoveMethod; |
||||
} |
||||
|
||||
public override MethodInfo GetSetMethod(PropertyInfo pi) |
||||
{ |
||||
return pi.SetMethod; |
||||
} |
||||
|
||||
|
||||
public override bool IsAssignableFrom(Type current, Type toCompare) |
||||
{ |
||||
return current.GetTypeInfo().IsAssignableFrom(toCompare.GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool IsDbNull(object o) |
||||
{ |
||||
return o != null && o.GetType().FullName.StartsWith("System.DBNull"); |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
if (o == null) |
||||
return false; |
||||
|
||||
return t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
private static MethodInfo GetMethodEx(Type t, string name, Type[] parameters) |
||||
{ |
||||
var ti = t.GetTypeInfo(); |
||||
var methods = ti.GetDeclaredMethods(name); |
||||
foreach (var m in methods) |
||||
{ |
||||
var plist = m.GetParameters(); |
||||
bool match = true; |
||||
foreach (var param in plist) |
||||
{ |
||||
bool valid = true; |
||||
if (parameters != null) |
||||
{ |
||||
foreach (var ptype in parameters) |
||||
valid &= ptype == param.ParameterType; |
||||
} |
||||
match &= valid; |
||||
} |
||||
if (match) |
||||
return m; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return GetMethodEx(resourcesType, name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return SafeArray(asm.ExportedTypes); |
||||
} |
||||
|
||||
} |
||||
} |
||||
#endif |
||||
} |
||||
|
||||
public override bool IsInstanceOfType(Type t, object o) |
||||
{ |
||||
if (o == null) |
||||
return false; |
||||
|
||||
return t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()); |
||||
} |
||||
|
||||
public override bool StringContainsChar(string str, char chr) |
||||
{ |
||||
return str.Contains(chr); |
||||
} |
||||
|
||||
private static MethodInfo GetMethodEx(Type t, string name, Type[] parameters) |
||||
{ |
||||
var ti = t.GetTypeInfo(); |
||||
var methods = ti.GetDeclaredMethods(name); |
||||
foreach (var m in methods) |
||||
{ |
||||
var plist = m.GetParameters(); |
||||
bool match = true; |
||||
foreach (var param in plist) |
||||
{ |
||||
bool valid = true; |
||||
if (parameters != null) |
||||
{ |
||||
foreach (var ptype in parameters) |
||||
valid &= ptype == param.ParameterType; |
||||
} |
||||
match &= valid; |
||||
} |
||||
if (match) |
||||
return m; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
|
||||
public override MethodInfo GetMethod(Type resourcesType, string name, Type[] types) |
||||
{ |
||||
return GetMethodEx(resourcesType, name, types); |
||||
} |
||||
|
||||
public override Type[] GetAssemblyTypes(Assembly asm) |
||||
{ |
||||
return SafeArray(asm.ExportedTypes); |
||||
} |
||||
|
||||
} |
||||
} |
||||
#endif |
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,175 +1,175 @@
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using MoonSharp.Interpreter.Compatibility; |
||||
|
||||
namespace MoonSharp.Interpreter.Loaders |
||||
{ |
||||
/// <summary> |
||||
/// A script loader which can load scripts from assets in Unity3D. |
||||
/// Scripts should be saved as .txt files in a subdirectory of Assets/Resources. |
||||
/// |
||||
/// When MoonSharp is activated on Unity3D and the default script loader is used, |
||||
/// scripts should be saved as .txt files in Assets/Resources/MoonSharp/Scripts. |
||||
/// </summary> |
||||
public class UnityAssetsScriptLoader : ScriptLoaderBase |
||||
{ |
||||
Dictionary<string, string> m_Resources = new Dictionary<string, string>(); |
||||
|
||||
/// <summary> |
||||
/// The default path where scripts are meant to be stored (if not changed) |
||||
/// </summary> |
||||
public const string DEFAULT_PATH = "MoonSharp/Scripts"; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="UnityAssetsScriptLoader"/> class. |
||||
/// </summary> |
||||
/// <param name="assetsPath">The path, relative to Assets/Resources. For example |
||||
/// if your scripts are stored under Assets/Resources/Scripts, you should |
||||
/// pass the value "Scripts". If null, "MoonSharp/Scripts" is used. </param> |
||||
public UnityAssetsScriptLoader(string assetsPath = null) |
||||
{ |
||||
assetsPath = assetsPath ?? DEFAULT_PATH; |
||||
#if UNITY_5 |
||||
LoadResourcesUnityNative(assetsPath); |
||||
#else |
||||
LoadResourcesWithReflection(assetsPath); |
||||
#endif |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="UnityAssetsScriptLoader"/> class. |
||||
/// </summary> |
||||
/// <param name="scriptToCodeMap">A dictionary mapping filenames to the proper Lua script code.</param> |
||||
public UnityAssetsScriptLoader(Dictionary<string, string> scriptToCodeMap) |
||||
{ |
||||
m_Resources = scriptToCodeMap; |
||||
} |
||||
|
||||
#if UNITY_5 |
||||
void LoadResourcesUnityNative(string assetsPath) |
||||
{ |
||||
try |
||||
{ |
||||
UnityEngine.Object[] array = UnityEngine.Resources.LoadAll(assetsPath, typeof(UnityEngine.TextAsset)); |
||||
|
||||
for (int i = 0; i < array.Length; i++) |
||||
{ |
||||
UnityEngine.TextAsset o = (UnityEngine.TextAsset)array[i]; |
||||
|
||||
string name = o.name; |
||||
string text = o.text; |
||||
|
||||
m_Resources.Add(name, text); |
||||
} |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
UnityEngine.Debug.LogErrorFormat("Error initializing UnityScriptLoader : {0}", ex); |
||||
} |
||||
} |
||||
|
||||
#else |
||||
|
||||
void LoadResourcesWithReflection(string assetsPath) |
||||
{ |
||||
try |
||||
{ |
||||
Type resourcesType = Type.GetType("UnityEngine.Resources, UnityEngine"); |
||||
Type textAssetType = Type.GetType("UnityEngine.TextAsset, UnityEngine"); |
||||
|
||||
MethodInfo textAssetNameGet = Framework.Do.GetGetMethod(Framework.Do.GetProperty(textAssetType, "name")); |
||||
MethodInfo textAssetTextGet = Framework.Do.GetGetMethod(Framework.Do.GetProperty(textAssetType, "text")); |
||||
|
||||
MethodInfo loadAll = Framework.Do.GetMethod(resourcesType, "LoadAll", |
||||
new Type[] { typeof(string), typeof(Type) }); |
||||
|
||||
Array array = (Array)loadAll.Invoke(null, new object[] { assetsPath, textAssetType }); |
||||
|
||||
for (int i = 0; i < array.Length; i++) |
||||
{ |
||||
object o = array.GetValue(i); |
||||
|
||||
string name = textAssetNameGet.Invoke(o, null) as string; |
||||
string text = textAssetTextGet.Invoke(o, null) as string; |
||||
|
||||
m_Resources.Add(name, text); |
||||
} |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using MoonSharp.Interpreter.Compatibility; |
||||
|
||||
namespace MoonSharp.Interpreter.Loaders |
||||
{ |
||||
/// <summary> |
||||
/// A script loader which can load scripts from assets in Unity3D. |
||||
/// Scripts should be saved as .txt files in a subdirectory of Assets/Resources. |
||||
/// |
||||
/// When MoonSharp is activated on Unity3D and the default script loader is used, |
||||
/// scripts should be saved as .txt files in Assets/Resources/MoonSharp/Scripts. |
||||
/// </summary> |
||||
public class UnityAssetsScriptLoader : ScriptLoaderBase |
||||
{ |
||||
Dictionary<string, string> m_Resources = new Dictionary<string, string>(); |
||||
|
||||
/// <summary> |
||||
/// The default path where scripts are meant to be stored (if not changed) |
||||
/// </summary> |
||||
public const string DEFAULT_PATH = "MoonSharp/Scripts"; |
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="UnityAssetsScriptLoader"/> class. |
||||
/// </summary> |
||||
/// <param name="assetsPath">The path, relative to Assets/Resources. For example |
||||
/// if your scripts are stored under Assets/Resources/Scripts, you should |
||||
/// pass the value "Scripts". If null, "MoonSharp/Scripts" is used. </param> |
||||
public UnityAssetsScriptLoader(string assetsPath = null) |
||||
{ |
||||
assetsPath = assetsPath ?? DEFAULT_PATH; |
||||
#if UNITY_5 |
||||
LoadResourcesUnityNative(assetsPath); |
||||
#else |
||||
LoadResourcesWithReflection(assetsPath); |
||||
#endif |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Initializes a new instance of the <see cref="UnityAssetsScriptLoader"/> class. |
||||
/// </summary> |
||||
/// <param name="scriptToCodeMap">A dictionary mapping filenames to the proper Lua script code.</param> |
||||
public UnityAssetsScriptLoader(Dictionary<string, string> scriptToCodeMap) |
||||
{ |
||||
m_Resources = scriptToCodeMap; |
||||
} |
||||
|
||||
#if UNITY_5 |
||||
void LoadResourcesUnityNative(string assetsPath) |
||||
{ |
||||
try |
||||
{ |
||||
UnityEngine.Object[] array = UnityEngine.Resources.LoadAll(assetsPath, typeof(UnityEngine.TextAsset)); |
||||
|
||||
for (int i = 0; i < array.Length; i++) |
||||
{ |
||||
UnityEngine.TextAsset o = (UnityEngine.TextAsset)array[i]; |
||||
|
||||
string name = o.name; |
||||
string text = o.text; |
||||
|
||||
m_Resources.Add(name, text); |
||||
} |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
UnityEngine.Debug.LogErrorFormat("Error initializing UnityScriptLoader : {0}", ex); |
||||
} |
||||
} |
||||
|
||||
#else |
||||
|
||||
void LoadResourcesWithReflection(string assetsPath) |
||||
{ |
||||
try |
||||
{ |
||||
Type resourcesType = Type.GetType("UnityEngine.Resources, UnityEngine"); |
||||
Type textAssetType = Type.GetType("UnityEngine.TextAsset, UnityEngine"); |
||||
|
||||
MethodInfo textAssetNameGet = Framework.Do.GetGetMethod(Framework.Do.GetProperty(textAssetType, "name")); |
||||
MethodInfo textAssetTextGet = Framework.Do.GetGetMethod(Framework.Do.GetProperty(textAssetType, "text")); |
||||
|
||||
MethodInfo loadAll = Framework.Do.GetMethod(resourcesType, "LoadAll", |
||||
new Type[] { typeof(string), typeof(Type) }); |
||||
|
||||
Array array = (Array)loadAll.Invoke(null, new object[] { assetsPath, textAssetType }); |
||||
|
||||
for (int i = 0; i < array.Length; i++) |
||||
{ |
||||
object o = array.GetValue(i); |
||||
|
||||
string name = textAssetNameGet.Invoke(o, null) as string; |
||||
string text = textAssetTextGet.Invoke(o, null) as string; |
||||
|
||||
m_Resources.Add(name, text); |
||||
} |
||||
} |
||||
catch (Exception ex) |
||||
{ |
||||
#if !(PCL || ENABLE_DOTNET || NETFX_CORE) |
||||
Console.WriteLine("Error initializing UnityScriptLoader : {0}", ex); |
||||
#endif |
||||
System.Diagnostics.Debug.WriteLine(string.Format("Error initializing UnityScriptLoader : {0}", ex)); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
private string GetFileName(string filename) |
||||
{ |
||||
int b = Math.Max(filename.LastIndexOf('\\'), filename.LastIndexOf('/')); |
||||
|
||||
if (b > 0) |
||||
filename = filename.Substring(b + 1); |
||||
|
||||
return filename; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Opens a file for reading the script code. |
||||
/// It can return either a string, a byte[] or a Stream. |
||||
/// If a byte[] is returned, the content is assumed to be a serialized (dumped) bytecode. If it's a string, it's |
||||
/// assumed to be either a script or the output of a string.dump call. If a Stream, autodetection takes place. |
||||
/// </summary> |
||||
/// <param name="file">The file.</param> |
||||
/// <param name="globalContext">The global context.</param> |
||||
/// <returns> |
||||
/// A string, a byte[] or a Stream. |
||||
/// </returns> |
||||
/// <exception cref="System.Exception">UnityAssetsScriptLoader.LoadFile : Cannot load + file</exception> |
||||
public override object LoadFile(string file, Table globalContext) |
||||
{ |
||||
file = GetFileName(file); |
||||
|
||||
if (m_Resources.ContainsKey(file)) |
||||
return m_Resources[file]; |
||||
else |
||||
{ |
||||
var error = string.Format( |
||||
@"Cannot load script '{0}'. By default, scripts should be .txt files placed under a Assets/Resources/{1} directory.
|
||||
If you want scripts to be put in another directory or another way, use a custom instance of UnityAssetsScriptLoader or implement |
||||
your own IScriptLoader (possibly extending ScriptLoaderBase).", file, DEFAULT_PATH);
|
||||
|
||||
throw new Exception(error); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Checks if a given file exists |
||||
/// </summary> |
||||
/// <param name="file">The file.</param> |
||||
/// <returns></returns> |
||||
public override bool ScriptFileExists(string file) |
||||
{ |
||||
file = GetFileName(file); |
||||
return m_Resources.ContainsKey(file); |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets the list of loaded scripts filenames (useful for debugging purposes). |
||||
/// </summary> |
||||
/// <returns></returns> |
||||
public string[] GetLoadedScripts() |
||||
{ |
||||
return m_Resources.Keys.ToArray(); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
Console.WriteLine("Error initializing UnityScriptLoader : {0}", ex); |
||||
#endif |
||||
System.Diagnostics.Debug.WriteLine(string.Format("Error initializing UnityScriptLoader : {0}", ex)); |
||||
} |
||||
} |
||||
#endif |
||||
|
||||
private string GetFileName(string filename) |
||||
{ |
||||
int b = Math.Max(filename.LastIndexOf('\\'), filename.LastIndexOf('/')); |
||||
|
||||
if (b > 0) |
||||
filename = filename.Substring(b + 1); |
||||
|
||||
return filename; |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Opens a file for reading the script code. |
||||
/// It can return either a string, a byte[] or a Stream. |
||||
/// If a byte[] is returned, the content is assumed to be a serialized (dumped) bytecode. If it's a string, it's |
||||
/// assumed to be either a script or the output of a string.dump call. If a Stream, autodetection takes place. |
||||
/// </summary> |
||||
/// <param name="file">The file.</param> |
||||
/// <param name="globalContext">The global context.</param> |
||||
/// <returns> |
||||
/// A string, a byte[] or a Stream. |
||||
/// </returns> |
||||
/// <exception cref="System.Exception">UnityAssetsScriptLoader.LoadFile : Cannot load + file</exception> |
||||
public override object LoadFile(string file, Table globalContext) |
||||
{ |
||||
file = GetFileName(file); |
||||
|
||||
if (m_Resources.ContainsKey(file)) |
||||
return m_Resources[file]; |
||||
else |
||||
{ |
||||
var error = string.Format( |
||||
@"Cannot load script '{0}'. By default, scripts should be .txt files placed under a Assets/Resources/{1} directory.
|
||||
If you want scripts to be put in another directory or another way, use a custom instance of UnityAssetsScriptLoader or implement |
||||
your own IScriptLoader (possibly extending ScriptLoaderBase).", file, DEFAULT_PATH);
|
||||
|
||||
throw new Exception(error); |
||||
} |
||||
} |
||||
|
||||
/// <summary> |
||||
/// Checks if a given file exists |
||||
/// </summary> |
||||
/// <param name="file">The file.</param> |
||||
/// <returns></returns> |
||||
public override bool ScriptFileExists(string file) |
||||
{ |
||||
file = GetFileName(file); |
||||
return m_Resources.ContainsKey(file); |
||||
} |
||||
|
||||
|
||||
/// <summary> |
||||
/// Gets the list of loaded scripts filenames (useful for debugging purposes). |
||||
/// </summary> |
||||
/// <returns></returns> |
||||
public string[] GetLoadedScripts() |
||||
{ |
||||
return m_Resources.Keys.ToArray(); |
||||
} |
||||
|
||||
|
||||
|
||||
} |
||||
} |
||||
|
||||
|
Loading…
Reference in new issue