using System; using System.IO; using System.Collections.Generic; using UnityEngine; namespace UniLua { internal class LuaFile { private static readonly string LUA_ROOT = System.IO.Path.Combine(Application.streamingAssetsPath, "LuaRoot"); public static FileLoadInfo OpenFile( string filename ) { var path = System.IO.Path.Combine(LUA_ROOT, filename); return new FileLoadInfo(LuaFile.GetStream(path)); } public static bool Readable( string filename ) { var path = System.IO.Path.Combine(LUA_ROOT, filename); try { using( var stream = File.Open( path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite ) ) { return true; } } catch( Exception ) { return false; } } private static Stream GetStream(string path, float timeOut = 0.5f) { if (Application.platform == RuntimePlatform.Android) { WWW www = new WWW(path); while (!www.isDone) ; if (!string.IsNullOrEmpty(www.error)) { return null; } return new MemoryStream(www.bytes); } return File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); } } internal class FileLoadInfo : ILoadInfo, IDisposable { public FileLoadInfo( Stream stream ) { Stream = stream; Buf = new Queue(); } public int ReadByte() { if( Buf.Count > 0 ) return (int)Buf.Dequeue(); else return Stream.ReadByte(); } public int PeekByte() { if( Buf.Count > 0 ) return (int)Buf.Peek(); else { var c = Stream.ReadByte(); if( c == -1 ) return c; Save( (byte)c ); return c; } } public void Dispose() { Stream.Dispose(); } private const string UTF8_BOM = "\u00EF\u00BB\u00BF"; private Stream Stream; private Queue Buf; private void Save( byte b ) { Buf.Enqueue( b ); } private void Clear() { Buf.Clear(); } private int SkipBOM() { for( var i=0; i