예제 #1
0
        // Awake() is the first function called in the lifecycle of a Unity3D MonoBehaviour.  In the case of KSP,
        // it happens to be called right before the game's PSystem is instantiated from PSystemManager.Instance.systemPrefab
        public void Awake()
        {
            // Abort, if KSP isn't compatible
            if (!CompatibilityChecker.IsCompatible())
            {
                string supported = CompatibilityChecker.version_major + "." + CompatibilityChecker.version_minor + "." + CompatibilityChecker.Revision;
                string current   = Versioning.version_major + "." + Versioning.version_minor + "." + Versioning.Revision;
                Debug.LogWarning("[Kopernicus] Detected incompatible install.\nCurrent version of KSP: " + current + ".\nSupported version of KSP: " + supported + ".\nPlease wait, until Kopernicus gets updated to match your version of KSP.");
                Debug.Log("[Kopernicus] Aborting...");

                // Abort
                Destroy(this);
                return;
            }

            // We're ALIVE
            Logger.Default.SetAsActive();
            Logger.Default.Log("Injector.Awake(): Begin");

            // Parser Config
            ParserOptions.Register("Kopernicus", new ParserOptions.Data {
                errorCallback = e => Logger.Active.LogException(e), logCallback = s => Logger.Active.Log(s)
            });

            // Yo garbage collector - we have work to do man
            DontDestroyOnLoad(this);

            // If the planetary manager does not work, well, error out
            if (PSystemManager.Instance == null)
            {
                // Log the error
                Logger.Default.Log("Injector.Awake(): If PSystemManager.Instance is null, there is nothing to do");
                return;
            }

            // Backup the old prefab
            StockSystemPrefab = PSystemManager.Instance.systemPrefab;

            // Get the current time
            DateTime start = DateTime.Now;

            // Get the configNode
            ConfigNode kopernicus = GameDatabase.Instance.GetConfigs(rootNodeName)[0].config;

            // THIS IS WHERE THE MAGIC HAPPENS - OVERWRITE THE SYSTEM PREFAB SO KSP ACCEPTS OUR CUSTOM SOLAR SYSTEM AS IF IT WERE FROM SQUAD
            PSystemManager.Instance.systemPrefab = Parser.CreateObjectFromConfigNode <Loader>(kopernicus, "Kopernicus").systemPrefab;

            // Clear space center instance so it will accept nouveau Kerbin
            SpaceCenter.Instance = null;

            // Add a handler so that we can do post spawn fixups.
            PSystemManager.Instance.OnPSystemReady.Add(PostSpawnFixups);

            // Done executing the awake function
            TimeSpan duration = (DateTime.Now - start);

            Logger.Default.Log("Injector.Awake(): Completed in: " + duration.TotalMilliseconds + " ms");
            Logger.Default.Flush();
        }
예제 #2
0
        // Awake() is the first function called in the lifecycle of a Unity3D MonoBehaviour.  In the case of KSP,
        // it happens to be called right before the game's PSystem is instantiated from PSystemManager.Instance.systemPrefab
        public void Awake()
        {
            // Abort, if KSP isn't compatible
            if (!CompatibilityChecker.IsCompatible())
            {
                String supported = CompatibilityChecker.VERSION_MAJOR + "." + CompatibilityChecker.VERSION_MINOR + "." +
                                   CompatibilityChecker.REVISION;
                String current = Versioning.version_major + "." + Versioning.version_minor + "." + Versioning.Revision;
                Debug.LogWarning("[Kopernicus] Detected incompatible install.\nCurrent version of KSP: " + current +
                                 ".\nSupported version of KSP: " + supported +
                                 ".\nPlease wait, until Kopernicus gets updated to match your version of KSP.");
                Debug.Log("[Kopernicus] Aborting...");

                // Abort
                Destroy(this);
                return;
            }

            // Log the current version to the log
            String kopernicusVersion = CompatibilityChecker.VERSION_MAJOR + "." +
                                       CompatibilityChecker.VERSION_MINOR + "." + CompatibilityChecker.REVISION +
                                       "-" + CompatibilityChecker.KOPERNICUS;
            String kspVersion = Versioning.version_major + "." + Versioning.version_minor + "." +
                                Versioning.Revision;

            Debug.Log("[Kopernicus] Running Kopernicus " + kopernicusVersion + " on KSP " + kspVersion);


            // Wrap this in a try - catch block so we can display a warning if Kopernicus fails to load for some reason
            try
            {
                // We're ALIVE
                IsInPrefab = true;
                Logger.Default.SetAsActive();
                Logger.Default.Log("Injector.Awake(): Begin");

                //Utility.DumpBuiltinTextures();

                // Parser Config
                ParserOptions.Register("Kopernicus",
                                       new ParserOptions.Data
                {
                    ErrorCallback = e => Logger.Active.LogException(e), LogCallback = s => Logger.Active.Log(s)
                });

                // Yo garbage collector - we have work to do man
                DontDestroyOnLoad(this);

                // If the planetary manager does not work, well, error out
                if (PSystemManager.Instance == null)
                {
                    // Log the error
                    Logger.Default.Log("Injector.Awake(): If PSystemManager.Instance is null, there is nothing to do");
                    DisplayWarning();
                    return;
                }

                // Was the system template modified?
                #if !DEBUG
                String systemCfgPath = KSPUtil.ApplicationRootPath + "GameData/Kopernicus/Config/System.cfg";
                if (File.Exists(systemCfgPath))
                {
                    Byte[] data     = File.ReadAllBytes(systemCfgPath);
                    SHA256 sha256   = SHA256.Create();
                    String checksum = BitConverter.ToString(sha256.ComputeHash(data));
                    checksum = checksum.Replace("-", "");
                    checksum = checksum.ToLower();
                    if (checksum != CONFIG_CHECKSUM)
                    {
                        throw new Exception(
                                  "The file 'Kopernicus/Config/System.cfg' was modified directly without ModuleManager");
                    }
                }
                #endif

                // Backup the old prefab
                StockSystemPrefab = PSystemManager.Instance.systemPrefab;

                // Fire Pre-Load Event
                Events.OnPreLoad.Fire();

                // Get the current time
                DateTime start = DateTime.Now;

                // Get the configNode
                ConfigNode kopernicus = GameDatabase.Instance.GetConfigs(ROOT_NODE_NAME)[0].config;

                // THIS IS WHERE THE MAGIC HAPPENS - OVERWRITE THE SYSTEM PREFAB SO KSP ACCEPTS OUR CUSTOM SOLAR SYSTEM AS IF IT WERE FROM SQUAD
                PSystemManager.Instance.systemPrefab =
                    Parser.CreateObjectFromConfigNode <Loader>(kopernicus, "Kopernicus").SystemPrefab;

                // Clear space center instance so it will accept nouveau Kerbin
                SpaceCenter.Instance = null;

                // Add a handler so that we can do post spawn fixups.
                PSystemManager.Instance.OnPSystemReady.Add(PostSpawnFixups);

                // Fire Post-Load Event
                Events.OnPostLoad.Fire(PSystemManager.Instance.systemPrefab);

                // Done executing the awake function
                TimeSpan duration = DateTime.Now - start;
                Logger.Default.Log("Injector.Awake(): Completed in: " + duration.TotalMilliseconds + " ms");
                Logger.Default.Flush();
                IsInPrefab = false;
            }
            catch (Exception e)
            {
                // Log the exception
                Debug.LogException(e);

                // Open the Warning popup
                DisplayWarning();
            }
        }