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