Esempio n. 1
0
        public static XDocument LoadXml(string filePath)
        {
            XDocument doc = null;

            ToolBox.IsProperFilenameCase(filePath);

            if (File.Exists(filePath))
            {
                try
                {
                    doc = XDocument.Load(filePath, LoadOptions.SetBaseUri);
                }
                catch
                {
                    return(null);
                }

                if (doc.Root == null)
                {
                    return(null);
                }
            }

            return(doc);
        }
Esempio n. 2
0
        public static Texture2D LoadTexture(string file, bool preMultiplyAlpha = true)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                Texture2D t = null;
                CrossThread.RequestExecutionOnMainThread(() =>
                {
                    t = new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, 1, 1);
                });
                return(t);
            }
            file = Path.GetFullPath(file);
            foreach (Sprite s in list)
            {
                if (s.FullPath == file && s.texture != null)
                {
                    return(s.texture);
                }
            }

            if (File.Exists(file))
            {
                ToolBox.IsProperFilenameCase(file);
                return(TextureLoader.FromFile(file, preMultiplyAlpha));
            }
            else
            {
                DebugConsole.ThrowError("Sprite \"" + file + "\" not found!");
            }

            return(null);
        }
Esempio n. 3
0
        public static XDocument LoadXml(string filePath)
        {
            XDocument doc = null;

            ToolBox.IsProperFilenameCase(filePath);

            if (File.Exists(filePath))
            {
                try
                {
                    using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                    using XmlReader reader  = CreateReader(stream);
                    doc = XDocument.Load(reader);
                }
                catch
                {
                    return(null);
                }

                if (doc.Root == null)
                {
                    return(null);
                }
            }

            return(doc);
        }
Esempio n. 4
0
        public static Texture2D LoadTexture(string file, bool preMultiplyAlpha = true)
        {
            if (string.IsNullOrWhiteSpace(file))
            {
                return(new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, 1, 1));
            }
            file = Path.GetFullPath(file);
            foreach (Sprite s in list)
            {
                if (string.IsNullOrEmpty(s.FilePath))
                {
                    continue;
                }
                if (Path.GetFullPath(s.file) == file)
                {
                    return(s.texture);
                }
            }

            if (File.Exists(file))
            {
                ToolBox.IsProperFilenameCase(file);
                return(TextureLoader.FromFile(file, preMultiplyAlpha));
            }
            else
            {
                DebugConsole.ThrowError("Sprite \"" + file + "\" not found!");
            }

            return(null);
        }
Esempio n. 5
0
        public GUIStyle(string file, GraphicsDevice graphicsDevice)
        {
            componentStyles = new Dictionary <string, GUIComponentStyle>();

            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(file);
                doc = XDocument.Load(file, LoadOptions.SetBaseUri);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e);
                return;
            }

            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "font":
                    Font = new ScalableFont(subElement, graphicsDevice);
                    break;

                case "smallfont":
                    SmallFont = new ScalableFont(subElement, graphicsDevice);
                    break;

                case "largefont":
                    LargeFont = new ScalableFont(subElement, graphicsDevice);
                    break;

                case "cursor":
                    CursorSprite = new Sprite(subElement);
                    break;

                case "uiglow":
                    UIGlow = new UISprite(subElement);
                    break;

                case "focusindicator":
                    FocusIndicator = new SpriteSheet(subElement);
                    break;

                default:
                    GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
                    componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle);
                    break;
                }
            }
        }
Esempio n. 6
0
        public static XDocument TryLoadXml(string filePath)
        {
            XDocument doc;
            try
            {
                ToolBox.IsProperFilenameCase(filePath);
                doc = XDocument.Load(filePath);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Couldn't load xml document \"" + filePath + "\"!", e);
                return null;
            }

            if (doc.Root == null) return null;

            return doc;
        }
Esempio n. 7
0
        public static XDocument TryLoadXml(string filePath)
        {
            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(filePath);
                doc = XDocument.Load(filePath, LoadOptions.SetBaseUri);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Couldn't load xml document \"" + filePath + "\"!", e);
                return(null);
            }
            if (doc?.Root == null)
            {
                DebugConsole.ThrowError("File \"" + filePath + "\" could not be loaded: Document or the root element is invalid!");
                return(null);
            }
            return(doc);
        }
Esempio n. 8
0
        public static Texture2D LoadTexture(string file, out Sprite reusedSprite, bool compress = true)
        {
            reusedSprite = null;
            if (string.IsNullOrWhiteSpace(file))
            {
                Texture2D t = null;
                CrossThread.RequestExecutionOnMainThread(() =>
                {
                    t = new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, 1, 1);
                });
                return(t);
            }
            string fullPath = Path.GetFullPath(file);

            foreach (Sprite s in LoadedSprites)
            {
                if (s.FullPath == fullPath && s.texture != null && !s.texture.IsDisposed)
                {
                    reusedSprite = s;
                    return(s.texture);
                }
            }

            if (File.Exists(file))
            {
                if (!ToolBox.IsProperFilenameCase(file))
                {
#if DEBUG
                    DebugConsole.ThrowError("Texture file \"" + file + "\" has incorrect case!");
#endif
                }
                return(TextureLoader.FromFile(file, compress));
            }
            else
            {
                DebugConsole.ThrowError($"Sprite \"{file}\" not found! {Environment.StackTrace.CleanupStackTrace()}");
            }

            return(null);
        }
Esempio n. 9
0
        public static Texture2D LoadTexture(string file)
        {
            foreach (Sprite s in list)
            {
                if (s.file == file)
                {
                    return(s.texture);
                }
            }

            if (File.Exists(file))
            {
                ToolBox.IsProperFilenameCase(file);
                return(TextureLoader.FromFile(file));
            }
            else
            {
                DebugConsole.ThrowError("Sprite \"" + file + "\" not found!");
            }

            return(null);
        }
Esempio n. 10
0
        public GUIStyle(string file)
        {
            componentStyles = new Dictionary <string, GUIComponentStyle>();

            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(file);
                doc = XDocument.Load(file);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e);
                return;
            }

            foreach (XElement subElement in doc.Root.Elements())
            {
                GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
                componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle);
            }
        }
Esempio n. 11
0
        public static XDocument TryLoadXml(string filePath)
        {
            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(filePath);
                using FileStream stream = File.Open(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
                using XmlReader reader  = CreateReader(stream);
                doc = XDocument.Load(reader);
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Couldn't load xml document \"" + filePath + "\"!", e);
                return(null);
            }
            if (doc?.Root == null)
            {
                DebugConsole.ThrowError("File \"" + filePath + "\" could not be loaded: Document or the root element is invalid!");
                return(null);
            }
            return(doc);
        }
Esempio n. 12
0
        private void LoadContentPackages(IEnumerable <string> contentPackagePaths)
        {
            var missingPackagePaths  = new List <string>();
            var incompatiblePackages = new List <ContentPackage>();

            SelectedContentPackages.Clear();
            foreach (string path in contentPackagePaths)
            {
                var matchingContentPackage = ContentPackage.List.Find(cp => System.IO.Path.GetFullPath(cp.Path) == path);

                if (matchingContentPackage == null)
                {
                    missingPackagePaths.Add(path);
                }
                else if (!matchingContentPackage.IsCompatible())
                {
                    incompatiblePackages.Add(matchingContentPackage);
                }
                else
                {
                    SelectedContentPackages.Add(matchingContentPackage);
                }
            }

            TextManager.LoadTextPacks(SelectedContentPackages);

            foreach (ContentPackage contentPackage in SelectedContentPackages)
            {
                bool packageOk = contentPackage.VerifyFiles(out List <string> errorMessages);
                if (!packageOk)
                {
                    DebugConsole.ThrowError("Error in content package \"" + contentPackage.Name + "\":\n" + string.Join("\n", errorMessages));
                    continue;
                }
                foreach (ContentFile file in contentPackage.Files)
                {
                    ToolBox.IsProperFilenameCase(file.Path);
                }
            }

            EnsureCoreContentPackageSelected();

            //save to get rid of the invalid selected packages in the config file
            if (missingPackagePaths.Count > 0 || incompatiblePackages.Count > 0)
            {
                SaveNewPlayerConfig();
            }

            //display error messages after all content packages have been loaded
            //to make sure the package that contains text files has been loaded before we attempt to use TextManager
            foreach (string missingPackagePath in missingPackagePaths)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariable("ContentPackageNotFound", "[packagepath]", missingPackagePath));
            }
            foreach (ContentPackage incompatiblePackage in incompatiblePackages)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariables(incompatiblePackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage",
                                                                     new string[3] {
                    "[packagename]", "[packageversion]", "[gameversion]"
                }, new string[3] {
                    incompatiblePackage.Name, incompatiblePackage.GameVersion.ToString(), GameMain.Version.ToString()
                }));
            }
        }
Esempio n. 13
0
        public static XDocument OpenFile(string file, out Exception exception)
        {
            XDocument doc       = null;
            string    extension = "";

            exception = null;

            try
            {
                extension = System.IO.Path.GetExtension(file);
            }
            catch
            {
                //no file extension specified: try using the default one
                file += ".sub";
            }

            if (string.IsNullOrWhiteSpace(extension))
            {
                extension = ".sub";
                file     += ".sub";
            }

            if (extension == ".sub")
            {
                System.IO.Stream stream = null;
                try
                {
                    stream = SaveUtil.DecompressFiletoStream(file);
                }
                catch (System.IO.FileNotFoundException e)
                {
                    exception = e;
                    DebugConsole.ThrowError("Loading submarine \"" + file + "\" failed! (File not found) " + Environment.StackTrace.CleanupStackTrace(), e);
                    return(null);
                }
                catch (Exception e)
                {
                    exception = e;
                    DebugConsole.ThrowError("Loading submarine \"" + file + "\" failed!", e);
                    return(null);
                }

                try
                {
                    stream.Position = 0;
                    doc             = XDocument.Load(stream); //ToolBox.TryLoadXml(file);
                    stream.Close();
                    stream.Dispose();
                }

                catch (Exception e)
                {
                    exception = e;
                    DebugConsole.ThrowError("Loading submarine \"" + file + "\" failed! (" + e.Message + ")");
                    return(null);
                }
            }
            else if (extension == ".xml")
            {
                try
                {
                    ToolBox.IsProperFilenameCase(file);
                    doc = XDocument.Load(file, LoadOptions.SetBaseUri);
                }

                catch (Exception e)
                {
                    exception = e;
                    DebugConsole.ThrowError("Loading submarine \"" + file + "\" failed! (" + e.Message + ")");
                    return(null);
                }
            }
            else
            {
                DebugConsole.ThrowError("Couldn't load submarine \"" + file + "! (Unrecognized file extension)");
                return(null);
            }

            return(doc);
        }
Esempio n. 14
0
        private void LoadContentPackages(IEnumerable <string> contentPackagePaths)
        {
            var missingPackagePaths  = new List <string>();
            var incompatiblePackages = new List <ContentPackage>();
            var invalidPackages      = new List <ContentPackage>();

            SelectedContentPackages.Clear();
            foreach (string path in contentPackagePaths)
            {
                var matchingContentPackage = ContentPackage.List.Find(cp => System.IO.Path.GetFullPath(cp.Path) == path);

                if (matchingContentPackage == null)
                {
                    missingPackagePaths.Add(path);
                }
                else if (!matchingContentPackage.IsCompatible())
                {
                    DebugConsole.NewMessage(
                        $"Content package \"{matchingContentPackage.Name}\" is not compatible with this version of Barotrauma (game version: {GameMain.Version}, content package version: {matchingContentPackage.GameVersion})",
                        Color.Red);
                    incompatiblePackages.Add(matchingContentPackage);
                }
                else if (!matchingContentPackage.CheckValidity(out List <string> errorMessages))
                {
                    DebugConsole.NewMessage(
                        $"Content package \"{matchingContentPackage.Name}\" is invalid: " + string.Join(", ", errorMessages),
                        Color.Red);
                    invalidPackages.Add(matchingContentPackage);
                    //never consider the vanilla content package invalid
                    //(otherwise a player might brick the game by, for example, deleting vanilla content files)
                    if (matchingContentPackage == GameMain.VanillaContent)
                    {
                        SelectedContentPackages.Add(matchingContentPackage);
                    }
                }
                else
                {
                    SelectedContentPackages.Add(matchingContentPackage);
                }
            }

            ContentPackage.SortContentPackages();
            TextManager.LoadTextPacks(SelectedContentPackages);

            foreach (ContentPackage contentPackage in SelectedContentPackages)
            {
                foreach (ContentFile file in contentPackage.Files)
                {
                    ToolBox.IsProperFilenameCase(file.Path);
                }
            }

            EnsureCoreContentPackageSelected();

            //save to get rid of the invalid selected packages in the config file
            if (missingPackagePaths.Count > 0 || incompatiblePackages.Count > 0 || invalidPackages.Count > 0)
            {
                SaveNewPlayerConfig();
            }

            //display error messages after all content packages have been loaded
            //to make sure the package that contains text files has been loaded before we attempt to use TextManager
            foreach (string missingPackagePath in missingPackagePaths)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariable("ContentPackageNotFound", "[packagepath]", missingPackagePath));
            }
            foreach (ContentPackage invalidPackage in invalidPackages)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariable("InvalidContentPackage", "[packagename]", invalidPackage.Name), createMessageBox: true);
            }
            foreach (ContentPackage incompatiblePackage in incompatiblePackages)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariables(incompatiblePackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage",
                                                                     new string[3] {
                    "[packagename]", "[packageversion]", "[gameversion]"
                }, new string[3] {
                    incompatiblePackage.Name, incompatiblePackage.GameVersion.ToString(), GameMain.Version.ToString()
                }),
                                        createMessageBox: true);
            }
        }
Esempio n. 15
0
        public GUIStyle(string file, GraphicsDevice graphicsDevice)
        {
            this.graphicsDevice = graphicsDevice;
            componentStyles     = new Dictionary <string, GUIComponentStyle>();

            XDocument doc;

            try
            {
                ToolBox.IsProperFilenameCase(file);
                doc = XDocument.Load(file, LoadOptions.SetBaseUri);
                if (doc == null)
                {
                    throw new Exception("doc is null");
                }
                if (doc.Root == null)
                {
                    throw new Exception("doc.Root is null");
                }
                if (doc.Root.Elements() == null)
                {
                    throw new Exception("doc.Root.Elements() is null");
                }
            }
            catch (Exception e)
            {
                DebugConsole.ThrowError("Loading style \"" + file + "\" failed", e);
                return;
            }
            configElement = doc.Root;
            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "cursor":
                    CursorSprite = new Sprite(subElement);
                    break;

                case "uiglow":
                    UIGlow = new UISprite(subElement);
                    break;

                case "focusindicator":
                    FocusIndicator = new SpriteSheet(subElement);
                    break;

                case "font":
                    Font = LoadFont(subElement, graphicsDevice);
                    break;

                case "unscaledsmallfont":
                    UnscaledSmallFont = LoadFont(subElement, graphicsDevice);
                    break;

                case "smallfont":
                    SmallFont = LoadFont(subElement, graphicsDevice);
                    break;

                case "largefont":
                    LargeFont = LoadFont(subElement, graphicsDevice);
                    break;

                case "objectivetitle":
                    ObjectiveTitleFont = LoadFont(subElement, graphicsDevice);
                    break;

                case "objectivename":
                    ObjectiveNameFont = LoadFont(subElement, graphicsDevice);
                    break;

                case "videotitle":
                    VideoTitleFont = LoadFont(subElement, graphicsDevice);
                    break;

                default:
                    GUIComponentStyle componentStyle = new GUIComponentStyle(subElement);
                    componentStyles.Add(subElement.Name.ToString().ToLowerInvariant(), componentStyle);
                    break;
                }
            }

            GameMain.Instance.OnResolutionChanged += () => { RescaleFonts(); };
        }
Esempio n. 16
0
        private void LoadDefaultConfig(bool setLanguage = true)
        {
            XDocument doc = XMLExtensions.TryLoadXml(savePath);

            if (setLanguage || string.IsNullOrEmpty(Language))
            {
                Language = doc.Root.GetAttributeString("language", "English");
            }

            MasterServerUrl = doc.Root.GetAttributeString("masterserverurl", "");

            AutoCheckUpdates = doc.Root.GetAttributeBool("autocheckupdates", true);
            WasGameUpdated   = doc.Root.GetAttributeBool("wasgameupdated", false);

            VerboseLogging       = doc.Root.GetAttributeBool("verboselogging", false);
            SaveDebugConsoleLogs = doc.Root.GetAttributeBool("savedebugconsolelogs", false);

            QuickStartSubmarineName = doc.Root.GetAttributeString("quickstartsub", "");

            if (doc == null)
            {
                GraphicsWidth  = 1024;
                GraphicsHeight = 678;

                MasterServerUrl = "";

                SelectedContentPackages.Add(ContentPackage.List.Any() ? ContentPackage.List[0] : new ContentPackage(""));

                jobPreferences = new List <string>();
                foreach (JobPrefab job in JobPrefab.List)
                {
                    jobPreferences.Add(job.Identifier);
                }
                return;
            }

            XElement graphicsMode = doc.Root.Element("graphicsmode");

            GraphicsWidth  = 0;
            GraphicsHeight = 0;
            VSyncEnabled   = graphicsMode.GetAttributeBool("vsync", true);

            XElement graphicsSettings = doc.Root.Element("graphicssettings");

            ParticleLimit              = graphicsSettings.GetAttributeInt("particlelimit", 1500);
            LightMapScale              = MathHelper.Clamp(graphicsSettings.GetAttributeFloat("lightmapscale", 0.5f), 0.1f, 1.0f);
            SpecularityEnabled         = graphicsSettings.GetAttributeBool("specularity", true);
            ChromaticAberrationEnabled = graphicsSettings.GetAttributeBool("chromaticaberration", true);
            HUDScale       = graphicsSettings.GetAttributeFloat("hudscale", 1.0f);
            InventoryScale = graphicsSettings.GetAttributeFloat("inventoryscale", 1.0f);
            var losModeStr = graphicsSettings.GetAttributeString("losmode", "Transparent");

            if (!Enum.TryParse(losModeStr, out losMode))
            {
                losMode = LosMode.Transparent;
            }

#if CLIENT
            if (GraphicsWidth == 0 || GraphicsHeight == 0)
            {
                GraphicsWidth  = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
                GraphicsHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            }
#endif

            var windowModeStr = graphicsMode.GetAttributeString("displaymode", "Fullscreen");
            if (!Enum.TryParse(windowModeStr, out WindowMode wm))
            {
                wm = WindowMode.Fullscreen;
            }
            WindowMode = wm;

            useSteamMatchmaking        = doc.Root.GetAttributeBool("usesteammatchmaking", true);
            requireSteamAuthentication = doc.Root.GetAttributeBool("requiresteamauthentication", true);
            AutoUpdateWorkshopItems    = doc.Root.GetAttributeBool("autoupdateworkshopitems", true);

#if DEBUG
            EnableSplashScreen = false;
#else
            EnableSplashScreen = doc.Root.GetAttributeBool("enablesplashscreen", true);
#endif

            AimAssistAmount = doc.Root.GetAttributeFloat("aimassistamount", 0.5f);

            SetDefaultBindings(doc, legacy: false);

            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "keymapping":
                    LoadKeyBinds(subElement);
                    break;

                case "gameplay":
                    jobPreferences = new List <string>();
                    foreach (XElement ele in subElement.Element("jobpreferences").Elements("job"))
                    {
                        string jobIdentifier = ele.GetAttributeString("identifier", "");
                        if (string.IsNullOrEmpty(jobIdentifier))
                        {
                            continue;
                        }
                        jobPreferences.Add(jobIdentifier);
                    }
                    break;

                case "player":
                    defaultPlayerName  = subElement.GetAttributeString("name", "");
                    CharacterHeadIndex = subElement.GetAttributeInt("headindex", CharacterHeadIndex);
                    if (Enum.TryParse(subElement.GetAttributeString("gender", "none"), true, out Gender g))
                    {
                        CharacterGender = g;
                    }
                    if (Enum.TryParse(subElement.GetAttributeString("race", "white"), true, out Race r))
                    {
                        CharacterRace = r;
                    }
                    else
                    {
                        CharacterRace = Race.White;
                    }
                    CharacterHairIndex           = subElement.GetAttributeInt("hairindex", -1);
                    CharacterBeardIndex          = subElement.GetAttributeInt("beardindex", -1);
                    CharacterMoustacheIndex      = subElement.GetAttributeInt("moustacheindex", -1);
                    CharacterFaceAttachmentIndex = subElement.GetAttributeInt("faceattachmentindex", -1);
                    break;
                }
            }

            List <string>         missingPackagePaths  = new List <string>();
            List <ContentPackage> incompatiblePackages = new List <ContentPackage>();
            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "contentpackage":
                    string path = System.IO.Path.GetFullPath(subElement.GetAttributeString("path", ""));
                    var    matchingContentPackage = ContentPackage.List.Find(cp => System.IO.Path.GetFullPath(cp.Path) == path);
                    if (matchingContentPackage == null)
                    {
                        missingPackagePaths.Add(path);
                    }
                    else if (!matchingContentPackage.IsCompatible())
                    {
                        incompatiblePackages.Add(matchingContentPackage);
                    }
                    else
                    {
                        SelectedContentPackages.Add(matchingContentPackage);
                    }
                    break;
                }
            }

            TextManager.LoadTextPacks(SelectedContentPackages);

            //display error messages after all content packages have been loaded
            //to make sure the package that contains text files has been loaded before we attempt to use TextManager
            foreach (string missingPackagePath in missingPackagePaths)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariable("ContentPackageNotFound", "[packagepath]", missingPackagePath));
            }
            foreach (ContentPackage incompatiblePackage in incompatiblePackages)
            {
                DebugConsole.ThrowError(TextManager.GetWithVariables(incompatiblePackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage",
                                                                     new string[3] {
                    "[packagename]", "[packageversion]", "[gameversion]"
                }, new string[3] {
                    incompatiblePackage.Name, incompatiblePackage.GameVersion.ToString(), GameMain.Version.ToString()
                }));
            }
            foreach (ContentPackage contentPackage in SelectedContentPackages)
            {
                bool packageOk = contentPackage.VerifyFiles(out List <string> errorMessages);
                if (!packageOk)
                {
                    DebugConsole.ThrowError("Error in content package \"" + contentPackage.Name + "\":\n" + string.Join("\n", errorMessages));
                    continue;
                }
                foreach (ContentFile file in contentPackage.Files)
                {
                    ToolBox.IsProperFilenameCase(file.Path);
                }
            }
            if (!SelectedContentPackages.Any())
            {
                var availablePackage = ContentPackage.List.FirstOrDefault(cp => cp.IsCompatible() && cp.CorePackage);
                if (availablePackage != null)
                {
                    SelectedContentPackages.Add(availablePackage);
                }
            }

            //save to get rid of the invalid selected packages in the config file
            if (missingPackagePaths.Count > 0 || incompatiblePackages.Count > 0)
            {
                SaveNewPlayerConfig();
            }
        }
Esempio n. 17
0
        private void LoadContentPackages(IEnumerable <string> contentPackagePaths)
        {
            var missingPackagePaths  = new List <string>();
            var incompatiblePackages = new List <ContentPackage>();

            SelectedContentPackages.Clear();
            foreach (string path in contentPackagePaths)
            {
                var matchingContentPackage = ContentPackage.List.Find(cp => System.IO.Path.GetFullPath(cp.Path) == path);

                if (matchingContentPackage == null)
                {
                    missingPackagePaths.Add(path);
                }
                else if (!matchingContentPackage.IsCompatible())
                {
                    incompatiblePackages.Add(matchingContentPackage);
                }
                else
                {
                    SelectedContentPackages.Add(matchingContentPackage);
                }
            }

            TextManager.LoadTextPacks(SelectedContentPackages);

            foreach (ContentPackage contentPackage in SelectedContentPackages)
            {
                foreach (ContentFile file in contentPackage.Files)
                {
                    if (!System.IO.File.Exists(file.Path))
                    {
                        DebugConsole.ThrowError("Error in content package \"" + contentPackage.Name + "\" - file \"" + file.Path + "\" not found.");
                        continue;
                    }
                    ToolBox.IsProperFilenameCase(file.Path);
                }
            }
            if (!SelectedContentPackages.Any())
            {
                var availablePackage = ContentPackage.List.FirstOrDefault(cp => cp.IsCompatible() && cp.CorePackage);
                if (availablePackage != null)
                {
                    SelectedContentPackages.Add(availablePackage);
                }
            }

            //save to get rid of the invalid selected packages in the config file
            if (missingPackagePaths.Count > 0 || incompatiblePackages.Count > 0)
            {
                SaveNewPlayerConfig();
            }

            //display error messages after all content packages have been loaded
            //to make sure the package that contains text files has been loaded before we attempt to use TextManager
            foreach (string missingPackagePath in missingPackagePaths)
            {
                DebugConsole.ThrowError(TextManager.Get("ContentPackageNotFound").Replace("[packagepath]", missingPackagePath));
            }
            foreach (ContentPackage incompatiblePackage in incompatiblePackages)
            {
                DebugConsole.ThrowError(TextManager.Get(incompatiblePackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage")
                                        .Replace("[packagename]", incompatiblePackage.Name)
                                        .Replace("[packageversion]", incompatiblePackage.GameVersion.ToString())
                                        .Replace("[gameversion]", GameMain.Version.ToString()));
            }
        }
Esempio n. 18
0
        public void LoadDefaultConfig()
        {
            XDocument doc = XMLExtensions.TryLoadXml(savePath);

            Language = doc.Root.GetAttributeString("language", "English");

            MasterServerUrl = doc.Root.GetAttributeString("masterserverurl", "");

            AutoCheckUpdates = doc.Root.GetAttributeBool("autocheckupdates", true);
            WasGameUpdated   = doc.Root.GetAttributeBool("wasgameupdated", false);

            VerboseLogging       = doc.Root.GetAttributeBool("verboselogging", false);
            SaveDebugConsoleLogs = doc.Root.GetAttributeBool("savedebugconsolelogs", false);

#if DEBUG
            UseSteam = doc.Root.GetAttributeBool("usesteam", true);
#endif
            QuickStartSubmarineName = doc.Root.GetAttributeString("quickstartsub", "");

            if (doc == null)
            {
                GraphicsWidth  = 1024;
                GraphicsHeight = 678;

                MasterServerUrl = "";

                SelectedContentPackages.Add(ContentPackage.List.Any() ? ContentPackage.List[0] : new ContentPackage(""));

                jobPreferences = new List <string>();
                foreach (JobPrefab job in JobPrefab.List)
                {
                    jobPreferences.Add(job.Identifier);
                }
                return;
            }

            XElement graphicsMode = doc.Root.Element("graphicsmode");
            GraphicsWidth  = 0;
            GraphicsHeight = 0;
            VSyncEnabled   = graphicsMode.GetAttributeBool("vsync", true);

            XElement graphicsSettings = doc.Root.Element("graphicssettings");
            ParticleLimit              = graphicsSettings.GetAttributeInt("particlelimit", 1500);
            LightMapScale              = MathHelper.Clamp(graphicsSettings.GetAttributeFloat("lightmapscale", 0.5f), 0.1f, 1.0f);
            SpecularityEnabled         = graphicsSettings.GetAttributeBool("specularity", true);
            ChromaticAberrationEnabled = graphicsSettings.GetAttributeBool("chromaticaberration", true);
            HUDScale       = graphicsSettings.GetAttributeFloat("hudscale", 1.0f);
            InventoryScale = graphicsSettings.GetAttributeFloat("inventoryscale", 1.0f);
            var losModeStr = graphicsSettings.GetAttributeString("losmode", "Transparent");
            if (!Enum.TryParse(losModeStr, out losMode))
            {
                losMode = LosMode.Transparent;
            }

#if CLIENT
            if (GraphicsWidth == 0 || GraphicsHeight == 0)
            {
                GraphicsWidth  = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
                GraphicsHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            }
#endif

            var windowModeStr = graphicsMode.GetAttributeString("displaymode", "Fullscreen");
            if (!Enum.TryParse(windowModeStr, out WindowMode wm))
            {
                wm = WindowMode.Fullscreen;
            }
            WindowMode = wm;

            useSteamMatchmaking        = doc.Root.GetAttributeBool("usesteammatchmaking", true);
            requireSteamAuthentication = doc.Root.GetAttributeBool("requiresteamauthentication", true);
            AutoUpdateWorkshopItems    = doc.Root.GetAttributeBool("autoupdateworkshopitems", true);

#if DEBUG
            EnableSplashScreen = false;
#else
            EnableSplashScreen = doc.Root.GetAttributeBool("enablesplashscreen", true);
#endif

            AimAssistAmount = doc.Root.GetAttributeFloat("aimassistamount", 0.5f);

            keyMapping = new KeyOrMouse[Enum.GetNames(typeof(InputType)).Length];
            keyMapping[(int)InputType.Up]    = new KeyOrMouse(Keys.W);
            keyMapping[(int)InputType.Down]  = new KeyOrMouse(Keys.S);
            keyMapping[(int)InputType.Left]  = new KeyOrMouse(Keys.A);
            keyMapping[(int)InputType.Right] = new KeyOrMouse(Keys.D);
            keyMapping[(int)InputType.Run]   = new KeyOrMouse(Keys.LeftShift);

            keyMapping[(int)InputType.InfoTab]    = new KeyOrMouse(Keys.Tab);
            keyMapping[(int)InputType.Chat]       = new KeyOrMouse(Keys.T);
            keyMapping[(int)InputType.RadioChat]  = new KeyOrMouse(Keys.Y);
            keyMapping[(int)InputType.CrewOrders] = new KeyOrMouse(Keys.C);

            keyMapping[(int)InputType.Select] = new KeyOrMouse(Keys.E);

            keyMapping[(int)InputType.SelectNextCharacter]     = new KeyOrMouse(Keys.Tab);
            keyMapping[(int)InputType.SelectPreviousCharacter] = new KeyOrMouse(Keys.Q);

            keyMapping[(int)InputType.Voice] = new KeyOrMouse(Keys.V);

            keyMapping[(int)InputType.Use] = new KeyOrMouse(0);
            keyMapping[(int)InputType.Aim] = new KeyOrMouse(1);

            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "keymapping":
                    foreach (XAttribute attribute in subElement.Attributes())
                    {
                        if (Enum.TryParse(attribute.Name.ToString(), true, out InputType inputType))
                        {
                            if (int.TryParse(attribute.Value.ToString(), out int mouseButton))
                            {
                                keyMapping[(int)inputType] = new KeyOrMouse(mouseButton);
                            }
                            else
                            {
                                if (Enum.TryParse(attribute.Value.ToString(), true, out Keys key))
                                {
                                    keyMapping[(int)inputType] = new KeyOrMouse(key);
                                }
                            }
                        }
                    }
                    break;

                case "gameplay":
                    jobPreferences = new List <string>();
                    foreach (XElement ele in subElement.Element("jobpreferences").Elements("job"))
                    {
                        string jobIdentifier = ele.GetAttributeString("identifier", "");
                        if (string.IsNullOrEmpty(jobIdentifier))
                        {
                            continue;
                        }
                        jobPreferences.Add(jobIdentifier);
                    }
                    break;

                case "player":
                    defaultPlayerName  = subElement.GetAttributeString("name", "");
                    CharacterHeadIndex = subElement.GetAttributeInt("headindex", CharacterHeadIndex);
                    if (Enum.TryParse(subElement.GetAttributeString("gender", "none"), true, out Gender g))
                    {
                        CharacterGender = g;
                    }
                    if (Enum.TryParse(subElement.GetAttributeString("race", "white"), true, out Race r))
                    {
                        CharacterRace = r;
                    }
                    else
                    {
                        CharacterRace = Race.White;
                    }
                    CharacterHairIndex           = subElement.GetAttributeInt("hairindex", -1);
                    CharacterBeardIndex          = subElement.GetAttributeInt("beardindex", -1);
                    CharacterMoustacheIndex      = subElement.GetAttributeInt("moustacheindex", -1);
                    CharacterFaceAttachmentIndex = subElement.GetAttributeInt("faceattachmentindex", -1);
                    break;
                }
            }

            foreach (InputType inputType in Enum.GetValues(typeof(InputType)))
            {
                if (keyMapping[(int)inputType] == null)
                {
                    DebugConsole.ThrowError("Key binding for the input type \"" + inputType + " not set!");
                    keyMapping[(int)inputType] = new KeyOrMouse(Keys.D1);
                }
            }

            List <string>         missingPackagePaths  = new List <string>();
            List <ContentPackage> incompatiblePackages = new List <ContentPackage>();
            foreach (XElement subElement in doc.Root.Elements())
            {
                switch (subElement.Name.ToString().ToLowerInvariant())
                {
                case "contentpackage":
                    string path = System.IO.Path.GetFullPath(subElement.GetAttributeString("path", ""));
                    var    matchingContentPackage = ContentPackage.List.Find(cp => System.IO.Path.GetFullPath(cp.Path) == path);
                    if (matchingContentPackage == null)
                    {
                        missingPackagePaths.Add(path);
                    }
                    else if (!matchingContentPackage.IsCompatible())
                    {
                        incompatiblePackages.Add(matchingContentPackage);
                    }
                    else
                    {
                        SelectedContentPackages.Add(matchingContentPackage);
                    }
                    break;
                }
            }

            TextManager.LoadTextPacks(SelectedContentPackages);

            //display error messages after all content packages have been loaded
            //to make sure the package that contains text files has been loaded before we attempt to use TextManager
            foreach (string missingPackagePath in missingPackagePaths)
            {
                DebugConsole.ThrowError(TextManager.Get("ContentPackageNotFound").Replace("[packagepath]", missingPackagePath));
            }
            foreach (ContentPackage incompatiblePackage in incompatiblePackages)
            {
                DebugConsole.ThrowError(TextManager.Get(incompatiblePackage.GameVersion <= new Version(0, 0, 0, 0) ? "IncompatibleContentPackageUnknownVersion" : "IncompatibleContentPackage")
                                        .Replace("[packagename]", incompatiblePackage.Name)
                                        .Replace("[packageversion]", incompatiblePackage.GameVersion.ToString())
                                        .Replace("[gameversion]", GameMain.Version.ToString()));
            }
            foreach (ContentPackage contentPackage in SelectedContentPackages)
            {
                foreach (ContentFile file in contentPackage.Files)
                {
                    if (!System.IO.File.Exists(file.Path))
                    {
                        DebugConsole.ThrowError("Error in content package \"" + contentPackage.Name + "\" - file \"" + file.Path + "\" not found.");
                        continue;
                    }
                    ToolBox.IsProperFilenameCase(file.Path);
                }
            }
            if (!SelectedContentPackages.Any())
            {
                var availablePackage = ContentPackage.List.FirstOrDefault(cp => cp.IsCompatible() && cp.CorePackage);
                if (availablePackage != null)
                {
                    SelectedContentPackages.Add(availablePackage);
                }
            }

            //save to get rid of the invalid selected packages in the config file
            if (missingPackagePaths.Count > 0 || incompatiblePackages.Count > 0)
            {
                SaveNewPlayerConfig();
            }
        }