From 83a4710020bc9e254241bdce880d20bcbcec0bee Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Wed, 9 Sep 2015 12:33:45 +0200 Subject: use Interface.GetStreamFromPath to handle either file or resource in every loading places --- src/Interface.cs | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-) (limited to 'src/Interface.cs') 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) { -- cgit v1.3.1-4-g156e