ModManager.LoadMod C# (CSharp) Method

LoadMod() public method

public LoadMod ( string Name ) : bool
Name string
return bool
    public bool LoadMod(string Name)
    {
        if (Mods.ContainsKey(Name))
            return true;

        string ModDirectory = Environment.CurrentDirectory.Replace('\\', '/') + "/Mods/";

        try
        {
            foreach (DirectoryInfo Directory in new DirectoryInfo(ModDirectory).GetDirectories())
            {
                FileInfo[] Path = Directory.GetFiles("*.mod");

                if (Path.Length == 0)
                {
                    Debug.Log("Found no mods bundles at '" + Directory.FullName + "'");
                }
                else if(Path[0].Name == Name + ".mod")
                {
                    FileInfo ModPath = Path[0];

                    try
                    {
                        FileStream TheFile = ModPath.OpenRead();
                        StreamReader TheReader = new StreamReader(TheFile);

                        string Content = TheReader.ReadToEnd();

                        TheReader.Close();
                        TheFile.Close();

                        Mod TheMod = new Mod();
                        TheMod.JSONSource = Content;
                        TheMod.Path = Directory.FullName.Replace('\\', '/');

                        if (!TheMod.Init())
                        {
                            Debug.Log("Failed to load mod bundle '" + ModPath.FullName + "': Load failure");

                            return false;
                        }

                        Mods.Add(Name, TheMod);

                        Debug.Log("Loaded mod '" + ModPath.FullName + "'");

                        return true;
                    }
                    catch (Exception e)
                    {
                        Debug.Log("Failed to load mod bundle '" + ModPath.FullName + "': " + e.ToString());

                        return false;
                    }
                }
            }
        }
        catch (Exception e)
        {
            return false;
        }

        return false;
    }

Usage Example

    void Awake()
    {
        // Get all files from the specified mods directory
        string[] modPaths = Directory.GetFiles(modFolder);

        // Try to load all valid mods
        foreach (string mod in modPaths)
        {
            ModManager.LoadMod(mod);
        }

        // Set the scene containing the default chunk
        if (terrainObject != null)
        {
            ChunkManager.Initialize(new InfiniTerrain(terrainObject));
        }

        // Initialize the interface manager with the specified skin
        InterfaceManager.Initialize(defaultSkin);

        // Start generating engine ticks
        StartCoroutine(GenerateEngineTick());
    }