private PlistDictionary LoadDictionaryContents(XmlReader reader, PlistDictionary dict)
 {
     Debug.Assert(reader.NodeType == XmlNodeType.Element && reader.LocalName == "key");
     while (!reader.EOF && reader.NodeType == XmlNodeType.Element)
     {
         string key = reader.ReadElementString();
         while (reader.NodeType != XmlNodeType.Element && reader.Read())
             if (reader.NodeType == XmlNodeType.EndElement)
                 throw new Exception(String.Format("No value found for key {0}", key));
         PlistObjectBase result = LoadFromNode(reader);
         if (result != null)
             dict.Add(key, result);
         reader.ReadToNextSibling("key");
     }
     return dict;
 }
예제 #2
0
        private static object ReadObjectBinary(byte[] bytes, ref Trailer trailer, ulong objectRef)
        {
            ulong objectAt = ReadOffset(bytes, trailer, objectRef);
            byte  headbyte = bytes[objectAt];

            int type = headbyte & 0xF0;
            int nnnn = headbyte & 0x0F;

            switch (type)
            {
            case TypeBitsBoolOrNil:      /* 0000 null, false, true*/
                switch (nnnn)
                {
                case 0x0:
                    throw new PlistException("unsupported null");

                case FalseBit:
                    return(false);

                case TrueBit:
                    return(true);

                default:
                    throw new PlistException("undefined value");
                }

            case TypeBitsInteger:      /* 0001 integer */
            {
                ulong nextAt;
                long  integer = ReadInteger(bytes, objectAt, out nextAt);
                if (integer < int.MinValue || int.MaxValue < integer)
                {
                    throw new PlistException("int overflow, we use int for simplity.");
                }
                return((int)integer);
            }

            case TypeBitsReal:      /* 0010 real */
            {
                int nnnn_bytes = TwoPowNNNN(nnnn);
                var real       = BigEndianReader.ReadNBytesReal(bytes, nnnn_bytes, objectAt + 1);
                return(real);
            }

            case TypeBitsDate:      /* 0011 date */
            {
                double   elapsed = BigEndianReader.ReadNBytesReal(bytes, 8, objectAt + 1);
                var      span    = TimeSpan.FromSeconds(elapsed);
                DateTime date    = BaseTime.Add(span).ToLocalTime();
                return(date);
            }

            case TypeBitsBinaryData:      /* 0100 data */
            {
                ulong dataLength;
                ulong dataAt;
                IntNNNNorNextInt(bytes, objectAt, nnnn, out dataLength, out dataAt);

                byte[] data = new byte[dataLength];
                Array.Copy(bytes, (long)dataAt, data, 0, (long)dataLength);
                return(data);
            }

            case TypeBitsASCIIString:      /* 0101 ascii string */
            {
                ulong asciiCount;
                ulong asciiAt;
                IntNNNNorNextInt(bytes, objectAt, nnnn, out asciiCount, out asciiAt);
                string ascii;
                if (asciiAt + asciiCount < int.MaxValue)
                {
                    ascii = System.Text.Encoding.ASCII.GetString(bytes, (int)asciiAt, (int)asciiCount);
                }
                else
                {
                    byte[] asciiBytes = new byte[asciiCount];
                    Array.Copy(bytes, (long)asciiAt, asciiBytes, 0, (long)asciiCount);
                    ascii = System.Text.Encoding.ASCII.GetString(asciiBytes, 0, (int)asciiBytes.Length);
                }
                return(ascii);
            }

            case TypeBitsUTF16String:      /* 0110 utf16 string */
            {
                ulong utf16Count;
                ulong utf16At;
                IntNNNNorNextInt(bytes, objectAt, nnnn, out utf16Count, out utf16At);
                ulong utf16byteLength = utf16Count * 2;

                string utf16;
                if (utf16At + utf16byteLength < int.MaxValue)
                {
                    utf16 = UTF16BE.GetString(bytes, (int)utf16At, (int)utf16byteLength);
                }
                else
                {
                    byte[] utf16Bytes = new byte[utf16byteLength];
                    Array.Copy(bytes, (long)utf16At, utf16Bytes, 0, (long)utf16byteLength);
                    utf16 = UTF16BE.GetString(utf16Bytes, 0, utf16Bytes.Length);
                }
                return(utf16);
            }

            case TypeBitsArray:      /* 1010 array */
            {
                ulong count;
                ulong arrayAt;
                IntNNNNorNextInt(bytes, objectAt, nnnn, out count, out arrayAt);

                int       capacity    = (int)(Math.Min(count, int.MaxValue));
                PlistList objectArray = new PlistList(capacity);
                for (ulong i = 0; i < count; ++i)
                {
                    ulong arrayObjectRef = BigEndianReader.ReadNBytesUnsignedInteger(bytes, trailer.ObjectRefSize, arrayAt + trailer.ObjectRefSize * i);
                    objectArray.Add(ReadObjectBinary(bytes, ref trailer, arrayObjectRef));
                }
                return(objectArray);
            }

            case TypeBitsDictionary:     /* 1101 dictionary */
            {
                ulong count;
                ulong dictionaryAt;
                IntNNNNorNextInt(bytes, objectAt, nnnn, out count, out dictionaryAt);
                int             capacity         = (int)(Math.Min(count, int.MaxValue));
                PlistDictionary objectDictionary = new PlistDictionary(capacity);

                /* key, key, key, value, value, value... */
                ulong keyBytes = trailer.ObjectRefSize * count;
                for (ulong i = 0; i < count; ++i)
                {
                    ulong  keyObjectRef   = BigEndianReader.ReadNBytesUnsignedInteger(bytes, trailer.ObjectRefSize, dictionaryAt + trailer.ObjectRefSize * i);
                    ulong  valueObjectRef = BigEndianReader.ReadNBytesUnsignedInteger(bytes, trailer.ObjectRefSize, dictionaryAt + keyBytes + trailer.ObjectRefSize * i);
                    string keyObject      = ReadObjectBinary(bytes, ref trailer, keyObjectRef) as string;
                    object valueObject    = ReadObjectBinary(bytes, ref trailer, valueObjectRef);
                    objectDictionary.Add(keyObject, valueObject);
                }
                return(objectDictionary);
            }

            default:
                throw new PlistException("undefined data type");
            }
        }
        //--------------------------------------------------------------------------------------------------------------
        //--------------------------------------------------------------------------------------------------------------
        public LHScene(PlistDictionary dict, string plistLevelFile, CCWindow mainWindow) : base(mainWindow)
        {
            _designResolutionSize = CCSize.Parse(dict ["designResolution"].AsString);

            foreach (PlistDictionary devInf in dict["devices"].AsArray)
            {
                devices.Add(new LHDevice(devInf));
            }
            currentDev = LHDevice.currentDeviceFromArray(devices, this);
            CCSize sceneSize = currentDev.getSize();
            float  ratio     = currentDev.getRatio();

            sceneSize.Width  = sceneSize.Width / ratio;
            sceneSize.Height = sceneSize.Height / ratio;

            var aspect = dict ["aspect"].AsInt;

            if (aspect == 0)           //exact fit
            {
                sceneSize = _designResolutionSize;
            }
            else if (aspect == 1)           //no borders
            {
            }
            else if (aspect == 2)           //show all
            {
                _designOffset.X = (sceneSize.Width - _designResolutionSize.Width) * 0.5f;
                _designOffset.Y = (sceneSize.Height - _designResolutionSize.Height) * 0.5f;
            }


            //loadingInProgress = true;
            Debug.WriteLine("plistLevelFile:|" + plistLevelFile + "|");

            this.relativePath = LHUtils.folderFromPath(plistLevelFile);
//			this.relativePath = Path.GetDirectoryName (plistLevelFile);

            Debug.WriteLine("SCENE REL |" + this.relativePath + "|");
//			Console.WriteLine ("SCENE REL |" + this.relativePath + "|");

            if (this.relativePath == null)
            {
                this.relativePath = "";
            }

//			loadingInProgress = true;

//			[[CCDirector sharedDirector] setContentScaleFactor:ratio];
//			#if __CC_PLATFORM_IOS
//			[[CCFileUtils sharedFileUtils] setiPhoneContentScaleFactor:curDev.ratio];
//			#endif

//			[self setName:relativePath];

            _nodeProtocolImp.loadGenericInfoFromDictionary(dict, this);

//			self.contentSize= CGSizeMake(curDev.size.width/curDev.ratio, curDev.size.height/curDev.ratio);
//			self.position   = CGPointZero;

            PlistDictionary tracedFixInfo = dict["tracedFixtures"].AsDictionary;

            if (tracedFixInfo != null)
            {
                tracedFixtures = new PlistDictionary();
                foreach (var pair in tracedFixInfo)
                {
                    string     fixUUID = pair.Key;
                    PlistArray fixInfo = pair.Value.AsArray;
                    if (null != fixInfo)
                    {
                        tracedFixtures.Add(fixUUID, fixInfo);
                    }
                }
            }

//			supportedDevices = [[NSArray alloc] initWithArray:devices];

//			[self loadBackgroundColorFromDictionary:dict];
//			[self loadGameWorldInfoFromDictionary:dict];



            LHNodeProtocolImp.loadChildrenForNode(this, dict);

//			[self loadGlobalGravityFromDictionary:dict];
//			[self loadPhysicsBoundariesFromDictionary:dict];

//			[self setUserInteractionEnabled:YES];

//			#if __CC_PLATFORM_IOS
//			pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
//			[[[CCDirector sharedDirector] view] addGestureRecognizer:pinchRecognizer];
//			#endif



//			#if LH_USE_BOX2D
//			_box2dCollision = [[LHBox2dCollisionHandling alloc] initWithScene:self];
//			#else//cocos2d

//			#endif
//			[self performLateLoading];

//			loadingInProgress = false;

            Debug.WriteLine("SCENE has children count " + this.ChildrenCount);
        }