Esempio n. 1
0
        /// <summary>
        /// Loads the configuration file.
        /// </summary>
        public void LoadConfiguration()
        {
            try
            {
                ConfigNode node = GetConfig();
                this.fundsMult   = float.Parse(node.GetValue("funds"));
                this.repMult     = float.Parse(node.GetValue("rep"));
                this.queueLength = int.Parse(node.GetValue("queueLength"));
            }
            catch (Exception e)
            {
                // Let's be honest: this try-catch is probably overkill, but on the other hand,
                // better safe than sorry I guess. If for some reason you can't parse the config values,
                // they are set to a totally-unbalanced default and the player is notified with a message.

                this.fundsMult   = 1000;
                this.repMult     = 1f;
                this.queueLength = 5;

                ScienceFunding.Log("There was an exception while loading the configuration: " + e.ToString());

                PostMessage(
                    "ScienceFunding error!",
                    "I'm sorry to break your immersion, but there seems to be an error in the configuration" +
                    " and ScienceFunding is not working properly right now. You should check the values in the config file.",
                    MessageSystemButton.MessageButtonColor.RED, MessageSystemButton.ButtonIcons.ALERT
                    );
            }

            ScienceFunding.Log("Configuration is " + this.fundsMult.ToString() + ", " + this.repMult.ToString() + ", " + this.queueLength.ToString());
        }
Esempio n. 2
0
        /// <summary>
        /// Saves the current message queue to the persistence file.
        /// This is necessary because the plugin's instance is destroyed between scenes.
        /// A static member might do the same thing, but it only works during one session
        /// while saving to the persistence file ensures that you don't screw up
        /// if the player reloads a previous save.
        /// </summary>
        /// <param name="node"></param>
        public override void OnSave(ConfigNode node)
        {
            try
            {
                ScienceFunding.Log("Saving message queue");

                // Ensure a fresh save each time
                if (node.HasNode("QUEUE"))
                {
                    node.RemoveNode("QUEUE");
                }
                ConfigNode queueNode = new ConfigNode("QUEUE");
                node.AddNode(queueNode);

                foreach (ScienceReport report in this.queue)
                {
                    queueNode.AddNode(report.ToConfigNode());
                }

                ScienceFunding.Log("Saved " + this.queue.Count.ToString() + " records");
            }
            catch (Exception e)
            {
                ScienceFunding.Log("Something went horribly wrong while saving: " + e.ToString());
                node.SetNode("QUEUE", new ConfigNode("QUEUE"));
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Science transmission handler: computes the funds and reputation boni
        /// and awards them to the player.
        /// </summary>
        public void ScienceReceivedHandler(float science, ScienceSubject sub, ProtoVessel v, bool whoKnows)
        {
            ScienceFunding.Log("Received " + science + " science points");

            // Don't bother for zero science
            if (science == 0)
            {
                return;
            }

            float funds = science * this.fundsMult;
            float rep   = science * this.repMult;

            // Cannot award funds if it's not a career
            if (Funding.Instance != null)
            {
                Funding.Instance.AddFunds(funds, TransactionReasons.ScienceTransmission);
                ScienceFunding.Log("Added " + funds + " funds");
            }

            // Cannot award reputation in sandbox
            if (Reputation.Instance != null)
            {
                Reputation.Instance.AddReputation(rep, TransactionReasons.ScienceTransmission);
                ScienceFunding.Log("Added " + rep + " reputation");
            }

            // Add the new report to the queue, and also send the notification if the queue has reached its limit.
            this.queue.Enqueue(new ScienceReport(funds, rep, sub.title));
            this.timer.Start();
        }
Esempio n. 4
0
        /// <summary>
        /// Load the saved queue.
        /// </summary>
        public override void OnLoad(ConfigNode node)
        {
            ScienceFunding.Log("Loading configuration");

            LoadConfiguration();

            this.queue = new Queue <ScienceReport>(this.queueLength);
            if (node.HasNode("QUEUE"))
            {
                // If there's a stored queue, try to parse each element to repopulate the message queue.
                foreach (ConfigNode reportNode in node.GetNodes("REPORT"))
                {
                    try
                    {
                        this.queue.Enqueue(ScienceReport.FromConfigNode(reportNode));
                    }
                    catch (Exception)
                    {
                        ScienceFunding.Log("Bad value found in queue, skipping:\n" + reportNode.ToString());
                        continue;
                    }
                }
            }
            else
            {
                ScienceFunding.Log("No node to load");
                node.AddNode(new ConfigNode("QUEUE"));
            }

            ScienceFunding.Log("Loaded " + this.queue.Count.ToString() + " records");
        }
Esempio n. 5
0
        /// <summary>
        /// "Constructor": it's not really the constructor, but Unity calls it
        /// immediately after the constructor, so...
        /// </summary>
        public override void OnAwake()
        {
            // Timer callback to send the report
            this.timer.OnTimer = this.OnTimer;

            // Start listening on the event to award the rewards
            GameEvents.OnScienceRecieved.Add(ScienceReceivedHandler);
            ScienceFunding.Log("listening for science...");
        }
Esempio n. 6
0
 public void OnDestroy()
 {
     try
     {
         GameEvents.OnScienceRecieved.Remove(ScienceReceivedHandler);
         ScienceFunding.Log("OnDestroy, removing handler.");
     }
     catch (Exception)
     {
         // do nothing
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Locates the settings file (right next to the assembly),
        /// reads it and parses it to a ConfigNode.
        /// </summary>
        static ConfigNode GetConfig()
        {
            string assemblyPath = Path.GetDirectoryName(typeof(ScienceFunding).Assembly.Location);
            string filePath     = Path.Combine(assemblyPath, "PluginData/settings.cfg");

            ScienceFunding.Log("Loading settings file:" + filePath);

            ConfigNode result = ConfigNode.Load(filePath).GetNode("SCIENCE_FUNDING_SETTINGS");

            ScienceFunding.Log(result.ToString());

            return(result);
        }
Esempio n. 8
0
        /// <summary>
        /// Empties the queue and sends the notification to the player.
        /// </summary>
        private void SendReport()
        {
            ScienceFunding.Log("Posting the user notification");

            // I don't even know how this might happen, honestly.
            // But it's almost midnight and I get paranoid when I program late at night.
            if (this.queue.Count == 0)
            {
                return;
            }

            // Header
            StringBuilder builder = new StringBuilder();

            builder.AppendLine("Your recent research efforts have granted you the following rewards:");
            builder.AppendLine("");

            // Running total
            float totalFunds = 0;
            float totalRep   = 0;

            // Append a string for every report in the queue
            foreach (ScienceReport item in this.queue.ToList())
            {
                totalFunds += item.funds;
                totalRep   += item.rep;

                builder.AppendLine(item.ToString());
            }

            // Footer with totals
            builder.AppendLine("");
            builder.AppendLine("Total: " + totalFunds + " funds, " + totalRep + " reputation.");

            // Delete everything
            this.queue = new Queue <ScienceReport>();

            PostMessage(
                "New funds available!",
                builder.ToString(),
                MessageSystemButton.MessageButtonColor.BLUE,
                MessageSystemButton.ButtonIcons.MESSAGE
                );
        }
        void Start()
        {
            var game = HighLogic.CurrentGame;
            ProtoScenarioModule psm = game.scenarios.Find(s => s.moduleName == typeof(ScienceFunding).Name);

            if (psm == null)
            {
                ScienceFunding.Log("Adding the controller to the game.");
                psm = game.AddProtoScenarioModule(typeof(ScienceFunding), GameScenes.EDITOR,
                                                  GameScenes.FLIGHT,
                                                  GameScenes.SPACECENTER,
                                                  GameScenes.TRACKSTATION);
            }
            else // make sure the scenario is targeting all the scenes
            {
                ScienceFunding.Log("The runtime is already installed (OK).");

                SetTargetScene(psm, GameScenes.EDITOR);
                SetTargetScene(psm, GameScenes.FLIGHT);
                SetTargetScene(psm, GameScenes.SPACECENTER);
                SetTargetScene(psm, GameScenes.TRACKSTATION);
            }
        }