Example #1
0
        void add_resources_to_assembly()
        {
            (string, string) escape_resource(string x)
            {
                var cp = x.IndexOf(',');
                return (cp != -1)
                    ? (x.Substring(0, cp), x.Substring(cp + 1))
                    // change name from /bar/bar/file.png to file.png namespace
                    : (x, Path.GetFileName(x));
            }

            /* we can embed resources only on mono or .NET 2.0 */
            foreach (var element in Manager.Options.EmbeddedResources)
            {
                var (file, name) = escape_resource(element);
                try
                {
                    if (File.Exists(file))
                    {
                        var fs = File.OpenRead(file);
                        var meth = _module_builder.GetType().GetMethod("DefineManifestResource");
                        if (meth == null)
                        {
                            try
                            {
                                var res = new byte[(int) fs.Length];
                                int pos = 0;
                                while (res.Length < pos)
                                {
                                    pos += fs.Read(res, pos, res.Length - pos);
                                }

                                var meth2 = _assembly_builder.GetType()
                                    .GetMethod("EmbedResource", BindingFlags.NonPublic | BindingFlags.Instance);
                                if (meth2 == null)
                                    Message.Error("cannot find API for saving resources");
                                else
                                    meth2.Invoke(_assembly_builder,
                                        new object[] {name, res, ResourceAttributes.Public});
                            }
                            finally
                            {
                                fs.Dispose();
                            }
                        }
                        else
                        {
                            // this method does the Dispose for us
                            meth.Invoke(_module_builder, new object[] {name, fs, ResourceAttributes.Public});
                        }
                    }
                    else
                        Message.Error("Could not find resource " + file);
                }
                catch (Exception e)
                {
                    Message.Error("Could not embed resource: " + e.Message);
                }
            }

            foreach (var element in Manager.Options.LinkedResources)
            {
                var (file, name) = escape_resource(element);
                try
                {
                    _assembly_builder.AddResourceFile(name, file);
                }
                catch (FileNotFoundException)
                {
                    Message.Error("Could not find resource " + file);
                }
                catch (System.ArgumentException e)
                {
                    Message.Error("Could not link resource: " + e.Message);
                }
            }

            var uresource = Manager.Options.UnmanagedResource;
            if (uresource != null)
            {
                try
                {
                    _module_builder.DefineUnmanagedResource(uresource);
                }
                catch (FileNotFoundException)
                {
                    Message.Error("Could not find resource" + uresource);
                }
                catch (System.ArgumentException e)
                {
                    Message.Error($"Could not embed unmanaged resource {uresource}: {e.Message}");
                }
            }
            else
            {
                this._assembly_builder.DefineVersionInfoResource();
            }
        }