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 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(s => Engine.Execute(File.ReadAllText(Application.StartupPath + "\\Content\\" + s)))); Engine.AddHostObject("eval", new Action(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 = ""; /// /// Sets or gets the base ContentPath. /// public string ContentPath { get { return _internalContentPath; } set { _internalContentPath = value; Construct(); } } /// /// Sets or gets the FileSystem. /// public IFileSystem FileSystem { set; get; } private readonly List _extensions; /// /// Initializes a new ContentManager. /// public ContentManager() { FileSystem = new Win32FileSystem(); ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content"); _extensions = new List(); Construct(); } /// /// Destructs the ContentManager. /// ~ContentManager() { FileSystem = null; } #region IConstructable Implementation /// /// Initializes the ContentManager. /// public void Construct() { if (ContentPath == "") { ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content"); } if (!FileSystem.DirectoryExists(ContentPath)) { FileSystem.CreateDirectory(ContentPath); } } #endregion #region Member /// /// Loads an asset into SGL. /// /// The Type. /// The Asset. /// public T Load(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."); } /// /// Extends the current asset loading. /// /// The Extension. public void Extend(IContentExtension extension) { _extensions.Add(extension); } #endregion } } VBScript vbr = cm.Load("test.vbs"); vbr.Instance.Run();