示例#1
0
        /// <summary>
        /// Load the configs when the game has started
        /// </summary>
        void Awake()
        {
            // Instance
            Instance = this;

            // Get the ConfigNodes
            ConfigNode[] nodes = GameDatabase.Instance.GetConfigs("LANGUAGEPATCHES").Select(c => c.config).ToArray();

            // Merge them into a single one
            if (nodes.Length > 1)
            {
                for (Int32 i = 1; i < nodes.Length; i++)
                {
                    foreach (ConfigNode n in nodes[i].nodes)
                    {
                        nodes[0].AddNode(n);
                    }
                    foreach (ConfigNode.Value v in nodes[i].values)
                    {
                        nodes[0].AddValue(v.name, v.value, v.comment);
                    }
                }
            }
            if (nodes.Length != 0)
            {
                config = nodes[0];
            }

            // Create a new Translation list from the node
            translations = new TranslationList(config);

            // Create fonts
            fonts = new Dictionary <String, Font>();

            // Create TMP_FontAssets
            TMP_fonts = new Dictionary <String, TMP_FontAsset>();

            // Create images
            images = new Dictionary <String, Texture2D>();

            // Prevent this class from getting destroyed
            DontDestroyOnLoad(this);

            // Internals
            patched = new CombinedDictionary();

            // Register Updates
            RegisterTask(Method.UPDATE, UpdateText);
            RegisterTask(Method.UPDATE, UpdateTextMesh);
            RegisterTask(Method.UPDATE, UpdateTextMeshPro);
            RegisterTask(Method.UPDATE, PopupDialogUpdate);
            RegisterTask(Method.UPDATE, SceneUpdate);
            RegisterTask(Method.UPDATE, UpdateImages);

            // Nullcheck
            if (config == null)
            {
                return;
            }

            // Override loading hints
            if (config.HasNode("HINTS"))
            {
                String[] hints = config.GetNode("HINTS").GetValues("hint");
                LoadingScreen.Instance.Screens.ForEach(s => s.tips = hints);
            }

            // Load the fonts
            foreach (ConfigNode node in config.GetNodes("FONT"))
            {
                // Vars
                String name = node.GetValue("name");
                String file = node.GetValue("file");

                String[] split = file.Split(':');
                if (split[0].ToLowerInvariant() == "os")
                {
                    String[] moreSplit = split[1].Split('@');
                    fonts.Add(name, Font.CreateDynamicFontFromOSFont(moreSplit[0], Int32.Parse(moreSplit[1])));
                }
                else
                {
                    AssetBundle bundle = AssetBundle.LoadFromMemory(File.ReadAllBytes(KSPUtil.ApplicationRootPath + "GameData/" + split[0]));
                    fonts.Add(name, bundle.LoadAsset <Font>(split[1]));
                    bundle.Unload(false);
                }
            }

            // Load the TMP_FontAssets
            foreach (ConfigNode node in config.GetNodes("TMPFONT"))
            {
                // Vars
                String name = node.GetValue("name");
                String file = node.GetValue("file");

                String[] split = file.Split(':');

                AssetBundle bundle = AssetBundle.LoadFromMemory(File.ReadAllBytes(KSPUtil.ApplicationRootPath + "GameData/" + split[0]));
                TMP_fonts.Add(name, bundle.LoadAsset <TMP_FontAsset>(split[1]));
                bundle.Unload(false);
            }

            // Load images
            GameEvents.OnGameDatabaseLoaded.Add(() =>
            {
                foreach (ConfigNode node in config.GetNodes("IMAGE"))
                {
                    String original   = node.GetValue("name");
                    String file       = node.GetValue("file");
                    Texture2D texture = GameDatabase.Instance.GetTexture(file, false);
                    images.Add(original, texture);
                }
            });

            // Load URLS
            if (config.HasNode("URLS"))
            {
                ConfigNode uNode = config.GetNode("URLS");
                urls = new List <String>
                {
                    uNode.HasValue("KSPsiteURL") ? "http://" + uNode.GetValue("KSPsiteURL") : null,
                    uNode.HasValue("SpaceportURL") ? "http://" + uNode.GetValue("SpaceportURL") : null,
                    uNode.HasValue("DefaultFlagURL") ? uNode.GetValue("DefaultFlagURL") : null
                };
            }

            // Load case sensivity
            Boolean.TryParse(config.GetValue("caseSensitive"), out caseSensitive);

            // Get debug mode
            Boolean.TryParse(config.GetValue("debug"), out debug);

            // Logger
            if (debug)
            {
                loggers = new Dictionary <GameScenes, Logger>();
                GameEvents.onGameSceneLoadRequested.Add((scene) =>
                {
                    if (!loggers.ContainsKey(scene))
                    {
                        loggers.Add(scene, new Logger(scene.ToString()));
                    }
                    loggers[scene].SetAsActive();
                    mainMenuPatched = false;
                });
                loggers.Add(HighLogic.LoadedScene, new Logger(HighLogic.LoadedScene.ToString()));
                loggers[HighLogic.LoadedScene].SetAsActive();
            }
        }
        /// <summary>
        /// Creates a new Translation component from a config node
        /// </summary>
        /// <param name="node">The config node where the </param>
        public Translation(ConfigNode node)
        {
            // Check for original text
            if (!node.HasValue("text"))
            {
                throw new Exception("The config node is missing the text value!");
            }

            // Check for translation
            if (!node.HasValue("translation"))
            {
                throw new Exception("The config node is missing the translation value!");
            }

            // Assign the new texts
            text        = node.GetValue("text");
            translation = node.GetValue("translation");
            String prepared = TranslationList.Prepare(text);

            if (!LanguagePatches.Instance.caseSensitive)
            {
                expression = new Regex("^(?i)" + prepared + "$", RegexOptions.Compiled);
            }
            else
            {
                expression = new Regex("^" + prepared + "$", RegexOptions.Compiled | RegexOptions.None);
            }

            // Loads scene value
            if (node.HasValue("scene"))
            {
                String[] scenes = node.GetValue("scene").Split('|');
                scene = scenes.Select(s => (GameScenes)Enum.Parse(typeof(GameScenes), s.Trim())).ToList();
            }
            else
            {
                scene = null;
            }

            // Loads size value
            if (node.HasValue("size"))
            {
                size = Int32.Parse(node.GetValue("size"));
            }
            else
            {
                size = -1;
            }

            // Context
            if (node.HasValue("context"))
            {
                context = node.GetValue("context");
            }
            else
            {
                context = typeof(Translation).Assembly.GetName().Name;
            }

            // Replace variable placeholders
            translation = Regex.Replace(translation, @"@(\d*)", "{$1}");
        }