add live select pannel
This commit is contained in:
@@ -0,0 +1,276 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
using UnityEngine;
|
||||
|
||||
|
||||
public class AssetUtil
|
||||
{
|
||||
static public bool _BitTest(int valueToTest, int testBit)
|
||||
{
|
||||
return ((valueToTest & (1 << testBit)) == (1 << testBit));
|
||||
}
|
||||
|
||||
public enum PlatformID
|
||||
{
|
||||
iOS = 4,
|
||||
PC = 5,
|
||||
Android = 13
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// プラットフォームをAndroid(13)からPC(5)へ変更する
|
||||
/// </summary>
|
||||
public static bool RewriteAssetPlatform(ref byte[] bundle, string unityVersion)
|
||||
{
|
||||
if (unityVersion == "")
|
||||
{
|
||||
return false;
|
||||
}
|
||||
byte[] sArray = Encoding.ASCII.GetBytes(unityVersion);
|
||||
|
||||
//後ろにプラットフォームを付加
|
||||
Array.Resize(ref sArray, sArray.Length + 2);
|
||||
sArray[sArray.Length - 1] = (byte)PlatformID.Android;
|
||||
|
||||
//UnityVersionの位置を検索
|
||||
int keypos = ByteIndexOfOne(ref bundle, sArray);
|
||||
if (keypos > 0)
|
||||
{
|
||||
bundle[keypos + sArray.Length - 1] = (byte)PlatformID.PC; //platformをPC(05)に変更する
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("unityVersion:" + unityVersion + " Notfound!");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AssetBundleファイルをの中の画像ファイルのRewriteReadableを書き換えてEnableに変更する
|
||||
/// </summary>
|
||||
/// <param name="binArray">AssetBundleバイナリ</param>
|
||||
public static void RewriteReadable(ref byte[] binArray)
|
||||
{
|
||||
//Bundleから画像一覧を取得
|
||||
List<string> texList = new List<string>();
|
||||
AssetBundle bundle = AssetBundle.LoadFromMemory(binArray);
|
||||
foreach (var item in bundle.LoadAllAssets<Texture2D>())
|
||||
{
|
||||
texList.Add(item.name);
|
||||
}
|
||||
bundle.Unload(true);//読んだTextureは破棄
|
||||
|
||||
//binaryを書き換え
|
||||
foreach (var item in texList)
|
||||
{
|
||||
RewriteTextureReadableFlag(ref binArray, item);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// AssetBundleバイナリからRewriteReadableフラグを書き換える
|
||||
/// </summary>
|
||||
/// <param name="bundle">Bundleバイナリ</param>
|
||||
/// <param name="texname">テクスチャ名</param>
|
||||
private static void RewriteTextureReadableFlag(ref byte[] bundle, string texname)
|
||||
{
|
||||
int readableOffset = 0x1c;
|
||||
|
||||
//テクスチャ名をバイナリに変換
|
||||
byte[] tmparray = Encoding.ASCII.GetBytes(texname);
|
||||
//頭にバイナリ長を加えることで誤検索を防止
|
||||
byte[] searcharray = new byte[tmparray.Length + 4];
|
||||
Array.Copy(tmparray, 0, searcharray, 4, tmparray.Length);
|
||||
|
||||
searcharray[0] = (byte)tmparray.Length; //テキストファイル名長なので0xff(256)バイトは超えないはず…
|
||||
|
||||
//binaryを検索
|
||||
int keypos = ByteIndexOfOne(ref bundle, searcharray);
|
||||
|
||||
if (keypos < 0) { return; }
|
||||
|
||||
//4byte区切りのデータ列のため
|
||||
int offset = searcharray.Length;
|
||||
if ((offset % 4) > 0)
|
||||
{
|
||||
offset += 4 - (offset % 4);
|
||||
}
|
||||
|
||||
if (bundle[keypos + offset + readableOffset] == 0x00)
|
||||
{
|
||||
bundle[keypos + offset + readableOffset] = 0x01; //readableをTrueに変更する
|
||||
}
|
||||
if (bundle[keypos + offset + readableOffset + 1] == 0x00)
|
||||
{
|
||||
bundle[keypos + offset + readableOffset + 1] = 0x01; //ReadAllowedをTrueに変更する
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// バイナリ列からバイナリ列の検索を行い、最初に見つかったポジションを返す
|
||||
/// </summary>
|
||||
/// <param name="target">検索元バイナリ</param>
|
||||
/// <param name="pattern">検索パターン</param>
|
||||
/// <returns>結果位置</returns>
|
||||
private static int ByteIndexOfOne(ref byte[] target, byte[] pattern)
|
||||
{
|
||||
byte key = 0;
|
||||
int binLen = target.Length;
|
||||
|
||||
for (int i = 0; i < binLen; i++)
|
||||
{
|
||||
key = target[i];
|
||||
if (key == pattern[0])
|
||||
{
|
||||
int cnt = 1;
|
||||
while (true)
|
||||
{
|
||||
if ((i + cnt) == target.Length)
|
||||
{
|
||||
//target長を超えた
|
||||
break;
|
||||
}
|
||||
key = target[i + cnt];
|
||||
if (key != pattern[cnt])
|
||||
{
|
||||
//差異あり
|
||||
break;
|
||||
}
|
||||
cnt++;
|
||||
if (cnt == pattern.Length)
|
||||
{
|
||||
//パターン長合致
|
||||
return i;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// バイナリ列からバイナリ列の検索を行い全ての結果を返す
|
||||
/// </summary>
|
||||
/// <param name="target">検索元バイナリ</param>
|
||||
/// <param name="pattern">検索パターン</param>
|
||||
/// <returns>結果位置</returns>
|
||||
private static int[] ByteIndexOfMultiple(ref byte[] target, byte[] pattern)
|
||||
{
|
||||
byte key = 0;
|
||||
int binLen = target.Length;
|
||||
List<int> hitdatas = new List<int>();
|
||||
|
||||
for (int i = 0; i < binLen; i++)
|
||||
{
|
||||
key = target[i];
|
||||
if (key == pattern[0])
|
||||
{
|
||||
int cnt = 1;
|
||||
while (true)
|
||||
{
|
||||
if ((i + cnt) == target.Length)
|
||||
{
|
||||
//target長を超えた
|
||||
break;
|
||||
}
|
||||
key = target[i + cnt];
|
||||
if (key != pattern[cnt])
|
||||
{
|
||||
//差異あり
|
||||
break;
|
||||
}
|
||||
cnt++;
|
||||
if (cnt == pattern.Length)
|
||||
{
|
||||
//パターン長合致
|
||||
hitdatas.Add(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//あったら配列で返す
|
||||
if (hitdatas.Count > 0)
|
||||
{
|
||||
return hitdatas.ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// バイナリ列を検索して最初に見つかった検索パターンを置換する
|
||||
/// </summary>
|
||||
/// <param name="target">検索元バイナリ</param>
|
||||
/// <param name="pattern">検索パターン</param>
|
||||
/// <param name="replace">置換パターン</param>
|
||||
/// <returns>位置</returns>
|
||||
private static int ReplaceIndexOfOne(ref byte[] target, byte[] pattern, byte[] replace)
|
||||
{
|
||||
if (pattern.Length != replace.Length)
|
||||
{
|
||||
return -2;
|
||||
}
|
||||
|
||||
int pos = ByteIndexOfOne(ref target, pattern);
|
||||
if (pos < 0) { return pos; } //見つからなかった
|
||||
|
||||
int length = pattern.Length;
|
||||
for (int i = 0; i < length; i++)
|
||||
{
|
||||
if (target[pos + i] == pattern[i])
|
||||
{
|
||||
target[pos + i] = replace[i];
|
||||
}
|
||||
}
|
||||
return pos;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// バイナリ列を検索して検索パターンにヒットした箇所を全て置換する
|
||||
/// </summary>
|
||||
/// <param name="target">検索元バイナリ</param>
|
||||
/// <param name="pattern">検索パターン</param>
|
||||
/// <param name="replace">置換パターン</param>
|
||||
/// <returns>位置配列</returns>
|
||||
private static int[] ReplaceIndexOfAll(ref byte[] target, byte[] pattern, byte[] replace)
|
||||
{
|
||||
if (pattern.Length != replace.Length)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
int[] pos = ByteIndexOfMultiple(ref target, pattern);
|
||||
if (pos == null) { return null; } //見つからなかった
|
||||
|
||||
int length = pattern.Length;
|
||||
for (int i = 0; i < pos.Length; i++)
|
||||
{
|
||||
for (int j = 0; j < length; j++)
|
||||
{
|
||||
if (target[pos[i] + j] == pattern[j])
|
||||
{
|
||||
target[pos[i] + j] = replace[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return pos; //置換回数を返す
|
||||
}
|
||||
|
||||
private static uint readIntBE(byte[] m_array)
|
||||
{
|
||||
if (m_array.Length == 4)
|
||||
{
|
||||
return (uint)(m_array[3] | (m_array[2] << 8) | (m_array[1] << 16) | (m_array[0] << 24));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 33490942ca39a6e4f9eea5c7f9041e35
|
||||
timeCreated: 1528619407
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace LZ4
|
||||
{
|
||||
|
||||
public class BinaryReader
|
||||
{
|
||||
private byte[] m_array;
|
||||
private int m_curPos = 0;
|
||||
|
||||
public BinaryReader(byte[] data)
|
||||
{
|
||||
m_array = new byte[data.Length];
|
||||
data.CopyTo(m_array, 0);
|
||||
}
|
||||
|
||||
public BinaryReader(string file)
|
||||
{
|
||||
System.IO.FileStream fs = new System.IO.FileStream(
|
||||
file,
|
||||
System.IO.FileMode.Open,
|
||||
System.IO.FileAccess.Read
|
||||
);
|
||||
|
||||
System.IO.FileInfo fi = new System.IO.FileInfo(file);
|
||||
m_array = new byte[fi.Length];
|
||||
|
||||
fs.Seek(0, System.IO.SeekOrigin.Begin);
|
||||
fs.Read(m_array, 0, (int)fi.Length);
|
||||
|
||||
fs.Close();
|
||||
}
|
||||
|
||||
public byte readByte()
|
||||
{
|
||||
m_curPos++;
|
||||
return m_array[m_curPos - 1];
|
||||
}
|
||||
|
||||
public ushort readShortLE()
|
||||
{
|
||||
m_curPos += 2;
|
||||
return (ushort)(m_array[m_curPos - 2] | (m_array[m_curPos - 1] << 8));
|
||||
}
|
||||
|
||||
public int readIntLE()
|
||||
{
|
||||
m_curPos += 4;
|
||||
return (int)(m_array[m_curPos - 4] | (m_array[m_curPos - 3] << 8) | (m_array[m_curPos - 2] << 16) | (m_array[m_curPos - 1] << 24));
|
||||
}
|
||||
|
||||
public void copyBytes(byte[] dst, int offset, int size)
|
||||
{
|
||||
Array.Copy(m_array, m_curPos, dst, offset, size);
|
||||
m_curPos += size;
|
||||
}
|
||||
|
||||
public void seekAbs(int pos)
|
||||
{
|
||||
this.m_curPos = pos;
|
||||
}
|
||||
|
||||
public void seekRel(int diff)
|
||||
{
|
||||
m_curPos += diff;
|
||||
}
|
||||
|
||||
public int getPos()
|
||||
{
|
||||
return m_curPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 78d8fc54671e1074f8cb90f1911e2b72
|
||||
timeCreated: 1528619541
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,129 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
//using LZ4Sharp;
|
||||
using LZ4ps;
|
||||
|
||||
namespace LZ4
|
||||
{
|
||||
/// <summary>
|
||||
/// 置換
|
||||
/// </summary>
|
||||
public class LZ4Util
|
||||
{
|
||||
public static byte[] decompress(byte[] indata)
|
||||
{
|
||||
int m_curPos = 4;
|
||||
int token = (int)(indata[m_curPos - 4] | (indata[m_curPos - 3] << 8) | (indata[m_curPos - 2] << 16) | (indata[m_curPos - 1] << 24));
|
||||
m_curPos += 4;
|
||||
int decompSize = (int)(indata[m_curPos - 4] | (indata[m_curPos - 3] << 8) | (indata[m_curPos - 2] << 16) | (indata[m_curPos - 1] << 24));
|
||||
m_curPos += 4;
|
||||
int compSize = (int)(indata[m_curPos - 4] | (indata[m_curPos - 3] << 8) | (indata[m_curPos - 2] << 16) | (indata[m_curPos - 1] << 24));
|
||||
m_curPos += 4;
|
||||
int tmp4 = (int)(indata[m_curPos - 4] | (indata[m_curPos - 3] << 8) | (indata[m_curPos - 2] << 16) | (indata[m_curPos - 1] << 24));
|
||||
|
||||
//byte[] srcBytes = new byte[compSize];
|
||||
//Array.Copy(indata, 0x10, srcBytes, 0, compSize);
|
||||
|
||||
if (IntPtr.Size == 4)
|
||||
{
|
||||
return LZ4Codec.Decode32(indata, 0x10, compSize, decompSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
return LZ4Codec.Decode64(indata, 0x10, compSize, decompSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
public class LZ4Util
|
||||
{
|
||||
|
||||
public static byte[] decompress(byte[] indata)
|
||||
{
|
||||
LZ4.BinaryReader r = new LZ4.BinaryReader(indata);
|
||||
|
||||
byte[] dec = decompress(r);
|
||||
|
||||
return dec;
|
||||
}
|
||||
|
||||
public static byte[] decompress(BinaryReader r)
|
||||
{
|
||||
byte[] retArray;
|
||||
var dataSize = 0;
|
||||
var decompressedSize = 0;
|
||||
|
||||
var token = 0;
|
||||
var sqSize = 0;
|
||||
var matchSize = 0;
|
||||
var offset = 0;
|
||||
var retCurPos = 0;
|
||||
var endPos = 0;
|
||||
|
||||
r.seekAbs(4);
|
||||
decompressedSize = r.readIntLE();
|
||||
dataSize = r.readIntLE();
|
||||
endPos = dataSize + 16;
|
||||
retArray = new byte[decompressedSize];
|
||||
|
||||
r.seekAbs(16);
|
||||
|
||||
//Start reading sequences
|
||||
while (true)
|
||||
{
|
||||
//read the LiteralSize and the MatchSize
|
||||
token = r.readByte();
|
||||
sqSize = token >> 4;
|
||||
matchSize = (token & 0x0F) + 4;
|
||||
if (sqSize == 15)
|
||||
sqSize += readAdditionalSize(r);
|
||||
|
||||
//copy the literal
|
||||
r.copyBytes(retArray, retCurPos, sqSize);
|
||||
retCurPos += sqSize;
|
||||
|
||||
if (r.getPos() >= endPos - 1)
|
||||
break;
|
||||
|
||||
//read the offset
|
||||
offset = r.readShortLE();
|
||||
|
||||
//read the additional MatchSize
|
||||
if (matchSize == 19)
|
||||
matchSize += readAdditionalSize(r);
|
||||
|
||||
//copy the match properly
|
||||
if (matchSize > offset)
|
||||
{
|
||||
var matchPos = retCurPos - offset;
|
||||
while (true)
|
||||
{
|
||||
Array.Copy(retArray, matchPos, retArray, retCurPos, offset);
|
||||
retCurPos += offset;
|
||||
matchSize -= offset;
|
||||
if (matchSize < offset)
|
||||
break;
|
||||
}
|
||||
}
|
||||
Array.Copy(retArray, retCurPos - offset, retArray, retCurPos, matchSize);
|
||||
retCurPos += matchSize;
|
||||
}
|
||||
|
||||
|
||||
return retArray;
|
||||
}
|
||||
|
||||
public static int readAdditionalSize(BinaryReader reader)
|
||||
{
|
||||
var size = reader.readByte();
|
||||
if (size == 255)
|
||||
return size + readAdditionalSize(reader);
|
||||
else
|
||||
return size;
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d828dcccbcb46c944833c9b18532b270
|
||||
timeCreated: 1528619407
|
||||
licenseType: Free
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,632 @@
|
||||
/*
|
||||
* Copyright (c) 2013 Calvin Rien
|
||||
*
|
||||
* Based on the JSON parser by Patrick van Bergen
|
||||
* http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
|
||||
*
|
||||
* Simplified it so that it doesn't throw exceptions
|
||||
* and can be used in Unity iPhone with maximum code stripping.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining
|
||||
* a copy of this software and associated documentation files (the
|
||||
* "Software"), to deal in the Software without restriction, including
|
||||
* without limitation the rights to use, copy, modify, merge, publish,
|
||||
* distribute, sublicense, and/or sell copies of the Software, and to
|
||||
* permit persons to whom the Software is furnished to do so, subject to
|
||||
* the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be
|
||||
* included in all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text;
|
||||
|
||||
namespace MiniJSON
|
||||
{
|
||||
// Example usage:
|
||||
//
|
||||
// using UnityEngine;
|
||||
// using System.Collections;
|
||||
// using System.Collections.Generic;
|
||||
// using MiniJSON;
|
||||
//
|
||||
// public class MiniJSONTest : MonoBehaviour {
|
||||
// void Start () {
|
||||
// var jsonString = "{ \"array\": [1.44,2,3], " +
|
||||
// "\"object\": {\"key1\":\"value1\", \"key2\":256}, " +
|
||||
// "\"string\": \"The quick brown fox \\\"jumps\\\" over the lazy dog \", " +
|
||||
// "\"unicode\": \"\\u3041 Men\u00fa sesi\u00f3n\", " +
|
||||
// "\"int\": 65536, " +
|
||||
// "\"float\": 3.1415926, " +
|
||||
// "\"bool\": true, " +
|
||||
// "\"null\": null }";
|
||||
//
|
||||
// var dict = Json.Deserialize(jsonString) as Dictionary<string,object>;
|
||||
//
|
||||
// Debug.Log("deserialized: " + dict.GetType());
|
||||
// Debug.Log("dict['array'][0]: " + ((List<object>) dict["array"])[0]);
|
||||
// Debug.Log("dict['string']: " + (string) dict["string"]);
|
||||
// Debug.Log("dict['float']: " + (double) dict["float"]); // floats come out as doubles
|
||||
// Debug.Log("dict['int']: " + (long) dict["int"]); // ints come out as longs
|
||||
// Debug.Log("dict['unicode']: " + (string) dict["unicode"]);
|
||||
//
|
||||
// var str = Json.Serialize(dict);
|
||||
//
|
||||
// Debug.Log("serialized: " + str);
|
||||
// }
|
||||
// }
|
||||
|
||||
/// <summary>
|
||||
/// This class encodes and decodes JSON strings.
|
||||
/// Spec. details, see http://www.json.org/
|
||||
///
|
||||
/// JSON uses Arrays and Objects. These correspond here to the datatypes IList and IDictionary.
|
||||
/// All numbers are parsed to doubles.
|
||||
/// </summary>
|
||||
public static class Json
|
||||
{
|
||||
/// <summary>
|
||||
/// Parses the string json into a value
|
||||
/// </summary>
|
||||
/// <param name="json">A JSON string.</param>
|
||||
/// <returns>An List<object>, a Dictionary<string, object>, a double, an integer,a string, null, true, or false</returns>
|
||||
public static object Deserialize(string json)
|
||||
{
|
||||
// save the string for debug information
|
||||
if (json == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Parser.Parse(json);
|
||||
}
|
||||
|
||||
sealed class Parser : IDisposable
|
||||
{
|
||||
const string WORD_BREAK = "{}[],:\"";
|
||||
|
||||
public static bool IsWordBreak(char c)
|
||||
{
|
||||
return Char.IsWhiteSpace(c) || WORD_BREAK.IndexOf(c) != -1;
|
||||
}
|
||||
|
||||
enum TOKEN
|
||||
{
|
||||
NONE,
|
||||
CURLY_OPEN,
|
||||
CURLY_CLOSE,
|
||||
SQUARED_OPEN,
|
||||
SQUARED_CLOSE,
|
||||
COLON,
|
||||
COMMA,
|
||||
STRING,
|
||||
NUMBER,
|
||||
TRUE,
|
||||
FALSE,
|
||||
NULL
|
||||
};
|
||||
|
||||
StringReader json;
|
||||
|
||||
Parser(string jsonString)
|
||||
{
|
||||
json = new StringReader(jsonString);
|
||||
}
|
||||
|
||||
public static object Parse(string jsonString)
|
||||
{
|
||||
using (var instance = new Parser(jsonString))
|
||||
{
|
||||
return instance.ParseValue();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
json.Dispose();
|
||||
json = null;
|
||||
}
|
||||
|
||||
Dictionary<string, object> ParseObject()
|
||||
{
|
||||
Dictionary<string, object> table = new Dictionary<string, object>();
|
||||
|
||||
// ditch opening brace
|
||||
json.Read();
|
||||
|
||||
// {
|
||||
while (true)
|
||||
{
|
||||
switch (NextToken)
|
||||
{
|
||||
case TOKEN.NONE:
|
||||
return null;
|
||||
case TOKEN.COMMA:
|
||||
continue;
|
||||
case TOKEN.CURLY_CLOSE:
|
||||
return table;
|
||||
default:
|
||||
// name
|
||||
string name = ParseString();
|
||||
if (name == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
// :
|
||||
if (NextToken != TOKEN.COLON)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
// ditch the colon
|
||||
json.Read();
|
||||
|
||||
// value
|
||||
table[name] = ParseValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<object> ParseArray()
|
||||
{
|
||||
List<object> array = new List<object>();
|
||||
|
||||
// ditch opening bracket
|
||||
json.Read();
|
||||
|
||||
// [
|
||||
var parsing = true;
|
||||
while (parsing)
|
||||
{
|
||||
TOKEN nextToken = NextToken;
|
||||
|
||||
switch (nextToken)
|
||||
{
|
||||
case TOKEN.NONE:
|
||||
return null;
|
||||
case TOKEN.COMMA:
|
||||
continue;
|
||||
case TOKEN.SQUARED_CLOSE:
|
||||
parsing = false;
|
||||
break;
|
||||
default:
|
||||
object value = ParseByToken(nextToken);
|
||||
|
||||
array.Add(value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
object ParseValue()
|
||||
{
|
||||
TOKEN nextToken = NextToken;
|
||||
return ParseByToken(nextToken);
|
||||
}
|
||||
|
||||
object ParseByToken(TOKEN token)
|
||||
{
|
||||
switch (token)
|
||||
{
|
||||
case TOKEN.STRING:
|
||||
return ParseString();
|
||||
case TOKEN.NUMBER:
|
||||
return ParseNumber();
|
||||
case TOKEN.CURLY_OPEN:
|
||||
return ParseObject();
|
||||
case TOKEN.SQUARED_OPEN:
|
||||
return ParseArray();
|
||||
case TOKEN.TRUE:
|
||||
return true;
|
||||
case TOKEN.FALSE:
|
||||
return false;
|
||||
case TOKEN.NULL:
|
||||
return null;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
string ParseString()
|
||||
{
|
||||
StringBuilder s = new StringBuilder();
|
||||
char c;
|
||||
|
||||
// ditch opening quote
|
||||
json.Read();
|
||||
|
||||
bool parsing = true;
|
||||
while (parsing)
|
||||
{
|
||||
|
||||
if (json.Peek() == -1)
|
||||
{
|
||||
parsing = false;
|
||||
break;
|
||||
}
|
||||
|
||||
c = NextChar;
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
parsing = false;
|
||||
break;
|
||||
case '\\':
|
||||
if (json.Peek() == -1)
|
||||
{
|
||||
parsing = false;
|
||||
break;
|
||||
}
|
||||
|
||||
c = NextChar;
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
case '\\':
|
||||
case '/':
|
||||
s.Append(c);
|
||||
break;
|
||||
case 'b':
|
||||
s.Append('\b');
|
||||
break;
|
||||
case 'f':
|
||||
s.Append('\f');
|
||||
break;
|
||||
case 'n':
|
||||
s.Append('\n');
|
||||
break;
|
||||
case 'r':
|
||||
s.Append('\r');
|
||||
break;
|
||||
case 't':
|
||||
s.Append('\t');
|
||||
break;
|
||||
case 'u':
|
||||
var hex = new char[4];
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
hex[i] = NextChar;
|
||||
}
|
||||
|
||||
s.Append((char)Convert.ToInt32(new string(hex), 16));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
s.Append(c);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return s.ToString();
|
||||
}
|
||||
|
||||
object ParseNumber()
|
||||
{
|
||||
string number = NextWord;
|
||||
|
||||
if (number.IndexOf('.') == -1)
|
||||
{
|
||||
long parsedInt;
|
||||
Int64.TryParse(number, out parsedInt);
|
||||
return parsedInt;
|
||||
}
|
||||
|
||||
double parsedDouble;
|
||||
Double.TryParse(number, out parsedDouble);
|
||||
return parsedDouble;
|
||||
}
|
||||
|
||||
void EatWhitespace()
|
||||
{
|
||||
while (Char.IsWhiteSpace(PeekChar))
|
||||
{
|
||||
json.Read();
|
||||
|
||||
if (json.Peek() == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
char PeekChar
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToChar(json.Peek());
|
||||
}
|
||||
}
|
||||
|
||||
char NextChar
|
||||
{
|
||||
get
|
||||
{
|
||||
return Convert.ToChar(json.Read());
|
||||
}
|
||||
}
|
||||
|
||||
string NextWord
|
||||
{
|
||||
get
|
||||
{
|
||||
StringBuilder word = new StringBuilder();
|
||||
|
||||
while (!IsWordBreak(PeekChar))
|
||||
{
|
||||
word.Append(NextChar);
|
||||
|
||||
if (json.Peek() == -1)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return word.ToString();
|
||||
}
|
||||
}
|
||||
|
||||
TOKEN NextToken
|
||||
{
|
||||
get
|
||||
{
|
||||
EatWhitespace();
|
||||
|
||||
if (json.Peek() == -1)
|
||||
{
|
||||
return TOKEN.NONE;
|
||||
}
|
||||
|
||||
switch (PeekChar)
|
||||
{
|
||||
case '{':
|
||||
return TOKEN.CURLY_OPEN;
|
||||
case '}':
|
||||
json.Read();
|
||||
return TOKEN.CURLY_CLOSE;
|
||||
case '[':
|
||||
return TOKEN.SQUARED_OPEN;
|
||||
case ']':
|
||||
json.Read();
|
||||
return TOKEN.SQUARED_CLOSE;
|
||||
case ',':
|
||||
json.Read();
|
||||
return TOKEN.COMMA;
|
||||
case '"':
|
||||
return TOKEN.STRING;
|
||||
case ':':
|
||||
return TOKEN.COLON;
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
case '4':
|
||||
case '5':
|
||||
case '6':
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
case '-':
|
||||
return TOKEN.NUMBER;
|
||||
}
|
||||
|
||||
switch (NextWord)
|
||||
{
|
||||
case "false":
|
||||
return TOKEN.FALSE;
|
||||
case "true":
|
||||
return TOKEN.TRUE;
|
||||
case "null":
|
||||
return TOKEN.NULL;
|
||||
}
|
||||
|
||||
return TOKEN.NONE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts a IDictionary / IList object or a simple type (string, int, etc.) into a JSON string
|
||||
/// </summary>
|
||||
/// <param name="json">A Dictionary<string, object> / List<object></param>
|
||||
/// <returns>A JSON encoded string, or null if object 'json' is not serializable</returns>
|
||||
public static string Serialize(object obj)
|
||||
{
|
||||
return Serializer.Serialize(obj);
|
||||
}
|
||||
|
||||
sealed class Serializer
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
Serializer()
|
||||
{
|
||||
builder = new StringBuilder();
|
||||
}
|
||||
|
||||
public static string Serialize(object obj)
|
||||
{
|
||||
var instance = new Serializer();
|
||||
|
||||
instance.SerializeValue(obj);
|
||||
|
||||
return instance.builder.ToString();
|
||||
}
|
||||
|
||||
void SerializeValue(object value)
|
||||
{
|
||||
IList asList;
|
||||
IDictionary asDict;
|
||||
string asStr;
|
||||
|
||||
if (value == null)
|
||||
{
|
||||
builder.Append("null");
|
||||
}
|
||||
else if ((asStr = value as string) != null)
|
||||
{
|
||||
SerializeString(asStr);
|
||||
}
|
||||
else if (value is bool)
|
||||
{
|
||||
builder.Append((bool)value ? "true" : "false");
|
||||
}
|
||||
else if ((asList = value as IList) != null)
|
||||
{
|
||||
SerializeArray(asList);
|
||||
}
|
||||
else if ((asDict = value as IDictionary) != null)
|
||||
{
|
||||
SerializeObject(asDict);
|
||||
}
|
||||
else if (value is char)
|
||||
{
|
||||
SerializeString(new string((char)value, 1));
|
||||
}
|
||||
else
|
||||
{
|
||||
SerializeOther(value);
|
||||
}
|
||||
}
|
||||
|
||||
void SerializeObject(IDictionary obj)
|
||||
{
|
||||
bool first = true;
|
||||
|
||||
builder.Append('{');
|
||||
|
||||
foreach (object e in obj.Keys)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
builder.Append(',');
|
||||
}
|
||||
|
||||
SerializeString(e.ToString());
|
||||
builder.Append(':');
|
||||
|
||||
SerializeValue(obj[e]);
|
||||
|
||||
first = false;
|
||||
}
|
||||
|
||||
builder.Append('}');
|
||||
}
|
||||
|
||||
void SerializeArray(IList anArray)
|
||||
{
|
||||
builder.Append('[');
|
||||
|
||||
bool first = true;
|
||||
|
||||
foreach (object obj in anArray)
|
||||
{
|
||||
if (!first)
|
||||
{
|
||||
builder.Append(',');
|
||||
}
|
||||
|
||||
SerializeValue(obj);
|
||||
|
||||
first = false;
|
||||
}
|
||||
|
||||
builder.Append(']');
|
||||
}
|
||||
|
||||
void SerializeString(string str)
|
||||
{
|
||||
builder.Append('\"');
|
||||
|
||||
char[] charArray = str.ToCharArray();
|
||||
foreach (var c in charArray)
|
||||
{
|
||||
switch (c)
|
||||
{
|
||||
case '"':
|
||||
builder.Append("\\\"");
|
||||
break;
|
||||
case '\\':
|
||||
builder.Append("\\\\");
|
||||
break;
|
||||
case '\b':
|
||||
builder.Append("\\b");
|
||||
break;
|
||||
case '\f':
|
||||
builder.Append("\\f");
|
||||
break;
|
||||
case '\n':
|
||||
builder.Append("\\n");
|
||||
break;
|
||||
case '\r':
|
||||
builder.Append("\\r");
|
||||
break;
|
||||
case '\t':
|
||||
builder.Append("\\t");
|
||||
break;
|
||||
default:
|
||||
int codepoint = Convert.ToInt32(c);
|
||||
if ((codepoint >= 32) && (codepoint <= 126))
|
||||
{
|
||||
builder.Append(c);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Append("\\u");
|
||||
builder.Append(codepoint.ToString("x4"));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
builder.Append('\"');
|
||||
}
|
||||
|
||||
void SerializeOther(object value)
|
||||
{
|
||||
// NOTE: decimals lose precision during serialization.
|
||||
// They always have, I'm just letting you know.
|
||||
// Previously floats and doubles lost precision too.
|
||||
if (value is float)
|
||||
{
|
||||
builder.Append(((float)value).ToString("R"));
|
||||
}
|
||||
else if (value is int
|
||||
|| value is uint
|
||||
|| value is long
|
||||
|| value is sbyte
|
||||
|| value is byte
|
||||
|| value is short
|
||||
|| value is ushort
|
||||
|| value is ulong)
|
||||
{
|
||||
builder.Append(value);
|
||||
}
|
||||
else if (value is double
|
||||
|| value is decimal)
|
||||
{
|
||||
builder.Append(Convert.ToDouble(value).ToString("R"));
|
||||
}
|
||||
else
|
||||
{
|
||||
SerializeString(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 54edc10f7e678794d8ffda934fbda011
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user