コード例 #1
0
        public void TestAccelerator()
        {
            string filename = Path.Combine(Environment.GetEnvironmentVariable("WINDIR"), "explorer.exe");

            Predicate <Accelerator> pReload = (Accelerator a) => { return(a.Key == "VK_F5" && a.Command == 41061); };

            using (ResourceInfo ri = new ResourceInfo())
            {
                ri.Load(filename);
                foreach (AcceleratorResource rc in ri[Kernel32.ResourceTypes.RT_ACCELERATOR])
                {
                    Console.WriteLine("Current AcceleratorResource: {0}, {1}", rc.Name, rc.TypeName);
                    Console.WriteLine(rc + "\n------------------------------------\n");

                    Accelerator newAC = new Accelerator();
                    newAC.Key     = "J";
                    newAC.Command = 41008;
                    newAC.Flags   = User32.AcceleratorVirtualKey.VIRTKEY | User32.AcceleratorVirtualKey.NOINVERT | User32.AcceleratorVirtualKey.CONTROL;

                    try
                    {
                        Accelerator tst = rc.Accelerators.Find(pReload);
                        int         loc = rc.Accelerators.FindIndex(pReload);
                        rc.Accelerators.Remove(tst);

                        tst.Key = "VK_NUMPAD9";
                        tst.addFlag(User32.AcceleratorVirtualKey.CONTROL);
                        tst.removeFlag(User32.AcceleratorVirtualKey.ALT | User32.AcceleratorVirtualKey.NOINVERT);
                        rc.Accelerators.Insert(loc, tst);
                        rc.Accelerators.Add(newAC);

                        List <Accelerator> acList = new List <Accelerator>();

                        Accelerator acA = new Accelerator();
                        acA.Command = 413;
                        acList.Add(acA);
                        acList[0].addFlag(User32.AcceleratorVirtualKey.SHIFT);
                        acList[0].Key = "VK_F1";

                        Accelerator acB = new Accelerator();
                        acB.Flags = User32.AcceleratorVirtualKey.VIRTKEY | User32.AcceleratorVirtualKey.NOINVERT;
                        acB.Key   = "Z";
                        acList.Add(acB);

                        rc.Accelerators.AddRange(acList);
                        Console.WriteLine("Modified AcceleratorResource: " + rc.Name);
                        Console.WriteLine(rc);
                        Console.ReadLine();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Error: " + e.ToString());
                        Console.ReadLine();
                        Environment.Exit(-1);
                    }
                    ri.Unload();
                }
            }
        }
コード例 #2
0
ファイル: ResourcesDB.cs プロジェクト: xiaolinggnb/CodeZero
    public void UnloadResource(string key)
    {
        ResourceInfo info = null;

        if (m_resourcesInfoDic.TryGetValue(key, out info))
        {
            info.Unload();
            info = null;
            m_resourcesInfoDic.Remove(key);
        }
    }
コード例 #3
0
        public bool UpdateString(ushort id, string data)
        {
            StringResource sr = new StringResource();
            sr.Language = 1033;
            sr.Name = new ResourceId(StringResource.GetBlockId(id));

            //load the data that was already in file
            ResourceInfo ri = new ResourceInfo();
            ri.Load(file);
            foreach (StringResource rc in ri[Kernel32.ResourceTypes.RT_STRING])
            {
                foreach (KeyValuePair<ushort,string> key in rc.Strings)
                    sr[key.Key] = key.Value;
            }
                
            ri.Unload();
            sr[id] = data;
            sr.SaveTo(file);
            return true;
        }
コード例 #4
0
        public override void CopyResources(string src, string dest)
        {
            ResourceInfo srcInfo = new ResourceInfo();
            ResourceInfo dstInfo = new ResourceInfo();

            srcInfo.Load(src);
            dstInfo.Load(dest);

            Kernel32.ResourceTypes resType = 0;
            switch (ResourceType)
            {
            case ResourceType.Icon:
                resType = Kernel32.ResourceTypes.RT_GROUP_ICON;
                break;

            case ResourceType.Bitmap:
                resType = Kernel32.ResourceTypes.RT_BITMAP;
                break;

            default:
                Debug.Fail("Unknown resource type");
                break;
            }

            List <Resource> resourcesToReplace = new List <Resource>();

            foreach (Resource res in srcInfo[resType])
            {
                if (Ids == null || Ids.Contains(res.Name.Name))
                {
                    // Find matching resource in destination file
                    Resource dstRes = dstInfo[resType].Single(dr => dr.Name.Equals(res.Name));

                    res.LoadFrom(src);
                    dstRes.LoadFrom(dest);

                    res.Language = dstRes.Language;

                    // Do some checks
                    if (ResourceType == VSIconSwitcher.ResourceType.Icon)
                    {
                        IconDirectoryResource srcIconDirectory = res as IconDirectoryResource;
                        IconDirectoryResource dstIconDirectory = dstRes as IconDirectoryResource;
                        //Debug.Assert(srcIconDirectory.Icons.Count == dstIconDirectory.Icons.Count, "Source/destination icon directory count should match.");
                    }
                    else if (ResourceType == VSIconSwitcher.ResourceType.Bitmap)
                    {
                        BitmapResource srcBitmap = res as BitmapResource;
                        BitmapResource dstBitmap = dstRes as BitmapResource;
                        Debug.Assert(srcBitmap.Bitmap.Header.biHeight == dstBitmap.Bitmap.Header.biHeight, "Source/destination bitmap height dimension should match.");
                        Debug.Assert(srcBitmap.Bitmap.Header.biWidth == dstBitmap.Bitmap.Header.biWidth, "Source/destination bitmap height dimension should match.");
                    }

                    resourcesToReplace.Add(res);
                }
            }

            dstInfo.Unload();

            foreach (Resource res in resourcesToReplace)
            {
                res.SaveTo(dest);
            }

            srcInfo.Unload();
        }
コード例 #5
0
        private void updateResource()
        {
            string filenameDownloader = WorkingDir + DownloaderFileName;
            string filenameDownloaderSatelliteResource = WorkingDir + constDownloaderSatelliteResource;

            // Look up the satellite resource
            ResourceInfo rcInfo = new ResourceInfo();

            rcInfo.Load(filenameDownloader);
            rcInfo.Unload(); // Release the module so its can be saved

            // Chinese Satellite Resource
            msResIdToFind = 1000;
            if (AppResource.Culture.Name == "en-US")
            {
                // English Satellite Resource
                msResIdToFind = 1002;
            }

            GenericResource satelliteRC = (GenericResource)rcInfo.Resources[new ResourceId(1001)].Find(FindRes);

            byte[]       temp         = satelliteRC.WriteAndGetBytes();
            MemoryStream memSatellite = new MemoryStream(1024 * 10);

            memSatellite.Write(temp, 0, temp.Length);
            memSatellite.Position = 0;
            //Create the decompressed file.
            using (FileStream fileSatellite = File.Create(filenameDownloaderSatelliteResource))
            {
                using (GZipStream Decompress = new GZipStream(memSatellite, CompressionMode.Decompress))
                {
                    readWriteStream(Decompress, fileSatellite);
                }
            }
            // Load the satellite resource
            rcInfo = new ResourceInfo();
            rcInfo.Load(filenameDownloaderSatelliteResource);
            rcInfo.Unload(); // Release the module so its can be saved
            // Begin the batch update to the designated module to speed up the process
            ResourceInfo.BeginBatchUpdate(filenameDownloaderSatelliteResource);
            try
            {
                // ==========================
                // Modify the Banner
                // ==========================
                // Look up the bitmap resource
                if (BannerBitmapFullPath.Trim() != "")
                {
                    msResIdToFind = 207;
                    BitmapResource bmpRC = (BitmapResource)rcInfo[Kernel32.ResourceTypes.RT_BITMAP].Find(FindRes);
                    if (bmpRC == null)
                    {
                        throw new ApplicationException(
                                  AppResource.ResBitmap207NotFound);
                    }

                    BitmapFile bmpFile = new BitmapFile(BannerBitmapFullPath.Trim());
                    bmpRC.Bitmap = bmpFile.Bitmap;
                    bmpRC.SaveTo(filenameDownloaderSatelliteResource);
                }
                // ==========================
                // Modify the Icon
                // ==========================
                // Look up the ICON resource
                if (IconFullPath.Trim() != "")
                {
                    msResIdToFind = 128;
                    IconDirectoryResource icoRC = (IconDirectoryResource)rcInfo[Kernel32.ResourceTypes.RT_GROUP_ICON].Find(FindRes);
                    if (icoRC == null)
                    {
                        throw new ApplicationException(AppResource.ResIcon128NotFound);
                    }

                    IconFile icoFile = new IconFile(IconFullPath.Trim());
                    uint     j       = 1;
                    icoRC.Icons.RemoveRange(0, icoRC.Icons.Count);
                    icoRC.SaveTo(filenameDownloaderSatelliteResource);
                    foreach (IconFileIcon icoFileIcon in icoFile.Icons)
                    {
                        IconResource icoRes = new IconResource(icoFileIcon, new ResourceId(j), 1033);
                        icoRes.Name = new ResourceId(j++);
                        icoRC.Icons.Add(icoRes);
                    }
                    icoRC.SaveTo(filenameDownloaderSatelliteResource);
                }
                // ==========================
                // Modify the main dialog box
                // ==========================
                // Look up the dialog resource
                msResIdToFind = 201;
                DialogResource dlgRC = (DialogResource)rcInfo[Kernel32.ResourceTypes.RT_DIALOG].Find(FindRes);
                if (dlgRC == null)
                {
                    throw new ApplicationException(AppResource.ResDialog201NotFound);
                }

                // Find the designated label control
                msCtrlIdToFind = 1010;
                DialogTemplateControlBase ctrl = dlgRC.Template.Controls.Find(FindDlgControl);
                ctrl.CaptionId.Name = string.Format(ctrl.CaptionId.Name, DownloaderDisplayName);
                // Find the designated link control
                msCtrlIdToFind      = 1006;
                ctrl                = dlgRC.Template.Controls.Find(FindDlgControl);
                ctrl.CaptionId.Name = string.Format(ctrl.CaptionId.Name, DownloaderHomeUrl);
                dlgRC.SaveTo(filenameDownloaderSatelliteResource);
                // ===================================================
                // Embed the specified .Torrent file into the resource
                // ===================================================
                // Look up the torrent resource
                msResIdToFind = 1021;
                GenericResource torrentRC = (GenericResource)rcInfo.Resources[new ResourceId(1022)].Find(FindRes);
                if (torrentRC == null)
                {
                    throw new ApplicationException(AppResource.ResTorrentSlot2011NotFound);
                }

                FileStream fs = new FileStream(MetafileFullPath, FileMode.Open);
                temp = new byte[fs.Length];
                fs.Read(temp, 0, temp.Length);
                fs.Close();
                torrentRC.Data = temp;
                torrentRC.SaveTo(filenameDownloaderSatelliteResource);
                // ===================================================
                // Embed the specified disclaimer file into the resource
                // ===================================================
                // Look up the disclaimer resource
                if (DisclaimerFullPath.Trim() != "")
                {
                    msResIdToFind = 40111;
                    GenericResource disclaimerRC = (GenericResource)rcInfo.Resources[new ResourceId(40112)].Find(FindRes);
                    if (disclaimerRC == null)
                    {
                        throw new ApplicationException(AppResource.ResDisclaimerSlot40112NotFound);
                    }

                    fs   = new FileStream(DisclaimerFullPath.Trim(), FileMode.Open);
                    temp = new byte[fs.Length];
                    fs.Read(temp, 0, temp.Length);
                    fs.Close();
                    disclaimerRC.Data = temp;
                    disclaimerRC.SaveTo(filenameDownloaderSatelliteResource);
                }
                // ==================================
                // Modify the strings in the resource
                // ==================================
                // Display Name
                StringResource stringRC = null;
                int[]          stringID = new int[] { 1112, 13015, 13016, 13017, 13018, 13019, 13027 };
                for (int i = 0; i < stringID.Length; i++)
                {
                    int sID = stringID[i];
                    // Check if the string resource has been loaded in the last string block or not.
                    if (stringRC == null || !stringRC.Strings.ContainsKey((ushort)sID))
                    {
                        msResIdToFind = StringResource.GetBlockId(sID);
                        stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                        if (stringRC == null)
                        {
                            throw new ApplicationException(
                                      string.Format(AppResource.ResStringTemplateNotFound, sID));
                        }
                    }
                    stringRC.Strings[(ushort)sID] = string.Format(stringRC.Strings[(ushort)sID], DownloaderDisplayName);
                    // Leave the modified string resource unsaved until all the strings in the string block are done.
                    if (stringID.Length == (i + 1) || !stringRC.Strings.ContainsKey((ushort)stringID[i + 1]))
                    {
                        stringRC.SaveTo(filenameDownloaderSatelliteResource);
                    }
                }
                // Google Analytics Profile ID
                msResIdToFind = StringResource.GetBlockId(1113);
                stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                if (stringRC == null)
                {
                    throw new ApplicationException(
                              string.Format(AppResource.ResStringTemplateNotFound, 1113));
                }

                stringRC.Strings[1113] = string.Format(stringRC.Strings[1113], GoogleAnalyticsProfileID);
                stringRC.SaveTo(filenameDownloaderSatelliteResource);
                // Downloader GUID
                msResIdToFind = StringResource.GetBlockId(40401);
                stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                if (stringRC == null)
                {
                    throw new ApplicationException(
                              string.Format(AppResource.ResStringTemplateNotFound, 40401));
                }

                stringRC.Strings[40401] = string.Format(stringRC.Strings[40401], DownloaderGuid);
                stringRC.SaveTo(filenameDownloaderSatelliteResource);
                // Online FAQ URL
                if (OnlineFaqUrl.Trim() != "")
                {
                    msResIdToFind = StringResource.GetBlockId(13020);
                    stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                    if (stringRC == null)
                    {
                        throw new ApplicationException(
                                  string.Format(AppResource.ResStringTemplateNotFound, 13020));
                    }

                    stringRC.Strings[13020] = OnlineFaqUrl;
                    stringRC.SaveTo(filenameDownloaderSatelliteResource);
                }
                // Event ID
                if (PromotionEventID.Trim() != "")
                {
                    msResIdToFind = StringResource.GetBlockId(1104);
                    stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                    if (stringRC == null)
                    {
                        throw new ApplicationException(
                                  string.Format(AppResource.ResStringTemplateNotFound, 1104));
                    }

                    stringRC.Strings[1104] = string.Format(stringRC.Strings[1104], PromotionEventID);
                    stringRC.SaveTo(filenameDownloaderSatelliteResource);
                }
                // Event Server URL
                if (PromotionEventID.Trim() != "")
                {
                    msResIdToFind = StringResource.GetBlockId(1101);
                    stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                    if (stringRC == null)
                    {
                        throw new ApplicationException(
                                  string.Format(AppResource.ResStringTemplateNotFound, 1101));
                    }

                    // Conver the URL to base64 encoded string
                    stringRC.Strings[1101] = Convert.ToBase64String(Encoding.ASCII.GetBytes(PromotionEventServerUrl));
                    stringRC.SaveTo(filenameDownloaderSatelliteResource);
                }
            }
            catch (Exception oEx)
            {
                throw oEx;
            }
            finally
            {
                // Commit the batch updates to the module
                ResourceInfo.EndBatchUpdate();
            }
        }
コード例 #6
0
        private void updateMainResource()
        {
            string filenameDownloader  = WorkingDir + DownloaderFileName;
            string filenameSatelliteRC = WorkingDir + constDownloaderSatelliteResource;

            ResourceInfo rcInfo = new ResourceInfo();

            rcInfo.Load(filenameDownloader);
            rcInfo.Unload(); // Release the module so its can be saved
            // Begin the batch update to the designated module to speed up the process
            ResourceInfo.BeginBatchUpdate(filenameDownloader);
            try
            {
                // ==========================
                // Modify the Icon
                // ==========================
                // Look up the ICON resource
                if (IconFullPath.Trim() != "")
                {
                    msResIdToFind = 128;
                    IconDirectoryResource icoRC = (IconDirectoryResource)rcInfo[Kernel32.ResourceTypes.RT_GROUP_ICON].Find(FindRes);
                    if (icoRC == null)
                    {
                        throw new ApplicationException(AppResource.ResIcon128NotFound);
                    }

                    IconFile icoFile = new IconFile(IconFullPath.Trim());
                    uint     j       = 1;
                    icoRC.Icons.RemoveRange(0, icoRC.Icons.Count);
                    icoRC.SaveTo(filenameDownloader);
                    foreach (IconFileIcon icoFileIcon in icoFile.Icons)
                    {
                        IconResource icoRes = new IconResource(icoFileIcon, new ResourceId(j), 1033);
                        icoRes.Name = new ResourceId(j++);
                        icoRC.Icons.Add(icoRes);
                    }
                    icoRC.SaveTo(filenameDownloader);
                }
                // ==================================
                // Modify the strings in the resource
                // ==================================
                // Downloader GUID
                StringResource stringRC = null;
                msResIdToFind = StringResource.GetBlockId(40401);
                stringRC      = (StringResource)rcInfo[Kernel32.ResourceTypes.RT_STRING].Find(FindRes);
                if (stringRC == null)
                {
                    throw new ApplicationException(
                              string.Format(AppResource.ResStringTemplateNotFound, 40401));
                }

                stringRC.Strings[40401] = string.Format(stringRC.Strings[40401], DownloaderGuid);
                stringRC.SaveTo(filenameDownloader);
                // ==========================================================
                // Embed the modified satellite resource into the main resource
                // ==========================================================
                // Look up the embedded satellite resource
                msResIdToFind = 1000;
                GenericResource satelliteRC = (GenericResource)rcInfo.Resources[new ResourceId(1001)].Find(FindRes);
                if (satelliteRC == null)
                {
                    throw new ApplicationException(
                              AppResource.ResEmbededSatelliteDllNotFound);
                }

                // Compresse the satellite RC file.
                MemoryStream memStream = new MemoryStream(1024 * 10);
                FileStream   fs        = new FileStream(
                    filenameSatelliteRC,
                    FileMode.Open);
                byte[] temp = new byte[fs.Length];
                fs.Read(temp, 0, temp.Length);
                fs.Close();
                using (GZipStream Compress = new GZipStream(memStream, CompressionMode.Compress))
                {
                    // Compress the data into the memory stream
                    Compress.Write(temp, 0, temp.Length);
                    // Read the compressed data from the memory stream
                    temp = new byte[memStream.Length + 1];
                    memStream.Position = 0;
                    // Leave the 1st byte as cheating byte and start to fill the array from the 2nd byte
                    for (long i = 1; i < memStream.Length + 1; i++)
                    {
                        temp[i] = Convert.ToByte(memStream.ReadByte());
                    }
                }
                memStream.Close();
                satelliteRC.Data = temp;
                satelliteRC.SaveTo(filenameDownloader);
                // Erease the english satellite resource since it has been used to overwrite the main satellite resource!!
                // Or it will remain useless if its choose to generate the chinese downloader!
                msResIdToFind = 1002;
                satelliteRC   = (GenericResource)rcInfo.Resources[new ResourceId(1001)].Find(FindRes);
                if (satelliteRC == null)
                {
                    throw new ApplicationException(
                              AppResource.ResEmbededSatelliteDllNotFound);
                }
                // The binary array must contain at least one element to prevent the failure in some cases
                satelliteRC.Data = new byte[1];
                satelliteRC.SaveTo(filenameDownloader);
            }
            catch (Exception oEx)
            {
                throw oEx;
            }
            finally
            {
                // Commit the batch updates to the module
                ResourceInfo.EndBatchUpdate();
            }
        }