public void AddListener <T> (UpgradeDelegate <T> del) where T : Upgrade, new () { if (delegateLookup.ContainsKey(del)) { return; } UpgradeDelegate internalDelegate = (u) => del((T)u); delegateLookup[del] = internalDelegate; KeyValuePair <Upgrade, UpgradeDelegate> tempDel; if (delegates.TryGetValue(typeof(T), out tempDel)) { delegates[typeof(T)] = new KeyValuePair <Upgrade, UpgradeDelegate> ( tempDel.Key, tempDel.Value + internalDelegate ); SetLevel <T> (tempDel.Key.CurrentLevel); } else { delegates[typeof(T)] = new KeyValuePair <Upgrade, UpgradeDelegate> ( new T(), internalDelegate ); SetLevel <T> (0); } }
public void RemoveListener <T> (UpgradeDelegate <T> del) where T : Upgrade { UpgradeDelegate internalDelegate; if (delegateLookup.TryGetValue(del, out internalDelegate)) { KeyValuePair <Upgrade, UpgradeDelegate> tempDel; if (delegates.TryGetValue(typeof(T), out tempDel)) { delegates[typeof(T)] = new KeyValuePair <Upgrade, UpgradeDelegate> ( tempDel.Key, tempDel.Value - internalDelegate ); if (delegates[typeof(T)].Value == null) { delegates.Remove(typeof(T)); } } delegateLookup.Remove(del); } }
public BuildingUpgrade(string newName, UpgradeDelegate method) { name = newName; performUpgrade = method; }
public static Project Load(FileInfo sourceFile) { Project project = new Project(); Stream stream = sourceFile.OpenRead(); XmlTextReader xml = new XmlTextReader(stream); xml.ReadStartElement("project"); // project format version xml.ReadStartElement("format"); int formatVersion = xml.ReadContentAsInt(); xml.ReadEndElement(); // check project format and upgrade support UpgradeDelegate projectUpgradeFunction = null; if (formatVersion != FormatVersion) { projectUpgradeFunction = ProjectFormatUpgrade(formatVersion); if (projectUpgradeFunction == null) { throw new Exception(String.Format("invalid project file format (found {0}, expected {1}, upgrade unsupported)", formatVersion, FormatVersion)); } else { Console.WriteLine("old format detected, upgrade supported ({0} -> {1})", formatVersion, FormatVersion); } } // audio tracks if (xml.IsStartElement("audiotracks")) { bool empty = xml.IsEmptyElement; xml.ReadStartElement("audiotracks"); if (!empty) { while (xml.IsStartElement("track")) { xml.MoveToAttribute("file"); string file = xml.Value; AudioTrack track = new AudioTrack(GetFileInfo(sourceFile, file)); xml.MoveToAttribute("name"); track.Name = xml.Value; string color = xml.GetAttribute("color"); if (color != null) { track.Color = color; } xml.MoveToAttribute("length"); track.Length = TimeSpan.Parse(xml.Value); xml.MoveToAttribute("offset"); track.Offset = TimeSpan.Parse(xml.Value); xml.MoveToAttribute("mute"); track.Mute = xml.ReadContentAsBoolean(); xml.MoveToAttribute("solo"); track.Solo = xml.ReadContentAsBoolean(); xml.MoveToAttribute("volume"); track.Volume = xml.ReadContentAsFloat(); xml.MoveToAttribute("balance"); track.Balance = xml.ReadContentAsFloat(); xml.MoveToAttribute("invertedphase"); track.InvertedPhase = xml.ReadContentAsBoolean(); if (xml.MoveToAttribute("locked")) { track.Locked = xml.ReadContentAsBoolean(); } xml.ReadStartElement("track"); if (xml.IsStartElement("timewarps")) { empty = xml.IsEmptyElement; xml.ReadStartElement("timewarps"); if (!empty) { var timeWarps = new List <TimeWarp>(); while (xml.IsStartElement("timewarp")) { TimeWarp warp = new TimeWarp(); xml.MoveToAttribute("from"); warp.From = new TimeSpan(xml.ReadContentAsLong()); xml.MoveToAttribute("to"); warp.To = new TimeSpan(xml.ReadContentAsLong()); xml.ReadStartElement(); //xml.ReadEndElement(); // not necessary since timewarp is an empty element timeWarps.Add(warp); } track.TimeWarps.AddRange(timeWarps); xml.ReadEndElement(); // timewarps } } xml.ReadEndElement(); // track project.AudioTracks.Add(track); } xml.ReadEndElement(); // audiotracks } } // matches if (xml.IsStartElement("matches")) { bool empty = xml.IsEmptyElement; xml.ReadStartElement("matches"); if (!empty) { while (xml.IsStartElement("match")) { Match match = new Match(); xml.MoveToAttribute("track1"); match.Track1 = project.AudioTracks[xml.ReadContentAsInt()]; xml.MoveToAttribute("track1time"); match.Track1Time = TimeSpan.Parse(xml.Value); xml.MoveToAttribute("track2"); match.Track2 = project.AudioTracks[xml.ReadContentAsInt()]; xml.MoveToAttribute("track2time"); match.Track2Time = TimeSpan.Parse(xml.Value); xml.MoveToAttribute("similarity"); match.Similarity = xml.ReadContentAsFloat(); if (xml.MoveToAttribute("source")) { match.Source = xml.ReadContentAsString(); } project.Matches.Add(match); xml.ReadStartElement("match"); } xml.ReadEndElement(); // matches } } // global settings xml.ReadStartElement("mastervolume"); project.MasterVolume = xml.ReadContentAsFloat(); xml.ReadEndElement(); xml.ReadEndElement(); xml.Close(); project.File = sourceFile; // project format upgrade execution if (projectUpgradeFunction != null) { // there's an upgrade function we need to call project = projectUpgradeFunction(project); } return(project); }