private static int Disasm(SDat sdat, string name)
        {
            if (Regex.IsMatch(name, @"^[0-9]+$"))
            {
                return(Disasm(sdat, int.Parse(name)));
            }
            try {
                var sseq = sdat.OpenSequence(name);
                var ser  = new SequenceSerializer();
                Console.Write(ser.Serialize(sseq.sequence));
                return(ERR_OK);
            } catch (FileNotFoundException) {
                //just swallow this one
            }

            try {
                var strm = sdat.OpenStream(name);
                Console.WriteLine("Is stream.");
                return(ERR_IS_STREAM);
            } catch (FileNotFoundException) {
                //keep on ignoring missing files
            }

            return(ERR_SEQ_NOT_FOUND);
        }
        private static void ScanFile(Stream fileStream)
        {
            NDS nds = null;

            try {
                nds = new NDS(fileStream);
            } catch (Exception) {
                Console.WriteLine("NDS parsing failed.");
                return;
            }

            var ser = new SequenceSerializer();

            var sdatFiles = nds.FileSystem.RootDir.FindMatchingFiles("*.sdat");

            foreach (var sdatFile in sdatFiles)
            {
                SDat sdat;
                try {
                    sdat = SDat.Open(nds.FileSystem.OpenFile(sdatFile));
                } catch (InvalidDataException) {
                    Console.Out.WriteLine($"{sdatFile.AbsPath} Fail");
                    continue;                    //if this isn't even a valid sdat, no need to try the sequence parser on it
                }
                Console.Out.WriteLine($"{sdatFile.AbsPath} Loaded");

                for (int sequenceIndex = 0; sequenceIndex < sdat.sequenceInfo.Count; ++sequenceIndex)
                {
                    if (sdat.sequenceInfo[sequenceIndex] == null)
                    {
                        continue;
                    }

                    string seqName;
                    if (sdat.seqSymbols != null && sdat.seqSymbols[sequenceIndex] != null)
                    {
                        seqName = $"# {sequenceIndex} {sdat.seqSymbols[sequenceIndex]}";
                    }
                    else
                    {
                        seqName = $"# {sequenceIndex}";
                    }

                    SSEQ sseq = null;
                    try {
                        sseq = sdat.OpenSequence(sequenceIndex);
                    } catch (Exception) {
                        Console.WriteLine($"Sequence {seqName} failed to parse.");
                    }

                    try {
                        ser.Clear();
                        ser.Serialize(sseq.sequence);
                    } catch (Exception) {
                        Console.WriteLine($"Sequence {seqName} failed to serialize.");
                    }
                }
            }
        }
 private static int Disasm(SDat sdat, int index)
 {
     try {
         var sseq = sdat.OpenSequence(index);
         var ser  = new SequenceSerializer();
         Console.Write(ser.Serialize(sseq.sequence));
         return(ERR_OK);
     } catch (FileNotFoundException) {
         return(ERR_SEQ_NOT_FOUND);
     }
 }
Esempio n. 4
0
        public void Serialize(ISequenceGroup sequenceGroup, SerializationTarget target, params string[] param)
        {
            switch (target)
            {
            case SerializationTarget.File:
                _typeMaintainer.VerifyVariableTypes(sequenceGroup);
                _typeMaintainer.RefreshUsedAssemblyAndType(sequenceGroup);
                SequenceSerializer.Serialize(param[0], sequenceGroup as SequenceGroup);
                break;

            case SerializationTarget.DataBase:
                throw new NotImplementedException();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(target), target, null);
            }
        }
Esempio n. 5
0
        public void Serialize(ITestProject testProject, SerializationTarget target, params string[] param)
        {
            switch (target)
            {
            case SerializationTarget.File:
                _typeMaintainer.VerifyVariableTypes(testProject);
                _typeMaintainer.RefreshUsedAssemblyAndType(testProject);
                _directoryHelper.SetToRelativePath(testProject);
                SequenceSerializer.Serialize(param[0], testProject as TestProject);
                _directoryHelper.SetToAbsolutePath(testProject);
                break;

            case SerializationTarget.DataBase:
                throw new NotImplementedException();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(target), target, null);
            }
        }
Esempio n. 6
0
        public void Serialize(ISequenceGroup sequenceGroup, SerializationTarget target, params string[] param)
        {
            switch (target)
            {
            case SerializationTarget.File:
                string filePath = param[0];
//                    filePath = ModuleUtils.GetAbsolutePath(filePath, ConfigData.GetProperty<string[]>("WorkspaceDir")[0]);
                _typeMaintainer.VerifyVariableTypes(sequenceGroup);
                _typeMaintainer.RefreshUsedAssemblyAndType(sequenceGroup);
                _directoryHelper.SetToRelativePath(sequenceGroup);
                SequenceSerializer.Serialize(filePath, sequenceGroup as SequenceGroup);
                _directoryHelper.SetToAbsolutePath(sequenceGroup);
                break;

            case SerializationTarget.DataBase:
                throw new NotImplementedException();
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(target), target, null);
            }
        }
Esempio n. 7
0
 public string RuntimeSerialize(ISequenceGroup sequenceGroup)
 {
     _typeMaintainer.VerifyVariableTypes(sequenceGroup);
     _typeMaintainer.RefreshUsedAssemblyAndType(sequenceGroup);
     return(SequenceSerializer.ToJson(sequenceGroup as SequenceGroup));
 }
Esempio n. 8
0
 public string RuntimeSerialize(ITestProject testProject)
 {
     _typeMaintainer.VerifyVariableTypes(testProject);
     _typeMaintainer.RefreshUsedAssemblyAndType(testProject);
     return(SequenceSerializer.ToJson(testProject as TestProject));
 }
Esempio n. 9
0
        public ProvisioningHierarchy ToProvisioningHierarchy(Stream hierarchy)
        {
            // Create a copy of the source stream
            MemoryStream sourceStream = new MemoryStream();

            hierarchy.Position = 0;
            hierarchy.CopyTo(sourceStream);
            sourceStream.Position = 0;

            // Check the provided template against the XML schema
            if (!this.IsValid(sourceStream))
            {
                // TODO: Use resource file
                throw new ApplicationException("The provided provisioning file is not valid!");
            }

            // Prepare the output variable
            ProvisioningHierarchy resultHierarchy = new ProvisioningHierarchy();

            // Determine if the file is a provisioning hierarchy
            sourceStream.Position = 0;
            XDocument xml = XDocument.Load(sourceStream);

            if (xml.Root.Name.LocalName != "Provisioning")
            {
                throw new ApplicationException("The provided provisioning file is not a Hierarchy!");
            }

            // Determine the specific formatter needed for the current provisioning file
            var innerFormatter = XMLPnPSchemaFormatter.GetSpecificFormatter(
                xml.Root.Name.NamespaceName);

            // Process all the provisioning templates included in the hierarchy, if any
            XmlNamespaceManager nsManager = new XmlNamespaceManager(new System.Xml.NameTable());

            nsManager.AddNamespace("pnp", xml.Root.Name.NamespaceName);

            // Start with templates embedded in the provisioning file
            var templates = xml.XPathSelectElements("/pnp:Provisioning/pnp:Templates/pnp:ProvisioningTemplate", nsManager).ToList();

            foreach (var template in templates)
            {
                // Save the single template into a MemoryStream
                MemoryStream templateStream = new MemoryStream();
                template.Save(templateStream);
                templateStream.Position = 0;

                // Process the single template with the classic technique
                var provisioningTemplate = innerFormatter.ToProvisioningTemplate(templateStream);

                // Add the generated template to the resulting hierarchy
                resultHierarchy.Templates.Add(provisioningTemplate);
            }

            // Then process any external file reference
            var templateFiles = xml.XPathSelectElements("/pnp:Provisioning/pnp:Templates/pnp:ProvisioningTemplateFile", nsManager).ToList();

            foreach (var template in templateFiles)
            {
                var templateID   = template.Attribute("ID")?.Value;
                var templateFile = template.Attribute("File")?.Value;
                if (!String.IsNullOrEmpty(templateFile) && !String.IsNullOrEmpty(templateID))
                {
                    // Process the single template file with the classic technique
                    var provisioningTemplate = this._provider.GetTemplate(templateFile);
                    provisioningTemplate.Id = templateID;

                    // Add the generated template to the resulting hierarchy
                    resultHierarchy.Templates.Add(provisioningTemplate);
                }
            }

            // And now process the top level children elements
            // using schema specific serializers

            using (var scope = new PnPSerializationScope(typeof(TSchemaTemplate)))
            {
                // We prepare a dummy template to leverage the existing serialization infrastructure
                var dummyTemplate = new ProvisioningTemplate();
                dummyTemplate.Id = $"DUMMY-{Guid.NewGuid()}";
                resultHierarchy.Templates.Add(dummyTemplate);

                // Deserialize the whole wrapper
                Object        wrapper       = null;
                var           wrapperType   = Type.GetType($"{PnPSerializationScope.Current?.BaseSchemaNamespace}.Provisioning, {PnPSerializationScope.Current?.BaseSchemaAssemblyName}", true);
                XmlSerializer xmlSerializer = new XmlSerializer(wrapperType);
                using (var reader = xml.Root.CreateReader())
                {
                    wrapper = xmlSerializer.Deserialize(reader);
                }

                // Handle the Parameters of the schema wrapper, if any
                var tps = new TemplateParametersSerializer();
                tps.Deserialize(wrapper, dummyTemplate);

                // Handle the Localizations of the schema wrapper, if any
                var ls = new LocalizationsSerializer();
                ls.Deserialize(wrapper, dummyTemplate);

                // Handle the Tenant-wide settings of the schema wrapper, if any
                var ts = new TenantSerializer();
                ts.Deserialize(wrapper, dummyTemplate);

                // Handle the Sequences
                var ss = new SequenceSerializer();
                ss.Deserialize(wrapper, dummyTemplate);

                // Handle the Provisioning Hierarchy properties
                var phs = new ProvisioningHierarchySerializer();
                phs.Deserialize(wrapper, dummyTemplate);

                // Remove the dummy template from the hierarchy
                resultHierarchy.Templates.Remove(dummyTemplate);
            }

            return(resultHierarchy);
        }
Esempio n. 10
0
        public Stream ToFormattedHierarchy(ProvisioningHierarchy hierarchy)
        {
            if (hierarchy == null)
            {
                throw new ArgumentNullException(nameof(hierarchy));
            }

            using (var scope = new PnPSerializationScope(typeof(TSchemaTemplate)))
            {
                // We prepare a dummy template to leverage the existing deserialization infrastructure
                var dummyTemplate = new ProvisioningTemplate();
                dummyTemplate.Id = $"DUMMY-{Guid.NewGuid()}";
                hierarchy.Templates.Add(dummyTemplate);

                // Prepare the output wrapper
                Type   wrapperType;
                object wrapper, templatesItem;
                Array  templates;

                ProcessOutputHierarchy(dummyTemplate, out wrapperType, out wrapper, out templates, out templatesItem);

                // Handle the Sequences, if any
                var ts = new SequenceSerializer();
                ts.Serialize(dummyTemplate, wrapper);

                // Remove the dummy template
                hierarchy.Templates.Remove(dummyTemplate);

                // Add every single template to the output
                var provisioningTemplates = Array.CreateInstance(typeof(TSchemaTemplate), hierarchy.Templates.Count);
                for (int c = 0; c < hierarchy.Templates.Count; c++)
                {
                    // Prepare variable to hold the output template
                    var outputTemplate = new TSchemaTemplate();

                    // Serialize the real templates
                    SerializeTemplate(hierarchy.Templates[c], outputTemplate);

                    // Add the serialized template to the output
                    provisioningTemplates.SetValue(outputTemplate, c);
                }

                templatesItem.GetType().GetProperty("ProvisioningTemplate",
                                                    System.Reflection.BindingFlags.Instance |
                                                    System.Reflection.BindingFlags.Public |
                                                    System.Reflection.BindingFlags.IgnoreCase).SetValue(templatesItem, provisioningTemplates);

                templates.SetValue(templatesItem, 0);

                wrapperType.GetProperty("Templates",
                                        System.Reflection.BindingFlags.Instance |
                                        System.Reflection.BindingFlags.Public |
                                        System.Reflection.BindingFlags.IgnoreCase).SetValue(wrapper, templates);

                XmlSerializerNamespaces ns =
                    new XmlSerializerNamespaces();
                ns.Add(((IXMLSchemaFormatter)this).NamespacePrefix,
                       ((IXMLSchemaFormatter)this).NamespaceUri);

                MemoryStream  output        = new MemoryStream();
                XmlSerializer xmlSerializer = new XmlSerializer(wrapperType);
                if (ns != null)
                {
                    xmlSerializer.Serialize(output, wrapper, ns);
                }
                else
                {
                    xmlSerializer.Serialize(output, wrapper);
                }

                output.Position = 0;
                return(output);
            }
        }