Пример #1
0
        /// <summary>
        /// Loads assembly from assemblies cache or if not found, from raw data.
        /// </summary>
        private static Assembly loadAssembly(IncludedObjectConfigBase assemblyConfig)
        {
            Assembly alreadyCachedAssembly;

            if (AssembliesCachedRegistry.TryGetCachedAssembly(assemblyConfig.ID, out alreadyCachedAssembly))
            {
                return(alreadyCachedAssembly);
            }
            //
            long     decodedDataLength;
            Assembly assembly = Assembly.Load(loadRawData(assemblyConfig, out decodedDataLength));

            //
            AssembliesCachedRegistry.AddAssemblyToCache(assemblyConfig.ID, assembly);
            return(assembly);
        }
Пример #2
0
        /// <summary>
        /// Retrieves decompressed data block for specified config of included object.
        /// </summary>
        private static byte[] loadRawData(IncludedObjectConfigBase configBase, out long rawDataLength)
        {
            byte[] bytes;
            //
            switch (configBase.IncludeMethod.IncludeMethodKind)
            {
            case IncludeMethodKind.File: {
                string fullPath = configurePath(configBase.IncludeMethod.FileLoadFromPath);
                bytes = File.ReadAllBytes(fullPath);
                break;
            }

            case IncludeMethodKind.Overlay: {
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                if (executingAssembly.Location == null)
                {
                    throw new InvalidOperationException("Cannot get location of executing assembly.");
                }
                //
                using (FileStream stream = File.OpenRead(executingAssembly.Location)) {
                    if (inititalOverlayOffset == 0)
                    {
                        stream.Seek(-4, SeekOrigin.End);
                        for (int i = 0; i < 3; i++)
                        {
                            inititalOverlayOffset |= (stream.ReadByte() << (8 * (3 - i)));
                        }
                        stream.Seek(0, SeekOrigin.Begin);
                    }
                    //
                    stream.Seek(inititalOverlayOffset + configBase.IncludeMethod.OverlayOffset, SeekOrigin.Begin);
                    bytes = new byte[configBase.IncludeMethod.OverlayLength];
                    if (stream.Read(bytes, 0, bytes.Length) != configBase.IncludeMethod.OverlayLength)
                    {
                        throw new InvalidOperationException("Cannot read overlay. Image may be corrupted.");
                    }
                }
                break;
            }

            case IncludeMethodKind.Resource: {
                string   resourceName      = configBase.IncludeMethod.ResourceName;
                Assembly executingAssembly = Assembly.GetExecutingAssembly();
                Stream   stream            = executingAssembly.GetManifestResourceStream(resourceName);
                if (stream == null)
                {
                    stream = executingAssembly.GetManifestResourceStream(executingAssembly.GetName().Name + "." + resourceName);
                    if (stream == null)
                    {
                        throw new InvalidOperationException("Cannot load resource by name.");
                    }
                }
                using (stream) {
                    bytes = new byte[stream.Length];
                    stream.Read(bytes, 0, unchecked ((int)stream.Length));
                }
                break;
            }

            default: {
                throw new InvalidOperationException("Unknown type.");
            }
            }
            return(LzmaHelper.Decode(bytes, bytes.LongLength, out rawDataLength));
        }