You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
109 lines
2.1 KiB
109 lines
2.1 KiB
11 years ago
|
using UnityEngine;
|
||
|
using System.Collections;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Text.RegularExpressions;
|
||
10 years ago
|
using Fungus.Script;
|
||
11 years ago
|
|
||
|
namespace Fungus
|
||
|
{
|
||
11 years ago
|
/**
|
||
|
* Parses a text file using a simple format and adds string values to the global string table.
|
||
|
* The format is:
|
||
|
* $FirstString
|
||
|
* The first string text goes here
|
||
|
* $SecondString
|
||
|
* The second string text goes here
|
||
|
* # This is a comment line and will be ignored by the parser
|
||
|
*/
|
||
11 years ago
|
public class StringsParser : MonoBehaviour
|
||
|
{
|
||
|
public TextAsset stringsFile;
|
||
|
|
||
|
private enum ParseMode
|
||
|
{
|
||
|
Idle,
|
||
|
Text,
|
||
|
};
|
||
|
|
||
|
void Start()
|
||
|
{
|
||
11 years ago
|
ProcessText(stringsFile.text);
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
void ProcessText(string text)
|
||
11 years ago
|
{
|
||
|
// Split text into lines. Add a newline at end to ensure last command is always parsed
|
||
|
string[] lines = Regex.Split(text + "\n", "(?<=\n)");
|
||
|
|
||
|
string blockBuffer = "";
|
||
|
|
||
|
ParseMode mode = ParseMode.Idle;
|
||
|
|
||
|
string blockTag = "";
|
||
|
for (int i = 0; i < lines.Length; ++i)
|
||
|
{
|
||
|
string line = lines[i];
|
||
|
|
||
|
bool newBlock = line.StartsWith("$");
|
||
|
|
||
|
if (mode == ParseMode.Idle && !newBlock)
|
||
|
{
|
||
|
// Ignore any text not preceded by a label tag
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
string newBlockTag = "";
|
||
|
if (newBlock)
|
||
|
{
|
||
|
newBlockTag = line.Replace ("\n", "");
|
||
|
}
|
||
|
|
||
|
bool endOfFile = (i == lines.Length-1);
|
||
|
|
||
|
bool storeBlock = false;
|
||
|
|
||
|
if (newBlock)
|
||
|
{
|
||
|
storeBlock = true;
|
||
|
}
|
||
|
else if (mode == ParseMode.Text && endOfFile)
|
||
|
{
|
||
|
storeBlock = true;
|
||
|
if (!line.StartsWith("#"))
|
||
|
{
|
||
|
blockBuffer += line;
|
||
|
}
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
if (!line.StartsWith("#"))
|
||
|
{
|
||
|
blockBuffer += line;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (storeBlock)
|
||
|
{
|
||
|
if (blockTag.Length > 0 && blockBuffer.Length > 0)
|
||
|
{
|
||
|
// Trim off last newline
|
||
|
blockBuffer = blockBuffer.TrimEnd( '\r', '\n', ' ', '\t');
|
||
|
|
||
10 years ago
|
GlobalVariables.SetString(blockTag, blockBuffer);
|
||
11 years ago
|
}
|
||
|
|
||
|
// Prepare to parse next block
|
||
|
mode = ParseMode.Idle;
|
||
|
if (newBlock)
|
||
|
{
|
||
|
mode = ParseMode.Text;
|
||
|
blockTag = newBlockTag;
|
||
|
}
|
||
|
|
||
|
blockBuffer = "";
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|