public override string CheckCanUpdateData(ResourceElement newResElem)
 {
     var res = base.CheckCanUpdateData(newResElem);
     if (!string.IsNullOrEmpty(res))
         return res;
     return SerializedImageListStreamerUtils.CheckCanUpdateData(this.GetModule(), newResElem);
 }
Ejemplo n.º 2
0
        ResourceElement resourceElement; // updated by the asm editor, see UpdateData()

        #endregion Fields

        #region Constructors

        protected ResourceElementNode(ITreeNodeGroup treeNodeGroup, ResourceElement resourceElement)
        {
            if (treeNodeGroup == null || resourceElement == null)
                throw new ArgumentNullException();
            this.treeNodeGroup = treeNodeGroup;
            this.resourceElement = resourceElement;
        }
Ejemplo n.º 3
0
		public BamlResourceElementNode(ModuleDef module, ResourceElement resourceElement, byte[] bamlData, ITreeNodeGroup treeNodeGroup, BamlSettings bamlSettings, IXamlOutputOptionsProvider xamlOutputOptionsProvider, IDocumentWriterService documentWriterService)
			: base(treeNodeGroup, resourceElement) {
			this.module = module;
			this.bamlData = bamlData;
			this.bamlSettings = bamlSettings;
			this.xamlOutputOptionsProvider = xamlOutputOptionsProvider;
			this.documentWriterService = documentWriterService;
		}
        public override void UpdateData(ResourceElement newResElem)
        {
            base.UpdateData(newResElem);

            var binData = (BinaryResourceData)newResElem.ResourceData;
            byte[] imageData;
            SerializedImageListStreamerUtils.GetImageData(this.GetModule(), binData.TypeName, binData.Data, out imageData);
            InitializeImageData(imageData);
        }
Ejemplo n.º 5
0
		public IResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup) {
			if (resourceElement.ResourceData.Code != ResourceTypeCode.ByteArray && resourceElement.ResourceData.Code != ResourceTypeCode.Stream)
				return null;

			var data = (byte[])((BuiltInResourceData)resourceElement.ResourceData).Data;

			if (!BamlReader.IsBamlHeader(new MemoryStream(data)))
				return null;

			return new BamlResourceElementNode(module, resourceElement, data, treeNodeGroup, bamlSettings);
		}
Ejemplo n.º 6
0
		public IResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup) {
			if (resourceElement.ResourceData.Code != ResourceTypeCode.ByteArray && resourceElement.ResourceData.Code != ResourceTypeCode.Stream)
				return null;

			var data = (byte[])((BuiltInResourceData)resourceElement.ResourceData).Data;
			var stream = MemoryImageStream.Create(data);
			if (!CouldBeImage(resourceElement.Name, stream))
				return null;

			return new ImageResourceElementNode(treeNodeGroup, resourceElement);
		}
		public ResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup) {
			var serializedData = resourceElement.ResourceData as BinaryResourceData;
			if (serializedData == null)
				return null;

			byte[] imageData;
			if (SerializedImageUtilities.GetImageData(module, serializedData.TypeName, serializedData.Data, out imageData))
				return new SerializedImageResourceElementNodeImpl(treeNodeGroup, resourceElement, imageData);

			return null;
		}
Ejemplo n.º 8
0
        public override string CheckCanUpdateData(ResourceElement newResElem)
        {
            var res = base.CheckCanUpdateData(newResElem);
            if (!string.IsNullOrEmpty(res))
                return res;

            try {
                ImageResourceUtils.CreateImageSource((byte[])((BuiltInResourceData)newResElem.ResourceData).Data);
            }
            catch {
                return dnSpy_Resources.NewDataIsNotAnImage;
            }

            return null;
        }
Ejemplo n.º 9
0
 public IResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup)
 {
     if (module == null || resourceElement == null || treeNodeGroup == null)
         throw new ArgumentNullException();
     foreach (var creator in creators) {
         try {
             var node = creator.Value.Create(module, resourceElement, treeNodeGroup);
             if (node != null)
                 return node;
         }
         catch {
         }
     }
     return new BuiltInResourceElementNode(treeNodeGroup, resourceElement);
 }
        public static string CheckCanUpdateData(ModuleDef module, ResourceElement newResElem)
        {
            var binData = (BinaryResourceData)newResElem.ResourceData;
            byte[] imageData;
            if (!GetImageData(module, binData.TypeName, binData.Data, out imageData))
                return dnSpy_Shared_Resources.NewDataNotImageList;

            try {
                ReadImageData(imageData);
            }
            catch {
                return dnSpy_Shared_Resources.NewDataNotImageList;
            }

            return string.Empty;
        }
Ejemplo n.º 11
0
		static ResourceElement CreateSerializedImage(Stream stream, string filename) {
			object obj;
			if (filename.EndsWith(".ico", StringComparison.OrdinalIgnoreCase))
				obj = new System.Drawing.Icon(stream);
			else
				obj = new System.Drawing.Bitmap(stream);
			var serializedData = Serialize(obj);

			var userType = new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes);
			var rsrcElem = new ResourceElement {
				Name = Path.GetFileName(filename),
				ResourceData = new BinaryResourceData(userType, serializedData),
			};

			return rsrcElem;
		}
Ejemplo n.º 12
0
		public ResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup) {
			if (module == null)
				throw new ArgumentNullException(nameof(module));
			if (resourceElement == null)
				throw new ArgumentNullException(nameof(resourceElement));
			if (treeNodeGroup == null)
				throw new ArgumentNullException(nameof(treeNodeGroup));
			foreach (var provider in resourceNodeProviders) {
				try {
					var node = provider.Value.Create(module, resourceElement, treeNodeGroup);
					if (node != null)
						return node;
				}
				catch {
				}
			}
			return new BuiltInResourceElementNodeImpl(treeNodeGroup, resourceElement);
		}
        public override string CheckCanUpdateData(ResourceElement newResElem)
        {
            var res = base.CheckCanUpdateData(newResElem);
            if (!string.IsNullOrEmpty(res))
                return res;

            var binData = (BinaryResourceData)newResElem.ResourceData;
            byte[] imageData;
            if (!SerializedImageUtils.GetImageData(this.GetModule(), binData.TypeName, binData.Data, out imageData))
                return dnSpy_Resources.NewDataIsNotAnImage;

            try {
                ImageResourceUtils.CreateImageSource(imageData);
            }
            catch {
                return dnSpy_Resources.NewDataIsNotAnImage;
            }

            return string.Empty;
        }
		public override void UpdateData(ResourceElement newResElem) {
			base.UpdateData(newResElem);
			deserializedData = null;
			DeserializeIfPossible();
		}
Ejemplo n.º 15
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="treeNodeGroup">Treenode group</param>
		/// <param name="resourceElement">Resource element</param>
		protected ImageResourceElementNode(ITreeNodeGroup treeNodeGroup, ResourceElement resourceElement)
			: base(treeNodeGroup, resourceElement) {
		}
Ejemplo n.º 16
0
		/// <summary>
		/// Constructor
		/// </summary>
		/// <param name="treeNodeGroup">Treenode group</param>
		/// <param name="resourceElement">Resource element</param>
		protected BuiltInResourceElementNode(ITreeNodeGroup treeNodeGroup, ResourceElement resourceElement)
			: base(treeNodeGroup, resourceElement) {
		}
Ejemplo n.º 17
0
 public IResourceElementNode Create(ModuleDef module, ResourceElement resourceElement, ITreeNodeGroup treeNodeGroup)
 {
     return null;
 }
Ejemplo n.º 18
0
        ResourceElementSet Read()
        {
            var resources = new ResourceElementSet();

            uint sig = reader.ReadUInt32();

            if (sig != 0xBEEFCACE)
            {
                throw new ResourceReaderException($"Invalid resource sig: {sig:X8}");
            }
            if (!CheckReaders())
            {
                throw new ResourceReaderException("Invalid resource reader");
            }
            int version = reader.ReadInt32();

            if (version != 2)            //TODO: Support version 1
            {
                throw new ResourceReaderException($"Invalid resource version: {version}");
            }
            int numResources = reader.ReadInt32();

            if (numResources < 0)
            {
                throw new ResourceReaderException($"Invalid number of resources: {numResources}");
            }
            int numUserTypes = reader.ReadInt32();

            if (numUserTypes < 0)
            {
                throw new ResourceReaderException($"Invalid number of user types: {numUserTypes}");
            }

            var userTypes = new List <UserResourceType>();

            for (int i = 0; i < numUserTypes; i++)
            {
                userTypes.Add(new UserResourceType(reader.ReadSerializedString(), ResourceTypeCode.UserTypes + i));
            }
            reader.Position = (reader.Position + 7) & ~7U;

            var hashes = new int[numResources];

            for (int i = 0; i < numResources; i++)
            {
                hashes[i] = reader.ReadInt32();
            }
            var offsets = new int[numResources];

            for (int i = 0; i < numResources; i++)
            {
                offsets[i] = reader.ReadInt32();
            }

            long baseOffset     = reader.Position;
            long dataBaseOffset = reader.ReadInt32();
            long nameBaseOffset = reader.Position;
            long end            = reader.Length;

            var infos = new List <ResourceInfo>(numResources);

            for (int i = 0; i < numResources; i++)
            {
                reader.Position = (uint)(nameBaseOffset + offsets[i]);
                var  name   = reader.ReadSerializedString(Encoding.Unicode);
                long offset = dataBaseOffset + reader.ReadInt32();
                infos.Add(new ResourceInfo(name, offset));
            }

            infos.Sort((a, b) => a.offset.CompareTo(b.offset));
            for (int i = 0; i < infos.Count; i++)
            {
                var info    = infos[i];
                var element = new ResourceElement();
                element.Name    = info.name;
                reader.Position = (uint)info.offset;
                long nextDataOffset = i == infos.Count - 1 ? end : infos[i + 1].offset;
                int  size           = (int)(nextDataOffset - info.offset);
                element.ResourceData             = ReadResourceData(userTypes, size);
                element.ResourceData.StartOffset = baseFileOffset + (FileOffset)info.offset;
                element.ResourceData.EndOffset   = baseFileOffset + (FileOffset)reader.Position;

                resources.Add(element);
            }

            return(resources);
        }
Ejemplo n.º 19
0
        protected ResourceElementSettingsBaseCommand(ResourceElementTreeNode rsrcElNode, ResourceElementOptions options)
        {
            this.rsrcSetNode = (ResourceElementSetTreeNode)rsrcElNode.Parent;
            this.rsrcElNode = rsrcElNode;
            this.newOptions = options.Create();
            this.origOptions = rsrcElNode.ResourceElement;

            this.module = ILSpyTreeNode.GetModule(rsrcSetNode);
            Debug.Assert(this.module != null);
            this.resource = rsrcSetNode.Resource;
            this.resourceIndex = module.Resources.IndexOf(this.resource);
            Debug.Assert(this.resourceIndex >= 0);
            if (this.resourceIndex < 0)
                throw new InvalidOperationException();

            this.origParentChildIndex = this.rsrcSetNode.Children.IndexOf(rsrcElNode);
            Debug.Assert(this.origParentChildIndex >= 0);
            if (this.origParentChildIndex < 0)
                throw new InvalidOperationException();

            this.nameChanged = origOptions.Name != newOptions.Name;
        }
Ejemplo n.º 20
0
		public ResourceElementOptions(ResourceElement resEl) {
			this.Name = resEl.Name;
			this.ResourceData = resEl.ResourceData;
		}
Ejemplo n.º 21
0
		internal static void Execute(Lazy<IUndoCommandManager> undoCommandManager, IAppWindow appWindow, IResourceNodeFactory resourceNodeFactory, IFileTreeNodeData[] nodes, ResourceTypeCode typeCode, Func<IResourceElementSetNode, IResourceElementNode[], IUndoCommand> createCommand) {
			var rsrcSetNode = nodes[0] as IResourceElementSetNode;
			if (rsrcSetNode == null)
				rsrcSetNode = nodes[0].TreeNode.Parent.Data as IResourceElementSetNode;
			Debug.Assert(rsrcSetNode != null);

			var module = nodes[0].GetModule();
			Debug.Assert(module != null);
			if (module == null)
				throw new InvalidOperationException();

			var dlg = new WF.OpenFileDialog {
				RestoreDirectory = true,
				Multiselect = true,
				Filter = PickFilenameConstants.AnyFilenameFilter,
			};
			if (dlg.ShowDialog() != WF.DialogResult.OK)
				return;
			var fnames = dlg.FileNames;
			if (fnames.Length == 0)
				return;

			var newNodes = new IResourceElementNode[fnames.Length];
			var treeView = appWindow.FileTreeView.TreeView;
			var treeNodeGroup = appWindow.FileTreeView.FileTreeNodeGroups.GetGroup(FileTreeNodeGroupType.ResourceElementTreeNodeGroup);
			for (int i = 0; i < fnames.Length; i++) {
				var fn = fnames[i];
				try {
					var rsrcElem = new ResourceElement {
						Name = Path.GetFileName(fn),
						ResourceData = new BuiltInResourceData(typeCode, File.ReadAllBytes(fn)),
					};
					newNodes[i] = (IResourceElementNode)treeView.Create(resourceNodeFactory.Create(module, rsrcElem, treeNodeGroup)).Data;
				}
				catch (Exception ex) {
					Shared.App.MsgBox.Instance.Show(string.Format(dnSpy_AsmEditor_Resources.Error_ReadingFiles, ex.Message));
					return;
				}
			}

			undoCommandManager.Value.Add(createCommand(rsrcSetNode, newNodes.ToArray()));
			appWindow.FileTabManager.FollowReference(newNodes[0]);
		}
		protected SerializedResourceElementTreeNode(ResourceElement resElem)
			: base(resElem) {
			Debug.Assert(resElem.ResourceData is BinaryResourceData);
			DeserializeIfPossible();
		}
Ejemplo n.º 23
0
 public ImageResourceElementNode(ITreeNodeGroup treeNodeGroup, ResourceElement resourceElement)
     : base(treeNodeGroup, resourceElement)
 {
     InitializeImageData();
 }
 public BuiltInResourceElementTreeNode(ResourceElement resElem)
     : base(resElem)
 {
 }
Ejemplo n.º 25
0
 public override void UpdateData(ResourceElement newResElem)
 {
     base.UpdateData(newResElem);
     InitializeImageData();
 }
Ejemplo n.º 26
0
        public static ResourceElement Serialize(ResourceElement resElem)
        {
            var data = (byte[])((BuiltInResourceData)resElem.ResourceData).Data;
            bool isIcon = BitConverter.ToUInt32(data, 0) == 0x00010000;

            object obj;
            if (isIcon)
                obj = new System.Drawing.Icon(new MemoryStream(data));
            else
                obj = new System.Drawing.Bitmap(new MemoryStream(data));

            return new ResourceElement {
                Name = resElem.Name,
                ResourceData = new BinaryResourceData(new UserResourceType(obj.GetType().AssemblyQualifiedName, ResourceTypeCode.UserTypes), SerializationUtils.Serialize(obj)),
            };
        }
Ejemplo n.º 27
0
		ResourceElementSet Read() {
			ResourceElementSet resources = new ResourceElementSet();

			uint sig = reader.ReadUInt32();
			if (sig != 0xBEEFCACE)
				throw new ResourceReaderException(string.Format("Invalid resource sig: {0:X8}", sig));
			if (!CheckReaders())
				throw new ResourceReaderException("Invalid resource reader");
			int version = reader.ReadInt32();
			if (version != 2)//TODO: Support version 1
				throw new ResourceReaderException(string.Format("Invalid resource version: {0}", version));
			int numResources = reader.ReadInt32();
			if (numResources < 0)
				throw new ResourceReaderException(string.Format("Invalid number of resources: {0}", numResources));
			int numUserTypes = reader.ReadInt32();
			if (numUserTypes < 0)
				throw new ResourceReaderException(string.Format("Invalid number of user types: {0}", numUserTypes));

			var userTypes = new List<UserResourceType>();
			for (int i = 0; i < numUserTypes; i++)
				userTypes.Add(new UserResourceType(reader.ReadString(), ResourceTypeCode.UserTypes + i));
			reader.Position = (reader.Position + 7) & ~7;

			var hashes = new int[numResources];
			for (int i = 0; i < numResources; i++)
				hashes[i] = reader.ReadInt32();
			var offsets = new int[numResources];
			for (int i = 0; i < numResources; i++)
				offsets[i] = reader.ReadInt32();

			long baseOffset = reader.Position;
			long dataBaseOffset = reader.ReadInt32();
			long nameBaseOffset = reader.Position;
			long end = reader.Length;

			var infos = new List<ResourceInfo>(numResources);

			for (int i = 0; i < numResources; i++) {
				reader.Position = nameBaseOffset + offsets[i];
				var name = reader.ReadString(Encoding.Unicode);
				long offset = dataBaseOffset + reader.ReadInt32();
				infos.Add(new ResourceInfo(name, offset));
			}

			infos.Sort((a, b) => a.offset.CompareTo(b.offset));
			for (int i = 0; i < infos.Count; i++) {
				var info = infos[i];
				var element = new ResourceElement();
				element.Name = info.name;
				reader.Position = info.offset;
				long nextDataOffset = i == infos.Count - 1 ? end : infos[i + 1].offset;
				int size = (int)(nextDataOffset - info.offset);
				element.ResourceData = ReadResourceData(userTypes, size);
				element.ResourceData.StartOffset = this.baseFileOffset + (FileOffset)info.offset;
				element.ResourceData.EndOffset = this.baseFileOffset + (FileOffset)reader.Position;

				resources.Add(element);
			}

			return resources;
		}
Ejemplo n.º 28
0
			public RenameInfo(ResourceElement element, string newName) {
				this.element = element;
				this.newName = newName;
				this.foundInCode = false;
			}
Ejemplo n.º 29
0
		public ResourceElement CopyTo(ResourceElement other) {
			other.Name = this.Name;
			other.ResourceData = this.ResourceData;
			return other;
		}
Ejemplo n.º 30
0
		string GetNewName(ResourceElement elem) {
			if (elem.ResourceData.Code != ResourceTypeCode.String)
				return CreateDefaultName();
			var stringData = (BuiltInResourceData)elem.ResourceData;
			var name = CreatePrefixFromStringData((string)stringData.Data);
			return CreateName(counter => counter == 0 ? name : string.Format("{0}_{1}", name, counter));
		}
Ejemplo n.º 31
0
        internal static void Execute(ILSpyTreeNode[] nodes, ResourceTypeCode typeCode, Func<ResourceElementSetTreeNode, ResourceElementTreeNode[], IUndoCommand> createCommand)
        {
            var rsrcSetNode = nodes[0] as ResourceElementSetTreeNode;
            if (rsrcSetNode == null)
                rsrcSetNode = nodes[0].Parent as ResourceElementSetTreeNode;
            Debug.Assert(rsrcSetNode != null);

            var module = ILSpyTreeNode.GetModule(nodes[0]);
            Debug.Assert(module != null);
            if (module == null)
                throw new InvalidOperationException();

            var dlg = new WF.OpenFileDialog {
                RestoreDirectory = true,
                Multiselect = true,
                Filter = "All files (*.*)|*.*",
            };
            if (dlg.ShowDialog() != WF.DialogResult.OK)
                return;
            var fnames = dlg.FileNames;
            if (fnames.Length == 0)
                return;

            var newNodes = new ResourceElementTreeNode[fnames.Length];
            for (int i = 0; i < fnames.Length; i++) {
                var fn = fnames[i];
                try {
                    var rsrcElem = new ResourceElement {
                        Name = Path.GetFileName(fn),
                        ResourceData = new BuiltInResourceData(typeCode, File.ReadAllBytes(fn)),
                    };
                    newNodes[i] = ResourceFactory.Create(module, rsrcElem);
                }
                catch (Exception ex) {
                    MainWindow.Instance.ShowMessageBox(string.Format("Error reading files: {0}", ex.Message));
                    return;
                }
            }

            UndoCommandManager.Instance.Add(createCommand(rsrcSetNode, newNodes.ToArray()));
        }
Ejemplo n.º 32
0
 /// <summary>
 /// Adds a new resource to the set, overwriting any existing resource
 /// </summary>
 /// <param name="elem"></param>
 public void Add(ResourceElement elem)
 {
     dict[elem.Name] = elem;
 }