Ejemplo n.º 1
0
 private static ModelSystem?Load(Stream rawStream, ModuleRepository modules, ModelSystemHeader modelSystemHeader, ref string?error)
 {
     try
     {
         var modelSystem = new ModelSystem(modelSystemHeader);
         using var stream = new MemoryStream();
         rawStream.CopyTo(stream);
         var reader     = new Utf8JsonReader(stream.GetBuffer().AsSpan());
         var typeLookup = new Dictionary <int, Type>();
         var nodes      = new Dictionary <int, Node>();
         while (reader.Read())
         {
             if (reader.TokenType == JsonTokenType.PropertyName)
             {
                 if (reader.ValueTextEquals(TypesProperty))
                 {
                     if (!LoadTypes(typeLookup, ref reader, ref error))
                     {
                         return(null);
                     }
                 }
                 else if (reader.ValueTextEquals(BoundariesProperty))
                 {
                     if (!LoadBoundaries(modules, typeLookup, nodes, ref reader, modelSystem.GlobalBoundary, ref error))
                     {
                         return(null);
                     }
                     break;
                 }
                 else
                 {
                     error = $"Unknown token found '{reader.GetString()}'";
                     return(null);
                 }
             }
         }
         return(modelSystem);
     }
     catch (JsonException e)
     {
         error = e.Message;
     }
     catch (IOException e)
     {
         error = e.Message;
     }
     return(null);
 }
Ejemplo n.º 2
0
        internal static bool Load(ProjectSession session, ModelSystemHeader modelSystemHeader, out ModelSystemSession?msSession, out CommandError?error)
        {
            // the parameters are have already been vetted
            var    path        = modelSystemHeader.ModelSystemPath;
            var    info        = new FileInfo(path);
            string?errorString = null;

            error = null;

            try
            {
                ModelSystem?ms;
                if (info.Exists)
                {
                    using var rawStream = File.OpenRead(modelSystemHeader.ModelSystemPath);
                    ms = Load(rawStream, session.GetModuleRepository(), modelSystemHeader, ref errorString);
                }
                else
                {
                    ms = new ModelSystem(modelSystemHeader);
                }
                if (ms == null)
                {
                    msSession = null;
                    // Give a generic error message if one was not already supplied.
                    error = new CommandError(errorString ?? "Unable to create a model system session for the given header.");
                    return(false);
                }
                msSession = new ModelSystemSession(session, ms);
                error     = null;
                return(true);
            }
            catch (IOException e)
            {
                error     = new CommandError(e.Message);
                msSession = null;
                return(false);
            }
        }