Beispiel #1
0
        private static Byte[] ReconstructRawData(IconDirectory dir, FileIconDirectoryEntry[] ents, IconCursorImageResourceData[] images, ResourceSource source)
        {
            Int32 sizeOfIconDirectory = Marshal.SizeOf(typeof(IconDirectory));
            Int32 sizeOfResIconDirEnt = Marshal.SizeOf(typeof(ResIconDirectoryEntry));

            Int32 sizeOfData =
                sizeOfIconDirectory +
                sizeOfResIconDirEnt * dir.wCount;

            IntPtr p = Marshal.AllocHGlobal(sizeOfData);

            Marshal.StructureToPtr(dir, p, true);

            IntPtr q = Inc(p, Marshal.SizeOf(typeof(IconDirectory)));

            for (int i = 0; i < ents.Length; i++)
            {
                FileIconDirectoryEntry e = ents[i];

                ResourceTypeIdentifier iconImageTypeId = new ResourceTypeIdentifier(new IntPtr((int)Win32ResourceType.IconImage));
                ResourceIdentifier     nameId          = source.GetUnusedName(iconImageTypeId);
                source.Add(iconImageTypeId, nameId, 1033, images[i]);

                ResIconDirectoryEntry d = new ResIconDirectoryEntry()
                {
                    bWidth       = e.bWidth,
                    bHeight      = e.bHeight,
                    bColorCount  = e.bColorCount,
                    bReserved    = e.bReserved,
                    wPlanes      = e.wPlanes,
                    wBitCount    = e.wPlanes,
                    dwBytesInRes = e.dwBytesInRes,
                    wId          = (ushort)images[i].Lang.Name.Identifier.NativeId
                };

                Marshal.StructureToPtr(d, q, true);
                q = Inc(q, sizeOfResIconDirEnt);
            }

            Byte[] data = new Byte[sizeOfData];
            Marshal.Copy(p, data, 0, sizeOfData);

            Marshal.FreeHGlobal(p);

            return(data);

            // Major problem:
            // RawData of a directory contains the ResourceNames of the entries in the ResourceSource
            // seeming as this source does not exist, what should go in their place?

            // idea:
            // when a ResourceData is created from a file it is passed the current ResourceSource which it can 'preliminarily add' the datas to
            //	a UI will need to be presented to prompt the user for details if not in Simple Mode. A UI is presented anyway, so this works.

            // this way the IconCursorImageResourceData has been added to the ResourceSource (with the Action.Add property) and this method can
            //	just query ResourceName. Simple (sort-of)
        }
Beispiel #2
0
        private static StatelessResult OneOffAdd(String sourceFilename, ResourceTypeIdentifier typeId, ResourceIdentifier nameId, UInt16 langId, String dataFilename)
        {
            ResourceSource source = ResourceSource.Open(sourceFilename, false, ResourceSourceLoadMode.EnumerateOnly);

            ResourceData data = ResourceData.FromFileToAdd(dataFilename, langId, source);

            source.Add(typeId, nameId, langId, data);

            source.CommitChanges();

            return(StatelessResult.Success);
        }
Beispiel #3
0
        /// <summary>Associates all of the IconImages in this directory with the ResourceSource by creating IconCursorResourceData instances for each IconImage (with the specified Lang)</summary>
        public void BindToSource(ResourceSource source, UInt16 langId)
        {
            IconCursorImageResourceDataFactory factory = GetImageFactory();

            ResourceTypeIdentifier typeId = Type == IconType.Icon ? _iiTypeId : _ciTypeId;

            foreach (IconImage image in _images)
            {
                IconCursorImageResourceData rd = (IconCursorImageResourceData)factory.FromFileData(null, image.ImageData, Type == IconType.Icon);

                image.ResourceDataTyped = rd;

                source.Add(typeId, source.GetUnusedName(typeId), langId, rd);
            }
        }
Beispiel #4
0
        /// <summary>
        ///     Associates all of the IconImages in this directory with the ResourceSource by creating IconCursorResourceData
        ///     instances for each IconImage (with the specified Lang)
        /// </summary>
        public void BindToSource(ResourceSource source, ushort langId)
        {
            var factory = GetImageFactory();

            var typeId = Type == IconType.Icon ? _iiTypeId : _ciTypeId;

            foreach (var directoryMember in _images)
            {
                var image = (IconImage)directoryMember;
                var rd    = factory.FromFileData(null, image.ImageData, Type == IconType.Icon);

                image.ResourceDataTyped = rd;

                source.Add(typeId, source.GetUnusedName(typeId), langId, rd);
            }
        }
Beispiel #5
0
        public override object?ReadJson(JsonReader reader, Type objectType, object?existingValue, JsonSerializer serializer)
        {
            var resourceSource = new ResourceSource();

            var jObject = JObject.Load(reader);

            foreach (var property in jObject.Properties())
            {
                if (property.Name == "ignore_paths")
                {
                    resourceSource.IgnorePaths = JsonConvert.DeserializeObject <List <string> >(property.Value?.ToString());
                    continue;
                }

                resourceSource.Add(property.Name, property.Value);
            }

            return(resourceSource);
        }
Beispiel #6
0
        protected override Boolean PatchFile(String fileName)
        {
            List <PatchResource> patchResources = new List <PatchResource>();

            foreach (PatchResourceSet set in _resourceSets)
            {
                if (EvaluatePatchResourceSet(set, fileName))
                {
                    // HACK: This just adds them together into a massive list. If the same name is mentioned it'll be overwritten several times
                    // fortunately it isnt' very expensive as only the last "final" one counts, but could do with filtering at this stage maybe?

                    patchResources.AddRange(set.Resources);
                }
                else
                {
                    Package.Log.Add(LogSeverity.Info, "Expression evaluation non-one: " + set.Condition.ExpressionString + ", did not process " + set.Resources.Count + " resources");
                }
            }

            if (patchResources.Count == 0)
            {
                Package.Log.Add(LogSeverity.Warning, "No resources to patch: " + fileName);
                return(false);
            }

            try {
                // for now, use lazy-load under all circumstances. In future analyse the Resources list to see if it's necessary or not
                // but the performance impact is minimal and it's the safest option, so keep it as it is
                using (ResourceSource source = ResourceSource.Open(fileName, false, ResourceSourceLoadMode.LazyLoadData)) {
                    List <String> tempFiles = new List <String>();

                    foreach (PatchResource res in patchResources)
                    {
                        if (res.Source.StartsWith("comp:", StringComparison.OrdinalIgnoreCase))
                        {
                            CompositedImage comp = new CompositedImage(res.Source, Package.RootDirectory);

                            DirectoryInfo packageTempDirectory = new DirectoryInfo(P.Combine(Package.RootDirectory.FullName, "Temp"));
                            if (!packageTempDirectory.Exists)
                            {
                                packageTempDirectory.Create();
                            }

                            // I think not using the *.bmp extension messes up Bitmap import
                            String tempFileName = PackageUtility.GetUnusedFileName(P.Combine(packageTempDirectory.FullName, P.GetFileName(Path) + res.Name) + ".bmp");

                            comp.Save(tempFileName, System.Drawing.Imaging.ImageFormat.Bmp);

                            res.File = tempFileName;
                            tempFiles.Add(tempFileName);
                        }
                        else
                        {
                            res.File = res.Source;
                        }

                        if (!File.Exists(res.File))
                        {
                            Package.Log.Add(LogSeverity.Error, "Data File not found: " + res.File);
                            continue;
                        }

                        ResourceTypeIdentifier typeId = ResourceTypeIdentifier.CreateFromString(res.Type, true);
                        ResourceIdentifier     nameId = ResourceIdentifier.CreateFromString(res.Name);
                        UInt16 langId = String.IsNullOrEmpty(res.Lang) ? UInt16.MaxValue : UInt16.Parse(res.Lang, System.Globalization.NumberStyles.Integer, System.Globalization.CultureInfo.InvariantCulture);


                        if (langId == UInt16.MaxValue)                                // if the lang="" attribute was not specified

                        {
                            ResourceName name = source.GetName(typeId, nameId);

                            if (name == null)                              // if the specified name does not exist

                            {
                                if (res.Add)
                                {
                                    UInt16 sysLang = (UInt16)CultureInfo.InvariantCulture.LCID;

                                    ResourceData data = ResourceData.FromFileToAdd(res.File, sysLang, source);
                                    source.Add(typeId, nameId, sysLang, data);
                                }
                                else
                                {
                                    // Error

                                    String sourcePath = source.Name;

                                    Anolis.Core.Source.FileResourceSource frs = source as Anolis.Core.Source.FileResourceSource;
                                    if (frs != null)
                                    {
                                        sourcePath = frs.FileInfo.FullName;
                                    }

                                    Package.Log.Add(LogSeverity.Warning, "Resource name not found: " + sourcePath + '\\' + typeId.ToString() + '\\' + nameId.FriendlyName);
                                }
                            }
                            else
                            {
                                foreach (ResourceLang lang in name.Langs)
                                {
                                    ResourceData data = ResourceData.FromFileToUpdate(res.File, lang);
                                    lang.SwapData(data);
                                }
                            }
                        }
                        else                             // if the lang="" attribute was specified

                        {
                            ResourceLang lang = source.GetLang(typeId, nameId, langId);
                            if (lang == null)
                            {
                                ResourceData data = ResourceData.FromFileToAdd(res.File, langId, source);
                                source.Add(typeId, nameId, langId, data);
                            }
                            else
                            {
                                ResourceData data = ResourceData.FromFileToUpdate(res.File, lang);
                                lang.SwapData(data);
                            }
                        }
                    }                    //foreach

                    // note that Win32ResourceSource now recomptues the PE checksum by itself
                    source.CommitChanges();

                    foreach (String tempFile in tempFiles)
                    {
                        File.Delete(tempFile);
                    }

                    return(true);
                }                //using source
            } catch (AnolisException aex) {
                Package.Log.Add(LogSeverity.Error, "Patch Exception: " + aex.Message);

                if (File.Exists(fileName))
                {
                    File.Delete(fileName);
                }

                throw;
            }
        }