public static object[] Parse(RecordInputStream in1, int nValues)
 {
     object[] result = new Object[nValues];
     for (int i = 0; i < result.Length; i++)
     {
         result.SetValue(ReadAConstantValue(in1),i);
     }
     return result;
 }
Exemple #2
0
 public override object[,] GetValues()
 {
     values_ = new Object[Rows, Cols];
     var remainingcubes = new string[Rows * Cols];
     string[] cubes;
     if (Big) cubes = bigCubes.Clone() as string[];
     else cubes = regularCubes.Clone() as string[];
     string tmpallvalues = "";
     //loop through rows and columns and insert a random number in each cell)
     for (int i = 0; i < Rows; i++) {
         for (int j = 0; j < Cols; j++) {
             // trying to get a number with a min of 0 and max of 9, skewed to lower numbers but not too many 0s
             // NextDouble gives a number b/w 0 and 1, so this gives 1 number in (0.3,3.3) and one number in
             // (0,3). Taking their product gives a number in (0,9.9) and flooring gives an int in [0,9]
             //NQR
             //randomly get an integer in [0,6] to pick which letter from the cube to choose
             int letterfromcube = rand.Next(6);
             //get a random integer in [0,remaining unused cubes) to pick the letter from
             int numRemainingCubes = Rows * Cols - Cols * i - j;
             int whichcube = rand.Next(numRemainingCubes);
             //get the cube that corresponds the one chosen above
             if (cubes != null){
                 var cubestring = cubes[whichcube];
                 //get the chosen letter from the cube specified
                 var value = cubestring.Substring(letterfromcube, 1);
                 //Q really means Qu
                 if (value == "Q") value = "Qu";
                 tmpallvalues += value;
                 SetLocation_(value.ToUpper(), i, j);
                 if (numRemainingCubes > 1) remainingcubes = new string[numRemainingCubes - 1];
                 int pos = 0;
                 //for debugging/testing algorithm write out the cube numbers and letter numbers used
                 /*using (StreamWriter file = new StreamWriter(@"d:\tmp.txt", true)){
                 file.WriteLine(letterfromcube);
                 if (numRemainingCubes == 1) file.WriteLine("end of game\n\n");
             }
             using (StreamWriter file = new StreamWriter(@"d:\whichcube.txt", true)){
                 file.WriteLine(whichcube);
                 if (numRemainingCubes == 1) file.WriteLine("end of game\n\n");
             }  */
                 //Make the cubes array just contain the unused cubes (effectively remove the cube that has just been used)
                 for (int k = 0; k < cubes.Length; k++) {
                     if (k != whichcube) {
                         remainingcubes.SetValue(cubes[k], pos);
                         pos++;
                     }
                 }
                 cubes = (string[])remainingcubes.Clone();
                 values_.SetValue(value, i, j);
             }
         }
     }
     allvalues_ = (string)tmpallvalues.Clone();
     return values_;
 }
Exemple #3
0
        public override object[,] GetValues()
        {
            string tmpallvalues = "";
            values_ = new Object[Rows, Cols];
            locations_ = new Dictionary<string, IList<int[]>>();
            //loop through rows and columns and insert a random number in each cell
            for (int i = 0; i < Rows; i++) {
                for (int j = 0; j < Cols; j++) {

                    int value = getSkewedRandNum();
                    values_.SetValue(value, i, j);
                    SetLocation_(value.ToString(), i, j);
                    //store the value in a list of all values
                    tmpallvalues += value;
                }
            }
            allvalues_ = (string)tmpallvalues.Clone();
            return values_;
        }
        protected override void Communication(System.Net.Sockets.Socket socket)
        {
            Player player = null;

            try
            {
                base.Communication(socket);
                if (Version.Major > 0)
                {
                    Version         clientversion = new Version(1, 0);
                    System.DateTime until         = System.DateTime.Now.AddMilliseconds(Game.UpdateInterval * 3);
                    while (Connected)
                    {
                        if (Socket.Available > 0)
                        {
                            until = System.DateTime.Now.AddMilliseconds(Game.UpdateInterval * 3);
                            switch (ReceiveCommand())
                            {
                            case Commands.NewNetworkPlayer:
                                byte major = ReceiveByte();
                                byte minor = ReceiveByte();
                                clientversion = new Version(major, minor);
                                player        = (Player)Game.ServerCommunication(clientversion, this, true, player);
                                if (OnPlayerConnectedEvent != null)
                                {
                                    System.Object[] objs = new System.Object[1];
                                    objs.SetValue(player, 0);
                                    Synchronize.Invoke(OnPlayerConnectedEvent, objs);
                                }
                                if (Game.UpdatePlayerInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                                }
                                break;

                            case Commands.UpdateGameInformations:
                                player = (Player)Game.ServerCommunication(clientversion, this, false, player);
                                if (Game.UpdatePlayerInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                                }
                                break;

                            case Commands.DisconnectPlayer:
                                Socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                                Socket.Disconnect(false);
                                Socket.Close();
                                break;

                            case Commands.BuyStocks:
                                Game.ClientBuyingStocks(clientversion, this, ReceiveString(), player);
                                if (Game.UpdateStockInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdateStockInformationsEvent, null);
                                }
                                if (Game.UpdatePlayerInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                                }
                                break;

                            case Commands.SellStocks:
                                Game.ClientSellingStocks(clientversion, this, ReceiveString(), player);
                                if (Game.UpdateStockInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdateStockInformationsEvent, null);
                                }
                                if (Game.UpdatePlayerInformationsEvent != null)
                                {
                                    Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                                }
                                break;
                            }
                        }
                        else if (System.DateTime.Now > until)
                        {
                            Socket.Shutdown(System.Net.Sockets.SocketShutdown.Both);
                            Socket.Disconnect(false);
                            Socket.Close();
                        }
                        System.Threading.Thread.Sleep(250);
                    }
                }
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                switch (ex.SocketErrorCode)
                {
                case System.Net.Sockets.SocketError.ConnectionAborted:
                    // Don't throw an excetion, the connection has just aborted.
                    break;

                default:
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (OnPlayerDisconnectedEvent != null)
                {
                    System.Object[] objs = new System.Object[1];
                    objs.SetValue(player, 0);
                    Synchronize.Invoke(OnPlayerDisconnectedEvent, objs);
                }
                Game.RemovePlayer(player);
                if (Game.UpdatePlayerInformationsEvent != null)
                {
                    Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                }
            }
        }
	public void TestSetValue4() {
		{
			int[] c1 = { 1, 2, 3 };
			long[] c2 = new long [3];

			for (int i = 0; i < c1.Length; i++)
				c2.SetValue (c1 [i], i);

			for (int i = 0; i < c1.Length; i++) {
				Assert ("#M81(" + i + ")", c1[i] == c2[i]);
				AssertEquals ("#M82(" + i + ")", typeof (long), c2[i].GetType ());
			}
		}
		{
			long[] c1 = { 1, 2, 3 };
			int[] c2 = new int [3];
			bool errorThrown = false;
			try {
				c2.SetValue (c1 [0], 0);
			} catch (ArgumentException) {
				errorThrown = true;
			}
			Assert("#M83", errorThrown);
		}
		{
			int[] c1 = { 1, 2, 3 };
			Object[] c2 = new Object [3];

			for (int i = 0; i < c1.Length; i++)
				c2.SetValue (c1 [i], i);

			for (int i = 0; i < c1.Length; i++)
				AssertEquals ("#M84(" + i + ")", c1[i], Convert.ToInt32 (c2[i]));
		}
		{
			Object[] c1 = new Object [3];
			Object[] c2 = new Object [3];
			c1[0] = new Object ();

			for (int i = 0; i < c1.Length; i++)
				c2.SetValue (c1 [i], i);

			for (int i = 0; i < c1.Length; i++)
				AssertEquals ("#M85(" + i + ")", c1[i], c2[i]);
		}
		{
			Object[] c1 = new Object [3];
			string[] c2 = new String [3];
			string test = "hello";
			c1[0] = test;

			c2.SetValue (c1 [0], 0);
			AssertEquals ("#M86", c1[0], c2[0]);
			AssertEquals ("#M87", "hello", c2[0]);
		}
		{
			char[] c1 = { 'a', 'b', 'c' };
			string[] c2 = new string [3];
			try {
				c2.SetValue (c1 [0], 0);
				Fail ("#M88");
			} catch (InvalidCastException) {}
		}
		{
			Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
			long[] c2 = new long [3];
			try {
				c2.SetValue (c1 [0], 0);
				Fail ("#M89");
			} catch (ArgumentException) {}
		}
		{
			Type[] types = {
				typeof (Boolean),
				typeof (Byte),
				typeof (Char),
				typeof (Double),
				typeof (Int16),
				typeof (Int32),
				typeof (Int64),
				typeof (SByte),
				typeof (Single),
				typeof (UInt16),
				typeof (UInt32),
				typeof (UInt64)
			};

			bool v1 = true;
			Byte v2 = 1;
			Char v3 = 'a';
			Double v4 = -1.2;
			Int16 v5 = -32;
			Int32 v6 = -234;
			Int64 v7 = -34523;
			SByte v8 = -1;
			Single v9 = -4.8F;
			UInt16 v10 = 24234;
			UInt32 v11 = 235354;
			UInt64 v12 = 234552;

			Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
			Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
					 "-4.8F", "24234", "235354", "234552" };

			Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };

			int[] arg_ex = {
				0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
				1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
				1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
				1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
				1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
				1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
				1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
				1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
				1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
				1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
				1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
			};

			// SetValue

			for (int i = 0; i < types.Length; i++) {
				for (int j = 0; j < types.Length; j++) {
					Array array = Array.CreateInstance (types [j], 2);

					Object value = vt[j][i];

					bool errorThrown = false;
					try {
						array.SetValue (value, 0);
					} catch (ArgumentException) {
						errorThrown = true;
					}

					int ex_index = (i * types.Length) + j;

					AssertEquals ("#M90(" + types [i] + "," + types [j] + ")",
						      errorThrown, arg_ex [ex_index] == 1);
				}
			}

			for (int i = 0; i < types.Length; i++) {
				String[] array = new String [2];

				Object value = va1 [i];

				bool errorThrown = false;
				try {
					array.SetValue (value, 0);
				} catch (InvalidCastException) {
					errorThrown = true;
				}

				Assert ("#M91(" + types [i] + ")", errorThrown);
			}

			for (int i = 0; i < types.Length; i++) {
				Array array = Array.CreateInstance (types [i], 2);

				Object value = va2 [i];

				bool errorThrown = false;
				try {
					array.SetValue (value, 0);
				} catch (InvalidCastException) {
					errorThrown = true;
				}

				Assert ("#M92(" + types [i] + ")", errorThrown);
			}

			for (int i = 0; i < types.Length; i++) {
				Array array = Array.CreateInstance (types [i], 2);

				Object value = null;

				bool errorThrown = false;
				try {
					array.SetValue (value, 0);
				} catch (InvalidCastException) {
					errorThrown = true;
				}

				Assert ("#M93(" + types [i] + ")", !errorThrown);
			}

			// Copy

			for (int i = 0; i < types.Length; i++) {
				for (int j = 0; j < types.Length; j++) {
					Array source = Array.CreateInstance (types [i], 2);
					Array array = Array.CreateInstance (types [j], 2);

					source.SetValue (vt[j][i], 0);
					source.SetValue (vt[j][i], 1);

					bool errorThrown = false;
					try {
						Array.Copy (source, array, 2);
					} catch (ArrayTypeMismatchException) {
						errorThrown = true;
					}

					int ex_index = (i * types.Length) + j;

					AssertEquals ("#M94(" + types [i] + "," + types [j] + ")",
						      errorThrown, arg_ex [ex_index] == 1);
				}
			}

			for (int i = 0; i < types.Length; i++) {
				Array source = Array.CreateInstance (types [i], 2);
				String[] array = new String [2];

				source.SetValue (va1 [i], 0);
				source.SetValue (va1 [i], 1);

				bool errorThrown = false;
				try {
					Array.Copy (source, array, 2);
				} catch (ArrayTypeMismatchException) {
					errorThrown = true;
				}

				Assert ("#M95(" + types [i] + ")", errorThrown);
			}

			for (int i = 0; i < types.Length; i++) {
				String[] source = new String [2];
				Array array = Array.CreateInstance (types [i], 2);

				source.SetValue (va2 [i], 0);
				source.SetValue (va2 [i], 1);

				bool errorThrown = false;
				try {
					Array.Copy (source, array, 2);
				} catch (ArrayTypeMismatchException) {
					errorThrown = true;
				}

				Assert ("#M96(" + types [i] + ")", errorThrown);
			}
		}
	}
Exemple #6
0
 public static void SetMyProperty(NativeDependencyObject obj, Windows.UI.Xaml.Controls.UIElementCollection value)
 {
     obj.SetValue(MyPropertyProperty, value);
 }
Exemple #7
0
 protected override void Communication(System.Net.Sockets.Socket socket)
 {
     try
     {
         base.Communication(socket);
         if (SelectedServer.Version.Major > 0)
         {
             while (Connected)
             {
                 System.DateTime wait = DateTime.Now.AddMilliseconds(Game.UpdateInterval);
                 while (Connected && System.DateTime.Now <= wait)
                 {
                     System.Threading.Thread.Sleep(250);
                 }
                 if (Connected)
                 {
                     lock (SynchronizeRoot)
                     {
                         SendCommand(Commands.UpdateGameInformations);
                         Game.ClientCommunication(SelectedServer.Version, this, false, null);
                     }
                     if (Game.Players.NewPlayers.Count > 0)
                     {
                         foreach (Player p in Game.Players.NewPlayers)
                         {
                             if (OnPlayerConnectedEvent != null)
                             {
                                 System.Object[] objs = new System.Object[1];
                                 objs.SetValue(p, 0);
                                 Synchronize.Invoke(OnPlayerConnectedEvent, objs);
                             }
                         }
                     }
                     if (Game.Players.DisconnectedPlayers.Count > 0)
                     {
                         foreach (Player p in Game.Players.DisconnectedPlayers)
                         {
                             if (OnPlayerDisconnectedEvent != null)
                             {
                                 System.Object[] objs = new System.Object[1];
                                 objs.SetValue(p, 0);
                                 Synchronize.Invoke(OnPlayerDisconnectedEvent, objs);
                             }
                             Game.RemovePlayer(p);
                         }
                     }
                     if (Game.UpdateStockInformationsEvent != null)
                     {
                         Synchronize.Invoke(Game.UpdateStockInformationsEvent, null);
                     }
                     if (Game.UpdatePlayerInformationsEvent != null)
                     {
                         Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                     }
                 }
             }
         }
     }
     catch (System.Net.Sockets.SocketException ex)
     {
         throw ex;
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemple #8
0
 public static void SetPriority(DependencyObject obj, int value)
 {
     obj.SetValue(PriorityProperty, value);
 }
 void OnExternalInterface(long MovieID, String command, IntPtr ptr, int numArgs, int valueSize)
 {
     int sfValueSize = Marshal.SizeOf(typeof(Value));
     int count = 0;
     
     // Array of types passed to ExternalInterface
     Type[] typeArray;
     System.Object[] args;
     
     // Note that we can't preallocate typeArray and args since we have to pass them to the 
     // GetMethod function below and there is no way to pass the size of the array, so the
     // array can't contain any null values.
     typeArray = new Type[numArgs];
     args = new System.Object[numArgs];
   
     for (int i = 0; i < numArgs; i++)
     {
         // Can't add an integer offset to IntPtr as you would with C/C++ pointer 
         IntPtr data = new IntPtr(ptr.ToInt32() + sfValueSize * i);
         
         // This Value makes a copy of the data and will be garbage collected.
         Value val = GetValueData(data);
         // Value val = (Value)Marshal.PtrToStructure(data, typeof(Value)); // Unsupported on iOS.
         
         if (val.IsString())
         {
             String str = val.GetString();
             typeArray.SetValue(typeof(String), count);
             args.SetValue(str, count);
             count++;
         }
         else if (val.IsNumber())
         {
             double num = val.GetNumber();
             Console.Write(num);
             typeArray.SetValue(typeof(double), count);
             args.SetValue(num, count);
             count++;
         }    
         else if (val.IsBoolean())
         {
             Boolean boolVal = val.GetBool();
             typeArray.SetValue(typeof(Boolean), count);
             args.SetValue(boolVal, count);
             count++;
         }    
         else if (val.IsUInt())
         {
             UInt32 uintVal = val.GetUInt();
             typeArray.SetValue(typeof(UInt32), count);
             args.SetValue(uintVal, count);
             count++;
         }    
         else if (val.IsInt())
         {
             Int32 intVal = val.GetInt();
             typeArray.SetValue(typeof(Int32), count);
             args.SetValue(intVal, count);
             count++;
         }    
         else if (val.IsObject())
         {
             Value newval = val.GetObject();
             typeArray.SetValue(typeof(Value), count);
             args.SetValue(newval, count);
             count++;
         }
     }
     
     // Preallocated memory is destroyed in the destructor
     /* This code can be uncommented if delegates are being used for EI implementation.
     if (ptr != pValues_PreAllocated)
     {
         Console.WriteLine("Destroying Value Array");
         Marshal.FreeCoTaskMem(ptr);
     }
     */
     
     // At this point, count must be = numArgs, since we should be able to determine the type of each Value. If not,
     // there is some problem.
     if (count != numArgs)
     {
         LogMessage("Invalid Type passed in ExternalInterface!");
         return;
     }
     
     for (int j = 0; j < SFMovieList.Count; j++) // Loop through List with for
     {
         Movie movie = SFMovieList[j];
         long mId = movie.GetID();
         if (MovieID != mId) continue;
         
         Type SFMovieClass = movie.GetType();
         UnityEngine.Debug.Log("ExternalInterface arrived");
         // Command passed as an argument is the method we want to call
         MethodInfo methodInfo;
         if (typeArray == null)
         {
             methodInfo = SFMovieClass.GetMethod(command);
         }
         else
         {
             methodInfo = SFMovieClass.GetMethod(command, typeArray);
         }
         
         if( methodInfo != null )
         {
             // LogMessage("Method Implementing " + command + " found, Invoking method");
             methodInfo.Invoke(movie, args); // Call the method
         }
         else
         {
             UnityEngine.Debug.Log("Handler for command: " + command + " not found!");
         }
         return;
     }
 }
 bool callJS(params Object[] args)
 {
     Object[] a = new Object[args.Length + 2];
     a.SetValue(HtmlUploaderID, 0);
     a.SetValue(HtmlFrontentID, 1);
     Array.Copy(args, 0, a, 2, args.Length);
     bool wasError = false;
     try
     {
         ScriptObject lu = (ScriptObject)HtmlPage.Window.GetProperty(HtmlProxyName);
         lu.Invoke("callFEMethod", a);
     }
     catch (Exception)
     {
         wasError = true;
     }
     return wasError;
 }
Exemple #11
0
        private bool loadComboBoxes()
        {
            //Prüfen ob die Datei existiert
            if (System.IO.File.Exists(Application.StartupPath + "//database/base.xml") == false)
            {
                MessageBox.Show(Locales.GetString("error_basefile_not_found"), Locales.GetString("error_messagebox_title"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            try
            {
                foreach (DataTable dataTable in this.baseXMLData.Tables) dataTable.BeginLoadData();
                this.baseXMLData.ReadXml(Application.StartupPath + "//database/base.xml");
                foreach (DataTable dataTable in this.baseXMLData.Tables) dataTable.EndLoadData();

                //Stats-Kategorien in eigene Tabellen speichern
                for (int i = 0; i < 11; i++)
                {
                    bonusData.SetValue(this.baseXMLData.Copy(), i);
                    foreach (DataRow row in bonusData[i].Tables["bonus"].Rows)
                    {
                        if (bonusData[i].Tables.Contains("stats_" + row["category"].ToString()) == false)
                        {
                            bonusData[i].Tables.Add("stats_" + row["category"].ToString());
                            bonusData[i].Tables["stats_" + row["category"].ToString()].Columns.Add("id");
                            bonusData[i].Tables["stats_" + row["category"].ToString()].Columns.Add("name");
                        }

                        object[] values = new Object[2];
                        values.SetValue(row["id"], 0);
                        values.SetValue(row["name"], 1);
                        bonusData[i].Tables["stats_" + row["category"].ToString()].Rows.Add(values);
                    }

                    ComboBox control = (ComboBox)this.Controls.Find("effectCat" + (i+1), true)[0];
                    control.DataSource = bonusData[i].Tables["bonus_category"];
                    control.ValueMember = "name";
                    control.DisplayMember = "name";
                    control.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(Locales.GetString("error_reading_basefile"), Locales.GetString("error_messagebox_title"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
            return true;
        }
Exemple #12
0
        private Boolean TryReadObject(Type type, ref Object value)
        {
            // 不支持基本类型
            if (Type.GetTypeCode(type) != TypeCode.Object) return false;
            // 不支持基类不是Object的特殊类型
            //if (type.BaseType != typeof(Object)) return false;
            if (!typeof(Object).IsAssignableFrom(type)) return false;

            var ims = Host.IgnoreMembers;

            var ms = GetMembers(type).Where(e => !ims.Contains(e.Name)).ToList();
            WriteLog("BinaryRead类{0} 共有成员{1}个", type.Name, ms.Count);

            if (value == null) value = type.CreateInstance();

            // 提前准备名值对
            var dic = ReadPair();
            if (dic.Count == 0) return true;

            Host.Hosts.Push(value);

            // 获取成员
            for (int i = 0; i < ms.Count; i++)
            {
                var member = ms[i];

                var mtype = GetMemberType(member);
                Host.Member = member;
                WriteLog("    {0}.{1}", member.DeclaringType.Name, member.Name);

                var name = member.Name;
                var att = member.GetCustomAttribute<XmlElementAttribute>();
                if (att != null) name = att.ElementName;

                Object v = null;
                if (TryReadPair(dic, name, mtype, ref v)) value.SetValue(member, v);
            }
            Host.Hosts.Pop();

            return true;
        }
Exemple #13
0
        void OnExternalInterface(long MovieID, String command, IntPtr ptr, int numArgs, int valueSize)
        {
            int sfValueSize = Marshal.SizeOf(typeof(Value));
            int count       = 0;

            // Array of types passed to ExternalInterface
            Type[]          typeArray;
            System.Object[] args;

            // Note that we can't preallocate typeArray and args since we have to pass them to the
            // GetMethod function below and there is no way to pass the size of the array, so the
            // array can't contain any null values.
            typeArray = new Type[numArgs];
            args      = new System.Object[numArgs];

            for (int i = 0; i < numArgs; i++)
            {
                // Can't add an integer offset to IntPtr as you would with C/C++ pointer
                IntPtr data = new IntPtr(ptr.ToInt32() + sfValueSize * i);

                // This Value makes a copy of the data and will be garbage collected.
                Value val = GetValueData(data);
                // Value val = (Value)Marshal.PtrToStructure(data, typeof(Value)); // Unsupported on iOS.

                if (val.IsString())
                {
                    String str = val.GetString();
                    typeArray.SetValue(typeof(String), count);
                    args.SetValue(str, count);
                    count++;
                }
                else if (val.IsNumber())
                {
                    double num = val.GetNumber();
                    Console.Write(num);
                    typeArray.SetValue(typeof(double), count);
                    args.SetValue(num, count);
                    count++;
                }
                else if (val.IsBoolean())
                {
                    Boolean boolVal = val.GetBool();
                    typeArray.SetValue(typeof(Boolean), count);
                    args.SetValue(boolVal, count);
                    count++;
                }
                else if (val.IsUInt())
                {
                    UInt32 uintVal = val.GetUInt();
                    typeArray.SetValue(typeof(UInt32), count);
                    args.SetValue(uintVal, count);
                    count++;
                }
                else if (val.IsInt())
                {
                    Int32 intVal = val.GetInt();
                    typeArray.SetValue(typeof(Int32), count);
                    args.SetValue(intVal, count);
                    count++;
                }
                else if (val.IsObject())
                {
                    Value newval = val.GetObject();
                    typeArray.SetValue(typeof(Value), count);
                    args.SetValue(newval, count);
                    count++;
                }
            }

            // Preallocated memory is destroyed in the destructor

            /* This code can be uncommented if delegates are being used for EI implementation.
             * if (ptr != pValues_PreAllocated)
             * {
             *  Console.WriteLine("Destroying Value Array");
             *  Marshal.FreeCoTaskMem(ptr);
             * }
             */

            // At this point, count must be = numArgs, since we should be able to determine the type of each Value. If not,
            // there is some problem.
            if (count != numArgs)
            {
                LogMessage("Invalid Type passed in ExternalInterface!");
                return;
            }

            for (int j = 0; j < SFMovieList.Count; j++) // Loop through List with for
            {
                Movie movie = SFMovieList[j];
                long  mId   = movie.GetID();
                if (MovieID != mId)
                {
                    continue;
                }

                Type SFMovieClass = movie.GetType();
                UnityEngine.Debug.Log("ExternalInterface arrived");
                // Command passed as an argument is the method we want to call
                MethodInfo methodInfo;
                if (typeArray == null)
                {
                    methodInfo = SFMovieClass.GetMethod(command);
                }
                else
                {
                    methodInfo = SFMovieClass.GetMethod(command, typeArray);
                }

                if (methodInfo != null)
                {
                    // LogMessage("Method Implementing " + command + " found, Invoking method");
                    methodInfo.Invoke(movie, args); // Call the method
                }
                else
                {
                    UnityEngine.Debug.Log("Handler for command: " + command + " not found!");
                }
                return;
            }
        }
Exemple #14
0
        /// <summary>尝试读取指定类型对象</summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public override Boolean TryRead(Type type, ref Object value)
        {
            if (type == null)
            {
                if (value == null) return false;
                type = value.GetType();
            }

            // 不支持基本类型
            if (Type.GetTypeCode(type) != TypeCode.Object) return false;
            // 不支持基类不是Object的特殊类型
            //if (type.BaseType != typeof(Object)) return false;
            if (!typeof(Object).IsAssignableFrom(type)) return false;

            var ms = GetMembers(type);
            WriteLog("JsonRead类{0} 共有成员{1}个", type.Name, ms.Count);

            if (value == null) value = type.CreateInstance();

            Host.Hosts.Push(value);

            // 成员序列化访问器
            var ac = value as IMemberAccessor;

            // 获取成员
            for (int i = 0; i < ms.Count; i++)
            {
                var member = ms[i];
                if (IgnoreMembers != null && IgnoreMembers.Contains(member.Name)) continue;

                var mtype = GetMemberType(member);
                Host.Member = member;
                WriteLog("    {0}.{1}", member.DeclaringType.Name, member.Name);

                // 成员访问器优先
                if (ac != null)
                {
                    // 访问器直接写入成员
                    if (ac.Read(Host, member))
                    {
                        // 访问器内部可能直接操作Hosts修改了父级对象,典型应用在于某些类需要根据某个字段值决定采用哪个派生类
                        var obj = Host.Hosts.Peek();
                        if (obj != value)
                        {
                            value = obj;
                            ms = GetMembers(value.GetType());
                            ac = value as IMemberAccessor;
                        }

                        continue;
                    }
                }

                Object v = null;
                if (!Host.TryRead(mtype, ref v))
                {
                    Host.Hosts.Pop();
                    return false;
                }

                value.SetValue(member, v);
            }
            Host.Hosts.Pop();

            return true;
        }