ToFile() public method

Write personality to a file. If the file name is not supplied the the default path of Constants.Data_DIR\personalities\eddi.json is used
public ToFile ( string filename = null ) : void
filename string
return void
Esempio n. 1
0
        /// <summary>
        /// Create a copy of this file, altering the datapath appropriately
        /// </summary>
        public Personality Copy(string name, string description)
        {
            // Tidy the name up to avoid bad characters
            string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
            Regex  r           = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));

            name = r.Replace(name, "");

            // Save a copy of this personality
            string iname    = name.ToLowerInvariant();
            string copyPath = Constants.DATA_DIR + @"\personalities\" + iname + ".json";

            // Ensure it doesn't exist
            if (!File.Exists(copyPath))
            {
                ToFile(copyPath);
            }
            // Load the personality back in
            Personality newPersonality = FromFile(copyPath);

            // Change its name and description and save it back out again
            newPersonality.Name        = name;
            newPersonality.Description = description;
            newPersonality.ToFile();
            // And finally return it
            return(newPersonality);
        }
        /// <summary>
        /// Create a copy of this file, altering the datapath appropriately
        /// </summary>
        public Personality Copy(string name, string description)
        {
            // Save a copy of this personality
            string iname    = name.ToLowerInvariant();
            string copyPath = Constants.DATA_DIR + @"\personalities\" + iname + ".json";

            ToFile(copyPath);
            // Load the personality back in
            Personality newPersonality = FromFile(copyPath);

            // Change its name and description and save it back out again
            newPersonality.Name        = name;
            newPersonality.Description = description;
            newPersonality.ToFile();
            // And finally return it
            return(newPersonality);
        }
Esempio n. 3
0
        /// <summary>
        /// Fix up the personality information to ensure that it contains the correct event
        /// </summary>
        private static void fixPersonalityInfo(Personality personality)
        {
            // Default personality for reference scripts
            Personality defaultPersonality = personality.IsDefault ? null : Default();

            Dictionary <string, Script> fixedScripts = new Dictionary <string, Script>();
            // Ensure that every required event is present
            List <string> missingScripts = new List <string>();

            foreach (KeyValuePair <string, string> defaultEvent in Events.DESCRIPTIONS)
            {
                personality.Scripts.TryGetValue(defaultEvent.Key, out Script script);
                Script defaultScript = null;
                defaultPersonality?.Scripts?.TryGetValue(defaultEvent.Key, out defaultScript);
                script = UpgradeScript(script, defaultScript);
                if (script == null)
                {
                    missingScripts.Add(defaultEvent.Key);
                }
                else
                {
                    fixedScripts.Add(defaultEvent.Key, script);
                }
            }
            foreach (KeyValuePair <string, Script> kv in personality.Scripts)
            {
                if (!fixedScripts.ContainsKey(kv.Key))
                {
                    Script defaultScript = null;
                    defaultPersonality?.Scripts?.TryGetValue(kv.Key, out defaultScript);
                    Script script = UpgradeScript(kv.Value, defaultScript);
                    fixedScripts.Add(kv.Key, script);
                }
            }
            if (!personality.IsDefault)
            {
                // Remove deprecated scripts from the list
                List <string> scriptHolder = new List <string>();
                foreach (KeyValuePair <string, Script> kv in fixedScripts)
                {
                    if (kv.Key == "Jumping") // Replaced by "FSD engaged" script
                    {
                        scriptHolder.Add(kv.Key);
                    }
                    else if (kv.Value.Name == "Crew member role change") // This name is mismatched to the key (should be "changed"),
                                                                         // so EDDI couldn't match the script name to the .json key correctly. The default script has been corrected.
                    {
                        scriptHolder.Add(kv.Key);
                    }
                    else if (kv.Value.Name == "Ship low fuel") // Accidental duplicate. The real event is called 'Low fuel'
                    {
                        scriptHolder.Add(kv.Key);
                    }
                    else if (kv.Value.Name == "Modification applied") // Event deprecated by FDev, no longer written.
                    {
                        scriptHolder.Add(kv.Key);
                    }
                    else if (kv.Value.Name == "List launchbays") // Replaced by "Launchbay report" script
                    {
                        scriptHolder.Add(kv.Key);
                    }
                }
                foreach (string script in scriptHolder)
                {
                    fixedScripts.Remove(script);
                }
                // Also add any secondary scripts in the default personality that aren't present in the list
                if (defaultPersonality?.Scripts != null)
                {
                    foreach (KeyValuePair <string, Script> kv in defaultPersonality?.Scripts)
                    {
                        if (!fixedScripts.ContainsKey(kv.Key))
                        {
                            fixedScripts.Add(kv.Key, kv.Value);
                        }
                    }
                }
            }

            // Report missing scripts, except those we have specifically named
            string[] ignoredEventKeys =
            {
                // Deprecated events
                "Belt scanned",
                "Jumping",
                "Modification applied",
                "Status",

                // Shares updates with monitors / responders but are not intended to be user facing
                "Cargo",
                "Market",
                "Outfitting",
                "Shipyard",
                "Squadron startup",
                "Stored ships",
                "Stored modules",
                "Unhandled event"
            };

            Array.Sort(ignoredEventKeys);
            bool isIgnoredEvent(string t) => Array.BinarySearch(ignoredEventKeys, t) >= 0;

            missingScripts.RemoveAll(isIgnoredEvent);
            if (missingScripts.Count > 0)
            {
                Logging.Info("Failed to find scripts" + string.Join(";", missingScripts));
            }

            // Re-order the scripts by name
            fixedScripts = fixedScripts.OrderBy(s => s.Key).ToDictionary(s => s.Key, s => s.Value);

            personality.Scripts = fixedScripts;
            personality.ToFile();
        }