93 lines
No EOL
2.3 KiB
C#
93 lines
No EOL
2.3 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace JD.Serialization
|
|
{
|
|
internal class LuaWriter : IDisposable
|
|
{
|
|
private readonly TextWriter _textWriter;
|
|
private int _indentationLevel;
|
|
private readonly string _fileName = null;
|
|
|
|
public bool IgnoreDefaultValues { get; set; } = true;
|
|
|
|
public LuaWriter(string fileName)
|
|
: this(new StreamWriter(fileName + "_tmp")) // FIXME : Workaround to ensure files are loaded in good order by the engine (.tbl first, then .tpl).
|
|
{
|
|
_fileName = fileName;
|
|
}
|
|
|
|
public LuaWriter(TextWriter writer)
|
|
{
|
|
_textWriter = writer;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_textWriter.Dispose();
|
|
if (_fileName != null)
|
|
{
|
|
File.Copy(_fileName + "_tmp", _fileName, true);
|
|
File.Delete(_fileName + "_tmp");
|
|
}
|
|
}
|
|
|
|
public int IndentationLevel
|
|
{
|
|
get { return _indentationLevel; }
|
|
set { _indentationLevel = value; }
|
|
}
|
|
|
|
public void WriteLine(string format, params object[] args)
|
|
{
|
|
for (int i = 0; i < _indentationLevel; ++i)
|
|
{
|
|
_textWriter.Write(" ");
|
|
}
|
|
|
|
if (args != null && args.Length > 0)
|
|
{
|
|
_textWriter.WriteLine(format, args);
|
|
}
|
|
else
|
|
{
|
|
_textWriter.WriteLine(format);
|
|
}
|
|
}
|
|
|
|
public void Write(string text)
|
|
{
|
|
_textWriter.Write(text);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts/ends a lua block.
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IDisposable Indent()
|
|
{
|
|
WriteLine("{");
|
|
IndentationLevel++;
|
|
return new DisposableHelper(() =>
|
|
{
|
|
IndentationLevel--;
|
|
WriteLine(IndentationLevel > 0 ? "}," : "}");
|
|
});
|
|
}
|
|
|
|
private class DisposableHelper : IDisposable
|
|
{
|
|
private readonly System.Action _action;
|
|
|
|
public DisposableHelper(System.Action action)
|
|
{
|
|
_action = action;
|
|
}
|
|
|
|
public void Dispose()
|
|
{
|
|
_action();
|
|
}
|
|
}
|
|
}
|
|
} |