Browse Source

Fully decouple asset loading

Bepis 6 years ago
parent
commit
465a65f126

+ 5 - 1
NTERA.Core/IScriptEngine.cs

@@ -1,4 +1,6 @@
-namespace NTERA.Core
+using NTERA.EmuEra.Game.EraEmu.Content;
+
+namespace NTERA.Core
 {
 	public interface IScriptEngine
 	{
@@ -8,5 +10,7 @@
 		void InputString(string input);
 		void InputInteger(long input);
 		void InputSystemInteger(long input);
+
+		CroppedImage GetImage(string name);
 	}
 }

+ 10 - 6
NTERA.EmuEra/Game/EraEmu/Content/CroppedImage.cs → NTERA.Core/Interop/CroppedImage.cs

@@ -2,19 +2,23 @@ using System.Drawing;
 
 namespace NTERA.EmuEra.Game.EraEmu.Content
 {
-	internal sealed class CroppedImage : AContentItem
+	public class CroppedImage
 	{
-		public CroppedImage(string name, BaseImage image, Rectangle rect, bool noresize) : base(name)
+		public readonly string Name;
+		public bool Enabled = false;
+
+		public CroppedImage(string name, Bitmap image, Rectangle rect, bool noresize)
 		{
-			BaseImage = image;
+			Name = name;
+
+			Bitmap = image;
 			Rectangle = rect;
-			if (image != null)
-				Enabled = image.Enabled;
+
 			if (rect.Width <= 0 || rect.Height <= 0)
 				Enabled = false;
 			NoResize = noresize;
 		}
-		public readonly BaseImage BaseImage;
+		public readonly Bitmap Bitmap;
 		public readonly Rectangle Rectangle;
 		public readonly bool NoResize;
 	}

+ 1 - 0
NTERA.Core/NTERA.Core.csproj

@@ -42,6 +42,7 @@
     <Reference Include="System.Xml" />
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Interop\CroppedImage.cs" />
     <Compile Include="Interop\DataStructs.cs" />
     <Compile Include="GameInstance.cs" />
     <Compile Include="IConsole.cs" />

+ 6 - 0
NTERA.EmuEra/EmuEraGameInstance.cs

@@ -1,6 +1,7 @@
 using System.IO;
 using NTERA.Core;
 using NTERA.EmuEra.Game;
+using NTERA.EmuEra.Game.EraEmu.Content;
 using NTERA.EmuEra.Game.EraEmu.GameProc;
 
 namespace NTERA.EmuEra
@@ -40,5 +41,10 @@ namespace NTERA.EmuEra
 		{
 			emuera.InputSystemInteger(input);
 		}
+
+		public CroppedImage GetImage(string name)
+		{
+			return AppContents.GetContent(name);
+		}
 	}
 }

+ 0 - 9
NTERA.EmuEra/Game/EraEmu/Content/AContentItem.cs

@@ -1,9 +0,0 @@
-namespace NTERA.EmuEra.Game.EraEmu.Content
-{
-	abstract class AContentItem
-	{
-		protected AContentItem(string name) { Name = name; }
-		public readonly string Name;
-		public bool Enabled = false;
-	}
-}

+ 6 - 6
NTERA.EmuEra/Game/EraEmu/Content/AppContents.cs

@@ -8,14 +8,14 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 {
 	static class AppContents
 	{
-		public static T GetContent<T>(string name) where T : AContentItem
+		public static CroppedImage GetContent(string name)
 		{
 			if (name == null)
 				return null;
 			name = name.ToUpper();
 			if (!itemDic.ContainsKey(name))
 				return null;
-			return itemDic[name] as T;
+			return itemDic[name];
 		}
 
 		public static void LoadContents()
@@ -46,7 +46,7 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 						if (str.Length == 0 || str.StartsWith(";"))
 							continue;
 						string[] tokens = str.Split(',');
-						AContentItem item = CreateFromCsv(tokens);
+						CroppedImage item = CreateFromCsv(tokens);
 						if (item != null && !itemDic.ContainsKey(item.Name))
 							itemDic.Add(item.Name, item);
 					}
@@ -66,7 +66,7 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 			itemDic.Clear();
 		}
 
-		private static AContentItem CreateFromCsv(string[] tokens)
+		private static CroppedImage CreateFromCsv(string[] tokens)
 		{
 			if(tokens.Length < 2)
 				return null;
@@ -108,7 +108,7 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 						}
 					}
 				}
-				CroppedImage image = new CroppedImage(name, parentImage, rect, noresize);
+				CroppedImage image = new CroppedImage(name, parentImage.Bitmap, rect, noresize);
 				return image;
 			}
 			return null;
@@ -116,7 +116,7 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 
 
 		static Dictionary<string, AContentFile> resourceDic = new Dictionary<string, AContentFile>();
-		static Dictionary<string, AContentItem> itemDic = new Dictionary<string, AContentItem>();
+		static Dictionary<string, CroppedImage> itemDic = new Dictionary<string, CroppedImage>();
 
 	}
 }

+ 1 - 6
NTERA.EmuEra/Game/EraEmu/Content/BaseImage.cs

@@ -8,7 +8,6 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 			: base(name, path)
 		{ }
 		public Bitmap Bitmap;
-		Graphics g;
 		public void Load(bool useGDI)
 		{
 			if (Loaded)
@@ -36,11 +35,7 @@ namespace NTERA.EmuEra.Game.EraEmu.Content
 		{
 			if (Bitmap == null)
 				return;
-			if (g != null)
-			{
-				g.Dispose();
-				g = null;
-			}
+
 			Bitmap.Dispose();
 			Bitmap = null;
 		}

+ 2 - 3
NTERA.EmuEra/NTERA.EmuEra.csproj

@@ -9,8 +9,9 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>NTERA.EmuEra</RootNamespace>
     <AssemblyName>NTERA.EmuEra</AssemblyName>
-    <TargetFrameworkVersion>v4.7.1</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
+    <TargetFrameworkProfile />
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -46,10 +47,8 @@
     <Compile Include="Game\EraEmu\Config\ConfigItem.cs" />
     <Compile Include="Game\EraEmu\Config\KeyMacro.cs" />
     <Compile Include="Game\EraEmu\Content\AContentFile.cs" />
-    <Compile Include="Game\EraEmu\Content\AContentItem.cs" />
     <Compile Include="Game\EraEmu\Content\AppContents.cs" />
     <Compile Include="Game\EraEmu\Content\BaseImage.cs" />
-    <Compile Include="Game\EraEmu\Content\CroppedImage.cs" />
     <Compile Include="Game\EraEmu\Display\HTMLManager.cs" />
     <Compile Include="EmuEraGameInstance.cs" />
     <Compile Include="Game\EraEmu\GameData\ConstantData.cs" />

+ 6 - 4
NTERA/Console/EraConsoleInstance.cs

@@ -10,6 +10,7 @@ namespace NTERA.Console
 	public class EraConsoleInstance : IConsole
 	{
 		public ConsoleRenderer Renderer { get; }
+		public IScriptEngine ScriptEngine { get; }
 
 		public AutoResetEvent InputResetEvent { get; protected set; } = new AutoResetEvent(false);
 
@@ -36,9 +37,10 @@ namespace NTERA.Console
 
 
 
-		public EraConsoleInstance(ConsoleRenderer renderer)
+		public EraConsoleInstance(ConsoleRenderer renderer, IScriptEngine scriptEngine)
 		{
 			Renderer = renderer;
+			ScriptEngine = scriptEngine;
 		}
 
 		public InputRequest CurrentRequest { get; set; }
@@ -232,15 +234,15 @@ namespace NTERA.Console
 
 		public void PrintHtml(string html, bool newline)
 		{
-			foreach (var item in HtmlParser.ParseHtml(html))
+			foreach (var item in HtmlParser.ParseHtml(html, ScriptEngine.GetImage))
 				Renderer.PrintItem(item);
 		}
 
 		public void PrintImg(string img)
 		{
-			var image = AppContents.GetContent<CroppedImage>(img);
+			var image = ScriptEngine.GetImage(img);
 
-			Renderer.WriteItem(new ImageRenderItem(image.BaseImage.Bitmap, image.Rectangle, alignment: Alignment));
+			Renderer.WriteItem(new ImageRenderItem(image.Bitmap, image.Rectangle, alignment: Alignment));
 		}
 
 		public void PrintShape(string shape, int[] param)

+ 10 - 8
NTERA/Console/HTMLParser.cs

@@ -4,13 +4,15 @@ using System.Drawing;
 using System.Text.RegularExpressions;
 using System.Xml.Linq;
 using NTERA.Console.RenderItem;
+using NTERA.Core;
 using NTERA.Core.Interop;
+using NTERA.EmuEra.Game.EraEmu.Content;
 
 namespace NTERA.Console
 {
 	public static class HtmlParser
 	{
-		public static IEnumerable<IRenderItem> ParseHtml(string html)
+		public static IEnumerable<IRenderItem> ParseHtml(string html, Func<string, CroppedImage> assetFunc)
 		{
 			//fix broken HTML from shitty emuera format
 			string fixedHtml = Regex.Replace(html, @"<img([^\/]*?)>", "<img$1 />");
@@ -19,10 +21,10 @@ namespace NTERA.Console
 
 			var element = XElement.Parse($"<parent>{fixedHtml}</parent>");
 
-			return ParseHtml_Internal(element, HtmlStyle.Default);
+			return ParseHtml_Internal(element, assetFunc, HtmlStyle.Default);
 		}
 
-		private static IEnumerable<IRenderItem> ParseHtml_Internal(XElement xmlNode, HtmlStyle style)
+		private static IEnumerable<IRenderItem> ParseHtml_Internal(XElement xmlNode, Func<string, CroppedImage> assetFunc, HtmlStyle style)
 		{
 			List<IRenderItem> renderItems = new List<IRenderItem>();
 			HtmlStyle localStyle = (HtmlStyle)style.Clone();
@@ -37,7 +39,7 @@ namespace NTERA.Console
 						if (alignment != null)
 							localStyle.Alignment = (DisplayLineAlignment)Enum.Parse(typeof(DisplayLineAlignment), alignment.ToUpper());
 
-						renderItems.AddRange(ParseHtml_Internal(node, localStyle));
+						renderItems.AddRange(ParseHtml_Internal(node, assetFunc, localStyle));
 						break;
 
 					case "img":
@@ -46,9 +48,9 @@ namespace NTERA.Console
 						if (src == null)
 							throw new InvalidOperationException("HTML 'img' tag in script is missing 'src' attribute");
 
-						var image = AppContents.GetContent<CroppedImage>(src);
+						var image = assetFunc(src);
 
-						renderItems.Add(new ImageRenderItem(image.BaseImage.Bitmap, image.Rectangle, alignment: localStyle.Alignment));
+						renderItems.Add(new ImageRenderItem(image.Bitmap, image.Rectangle, alignment: localStyle.Alignment));
 						break;
 
 					case "br":
@@ -74,12 +76,12 @@ namespace NTERA.Console
 								localStyle.ForeColor = Color.FromName(color);
 						}
 
-						renderItems.AddRange(ParseHtml_Internal(node, localStyle));
+						renderItems.AddRange(ParseHtml_Internal(node, assetFunc, localStyle));
 						break;
 
 					case "nonbutton":
 						//renderItems.Add(new TextRenderItem(node.Value, color: localStyle.ForeColor, alignment: localStyle.Alignment));
-						renderItems.AddRange(ParseHtml_Internal(node, localStyle));
+						renderItems.AddRange(ParseHtml_Internal(node, assetFunc, localStyle));
 						break;
 
 					default:

+ 6 - 5
NTERA/formMain.cs

@@ -1,9 +1,8 @@
-using System.IO;
-using System.Threading.Tasks;
+using System.Threading.Tasks;
 using System.Windows.Forms;
 using NTERA.Console;
 using NTERA.Core;
-using NTERA.Game.EraEmu;
+using NTERA.EmuEra;
 
 namespace NTERA
 {
@@ -15,7 +14,9 @@ namespace NTERA
 		{
 			InitializeComponent();
 
-			Task.Factory.StartNew(() => instance.Run(new EraConsoleInstance(consoleControl1.Renderer), new EmuEraGameInstance()));
+			var scriptEngine = new EmuEraGameInstance();
+
+			Task.Factory.StartNew(() => instance.Run(new EraConsoleInstance(consoleControl1.Renderer, scriptEngine), scriptEngine));
 		}
 
 		private void txtInput_KeyDown(object sender, KeyEventArgs e)
@@ -30,4 +31,4 @@ namespace NTERA
 			}
 		}
 	}
-}
+}