script


SUBMITTED BY: Guest

DATE: Jan. 20, 2014, 7:27 p.m.

FORMAT: C#

SIZE: 6.1 kB

HITS: 1190

  1. using System;
  2. using System.IO;
  3. using System.Windows.Forms;
  4. using Lib.Data.Content.Common;
  5. using Microsoft.ClearScript;
  6. namespace Lib.Data.Content.Types
  7. {
  8. [Serializable]
  9. public class VBScript : IContent
  10. {
  11. public readonly Microsoft.ClearScript.Windows.VBScriptEngine Engine;
  12. public dynamic Instance { get; set; }
  13. public VBScript(string src)
  14. {
  15. Engine = new Microsoft.ClearScript.Windows.VBScriptEngine();
  16. Engine.AddHostObject("host", new HostFunctions());
  17. Predicate<Type> filter = type => type.IsPublic;
  18. var typeCollection = new HostTypeCollection(filter, "mscorlib", "System.Windows.Forms");
  19. Engine.AddHostObject("clr", typeCollection);
  20. Engine.Execute("Dim System = clr.System");
  21. Engine.AddHostObject("require", new Action<string>(s => Engine.Execute(File.ReadAllText(Application.StartupPath + "\\Content\\" + s))));
  22. Engine.AddHostObject("eval", new Action<string>(s => Engine.Execute(s)));
  23. Engine.AddHostObject("xHost", new ExtendedHostFunctions());
  24. Engine.Execute(src);
  25. Instance = Engine.Script;
  26. }
  27. }
  28. }
  29. using System;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using Lib.Data.Content.Common;
  33. using Lib.Data.Content.Interfaces;
  34. using Lib.Data.Content.Serialization;
  35. using Lib.Data.Content.Types;
  36. namespace Lib.Data.Content
  37. {
  38. public class ContentManager : IConstructable
  39. {
  40. private string _internalContentPath = "";
  41. /// <summary>
  42. /// Sets or gets the base ContentPath.
  43. /// </summary>
  44. public string ContentPath
  45. {
  46. get
  47. {
  48. return _internalContentPath;
  49. }
  50. set
  51. {
  52. _internalContentPath = value;
  53. Construct();
  54. }
  55. }
  56. /// <summary>
  57. /// Sets or gets the FileSystem.
  58. /// </summary>
  59. public IFileSystem FileSystem { set; get; }
  60. private readonly List<IContentExtension> _extensions;
  61. /// <summary>
  62. /// Initializes a new ContentManager.
  63. /// </summary>
  64. public ContentManager()
  65. {
  66. FileSystem = new Win32FileSystem();
  67. ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content");
  68. _extensions = new List<IContentExtension>();
  69. Construct();
  70. }
  71. /// <summary>
  72. /// Destructs the ContentManager.
  73. /// </summary>
  74. ~ContentManager()
  75. {
  76. FileSystem = null;
  77. }
  78. #region IConstructable Implementation
  79. /// <summary>
  80. /// Initializes the ContentManager.
  81. /// </summary>
  82. public void Construct()
  83. {
  84. if (ContentPath == "")
  85. {
  86. ContentPath = FileSystem.ConnectPath(Environment.CurrentDirectory, "Content");
  87. }
  88. if (!FileSystem.DirectoryExists(ContentPath))
  89. {
  90. FileSystem.CreateDirectory(ContentPath);
  91. }
  92. }
  93. #endregion
  94. #region Member
  95. /// <summary>
  96. /// Loads an asset into SGL.
  97. /// </summary>
  98. /// <typeparam name="T">The Type.</typeparam>
  99. /// <param name="asset">The Asset.</param>
  100. /// <returns></returns>
  101. public T Load<T>(string asset) where T : IContent
  102. {
  103. if (typeof(T) == typeof(Texture))
  104. {
  105. using (var fileStream = FileSystem.Open(FileSystem.ConnectPath(ContentPath, asset)))
  106. {
  107. return (T)(Object)new TextureSerializer().Read(new BinaryReader(fileStream));
  108. }
  109. }
  110. if (typeof(T) == typeof(Color))
  111. {
  112. using (var fileStream = FileSystem.Open(FileSystem.ConnectPath(ContentPath, asset)))
  113. {
  114. return (T)(Object)new ColorSerializer().Read(new BinaryReader(fileStream));
  115. }
  116. }
  117. if (typeof(T) == typeof(CSScript))
  118. {
  119. return (T)(Object)new CSScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
  120. }
  121. if (typeof(T) == typeof(JScript))
  122. {
  123. return (T)(Object)new JScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
  124. }
  125. if (typeof(T) == typeof(VBScript))
  126. {
  127. return (T)(Object)new VBScript(File.ReadAllText(FileSystem.ConnectPath(ContentPath, asset)));
  128. }
  129. for (var i = 0; i <= _extensions.Count - 1; i++)
  130. {
  131. if (_extensions[i].ContentType == null) continue;
  132. if (_extensions[i].ContentType != typeof (T)) continue;
  133. System.Diagnostics.Debug.WriteLine("Loaded content with IContentExtension: {0}.", _extensions[i].Guid);
  134. return (T) _extensions[i].Create(FileSystem.ConnectPath(ContentPath, asset));
  135. }
  136. throw new InvalidOperationException(typeof (T).FullName + " could not be loaded.");
  137. }
  138. /// <summary>
  139. /// Extends the current asset loading.
  140. /// </summary>
  141. /// <param name="extension">The Extension.</param>
  142. public void Extend(IContentExtension extension)
  143. {
  144. _extensions.Add(extension);
  145. }
  146. #endregion
  147. }
  148. }
  149. VBScript vbr = cm.Load<VBScript>("test.vbs");
  150. vbr.Instance.Run();

comments powered by Disqus