using System;
using System.IO;
using System.Windows.Forms;
using Lib.Data.Content.Common;
using Microsoft.ClearScript;
namespace Lib.Data.Content.Types
{
[Serializable]
public class VBScript : IContent
{
public readonly Microsoft.ClearScript.Windows.VBScriptEngine Engine;
public dynamic Instance { get; set; }
public VBScript(string src)
{
Engine = new Microsoft.ClearScript.Windows.VBScriptEngine();
Engine.AddHostObject("host", new HostFunctions());
Predicate<Type> filter = type => type.IsPublic;
var typeCollection = new HostTypeCollection(filter, "mscorlib", "System.Windows.Forms");
Engine.AddHostObject("clr", typeCollection);
Engine.Execute("Dim System = clr.System");
Engine.AddHostObject("require", new Action<string>(s => Engine.Execute(File.ReadAllText(Application.StartupPath + "\\Content\\" + s))));
Engine.AddHostObject("eval", new Action<string>(s => Engine.Execute(s)));
Engine.AddHostObject("xHost", new ExtendedHostFunctions());
Engine.Execute(src);
Instance = Engine.Script;
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using Lib.Data.Content.Common;
using Lib.Data.Content.Interfaces;
using Lib.Data.Content.Serialization;
using Lib.Data.Content.Types;
namespace Lib.Data.Content
{
public class ContentManager : IConstructable
{
private string _internalContentPath = "";
/// <summary>
/// Sets or gets the base ContentPath.
/// </summary>
public string ContentPath
{
get
{
return _internalContentPath;
}
set
{
_internalContentPath = value;
Construct();
}
}
/// <summary>
/// Sets or gets the FileSystem.
/// </summary>
public IFileSystem FileSystem { set; get; }
private readonly List<IContentExtension> _extensions;
/// <summary>
/// Initializes a new ContentManager.
/// </summary>
public ContentManager()
{
FileSystem = new Win32FileSystem();
ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content");
_extensions = new List<IContentExtension>();
Construct();
}
/// <summary>
/// Destructs the ContentManager.
/// </summary>
~ContentManager()
{
FileSystem = null;
}
#region IConstructable Implementation
/// <summary>
/// Initializes the ContentManager.
/// </summary>
public void Construct()
{
if (ContentPath == "")
{
ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content");
}
if (!FileSystem.DirectoryExists(ContentPath))
{
FileSystem.CreateDirectory(ContentPath);
}
}
#endregion
#region Member
/// <summary>
/// Loads an asset into SGL.
/// </summary>
/// <typeparam name="T">The Type.</typeparam>
/// <param name="asset">The Asset.</param>
/// <returns></returns>
public T Load<T>(string asset) where T : IContent
{
if (typeof(T) == typeof(Texture))
{
using (var fileStream = FileSystem.Open(FileSystem.ConnectPath(ContentPath, asset)))
{
return (T)(Object)new TextureSerializer().Read(new BinaryReader(fileStream));
}
}
if (typeof(T) == typeof(Color))
{
using (var fileStream = FileSystem.Open(FileSystem.ConnectPath(ContentPath, asset)))
{
return (T)(Object)new ColorSerializer().Read(new BinaryReader(fileStream));
}
}
if (typeof(T) == typeof(CSScript))
{
return (T)(Object)new CSScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
}
if (typeof(T) == typeof(JScript))
{
return (T)(Object)new JScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
}
if (typeof(T) == typeof(VBScript))
{
return (T)(Object)new VBScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
}
for (var i = 0; i <= _extensions.Count - 1; i++)
{
if (_extensions[i].ContentType == null) continue;
if (_extensions[i].ContentType != typeof (T)) continue;
System.Diagnostics.Debug.WriteLine("Loaded content with IContentExtension: {0}.", _extensions[i].Guid);
return (T) _extensions[i].Create(FileSystem.ConnectPath(ContentPath, asset));
}
throw new InvalidOperationException(typeof (T).FullName + " could not be loaded.");
}
/// <summary>
/// Extends the current asset loading.
/// </summary>
/// <param name="extension">The Extension.</param>
public void Extend(IContentExtension extension)
{
_extensions.Add(extension);
}
#endregion
}
}
VBScript vbr = cm.Load<VBScript>("test.vbs");
vbr.Instance.Run();