Skip to content

Commit

Permalink
Progress LocalizedBook
Browse files Browse the repository at this point in the history
  • Loading branch information
Interkarma committed Nov 29, 2022
1 parent 4124fb4 commit 5d3a919
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 15 deletions.
101 changes: 86 additions & 15 deletions Assets/Scripts/Game/LocalizedBook.cs
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/// <summary>
Expand All @@ -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());
}

Expand Down Expand Up @@ -192,4 +252,15 @@ string ConvertTokensToString(TextFile.Token[] tokens)

return text;
}

/// <summary>
/// Convert book markup back to tokens.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
TextFile.Token[] ConvertStringToTokens(string input)
{
// TODO:
return null;
}
}
Expand Up @@ -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()
Expand Down

0 comments on commit 5d3a919

Please sign in to comment.