/// <summary>
        ///     Loads mod's scripting features.
        ///     Returns true if successful.
        /// </summary>
        public static bool LoadEngine(bool verbose = false)
        {
            if (PythonEnvironment.LoadPythonAssembly())
            {
                try
                {
                    PythonEnvironment.InitializeEngine();
                    CreateScriptingEnvironment();
                    _component = Mod.Controller.AddComponent <ScriptComponent>();

                    if (verbose)
                    {
                        ModConsole.AddMessage(LogType.Log, $"[LenchScripterMod]: {Python.Execute("sys.version")}");
                    }

                    Mod.LoadedScripter = true;
                }
                catch (Exception e)
                {
                    if (verbose)
                    {
                        ModConsole.AddMessage(LogType.Log, "[LenchScripterMod]: Error while initializing python engine:", e.ToString());
                    }
                    Mod.LoadedScripter = false;
                }
            }
            else
            {
                Mod.LoadedScripter = false;
            }
            return(Mod.LoadedScripter);
        }
        private void InitializeMods()
        {
            string output = "";

            // Initialize mods marked as Preload first
            var preloadMods = loadedMods.Where(m => m.Mod.Preload);

            foreach (var mod in preloadMods)
            {
                mod.IsEnabled = Configuration.GetBool("modStatus:" + mod.Mod.Name, true);

                if (!mod.IsEnabled)
                {
                    output += "Not activating " + mod.Mod.DisplayName + "\n";
                    continue;
                }

                if (mod.Mod.BesiegeVersion != BesiegeVersion)
                {
                    Debug.LogWarning(mod.Mod.DisplayName
                                     + " is not targeted at the current Besiege version."
                                     + " Unexpected behaviour may occur.");
                }

                mod.Activate();

                output += "Activated " + mod.Mod.ToString() + "\n";
            }

            foreach (var mod in loadedMods)
            {
                if (mod.Mod.Preload)
                {
                    // Preload mods were already initialized
                    continue;
                }

                mod.IsEnabled = Configuration.GetBool("modStatus:" + mod.Mod.Name, true);

                if (!mod.IsEnabled)
                {
                    output += "Not activating " + mod.Mod.DisplayName + "\n";
                    continue;
                }

                if (mod.Mod.BesiegeVersion != BesiegeVersion)
                {
                    Debug.LogWarning(mod.Mod.DisplayName
                                     + " is not targeted at the current Besiege version."
                                     + " Unexpected behaviour may occur.");
                }

                mod.Activate();

                output += "Activated " + mod.Mod.ToString() + "\n";
            }

            ModConsole.AddMessage(LogType.Log, "Activated mods", output);
        }
Exemple #3
0
 protected void LoadBlock(Block block)
 {
     if (block.objs == null)
     {
         ModConsole.AddMessage(LogType.Error, "[BlockLoader]: Tried to load '" + block.name + "', but it's outdated.", "BlockLoader automatically stopped loading process.");
         return;
     }
     BlockLoader.AddModBlock(block);
 }
Exemple #4
0
        /// <summary>
        /// Loads mod's scripting features.
        /// </summary>
        public static void LoadScripter()
        {
            if (LoadedScripter)
            {
                throw new InvalidOperationException("Mod's scripting features already loaded.");
            }

            if (PythonEnvironment.LoadPythonAssembly())
            {
                PythonEnvironment.InitializeEngine();
                PythonEnvironment.ScripterEnvironment = new PythonEnvironment();

                ModConsole.AddMessage(LogType.Log, "[LenchScripterMod]: " + PythonEnvironment.ScripterEnvironment.Execute("sys.version"));
                LoadedScripter = true;
            }
        }
Exemple #5
0
    private void LoadMods()
    {
      var files = (new DirectoryInfo(Application.dataPath + "/Mods"))
        .GetFiles("*.dll");
      var loadingOutput = "";

      foreach (var fileInfo in files)
      {
        if (!fileInfo.Name.EndsWith(".no.dll", StringComparison.CurrentCulture)
          && fileInfo.Name != "SpaarModLoader.dll")
        {
          loadingOutput += LoadMod(fileInfo);
        }
      }

      ModConsole.AddMessage(LogType.Log, "Loaded mods", loadingOutput);
    }
        public static void InstallIronPython()
        {
            _filesDownloaded       = 0;
            _downloadingInProgress = true;
            _downloadButtonText    = "0.0 %";
            _infoText = "<b>Please wait</b>\n";
            if (!Directory.Exists(PythonEnvironment.LibPath))
            {
                Directory.CreateDirectory(PythonEnvironment.LibPath);
            }
            try
            {
                for (var fileIndex = 0; fileIndex < FilesRequired; fileIndex++)
                {
                    using (var client = new WebClient())
                    {
                        var i = fileIndex;

                        // delete existing file
                        if (File.Exists(PythonEnvironment.LibPath + FileNames[i]))
                        {
                            File.Delete(PythonEnvironment.LibPath + FileNames[i]);
                        }

                        // progress handler
                        client.DownloadProgressChanged += (sender, e) =>
                        {
                            ReceivedSize[i] = e.BytesReceived;
                            TotalSize[i]    = e.TotalBytesToReceive;
                            var progress = Convert.ToSingle(ReceivedSize.Sum()) / Convert.ToSingle(TotalSize.Sum()) *
                                           100f;
                            _downloadButtonText = progress.ToString("0.0") + " %";
                        };

                        // completion handler
                        client.DownloadFileCompleted += (sender, e) =>
                        {
                            if (e.Error != null)
                            {
                                // set error messages
                                ModConsole.AddMessage(LogType.Log,
                                                      "[LenchScripterMod]: Error downloading file:" + FileNames[i]);
                                ModConsole.AddMessage(LogType.Error, "\t" + e.Error.Message);
                                _infoText = FileNames[i] + " <color=red>✘</color>" +
                                            "\n\n<b><color=red>Download failed</color></b>\n" + e.Error.Message;

                                _downloadingInProgress = false;
                                _downloadButtonText    = "Retry";

                                // delete failed file
                                if (File.Exists(PythonEnvironment.LibPath + FileNames[i]))
                                {
                                    File.Delete(PythonEnvironment.LibPath + FileNames[i]);
                                }
                            }
                            else
                            {
                                ModConsole.AddMessage(LogType.Log,
                                                      "[LenchScripterMod]: File downloaded: " + FileNames[i]);
                                _infoText += "\n" + FileNames[i] + " <color=green>✓</color>";

                                _filesDownloaded++;
                                if (_filesDownloaded != FilesRequired)
                                {
                                    return;
                                }

                                // finish download and load assemblies
                                _downloadButtonText = "Loading";
                                if (Script.LoadEngine(true))
                                {
                                    Visible = false;
                                }
                                else
                                {
                                    _downloadButtonText = "Retry";
                                    _infoText           =
                                        "<b><color=red>Download failed</color></b>\nFailed to initialize Python engine.";
                                }
                                _downloadingInProgress = false;
                            }
                        };

                        // start download
                        client.DownloadFileAsync(
                            new Uri(BaseUri + PythonEnvironment.Version + "/" + FileNames[i]),
                            PythonEnvironment.LibPath + FileNames[i]);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log("[LenchScripterMod]: Error while downloading:");
                Debug.LogException(e);
                _downloadingInProgress = false;
                _downloadButtonText    = "Retry";
                _infoText = "<b><color=red>Download failed</color></b>\n" + e.Message;
            }
        }
Exemple #7
0
        private static void InstallIronPython()
        {
            downloading_in_progress = true;
            download_button_text    = "0.0 %";
            info_text = "<b>Please wait</b>\n";
            if (!Directory.Exists(Application.dataPath + "/Mods/Resources/LenchScripter/lib/"))
            {
                Directory.CreateDirectory(Application.dataPath + "/Mods/Resources/LenchScripter/lib/");
            }
            try
            {
                for (int file_index = 0; file_index < files_required; file_index++)
                {
                    using (var client = new WebClient())
                    {
                        var i = file_index;

                        // delete existing file
                        if (File.Exists(Application.dataPath + file_paths[i]))
                        {
                            File.Delete(Application.dataPath + file_paths[i]);
                        }

                        // progress handler
                        client.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) =>
                        {
                            received_size[i] = e.BytesReceived;
                            float progress = (Convert.ToSingle(received_size.Sum()) / Convert.ToSingle(total_size.Sum()) * 100f);
                            download_button_text = progress.ToString("0.0") + " %";
                        };

                        // completion handler
                        client.DownloadFileCompleted += (object sender, AsyncCompletedEventArgs e) =>
                        {
                            if (e.Error != null)
                            {
                                // set error messages
                                ModConsole.AddMessage(LogType.Log, "[LenchScripterMod]: Error downloading file:" + file_paths[i].Split('/').Last());
                                ModConsole.AddMessage(LogType.Error, "\t" + e.Error.Message);
                                info_text = file_paths[i].Split('/').Last() + " <color=red>✘</color>" +
                                            "\n\n<b><color=red>Download failed</color></b>\n" + e.Error.Message;

                                downloading_in_progress = false;
                                download_button_text    = "Retry";

                                // delete failed file
                                if (File.Exists(Application.dataPath + file_paths[i]))
                                {
                                    File.Delete(Application.dataPath + file_paths[i]);
                                }
                            }
                            else
                            {
                                ModConsole.AddMessage(LogType.Log, "[LenchScripterMod]: File downloaded: " + file_paths[i].Split('/').Last());
                                info_text += "\n" + file_paths[i].Split('/').Last() + " <color=green>✓</color>";

                                files_downloaded++;
                                if (files_downloaded == files_required)
                                {
                                    // finish download and load assemblies
                                    if (PythonEnvironment.LoadPythonAssembly())
                                    {
                                        download_button_text = "Complete";
                                        ScripterMod.LoadScripter();
                                        Instance.Visible = false;
                                        Destroy(Instance);
                                    }
                                    else
                                    {
                                        download_button_text = "Retry";
                                        info_text            = "<b><color=red>Download failed</color></b>\nFailed to initialize Python engine.";
                                    }
                                    downloading_in_progress = false;
                                }
                            }
                        };

                        // start download
                        client.DownloadFileAsync(
                            file_uris[i],
                            Application.dataPath + file_paths[i]);
                    }
                }
            }
            catch (Exception e)
            {
                Debug.Log("[LenchScripterMod]: Error while downloading:");
                Debug.LogException(e);
                downloading_in_progress = false;
                download_button_text    = "Retry";
                info_text = "<b><color=red>Download failed</color></b>\n" + e.Message;
            }
        }