Beispiel #1
0
    public static void InitModDir(string dir)
    {
        Debug.Log("Initializing mod directory " + dir);

        if (!Directory.Exists(dir))
        {
            // Probably a mod in the mod directory
            dir = Path.Combine(ModsDirectory, dir);
        }

        // Fallback metadata in case none is found
        ETGModuleMetadata metadata = new ETGModuleMetadata()
        {
            Name    = Path.GetFileName(dir),
            Version = new Version(0, 0),
            DLL     = "mod.dll"
        };
        Assembly asm = null;

        // First read the metadata, ...
        string metadataPath = Path.Combine(dir, "metadata.txt");

        if (File.Exists(metadataPath))
        {
            using (FileStream fs = File.OpenRead(metadataPath)) {
                metadata = ETGModuleMetadata.Parse("", dir, fs);
            }
        }

        // ... then check if the dependencies are loaded ...
        foreach (ETGModuleMetadata dependency in metadata.Dependencies)
        {
            if (!DependencyLoaded(dependency))
            {
                Debug.LogWarning("DEPENDENCY " + dependency + " OF " + metadata + " NOT LOADED!");
                return;
            }
        }

        // ... then add an AssemblyResolve handler for all the .zip-ped libraries
        AppDomain.CurrentDomain.AssemblyResolve += metadata.GenerateModAssemblyResolver();

        // ... then everything else
        if (!File.Exists(metadata.DLL))
        {
            return;
        }
        if (metadata.Prelinked)
        {
            asm = Assembly.LoadFrom(metadata.DLL);
        }
        else
        {
            using (FileStream fs = File.OpenRead(metadata.DLL)) {
                asm = metadata.GetRelinkedAssembly(fs);
            }
        }

        Assets.Crawl(dir);

        Type[] types = asm.GetTypes();
        for (int i = 0; i < types.Length; i++)
        {
            Type type = types[i];
            if (!typeof(ETGModule).IsAssignableFrom(type) || type.IsAbstract)
            {
                continue;
            }

            ETGModule module = (ETGModule)type.GetConstructor(a_Type_0).Invoke(a_object_0);

            module.Metadata = metadata;

            GameMods.Add(module);
            AllMods.Add(module);
            ModuleTypes.Add(type);
            ModuleMethods.Add(new Dictionary <string, MethodInfo>());
        }

        Debug.Log("Mod " + metadata.Name + " initialized.");
    }
Beispiel #2
0
    public static void InitModZIP(string archive)
    {
        Debug.Log("Initializing mod ZIP " + archive);

        if (!File.Exists(archive))
        {
            // Probably a mod in the mod directory
            archive = Path.Combine(ModsDirectory, archive);
        }

        // Fallback metadata in case none is found
        ETGModuleMetadata metadata = new ETGModuleMetadata()
        {
            Name    = Path.GetFileNameWithoutExtension(archive),
            Version = new Version(0, 0),
            DLL     = "mod.dll"
        };
        Assembly asm = null;

        using (ZipFile zip = ZipFile.Read(archive)) {
            // First read the metadata, ...
            foreach (ZipEntry entry in zip.Entries)
            {
                if (entry.FileName == "metadata.txt")
                {
                    using (MemoryStream ms = new MemoryStream()) {
                        entry.Extract(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        metadata = ETGModuleMetadata.Parse(archive, "", ms);
                    }
                    break;
                }
            }

            // ... then check if the dependencies are loaded ...
            foreach (ETGModuleMetadata dependency in metadata.Dependencies)
            {
                if (!DependencyLoaded(dependency))
                {
                    Debug.LogWarning("DEPENDENCY " + dependency + " OF " + metadata + " NOT LOADED!");
                    return;
                }
            }

            // ... then add an AssemblyResolve handler for all the .zip-ped libraries
            AppDomain.CurrentDomain.AssemblyResolve += metadata.GenerateModAssemblyResolver();

            // ... then everything else
            foreach (ZipEntry entry in zip.Entries)
            {
                string entryName = entry.FileName.Replace("\\", "/");
                if (entryName == metadata.DLL)
                {
                    using (MemoryStream ms = new MemoryStream()) {
                        entry.Extract(ms);
                        ms.Seek(0, SeekOrigin.Begin);
                        if (metadata.Prelinked)
                        {
                            asm = Assembly.Load(ms.GetBuffer());
                        }
                        else
                        {
                            asm = metadata.GetRelinkedAssembly(ms);
                        }
                    }
                }
                else
                {
                    Assets.Map[Assets.RemoveExtension(entryName)] = new ETGModAssetMetadata(archive, entryName);
                }
            }
        }

        if (asm == null)
        {
            return;
        }

        Type[] types = asm.GetTypes();
        for (int i = 0; i < types.Length; i++)
        {
            Type type = types[i];
            if (!typeof(ETGModule).IsAssignableFrom(type) || type.IsAbstract)
            {
                continue;
            }

            ETGModule module = (ETGModule)type.GetConstructor(a_Type_0).Invoke(a_object_0);

            module.Metadata = metadata;

            GameMods.Add(module);
            AllMods.Add(module);
            ModuleTypes.Add(type);
            ModuleMethods.Add(new Dictionary <string, MethodInfo>());
        }

        Debug.Log("Mod " + metadata.Name + " initialized.");
    }