internal static Delegate GetConverter(Type fromType, Type toType) { var key = new KeyAndValue(fromType, toType); Delegate @delegate; if (!Converters.TryGetValue(key, out @delegate)) { LambdaExpression lambda = null; if (fromType != null) { var fromExp = Expression.Parameter(fromType, "from"); var body = GetConvertExpression(fromType, toType, fromExp); if (body.Type != toType) { body = Expression.Convert(body, toType); } lambda = LambdaExpression.Lambda(typeof(Func <,>).MakeGenericType(fromType, toType), body, fromExp); } else { lambda = LambdaExpression.Lambda(typeof(Func <>).MakeGenericType(toType), GetDefaultValueExpression(toType)); } var converter = lambda.Compile(); lock (Converters) Converters[key] = converter; return(converter); } return(@delegate); }
public DeviceConfigurationResponse(SendReceiveResult response) { if (response == null) { throw new ArgumentNullException("response"); } if (!response.Success) { throw new ProtocolException(response.Error); } if (response.Lines.Count < 1) { throw new ProtocolException("Response to \"device-config\" command wasn't at least 1 line long."); } var deviceNameLine = new KeyAndValue(response.Lines[0]); if (deviceNameLine.Key != "Device Name") { throw new ProtocolException("Key of Device Name line isn't right."); } this.DeviceName = deviceNameLine.Value; // Remaining lines are comma (and space) separated with the format: // Name, Address, Type // where Type is one of: input, output, analogInput, analogOutput var ioSignals = new List <IOSignal>(); for (var i = 1; i < response.Lines.Count; i++) { ioSignals.Add(new IOSignal(response.Lines[i])); } this.IOSignals = ioSignals.AsReadOnly(); }
public static KeyAndValue GetVar(string name) { string tmp = ""; if (File.Exists(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ConsoleGameEngine.tmp")) { tmp = File.ReadAllText(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\ConsoleGameEngine.tmp"); } else { Crash("Ошибка, переменнные еще не объявлены"); } string[] vars = tmp.Split(';'); KeyAndValue ret = new KeyAndValue(null, null); foreach (var item in vars) { if (item.Split('=')[0] == name) { if (item.Split('=').Length > 1) { ret = (new KeyAndValue(item.Split('=')[0], item.Split('=')[1])); } } } return(ret); }
public DownloadOrPatchResponse(SendReceiveResult response) { if (response == null) { throw new ArgumentNullException("response"); } if (!response.Success) { throw new ProtocolException(response.Error); } if (response.Lines.Count != 2) { throw new ProtocolException("Response to \"download\" or \"patch\" command wasn't 2 lines long."); } var successLine = new KeyAndValue(response.Lines[0]); if (successLine.Key != "Success") { throw new ProtocolException("Key of Success line isn't right."); } this.Success = bool.Parse(successLine.Value); var bytesLine = new KeyAndValue(response.Lines[1]); if (bytesLine.Key != "Bytes") { throw new ProtocolException("Key of Bytes line isn't right."); } this.Bytes = Int32.Parse(bytesLine.Value); }
public void AddContent() { if (HasError()) { MessageBox("ERROR", string.Format("<color=#0000a0ff>{0}</color> can not be emply!", GetFirstError())); return; } currentNode.contents.Add(currentContent); currentContent = new KeyAndValue(); contentNameField.text = string.Empty; contentField.text = string.Empty; }
public void AddAttribute() { if (HasError(true)) { MessageBox("ERROR", string.Format("<color=#0000a0ff>{0}</color> can not be emply!", GetFirstError(true))); return; } currentNode.attributes.Add(currentAttribute); currentAttribute = new KeyAndValue(); attributeNameField.text = string.Empty; attributeValueField.text = string.Empty; }
// Use this for initialization void Start() { // Time.timeScale = 0.2f; camera = GetComponent <Camera>(); kav = new KeyAndValue(); if (platform.Equals("ios")) { scale = 1500; minPinchDistance = 10.0f; } else { scale = 1500; minPinchDistance = 10.0f; } minHigh = transform.position.y / 5; maxHigh = 70; print("start"); StartCoroutine(StartGPS()); for (int i = 1; i <= 13; i++) { GameObject text; if (i < 10) { text = GameObject.Find("Text0" + i); } else { text = GameObject.Find("Text" + i); } texts[i - 1] = text; } print("length is " + texts.Length); ball = GameObject.Find("ball"); controller = ball.GetComponent <CharacterController>(); }
public RuntimeIdResponse(SendReceiveResult response) { if (response == null) { throw new ArgumentNullException("response"); } if (!response.Success) { throw new ProtocolException(response.Error); } if (response.Lines.Count != 1) { throw new ProtocolException("Response to \"runtime-id\" command wasn't 1 lines long."); } var runningLine = new KeyAndValue(response.Lines[0]); if (runningLine.Key != "RuntimeId") { throw new ProtocolException("Key of RuntimeId line isn't right."); } this.RuntimeId = Guid.Parse(runningLine.Value); }
public CommandResponse(SendReceiveResult response) { if (response == null) { throw new ArgumentNullException("response"); } if (!response.Success) { throw new ProtocolException(response.Error); } if (response.Lines.Count != 1) { throw new ProtocolException("Response to command wasn't 1 lines long."); } var successLine = new KeyAndValue(response.Lines[0]); if (successLine.Key != "Success") { throw new ProtocolException("Key of Success line isn't right."); } this.Success = bool.Parse(successLine.Value); }
internal static Expression GetConvertExpression(Type fromType, Type toType, Expression from) { if (fromType == null || fromType == Types.Void || fromType == Types.DBNull) { return(GetDefaultValueExpression(toType)); } if (toType == fromType) { if (from.Type == toType) { return(from); } return(Expression.Convert(from, toType)); } if (toType.IsAssignableFrom(fromType) && !toType.IsNullable() && !toType.IsEnum) { if (from.Type == toType) { return(from); } return(Expression.Convert(from, toType)); } if (toType == Types.String) { if (fromType == typeof(char[])) { return(Expression.New(Types.String.GetConstructor(new Type[] { typeof(char[]) }), from)); } return(Expression.Call(from, Types.Object.GetMethod("ToString"))); } var type = toType.IsNullable() ? Nullable.GetUnderlyingType(toType) : toType; if (fromType.IsNullable()) { fromType = Nullable.GetUnderlyingType(fromType); from = Expression.Condition(Expression.Property(from, "HasValue"), Expression.Property(from, "Value"), GetDefaultValueExpression(fromType)); } if (fromType == Types.DBNull) { return(GetDefaultValueExpression(toType)); } if (fromType.IsEnum) { from = Expression.Convert(from, Enum.GetUnderlyingType(fromType)); fromType = Enum.GetUnderlyingType(fromType); } Expression to = from; if (type.IsEnum) { if (fromType == Types.String) { to = Expression.Convert( Expression.Call( null , enumParseMethod , Expression.Constant(type) , to , Expression.Constant(true)) , type); } else { var typeCode = Type.GetTypeCode(fromType); if (typeCode == TypeCode.Single || typeCode == TypeCode.Decimal || typeCode == TypeCode.Double) { from = Expression.Call( null , Mappers[new KeyAndValue(fromType, Types.Int64)] , from); fromType = Types.Int64; typeCode = TypeCode.Int64; } switch (typeCode) { case TypeCode.Byte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.SByte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: to = Expression.Convert( Expression.Call( null , typeof(Enum).GetMethod("ToObject", new Type[] { Types.Type, fromType }) , Expression.Constant(type) , from) , type); break; default: throw new InvalidCastException(string.Format("from '{0}' -> '{1}'", fromType, toType)); } return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } } if (type == typeof(TimeSpan)) { if (fromType == typeof(string)) { to = Expression.Call(null, Types.TimeSpan.GetMethod("Parse", new Type[] { Types.String }), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (fromType == typeof(DateTime)) { to = Expression.Property(Expression.Call( null , Types.DateTime.GetMethod("Parse", new Type[] { Types.String, typeof(CultureInfo) }) , Expression.Call(from, Types.DateTime.GetMethod("ToString")) , Expression.Property(null, typeof(CultureInfo).GetProperty("InvariantCulture"))) , Types.DateTime.GetProperty("TimeOfDay")); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (fromType == typeof(DateTimeOffset)) { to = Expression.Property( Expression.Call( null , typeof(DateTimeOffset).GetMethod("Parse", new Type[] { Types.String }) , Expression.Call(from, typeof(DateTimeOffset).GetMethod("ToString")) , Expression.Property(null, typeof(CultureInfo).GetProperty("InvariantCulture"))) , typeof(DateTimeOffset).GetProperty("TimeOfDay")); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } to = Expression.New( Types.TimeSpan.GetConstructor(new Type[] { Types.Int64 }) , Expression.Convert( Expression.Call( null , typeof(Convert).GetMethod("ChangeType", new Type[] { Types.Object, Types.Type, typeof(CultureInfo) }) , from , Expression.Constant(typeof(long)) , Expression.Property(null, typeof(CultureInfo).GetProperty("InvariantCulture"))) , Types.Int64) ); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (type == Types.ByteArray) { var fromTypeCode = Type.GetTypeCode(fromType); switch (fromTypeCode) { case TypeCode.Boolean: case TypeCode.Char: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: return(Expression.Call(typeof(BitConverter).GetMethod("GetBytes", new Type[] { fromType }), from)); case (TypeCode)21: //Guid return(Expression.Call(from, fromType.GetMethod("ToByteArray"))); //case TypeCode.DateTime: // break;TODO: } } if (fromType == typeof(TimeSpan)) { if (type == typeof(DateTime)) { to = Expression.Add(GetDefaultValueExpression(Types.DateTime), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (type == typeof(DateTimeOffset)) { to = Expression.Add(GetDefaultValueExpression(typeof(DateTimeOffset)), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } to = Expression.Convert(Expression.Call( null , typeof(Convert).GetMethod("ChangeType", new Type[] { Types.Object, Types.Type, typeof(CultureInfo) }) , Expression.Property(from, Types.TimeSpan.GetProperty("Ticks")) , Expression.Constant(type) , Expression.Property(null, typeof(CultureInfo).GetProperty("InvariantCulture"))) , type); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (type == typeof(DateTime) && fromType == typeof(DateTimeOffset)) { to = Expression.Property(from, typeof(DateTimeOffset).GetProperty("DateTime")); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (type == typeof(DateTimeOffset) && fromType == typeof(DateTime)) { to = Expression.New(typeof(DateTimeOffset).GetConstructor(new Type[] { Types.DateTime }), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (fromType == Types.String) { if (type == Types.Char) { var itemMethod = Types.String.GetProperty("Chars"); to = Expression.Property(from, itemMethod, Expression.Constant(0)); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } if (type == Types.Guid) { to = Expression.New(Types.Guid.GetConstructor(new Type[] { Types.String }), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } } else if (fromType == typeof(char[]) && type == Types.String) { to = Expression.New(Types.String.GetConstructor(new Type[] { typeof(char[]) }), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } else if (fromType == Types.ByteArray) { if (type == Types.Guid) { to = Expression.New(Types.Guid.GetConstructor(new Type[] { Types.ByteArray }), from); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } else if (toType.FullName == "System.Drawing.Image") { to = Expression.Call( null , toType.GetMethod("FromStream", new Type[] { typeof(Stream) }) , Expression.New(typeof(MemoryStream).GetConstructor(new Type[] { Types.ByteArray }), from)); return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); } var toTypeCode = Type.GetTypeCode(type); switch (toTypeCode) { case TypeCode.Boolean: case TypeCode.Char: //case TypeCode.Decimal: case TypeCode.Double: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: case TypeCode.Single: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: { to = Expression.Call( null , typeof(BitConverter).GetMethod("To" + toTypeCode.ToString()) , from , Expression.Constant(0, Types.Int32)); return(toType.IsNullable() ? Expression.Convert(to, toType) : to); } case TypeCode.String: { return(Expression.Call( Expression.Property(null, typeof(Encoding).GetProperty("Default")) , typeof(Encoding).GetMethod("GetString", new Type[] { typeof(byte[]) }) , from)); } case TypeCode.DateTime: { return(GetConvertExpression(Types.String, toType, Expression.Call( Expression.Property(null, typeof(Encoding).GetProperty("Default")) , typeof(Encoding).GetMethod("GetString", new Type[] { typeof(byte[]) }) , from))); } case TypeCode.Object: { var BinaryFormatter = Expression.New(typeof(BinaryFormatter)); var stream = Expression.New(typeof(MemoryStream).GetConstructor(new Type[] { Types.ByteArray }), from); from = Expression.Call(BinaryFormatter, typeof(BinaryFormatter).GetMethod("Deserialize", new Type[] { typeof(Stream) }), stream); Expression.Call(stream, typeof(IDisposable).GetMethod("Dispose")); return(GetConvertExpression(from.Type, toType, from)); } } } else if (fromType == Types.Guid) { if (toType == Types.ByteArray) { return(Expression.Call( from , fromType.GetMethod("ToByteArray"))); } } if (type != fromType) { var key = new KeyAndValue(fromType, type); if (ExplictMappers.Contains(key)) { to = Expression.Convert(from, type); } else if (Mappers.ContainsKey(key)) { to = Expression.Call( null , Mappers[key] , from); } else if (fromType.Namespace + "." + fromType.Name == "MySql.Data.Types.MySqlDateTime" && type == Types.DateTime) { to = Expression.Call( null , fromType.GetMethod("op_Explicit", new Type[] { fromType }) , Expression.Convert(from, fromType)); } else if (type == Types.ByteArray) { var binaryFormatter = Expression.New(typeof(BinaryFormatter)); var stream = Expression.New(typeof(MemoryStream)); Expression.Call(binaryFormatter, typeof(BinaryFormatter).GetMethod("Serialize", new Type[] { typeof(Stream), Types.Object }), stream, from); to = Expression.Call(stream, typeof(MemoryStream).GetMethod("ToArray")); Expression.Call(stream, typeof(IDisposable).GetMethod("Dispose")); return(to); } } return(toType.IsNullable() ? Expression.Convert(to, toType)// Expression.New(toType.GetConstructor(new Type[] { type }), to) : to); }
public void ReadFile(string fileDir) { //ADDDEBUG when point key and var comes before point section. //ADDDEBUG when two sections have the same key. //ADDDEBUG when there is two "=" in one line. //ADDDEBUG when there is an unneven amount of control characters. //ADDDEBUG invalid chars in rowValues, keys. //ADDDEBUG if things like this appears "key=423 helloasd". //ADDTOGAME escape characters. FileStream fs = new FileStream(fileDir, FileMode.Open, FileAccess.Read); TextReader tr = new StreamReader(fs); string currentSection = ""; Dictionary <string, string> currentDict = new Dictionary <string, string>(); KeyAndValue keyAndVal; string line; int indexOfFirstQuote; int indexOfSecondQuote; while ((line = tr.ReadLine()) != null) { keyAndVal = new KeyAndValue(); for (int c = 0; c < line.Length; c++) { if (line[c] == '[') { if (currentSection != "") { _Data.Add(currentSection, currentDict); } currentSection = line.Substring(line.IndexOf('[') + 1, line.IndexOf(']') - line.IndexOf('[') - 1); currentDict = new Dictionary <string, string>(); } if (line[c] == ';') { break; } if (line[c] == '=') { keyAndVal.Key = line.Substring(0, c); line = line.Substring(c + 1); if (line.Contains('"')) { if (line.Contains(';')) { if (line.IndexOf(';') > line.IndexOf('"')) { indexOfFirstQuote = line.IndexOf('"'); indexOfSecondQuote = line.IndexOf('"', indexOfFirstQuote + 1); keyAndVal.Value = AddEscapeSequences(line.Substring(indexOfFirstQuote + 1, indexOfSecondQuote - indexOfFirstQuote - 1)); } else { keyAndVal.Value = AddEscapeSequences(line.Substring(0, line.IndexOf(';')).Split(new char[] { ' ' })[0]); } } else { indexOfFirstQuote = line.IndexOf('"'); indexOfSecondQuote = line.IndexOf('"', indexOfFirstQuote + 1); keyAndVal.Value = AddEscapeSequences(line.Substring(indexOfFirstQuote + 1, indexOfSecondQuote - indexOfFirstQuote - 1)); } } else { keyAndVal.Value = AddEscapeSequences(line.Split(new char[] { ' ' })[0]); } currentDict.Add(keyAndVal.Key, keyAndVal.Value); } } } if (currentSection != "") { _Data.Add(currentSection, currentDict); } }
// Use this for initialization void Start() { // Time.timeScale = 0.2f; camera = GetComponent<Camera>(); kav = new KeyAndValue (); if (platform.Equals ("ios")) { scale = 1500; minPinchDistance = 10.0f; } else { scale = 1500; minPinchDistance = 10.0f; } minHigh = transform.position.y / 5; maxHigh = 70; print ("start"); StartCoroutine(StartGPS()); for (int i=1; i<=13; i++) { GameObject text; if(i<10){ text = GameObject.Find ("Text0"+i); }else{ text = GameObject.Find ("Text"+i); } texts[i-1] = text; } print ("length is "+texts.Length); ball = GameObject.Find ("ball"); controller = ball.GetComponent<CharacterController>(); }
// Use this for initialization void Start() { kav = new KeyAndValue (); camera = GetComponent<Camera>(); if (platform.Equals ("ios")) { minPinchDistance = 10.0f; } else { minPinchDistance = 10.0f; } minHigh = transform.position.y / 10; maxHigh = 40; fitHigh = 20; StartCoroutine(StartGPS()); ball = GameObject.Find ("ball"); controller = ball.GetComponent<CharacterController>(); //test code: zywx_setPoiDateSource (""); // LonLatPoint lonlatpoint = new LonLatPoint(116.270928f,39.986163f); // PixelPoint point = gp.lonlatToPixel (lonlatpoint,17); // print("coor is "+(-(float)point.pointX/100f)+" "+(float)point.pointY/100f); for (int i = 1; i<=24; i++) { Texture2D image_big = (Texture2D)Resources.Load ("map_"+i+"@3x"); GUIContent content_big = new GUIContent (); content_big.image = image_big; texture_big.Add(content_big); Texture2D image_small = (Texture2D)Resources.Load ("map_icon_"+i); GUIContent content_small = new GUIContent (); content_small.image = image_small; texture_small.Add(content_small); } texture_door = (Texture2D)Resources.Load ("door"); }
public InformationResponse(SendReceiveResult response) { if (response == null) { throw new ArgumentNullException("response"); } if (!response.Success) { throw new ProtocolException(response.Error); } if (response.Lines.Count != 7) { throw new ProtocolException("Response to \"information\" command wasn't 7 lines long."); } this.RuntimeName = response.Lines[0]; var protocolVersionLine = new KeyAndValue(response.Lines[1]); if (protocolVersionLine.Key != "Protocol Version") { throw new ProtocolException("Key of Protocol Version line isn't right."); } this.ProtocolVersion = protocolVersionLine.Value; var booleansLine = new KeyAndValue(response.Lines[2]); if (booleansLine.Key != "Booleans") { throw new ProtocolException("Key of Booleans line isn't right."); } this.Booleans = Int32.Parse(booleansLine.Value); var numericsLine = new KeyAndValue(response.Lines[3]); if (numericsLine.Key != "Numerics") { throw new ProtocolException("Key of Numerics line isn't right."); } this.Numerics = Int32.Parse(numericsLine.Value); var stringsLine = new KeyAndValue(response.Lines[4]); if (stringsLine.Key != "Strings") { throw new ProtocolException("Key of Strings line isn't right."); } this.Strings = Int32.Parse(stringsLine.Value); var maxProgramSizeLine = new KeyAndValue(response.Lines[5]); if (maxProgramSizeLine.Key != "Max Program Size") { throw new ProtocolException("Key of Max Program Size line isn't right."); } this.MaxProgramSize = Int32.Parse(maxProgramSizeLine.Value); var currentProgramSizeLine = new KeyAndValue(response.Lines[6]); if (currentProgramSizeLine.Key != "Current Program Size") { throw new ProtocolException("Key of Current Program Size line isn't right."); } this.CurrentProgramSize = Int32.Parse(currentProgramSizeLine.Value); }