diff --git a/Assets/Scripts/Game/LocalizedBook.cs b/Assets/Scripts/Game/LocalizedBook.cs index 2b1732f2e1..37d688b50f 100644 --- a/Assets/Scripts/Game/LocalizedBook.cs +++ b/Assets/Scripts/Game/LocalizedBook.cs @@ -33,13 +33,13 @@ public class LocalizedBook const string markupFont = "[/font={0}]"; // Keywords for save/load localized book format - const string titleKeyword = "Title"; - const string authorKeyword = "Author"; - const string isNaughtyKeyword = "IsNaughty"; - const string priceKeyword = "Price"; - const string isUniqueKeyword = "IsUnique"; - const string whenVarSetKeyword = "WhenVarSet"; - const string contentKeyword = "Content"; + const string titleKeyword = "Title:"; + const string authorKeyword = "Author:"; + const string isNaughtyKeyword = "IsNaughty:"; + const string priceKeyword = "Price:"; + const string isUniqueKeyword = "IsUnique:"; + const string whenVarSetKeyword = "WhenVarSet:"; + const string contentKeyword = "Content:"; // Book data fields public string Title; // Title of book @@ -127,7 +127,67 @@ public bool OpenLocalizedBookFile(string filename) if (lines == null || lines.Length == 0) return false; - return false; + // Read file + bool readingContent = false; + Content = string.Empty; + for(int l = 0; l < lines.Length; l++) + { + if (!readingContent) + { + // Everything up to and including Content: line is considered data input + // Trim whitespace from either side of line and read tag data + string line = lines[l].Trim(); + if (line.StartsWith(titleKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // Title: + Title = line.Substring(titleKeyword.Length).Trim(); + } + else if (line.StartsWith(authorKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // Author: + Author = line.Substring(authorKeyword.Length).Trim(); + } + else if (line.StartsWith(isNaughtyKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // IsNaughty: + string isNaughtyString = line.Substring(isNaughtyKeyword.Length).Trim(); + if (!bool.TryParse(isNaughtyString, out IsNaughty)) + Debug.LogErrorFormat("Could not parse IsNaughty bool from '{0}'. Value must be True or False.", isNaughtyString); + } + else if (line.StartsWith(priceKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // Price: + string priceString = line.Substring(priceKeyword.Length).Trim(); + if (!int.TryParse(priceString, out Price)) + Debug.LogErrorFormat("Could not parse Price int from '{0}'. Value must be numerical.", priceString); + } + else if (line.StartsWith(isUniqueKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // IsUnique: + string uniqueString = line.Substring(isUniqueKeyword.Length).Trim(); + if (!bool.TryParse(uniqueString, out IsUnique)) + Debug.LogErrorFormat("Could not parse IsUnique bool from '{0}'. Value must be True or False."); + } + else if (line.StartsWith(whenVarSetKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // WhenVarSet: + WhenVarSet = line.Substring(whenVarSetKeyword.Length).Trim(); + } + else if (line.StartsWith(contentKeyword, System.StringComparison.InvariantCultureIgnoreCase)) + { + // Content: + readingContent = true; + } + } + else + { + // Everything after Content: tag is book contents + // Add back newline as File.ReadAllLines() strips this when splitting lines + Content += lines[l] + "\n"; + } + } + + return true; } /// @@ -138,13 +198,13 @@ public bool OpenLocalizedBookFile(string filename) public void SaveLocalizedBook(string filename) { StringBuilder builder = new StringBuilder(); - builder.AppendLine(string.Format("{0}: {1}", titleKeyword, Title)); - builder.AppendLine(string.Format("{0}: {1}", authorKeyword, Author)); - builder.AppendLine(string.Format("{0}: {1}", isNaughtyKeyword, IsNaughty)); - builder.AppendLine(string.Format("{0}: {1}", priceKeyword, Price)); - builder.AppendLine(string.Format("{0}: {1}", isUniqueKeyword, IsUnique)); - builder.AppendLine(string.Format("{0}: {1}", whenVarSetKeyword, WhenVarSet)); - builder.AppendLine(string.Format("{0}:\n{1}", contentKeyword, Content)); + builder.AppendLine(string.Format("{0} {1}", titleKeyword, Title)); + builder.AppendLine(string.Format("{0} {1}", authorKeyword, Author)); + builder.AppendLine(string.Format("{0} {1}", isNaughtyKeyword, IsNaughty)); + builder.AppendLine(string.Format("{0} {1}", priceKeyword, Price)); + builder.AppendLine(string.Format("{0} {1}", isUniqueKeyword, IsUnique)); + builder.AppendLine(string.Format("{0} {1}", whenVarSetKeyword, WhenVarSet)); + builder.AppendLine(string.Format("{0}\n{1}", contentKeyword, Content)); File.WriteAllText(filename, builder.ToString()); } @@ -192,4 +252,15 @@ string ConvertTokensToString(TextFile.Token[] tokens) return text; } + + /// + /// Convert book markup back to tokens. + /// + /// + /// + TextFile.Token[] ConvertStringToTokens(string input) + { + // TODO: + return null; + } } \ No newline at end of file diff --git a/Assets/Scripts/Game/UserInterfaceWindows/DaggerfallBookReaderWindow.cs b/Assets/Scripts/Game/UserInterfaceWindows/DaggerfallBookReaderWindow.cs index 44a873fcc7..399f57beb6 100644 --- a/Assets/Scripts/Game/UserInterfaceWindows/DaggerfallBookReaderWindow.cs +++ b/Assets/Scripts/Game/UserInterfaceWindows/DaggerfallBookReaderWindow.cs @@ -132,6 +132,14 @@ public void OpenBook(DaggerfallUnityItem target) IsBookOpen = labelFormatter.ReformatBook(target.message); if (IsBookOpen) bookLabels = labelFormatter.CreateLabels(); + + // TEMP: Test open localized book + //LocalizedBook lb = new LocalizedBook(); + //string filename = DaggerfallUnity.Instance.ItemHelper.GetBookFileName(target.message); + //if (!lb.OpenLocalizedBookFile(filename)) + // Debug.LogErrorFormat("Failed to open LocalizedBook '{0}'", filename); + //else + // Debug.LogFormat("Opened LocalizedBook '{0}'", filename); } void LayoutBook()