Esempio n. 1
0
        internal static void LoadFromFile(string filepath, string symbolspath = null)
        {
            if (string.IsNullOrEmpty(filepath))
            {
                return;
            }

            if (MelonDebug.IsEnabled())
            {
                Assembly melonassembly = null;
                try
                {
                    melonassembly = Assembly.LoadFrom(filepath);
                }
                catch (Exception ex)
                {
                    MelonLogger.Error($"Failed to Load Assembly for {filepath}: {ex}");
                    return;
                }

                if (melonassembly == null)
                {
                    MelonLogger.Error($"Failed to Load Assembly for {filepath}: Assembly.LoadFrom returned null");;
                    return;
                }

                MelonHandler.LoadFromAssembly(melonassembly, filepath);
            }
            else
            {
                byte[] symbolsdata = new byte[0];
                if (string.IsNullOrEmpty(symbolspath))
                {
                    symbolspath = Path.Combine(Path.GetDirectoryName(filepath), $"{Path.GetFileNameWithoutExtension(filepath)}.mdb");
                }
                if (!File.Exists(symbolspath))
                {
                    symbolspath = $"{filepath}.mdb";
                }
                if (File.Exists(symbolspath))
                {
                    try
                    {
                        symbolsdata = File.ReadAllBytes(symbolspath);
                    }
                    catch (Exception ex)
                    {
                        if (MelonDebug.IsEnabled())
                        {
                            MelonLogger.Warning($"Failed to Load Symbols for {filepath}: {ex}");
                        }
                    }
                }

                byte[] filedata = null;
                try
                {
                    //filedata = File.ReadAllBytes(filepath);
                    filedata = VRCModsSupport.GetFixedAssemblyBytes(filepath);
                }
                catch (Exception ex)
                {
                    MelonLogger.Error($"Failed to Read File Data for {filepath}: {ex}");
                    return;
                }

                LoadFromByteArray(filedata, symbolsdata, filepath);
            }
        }
Esempio n. 2
0
        internal static void LoadFromFile(string filepath)
        {
            if (string.IsNullOrEmpty(filepath))
            {
                return;
            }

            Dictionary <string, ByteArrayPair> pairs = new Dictionary <string, ByteArrayPair>();
            bool source_code_detected = false;

            try
            {
                using (var filestream = File.OpenRead(filepath))
                    using (var zipstream = new ZipInputStream(filestream))
                    {
                        ZipEntry entry;
                        while ((entry = zipstream.GetNextEntry()) != null)
                        {
                            if (string.IsNullOrEmpty(entry.Name))
                            {
                                continue;
                            }

                            string entry_path     = entry.Name;
                            string entry_filename = Path.GetFileName(entry.Name);
                            if (string.IsNullOrEmpty(entry_filename))
                            {
                                continue;
                            }

                            string extension = Path.GetExtension(entry_filename).ToLowerInvariant();
                            if (extension.Equals(".cs"))
                            {
                                source_code_detected = true;
                                break;
                            }

                            bool is_dll = extension.Equals(".dll");
                            bool is_mdb = extension.Equals(".mdb");
                            if (!is_dll && !is_mdb)
                            {
                                continue;
                            }

                            string entrypairid = entry_path;
                            if (is_mdb)
                            {
                                string file_name_no_ext = Path.GetFileNameWithoutExtension(entrypairid).ToLowerInvariant();
                                if (file_name_no_ext.EndsWith(".dll"))
                                {
                                    entrypairid = Path.Combine(Path.GetDirectoryName(entry_path), Path.GetFileNameWithoutExtension(entrypairid));
                                }
                            }

                            ByteArrayPair byteArrayPair = null;
                            if (!pairs.TryGetValue(entrypairid, out byteArrayPair))
                            {
                                byteArrayPair     = new ByteArrayPair();
                                pairs[entry.Name] = byteArrayPair;
                            }

                            try
                            {
                                using (MemoryStream memorystream = new MemoryStream())
                                {
                                    int    size   = 0;
                                    byte[] buffer = new byte[32768];
                                    while (true)
                                    {
                                        size = zipstream.Read(buffer, 0, buffer.Length);
                                        if (size > 0)
                                        {
                                            memorystream.Write(buffer, 0, size);
                                        }
                                        else
                                        {
                                            break;
                                        }
                                    }
                                    if (is_dll)
                                    {
                                        byteArrayPair.filedata = VRCModsSupport.GetFixedAssemblyBytes(memorystream); //byteArrayPair.filedata = memoryStream.ToArray();
                                    }
                                    else if (is_mdb)
                                    {
                                        byteArrayPair.symbolsdata = memorystream.ToArray();
                                    }
                                }
                            }
                            catch (Exception ex)
                            {
                                MelonLogger.Error($"Failed to Read Entry {entry.Name} in ZIP Archive {filepath}: {ex}");
                                continue;
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                MelonLogger.Error($"Failed to Read ZIP Archive {filepath}: {ex}");
                return;
            }

            if (source_code_detected)
            {
                MelonLogger.Error($"Source Code ZIP Detected {filepath}");
                return;
            }

            if (pairs.Count <= 0)
            {
                return;
            }

            foreach (KeyValuePair <string, ByteArrayPair> keyValuePair in pairs)
            {
                if ((keyValuePair.Value == null) ||
                    (keyValuePair.Value.filedata == null))
                {
                    continue;
                }
                DLL.LoadFromByteArray(keyValuePair.Value.filedata, keyValuePair.Value.symbolsdata, filepath);
            }
        }