private void MainForm_Load(object sender, EventArgs e) { var ys = new YamlSerializer(); //var oo = ys.DeserializeFromFile(args.Length > 0 ? args[0] // : "data.txt", typeof(Rental[]), typeof(Bid[])); var oo = ys.DeserializeFromFile("data.txt", typeof(Rental[]), typeof(Bid[])); //ys.SerializeToFile("data0.txt", oo); rentals = (Rental[])oo[0]; bids = (Bid[])oo[1]; foreach (Rental re in rentals) listBox1.Items.Add(re.Name); foreach (Bid bi in bids) listBox2.Items.Add(bi.Name); }
static void DeserializeSample(string fnam) { var ys = new YamlSerializer(); var co = (Company)ys.DeserializeFromFile(fnam, typeof(Company))[0]; co.Employees[0].Age = 24; Console.WriteLine(co.Groups[0].Employees[0].Age); }
private void LoadConfigFile() { var serializer = new YamlSerializer(); if (File.Exists(_settingsFile)) { var obj = serializer.DeserializeFromFile(Path.GetDirectoryName(Application.ExecutablePath) + "\\Settings.cfg"); _config = (Config)obj[0]; configFilepath = Path.GetDirectoryName(Application.ExecutablePath) + "\\" + _config.LastMapFile + ".cfg"; } else { _config = new Config(); _config.LastMapFile = "Default"; } if (File.Exists(configFilepath)) { var obj = serializer.DeserializeFromFile(configFilepath); mapPoints = (List<MapPoint>)obj[0]; } else mapPoints = new List<MapPoint>(); this.Text = "Salt Charts - " + _config.LastMapFile; }
/// <summary> /// Load a gameobject from a file /// </summary> /// <param name="filePath"></param> /// <param name="roomToInstantiateIn"></param> /// <param name="position"> </param> /// <param name="rotation"> </param> /// <param name="allowNetworkInstantiateIfHasNetworkView"></param> /// <param name="visibleToAll">makes all players in the room subscribed to the object</param> /// <param name="owner">owner of the loaded object if network instantiated. By default, it is the server</param> /// <returns></returns> public static GameObject Load(string filePath, Room roomToInstantiateIn, bool allowNetworkInstantiateIfHasNetworkView = false, Vector3? position = null, Quaternion? rotation = null, bool visibleToAll = true, Player owner = null) { var dser = new GameObject(); dser.Room = roomToInstantiateIn; var awakes = new List<Action>(); var config = new YamlConfig(); var actualFilePath = Path.Combine(ResourceFolder, filePath + ".prefab"); config.AddActivator<GameObject>(() => { //be returning an object we've already created, the AddComponent will work properly return dser; }); //config.AddActivator < List<ComponentTracker>>(() => //{ // return dser.components; //}); var trackers = new Stack<GameObject.ComponentTracker>(); config.AddActivator<GameObject.ComponentTracker>(() => { var ntrack = new GameObject.ComponentTracker(); trackers.Push(ntrack); return ntrack; }); foreach (Type t in GetComponentTypes()) { Type tLocal = t; if (tLocal == typeof(NetworkView) && !allowNetworkInstantiateIfHasNetworkView) { Debug.LogWarning("[Resources.Load] file {0} has a NetworkView component on it, but was run as to not network instantiate. It will not be networked.", actualFilePath); } GameObject dser1 = dser; config.AddActivator(tLocal, () => { Action awake; var ntrack = trackers.Pop(); var ret = dser1.DeserializeAddComponent(tLocal, out awake, ntrack); awakes.Add(awake); return ret; }); } var serializer = new YamlSerializer(config); serializer.DeserializeFromFile(actualFilePath, typeof(GameObject)); if (dser.Resource == null) dser.Resource = filePath; if (position.HasValue) dser.Position = position.Value; if (rotation.HasValue) dser.Rotation = rotation.Value; roomToInstantiateIn.OnGameObjectAdded(dser); foreach (var awake in awakes) if (awake != null) awake(); dser.OnComponentAfterDeserialization(); if (allowNetworkInstantiateIfHasNetworkView && dser.GetComponent<NetworkView>() != null) roomToInstantiateIn.ResourceNetworkInstantiate(dser, visibleToAll, owner); return dser; }
public static YamlConfig CreateFromFile(string pathToYaml) { YamlSerializer serializer = new YamlSerializer(); // var objectRestored = serializer.DeserializeFromFile(@"C:\Users\Jimmy\Documents\Pinball\demo_man\config\dm.yaml")[0]; object[] results = serializer.DeserializeFromFile(@"C:\Users\Jimmy\Documents\Pinball\demo_man\config\dm.yaml", Type.GetType("string")); YamlConfig resultConfig = new YamlConfig(); Dictionary<object, object> cfg = (Dictionary<object, object>)results[0]; Dictionary<object, object> tmpDict = (Dictionary<object, object>)cfg["PRGame"]; Dictionary<object, object> tmpDict2; /////////////////////////////////////////////////////////////////////////////// /// Process PRGame config data /////////////////////////////////////////////////////////////////////////////// foreach (object key in tmpDict.Keys) { resultConfig.PRGame.Add(key.ToString(), tmpDict[key].ToString()); } /////////////////////////////////////////////////////////////////////////////// /// Process PRBumpers config data /////////////////////////////////////////////////////////////////////////////// foreach (object o in (object[])cfg["PRBumpers"]) resultConfig.PRBumpers.Add(o.ToString()); /////////////////////////////////////////////////////////////////////////////// /// Process PRFlippers config data /////////////////////////////////////////////////////////////////////////////// foreach (object o in (object[])cfg["PRFlippers"]) resultConfig.PRFlippers.Add(o.ToString()); /////////////////////////////////////////////////////////////////////////////// /// Process PRSwitches config data /////////////////////////////////////////////////////////////////////////////// tmpDict = (Dictionary<object, object>)cfg["PRSwitches"]; foreach (object key1 in tmpDict.Keys) { tmpDict2 = (Dictionary<object, object>)tmpDict[key1]; resultConfig.PRSwitches.Add(key1.ToString(), new Dictionary<string, string>()); foreach (object key2 in tmpDict2.Keys) { resultConfig.PRSwitches[key1.ToString()].Add(key2.ToString(), tmpDict2[key2].ToString()); } } /////////////////////////////////////////////////////////////////////////////// /// Process PRCoils config data /////////////////////////////////////////////////////////////////////////////// tmpDict = (Dictionary<object, object>)cfg["PRCoils"]; foreach (object key1 in tmpDict.Keys) { tmpDict2 = (Dictionary<object, object>)tmpDict[key1]; resultConfig.PRCoils.Add(key1.ToString(), new Dictionary<string, string>()); foreach (object key2 in tmpDict2.Keys) { resultConfig.PRCoils[key1.ToString()].Add(key2.ToString(), tmpDict2[key2].ToString()); } } /////////////////////////////////////////////////////////////////////////////// /// Process PRLamps config data /////////////////////////////////////////////////////////////////////////////// tmpDict = (Dictionary<object, object>)cfg["PRLamps"]; foreach (object key1 in tmpDict.Keys) { tmpDict2 = (Dictionary<object, object>)tmpDict[key1]; resultConfig.PRLamps.Add(key1.ToString(), new Dictionary<string, string>()); foreach (object key2 in tmpDict2.Keys) { resultConfig.PRLamps[key1.ToString()].Add(key2.ToString(), tmpDict2[key2].ToString()); } } /////////////////////////////////////////////////////////////////////////////// /// TODO: Process PRBallSave config data. Introduce new structs/classes /// as needed for use in the overall Config class /////////////////////////////////////////////////////////////////////////////// Console.WriteLine("Parsing file..."); return resultConfig; }