C# - object转成lua文件

2017-09-11 14:45:46来源:网络收集作者:程序诗人人点击

分享

将C#数据结构转成lua格式的文件。需要将C#数据结构封装成字典,然后调用HELua.Encode(),可以得到转换成后lua格式的table,将其保存为lua文件即可。


#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.IO;
namespace TileEditor
{
public class HELua
{
private const int BUILDER_CAPACITY = 2000;
public static string Encode (object lua)
{
StringBuilder builder = new StringBuilder (BUILDER_CAPACITY);
bool success = SerializeValue (lua, builder);
return (success ? builder.ToString () : null);
}
protected static bool SerializeValue(object value, StringBuilder builder)
{
bool success = true;
if (value is string) {
success = SerializeString ((string)value, builder);
} else if (value is IDictionary) {
success = SerializeObject ((IDictionary)value, builder);
} else if (value is IList) {
success = SerializeArray (value as IList, builder);
} else if ((value is Boolean) && ((Boolean)value == true)) {
builder.Append ("true");
} else if ((value is Boolean) && ((Boolean)value == false)) {
builder.Append ("false");
} else if (value is ValueType) {
success = SerializeNumber (Convert.ToDouble (value), builder);
} else if (value == null) {
builder.Append ("nil");
} else {
success = false;
}
return success;
}
protected static bool SerializeString(string aString, StringBuilder builder)
{
builder.Append ("/"");
char[] charArray = aString.ToCharArray ();
for (int i = 0; i < charArray.Length; i++) {
char c = charArray [i];
if (c == '"') {
builder.Append ("///"");
} else if (c == '//') {
builder.Append ("////");
} else if (c == '/b') {
builder.Append ("//b");
} else if (c == '/f') {
builder.Append ("//f");
} else if (c == '/n') {
builder.Append ("//n");
} else if (c == '/r') {
builder.Append ("//r");
} else if (c == '/t') {
builder.Append ("//t");
} else {
int codepoint = Convert.ToInt32 (c);
if ((codepoint >= 32) && (codepoint <= 126)) {
builder.Append (c);
} else {
builder.Append ("//u" + Convert.ToString (codepoint, 16).PadLeft (4, '0'));
}
}
}
builder.Append ("/"");
return true;
}
protected static bool SerializeObject(IDictionary anObject, StringBuilder builder)
{
builder.Append ("{");
IDictionaryEnumerator e = anObject.GetEnumerator ();
bool first = true;
while (e.MoveNext()) {
string key = e.Key.ToString ();
object value = e.Value;
if (!first) {
builder.Append (", ");
}
builder.Append("[");
SerializeString (key, builder);
builder.Append ("]");
builder.Append (" = ");
if (!SerializeValue(value, builder)) {
return false;
}
first = false;
}
builder.Append ("}");
return true;
}
protected static bool SerializeArray(IList anArray, StringBuilder builder)
{
builder.Append ("{");
bool first = true;
for (int i = 0; i < anArray.Count; i++) {
object value = anArray [i];
if (!first) {
builder.Append (", ");
}
if (!SerializeValue(value, builder)) {
return false;
}
first = false;
}
builder.Append ("}");
return true;
}
protected static bool SerializeNumber(Double number, StringBuilder builder)
{
builder.Append (Convert.ToString (number, CultureInfo.InvariantCulture));
return true;
}
}
}
#endif


最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台