private static void CreateTags(IDictionary <Element, Tag> tagMap, ICollection <Tag> tagList, AssemblingContext context, Element element, BeginningTag parent) { if (element is ConcreteElement) { ElementTag tag = new ElementTag(tagList.Count, context, parent, (ConcreteElement)element); tagMap.Add(element, tag); tagList.Add(tag); } else if (element is CompositeElement) { CompositeElement compositeElement = (CompositeElement)element; BeginningTag beginningTag = new BeginningTag(tagList.Count, context, parent, compositeElement); tagMap.Add(element, beginningTag); tagList.Add(beginningTag); foreach (Element e in compositeElement.GetElements()) { CreateTags(tagMap, tagList, context, e, beginningTag); } EndTag endTag = new EndTag(tagList.Count, context, parent, beginningTag); tagList.Add(endTag); } }
private static ConcreteComponentAssembler GetAssembler(Game game, ComponentAssembler assembler) { var tokenReplacer = new TokenReplacer(new ApplicationFilePathTokenReplacingPolicy(() => game.FilePath)); var assemblingContext = new AssemblingContext(FormType.New, tokenReplacer); return(new ConcreteComponentAssembler(assembler, assemblingContext)); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if (value == null) { return(null); } if (value is IComponentViewModel) { return(value); } var viewModelFactory = DependencyResolver.Current.Resolve <ComponentViewModelFactory>(); if (viewModelFactory == null) { return(null); } lock (_lock) { foreach (var pair in _dictionary.ToArray() .Where(pair => pair.Key.Target == null)) { _dictionary.Remove(pair); } var holder = _dictionary.Select(pair => new { pair.Key.Target, pair.Value }) .FirstOrDefault(pair => (pair.Target != null) && ReferenceEquals(pair.Target, value)); if (holder != null) { return(holder.Value); } var assembler = DependencyResolver.Current.Resolve <ComponentAssembler>(); var assemblingContext = new AssemblingContext(FormType); var viewModel = viewModelFactory.Create(assembler.Assemble((dynamic)value, assemblingContext)); _dictionary.Add(new WeakReference(value), viewModel); return(viewModel); } }
/// <summary> /// Assemble, that is measure, arrange, and write elements in the given BinaryWriter /// </summary> /// <param name="element">Element to assemble</param> /// <param name="baseMemoryAddress">Memory address at which the program will be loaded</param> /// <param name="pointerSize">Size of a memory address</param> /// <param name="writer">Writer to write the result to</param> public void Assemble(Element element, ulong baseMemoryAddress, PointerSize pointerSize, BinaryWriter writer) { Dictionary <Element, Tag> tagMap = new Dictionary <Element, Tag>(); List <Tag> tagList = new List <Tag>(); AssemblingContext context = new AssemblingContext(tagMap, baseMemoryAddress, pointerSize); CreateTags(tagMap, tagList, context, element, null); foreach (Tag tag in tagList) { tag.RegisterDependencies(); } uint fileAddress = 0; uint relativeMemoryAddress = 0; for (int i = 0; i < tagList.Count; i++) { Tag tag = tagList[i]; if (tag.SetAddresses(fileAddress, relativeMemoryAddress) && tag.Observers != null) { int index = int.MaxValue; foreach (ElementTag observer in tag.Observers) { observer.InvalidateMeasure(); if (observer.Index < i && observer.Measure()) { index = Math.Min(index, observer.Index); } } if (index < i) { i = index; tag = tagList[i]; fileAddress = tag.FileAddress; relativeMemoryAddress = tag.RelativeMemoryAddress; } } tag.Measure(); fileAddress += tag.RealFileSize; relativeMemoryAddress += tag.RealMemorySize; } long baseStreamPosition = writer.BaseStream.Position; foreach (Tag tag in tagList) { tag.WriteTo(writer); baseStreamPosition += tag.RealFileSize; if (baseStreamPosition != writer.BaseStream.Position) { throw new Exception(); } } writer.Flush(); }
protected Tag(int index, AssemblingContext context, BeginningTag parent) { m_index = index; m_context = context; m_parent = parent; }
public EndTag(int index, AssemblingContext context, BeginningTag parent, BeginningTag beginningTag) : base(index, context, parent) { m_beginningTag = beginningTag; }
public ElementTag(int index, AssemblingContext context, BeginningTag parent, ConcreteElement concreteElement) : base(index, context, parent) { m_concreteElement = concreteElement; m_isMeasureValid = false; }
public BeginningTag(int index, AssemblingContext context, BeginningTag parent, CompositeElement compositeElement) : base(index, context, parent) { m_compositeElement = compositeElement; }
public ConcreteComponentAssembler(ComponentAssembler assembler, AssemblingContext assemblingContext) { _assembler = assembler; _assemblingContext = assemblingContext; }