Exemplo n.º 1
0
        public static Language FromModExport(ModAsset asset)
        {
            Language lang = new Language();

            using (BinaryReader reader = new BinaryReader(asset.Stream)) {
                lang.Id    = reader.ReadString();
                lang.Label = reader.ReadString();

                lang.IconPath = reader.ReadString();
                lang.Icon     = new MTexture(VirtualContent.CreateTexture(Path.Combine("Dialog", lang.IconPath)));

                lang.Order = reader.ReadInt32();

                lang.FontFace     = reader.ReadString();
                lang.FontFaceSize = reader.ReadSingle();

                lang.SplitRegex       = reader.ReadString();
                lang.CommaCharacters  = reader.ReadString();
                lang.PeriodCharacters = reader.ReadString();

                lang.Lines = reader.ReadInt32();
                lang.Words = reader.ReadInt32();

                int count = reader.ReadInt32();
                for (int i = 0; i < count; i++)
                {
                    string key = reader.ReadString();
                    lang.Dialog[key]  = reader.ReadString();
                    lang.Cleaned[key] = reader.ReadString();
                }
            }

            return(lang);
        }
Exemplo n.º 2
0
        public static void PostLanguageLoad()
        {
            Language[] langs = Dialog.Languages.Values.Distinct().ToArray();
            Dialog.Languages.Clear();

            foreach (Language lang in langs)
            {
                if (lang.Dialog.Count == 0 || (string.IsNullOrEmpty(lang.Label) && string.IsNullOrEmpty(lang.FontFace) && string.IsNullOrEmpty(lang.IconPath)))
                {
                    if (lang.Icon != null)
                    {
                        lang.Dispose();
                    }
                    continue;
                }

                if (string.IsNullOrEmpty(lang.FontFace))
                {
                    lang.FontFace     = "Renogare";
                    lang.FontFaceSize = 64;
                }

                if (lang.Icon == null)
                {
                    lang.Icon = new MTexture(VirtualContent.CreateTexture(Path.Combine("Graphics", "Atlases", "Gui", "menu", "langnoicon")));
                }

                Dialog.Languages[lang.Id] = lang;
                if (lang.Id.Equals("english", StringComparison.InvariantCultureIgnoreCase))
                {
                    FallbackLanguage = lang;
                }
            }

            Dialog.OrderedLanguages = new List <Language>();
            foreach (KeyValuePair <string, Language> keyValuePair in Dialog.Languages)
            {
                Dialog.OrderedLanguages.Add(keyValuePair.Value);
            }
            Dialog.OrderedLanguages.Sort((a, b) => a.Order != b.Order ? a.Order - b.Order : a.Id.CompareTo(b.Id));
        }
Exemplo n.º 3
0
        private static void ReadAtlasData(Atlas _atlas, string path, AtlasDataFormat format)
        {
            if (VTextureToMTextureMap == null)
            {
                VTextureToMTextureMap = new Dictionary <string, MTexture>();
            }

            patch_Atlas atlas = (patch_Atlas)_atlas;

            string pathFull = Path.Combine(Engine.ContentDirectory, path);

            XmlDocument    xmlDoc;
            VirtualTexture texV;
            MTexture       texM;

            switch (format)
            {
            case AtlasDataFormat.TexturePacker_Sparrow:
                xmlDoc = Calc.LoadContentXML(path);
                XmlElement xmlTextureAtlas = xmlDoc["TextureAtlas"];
                if (xmlTextureAtlas == null)
                {
                    break;
                }

                texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), xmlTextureAtlas.Attr("imagePath", "")));
                texM = new MTexture(texV);
                VTextureToMTextureMap[texV.Name] = texM;
                atlas.Sources.Add(texV);

                XmlNodeList xmlSubs = xmlTextureAtlas.GetElementsByTagName("SubTexture");
                foreach (XmlElement xmlSub in xmlSubs)
                {
                    string    name     = xmlSub.Attr("name");
                    Rectangle clipRect = xmlSub.Rect();
                    if (xmlSub.HasAttr("frameX"))
                    {
                        atlas.textures[name] = new MTexture(
                            texM, name, clipRect,
                            new Vector2(-xmlSub.AttrInt("frameX"), -xmlSub.AttrInt("frameY")),
                            xmlSub.AttrInt("frameWidth"), xmlSub.AttrInt("frameHeight")
                            );
                    }
                    else
                    {
                        atlas.textures[name] = new MTexture(texM, name, clipRect);
                    }
                }
                break;

            case AtlasDataFormat.CrunchXml:
                if (!File.Exists(pathFull))
                {
                    break;
                }

                xmlDoc = Calc.LoadContentXML(path);
                XmlElement xmlAtlas = xmlDoc["atlas"];

                foreach (XmlElement xmlAtlasSource in xmlAtlas)
                {
                    texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), xmlAtlasSource.Attr("n", "") + ".png"));
                    texM = new MTexture(texV);
                    VTextureToMTextureMap[texV.Name] = texM;
                    atlas.Sources.Add(texV);
                    foreach (XmlElement xmlSub in xmlAtlasSource)
                    {
                        string    name     = xmlSub.Attr("n");
                        Rectangle clipRect = new Rectangle(xmlSub.AttrInt("x"), xmlSub.AttrInt("y"), xmlSub.AttrInt("w"), xmlSub.AttrInt("h"));
                        if (xmlSub.HasAttr("fx"))
                        {
                            atlas.textures[name] = new MTexture(
                                texM, name, clipRect,
                                new Vector2(-xmlSub.AttrInt("fx"), -xmlSub.AttrInt("fy")),
                                xmlSub.AttrInt("fw"), xmlSub.AttrInt("fh")
                                );
                        }
                        else
                        {
                            atlas.textures[name] = new MTexture(texM, name, clipRect);
                        }
                    }
                }
                break;

            case AtlasDataFormat.CrunchBinary:
                if (!File.Exists(pathFull))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), reader.ReadNullTerminatedString() + ".png"));
                            texM = new MTexture(texV);
                            VTextureToMTextureMap[texV.Name] = texM;
                            atlas.Sources.Add(texV);
                            short subs = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name       = reader.ReadNullTerminatedString();
                                short  clipX      = reader.ReadInt16();
                                short  clipY      = reader.ReadInt16();
                                short  clipWidth  = reader.ReadInt16();
                                short  clipHeight = reader.ReadInt16();
                                short  offsX      = reader.ReadInt16();
                                short  offsY      = reader.ReadInt16();
                                short  width      = reader.ReadInt16();
                                short  height     = reader.ReadInt16();
                                atlas.textures[name] = new MTexture(
                                    texM, name, new Rectangle(clipX, clipY, clipWidth, clipHeight),
                                    new Vector2(-offsX, -offsY),
                                    width, height
                                    );
                            }
                        }
                    }
                break;

            case AtlasDataFormat.CrunchXmlOrBinary:
                if (File.Exists(pathFull + ".bin"))
                {
                    ReadAtlasData(atlas, path + ".bin", AtlasDataFormat.CrunchBinary);
                }
                else if (File.Exists(pathFull + ".xml"))
                {
                    ReadAtlasData(atlas, path + ".xml", AtlasDataFormat.CrunchXml);
                }
                return;

            case AtlasDataFormat.CrunchBinaryNoAtlas:
                if (!File.Exists(pathFull + ".bin"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".bin"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            string sourcePath = Path.Combine(Path.GetDirectoryName(path), reader.ReadNullTerminatedString());
                            short  subs       = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name     = reader.ReadNullTerminatedString();
                                short  unknownA = reader.ReadInt16();
                                short  unknownB = reader.ReadInt16();
                                short  unknownC = reader.ReadInt16();
                                short  unknownD = reader.ReadInt16();
                                short  offsX    = reader.ReadInt16();
                                short  offsY    = reader.ReadInt16();
                                short  width    = reader.ReadInt16();
                                short  height   = reader.ReadInt16();
                                texV = VirtualContent.CreateTexture(Path.Combine(sourcePath, name + ".png"));
                                atlas.textures[name] = VTextureToMTextureMap[texV.Name] = new MTexture(texV, new Vector2(-offsX, -offsY), width, height);
                                atlas.Sources.Add(texV);
                            }
                        }
                    }
                break;

            case AtlasDataFormat.Packer:
                if (!File.Exists(pathFull + ".meta"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".meta"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        reader.ReadInt32();  // ???
                        reader.ReadString(); // ???
                        reader.ReadInt32();  // ???
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            texV = VirtualContent.CreateTexture(Path.Combine(Path.GetDirectoryName(path), reader.ReadString() + ".data"));
                            texM = new MTexture(texV);
                            VTextureToMTextureMap[texV.Name] = texM;
                            atlas.Sources.Add(texV);
                            short subs = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name       = reader.ReadString().Replace('\\', '/');
                                short  clipX      = reader.ReadInt16();
                                short  clipY      = reader.ReadInt16();
                                short  clipWidth  = reader.ReadInt16();
                                short  clipHeight = reader.ReadInt16();
                                short  offsX      = reader.ReadInt16();
                                short  offsY      = reader.ReadInt16();
                                short  width      = reader.ReadInt16();
                                short  height     = reader.ReadInt16();
                                atlas.textures[name] = new MTexture(
                                    texM, name, new Rectangle(clipX, clipY, clipWidth, clipHeight),
                                    new Vector2(-offsX, -offsY),
                                    width, height
                                    );
                            }
                        }
                    }
                break;

            case AtlasDataFormat.PackerNoAtlas:
                if (!File.Exists(pathFull + ".meta"))
                {
                    break;
                }

                using (FileStream stream = File.OpenRead(pathFull + ".meta"))
                    using (BinaryReader reader = new BinaryReader(stream)) {
                        reader.ReadInt32();
                        reader.ReadString();
                        reader.ReadInt32();
                        short sources = reader.ReadInt16();
                        for (int i = 0; i < sources; i++)
                        {
                            string sourcePath = Path.Combine(Path.GetDirectoryName(path), reader.ReadString());
                            short  subs       = reader.ReadInt16();
                            for (int j = 0; j < subs; j++)
                            {
                                string name     = reader.ReadString().Replace('\\', '/');
                                short  unknownA = reader.ReadInt16();
                                short  unknownB = reader.ReadInt16();
                                short  unknownC = reader.ReadInt16();
                                short  unknownD = reader.ReadInt16();
                                short  offsX    = reader.ReadInt16();
                                short  offsY    = reader.ReadInt16();
                                short  width    = reader.ReadInt16();
                                short  height   = reader.ReadInt16();
                                texV = VirtualContent.CreateTexture(Path.Combine(sourcePath, name + ".data"));
                                atlas.textures[name] = VTextureToMTextureMap[texV.Name] = new MTexture(texV, new Vector2(-offsX, -offsY), width, height);
                                atlas.Sources.Add(texV);
                            }
                        }
                    }
                break;

            default:
                break;
            }
        }
Exemplo n.º 4
0
        public static Language LoadLanguage(string filename)
        {
            Language language = orig_LoadLanguage(filename);

            if (language.Id.Equals("english", StringComparison.InvariantCultureIgnoreCase))
            {
                FallbackLanguage = language;
            }

            string path = filename;

            if (path.StartsWith(Everest.Content.PathContentOrig))
            {
                path = path.Substring(Everest.Content.PathContentOrig.Length + 1);
            }
            path = path.Replace('\\', '/');
            if (path.EndsWith(".txt"))
            {
                path = path.Substring(0, path.Length - 4);
            }

            IEnumerable <ModAsset> metas = Everest.Content.Mods.Select(mod => {
                ModAsset asset;
                if (mod.Map.TryGetValue(path, out asset))
                {
                    return(asset);
                }
                return(null);
            }).Where(asset => asset != null);

            if (!metas.Any())
            {
                return(language);
            }

            foreach (ModAsset meta in metas)
            {
                string line;

                string        currentName = "";
                StringBuilder builder     = new StringBuilder();
                string        prev        = "";
                using (StreamReader reader = new StreamReader(meta.Stream))
                    while (reader.Peek() != -1)
                    {
                        line = reader.ReadLine().Trim('\r', '\n').Trim();

                        // The following is the original parser decompiled and formatted to our best understanding.

                        // ???
                        bool startsWithVariable = false;
                        foreach (string variable in LanguageDataVariables)
                        {
                            if (!string.IsNullOrEmpty(variable) && line.StartsWith(variable, StringComparison.InvariantCultureIgnoreCase))
                            {
                                startsWithVariable = true;
                                break;
                            }
                        }
                        if (!startsWithVariable)
                        {
                            line = Regex.Replace(line, @"\[unknown\]", @"", RegexOptions.IgnoreCase);
                            line = Regex.Replace(line, @"\[left\]", @"{left}", RegexOptions.IgnoreCase);
                            line = Regex.Replace(line, @"\[right\]", @"{right}", RegexOptions.IgnoreCase);
                            line = Regex.Replace(line, @"\[(?<content>[^\[\\]*(?:\\.[^\]\\]*)*)\]", @"{portrait ${content}}");
                        }

                        if (line.Length <= 0)
                        {
                            continue;
                        }
                        if (line[0] == '#')
                        {
                            continue;
                        }

                        line = line.Replace("\\#", "#");
                        bool isVariable = variable.IsMatch(line);
                        if (!isVariable)
                        {
                            if (builder.Length > 0)
                            {
                                string built = builder.ToString();
                                if (!built.EndsWith("{break}") && !built.EndsWith("{n}") && command.Replace(prev, "").Length > 0)
                                {
                                    builder.Append("{break}");
                                }
                            }
                            builder.Append(line);
                            goto Next;
                        }

                        if (!string.IsNullOrEmpty(currentName) && !language.Dialog.ContainsKey(currentName))
                        {
                            language.Dialog.Add(currentName, builder.ToString());
                        }

                        string[] splitByEqual = line.Split('=');
                        string   name         = splitByEqual[0].Trim();
                        string   value        = (splitByEqual.Length > 1) ? splitByEqual[1].Trim() : "";

                        if (name.Equals("language", StringComparison.OrdinalIgnoreCase))
                        {
                            string[] splitByComma = value.Split(',');
                            if (!Dialog.Languages.TryGetValue(splitByComma[0], out language))
                            {
                                language          = new Language();
                                language.FontFace = null;
                                language.Id       = splitByComma[0];
                                Dialog.Languages.Add(language.Id, language);
                            }
                            if (splitByComma.Length > 1)
                            {
                                language.Label = splitByComma[1];
                            }
                            goto Next;
                        }

                        if (name.Equals("icon", StringComparison.OrdinalIgnoreCase))
                        {
                            VirtualTexture texture = VirtualContent.CreateTexture(Path.Combine("Dialog", value));
                            language.Icon = new MTexture(texture);
                            goto Next;
                        }

                        if (name.Equals("order", StringComparison.OrdinalIgnoreCase))
                        {
                            language.Order = int.Parse(value);
                            goto Next;
                        }

                        if (name.Equals("font", StringComparison.OrdinalIgnoreCase))
                        {
                            string[] splitByComma = value.Split(',');
                            language.FontFace     = splitByComma[0];
                            language.FontFaceSize = float.Parse(splitByComma[1], CultureInfo.InvariantCulture);
                            goto Next;
                        }

                        if (name.Equals("SPLIT_REGEX", StringComparison.OrdinalIgnoreCase))
                        {
                            language.SplitRegex = value;
                            goto Next;
                        }

                        if (name.Equals("commas", StringComparison.OrdinalIgnoreCase))
                        {
                            language.CommaCharacters = value;
                            goto Next;
                        }

                        if (name.Equals("periods", StringComparison.OrdinalIgnoreCase))
                        {
                            language.PeriodCharacters = value;
                            goto Next;
                        }

                        currentName = name;
                        builder.Clear();
                        builder.Append(value);

Next:
                        prev = line;
                    }

                // The game originally also checks if the key already exists and uses Add(...).
                if (!string.IsNullOrEmpty(currentName) /* && !language.Dialog.ContainsKey(currentName)*/)
                {
                    // language.Dialog.Add(currentName, builder.ToString());
                    language.Dialog[currentName] = builder.ToString();
                }
            }

            return(language);
        }