]> O.S.I.I.S - jp/crow.git/commitdiff
use Interface.GetStreamFromPath to handle either file or resource in
authorjpbruyere <jp.bruyere@hotmail.com>
Wed, 9 Sep 2015 10:33:45 +0000 (12:33 +0200)
committerjpbruyere <jp.bruyere@hotmail.com>
Wed, 9 Sep 2015 10:33:45 +0000 (12:33 +0200)
every loading places

src/BmpPicture.cs
src/Interface.cs
src/Picture.cs
src/SvgPicture.cs
src/XCursor.cs

index 053c8fbf5d9a050345be02989a0d854230c058ce..2f6d11d693604f48b6b4bbe78131d649f91e77c0 100644 (file)
@@ -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)
                {
index 2fea548a78e2d173d980abbc3cc32def2ea5fb18..1812217b28a5f55a470f20243185178953fad2b8 100644 (file)
@@ -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)
                {
index df96733ccf9608aded637610a9e5150f1ac6a3e5..411f3eb4ad83bdba5012c7b197647ad99b712c7e 100644 (file)
@@ -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 = "");
index 9fb3b8ebaca99cd33b3892ef2af8e63e903362ad..4a0bf92272f0b87724100bccc6bb74983678b2a0 100644 (file)
@@ -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;
index 8120530e158be5a494d63cd1160e1701ca86a6c6..9c42c8daf9e1a8b05501a7d50e0cbb872c43b330 100644 (file)
@@ -46,32 +46,7 @@ namespace go
 
                public List<XCursor> Cursors = new List<XCursor>();
 
-               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<toc> tocList = new List<toc> ();
@@ -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)