From 83a4710020bc9e254241bdce880d20bcbcec0bee Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 9 Sep 2015 12:33:45 +0200 Subject: [PATCH] use Interface.GetStreamFromPath to handle either file or resource in every loading places --- src/BmpPicture.cs | 20 +------------------- src/Interface.cs | 40 ++++++++++++++++++++-------------------- src/Picture.cs | 10 +++------- src/SvgPicture.cs | 23 ++--------------------- src/XCursor.cs | 30 +----------------------------- 5 files changed, 27 insertions(+), 96 deletions(-) diff --git a/src/BmpPicture.cs b/src/BmpPicture.cs index 053c8fbf..2f6d11d6 100644 --- a/src/BmpPicture.cs +++ b/src/BmpPicture.cs @@ -32,32 +32,14 @@ namespace go {} public BmpPicture (string path) : base(path) {} - - protected override void loadFromRessource (string resId) + protected override void loadFromStream (Stream stream) { - Stream stream = null; - - //first, search for ressource in main executable assembly - stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); - if (stream == null)//try to find ressource in golib assembly - stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); - if (stream == null) - return; - using (MemoryStream ms = new MemoryStream ()) { stream.CopyTo (ms); loadBitmap (new System.Drawing.Bitmap (ms)); } } - protected override void loadFromFile (string path) - { - if (!File.Exists(path)) - return; - - loadBitmap (new System.Drawing.Bitmap (path)); - } - //load image via System.Drawing.Bitmap, cairo load png only void loadBitmap (System.Drawing.Bitmap bitmap) { diff --git a/src/Interface.cs b/src/Interface.cs index 2fea548a..1812217b 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -79,11 +79,11 @@ namespace go if (stream == null)//try to find ressource in golib assembly stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); if (stream == null) - return null; + throw new Exception ("Resource not found: " + path); } else { if (!File.Exists (path)) - return null; - stream = new FileStream (path, FileMode.Open); + throw new FileNotFoundException ("File not found: ", path); + stream = new FileStream (path, FileMode.Open, FileAccess.Read); } return stream; } @@ -92,26 +92,26 @@ namespace go { string root = "Object"; - Stream stream = GetStreamFromPath (path); - if (stream == null) - throw new FileNotFoundException ("GOML file not found", path); - - using (XmlReader reader = XmlReader.Create (stream)) { - while (reader.Read()) { - // first element is the root element - if (reader.NodeType == XmlNodeType.Element) { - root = reader.Name; - break; + using (Stream stream = GetStreamFromPath (path)) { + + #region Pre-read first node to set GraphicObject class for loading + using (XmlReader reader = XmlReader.Create (stream)) { + while (reader.Read()) { + // first element is the root element + if (reader.NodeType == XmlNodeType.Element) { + root = reader.Name; + break; + } } } - } - Type t = Type.GetType("go." + root); - //var go = Activator.CreateInstance(t); - stream.Seek(0,SeekOrigin.Begin); - GraphicObject tmp = Load(stream, t, hostClass); - stream.Dispose (); - return tmp; + Type t = Type.GetType("go." + root); + //var go = Activator.CreateInstance(t); + stream.Seek(0,SeekOrigin.Begin); + #endregion + + return Load(stream, t, hostClass); + } } static GraphicObject Load(Stream stream, Type type, object hostClass = null) { diff --git a/src/Picture.cs b/src/Picture.cs index df96733c..411f3eb4 100644 --- a/src/Picture.cs +++ b/src/Picture.cs @@ -41,16 +41,12 @@ namespace go #region Image Loading public void LoadImage (string path) { - if (path.StartsWith ("#")) - loadFromRessource (path.Substring (1)); - else - loadFromFile (path); + loadFromStream (Interface.GetStreamFromPath (path)); Path = path; } - - protected abstract void loadFromRessource(string resId); - protected abstract void loadFromFile(string path); + + protected abstract void loadFromStream(Stream stream); #endregion public abstract void Paint(Context ctx, Rectangle rect, string subPart = ""); diff --git a/src/SvgPicture.cs b/src/SvgPicture.cs index 9fb3b8eb..4a0bf922 100644 --- a/src/SvgPicture.cs +++ b/src/SvgPicture.cs @@ -33,18 +33,8 @@ namespace go {} public SvgPicture (string path) : base(path) {} - - protected override void loadFromRessource (string resId) + protected override void loadFromStream (Stream stream) { - Stream stream = null; - - //first, search for ressource in main executable assembly - stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); - if (stream == null)//try to find ressource in golib assembly - stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); - if (stream == null) - return; - using (MemoryStream ms = new MemoryStream ()) { stream.CopyTo (ms); @@ -52,16 +42,7 @@ namespace go Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); } } - - protected override void loadFromFile (string path) - { - if (!File.Exists(path)) - return; - - hSVG = new Rsvg.Handle (path); - Dimensions = new Size (hSVG.Dimensions.Width, hSVG.Dimensions.Height); - } - + public override void Paint (Cairo.Context gr, Rectangle rect, string subPart = "") { float widthRatio = (float)rect.Width / Dimensions.Width; diff --git a/src/XCursor.cs b/src/XCursor.cs index 8120530e..9c42c8da 100644 --- a/src/XCursor.cs +++ b/src/XCursor.cs @@ -46,32 +46,7 @@ namespace go public List Cursors = new List(); - static XCursorFile loadFromRessource (string resId) - { - Stream stream = null; - - //first, search for ressource in main executable assembly - stream = System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(resId); - if (stream == null)//try to find ressource in golib assembly - stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(resId); - if (stream == null) { - Debug.WriteLine ("Ressource not found: " + resId); - return null; - } - using (MemoryStream ms = new MemoryStream ()) { - stream.CopyTo (ms); - ms.Seek (0, SeekOrigin.Begin); - return loadFromStream (ms); - } - } - static XCursorFile loadFromFile (string path) - { - using (Stream s = new FileStream (path, - FileMode.Open, FileAccess.Read)) { - return loadFromStream (s); - } - } static XCursorFile loadFromStream(Stream s) { List tocList = new List (); @@ -108,10 +83,7 @@ namespace go public static XCursorFile Load(string path) { - if (path.StartsWith ("#")) - return loadFromRessource (path.Substring(1)); - else - return loadFromFile (path); + return loadFromStream (Interface.GetStreamFromPath (path)); } static XCursor imageLoad(BinaryReader sr) -- 2.47.3