Esempio n. 1
0
        public static bool ReadProjectFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                return(false);
            }

            FileStream   fs = new FileStream(fileName, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            var fileData = sr.ReadToEnd();

            sr.Close();
            fs.Close();

            try
            {
                var yamlSerialzer = new SharpYaml.Serialization.Serializer();
                projectFile = yamlSerialzer.Deserialize <ProjectFile>(fileData);
            }
            catch (Exception e)
            {
                string errorMessage = string.Format("There was an error while reading the project file.\r\n\r\n{0}", e.Message);
                MessageBox.Show(errorMessage);
                return(false);
            }

            ImageHandler.initalized = false; // necessary for if the rootDir changes, a bit weird to struture the program this way. think about changing it. (update: just found a weird bug related to ImageHandler initialization, definitely needs changed)

            return(true);
        }
Esempio n. 2
0
 static int Main(string[] args)
 {
     SharpYaml.Serialization.Serializer serializer = new SharpYaml.Serialization.Serializer();
     if (args.Length < 1)
     {
         Console.Error.WriteLine("You must provide an input YAML file!");
         Console.In.Read();
         return(1);
     }
     try
     {
         GameDefinition def = serializer.Deserialize(File.ReadAllText(args[0]), typeof(GameDefinition)) as GameDefinition;
         def.ParseTypes();
         string baseDir = Directory.GetCurrentDirectory();
         if (args.Length > 1)
         {
             baseDir = args[1];
         }
         cxx.CxxHelper.GenerateFiles(Path.Combine(baseDir, "cxx"), def);
         cxx.CxxHelper.GenerateServerFiles(Path.Combine(baseDir, "server"), def);
         cs.CsHelper.GenerateFiles(Path.Combine(baseDir, "cs"), def);
         return(0);
     }
     catch (Exception e)
     {
         Console.Error.WriteLine(e);
         Console.In.Read();
         return(1);
     }
 }
Esempio n. 3
0
        private void ConfigureMicroservice(IHostingEnvironment env)
        {
            var serializer = new SharpYaml.Serialization.Serializer();

            ApplicationComposition input;

            using (var fileStream = new FileStream(Path.Combine(env.ContentRootPath, "core.yml"), FileMode.Open, FileAccess.Read))
            {
                input = (ApplicationComposition)serializer.Deserialize(fileStream);
            }

            new Bootstrapper().Startup(input);
        }
Esempio n. 4
0
        private void SaveLastOpened()
        {
            FileStream   fs = new FileStream("last.yaml", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            var yamlSerialzer = new SharpYaml.Serialization.Serializer();

            var serialzedData = yamlSerialzer.Serialize(initalizationData);

            sw.Write(serialzedData);

            sw.Close();
            fs.Close();
        }
Esempio n. 5
0
        private Dictionary <object, object> LoadHybrasylMapYamlFile(string filename)
        {
            Dictionary <object, object> hybrasylMap = null;

            if (!string.IsNullOrEmpty(filename) &&
                !string.IsNullOrEmpty(Configuration.Current.HybrasylWorldDirectory))
            {
                string hybrasylMapData = File.ReadAllText(Path.Combine(Configuration.Current.HybrasylWorldDirectory, "maps", filename));

                var s = new SharpYaml.Serialization.Serializer();
                hybrasylMap = s.Deserialize(hybrasylMapData) as Dictionary <object, object>;
            }

            return(hybrasylMap);
        }
Esempio n. 6
0
        public bool DeserializeFromDisk(string targetFile)
        {
            if (!File.Exists(targetFile))
            {
                string errorMessage = string.Format("File {0} did not exist.", targetFile);
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            var        yamlSerializer = new SharpYaml.Serialization.Serializer();
            FileStream fs;



            try
            {
                fs = new FileStream(targetFile, FileMode.Open);
            }
            catch (Exception e)
            {
                string errorMessage = string.Format("There was an error when opening file {0}.\r\n\r\n{1}.", targetFile, e.Message);
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            StreamReader sr   = new StreamReader(fs);
            string       data = sr.ReadToEnd();

            files = new Dictionary <uint, CPKEmbeddedFileMeta>();

            try
            {
                yamlSerializer.DeserializeInto <CPKBuildObject>(data, this);
            }
            catch (Exception e)
            {
                string errorMessage = string.Format("There was an error when parsing file {0}.\r\n\r\n{1}.", targetFile, e.Message);
                MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return(false);
            }

            // might want to specifically check all fields for being null here even if it is a very unlikely edge case

            sr.Close();
            fs.Close();

            return(true);
        }
Esempio n. 7
0
        private static void WriteProjectFile()
        {
            string fileName = Path.Combine(rootDir, projectFileName);

            FileStream   fs = new FileStream(fileName, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            var yamlSerialzer = new SharpYaml.Serialization.Serializer();

            var serialzedData = yamlSerialzer.Serialize(projectFile);

            sw.Write(serialzedData);

            sw.Close();
            fs.Close();
        }
Esempio n. 8
0
        public void Serialize()
        {
            var config = new ConfigurationDefinition();

            config.Info = "Test data";
            config.Map  = new List <RequestConfigurationDefinition>
            {
                new RequestConfigurationDefinition
                {
                    Url         = "/probe",
                    Status      = 200,
                    Delay       = 100,
                    Description = "Probe endpoint"
                },
                new RequestConfigurationDefinition
                {
                    Url         = "/swagger",
                    Status      = 200,
                    Delay       = 200,
                    Description = "Swagger endpoint"
                },
                new RequestConfigurationDefinition
                {
                    Url         = "/order",
                    Status      = 201,
                    Delay       = 300,
                    Description = "Order endpoint",
                    Payload     = "{\"paymentId\":\"@guid\"}",
                    Headers     = new Dictionary <string, string>
                    {
                        { "Location", "/probe?a=b" },
                        { "Authorization", "Bearer aaa" }
                    }
                }
            };

            var serializer = new SharpYaml.Serialization.Serializer(new SharpYaml.Serialization.SerializerSettings
            {
                EmitAlias         = false,
                EmitShortTypeName = false,
                DefaultStyle      = SharpYaml.YamlStyle.Block,
            });

            var result = serializer.Serialize(config);

            result.Should().NotBeNull();
        }
Esempio n. 9
0
        public void SerializeToDisk(string targetFile)
        {
            var yamlSerialzer  = new SharpYaml.Serialization.Serializer();
            var serializedData = yamlSerialzer.Serialize(this);

            string targetPath = Path.Combine(targetFile, GenerateCPKID() + ".yaml");

            DirectoryGuard.CheckDirectory(targetPath);

            FileStream   fs = new FileStream(targetPath, FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);

            sw.Write(serializedData);

            sw.Close();
            fs.Close();
        }
Esempio n. 10
0
        public bool DeserializeFromDisk(string targetFile)
        {
            {
                if (!File.Exists(targetFile))
                {
                    string errorMessage = string.Format("File {0} did not exist.", targetFile);
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                var        yamlSerializer = new SharpYaml.Serialization.Serializer();
                FileStream fs;

                try
                {
                    fs = new FileStream(targetFile, FileMode.Open);
                }
                catch (Exception e)
                {
                    string errorMessage = string.Format("There was an error when opening file {0}.\r\n\r\n{1}.", targetFile, e.Message);
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                StreamReader sr   = new StreamReader(fs);
                string       data = sr.ReadToEnd();

                try
                {
                    yamlSerializer.DeserializeInto <GIMBuildObject>(data, this);
                }
                catch (Exception e)
                {
                    string errorMessage = string.Format("There was an error when parsing file {0}.\r\n\r\n{1}.", targetFile, e.Message);
                    MessageBox.Show(errorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return(false);
                }

                sr.Close();
                fs.Close();

                return(true);
            }
        }
Esempio n. 11
0
        private bool LoadLastOpened()
        {
            if (!File.Exists(lastOpenedFileData))
            {
                return(false);
            }

            FileStream   fs = new FileStream(lastOpenedFileData, FileMode.Open);
            StreamReader sr = new StreamReader(fs);

            var fileData = sr.ReadToEnd();

            sr.Close();
            fs.Close();

            var yamlSerialzer = new SharpYaml.Serialization.Serializer();

            initalizationData = yamlSerialzer.Deserialize <InitalizationData>(fileData);

            return(true);
        }
Esempio n. 12
0
        /// <summary>
        /// Converts the YAML string to JSON string.
        /// </summary>
        /// <param name="yaml">YAML string.</param>
        /// <param name="ignoreXYarm">Value indicating whether to ignore the top level properties is or starts with <c>x-yarm</c>, or not. Default value is <c>true</c>.</param>
        /// <returns>Returns JSON string converted.</returns>
        public static string ToJson(this string yaml, bool ignoreXYarm = true)
        {
            if (string.IsNullOrWhiteSpace(yaml))
            {
                return(null);
            }

            object deserialized;

            try
            {
                deserialized = new Serializer().Deserialize(yaml);
            }
            catch (Exception ex)
            {
                throw new InvalidYamlException(ex.Message);
            }

            var json = ignoreXYarm
                ? JsonConvert.SerializeObject(deserialized, Formatting.Indented, new IgnoreXYarmJsonConverter())
                : JsonConvert.SerializeObject(deserialized, Formatting.Indented);

            return(json);
        }
Esempio n. 13
0
        public void Deserialize()
        {
            var yaml = @"
Info: Test data
Map:
  - Delay: 100
    Description: Probe endpoint
    Status: 200
    Url: /probe
    
  - Delay: 200
    Description: Swagger endpoint
    Status: 200
    Url: /swagger
    Foo: bar
    
  - Delay: 300
    Method: POST
    Description: Order endpoint
    Status: 201
    Url: /order
    Payload: '{""paymentId"":""@guid""}'
    Headers:
      Location: /probe?a=b
      Authorization: 'Bearer aaa'
";

            var serializer = new SharpYaml.Serialization.Serializer();

            serializer.Settings.IgnoreUnmatchedProperties = true;

            var result = serializer.Deserialize <ConfigurationDefinition>(yaml);

            // Assert
            result.Should().NotBeNull();
            result.Info.Should().Be("Test data");
            result.Map.Should().NotBeNullOrEmpty();
            result.Map.Should().HaveCount(3);

            var firstItem = result.Map !.ElementAt(0);

            firstItem.Delay.Should().Be(100);
            firstItem.Description.Should().Be("Probe endpoint");
            firstItem.Status.Should().Be(200);
            firstItem.Url.Should().Be("/probe");
            firstItem.Payload.Should().BeNull();
            firstItem.Headers.Should().BeNull();
            firstItem.Method.Should().BeNull();

            var secondItem = result.Map !.ElementAt(1);

            secondItem.Delay.Should().Be(200);
            secondItem.Description.Should().Be("Swagger endpoint");
            secondItem.Status.Should().Be(200);
            secondItem.Url.Should().Be("/swagger");
            secondItem.Payload.Should().BeNull();
            secondItem.Headers.Should().BeNull();
            secondItem.Method.Should().BeNull();

            var thirdItem = result.Map !.ElementAt(2);

            thirdItem.Delay.Should().Be(300);
            thirdItem.Description.Should().Be("Order endpoint");
            thirdItem.Status.Should().Be(201);
            thirdItem.Url.Should().Be("/order");
            thirdItem.Payload.Should().Be("{\"paymentId\":\"@guid\"}");
            thirdItem.Method.Should().Be("POST");
            thirdItem.Headers.Should().NotBeNull();
            thirdItem.Headers.Should().HaveCount(2);
            thirdItem.Headers !["Location"].Should().Be("/probe?a=b");
Esempio n. 14
0
        private void SaveYaml_viaYamlDotNet()
        {
            // Load the YAML from file
            string hybrasylMapFilepath = Path.Combine(Configuration.Current.HybrasylWorldDirectory, "maps", map.Name);
            string hybrasylMapData     = File.ReadAllText(hybrasylMapFilepath);

            var serializer  = new SharpYaml.Serialization.Serializer();
            var hybrasylMap = serializer.Deserialize(hybrasylMapData) as Dictionary <object, object>;

            // update the Map properties supported by the editor
            // (TODO: replace this and pass in a Map Model object that the editor has been modifying, when that Model class exists)
            dynamic spawns = null;

            if (map.SpawnsFixed.Count > 0 && map.SpawnsZone.Count > 0)
            {
                spawns = new { @fixed = new List <object>(), zone = new List <object>() };
            }
            else if (map.SpawnsFixed.Count > 0)
            {
                spawns = new { @fixed = new List <object>() };
            }
            else if (map.SpawnsZone.Count > 0)
            {
                spawns = new { zone = new List <object>() };
            }

            if (spawns != null)
            {
                foreach (var f in map.SpawnsFixed)
                {
                    dynamic s = new ExpandoObject();
                    s.name      = f.name;
                    s.checkrate = f.checkrate;

                    // optional fields
                    if (f.min > 0)
                    {
                        s.min = f.min;
                    }
                    if (f.max > 0)
                    {
                        s.max = f.max;
                    }
                    if (f.every > 0)
                    {
                        s.every = f.every;
                    }
                    if (f.percentage > 0F)
                    {
                        s.percentage = f.percentage;
                    }
                    if (f.speed != FixedSpawn.DEFAULT_SPEED)
                    {
                        s.speed = f.speed;
                    }
                    if (f.hostile != FixedSpawn.DEFAULT_HOSTILE)
                    {
                        s.hostile = f.hostile;
                    }
                    if (f.strategy != FixedSpawn.DEFAULT_STRATEGY)
                    {
                        s.strategy = f.strategy;
                    }

                    [email protected](s);
                }

                foreach (var z in map.SpawnsZone)
                {
                    dynamic s = new ExpandoObject();
                    s.name        = z.name;
                    s.checkrate   = z.checkrate;
                    s.start_point = string.Format("{0},{1}", z.start_point.X, z.start_point.Y);
                    s.end_point   = string.Format("{0},{1}", z.end_point.X, z.end_point.Y);

                    // optional fields
                    if (z.min > 0)
                    {
                        s.min = z.min;
                    }
                    if (z.max > 0)
                    {
                        s.max = z.max;
                    }
                    if (z.every > 0)
                    {
                        s.every = z.every;
                    }
                    if (z.percentage > 0F)
                    {
                        s.percentage = z.percentage;
                    }
                    if (z.speed != ZoneSpawn.DEFAULT_SPEED)
                    {
                        s.speed = z.speed;
                    }
                    if (z.hostile != ZoneSpawn.DEFAULT_HOSTILE)
                    {
                        s.hostile = z.hostile;
                    }
                    if (z.strategy != ZoneSpawn.DEFAULT_STRATEGY)
                    {
                        s.strategy = z.strategy;
                    }

                    spawns.zone.Add(s);
                }

                hybrasylMap["spawns"] = spawns;
            }
            else if (spawns == null && hybrasylMap.ContainsKey("spawns"))
            {
                hybrasylMap.Remove("spawns");
            }


            using (var sw = new StreamWriter(System.IO.File.Create(hybrasylMapFilepath)))
            {
                var s = new YamlDotNet.Serialization.Serializer(namingConvention: new YamlDotNet.Serialization.NamingConventions.CamelCaseNamingConvention());
                s.Serialize(sw, hybrasylMap);
            }
        }