├── .hgignore ├── .travis.yml ├── LICENSE ├── Makefile ├── README ├── README.md ├── as3 └── com │ └── netease │ └── protobuf │ ├── BaseFieldDescriptor.as │ ├── Binary64.as │ ├── CustomOption.as │ ├── FieldDescriptor.as │ ├── IFieldDescriptor.as │ ├── Int64.as │ ├── Message.as │ ├── ReadUtils.as │ ├── RepeatedFieldDescriptor.as │ ├── SimpleWebRPC.as │ ├── TextFormat.as │ ├── UInt64.as │ ├── WireType.as │ ├── WriteUtils.as │ ├── WritingBuffer.as │ ├── ZigZag.as │ ├── fieldDescriptors │ ├── FieldDescriptor_TYPE_BOOL.as │ ├── FieldDescriptor_TYPE_BYTES.as │ ├── FieldDescriptor_TYPE_DOUBLE.as │ ├── FieldDescriptor_TYPE_ENUM.as │ ├── FieldDescriptor_TYPE_FIXED32.as │ ├── FieldDescriptor_TYPE_FIXED64.as │ ├── FieldDescriptor_TYPE_FLOAT.as │ ├── FieldDescriptor_TYPE_INT32.as │ ├── FieldDescriptor_TYPE_INT64.as │ ├── FieldDescriptor_TYPE_MESSAGE.as │ ├── FieldDescriptor_TYPE_SFIXED32.as │ ├── FieldDescriptor_TYPE_SFIXED64.as │ ├── FieldDescriptor_TYPE_SINT32.as │ ├── FieldDescriptor_TYPE_SINT64.as │ ├── FieldDescriptor_TYPE_STRING.as │ ├── FieldDescriptor_TYPE_UINT32.as │ ├── FieldDescriptor_TYPE_UINT64.as │ ├── RepeatedFieldDescriptor_TYPE_BOOL.as │ ├── RepeatedFieldDescriptor_TYPE_BYTES.as │ ├── RepeatedFieldDescriptor_TYPE_DOUBLE.as │ ├── RepeatedFieldDescriptor_TYPE_ENUM.as │ ├── RepeatedFieldDescriptor_TYPE_FIXED32.as │ ├── RepeatedFieldDescriptor_TYPE_FIXED64.as │ ├── RepeatedFieldDescriptor_TYPE_FLOAT.as │ ├── RepeatedFieldDescriptor_TYPE_INT32.as │ ├── RepeatedFieldDescriptor_TYPE_INT64.as │ ├── RepeatedFieldDescriptor_TYPE_MESSAGE.as │ ├── RepeatedFieldDescriptor_TYPE_SFIXED32.as │ ├── RepeatedFieldDescriptor_TYPE_SFIXED64.as │ ├── RepeatedFieldDescriptor_TYPE_SINT32.as │ ├── RepeatedFieldDescriptor_TYPE_SINT64.as │ ├── RepeatedFieldDescriptor_TYPE_STRING.as │ ├── RepeatedFieldDescriptor_TYPE_UINT32.as │ └── RepeatedFieldDescriptor_TYPE_UINT64.as │ ├── stringToByteArray.as │ └── used_by_generated_code.as ├── compiler └── com │ └── netease │ └── protocGenAs3 │ └── Main.java ├── config.mk.sample ├── haxe-test.patch ├── haxelib.xml ├── hx └── com │ └── dongxiguo │ └── protobuf │ └── Run.hx ├── options.proto ├── protoc-gen-as3-test.as3proj └── test ├── com └── netease │ └── protobuf │ └── test │ ├── HaxeTest.hx │ ├── Test.mxml │ └── TestAll.as ├── issue12.proto ├── issue16.proto ├── issue21.proto ├── no_package.proto └── test.proto /.hgignore: -------------------------------------------------------------------------------- 1 | glob:*.class 2 | glob:.* 3 | glob:dist/ 4 | glob:*.tar.gz 5 | glob:descriptor.proto.as3/ 6 | glob:unittest.proto.as3/ 7 | glob:plugin.proto.java/ 8 | glob:options.proto.java/ 9 | glob:config.mk 10 | glob:MANIFEST.MF 11 | glob:*~ 12 | glob:*.swf 13 | glob:*.swc 14 | glob:unittest.bin 15 | glob:obj/ 16 | glob:doc/ 17 | syntax: glob 18 | release.zip 19 | hxclasses/ 20 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: haxe 2 | 3 | cache: 4 | directories: 5 | - $HOME/.m2 6 | 7 | addons: 8 | apt: 9 | packages: 10 | - protobuf-compiler 11 | 12 | before_install: 13 | - wget http://download.macromedia.com/pub/flex/sdk/flex_sdk_3.6.zip 14 | - unzip -d $HOME/flex_sdk flex_sdk_3.6.zip 15 | 16 | install: 17 | - haxelib install haxelib-run 18 | - git clone --branch v2.4.1 --depth 1 https://github.com/google/protobuf.git 19 | - | 20 | echo ' 21 | PROTOBUF_VERSION=2.4.1 22 | PROTOBUF_DIR=protobuf 23 | PROTOC=protoc 24 | ifeq ($(OS), Windows_NT) 25 | BAT=.bat 26 | EXE=.exe 27 | PATH_SEPARATOR=; 28 | else 29 | PATH_SEPARATOR=: 30 | endif 31 | COMPC=$(HOME)/flex_sdk/bin/compc$(BAT) 32 | MXMLC=$(HOME)/flex_sdk/bin/mxmlc$(BAT) 33 | ASDOC=$(HOME)/flex_sdk/bin/asdoc$(BAT) 34 | FDB=$(HOME)/flex_sdk/bin/fdb$(BAT) 35 | MVN=mvn 36 | JAVAC=javac 37 | JAR=jar 38 | HAXE=haxe 39 | ' > config.mk 40 | - mkdir -p protobuf/java/target 41 | - wget --output-document=protobuf/java/target/protobuf-java-2.4.1.jar http://central.maven.org/maven2/com/google/protobuf/protobuf-java/2.4.1/protobuf-java-2.4.1.jar 42 | 43 | script: 44 | - make 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010, NetEase.com,Inc. 2 | Copyright (c) 2011, 杨博(Yang Bo) 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 6 | 7 | Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 8 | Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 9 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 10 | 11 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | # 3 | # Author: Yang Bo (pop.atry@gmail.com) 4 | # 5 | # Use, modification and distribution are subject to the "New BSD License" 6 | # as listed at . 7 | include config.mk 8 | 9 | ifeq ($(OS), Windows_NT) 10 | PROTOC_GEN_AS3=dist\\protoc-gen-as3$(BAT) 11 | else 12 | PROTOC_GEN_AS3=dist/protoc-gen-as3$(BAT) 13 | endif 14 | 15 | ALL=dist/protoc-gen-as3 dist/protoc-gen-as3.bat dist/LICENSE \ 16 | dist/protobuf.swc dist/README dist/options.proto \ 17 | dist/protoc-gen-as3.jar dist/protobuf-java-$(PROTOBUF_VERSION).jar \ 18 | dist/run.n dist/haxelib.xml dist/com dist/google 19 | 20 | all: $(ALL) 21 | 22 | ifndef PROTOC 23 | PROTOC=$(PROTOBUF_DIR)/src/protoc$(EXE) 24 | PROTOCDEPS=$(PROTOC) 25 | $(PROTOC): $(PROTOBUF_DIR)/Makefile 26 | cd $(PROTOBUF_DIR) && $(MAKE) 27 | 28 | $(PROTOBUF_DIR)/Makefile: $(PROTOBUF_DIR)/configure 29 | cd $(PROTOBUF_DIR) && ./configure 30 | 31 | $(PROTOBUF_DIR)/configure: 32 | cd $(PROTOBUF_DIR) && ./autogen.sh 33 | 34 | $(PROTOBUF_DIR)/java/target/protobuf-java-$(PROTOBUF_VERSION).jar: \ 35 | $(PROTOBUF_DIR)/src \ 36 | $(PROTOC) 37 | cd $(PROTOBUF_DIR)/java && $(MVN) package -Dmaven.test.skip=true 38 | endif 39 | 40 | hxclasses: dist/protobuf.swc 41 | $(RM) -r $@ 42 | $(HAXE) --gen-hx-classes -swf-lib dist/protobuf.swc -swf dummy.swf --no-output 43 | 44 | dist/com: hxclasses 45 | $(RM) -r $@ 46 | cp -r hxclasses/com $@ 47 | 48 | dist/google: hxclasses 49 | $(RM) -r $@ 50 | cp -r hxclasses/google $@ 51 | 52 | dist/haxelib.xml: haxelib.xml 53 | install --mode=644 $< $@ 54 | 55 | dist/run.n: hx/com/dongxiguo/protobuf/Run.hx 56 | $(HAXE) -cp hx -lib haxelib-run -main com.dongxiguo.protobuf.Run -neko $@ 57 | 58 | classes/com/netease/protocGenAs3/Main.class: \ 59 | plugin.proto.java/google/protobuf/compiler/Plugin.java \ 60 | options.proto.java/com \ 61 | compiler/com/netease/protocGenAs3/Main.java \ 62 | $(PROTOBUF_DIR)/java/target/protobuf-java-$(PROTOBUF_VERSION).jar \ 63 | | classes 64 | $(JAVAC) -source 1.5 -target 1.5 -encoding UTF-8 -d classes \ 65 | -classpath "$(PROTOBUF_DIR)/java/target/protobuf-java-$(PROTOBUF_VERSION).jar" \ 66 | -sourcepath "plugin.proto.java$(PATH_SEPARATOR)compiler$(PATH_SEPARATOR)options.proto.java" \ 67 | compiler/com/netease/protocGenAs3/Main.java 68 | 69 | plugin.proto.java/google/protobuf/compiler/Plugin.java: \ 70 | $(PROTOCDEPS) | plugin.proto.java 71 | $(PROTOC) \ 72 | "--proto_path=$(PROTOBUF_DIR)/src" --java_out=plugin.proto.java \ 73 | "$(PROTOBUF_DIR)/src/google/protobuf/compiler/plugin.proto" 74 | 75 | dist.tar.gz: $(ALL) 76 | tar -acf dist.tar.gz -C dist . 77 | 78 | release.zip: $(ALL) 79 | cd dist && zip --recurse-paths --filesync ../$@ $(patsubst dist/%,%,$^) 80 | 81 | dist/LICENSE: LICENSE | dist 82 | install --mode=644 $< $@ 83 | 84 | dist/README: README | dist 85 | install --mode=644 $< $@ 86 | 87 | dist/options.proto: options.proto | dist 88 | install --mode=644 $< $@ 89 | 90 | dist/protoc-gen-as3: dist/protoc-gen-as3.jar dist/protobuf-java-$(PROTOBUF_VERSION).jar \ 91 | | dist 92 | (echo '#!/bin/sh';\ 93 | echo 'cd "`dirname "$$0"`" && java -jar protoc-gen-as3.jar') > $@ 94 | chmod +x $@ 95 | 96 | dist/protoc-gen-as3.bat: dist/protoc-gen-as3.jar dist/protobuf-java-$(PROTOBUF_VERSION).jar \ 97 | | dist 98 | (echo '@cd %~dp0';\ 99 | echo '@java -jar protoc-gen-as3.jar') > $@ 100 | chmod +x $@ 101 | 102 | COMMA=, 103 | 104 | dist/protobuf.swc: $(wildcard as3/com/netease/protobuf/*/*.as as3/com/netease/protobuf/*.as) descriptor.proto.as3/google | dist 105 | $(COMPC) -target-player=10 \ 106 | -source-path+=as3,descriptor.proto.as3 \ 107 | -include-sources+=as3 \ 108 | -output=$@ 109 | 110 | doc: \ 111 | $(wildcard as3/com/netease/protobuf/*/*.as as3/com/netease/protobuf/*.as) \ 112 | descriptor.proto.as3/google \ 113 | | dist 114 | $(ASDOC) -target-player=10 \ 115 | --doc-sources+=as3 \ 116 | --source-path+=descriptor.proto.as3 \ 117 | -output=$@ \ 118 | -exclude-sources+=as3/com/netease/protobuf/CustomOption.as 119 | 120 | doc.tar.gz: doc 121 | tar -acf $@ $< 122 | 123 | MANIFEST.MF: 124 | echo Class-Path: protobuf-java-$(PROTOBUF_VERSION).jar > $@ 125 | 126 | dist/protoc-gen-as3.jar: classes/com/netease/protocGenAs3/Main.class \ 127 | MANIFEST.MF | dist 128 | $(JAR) cemf com/netease/protocGenAs3/Main MANIFEST.MF $@ -C classes . 129 | 130 | dist/protobuf-java-$(PROTOBUF_VERSION).jar: \ 131 | $(PROTOBUF_DIR)/java/target/protobuf-java-$(PROTOBUF_VERSION).jar \ 132 | | dist 133 | cp $< $@ 134 | 135 | options.proto.java descriptor.proto.as3 classes plugin.proto.java unittest.proto.as3 dist: 136 | mkdir $@ 137 | 138 | clean: 139 | $(RM) -r release.zip doc doc.tar.gz dist dist.tar.gz classes unittest.proto.as3 descriptor.proto.as3 plugin.proto.java test.swc test.swf options.proto.java 140 | 141 | test: test.swf 142 | (sleep 1s; echo c; sleep 3s; echo c; sleep 1s) | $(FDB) $< 143 | 144 | haxe-test: haxe-test.swf 145 | (sleep 1s; echo c; sleep 1s; echo c; sleep 1s) | $(FDB) $< 146 | 147 | haxe-test.swc: test/com/netease/protobuf/test/TestAll.as \ 148 | dist/protobuf.swc test.swc descriptor.proto.as3/google unittest.bin 149 | $(RM) -r $@ 150 | $(COMPC) -target-player=10 \ 151 | -directory -include-sources+=$< \ 152 | -source-path+=descriptor.proto.as3 \ 153 | -library-path+=test.swc,dist/protobuf.swc \ 154 | -output=$@ 155 | 156 | haxe-test.swf: haxe-test.swc test/com/netease/protobuf/test/HaxeTest.hx test.swf 157 | $(HAXE) -cp test -main com.netease.protobuf.test.HaxeTest \ 158 | -debug -D fdb --macro 'patchTypes("haxe-test.patch")' \ 159 | -swf $@ -swf-version 10 -swf-lib $. 9 | package com.netease.protobuf { 10 | import flash.errors.IllegalOperationError 11 | import flash.utils.getDefinitionByName; 12 | import flash.utils.IDataInput 13 | /** 14 | * @private 15 | */ 16 | public class BaseFieldDescriptor implements IFieldDescriptor { 17 | public var fullName:String 18 | protected var _name:String 19 | public final function get name():String { 20 | return _name 21 | } 22 | protected var tag:uint 23 | public final function get tagNumber():uint { 24 | return tag >>> 3 25 | } 26 | public function get type():Class { 27 | throw new IllegalOperationError("Not Implemented!") 28 | } 29 | public function readSingleField(input:IDataInput):* { 30 | throw new IllegalOperationError("Not Implemented!") 31 | } 32 | public function writeSingleField(output:WritingBuffer, value:*):void { 33 | throw new IllegalOperationError("Not Implemented!") 34 | } 35 | public function write(destination:WritingBuffer, source:Message):void { 36 | throw new IllegalOperationError("Not Implemented!") 37 | } 38 | private static const ACTIONSCRIPT_KEYWORDS:Object = { 39 | "as" : true, "break" : true, "case" : true, 40 | "catch" : true, "class" : true, "const" : true, 41 | "continue" : true, "default" : true, "delete" : true, 42 | "do" : true, "else" : true, "extends" : true, 43 | "false" : true, "finally" : true, "for" : true, 44 | "function" : true, "if" : true, "implements" : true, 45 | "import" : true, "in" : true, "instanceof" : true, 46 | "interface" : true, "internal" : true, "is" : true, 47 | "native" : true, "new" : true, "null" : true, 48 | "package" : true, "private" : true, "protected" : true, 49 | "public" : true, "return" : true, "super" : true, 50 | "switch" : true, "this" : true, "throw" : true, 51 | "to" : true, "true" : true, "try" : true, 52 | "typeof" : true, "use" : true, "var" : true, 53 | "void" : true, "while" : true, "with" : true 54 | } 55 | 56 | public function toString():String { 57 | return name 58 | } 59 | 60 | internal static function getExtensionByName( 61 | name:String):BaseFieldDescriptor { 62 | const fieldPosition:int = name.lastIndexOf('/') 63 | if (fieldPosition == -1) { 64 | return BaseFieldDescriptor(getDefinitionByName(name)) 65 | } else { 66 | return getDefinitionByName(name.substring(0, fieldPosition))[ 67 | name.substring(fieldPosition + 1)] 68 | } 69 | } 70 | } 71 | 72 | } 73 | function regexToUpperCase(matched:String, index:int, whole:String):String { 74 | return matched.charAt(1).toUpperCase() 75 | } 76 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/Binary64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | public class Binary64 { 12 | /** 13 | * @private 14 | */ 15 | internal static const CHAR_CODE_0:uint = '0'.charCodeAt(); 16 | /** 17 | * @private 18 | */ 19 | internal static const CHAR_CODE_9:uint = '9'.charCodeAt(); 20 | /** 21 | * @private 22 | */ 23 | internal static const CHAR_CODE_A:uint = 'a'.charCodeAt(); 24 | /** 25 | * @private 26 | */ 27 | internal static const CHAR_CODE_Z:uint = 'z'.charCodeAt(); 28 | public var low:uint; 29 | /** 30 | * @private 31 | */ 32 | internal var internalHigh:uint; 33 | public function Binary64(low:uint = 0, high:uint = 0) { 34 | this.low = low 35 | this.internalHigh = high 36 | } 37 | /** 38 | * Division by n. 39 | * @return The remainder after division. 40 | * @private 41 | */ 42 | internal final function div(n:uint):uint { 43 | const modHigh:uint = internalHigh % n 44 | const mod:uint = uint((modHigh * 4294967296.0 + low) % n) 45 | internalHigh /= n 46 | const newLow:Number = (modHigh * 4294967296.0 + low) / n 47 | internalHigh += uint(newLow / 4294967296.0) 48 | low = newLow 49 | return mod 50 | } 51 | /** 52 | * @private 53 | */ 54 | internal final function mul(n:uint):void { 55 | const newLow:Number = Number(low) * n 56 | internalHigh *= n 57 | internalHigh += uint(newLow / 4294967296.0) 58 | low *= n 59 | } 60 | /** 61 | * @private 62 | */ 63 | internal final function add(n:uint):void { 64 | const newLow:Number = Number(low) + n 65 | internalHigh += uint(newLow / 4294967296.0) 66 | low = newLow 67 | } 68 | /** 69 | * @private 70 | */ 71 | internal final function bitwiseNot():void { 72 | low = ~low 73 | internalHigh = ~internalHigh 74 | } 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/CustomOption.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // Copyright (c) 2012 , Yang Bo. All rights reserved. 5 | // 6 | // Author: Yang Bo (pop.atry@gmail.com) 7 | // 8 | // Use, modification and distribution are subject to the New BSD License 9 | // as listed at . 10 | 11 | package com.netease.protobuf { 12 | import google.protobuf.MethodOptions 13 | import google.protobuf.ServiceOptions 14 | import flash.errors.* 15 | import flash.utils.* 16 | 17 | public final class CustomOption { 18 | public static function getMethodOptions(methodFullName:String):MethodOptions { 19 | const m:Array = methodFullName.match(/^(.+)\.[^\.]+$/) 20 | if (m == null) { 21 | return null 22 | } 23 | const serviceClass:Class = Class(getDefinitionByName(m[1])) 24 | var optionsBytes:ByteArray 25 | try { 26 | optionsBytes = 27 | serviceClass.OPTIONS_BYTES_BY_METHOD_NAME[methodFullName] 28 | } catch (e:ReferenceError) { 29 | return null 30 | } 31 | if (optionsBytes) { 32 | const result:MethodOptions = new MethodOptions 33 | result.mergeFrom(optionsBytes) 34 | return result 35 | } else { 36 | return null 37 | } 38 | } 39 | 40 | public static function getServiceOptions(serviceClass:Class):ServiceOptions { 41 | var optionsBytes:ByteArray 42 | try { 43 | optionsBytes = serviceClass.OPTIONS_BYTES 44 | } catch (e:ReferenceError) { 45 | return null 46 | } 47 | const result:ServiceOptions = new ServiceOptions 48 | result.mergeFrom(optionsBytes) 49 | return result 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/FieldDescriptor.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import flash.errors.IllegalOperationError; 12 | import flash.utils.IDataInput 13 | /** 14 | * @private 15 | */ 16 | public class FieldDescriptor extends BaseFieldDescriptor { 17 | public final function read(input:IDataInput, 18 | message:Message):void { 19 | message[name] = readSingleField(input) 20 | } 21 | override public final function write(output:WritingBuffer, 22 | message:Message):void { 23 | WriteUtils.write_TYPE_UINT32(output, tag) 24 | writeSingleField(output, message[name]) 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/IFieldDescriptor.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the New BSD License 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | /** 12 | * Run-time infomation for a field. 13 | */ 14 | public interface IFieldDescriptor { 15 | function get type():Class 16 | 17 | function get name():String 18 | 19 | function get tagNumber():uint 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/Int64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | public final class Int64 extends Binary64 { 12 | public final function set high(value:int):void { 13 | internalHigh = value 14 | } 15 | public final function get high():int { 16 | return internalHigh 17 | } 18 | public function Int64(low:uint = 0, high:int = 0) { 19 | super(low, high) 20 | } 21 | /** 22 | * Convert from Number. 23 | */ 24 | public static function fromNumber(n: Number):Int64 { 25 | return new Int64(n, Math.floor(n / 4294967296.0)) 26 | } 27 | /** 28 | * Convert to Number. 29 | */ 30 | public final function toNumber():Number { 31 | return high * 4294967296.0 + low 32 | } 33 | public final function toString(radix:uint = 10):String { 34 | if (radix < 2 || radix > 36) { 35 | throw new ArgumentError 36 | } 37 | switch (high) { 38 | case 0: 39 | { 40 | return low.toString(radix) 41 | } 42 | 43 | case -1: 44 | { 45 | if ((low & 0x80000000) == 0) 46 | { 47 | return (int(low | 0x80000000) - 2147483648.0).toString(radix) 48 | } 49 | else 50 | { 51 | return int(low).toString(radix) 52 | } 53 | } 54 | 55 | default: 56 | { 57 | break; 58 | } 59 | } 60 | if (low == 0 && high == 0) { 61 | return "0" 62 | } 63 | const digitChars:Array = []; 64 | const copyOfThis:UInt64 = new UInt64(low, high); 65 | if (high < 0) { 66 | copyOfThis.bitwiseNot() 67 | copyOfThis.add(1) 68 | } 69 | do { 70 | const digit:uint = copyOfThis.div(radix); 71 | if (digit < 10) { 72 | digitChars.push(digit + CHAR_CODE_0); 73 | } else { 74 | digitChars.push(digit - 10 + CHAR_CODE_A); 75 | } 76 | } while (copyOfThis.high != 0) 77 | if (high < 0) { 78 | return '-' + copyOfThis.low.toString(radix) + 79 | String.fromCharCode.apply( 80 | String, digitChars.reverse()) 81 | } else { 82 | return copyOfThis.low.toString(radix) + 83 | String.fromCharCode.apply( 84 | String, digitChars.reverse()) 85 | } 86 | } 87 | public static function parseInt64(str:String, radix:uint = 0):Int64 { 88 | const negative:Boolean = str.search(/^\-/) == 0 89 | var i:uint = negative ? 1 : 0 90 | if (radix == 0) { 91 | if (str.search(/^\-?0x/) == 0) { 92 | radix = 16 93 | i += 2 94 | } else { 95 | radix = 10 96 | } 97 | } 98 | if (radix < 2 || radix > 36) { 99 | throw new ArgumentError 100 | } 101 | str = str.toLowerCase() 102 | const result:Int64 = new Int64 103 | for (; i < str.length; i++) { 104 | var digit:uint = str.charCodeAt(i) 105 | if (digit >= CHAR_CODE_0 && digit <= CHAR_CODE_9) { 106 | digit -= CHAR_CODE_0 107 | } else if (digit >= CHAR_CODE_A && digit <= CHAR_CODE_Z) { 108 | digit -= CHAR_CODE_A 109 | digit += 10 110 | } else { 111 | throw new ArgumentError 112 | } 113 | if (digit >= radix) { 114 | throw new ArgumentError 115 | } 116 | result.mul(radix) 117 | result.add(digit) 118 | } 119 | if (negative) { 120 | result.bitwiseNot() 121 | result.add(1) 122 | } 123 | return result 124 | } 125 | } 126 | } 127 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/Message.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // Copyright (c) 2012 , Yang Bo. All rights reserved. 5 | // 6 | // Author: Yang Bo (pop.atry@gmail.com) 7 | // 8 | // Use, modification and distribution are subject to the New BSD License 9 | // as listed at . 10 | 11 | package com.netease.protobuf { 12 | import flash.errors.IllegalOperationError 13 | import flash.errors.IOError 14 | import flash.utils.IDataInput 15 | import flash.utils.IDataOutput 16 | use namespace com.netease.protobuf.used_by_generated_code 17 | 18 | public class Message { 19 | /** 20 | * Parse data as a message of this type and merge it with this. 21 | * 22 | * @param input The source where data are reading from.

After calling 23 | * this method, input.endian will be changed to 24 | * flash.utils.Endian.LITTLE_ENDIAN. If input is a 25 | * flash.utils.ByteArray, input.position will increase by 26 | * number of bytes being read.

27 | */ 28 | public final function mergeFrom(input:IDataInput):void { 29 | input.endian = flash.utils.Endian.LITTLE_ENDIAN 30 | used_by_generated_code::readFromSlice(input, 0) 31 | } 32 | /** 33 | * Like mergeFrom(), but does not read until EOF. Instead, 34 | * the size of the message (encoded as a varint) is read first, then 35 | * the message data. Use writeDelimitedTo() to write 36 | * messages in this format. 37 | * 38 | * @param input The source where data are reading from.

After calling 39 | * this method, input.endian will be changed to 40 | * flash.utils.Endian.LITTLE_ENDIAN. If input is a 41 | * flash.utils.ByteArray, input.position will increase by 42 | * number of bytes being read.

43 | * 44 | * @see #mergeFrom() 45 | * @see #writeDelimitedTo() 46 | */ 47 | public final function mergeDelimitedFrom(input:IDataInput):void { 48 | input.endian = flash.utils.Endian.LITTLE_ENDIAN 49 | ReadUtils.read_TYPE_MESSAGE(input, this) 50 | } 51 | /** 52 | * Serializes the message and writes it to output. 53 | * 54 | *

55 | * NOTE: Protocol Buffers are not self-delimiting. Therefore, if you 56 | * write any more data to the stream after the message, you must 57 | * somehow ensure that the parser on the receiving end does not 58 | * interpret this as being * part of the protocol message. This can be 59 | * done e.g. by writing the size of the message before the data, then 60 | * making sure to limit the input to that size on the receiving end 61 | * (e.g. by wrapping the InputStream in one which limits the input). 62 | * Alternatively, just use writeDelimitedTo(). 63 | *

64 | * 65 | * @param output The destination where data are writing to.

If 66 | * output is a flash.utils.ByteArray, 67 | * output.position will increase by number of bytes being 68 | * written.

69 | * 70 | * @see #writeDelimitedTo() 71 | */ 72 | public final function writeTo(output:IDataOutput):void { 73 | const buffer:com.netease.protobuf.WritingBuffer = new com.netease.protobuf.WritingBuffer() 74 | used_by_generated_code::writeToBuffer(buffer) 75 | buffer.toNormal(output) 76 | } 77 | 78 | /** 79 | * Like writeTo(), but writes the size of the message as 80 | * a varint before writing the data. This allows more data to be 81 | * written to the stream after the message without the need to delimit 82 | * the message data yourself. Use mergeDelimitedFrom() to 83 | * parse messages written by this method. 84 | * 85 | * @param output The destination where data are writing to.

If 86 | * output is a flash.utils.ByteArray, 87 | * output.position will increase by number of bytes being 88 | * written.

89 | * 90 | * @see #writeTo() 91 | * @see #mergeDelimitedFrom() 92 | */ 93 | public final function writeDelimitedTo(output:IDataOutput):void { 94 | const buffer:com.netease.protobuf.WritingBuffer = new com.netease.protobuf.WritingBuffer() 95 | WriteUtils.write_TYPE_MESSAGE(buffer, this) 96 | buffer.toNormal(output) 97 | } 98 | 99 | /** 100 | * @private 101 | */ 102 | used_by_generated_code function readFromSlice( 103 | input:IDataInput, bytesAfterSlice:uint):void { 104 | throw new IllegalOperationError("Not implemented!") 105 | } 106 | 107 | /** 108 | * @private 109 | */ 110 | used_by_generated_code function writeToBuffer( 111 | output:WritingBuffer):void { 112 | throw new IllegalOperationError("Not implemented!") 113 | } 114 | 115 | private function writeSingleUnknown(output:WritingBuffer, tag:uint, 116 | value:*):void { 117 | WriteUtils.write_TYPE_UINT32(output, tag) 118 | switch (tag & 7) { 119 | case WireType.VARINT: 120 | WriteUtils.write_TYPE_UINT64(output, value) 121 | break 122 | case WireType.FIXED_64_BIT: 123 | WriteUtils.write_TYPE_FIXED64(output, value) 124 | break 125 | case WireType.LENGTH_DELIMITED: 126 | WriteUtils.write_TYPE_BYTES(output, value) 127 | break 128 | case WireType.FIXED_32_BIT: 129 | WriteUtils.write_TYPE_FIXED32(output, value) 130 | break 131 | default: 132 | throw new IOError("Invalid wire type: " + (tag & 7)) 133 | } 134 | } 135 | 136 | /** 137 | * @private 138 | */ 139 | protected final function writeUnknown(output:WritingBuffer, 140 | fieldName:String):void { 141 | const tag:uint = uint(fieldName) 142 | if (tag == 0) { 143 | throw new ArgumentError( 144 | "Attemp to write an undefined string filed: " + 145 | fieldName) 146 | } 147 | WriteUtils.writeUnknownPair(output, tag, this[fieldName]) 148 | } 149 | /** 150 | * @private 151 | */ 152 | protected final function writeExtensionOrUnknown(output:WritingBuffer, 153 | fieldName:String):void { 154 | if (!this.propertyIsEnumerable(fieldName)) { 155 | return 156 | } 157 | var fieldDescriptor:BaseFieldDescriptor 158 | try { 159 | fieldDescriptor = 160 | BaseFieldDescriptor.getExtensionByName(fieldName) 161 | } catch (e:ReferenceError) { 162 | writeUnknown(output, fieldName) 163 | return 164 | } 165 | fieldDescriptor.write(output, this) 166 | } 167 | /** 168 | * @private 169 | */ 170 | protected final function readUnknown(input:IDataInput, tag:uint):void { 171 | var value:* 172 | switch (tag & 7) { 173 | case WireType.VARINT: 174 | value = ReadUtils.read_TYPE_UINT64(input) 175 | break 176 | case WireType.FIXED_64_BIT: 177 | value = ReadUtils.read_TYPE_FIXED64(input) 178 | break 179 | case WireType.LENGTH_DELIMITED: 180 | value = ReadUtils.read_TYPE_BYTES(input) 181 | break 182 | case WireType.FIXED_32_BIT: 183 | value = ReadUtils.read_TYPE_FIXED32(input) 184 | break 185 | default: 186 | throw new IOError("Invalid wire type: " + (tag & 7)) 187 | } 188 | const currentValue:* = this[tag] 189 | if (!currentValue) { 190 | this[tag] = value 191 | } else if (currentValue is Array) { 192 | currentValue.push(value) 193 | } else { 194 | this[tag] = [currentValue, value] 195 | } 196 | } 197 | /** 198 | * @private 199 | */ 200 | protected final function readExtensionOrUnknown(extensions:Array, 201 | input:IDataInput, tag:uint):void { 202 | var readFunction:Function = extensions[tag]; 203 | if (readFunction != null) { 204 | readFunction(input, this); 205 | } else { 206 | readUnknown(input, tag) 207 | } 208 | } 209 | 210 | public final function toString():String { 211 | return TextFormat.printToString(this) 212 | } 213 | /** 214 | * Get information of a field. 215 | */ 216 | public static function getExtensionByName( 217 | name:String):IFieldDescriptor { 218 | return BaseFieldDescriptor.getExtensionByName(name) 219 | } 220 | 221 | } 222 | } 223 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/ReadUtils.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // Copyright (c) 2012 , Yang Bo. All rights reserved. 5 | // 6 | // Author: Yang Bo (pop.atry@gmail.com) 7 | // 8 | // Use, modification and distribution are subject to the "New BSD License" 9 | // as listed at . 10 | 11 | package com.netease.protobuf { 12 | import flash.errors.* 13 | import flash.utils.* 14 | /** 15 | * @private 16 | */ 17 | public final class ReadUtils { 18 | public static function skip(input:IDataInput, wireType:uint):void { 19 | switch (wireType) { 20 | case WireType.VARINT: 21 | while (input.readUnsignedByte() >= 0x80) {} 22 | break 23 | case WireType.FIXED_64_BIT: 24 | input.readInt() 25 | input.readInt() 26 | break 27 | case WireType.LENGTH_DELIMITED: 28 | for (var i:uint = read_TYPE_UINT32(input); i != 0; i--) { 29 | input.readByte() 30 | } 31 | break 32 | case WireType.FIXED_32_BIT: 33 | input.readInt() 34 | break 35 | default: 36 | throw new IOError("Invalid wire type: " + wireType) 37 | } 38 | } 39 | public static function read_TYPE_DOUBLE(input:IDataInput):Number { 40 | return input.readDouble() 41 | } 42 | public static function read_TYPE_FLOAT(input:IDataInput):Number { 43 | return input.readFloat() 44 | } 45 | public static function read_TYPE_INT64(input:IDataInput):Int64 { 46 | const result:Int64 = new Int64 47 | var b:uint 48 | var i:uint = 0 49 | for (;; i += 7) { 50 | b = input.readUnsignedByte() 51 | if (i == 28) { 52 | break 53 | } else { 54 | if (b >= 0x80) { 55 | result.low |= ((b & 0x7f) << i) 56 | } else { 57 | result.low |= (b << i) 58 | return result 59 | } 60 | } 61 | } 62 | if (b >= 0x80) { 63 | b &= 0x7f 64 | result.low |= (b << i) 65 | result.high = b >>> 4 66 | } else { 67 | result.low |= (b << i) 68 | result.high = b >>> 4 69 | return result 70 | } 71 | for (i = 3;; i += 7) { 72 | b = input.readUnsignedByte() 73 | if (i < 32) { 74 | if (b >= 0x80) { 75 | result.high |= ((b & 0x7f) << i) 76 | } else { 77 | result.high |= (b << i) 78 | break 79 | } 80 | } 81 | } 82 | return result 83 | } 84 | public static function read_TYPE_UINT64(input:IDataInput):UInt64 { 85 | const result:UInt64 = new UInt64 86 | var b:uint 87 | var i:uint = 0 88 | for (;; i += 7) { 89 | b = input.readUnsignedByte() 90 | if (i == 28) { 91 | break 92 | } else { 93 | if (b >= 0x80) { 94 | result.low |= ((b & 0x7f) << i) 95 | } else { 96 | result.low |= (b << i) 97 | return result 98 | } 99 | } 100 | } 101 | if (b >= 0x80) { 102 | b &= 0x7f 103 | result.low |= (b << i) 104 | result.high = b >>> 4 105 | } else { 106 | result.low |= (b << i) 107 | result.high = b >>> 4 108 | return result 109 | } 110 | for (i = 3;; i += 7) { 111 | b = input.readUnsignedByte() 112 | if (i < 32) { 113 | if (b >= 0x80) { 114 | result.high |= ((b & 0x7f) << i) 115 | } else { 116 | result.high |= (b << i) 117 | break 118 | } 119 | } 120 | } 121 | return result 122 | } 123 | public static function read_TYPE_INT32(input:IDataInput):int { 124 | return int(read_TYPE_UINT32(input)) 125 | } 126 | public static function read_TYPE_FIXED64(input:IDataInput):UInt64 { 127 | const result:UInt64 = new UInt64 128 | result.low = input.readUnsignedInt() 129 | result.high = input.readUnsignedInt() 130 | return result 131 | } 132 | public static function read_TYPE_FIXED32(input:IDataInput):uint { 133 | return input.readUnsignedInt() 134 | } 135 | public static function read_TYPE_BOOL(input:IDataInput):Boolean { 136 | return read_TYPE_UINT32(input) != 0 137 | } 138 | public static function read_TYPE_STRING(input:IDataInput):String { 139 | const length:uint = read_TYPE_UINT32(input) 140 | return input.readUTFBytes(length) 141 | } 142 | public static function read_TYPE_BYTES(input:IDataInput):ByteArray { 143 | const result:ByteArray = new ByteArray 144 | const length:uint = read_TYPE_UINT32(input) 145 | if (length > 0) { 146 | input.readBytes(result, 0, length) 147 | } 148 | return result 149 | } 150 | public static function read_TYPE_UINT32(input:IDataInput):uint { 151 | var result:uint = 0 152 | for (var i:uint = 0;; i += 7) { 153 | const b:uint = input.readUnsignedByte() 154 | if (i < 32) { 155 | if (b >= 0x80) { 156 | result |= ((b & 0x7f) << i) 157 | } else { 158 | result |= (b << i) 159 | break 160 | } 161 | } else { 162 | while (input.readUnsignedByte() >= 0x80) {} 163 | break 164 | } 165 | } 166 | return result 167 | } 168 | public static function read_TYPE_ENUM(input:IDataInput):int { 169 | return read_TYPE_INT32(input) 170 | } 171 | public static function read_TYPE_SFIXED32(input:IDataInput):int { 172 | return input.readInt() 173 | } 174 | public static function read_TYPE_SFIXED64(input:IDataInput):Int64 { 175 | const result:Int64 = new Int64 176 | result.low = input.readUnsignedInt() 177 | result.high = input.readInt() 178 | return result 179 | } 180 | public static function read_TYPE_SINT32(input:IDataInput):int { 181 | return ZigZag.decode32(read_TYPE_UINT32(input)) 182 | } 183 | public static function read_TYPE_SINT64(input:IDataInput):Int64 { 184 | const result:Int64 = read_TYPE_INT64(input) 185 | const low:uint = result.low 186 | const high:uint = result.high 187 | result.low = ZigZag.decode64low(low, high) 188 | result.high = ZigZag.decode64high(low, high) 189 | return result 190 | } 191 | public static function read_TYPE_MESSAGE(input:IDataInput, 192 | message:Message):Message { 193 | const length:uint = read_TYPE_UINT32(input) 194 | if (input.bytesAvailable < length) { 195 | throw new IOError("Invalid message length: " + length) 196 | } 197 | const bytesAfterSlice:uint = input.bytesAvailable - length 198 | message.used_by_generated_code::readFromSlice(input, bytesAfterSlice) 199 | if (input.bytesAvailable != bytesAfterSlice) { 200 | throw new IOError("Invalid nested message") 201 | } 202 | return message 203 | } 204 | public static function readPackedRepeated(input:IDataInput, 205 | readFuntion:Function, value:Array):void { 206 | const length:uint = read_TYPE_UINT32(input) 207 | if (input.bytesAvailable < length) { 208 | throw new IOError("Invalid message length: " + length) 209 | } 210 | const bytesAfterSlice:uint = input.bytesAvailable - length 211 | while (input.bytesAvailable > bytesAfterSlice) { 212 | value.push(readFuntion(input)) 213 | } 214 | if (input.bytesAvailable != bytesAfterSlice) { 215 | throw new IOError("Invalid packed repeated data") 216 | } 217 | } 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/RepeatedFieldDescriptor.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import flash.errors.IllegalOperationError 12 | import flash.errors.IOError 13 | import flash.utils.IDataInput 14 | import flash.utils.ByteArray 15 | /** 16 | * @private 17 | */ 18 | public class RepeatedFieldDescriptor extends BaseFieldDescriptor { 19 | public function get elementType():Class { 20 | throw new IllegalOperationError("Not Implemented!") 21 | } 22 | public final function readNonPacked(input:IDataInput, 23 | message:Message):void { 24 | const destination:Array = message[name] || (message[name] = []) 25 | destination.push(readSingleField(input)) 26 | } 27 | public final function readPacked(input:IDataInput, 28 | message:Message):void { 29 | const destination:Array = message[name] || (message[name] = []) 30 | const length:uint = ReadUtils.read_TYPE_UINT32(input) 31 | if (input.bytesAvailable < length) { 32 | throw new IOError("Invalid message length: " + length) 33 | } 34 | const bytesAfterSlice:uint = input.bytesAvailable - length 35 | while (input.bytesAvailable > bytesAfterSlice) { 36 | destination.push(readSingleField(input)) 37 | } 38 | if (input.bytesAvailable != bytesAfterSlice) { 39 | throw new IOError("Invalid packed destination data") 40 | } 41 | } 42 | public function get nonPackedWireType():int { 43 | throw new IllegalOperationError("Not Implemented!") 44 | } 45 | override public final function write(output:WritingBuffer, 46 | message:Message):void { 47 | const source:Array = message[name] 48 | if ((tag & 7) == nonPackedWireType) { 49 | for (var k:uint = 0; k < source.length; k++) { 50 | WriteUtils.write_TYPE_UINT32(output, tag) 51 | writeSingleField(output, source[k]) 52 | } 53 | } else { 54 | WriteUtils.write_TYPE_UINT32(output, tag) 55 | const i:uint = output.beginBlock() 56 | for (var j:uint = 0; j < source.length; j++) { 57 | writeSingleField(output, source[j]) 58 | } 59 | output.endBlock(i) 60 | } 61 | } 62 | 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/SimpleWebRPC.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import flash.net.*; 12 | import flash.utils.*; 13 | import flash.events.*; 14 | /** 15 | * A simple sample of RPC implementation. 16 | */ 17 | public final class SimpleWebRPC { 18 | private var urlPrefix:String 19 | public function SimpleWebRPC(urlPrefix:String) { 20 | this.urlPrefix = urlPrefix 21 | } 22 | 23 | private static const REF:Dictionary = new Dictionary 24 | 25 | public function send(qualifiedMethodName:String, 26 | requestMessage:Message, 27 | rpcResult:Function, 28 | responseClass:Class):void { 29 | const loader:URLLoader = new URLLoader 30 | REF[loader] = true; 31 | loader.dataFormat = URLLoaderDataFormat.BINARY 32 | loader.addEventListener(Event.COMPLETE, function(event:Event):void { 33 | delete REF[loader] 34 | const responseMessage:Message = new responseClass 35 | responseMessage.mergeFrom(loader.data) 36 | rpcResult(responseMessage) 37 | }) 38 | function errorEventHandler(event:Event):void { 39 | delete REF[loader] 40 | rpcResult(event) 41 | } 42 | loader.addEventListener(IOErrorEvent.IO_ERROR, errorEventHandler) 43 | loader.addEventListener( 44 | SecurityErrorEvent.SECURITY_ERROR, errorEventHandler) 45 | const request:URLRequest = new URLRequest( 46 | urlPrefix + qualifiedMethodName.replace(/\./g, "/"). 47 | replace(/^((com|org|net)\/\w+\/\w+\/)?(.*)$/, "$3")) 48 | const requestContent:ByteArray = new ByteArray 49 | requestMessage.writeTo(requestContent) 50 | if (requestContent.length != 0) 51 | { 52 | request.data = requestContent 53 | } 54 | request.contentType = "application/x-protobuf" 55 | request.method = URLRequestMethod.POST 56 | loader.load(request) 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/TextFormat.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import com.netease.protobuf.fieldDescriptors.*; 12 | import flash.errors.IllegalOperationError; 13 | import flash.errors.IOError; 14 | import flash.utils.describeType 15 | import flash.utils.Dictionary; 16 | import flash.utils.getDefinitionByName; 17 | import flash.utils.IDataInput 18 | import flash.utils.IDataOutput 19 | import flash.utils.ByteArray 20 | public final class TextFormat { 21 | private static function printHex(output:IDataOutput, value:uint):void { 22 | const hexString:String = value.toString(16) 23 | output.writeUTFBytes("00000000".substring(0, 8 - hexString.length)) 24 | output.writeUTFBytes(hexString) 25 | } 26 | private static const allEnumValues:Dictionary = new Dictionary 27 | private static function printEnum(output:IDataOutput, 28 | value:int, enumType:Class):void { 29 | var enumValues:Array 30 | if (enumType in allEnumValues) { 31 | enumValues = allEnumValues[enumType] 32 | } else { 33 | const enumTypeDescription:XML = describeType(enumType) 34 | // Not enumTypeDescription.*.@name, 35 | // because haXe will replace all constants to variables, WTF! 36 | const xmlNames:XMLList = enumTypeDescription.*.@name 37 | enumValues = [] 38 | for each(var name:String in xmlNames) { 39 | enumValues[enumType[name]] = name 40 | } 41 | allEnumValues[enumType] = enumValues 42 | } 43 | if (value in enumValues) { 44 | output.writeUTFBytes(enumValues[value]) 45 | } else { 46 | throw new IOError(value + " is invalid for " + 47 | enumTypeDescription.@name) 48 | } 49 | } 50 | private static function printBytes(output:IDataOutput, 51 | value:ByteArray):void { 52 | output.writeUTFBytes("\""); 53 | value.position = 0 54 | while (value.bytesAvailable > 0) { 55 | const byte:int = value.readByte() 56 | switch (byte) { 57 | case 7: output.writeUTFBytes("\\a" ); break; 58 | case 8: output.writeUTFBytes("\\b" ); break; 59 | case 12: output.writeUTFBytes("\\f" ); break; 60 | case 10: output.writeUTFBytes("\\n" ); break; 61 | case 13: output.writeUTFBytes("\\r" ); break; 62 | case 9: output.writeUTFBytes("\\t" ); break; 63 | case 11: output.writeUTFBytes("\\v" ); break; 64 | case 92: output.writeUTFBytes("\\\\"); break; 65 | case 39: output.writeUTFBytes("\\\'"); break; 66 | case 34 : output.writeUTFBytes("\\\""); break; 67 | default: 68 | if (byte >= 0x20) { 69 | output.writeByte(byte); 70 | } else { 71 | output.writeUTFBytes('\\'); 72 | output.writeByte('0'.charCodeAt() + ((byte >>> 6) & 3)); 73 | output.writeByte('0'.charCodeAt() + ((byte >>> 3) & 7)); 74 | output.writeByte('0'.charCodeAt() + (byte & 7)); 75 | } 76 | break; 77 | } 78 | } 79 | output.writeUTFBytes("\""); 80 | } 81 | private static function printString(output:IDataOutput, 82 | value:String):void { 83 | const buffer:ByteArray = new ByteArray 84 | buffer.writeUTFBytes(value) 85 | printBytes(output, buffer) 86 | } 87 | private static function printUnknownField(output:IDataOutput, tag:uint, 88 | value:Object, printSetting:PrintSetting, currentIndent:String):void { 89 | const unknownArray:Array = value as Array 90 | if (unknownArray) { 91 | if (unknownArray.length > 0) { 92 | printSingleUnknownField(output, tag, unknownArray[k], 93 | printSetting, currentIndent) 94 | for (var k:int = 1; k < unknownArray.length; k++) { 95 | output.writeByte(printSetting.newLine) 96 | printSingleUnknownField(output, tag, unknownArray[k], 97 | printSetting, currentIndent) 98 | } 99 | } 100 | } else { 101 | printSingleUnknownField( 102 | output, tag, value, printSetting, 103 | currentIndent) 104 | } 105 | } 106 | 107 | private static function printSingleUnknownField(output:IDataOutput, 108 | tag:uint, value:Object, printSetting:PrintSetting, 109 | currentIndent:String):void { 110 | output.writeUTFBytes(currentIndent) 111 | output.writeUTFBytes(String(tag >>> 3)) 112 | output.writeUTFBytes(printSetting.simpleFieldSeperator) 113 | switch (tag & 7) { 114 | case WireType.VARINT: 115 | output.writeUTFBytes(UInt64(value).toString()) 116 | break 117 | case WireType.FIXED_32_BIT: 118 | output.writeUTFBytes("0x") 119 | printHex(output, uint(value)) 120 | break 121 | case WireType.FIXED_64_BIT: 122 | const u64:UInt64 = UInt64(value) 123 | output.writeUTFBytes("0x") 124 | printHex(output, u64.high) 125 | printHex(output, u64.low) 126 | break 127 | case WireType.LENGTH_DELIMITED: 128 | printBytes(output, ByteArray(value)) 129 | break 130 | } 131 | } 132 | private static const allMessageFields:Dictionary = new Dictionary 133 | private static function printMessageFields(output:IDataOutput, 134 | message:Message, 135 | printSetting:PrintSetting, 136 | currentIndent:String = ""):void { 137 | var isFirst:Boolean = true 138 | const type:Class = Object(message).constructor 139 | var messageFields:XMLList 140 | if (type in allMessageFields) { 141 | // Fetch in cache 142 | messageFields = allMessageFields[type] 143 | } else { 144 | const description:XML = describeType(type) 145 | // Not description.constant, 146 | // because haXe will replace constant to variable, WTF! 147 | messageFields = description.*. 148 | ( 149 | 0 == String(@type).search( 150 | /^com.netease.protobuf.fieldDescriptors::(Repeated)?FieldDescriptor_/) && 151 | // Not extension 152 | BaseFieldDescriptor(type[@name]).name.search(/\//) == -1 153 | ).@name 154 | allMessageFields[type] = messageFields 155 | } 156 | 157 | for each (var fieldDescriptorName:String in messageFields) { 158 | const fieldDescriptor:BaseFieldDescriptor = 159 | type[fieldDescriptorName] 160 | const shortName:String = fieldDescriptor.fullName.substring( 161 | fieldDescriptor.fullName.lastIndexOf('.') + 1) 162 | if (fieldDescriptor.type == Array) { 163 | const fieldValues:Array = message[fieldDescriptor.name] 164 | if (fieldValues) { 165 | for (var i:int = 0; i < fieldValues.length; i++) { 166 | if (isFirst) { 167 | isFirst = false 168 | } else { 169 | output.writeByte(printSetting.newLine) 170 | } 171 | output.writeUTFBytes(currentIndent) 172 | output.writeUTFBytes(shortName) 173 | printValue(output, fieldDescriptor, fieldValues[i], 174 | printSetting, currentIndent) 175 | } 176 | } 177 | } else { 178 | const m:Array = fieldDescriptor.name.match(/^(__)?(.)(.*)$/) 179 | m[0] = "" 180 | m[1] = "has" 181 | m[2] = m[2].toUpperCase() 182 | const hasField:String = m.join("") 183 | try { 184 | // optional and does not have that field. 185 | if (false === message[hasField]) { 186 | continue 187 | } 188 | } catch (e:ReferenceError) { 189 | // required 190 | } 191 | if (isFirst) { 192 | isFirst = false 193 | } else { 194 | output.writeByte(printSetting.newLine) 195 | } 196 | output.writeUTFBytes(currentIndent) 197 | output.writeUTFBytes(shortName) 198 | printValue(output, fieldDescriptor, 199 | message[fieldDescriptor.name], printSetting, 200 | currentIndent) 201 | } 202 | } 203 | for (var key:String in message) { 204 | if (!message.propertyIsEnumerable(key)) { 205 | continue 206 | } 207 | var extension:BaseFieldDescriptor 208 | try { 209 | extension = BaseFieldDescriptor.getExtensionByName(key) 210 | } catch (e:ReferenceError) { 211 | if (key.search(/^[0-9]+$/) == 0) { 212 | // unknown field 213 | if (isFirst) { 214 | isFirst = false 215 | } else { 216 | output.writeByte(printSetting.newLine) 217 | } 218 | printUnknownField(output, uint(key), message[key], 219 | printSetting, currentIndent) 220 | } else { 221 | throw new IOError("Bad unknown field " + key) 222 | } 223 | continue 224 | } 225 | if (extension.type == Array) { 226 | const extensionFieldValues:Array = message[key] 227 | for (var j:int = 0; j < extensionFieldValues.length; j++) { 228 | if (isFirst) { 229 | isFirst = false 230 | } else { 231 | output.writeByte(printSetting.newLine) 232 | } 233 | output.writeUTFBytes(currentIndent) 234 | output.writeUTFBytes("[") 235 | output.writeUTFBytes(extension.fullName) 236 | output.writeUTFBytes("]") 237 | printValue(output, extension, 238 | extensionFieldValues[j], printSetting, 239 | currentIndent) 240 | } 241 | } else { 242 | if (isFirst) { 243 | isFirst = false 244 | } else { 245 | output.writeByte(printSetting.newLine) 246 | } 247 | output.writeUTFBytes(currentIndent) 248 | output.writeUTFBytes("[") 249 | output.writeUTFBytes(extension.fullName) 250 | output.writeUTFBytes("]") 251 | printValue(output, extension, message[key], printSetting, 252 | currentIndent) 253 | } 254 | } 255 | } 256 | 257 | private static function printValue(output:IDataOutput, 258 | fieldDescriptor:BaseFieldDescriptor, 259 | value:Object, 260 | printSetting:PrintSetting, 261 | currentIndent:String = ""):void { 262 | const message:Message = value as Message 263 | if (message) { 264 | if (printSetting == SINGLELINE_MODE) { 265 | output.writeUTFBytes("{") 266 | } else { 267 | output.writeUTFBytes(" {\n") 268 | } 269 | printMessageFields(output, message, printSetting, 270 | printSetting.indentChars + currentIndent) 271 | if (printSetting == SINGLELINE_MODE) { 272 | output.writeUTFBytes("}") 273 | } else { 274 | output.writeByte(printSetting.newLine) 275 | output.writeUTFBytes(currentIndent) 276 | output.writeUTFBytes("}") 277 | } 278 | } else { 279 | output.writeUTFBytes(printSetting.simpleFieldSeperator) 280 | const stringValue:String = value as String 281 | if (stringValue !== null) { 282 | printString(output, stringValue) 283 | } else { 284 | const enumFieldDescriptor:FieldDescriptor_TYPE_ENUM = 285 | fieldDescriptor as FieldDescriptor_TYPE_ENUM 286 | if (enumFieldDescriptor) { 287 | printEnum(output, int(value), 288 | enumFieldDescriptor.enumType) 289 | } else { 290 | const enumRepeatedFieldDescriptor: 291 | RepeatedFieldDescriptor_TYPE_ENUM = 292 | fieldDescriptor as 293 | RepeatedFieldDescriptor_TYPE_ENUM 294 | if (enumRepeatedFieldDescriptor) { 295 | printEnum(output, int(value), 296 | enumRepeatedFieldDescriptor.enumType) 297 | } else if ( 298 | fieldDescriptor is FieldDescriptor_TYPE_BYTES || 299 | fieldDescriptor is 300 | RepeatedFieldDescriptor_TYPE_BYTES) { 301 | printBytes(output, ByteArray(value)) 302 | } else { 303 | output.writeUTFBytes(value.toString()) 304 | } 305 | } 306 | } 307 | } 308 | } 309 | 310 | /** 311 | * Outputs a textual representation of the Protocol Message supplied into 312 | * the parameter output. 313 | */ 314 | public static function printToUTFBytes( 315 | output:IDataOutput, 316 | message:Message, 317 | singleLineMode:Boolean = true, 318 | currentIndent:String = ""):void { 319 | printMessageFields( 320 | output, 321 | message, 322 | singleLineMode ? SINGLELINE_MODE : MULTILINE_MODE, 323 | currentIndent) 324 | } 325 | 326 | /** 327 | * Like printToUTFBytes(), but writes directly to a String and 328 | * returns it. 329 | */ 330 | public static function printToString( 331 | message:Message, 332 | singleLineMode:Boolean = true, 333 | currentIndent:String = ""):String { 334 | const ba:ByteArray = new ByteArray 335 | printToUTFBytes(ba, message, singleLineMode, currentIndent) 336 | ba.position = 0 337 | return ba.readUTFBytes(ba.length) 338 | } 339 | 340 | private static function skipWhitespace(source:ISource):void { 341 | for (;; ) { 342 | const b:int = source.read() 343 | switch (b) { 344 | case 0x20:/* space */ 345 | case 0x09:/* \t */ 346 | case 0x0a:/* \n */ 347 | case 0x0d:/* \r */ 348 | continue 349 | case 0x23:/* # */ 350 | comment: for (;;) { 351 | switch (source.read()) 352 | { 353 | case 0x0a:/* \n */ 354 | case 0x0d:/* \n */ 355 | break comment 356 | } 357 | } 358 | break 359 | default: 360 | source.unread(b) 361 | return 362 | } 363 | } 364 | } 365 | private static function toHexDigit(b:int):int { 366 | if (b >= 0x30 && b <= 0x39) { 367 | return b - 0x30 368 | } else if (b >= 0x61 && b <= 0x66) { 369 | return b - 0x57 370 | } else if (b >= 0x41 && b <= 0x46) { 371 | return b - 0x37 372 | } else { 373 | throw new IOError("Expect hex, got " + String.fromCharCode(b)) 374 | } 375 | } 376 | private static function toOctalDigit(b:int):int { 377 | if (b >= 0x30 && b <= 0x37) { 378 | return b - 0x30 379 | } else { 380 | throw new IOError("Expect digit, got " + String.fromCharCode(b)) 381 | } 382 | } 383 | private static function tryConsumeBytes(source:ISource):ByteArray { 384 | skipWhitespace(source) 385 | const start:int = source.read() 386 | switch (start) { 387 | case 0x22 /* " */: 388 | case 0x27 /* ' */: 389 | const result:ByteArray = new ByteArray 390 | for (;;) { 391 | const b:int = source.read() 392 | switch (b) { 393 | case start: 394 | return result 395 | case 0x5c: /* \ */ 396 | const b0:int = source.read() 397 | switch (b0) { 398 | case 0x61 /* \a */: result.writeByte(7); continue; 399 | case 0x62 /* \b */: result.writeByte(8); continue; 400 | case 0x66 /* \f */: result.writeByte(12); continue; 401 | case 0x6e /* \n */: result.writeByte(10); continue; 402 | case 0x72 /* \r */: result.writeByte(13); continue; 403 | case 0x74 /* \t */: result.writeByte(9); continue; 404 | case 0x76 /* \v */: result.writeByte(11); continue; 405 | case 0x78 /* \xXX */: 406 | const x0:int = source.read() 407 | const x1:int = source.read() 408 | result.writeByte( 409 | toHexDigit(x0) * 0x10 + 410 | toHexDigit(x1)) 411 | continue 412 | default: 413 | if (b0 >= 0x30 && b0 <= 0x39) { 414 | const b1:int = source.read() 415 | const b2:int = source.read() 416 | result.writeByte( 417 | toOctalDigit(b0) * 64 + 418 | toOctalDigit(b1) * 8 + 419 | toOctalDigit(b2)) 420 | } else { 421 | result.writeByte(b0) 422 | } 423 | continue 424 | } 425 | default: 426 | result.writeByte(b) 427 | break 428 | } 429 | } 430 | break 431 | default: 432 | source.unread(start) 433 | break 434 | } 435 | return null 436 | } 437 | 438 | private static function tryConsume(source:ISource, 439 | expected:int):Boolean { 440 | skipWhitespace(source) 441 | const b:int = source.read() 442 | if (b == expected) { 443 | return true 444 | } else { 445 | source.unread(b) 446 | return false 447 | } 448 | } 449 | 450 | private static function consume(source:ISource, expected:int):void { 451 | skipWhitespace(source) 452 | const b:int = source.read() 453 | if (b != expected) { 454 | throw new IOError("Expect " + String.fromCharCode(expected) + 455 | ", got " + String.fromCharCode(b)) 456 | } 457 | } 458 | 459 | private static function consumeIdentifier(source:ISource):String { 460 | skipWhitespace(source) 461 | const nameBuffer:ByteArray = new ByteArray 462 | for (;; ) { 463 | const b:int = source.read() 464 | if (b >= 0x30 && b <= 0x39 || // 0-9 465 | b >= 0x41 && b <= 0x5a || // A-Z 466 | b >= 0x61 && b <= 0x7a || // a-z 467 | b == 0x2e || b == 0x5f || b == 0x2d || b < 0) { 468 | nameBuffer.writeByte(b) 469 | } else { 470 | if (nameBuffer.length == 0) { 471 | throw new IOError("Expect Identifier, got " + 472 | String.fromCharCode(b)) 473 | } 474 | source.unread(b) 475 | break 476 | } 477 | } 478 | nameBuffer.position = 0 479 | return nameBuffer.readUTFBytes(nameBuffer.length) 480 | } 481 | 482 | private static function appendUnknown(message:Message, tag:uint, 483 | value:*):void { 484 | const oldValue:* = message[tag] 485 | if (oldValue === undefined) { 486 | message[tag] = value 487 | } else { 488 | const oldArray:Array = oldValue as Array 489 | if (oldArray) { 490 | oldArray.push(value) 491 | } else { 492 | message[tag] = [oldValue, value] 493 | } 494 | } 495 | } 496 | 497 | private static function consumeUnknown(source:ISource, 498 | message:Message, number:uint):void { 499 | const bytes:ByteArray = tryConsumeBytes(source) 500 | if (bytes) { 501 | appendUnknown(message, 502 | (number << 3) | WireType.LENGTH_DELIMITED, 503 | bytes) 504 | return 505 | } 506 | const identifier:String = consumeIdentifier(source) 507 | const m:Array = identifier.match( 508 | /^0[xX]([0-9a-fA-F]{16}|[0-9a-fA-F]{8})$/) 509 | if (!m) { 510 | appendUnknown(message, 511 | (number << 3) | WireType.VARINT, 512 | UInt64.parseUInt64(identifier)) 513 | return 514 | } 515 | const hex:String = m[1] 516 | if (hex.length == 8) { 517 | appendUnknown(message, 518 | (number << 3) | WireType.FIXED_32_BIT, 519 | uint(parseInt(hex, 16))) 520 | } else { 521 | appendUnknown(message, 522 | (number << 3) | WireType.FIXED_64_BIT, 523 | UInt64.parseUInt64(hex, 16)) 524 | } 525 | } 526 | 527 | private static function consumeEnumFieldValue(source:ISource, 528 | enumType:Class):int { 529 | consume(source, 0x3a/* : */) 530 | const enumName:String = consumeIdentifier(source) 531 | const result:* = enumType[enumName] 532 | if (result === undefined) { 533 | throw new IOError("Invalid enum name " + enumName) 534 | } else { 535 | return result 536 | } 537 | } 538 | 539 | private static function parseUnknown(message:Message):void { 540 | const buffer:WritingBuffer = new WritingBuffer 541 | for (var fieldName:String in message) { 542 | const tag:uint = uint(fieldName) 543 | if (tag == 0) { 544 | continue 545 | } 546 | WriteUtils.writeUnknownPair(buffer, tag, message[fieldName]) 547 | delete message[fieldName] 548 | } 549 | const normalBuffer:ByteArray = new ByteArray 550 | buffer.toNormal(normalBuffer) 551 | normalBuffer.position = 0 552 | message.mergeFrom(normalBuffer) 553 | } 554 | 555 | private static function consumeFieldValue(source:ISource, 556 | type:Class):* { 557 | switch (type) { 558 | case ByteArray: 559 | consume(source, 0x3a/* : */) 560 | const bytes:ByteArray = tryConsumeBytes(source) 561 | if (bytes) { 562 | bytes.position = 0 563 | return bytes 564 | } else { 565 | throw new IOError("Expect quoted bytes") 566 | } 567 | case String: 568 | consume(source, 0x3a/* : */) 569 | const binaryString:ByteArray = tryConsumeBytes(source) 570 | if (binaryString) { 571 | binaryString.position = 0 572 | return binaryString.readUTFBytes(binaryString.length) 573 | } else { 574 | throw new IOError("Expect quoted string") 575 | } 576 | case Boolean: 577 | consume(source, 0x3a/* : */) 578 | const booleanString:String = consumeIdentifier(source) 579 | switch (booleanString) { 580 | case "true": 581 | return true 582 | case "false": 583 | return false 584 | default: 585 | throw new IOError("Expect boolean, got " + 586 | booleanString) 587 | } 588 | break 589 | case Int64: 590 | consume(source, 0x3a/* : */) 591 | return Int64.parseInt64(consumeIdentifier(source)) 592 | case UInt64: 593 | consume(source, 0x3a/* : */) 594 | return UInt64.parseUInt64(consumeIdentifier(source)) 595 | case uint: 596 | consume(source, 0x3a/* : */) 597 | return uint(parseInt(consumeIdentifier(source))) 598 | case int: 599 | consume(source, 0x3a/* : */) 600 | return int(parseInt(consumeIdentifier(source))) 601 | case Number: 602 | consume(source, 0x3a/* : */) 603 | return parseFloat(consumeIdentifier(source)) 604 | default: 605 | tryConsume(source, 0x3a/* : */) 606 | consume(source, 0x7b/* { */) 607 | const message:Message = new type 608 | for (;; ) { 609 | if (tryConsume(source, 0x7d/* } */)) { 610 | break 611 | } 612 | consumeField(source, message) 613 | } 614 | parseUnknown(message) 615 | return message 616 | } 617 | } 618 | 619 | private static function consumeField(source:ISource, 620 | message:Message):void { 621 | const isExtension:Boolean = tryConsume(source, 0x5b /* [ */) 622 | const name:String = consumeIdentifier(source) 623 | if (isExtension) { 624 | consume(source, 0x5d /* ] */) 625 | } 626 | var fieldDescriptor:BaseFieldDescriptor 627 | if (isExtension) { 628 | const lastDotPosition:int = name.lastIndexOf('.') 629 | const scope:String = name.substring(0, lastDotPosition) 630 | const localName:String = name.substring(lastDotPosition + 1) 631 | try { 632 | fieldDescriptor = getDefinitionByName(scope)[ 633 | localName.toUpperCase()] 634 | } catch (e:ReferenceError) { 635 | try { 636 | fieldDescriptor = BaseFieldDescriptor( 637 | getDefinitionByName(scope + '.' + 638 | localName.toUpperCase())) 639 | } catch (e:ReferenceError) { 640 | throw new IOError("Unknown extension: " + name) 641 | } 642 | } 643 | } else { 644 | if (name.search(/[0-9]+/) == 0) { 645 | consume(source, 0x3a/* : */) 646 | consumeUnknown(source, message, uint(name)) 647 | return 648 | } else { 649 | fieldDescriptor = Object(message).constructor[ 650 | name.toUpperCase()] 651 | if (!fieldDescriptor) { 652 | throw new IOError("Unknown field: " + name); 653 | } 654 | } 655 | } 656 | const repeatedFieldDescriptor:RepeatedFieldDescriptor = 657 | fieldDescriptor as RepeatedFieldDescriptor 658 | if (repeatedFieldDescriptor) { 659 | const destination:Array = 660 | message[fieldDescriptor.name] || 661 | (message[fieldDescriptor.name] = []) 662 | const enumRepeatedFieldDescriptor: 663 | RepeatedFieldDescriptor_TYPE_ENUM = 664 | repeatedFieldDescriptor as 665 | RepeatedFieldDescriptor_TYPE_ENUM 666 | destination.push(enumRepeatedFieldDescriptor ? 667 | consumeEnumFieldValue(source, 668 | enumRepeatedFieldDescriptor.enumType) : 669 | consumeFieldValue(source, 670 | repeatedFieldDescriptor.elementType)) 671 | } else { 672 | const enumFieldDescriptor:FieldDescriptor_TYPE_ENUM = 673 | fieldDescriptor as FieldDescriptor_TYPE_ENUM 674 | message[fieldDescriptor.name] = enumFieldDescriptor ? 675 | consumeEnumFieldValue(source, 676 | enumFieldDescriptor.enumType) : 677 | consumeFieldValue(source, 678 | fieldDescriptor.type) 679 | } 680 | } 681 | 682 | private static function mergeFromSource(source:ISource, 683 | message:Message):void { 684 | for (;; ) { 685 | if (tryConsume(source, 0/* EOF */)) { 686 | break 687 | } 688 | consumeField(source, message) 689 | } 690 | parseUnknown(message) 691 | } 692 | 693 | /** 694 | * Parse a text-format message from input and merge the 695 | * contents into message. 696 | */ 697 | public static function mergeFromUTFBytes(input:IDataInput, 698 | message:Message):void { 699 | mergeFromSource(new WrappedSource(input), message) 700 | } 701 | 702 | /** 703 | * Parse a text-format message from text and merge the 704 | * contents into message. 705 | */ 706 | public static function mergeFromString(text:String, message:Message):void { 707 | const source:BufferedSource = new BufferedSource 708 | source.writeUTFBytes(text) 709 | source.position = 0 710 | mergeFromSource(source, message) 711 | } 712 | } 713 | } 714 | import flash.errors.IOError; 715 | import flash.utils.IDataInput; 716 | import flash.utils.ByteArray; 717 | import flash.errors.EOFError 718 | 719 | 720 | interface ISource { 721 | function read():int 722 | function unread(b:int):void 723 | } 724 | 725 | class BufferedSource extends ByteArray implements ISource { 726 | public function unread(value:int):void { 727 | if (value == 0 && bytesAvailable == 0) { 728 | return 729 | } 730 | position-- 731 | } 732 | public function read():int { 733 | if (bytesAvailable > 0) { 734 | return readByte() 735 | } else { 736 | return 0 737 | } 738 | } 739 | } 740 | 741 | class WrappedSource implements ISource { 742 | private var input:IDataInput 743 | private var temp:int 744 | public function WrappedSource(input:IDataInput) { 745 | this.input = input 746 | } 747 | public function unread(value:int):void { 748 | if (temp) { 749 | throw new IOError("Cannot unread twice!") 750 | } 751 | temp = value 752 | } 753 | public function read():int { 754 | if (temp) { 755 | const result:int = temp 756 | temp = 0 757 | return result 758 | } else { 759 | try { 760 | return input.readByte() 761 | } catch (e: EOFError) { 762 | } 763 | return 0 764 | } 765 | } 766 | } 767 | 768 | class PrintSetting { 769 | public var newLine:uint 770 | public var indentChars:String 771 | public var simpleFieldSeperator:String 772 | } 773 | 774 | const SINGLELINE_MODE:PrintSetting = new PrintSetting 775 | SINGLELINE_MODE.newLine = ' '.charCodeAt() 776 | SINGLELINE_MODE.indentChars = "" 777 | SINGLELINE_MODE.simpleFieldSeperator = ":" 778 | 779 | const MULTILINE_MODE:PrintSetting = new PrintSetting 780 | MULTILINE_MODE.newLine = '\n'.charCodeAt() 781 | MULTILINE_MODE.indentChars = " " 782 | MULTILINE_MODE.simpleFieldSeperator = ": " 783 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/UInt64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | public final class UInt64 extends Binary64 { 12 | public final function set high(value:uint):void { 13 | internalHigh = value 14 | } 15 | public final function get high():uint { 16 | return internalHigh 17 | } 18 | public function UInt64(low:uint = 0, high:uint = 0) { 19 | super(low, high) 20 | } 21 | /** 22 | * Convert from Number. 23 | */ 24 | public static function fromNumber(n: Number):UInt64 { 25 | return new UInt64(n, Math.floor(n / 4294967296.0)) 26 | } 27 | /** 28 | * Convert to Number. 29 | */ 30 | public final function toNumber():Number { 31 | return (high * 4294967296.0) + low 32 | } 33 | public final function toString(radix:uint = 10):String { 34 | if (radix < 2 || radix > 36) { 35 | throw new ArgumentError 36 | } 37 | if (high == 0) { 38 | return low.toString(radix) 39 | } 40 | const digitChars:Array = []; 41 | const copyOfThis:UInt64 = new UInt64(low, high); 42 | do { 43 | const digit:uint = copyOfThis.div(radix); 44 | if (digit < 10) { 45 | digitChars.push(digit + CHAR_CODE_0); 46 | } else { 47 | digitChars.push(digit - 10 + CHAR_CODE_A); 48 | } 49 | } while (copyOfThis.high != 0) 50 | return copyOfThis.low.toString(radix) + 51 | String.fromCharCode.apply( 52 | String, digitChars.reverse()) 53 | } 54 | public static function parseUInt64(str:String, radix:uint = 0):UInt64 { 55 | var i:uint = 0 56 | if (radix == 0) { 57 | if (str.search(/^0x/) == 0) { 58 | radix = 16 59 | i = 2 60 | } else { 61 | radix = 10 62 | } 63 | } 64 | if (radix < 2 || radix > 36) { 65 | throw new ArgumentError 66 | } 67 | str = str.toLowerCase() 68 | const result:UInt64 = new UInt64 69 | for (; i < str.length; i++) { 70 | var digit:uint = str.charCodeAt(i) 71 | if (digit >= CHAR_CODE_0 && digit <= CHAR_CODE_9) { 72 | digit -= CHAR_CODE_0 73 | } else if (digit >= CHAR_CODE_A && digit <= CHAR_CODE_Z) { 74 | digit -= CHAR_CODE_A 75 | digit += 10 76 | } else { 77 | throw new ArgumentError 78 | } 79 | if (digit >= radix) { 80 | throw new ArgumentError 81 | } 82 | result.mul(radix) 83 | result.add(digit) 84 | } 85 | return result 86 | } 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/WireType.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | public final class WireType { 12 | public static const VARINT:uint = 0; 13 | public static const FIXED_64_BIT:uint = 1; 14 | public static const LENGTH_DELIMITED:uint = 2; 15 | public static const FIXED_32_BIT:uint = 5; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/WriteUtils.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // Copyright (c) 2012 , Yang Bo. All rights reserved. 5 | // 6 | // Author: Yang Bo (pop.atry@gmail.com) 7 | // 8 | // Use, modification and distribution are subject to the "New BSD License" 9 | // as listed at . 10 | 11 | package com.netease.protobuf { 12 | import flash.errors.* 13 | import flash.utils.* 14 | /** 15 | * @private 16 | */ 17 | public final class WriteUtils { 18 | private static function writeSingleUnknown(output:WritingBuffer, 19 | tag:uint, value:*):void { 20 | WriteUtils.write_TYPE_UINT32(output, tag) 21 | switch (tag & 7) { 22 | case WireType.VARINT: 23 | WriteUtils.write_TYPE_UINT64(output, value) 24 | break 25 | case WireType.FIXED_64_BIT: 26 | WriteUtils.write_TYPE_FIXED64(output, value) 27 | break 28 | case WireType.LENGTH_DELIMITED: 29 | WriteUtils.write_TYPE_BYTES(output, value) 30 | break 31 | case WireType.FIXED_32_BIT: 32 | WriteUtils.write_TYPE_FIXED32(output, value) 33 | break 34 | default: 35 | throw new IOError("Invalid wire type: " + (tag & 7)) 36 | } 37 | } 38 | 39 | public static function writeUnknownPair(output:WritingBuffer, tag:uint, 40 | value:*):void { 41 | const repeated:Array = value as Array 42 | if (repeated) { 43 | for each (var element:* in repeated) { 44 | writeSingleUnknown(output, tag, element) 45 | } 46 | } else { 47 | writeSingleUnknown(output, tag, value) 48 | } 49 | } 50 | 51 | private static function writeVarint64(output:WritingBuffer, 52 | low:uint, high:uint):void { 53 | if (high == 0) { 54 | write_TYPE_UINT32(output, low) 55 | } else { 56 | for (var i:uint = 0; i < 4; ++i) { 57 | output.writeByte((low & 0x7F) | 0x80) 58 | low >>>= 7 59 | } 60 | if ((high & (0xFFFFFFF << 3)) == 0) { 61 | output.writeByte((high << 4) | low) 62 | } else { 63 | output.writeByte((((high << 4) | low) & 0x7F) | 0x80) 64 | write_TYPE_UINT32(output, high >>> 3) 65 | } 66 | } 67 | } 68 | public static function writeTag(output:WritingBuffer, 69 | wireType:uint, number:uint):void { 70 | write_TYPE_UINT32(output, (number << 3) | wireType) 71 | } 72 | public static function write_TYPE_DOUBLE(output:WritingBuffer, value:Number):void { 73 | output.writeDouble(value) 74 | } 75 | public static function write_TYPE_FLOAT(output:WritingBuffer, value:Number):void { 76 | output.writeFloat(value) 77 | } 78 | public static function write_TYPE_INT64(output:WritingBuffer, value:Int64):void { 79 | writeVarint64(output, value.low, value.high) 80 | } 81 | public static function write_TYPE_UINT64(output:WritingBuffer, value:UInt64):void { 82 | writeVarint64(output, value.low, value.high) 83 | } 84 | public static function write_TYPE_INT32(output:WritingBuffer, value:int):void { 85 | if (value < 0) { 86 | writeVarint64(output, value, 0xFFFFFFFF) 87 | } else { 88 | write_TYPE_UINT32(output, value) 89 | } 90 | } 91 | public static function write_TYPE_FIXED64(output:WritingBuffer, value:UInt64):void { 92 | output.writeUnsignedInt(value.low) 93 | output.writeUnsignedInt(value.high) 94 | } 95 | public static function write_TYPE_FIXED32(output:WritingBuffer, value:uint):void { 96 | output.writeUnsignedInt(value) 97 | } 98 | public static function write_TYPE_BOOL(output:WritingBuffer, value:Boolean):void { 99 | output.writeByte(value ? 1 : 0) 100 | } 101 | public static function write_TYPE_STRING(output:WritingBuffer, value:String):void { 102 | const i:uint = output.beginBlock() 103 | output.writeUTFBytes(value) 104 | output.endBlock(i) 105 | } 106 | public static function write_TYPE_BYTES(output:WritingBuffer, value:ByteArray):void { 107 | write_TYPE_UINT32(output, value.length) 108 | output.writeBytes(value) 109 | } 110 | public static function write_TYPE_UINT32(output:WritingBuffer, value:uint):void { 111 | for (;;) { 112 | if (value < 0x80) { 113 | output.writeByte(value) 114 | return; 115 | } else { 116 | output.writeByte((value & 0x7F) | 0x80) 117 | value >>>= 7 118 | } 119 | } 120 | } 121 | public static function write_TYPE_ENUM(output:WritingBuffer, value:int):void { 122 | write_TYPE_INT32(output, value) 123 | } 124 | public static function write_TYPE_SFIXED32(output:WritingBuffer, value:int):void { 125 | output.writeInt(value) 126 | } 127 | public static function write_TYPE_SFIXED64(output:WritingBuffer, value:Int64):void { 128 | output.writeUnsignedInt(value.low) 129 | output.writeInt(value.high) 130 | } 131 | public static function write_TYPE_SINT32(output:WritingBuffer, value:int):void { 132 | write_TYPE_UINT32(output, ZigZag.encode32(value)) 133 | } 134 | public static function write_TYPE_SINT64(output:WritingBuffer, value:Int64):void { 135 | writeVarint64(output, 136 | ZigZag.encode64low(value.low, value.high), 137 | ZigZag.encode64high(value.low, value.high)) 138 | } 139 | public static function write_TYPE_MESSAGE(output:WritingBuffer, value:Message):void { 140 | const i:uint = output.beginBlock() 141 | value.used_by_generated_code::writeToBuffer(output) 142 | output.endBlock(i) 143 | } 144 | public static function writePackedRepeated(output:WritingBuffer, 145 | writeFunction:Function, value:Array):void { 146 | const i:uint = output.beginBlock() 147 | for (var j:uint = 0; j < value.length; j++) { 148 | writeFunction(output, value[j]) 149 | } 150 | output.endBlock(i) 151 | } 152 | } 153 | } 154 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/WritingBuffer.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import flash.errors.*; 12 | import flash.utils.*; 13 | /** 14 | * @private 15 | */ 16 | public final class WritingBuffer extends ByteArray { 17 | public function WritingBuffer() { 18 | endian = Endian.LITTLE_ENDIAN 19 | } 20 | /* 21 | // for Flash Player 9 22 | [ArrayElementType("uint")] 23 | private const slices:Array = [] 24 | /*/ 25 | // for Flash Player 10 26 | private const slices:Vector. = new Vector. 27 | //*/ 28 | public function beginBlock():uint { 29 | slices.push(position) 30 | const beginSliceIndex:uint = slices.length 31 | slices.length += 2 32 | slices.push(position) 33 | return beginSliceIndex 34 | } 35 | public function endBlock(beginSliceIndex:uint):void { 36 | slices.push(position) 37 | const beginPosition:uint = slices[beginSliceIndex + 2] 38 | slices[beginSliceIndex] = position 39 | WriteUtils.write_TYPE_UINT32(this, position - beginPosition) 40 | slices[beginSliceIndex + 1] = position 41 | slices.push(position) 42 | } 43 | public function toNormal(output:IDataOutput):void { 44 | var i:uint = 0 45 | var begin:uint = 0 46 | while (i < slices.length) { 47 | var end:uint = slices[i] 48 | ++i 49 | if (end > begin) { 50 | output.writeBytes(this, begin, end - begin) 51 | } else if (end < begin) { 52 | throw new IllegalOperationError 53 | } 54 | begin = slices[i] 55 | ++i 56 | } 57 | output.writeBytes(this, begin) 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/ZigZag.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | /** 12 | * @private 13 | */ 14 | public final class ZigZag { 15 | public static function encode32(n:int):int { 16 | return (n << 1) ^ (n >> 31) 17 | } 18 | public static function decode32(n:int):int { 19 | return (n >>> 1) ^ -(n & 1) 20 | } 21 | public static function encode64low(low:uint, high:int):uint { 22 | return (low << 1) ^ (high >> 31) 23 | } 24 | public static function encode64high(low:uint, high:int):int { 25 | return (low >>> 31) ^ (high << 1) ^ (high >> 31) 26 | } 27 | public static function decode64low(low:uint, high:int):uint { 28 | return (high << 31) ^ (low >>> 1) ^ -(low & 1) 29 | } 30 | public static function decode64high(low:uint, high:int):int { 31 | return (high >>> 1) ^ -(low & 1) 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_BOOL.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_BOOL extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_BOOL( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Boolean 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_BOOL(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_BOOL(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_BYTES.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_BYTES extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_BYTES( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return flash.utils.ByteArray 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_BYTES(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_BYTES(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_DOUBLE.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_DOUBLE extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_DOUBLE( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Number 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_DOUBLE(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_DOUBLE(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_ENUM.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_ENUM extends 17 | FieldDescriptor { 18 | public var enumType:Class 19 | public function FieldDescriptor_TYPE_ENUM( 20 | fullName:String, name:String, tag:uint, enumType:Class) { 21 | this.fullName = fullName 22 | this._name = name 23 | this.tag = tag 24 | this.enumType = enumType 25 | } 26 | override public function get type():Class { 27 | return int 28 | } 29 | override public function readSingleField(input:IDataInput):* { 30 | return ReadUtils.read_TYPE_ENUM(input) 31 | } 32 | override public function writeSingleField(output:WritingBuffer, 33 | value:*):void { 34 | WriteUtils.write_TYPE_ENUM(output, value) 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_FIXED32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_FIXED32 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_FIXED32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return int 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_FIXED32(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_FIXED32(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_FIXED64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_FIXED64 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_FIXED64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return UInt64 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_FIXED64(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_FIXED64(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_FLOAT.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_FLOAT extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_FLOAT( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Number 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_FLOAT(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_FLOAT(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_INT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_INT32 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_INT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return int 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_INT32(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_INT32(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_INT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_INT64 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_INT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Int64 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_INT64(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_INT64(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_MESSAGE.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_MESSAGE extends 17 | FieldDescriptor { 18 | public var messageUnion:Object 19 | public function FieldDescriptor_TYPE_MESSAGE( 20 | fullName:String, name:String, tag:uint, messageUnion:Object) { 21 | this.fullName = fullName 22 | this._name = name 23 | this.tag = tag 24 | this.messageUnion = messageUnion 25 | } 26 | override public function get type():Class { 27 | return (messageUnion as Class) || Class(messageUnion = messageUnion()) 28 | } 29 | override public function readSingleField(input:IDataInput):* { 30 | return ReadUtils.read_TYPE_MESSAGE(input, new type) 31 | } 32 | override public function writeSingleField(output:WritingBuffer, 33 | value:*):void { 34 | WriteUtils.write_TYPE_MESSAGE(output, value) 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_SFIXED32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_SFIXED32 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_SFIXED32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return int 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_SFIXED32(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_SFIXED32(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_SFIXED64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_SFIXED64 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_SFIXED64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Int64 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_SFIXED64(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_SFIXED64(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_SINT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_SINT32 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_SINT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return int 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_SINT32(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_SINT32(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_SINT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_SINT64 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_SINT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return Int64 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_SINT64(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_SINT64(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_STRING.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_STRING extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_STRING( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return String 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_STRING(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_STRING(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_UINT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_UINT32 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_UINT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return uint 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_UINT32(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_UINT32(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/FieldDescriptor_TYPE_UINT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class FieldDescriptor_TYPE_UINT64 extends 17 | FieldDescriptor { 18 | public function FieldDescriptor_TYPE_UINT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get type():Class { 25 | return UInt64 26 | } 27 | override public function readSingleField(input:IDataInput):* { 28 | return ReadUtils.read_TYPE_UINT64(input) 29 | } 30 | override public function writeSingleField(output:WritingBuffer, 31 | value:*):void { 32 | WriteUtils.write_TYPE_UINT64(output, value) 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_BOOL.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_BOOL extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_BOOL( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Boolean 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_BOOL(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_BOOL(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_BYTES.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_BYTES extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_BYTES( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.LENGTH_DELIMITED 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return flash.utils.ByteArray 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_BYTES(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_BYTES(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_DOUBLE.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_DOUBLE extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_DOUBLE( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_64_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Number 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_DOUBLE(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_DOUBLE(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_ENUM.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_ENUM extends 17 | RepeatedFieldDescriptor { 18 | public var enumType:Class 19 | public function RepeatedFieldDescriptor_TYPE_ENUM( 20 | fullName:String, name:String, tag:uint, enumType:Class) { 21 | this.fullName = fullName 22 | this._name = name 23 | this.tag = tag 24 | this.enumType = enumType 25 | } 26 | override public function get nonPackedWireType():int { 27 | return WireType.VARINT 28 | } 29 | override public function get type():Class { 30 | return Array 31 | } 32 | override public function get elementType():Class { 33 | return int 34 | } 35 | override public function readSingleField(input:IDataInput):* { 36 | return ReadUtils.read_TYPE_ENUM(input) 37 | } 38 | override public function writeSingleField(output:WritingBuffer, 39 | value:*):void { 40 | WriteUtils.write_TYPE_ENUM(output, value) 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_FIXED32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_FIXED32 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_FIXED32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_32_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return int 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_FIXED32(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_FIXED32(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_FIXED64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_FIXED64 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_FIXED64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_64_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Int64 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_FIXED64(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_FIXED64(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_FLOAT.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_FLOAT extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_FLOAT( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_32_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Number 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_FLOAT(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_FLOAT(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_INT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_INT32 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_INT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return int 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_INT32(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_INT32(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_INT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_INT64 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_INT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Int64 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_INT64(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_INT64(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_MESSAGE.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_MESSAGE extends 17 | RepeatedFieldDescriptor { 18 | public var messageUnion:Object 19 | public function RepeatedFieldDescriptor_TYPE_MESSAGE( 20 | fullName:String, name:String, tag:uint, messageUnion:Object) { 21 | this.fullName = fullName 22 | this._name = name 23 | this.tag = tag 24 | this.messageUnion = messageUnion 25 | } 26 | override public function get nonPackedWireType():int { 27 | return WireType.LENGTH_DELIMITED 28 | } 29 | override public function get type():Class { 30 | return Array 31 | } 32 | override public function get elementType():Class { 33 | return (messageUnion as Class) || Class(messageUnion = messageUnion()) 34 | } 35 | override public function readSingleField(input:IDataInput):* { 36 | return ReadUtils.read_TYPE_MESSAGE(input, new elementType) 37 | } 38 | override public function writeSingleField(output:WritingBuffer, 39 | value:*):void { 40 | WriteUtils.write_TYPE_MESSAGE(output, value) 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_SFIXED32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_SFIXED32 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_SFIXED32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_32_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return int 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_SFIXED32(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_SFIXED32(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_SFIXED64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_SFIXED64 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_SFIXED64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.FIXED_64_BIT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Int64 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_SFIXED64(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_SFIXED64(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_SINT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_SINT32 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_SINT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return int 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_SINT32(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_SINT32(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_SINT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_SINT64 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_SINT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return Int64 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_SINT64(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_SINT64(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_STRING.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_STRING extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_STRING( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.LENGTH_DELIMITED 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return String 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_STRING(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_STRING(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_UINT32.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_UINT32 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_UINT32( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return uint 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_UINT32(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_UINT32(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/fieldDescriptors/RepeatedFieldDescriptor_TYPE_UINT64.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2011 , Yang Bo All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf.fieldDescriptors { 11 | import com.netease.protobuf.* 12 | import flash.utils.* 13 | /** 14 | * @private 15 | */ 16 | public final class RepeatedFieldDescriptor_TYPE_UINT64 extends 17 | RepeatedFieldDescriptor { 18 | public function RepeatedFieldDescriptor_TYPE_UINT64( 19 | fullName:String, name:String, tag:uint) { 20 | this.fullName = fullName 21 | this._name = name 22 | this.tag = tag 23 | } 24 | override public function get nonPackedWireType():int { 25 | return WireType.VARINT 26 | } 27 | override public function get type():Class { 28 | return Array 29 | } 30 | override public function get elementType():Class { 31 | return UInt64 32 | } 33 | override public function readSingleField(input:IDataInput):* { 34 | return ReadUtils.read_TYPE_UINT64(input) 35 | } 36 | override public function writeSingleField(output:WritingBuffer, 37 | value:*):void { 38 | WriteUtils.write_TYPE_UINT64(output, value) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/stringToByteArray.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | import flash.utils.ByteArray 12 | /** 13 | * @private 14 | */ 15 | public function stringToByteArray(s:String):ByteArray { 16 | const ba:ByteArray = new ByteArray 17 | ba.length = s.length 18 | for (var i:uint = 0; i < s.length; ++i) { 19 | ba[i] = s.charCodeAt(i) 20 | } 21 | return ba 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /as3/com/netease/protobuf/used_by_generated_code.as: -------------------------------------------------------------------------------- 1 | // vim: tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2012 , Yang Bo. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protobuf { 11 | /** 12 | * @private 13 | */ 14 | public namespace used_by_generated_code 15 | } 16 | -------------------------------------------------------------------------------- /compiler/com/netease/protocGenAs3/Main.java: -------------------------------------------------------------------------------- 1 | // vim: fileencoding=utf-8 tabstop=4 shiftwidth=4 2 | 3 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 4 | // 5 | // Author: Yang Bo (pop.atry@gmail.com) 6 | // 7 | // Use, modification and distribution are subject to the "New BSD License" 8 | // as listed at . 9 | 10 | package com.netease.protocGenAs3; 11 | import static google.protobuf.compiler.Plugin.*; 12 | import static com.google.protobuf.DescriptorProtos.*; 13 | import com.google.protobuf.*; 14 | import java.io.*; 15 | import java.util.*; 16 | import java.math.*; 17 | public final class Main { 18 | private static final String[] ACTIONSCRIPT_KEYWORDS = { 19 | "as", "break", "case", "catch", "class", "const", "continue", "default", 20 | "delete", "do", "else", "extends", "false", "finally", "for", 21 | "function", "if", "implements", "import", "in", "instanceof", 22 | "interface", "internal", "is", "native", "new", "null", "package", 23 | "private", "protected", "public", "return", "super", "switch", "this", 24 | "throw", "to", "true", "try", "typeof", "use", "var", "void", "while", 25 | "with" 26 | }; 27 | private static final class Scope { 28 | // 如果 proto instanceOf Scope ,则这个 Scope 是对另一 Scope 的引用 29 | public final String fullName; 30 | public final Scope parent; 31 | public final Proto proto; 32 | public final boolean export; 33 | public final HashMap> children = 34 | new HashMap>(); 35 | private Scope find(String[] pathElements, int i) { 36 | Scope result = this; 37 | for (; i < pathElements.length; i++) { 38 | String name = pathElements[i]; 39 | if (result.children.containsKey(name)) { 40 | result = result.children.get(name); 41 | } else { 42 | return null; 43 | } 44 | } 45 | while (result.proto instanceof Scope) { 46 | result = (Scope)result.proto; 47 | } 48 | return result; 49 | } 50 | public boolean isRoot() { 51 | return parent == null; 52 | } 53 | private Scope getRoot() { 54 | Scope scope = this; 55 | while (!scope.isRoot()) { 56 | scope = scope.parent; 57 | } 58 | return scope; 59 | } 60 | public Scope find(String path) { 61 | String[] pathElements = path.split("\\."); 62 | if (pathElements[0].equals("")) { 63 | return getRoot().find(pathElements, 1); 64 | } else { 65 | for (Scope scope = this; scope != null; scope = scope.parent) { 66 | Scope result = scope.find(pathElements, 0); 67 | if (result != null) { 68 | return result; 69 | } 70 | } 71 | return null; 72 | } 73 | } 74 | private Scope findOrCreate(String[] pathElements, int i) { 75 | Scope scope = this; 76 | for (; i < pathElements.length; i++) { 77 | String name = pathElements[i]; 78 | if (scope.children.containsKey(name)) { 79 | scope = scope.children.get(name); 80 | } else { 81 | Scope child = 82 | new Scope(scope, null, false, name); 83 | scope.children.put(name, child); 84 | scope = child; 85 | } 86 | } 87 | return scope; 88 | } 89 | public Scope findOrCreate(String path) { 90 | String[] pathElements = path.split("\\."); 91 | if (pathElements[0].equals("")) { 92 | return getRoot().findOrCreate(pathElements, 1); 93 | } else { 94 | return findOrCreate(pathElements, 0); 95 | } 96 | } 97 | private Scope(Scope parent, Proto proto, boolean export, 98 | String name) { 99 | this.parent = parent; 100 | this.proto = proto; 101 | this.export = export; 102 | if (isRoot() || parent.isRoot()) { 103 | fullName = name; 104 | } else { 105 | fullName = parent.fullName + '.' + name; 106 | } 107 | } 108 | public Scope addChild( 109 | String name, ChildProto proto, boolean export) { 110 | assert(name != null); 111 | assert(!name.equals("")); 112 | Scope child = 113 | new Scope(this, proto, export, name); 114 | if(children.containsKey(child)) { 115 | throw new IllegalArgumentException(); 116 | } 117 | children.put(name, child); 118 | return child; 119 | } 120 | public static Scope newRoot() { 121 | return new Scope(null, null, false, ""); 122 | } 123 | } 124 | private static void addServiceToScope(Scope scope, 125 | ServiceDescriptorProto sdp, boolean export) { 126 | scope.addChild(sdp.getName(), sdp, export); 127 | } 128 | private static void addExtensionToScope(Scope scope, 129 | FieldDescriptorProto efdp, boolean export) { 130 | scope.addChild(efdp.getName().toUpperCase(), efdp, export); 131 | } 132 | private static void addEnumToScope(Scope scope, EnumDescriptorProto edp, 133 | boolean export) { 134 | assert(edp.hasName()); 135 | Scope enumScope = 136 | scope.addChild(edp.getName(), edp, export); 137 | for (EnumValueDescriptorProto evdp : edp.getValueList()) { 138 | Scope enumValueScope = 139 | enumScope.addChild(evdp.getName(), evdp, false); 140 | scope.addChild(evdp.getName(), enumValueScope, false); 141 | } 142 | } 143 | private static void addMessageToScope(Scope scope, DescriptorProto dp, 144 | boolean export) { 145 | Scope messageScope = 146 | scope.addChild(dp.getName(), dp, export); 147 | for (EnumDescriptorProto edp : dp.getEnumTypeList()) { 148 | addEnumToScope(messageScope, edp, export); 149 | } 150 | for (DescriptorProto nested: dp.getNestedTypeList()) { 151 | addMessageToScope(messageScope, nested, export); 152 | } 153 | } 154 | private static Scope buildScopeTree(CodeGeneratorRequest request) { 155 | Scope root = Scope.newRoot(); 156 | List filesToGenerate = request.getFileToGenerateList(); 157 | for (FileDescriptorProto fdp : request.getProtoFileList()) { 158 | Scope packageScope = fdp.hasPackage() ? 159 | root.findOrCreate(fdp.getPackage()) : root; 160 | boolean export = filesToGenerate.contains(fdp.getName()); 161 | for (ServiceDescriptorProto sdp : fdp.getServiceList()) { 162 | addServiceToScope(packageScope, sdp, export); 163 | } 164 | for (FieldDescriptorProto efdp : fdp.getExtensionList()) { 165 | addExtensionToScope(packageScope, efdp, export); 166 | } 167 | for (EnumDescriptorProto edp : fdp.getEnumTypeList()) { 168 | addEnumToScope(packageScope, edp, export); 169 | } 170 | for (DescriptorProto dp : fdp.getMessageTypeList()) { 171 | addMessageToScope(packageScope, dp, export); 172 | } 173 | } 174 | return root; 175 | } 176 | private static String getImportType(Scope scope, 177 | FieldDescriptorProto fdp) { 178 | switch (fdp.getType()) { 179 | case TYPE_ENUM: 180 | case TYPE_MESSAGE: 181 | Scope typeScope = scope.find(fdp.getTypeName()); 182 | if (typeScope == null) { 183 | throw new IllegalArgumentException( 184 | fdp.getTypeName() + " not found."); 185 | } 186 | return typeScope.fullName; 187 | case TYPE_BYTES: 188 | return "flash.utils.ByteArray"; 189 | default: 190 | return null; 191 | } 192 | } 193 | private static boolean isValueType(FieldDescriptorProto.Type type) { 194 | switch (type) { 195 | case TYPE_DOUBLE: 196 | case TYPE_FLOAT: 197 | case TYPE_INT32: 198 | case TYPE_FIXED32: 199 | case TYPE_BOOL: 200 | case TYPE_UINT32: 201 | case TYPE_SFIXED32: 202 | case TYPE_SINT32: 203 | case TYPE_ENUM: 204 | return true; 205 | default: 206 | return false; 207 | } 208 | } 209 | static final int VARINT = 0; 210 | static final int FIXED_64_BIT = 1; 211 | static final int LENGTH_DELIMITED = 2; 212 | static final int FIXED_32_BIT = 5; 213 | private static int getWireType( 214 | FieldDescriptorProto.Type type) { 215 | switch (type) { 216 | case TYPE_DOUBLE: 217 | case TYPE_FIXED64: 218 | case TYPE_SFIXED64: 219 | return FIXED_64_BIT; 220 | case TYPE_FLOAT: 221 | case TYPE_FIXED32: 222 | case TYPE_SFIXED32: 223 | return FIXED_32_BIT; 224 | case TYPE_INT32: 225 | case TYPE_SINT32: 226 | case TYPE_UINT32: 227 | case TYPE_BOOL: 228 | case TYPE_INT64: 229 | case TYPE_UINT64: 230 | case TYPE_SINT64: 231 | case TYPE_ENUM: 232 | return VARINT; 233 | case TYPE_STRING: 234 | case TYPE_MESSAGE: 235 | case TYPE_BYTES: 236 | return LENGTH_DELIMITED; 237 | default: 238 | throw new IllegalArgumentException(); 239 | } 240 | } 241 | private static String getActionScript3WireType( 242 | FieldDescriptorProto.Type type) { 243 | switch (type) { 244 | case TYPE_DOUBLE: 245 | case TYPE_FIXED64: 246 | case TYPE_SFIXED64: 247 | return "FIXED_64_BIT"; 248 | case TYPE_FLOAT: 249 | case TYPE_FIXED32: 250 | case TYPE_SFIXED32: 251 | return "FIXED_32_BIT"; 252 | case TYPE_INT32: 253 | case TYPE_SINT32: 254 | case TYPE_UINT32: 255 | case TYPE_BOOL: 256 | case TYPE_INT64: 257 | case TYPE_UINT64: 258 | case TYPE_SINT64: 259 | case TYPE_ENUM: 260 | return "VARINT"; 261 | case TYPE_STRING: 262 | case TYPE_MESSAGE: 263 | case TYPE_BYTES: 264 | return "LENGTH_DELIMITED"; 265 | default: 266 | throw new IllegalArgumentException(); 267 | } 268 | } 269 | private static String getActionScript3LogicType(Scope scope, 270 | FieldDescriptorProto fdp) { 271 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_ENUM) { 272 | Scope typeScope = scope.find(fdp.getTypeName()); 273 | if (typeScope == null) { 274 | throw new IllegalArgumentException( 275 | fdp.getTypeName() + " not found."); 276 | } 277 | if (typeScope == scope) { 278 | // workaround for mxmlc's bug. 279 | return typeScope.fullName.substring( 280 | typeScope.fullName.lastIndexOf('.') + 1); 281 | } 282 | return typeScope.fullName; 283 | } else { 284 | return getActionScript3Type(scope, fdp); 285 | } 286 | } 287 | private static String getActionScript3Type(Scope scope, 288 | FieldDescriptorProto fdp) { 289 | switch (fdp.getType()) { 290 | case TYPE_DOUBLE: 291 | case TYPE_FLOAT: 292 | return "Number"; 293 | case TYPE_INT32: 294 | case TYPE_SFIXED32: 295 | case TYPE_SINT32: 296 | case TYPE_ENUM: 297 | return "int"; 298 | case TYPE_UINT32: 299 | case TYPE_FIXED32: 300 | return "uint"; 301 | case TYPE_BOOL: 302 | return "Boolean"; 303 | case TYPE_INT64: 304 | case TYPE_SFIXED64: 305 | case TYPE_SINT64: 306 | return "Int64"; 307 | case TYPE_UINT64: 308 | case TYPE_FIXED64: 309 | return "UInt64"; 310 | case TYPE_STRING: 311 | return "String"; 312 | case TYPE_MESSAGE: 313 | Scope typeScope = scope.find(fdp.getTypeName()); 314 | if (typeScope == null) { 315 | throw new IllegalArgumentException( 316 | fdp.getTypeName() + " not found."); 317 | } 318 | if (typeScope == scope) { 319 | // workaround for mxmlc's bug. 320 | return typeScope.fullName.substring( 321 | typeScope.fullName.lastIndexOf('.') + 1); 322 | } 323 | return typeScope.fullName; 324 | case TYPE_BYTES: 325 | return "flash.utils.ByteArray"; 326 | default: 327 | throw new IllegalArgumentException(); 328 | } 329 | } 330 | private static void appendQuotedString(StringBuilder sb, String value) { 331 | sb.append('\"'); 332 | for (int i = 0; i < value.length(); i++) { 333 | char c = value.charAt(i); 334 | switch (c) { 335 | case '\"': 336 | case '\\': 337 | sb.append('\\'); 338 | sb.append(c); 339 | break; 340 | default: 341 | if (c >= 128 || Character.isISOControl(c)) { 342 | sb.append("\\u"); 343 | sb.append(String.format("%04X", new Integer(c))); 344 | } else { 345 | sb.append(c); 346 | } 347 | } 348 | } 349 | sb.append('\"'); 350 | } 351 | private static void appendDefaultValue(StringBuilder sb, Scope scope, 352 | FieldDescriptorProto fdp) { 353 | String value = fdp.getDefaultValue(); 354 | switch (fdp.getType()) { 355 | case TYPE_DOUBLE: 356 | case TYPE_FLOAT: 357 | if (value.equals("nan")) { 358 | sb.append("NaN"); 359 | } else if (value.equals("inf")) { 360 | sb.append("Infinity"); 361 | } else if (value.equals("-inf")) { 362 | sb.append("-Infinity"); 363 | } else { 364 | sb.append(value); 365 | } 366 | break; 367 | case TYPE_UINT64: 368 | case TYPE_FIXED64: 369 | { 370 | long v = new BigInteger(value).longValue(); 371 | sb.append("new UInt64("); 372 | sb.append(Long.toString(v & 0xFFFFFFFFL)); 373 | sb.append(", "); 374 | sb.append(Long.toString((v >>> 32) & 0xFFFFFFFFL)); 375 | sb.append(")"); 376 | } 377 | break; 378 | case TYPE_INT64: 379 | case TYPE_SFIXED64: 380 | case TYPE_SINT64: 381 | { 382 | long v = Long.parseLong(value); 383 | sb.append("new Int64("); 384 | sb.append(Long.toString(v & 0xFFFFFFFFL)); 385 | sb.append(", "); 386 | sb.append(Integer.toString((int)v >>> 32)); 387 | sb.append(")"); 388 | } 389 | break; 390 | case TYPE_INT32: 391 | case TYPE_FIXED32: 392 | case TYPE_SFIXED32: 393 | case TYPE_SINT32: 394 | case TYPE_UINT32: 395 | case TYPE_BOOL: 396 | sb.append(value); 397 | break; 398 | case TYPE_STRING: 399 | appendQuotedString(sb, value); 400 | break; 401 | case TYPE_ENUM: 402 | sb.append(scope.find(fdp.getTypeName()). 403 | children.get(value).fullName); 404 | break; 405 | case TYPE_BYTES: 406 | sb.append("stringToByteArray("); 407 | sb.append("\""); 408 | sb.append(value); 409 | sb.append("\")"); 410 | break; 411 | default: 412 | throw new IllegalArgumentException(); 413 | } 414 | } 415 | private static void appendLowerCamelCase(StringBuilder sb, String s) { 416 | if (Arrays.binarySearch(ACTIONSCRIPT_KEYWORDS, s) >= 0) { 417 | sb.append("__"); 418 | } 419 | sb.append(Character.toLowerCase(s.charAt(0))); 420 | boolean upper = false; 421 | for (int i = 1; i < s.length(); i++) { 422 | char c = s.charAt(i); 423 | if (upper) { 424 | if (Character.isLowerCase(c)) { 425 | sb.append(Character.toUpperCase(c)); 426 | upper = false; 427 | continue; 428 | } else { 429 | sb.append('_'); 430 | } 431 | } 432 | upper = c == '_'; 433 | if (!upper) { 434 | sb.append(c); 435 | } 436 | } 437 | } 438 | private static void appendUpperCamelCase(StringBuilder sb, String s) { 439 | sb.append(Character.toUpperCase(s.charAt(0))); 440 | boolean upper = false; 441 | for (int i = 1; i < s.length(); i++) { 442 | char c = s.charAt(i); 443 | if (upper) { 444 | if (Character.isLowerCase(c)) { 445 | sb.append(Character.toUpperCase(c)); 446 | upper = false; 447 | continue; 448 | } else { 449 | sb.append('_'); 450 | } 451 | } 452 | upper = c == '_'; 453 | if (!upper) { 454 | sb.append(c); 455 | } 456 | } 457 | } 458 | private static void writeMessage(Scope scope, 459 | StringBuilder content, StringBuilder initializerContent) { 460 | content.append("\timport com.netease.protobuf.*;\n"); 461 | content.append("\tuse namespace com.netease.protobuf.used_by_generated_code;\n"); 462 | content.append("\timport com.netease.protobuf.fieldDescriptors.*;\n"); 463 | content.append("\timport flash.utils.Endian;\n"); 464 | content.append("\timport flash.utils.IDataInput;\n"); 465 | content.append("\timport flash.utils.IDataOutput;\n"); 466 | content.append("\timport flash.utils.IExternalizable;\n"); 467 | content.append("\timport flash.errors.IOError;\n"); 468 | HashSet importTypes = new HashSet(); 469 | for (FieldDescriptorProto efdp : scope.proto.getExtensionList()) { 470 | importTypes.add(scope.find(efdp.getExtendee()).fullName); 471 | if (efdp.getType().equals(FieldDescriptorProto.Type.TYPE_MESSAGE)) { 472 | importTypes.add(scope.find(efdp.getTypeName()).fullName); 473 | } 474 | String importType = getImportType(scope, efdp); 475 | if (importType != null) { 476 | importTypes.add(importType); 477 | } 478 | } 479 | for (FieldDescriptorProto fdp : scope.proto.getFieldList()) { 480 | String importType = getImportType(scope, fdp); 481 | if (importType != null) { 482 | importTypes.add(importType); 483 | } 484 | } 485 | for (String importType : importTypes) { 486 | content.append("\timport "); 487 | content.append(importType); 488 | content.append(";\n"); 489 | } 490 | content.append("\t// @@protoc_insertion_point(imports)\n\n"); 491 | String remoteClassAlias; 492 | if (scope.proto.hasOptions()) { 493 | if (scope.proto.getOptions().hasExtension(Options.as3AmfAlias)) { 494 | remoteClassAlias = scope.proto.getOptions().getExtension(Options.as3AmfAlias); 495 | } else if (scope.proto.getOptions().getExtension(Options.as3AmfAutoAlias)) { 496 | remoteClassAlias = scope.fullName; 497 | } else { 498 | remoteClassAlias = null; 499 | } 500 | if (remoteClassAlias != null) { 501 | content.append("\t[RemoteClass(alias="); 502 | appendQuotedString(content, remoteClassAlias); 503 | content.append(")]\n"); 504 | } 505 | if (scope.proto.getOptions().getExtension(Options.as3Bindable)) { 506 | content.append("\t[Bindable]\n"); 507 | } 508 | } else { 509 | remoteClassAlias = null; 510 | } 511 | content.append("\t// @@protoc_insertion_point(class_metadata)\n"); 512 | content.append("\tpublic dynamic final class "); 513 | content.append(scope.proto.getName()); 514 | content.append(" extends com.netease.protobuf.Message"); 515 | if (remoteClassAlias != null) { 516 | content.append(" implements flash.utils.IExternalizable {\n"); 517 | content.append("\t\tpublic final function writeExternal(output:flash.utils.IDataOutput):void {\n"); 518 | content.append("\t\t\twriteDelimitedTo(output);\n"); 519 | content.append("\t\t}\n\n"); 520 | content.append("\t\tpublic final function readExternal(input:flash.utils.IDataInput):void {\n"); 521 | content.append("\t\t\tmergeDelimitedFrom(input);\n"); 522 | content.append("\t\t}\n\n"); 523 | } else { 524 | content.append(" {\n"); 525 | } 526 | if (scope.proto.getExtensionRangeCount() > 0) { 527 | content.append("\t\t[ArrayElementType(\"Function\")]\n"); 528 | content.append("\t\tpublic static const extensionReadFunctions:Array = [];\n\n"); 529 | } 530 | if (scope.proto.getExtensionCount() > 0) { 531 | initializerContent.append("import "); 532 | initializerContent.append(scope.fullName); 533 | initializerContent.append(";\n"); 534 | initializerContent.append("if(!"); 535 | initializerContent.append(scope.fullName); 536 | initializerContent.append(") throw new Error();\n"); 537 | } 538 | for (FieldDescriptorProto efdp : scope.proto.getExtensionList()) { 539 | if (efdp.getType() == FieldDescriptorProto.Type.TYPE_GROUP) { 540 | System.err.println("Warning: Group is not supported."); 541 | continue; 542 | } 543 | String extendee = scope.find(efdp.getExtendee()).fullName; 544 | content.append("\t\t/**\n\t\t * @private\n\t\t */\n"); 545 | content.append("\t\tpublic static const "); 546 | content.append(efdp.getName().toUpperCase()); 547 | content.append(":"); 548 | appendFieldDescriptorClass(content, efdp); 549 | content.append(" = "); 550 | appendFieldDescriptor(content, scope, efdp); 551 | content.append(";\n\n"); 552 | if (efdp.hasDefaultValue()) { 553 | content.append("\t\t"); 554 | content.append(extendee); 555 | content.append(".prototype["); 556 | content.append(efdp.getName().toUpperCase()); 557 | content.append("] = "); 558 | appendDefaultValue(content, scope, efdp); 559 | content.append(";\n\n"); 560 | } 561 | appendExtensionReadFunction(content, "\t\t", scope, efdp); 562 | } 563 | int valueTypeCount = 0; 564 | for (FieldDescriptorProto fdp : scope.proto.getFieldList()) { 565 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_GROUP) { 566 | System.err.println("Warning: Group is not supported."); 567 | continue; 568 | } 569 | content.append("\t\t/**\n\t\t * @private\n\t\t */\n"); 570 | content.append("\t\tpublic static const "); 571 | content.append(fdp.getName().toUpperCase()); 572 | content.append(":"); 573 | appendFieldDescriptorClass(content, fdp); 574 | content.append(" = "); 575 | appendFieldDescriptor(content, scope, fdp); 576 | content.append(";\n\n"); 577 | assert(fdp.hasLabel()); 578 | switch (fdp.getLabel()) { 579 | case LABEL_OPTIONAL: 580 | content.append("\t\tprivate var "); 581 | content.append(fdp.getName()); 582 | content.append("$field:"); 583 | content.append(getActionScript3Type(scope, fdp)); 584 | content.append(";\n\n"); 585 | 586 | if (isValueType(fdp.getType())) { 587 | final int valueTypeId = valueTypeCount++; 588 | final int valueTypeField = valueTypeId / 32; 589 | final int valueTypeBit = valueTypeId % 32; 590 | if (valueTypeBit == 0) { 591 | content.append("\t\tprivate var hasField$"); 592 | content.append(valueTypeField); 593 | content.append(":uint = 0;\n\n"); 594 | } 595 | 596 | content.append("\t\tpublic function clear"); 597 | appendUpperCamelCase(content, fdp.getName()); 598 | content.append("():void {\n"); 599 | content.append("\t\t\thasField$"); 600 | content.append(valueTypeField); 601 | content.append(" &= 0x"); 602 | content.append(Integer.toHexString(~(1 << valueTypeBit))); 603 | content.append(";\n"); 604 | 605 | content.append("\t\t\t"); 606 | content.append(fdp.getName()); 607 | content.append("$field = new "); 608 | content.append(getActionScript3Type(scope, fdp)); 609 | content.append("();\n"); 610 | content.append("\t\t}\n\n"); 611 | 612 | content.append("\t\tpublic function get has"); 613 | appendUpperCamelCase(content, fdp.getName()); 614 | content.append("():Boolean {\n"); 615 | content.append("\t\t\treturn (hasField$"); 616 | content.append(valueTypeField); 617 | content.append(" & 0x"); 618 | content.append(Integer.toHexString(1 << valueTypeBit)); 619 | content.append(") != 0;\n"); 620 | content.append("\t\t}\n\n"); 621 | 622 | content.append("\t\tpublic function set "); 623 | appendLowerCamelCase(content, fdp.getName()); 624 | content.append("(value:"); 625 | content.append(getActionScript3Type(scope, fdp)); 626 | content.append("):void {\n"); 627 | content.append("\t\t\thasField$"); 628 | content.append(valueTypeField); 629 | content.append(" |= 0x"); 630 | content.append(Integer.toHexString(1 << valueTypeBit)); 631 | content.append(";\n"); 632 | content.append("\t\t\t"); 633 | content.append(fdp.getName()); 634 | content.append("$field = value;\n"); 635 | content.append("\t\t}\n\n"); 636 | } else { 637 | content.append("\t\tpublic function clear"); 638 | appendUpperCamelCase(content, fdp.getName()); 639 | content.append("():void {\n"); 640 | content.append("\t\t\t"); 641 | content.append(fdp.getName()); 642 | content.append("$field = null;\n"); 643 | content.append("\t\t}\n\n"); 644 | 645 | content.append("\t\tpublic function get has"); 646 | appendUpperCamelCase(content, fdp.getName()); 647 | content.append("():Boolean {\n"); 648 | content.append("\t\t\treturn "); 649 | content.append(fdp.getName()); 650 | content.append("$field != null;\n"); 651 | content.append("\t\t}\n\n"); 652 | 653 | content.append("\t\tpublic function set "); 654 | appendLowerCamelCase(content, fdp.getName()); 655 | content.append("(value:"); 656 | content.append(getActionScript3Type(scope, fdp)); 657 | content.append("):void {\n"); 658 | content.append("\t\t\t"); 659 | content.append(fdp.getName()); 660 | content.append("$field = value;\n"); 661 | content.append("\t\t}\n\n"); 662 | } 663 | 664 | content.append("\t\tpublic function get "); 665 | appendLowerCamelCase(content, fdp.getName()); 666 | content.append("():"); 667 | content.append(getActionScript3Type(scope, fdp)); 668 | content.append(" {\n"); 669 | if (fdp.hasDefaultValue()) { 670 | content.append("\t\t\tif(!has"); 671 | appendUpperCamelCase(content, fdp.getName()); 672 | content.append(") {\n"); 673 | content.append("\t\t\t\treturn "); 674 | appendDefaultValue(content, scope, fdp); 675 | content.append(";\n"); 676 | content.append("\t\t\t}\n"); 677 | } 678 | content.append("\t\t\treturn "); 679 | content.append(fdp.getName()); 680 | content.append("$field;\n"); 681 | content.append("\t\t}\n\n"); 682 | break; 683 | case LABEL_REQUIRED: 684 | content.append("\t\tpublic var "); 685 | appendLowerCamelCase(content, fdp.getName()); 686 | content.append(":"); 687 | content.append(getActionScript3Type(scope, fdp)); 688 | if (fdp.hasDefaultValue()) { 689 | content.append(" = "); 690 | appendDefaultValue(content, scope, fdp); 691 | } 692 | content.append(";\n\n"); 693 | break; 694 | case LABEL_REPEATED: 695 | content.append("\t\t[ArrayElementType(\""); 696 | content.append(getActionScript3Type(scope, fdp)); 697 | content.append("\")]\n"); 698 | content.append("\t\tpublic var "); 699 | appendLowerCamelCase(content, fdp.getName()); 700 | content.append(":Array = [];\n\n"); 701 | break; 702 | default: 703 | throw new IllegalArgumentException(); 704 | } 705 | } 706 | content.append("\t\t/**\n\t\t * @private\n\t\t */\n\t\toverride com.netease.protobuf.used_by_generated_code final function writeToBuffer(output:com.netease.protobuf.WritingBuffer):void {\n"); 707 | for (FieldDescriptorProto fdp : scope.proto.getFieldList()) { 708 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_GROUP) { 709 | System.err.println("Warning: Group is not supported."); 710 | continue; 711 | } 712 | switch (fdp.getLabel()) { 713 | case LABEL_OPTIONAL: 714 | content.append("\t\t\tif ("); 715 | content.append("has"); 716 | appendUpperCamelCase(content, fdp.getName()); 717 | content.append(") {\n"); 718 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.writeTag(output, com.netease.protobuf.WireType."); 719 | content.append(getActionScript3WireType(fdp.getType())); 720 | content.append(", "); 721 | content.append(Integer.toString(fdp.getNumber())); 722 | content.append(");\n"); 723 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.write_"); 724 | content.append(fdp.getType().name()); 725 | content.append("(output, "); 726 | content.append(fdp.getName()); 727 | content.append("$field);\n"); 728 | content.append("\t\t\t}\n"); 729 | break; 730 | case LABEL_REQUIRED: 731 | content.append("\t\t\tcom.netease.protobuf.WriteUtils.writeTag(output, com.netease.protobuf.WireType."); 732 | content.append(getActionScript3WireType(fdp.getType())); 733 | content.append(", "); 734 | content.append(Integer.toString(fdp.getNumber())); 735 | content.append(");\n"); 736 | content.append("\t\t\tcom.netease.protobuf.WriteUtils.write_"); 737 | content.append(fdp.getType().name()); 738 | content.append("(output, this."); 739 | appendLowerCamelCase(content, fdp.getName()); 740 | content.append(");\n"); 741 | break; 742 | case LABEL_REPEATED: 743 | if (fdp.hasOptions() && fdp.getOptions().getPacked()) { 744 | content.append("\t\t\tif (this."); 745 | appendLowerCamelCase(content, fdp.getName()); 746 | content.append(" != null && this."); 747 | appendLowerCamelCase(content, fdp.getName()); 748 | content.append(".length > 0) {\n"); 749 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.writeTag(output, com.netease.protobuf.WireType.LENGTH_DELIMITED, "); 750 | content.append(Integer.toString(fdp.getNumber())); 751 | content.append(");\n"); 752 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.writePackedRepeated(output, com.netease.protobuf.WriteUtils.write_"); 753 | content.append(fdp.getType().name()); 754 | content.append(", this."); 755 | appendLowerCamelCase(content, fdp.getName()); 756 | content.append(");\n"); 757 | content.append("\t\t\t}\n"); 758 | } else { 759 | content.append("\t\t\tfor (var "); 760 | appendLowerCamelCase(content, fdp.getName()); 761 | content.append("$index:uint = 0; "); 762 | appendLowerCamelCase(content, fdp.getName()); 763 | content.append("$index < this."); 764 | appendLowerCamelCase(content, fdp.getName()); 765 | content.append(".length; ++"); 766 | appendLowerCamelCase(content, fdp.getName()); 767 | content.append("$index) {\n"); 768 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.writeTag(output, com.netease.protobuf.WireType."); 769 | content.append(getActionScript3WireType(fdp.getType())); 770 | content.append(", "); 771 | content.append(Integer.toString(fdp.getNumber())); 772 | content.append(");\n"); 773 | content.append("\t\t\t\tcom.netease.protobuf.WriteUtils.write_"); 774 | content.append(fdp.getType().name()); 775 | content.append("(output, this."); 776 | appendLowerCamelCase(content, fdp.getName()); 777 | content.append("["); 778 | appendLowerCamelCase(content, fdp.getName()); 779 | content.append("$index]);\n"); 780 | content.append("\t\t\t}\n"); 781 | } 782 | break; 783 | } 784 | } 785 | content.append("\t\t\tfor (var fieldKey:* in this) {\n"); 786 | if (scope.proto.getExtensionRangeCount() > 0) { 787 | content.append("\t\t\t\tsuper.writeExtensionOrUnknown(output, fieldKey);\n"); 788 | } else { 789 | content.append("\t\t\t\tsuper.writeUnknown(output, fieldKey);\n"); 790 | } 791 | content.append("\t\t\t}\n"); 792 | content.append("\t\t}\n\n"); 793 | content.append("\t\t/**\n\t\t * @private\n\t\t */\n"); 794 | content.append("\t\toverride com.netease.protobuf.used_by_generated_code final function readFromSlice(input:flash.utils.IDataInput, bytesAfterSlice:uint):void {\n"); 795 | for (FieldDescriptorProto fdp : scope.proto.getFieldList()) { 796 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_GROUP) { 797 | System.err.println("Warning: Group is not supported."); 798 | continue; 799 | } 800 | switch (fdp.getLabel()) { 801 | case LABEL_OPTIONAL: 802 | case LABEL_REQUIRED: 803 | content.append("\t\t\tvar "); 804 | content.append(fdp.getName()); 805 | content.append("$count:uint = 0;\n"); 806 | break; 807 | } 808 | } 809 | content.append("\t\t\twhile (input.bytesAvailable > bytesAfterSlice) {\n"); 810 | content.append("\t\t\t\tvar tag:uint = com.netease.protobuf.ReadUtils.read_TYPE_UINT32(input);\n"); 811 | content.append("\t\t\t\tswitch (tag >> 3) {\n"); 812 | for (FieldDescriptorProto fdp : scope.proto.getFieldList()) { 813 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_GROUP) { 814 | System.err.println("Warning: Group is not supported."); 815 | continue; 816 | } 817 | content.append("\t\t\t\tcase "); 818 | content.append(Integer.toString(fdp.getNumber())); 819 | content.append(":\n"); 820 | switch (fdp.getLabel()) { 821 | case LABEL_OPTIONAL: 822 | case LABEL_REQUIRED: 823 | content.append("\t\t\t\t\tif ("); 824 | content.append(fdp.getName()); 825 | content.append("$count != 0) {\n"); 826 | content.append("\t\t\t\t\t\tthrow new flash.errors.IOError('Bad data format: "); 827 | content.append(scope.proto.getName()); 828 | content.append('.'); 829 | appendLowerCamelCase(content, fdp.getName()); 830 | content.append(" cannot be set twice.');\n"); 831 | content.append("\t\t\t\t\t}\n"); 832 | content.append("\t\t\t\t\t++"); 833 | content.append(fdp.getName()); 834 | content.append("$count;\n"); 835 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_MESSAGE) { 836 | content.append("\t\t\t\t\tthis."); 837 | appendLowerCamelCase(content, fdp.getName()); 838 | content.append(" = new "); 839 | content.append(getActionScript3Type(scope, fdp)); 840 | content.append("();\n"); 841 | content.append("\t\t\t\t\tcom.netease.protobuf.ReadUtils.read_TYPE_MESSAGE(input, this."); 842 | appendLowerCamelCase(content, fdp.getName()); 843 | content.append(");\n"); 844 | } else { 845 | content.append("\t\t\t\t\tthis."); 846 | appendLowerCamelCase(content, fdp.getName()); 847 | content.append(" = com.netease.protobuf.ReadUtils.read_"); 848 | content.append(fdp.getType().name()); 849 | content.append("(input);\n"); 850 | } 851 | break; 852 | case LABEL_REPEATED: 853 | switch (fdp.getType()) { 854 | case TYPE_DOUBLE: 855 | case TYPE_FLOAT: 856 | case TYPE_BOOL: 857 | case TYPE_INT32: 858 | case TYPE_FIXED32: 859 | case TYPE_UINT32: 860 | case TYPE_SFIXED32: 861 | case TYPE_SINT32: 862 | case TYPE_INT64: 863 | case TYPE_FIXED64: 864 | case TYPE_UINT64: 865 | case TYPE_SFIXED64: 866 | case TYPE_SINT64: 867 | case TYPE_ENUM: 868 | content.append("\t\t\t\t\tif ((tag & 7) == com.netease.protobuf.WireType.LENGTH_DELIMITED) {\n"); 869 | content.append("\t\t\t\t\t\tcom.netease.protobuf.ReadUtils.readPackedRepeated(input, com.netease.protobuf.ReadUtils.read_"); 870 | content.append(fdp.getType().name()); 871 | content.append(", this."); 872 | appendLowerCamelCase(content, fdp.getName()); 873 | content.append(");\n"); 874 | content.append("\t\t\t\t\t\tbreak;\n"); 875 | content.append("\t\t\t\t\t}\n"); 876 | } 877 | if (fdp.getType() == FieldDescriptorProto.Type.TYPE_MESSAGE) { 878 | content.append("\t\t\t\t\tthis."); 879 | appendLowerCamelCase(content, fdp.getName()); 880 | content.append(".push("); 881 | content.append("com.netease.protobuf.ReadUtils.read_TYPE_MESSAGE(input, "); 882 | content.append("new "); 883 | content.append(getActionScript3Type(scope, fdp)); 884 | content.append("()));\n"); 885 | } else { 886 | content.append("\t\t\t\t\tthis."); 887 | appendLowerCamelCase(content, fdp.getName()); 888 | content.append(".push(com.netease.protobuf.ReadUtils.read_"); 889 | content.append(fdp.getType().name()); 890 | content.append("(input));\n"); 891 | } 892 | break; 893 | } 894 | content.append("\t\t\t\t\tbreak;\n"); 895 | } 896 | content.append("\t\t\t\tdefault:\n"); 897 | if (scope.proto.getExtensionRangeCount() > 0) { 898 | content.append("\t\t\t\t\tsuper.readExtensionOrUnknown(extensionReadFunctions, input, tag);\n"); 899 | } else { 900 | content.append("\t\t\t\t\tsuper.readUnknown(input, tag);\n"); 901 | } 902 | content.append("\t\t\t\t\tbreak;\n"); 903 | content.append("\t\t\t\t}\n"); 904 | content.append("\t\t\t}\n"); 905 | content.append("\t\t}\n\n"); 906 | content.append("\t}\n"); 907 | } 908 | private static void appendFieldDescriptorClass(StringBuilder content, 909 | FieldDescriptorProto fdp) { 910 | switch (fdp.getLabel()) { 911 | case LABEL_REQUIRED: 912 | case LABEL_OPTIONAL: 913 | break; 914 | case LABEL_REPEATED: 915 | content.append("Repeated"); 916 | break; 917 | default: 918 | throw new IllegalArgumentException(); 919 | } 920 | content.append("FieldDescriptor_"); 921 | content.append(fdp.getType().name()); 922 | } 923 | private static void appendFieldDescriptor(StringBuilder content, 924 | Scope scope, 925 | FieldDescriptorProto fdp) { 926 | content.append("new "); 927 | appendFieldDescriptorClass(content, fdp); 928 | content.append("("); 929 | if (scope.parent == null) { 930 | appendQuotedString(content, fdp.getName()); 931 | } else { 932 | appendQuotedString(content, scope.fullName + '.' + fdp.getName()); 933 | } 934 | content.append(", "); 935 | if (fdp.hasExtendee()) { 936 | if (scope.proto instanceof DescriptorProto) { 937 | appendQuotedString(content, scope.fullName + '/' + fdp.getName().toUpperCase()); 938 | } else { 939 | if (scope.parent == null) { 940 | appendQuotedString(content, fdp.getName().toUpperCase()); 941 | } else { 942 | appendQuotedString(content, scope.fullName + '.' + fdp.getName().toUpperCase()); 943 | } 944 | } 945 | } else { 946 | StringBuilder fieldName = new StringBuilder(); 947 | appendLowerCamelCase(fieldName, fdp.getName()); 948 | appendQuotedString(content, fieldName.toString()); 949 | } 950 | content.append(", ("); 951 | switch (fdp.getLabel()) { 952 | case LABEL_REQUIRED: 953 | case LABEL_OPTIONAL: 954 | content.append(Integer.toString(fdp.getNumber())); 955 | content.append(" << 3) | com.netease.protobuf.WireType."); 956 | content.append(getActionScript3WireType(fdp.getType())); 957 | break; 958 | case LABEL_REPEATED: 959 | if (fdp.hasOptions() && fdp.getOptions().getPacked()) { 960 | content.append(Integer.toString(fdp.getNumber())); 961 | content.append(" << 3) | com.netease.protobuf.WireType.LENGTH_DELIMITED"); 962 | } else { 963 | content.append(Integer.toString(fdp.getNumber())); 964 | content.append(" << 3) | com.netease.protobuf.WireType."); 965 | content.append(getActionScript3WireType(fdp.getType())); 966 | } 967 | break; 968 | } 969 | switch (fdp.getType()) { 970 | case TYPE_MESSAGE: 971 | if (scope.proto instanceof DescriptorProto) { 972 | content.append(", function():Class { return "); 973 | content.append(getActionScript3LogicType(scope, fdp)); 974 | content.append("; }"); 975 | } else { 976 | content.append(", "); 977 | content.append(getActionScript3LogicType(scope, fdp)); 978 | } 979 | break; 980 | case TYPE_ENUM: 981 | content.append(", "); 982 | content.append(getActionScript3LogicType(scope, fdp)); 983 | break; 984 | } 985 | content.append(")"); 986 | } 987 | private static void appendExtensionReadFunction(StringBuilder content, 988 | String tabs, 989 | Scope scope, 990 | FieldDescriptorProto fdp) { 991 | String extendee = scope.find(fdp.getExtendee()).fullName; 992 | switch (fdp.getLabel()) { 993 | case LABEL_REQUIRED: 994 | case LABEL_OPTIONAL: 995 | content.append(tabs); 996 | content.append(extendee); 997 | content.append(".extensionReadFunctions[("); 998 | content.append(Integer.toString(fdp.getNumber())); 999 | content.append(" << 3) | com.netease.protobuf.WireType."); 1000 | content.append(getActionScript3WireType(fdp.getType())); 1001 | content.append("] = "); 1002 | content.append(fdp.getName().toUpperCase()); 1003 | content.append(".read;\n\n"); 1004 | break; 1005 | case LABEL_REPEATED: 1006 | content.append(tabs); 1007 | content.append(extendee); 1008 | content.append(".extensionReadFunctions[("); 1009 | content.append(Integer.toString(fdp.getNumber())); 1010 | content.append(" << 3) | com.netease.protobuf.WireType."); 1011 | content.append(getActionScript3WireType(fdp.getType())); 1012 | content.append("] = "); 1013 | content.append(fdp.getName().toUpperCase()); 1014 | content.append(".readNonPacked;\n\n"); 1015 | switch (fdp.getType()) { 1016 | case TYPE_MESSAGE: 1017 | case TYPE_BYTES: 1018 | case TYPE_STRING: 1019 | break; 1020 | default: 1021 | content.append(tabs); 1022 | content.append(extendee); 1023 | content.append(".extensionReadFunctions[("); 1024 | content.append(Integer.toString(fdp.getNumber())); 1025 | content.append(" << 3) | com.netease.protobuf.WireType.LENGTH_DELIMITED] = "); 1026 | content.append(fdp.getName().toUpperCase()); 1027 | content.append(".readPacked;\n\n"); 1028 | break; 1029 | } 1030 | break; 1031 | } 1032 | } 1033 | private static void writeExtension(Scope scope, 1034 | StringBuilder content, StringBuilder initializerContent) { 1035 | initializerContent.append("import "); 1036 | initializerContent.append(scope.fullName); 1037 | initializerContent.append(";\n"); 1038 | initializerContent.append("if(!"); 1039 | initializerContent.append(scope.fullName); 1040 | initializerContent.append(") throw new Error;\n"); 1041 | content.append("\timport com.netease.protobuf.*;\n"); 1042 | content.append("\timport com.netease.protobuf.fieldDescriptors.*;\n"); 1043 | String importType = getImportType(scope.parent, scope.proto); 1044 | if (importType != null) { 1045 | content.append("\timport "); 1046 | content.append(importType); 1047 | content.append(";\n"); 1048 | } 1049 | String extendee = scope.parent.find(scope.proto.getExtendee()).fullName; 1050 | content.append("\timport "); 1051 | content.append(extendee); 1052 | content.append(";\n"); 1053 | content.append("\t// @@protoc_insertion_point(imports)\n\n"); 1054 | 1055 | content.append("\t// @@protoc_insertion_point(constant_metadata)\n"); 1056 | content.append("\t/**\n\t * @private\n\t */\n"); 1057 | content.append("\tpublic const "); 1058 | content.append(scope.proto.getName().toUpperCase()); 1059 | content.append(":"); 1060 | appendFieldDescriptorClass(content, scope.proto); 1061 | content.append(" = "); 1062 | appendFieldDescriptor(content, scope.parent, scope.proto); 1063 | content.append(";\n\n"); 1064 | if (scope.proto.hasDefaultValue()) { 1065 | content.append("\t"); 1066 | content.append(extendee); 1067 | content.append(".prototype["); 1068 | content.append(scope.proto.getName().toUpperCase()); 1069 | content.append("] = "); 1070 | appendDefaultValue(content, scope.parent, scope.proto); 1071 | content.append(";\n\n"); 1072 | } 1073 | appendExtensionReadFunction(content, "\t", scope.parent, scope.proto); 1074 | } 1075 | 1076 | private static void writeEnum(Scope scope, 1077 | StringBuilder content) { 1078 | content.append("\tpublic final class "); 1079 | content.append(scope.proto.getName()); 1080 | content.append(" {\n"); 1081 | for (EnumValueDescriptorProto evdp : scope.proto.getValueList()) { 1082 | content.append("\t\tpublic static const "); 1083 | content.append(evdp.getName()); 1084 | content.append(":int = "); 1085 | content.append(evdp.getNumber()); 1086 | content.append(";\n"); 1087 | } 1088 | content.append("\t}\n"); 1089 | } 1090 | @SuppressWarnings("unchecked") 1091 | private static void writeFile(Scope scope, StringBuilder content, 1092 | StringBuilder initializerContent) { 1093 | content.append("package "); 1094 | content.append(scope.parent.fullName); 1095 | content.append(" {\n"); 1096 | if (scope.proto instanceof DescriptorProto) { 1097 | writeMessage((Scope)scope, content, 1098 | initializerContent); 1099 | } else if (scope.proto instanceof EnumDescriptorProto) { 1100 | writeEnum((Scope)scope, content); 1101 | } else if (scope.proto instanceof FieldDescriptorProto) { 1102 | Scope fdpScope = 1103 | (Scope)scope; 1104 | if (fdpScope.proto.getType() == 1105 | FieldDescriptorProto.Type.TYPE_GROUP) { 1106 | System.err.println("Warning: Group is not supported."); 1107 | } else { 1108 | writeExtension(fdpScope, content, initializerContent); 1109 | } 1110 | } else { 1111 | throw new IllegalArgumentException(); 1112 | } 1113 | content.append("}\n"); 1114 | } 1115 | 1116 | @SuppressWarnings("unchecked") 1117 | private static void writeFiles(Scope root, 1118 | CodeGeneratorResponse.Builder responseBuilder, 1119 | StringBuilder initializerContent) { 1120 | for (Map.Entry> entry : root.children.entrySet()) { 1121 | Scope scope = entry.getValue(); 1122 | if (scope.export) { 1123 | if (scope.proto instanceof ServiceDescriptorProto) { 1124 | ServiceDescriptorProto serviceProto = (ServiceDescriptorProto)scope.proto; 1125 | if (serviceProto.getOptions().getExtension(Options.as3ClientSideService) || 1126 | serviceProto.getOptions().getExtension(Options.as3ServerSideService)) { 1127 | StringBuilder classContent = new StringBuilder(); 1128 | writeServiceClass((Scope)scope, classContent); 1129 | responseBuilder.addFile( 1130 | CodeGeneratorResponse.File.newBuilder(). 1131 | setName(scope.fullName.replace('.', '/') + ".as"). 1132 | setContent(classContent.toString()). 1133 | build() 1134 | ); 1135 | StringBuilder interfaceContent = new StringBuilder(); 1136 | writeServiceInterface((Scope)scope, interfaceContent); 1137 | String[] servicePath = scope.fullName.split("\\."); 1138 | StringBuilder sb = new StringBuilder(); 1139 | int i = 0; 1140 | for (; i < servicePath.length - 1; i++) { 1141 | sb.append(servicePath[i]); 1142 | sb.append('/'); 1143 | } 1144 | sb.append('I'); 1145 | sb.append(servicePath[i]); 1146 | sb.append(".as"); 1147 | 1148 | responseBuilder.addFile( 1149 | CodeGeneratorResponse.File.newBuilder(). 1150 | setName(sb.toString()). 1151 | setContent(interfaceContent.toString()). 1152 | build() 1153 | ); 1154 | } 1155 | } 1156 | else 1157 | { 1158 | StringBuilder content = new StringBuilder(); 1159 | writeFile(scope, content, initializerContent); 1160 | responseBuilder.addFile( 1161 | CodeGeneratorResponse.File.newBuilder(). 1162 | setName(scope.fullName.replace('.', '/') + ".as"). 1163 | setContent(content.toString()). 1164 | build() 1165 | ); 1166 | } 1167 | } 1168 | writeFiles(scope, responseBuilder, initializerContent); 1169 | } 1170 | } 1171 | private static void writeFiles(Scope root, 1172 | CodeGeneratorResponse.Builder responseBuilder) { 1173 | StringBuilder initializerContent = new StringBuilder(); 1174 | initializerContent.append("{\n"); 1175 | writeFiles(root, responseBuilder, initializerContent); 1176 | initializerContent.append("}\n"); 1177 | responseBuilder.addFile( 1178 | CodeGeneratorResponse.File.newBuilder(). 1179 | setName("initializer.as.inc"). 1180 | setContent(initializerContent.toString()). 1181 | build() 1182 | ); 1183 | } 1184 | private static void writeServiceClass(Scope scope, 1185 | StringBuilder content) { 1186 | content.append("package "); 1187 | content.append(scope.parent.fullName); 1188 | content.append(" {\n"); 1189 | HashSet importTypes = new HashSet(); 1190 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1191 | importTypes.add(scope.find(mdp.getInputType()).fullName); 1192 | if (scope.proto.getOptions().getExtension(Options.as3ClientSideService)) { 1193 | importTypes.add(scope.find(mdp.getOutputType()).fullName); 1194 | } 1195 | } 1196 | for (String importType : importTypes) { 1197 | content.append("\timport "); 1198 | content.append(importType); 1199 | content.append(";\n"); 1200 | } 1201 | content.append("\timport google.protobuf.*;\n"); 1202 | content.append("\timport flash.utils.*;\n"); 1203 | content.append("\timport com.netease.protobuf.*;\n"); 1204 | content.append("\t// @@protoc_insertion_point(imports)\n\n"); 1205 | content.append("\tpublic final class "); 1206 | content.append(scope.proto.getName()); 1207 | if (scope.proto.getOptions().getExtension(Options.as3ClientSideService)) { 1208 | content.append(" implements "); 1209 | if (scope.parent.isRoot()) { 1210 | content.append("I"); 1211 | } else { 1212 | content.append(scope.parent.fullName); 1213 | content.append(".I"); 1214 | } 1215 | content.append(scope.proto.getName()); 1216 | } 1217 | content.append(" {\n"); 1218 | 1219 | if (scope.proto.hasOptions()) { 1220 | content.append("\t\tpublic static const OPTIONS_BYTES:flash.utils.ByteArray = com.netease.protobuf.stringToByteArray(\""); 1221 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 1222 | try { 1223 | scope.proto.getOptions().writeTo(buffer); 1224 | } catch (IOException e) { 1225 | throw new IllegalStateException("ByteArrayOutputStream should not throw IOException!", e); 1226 | } 1227 | for (byte b : buffer.toByteArray()) { 1228 | content.append("\\x"); 1229 | content.append(Character.forDigit((b & 0xF0) >>> 4, 16)); 1230 | content.append(Character.forDigit(b & 0x0F, 16)); 1231 | } 1232 | content.append("\");\n\n"); 1233 | content.append("\t\tpublic static function getOptions():google.protobuf.ServiceOptions\n"); 1234 | content.append("\t\t{\n"); 1235 | content.append("\t\t\tOPTIONS_BYTES.position = 0;\n"); 1236 | content.append("\t\t\tconst options:google.protobuf.ServiceOptions = new google.protobuf.ServiceOptions();\n"); 1237 | content.append("\t\t\toptions.mergeFrom(OPTIONS_BYTES);\n\n"); 1238 | content.append("\t\t\treturn options;\n"); 1239 | content.append("\t\t}\n\n"); 1240 | } 1241 | 1242 | boolean hasMethodOptions = false; 1243 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1244 | if (mdp.hasOptions()) { 1245 | if (!hasMethodOptions) { 1246 | hasMethodOptions = true; 1247 | content.append("\t\tpublic static const OPTIONS_BYTES_BY_METHOD_NAME:Object =\n"); 1248 | content.append("\t\t{\n"); 1249 | } else { 1250 | content.append(",\n"); 1251 | } 1252 | content.append("\t\t\t\""); 1253 | content.append(scope.fullName); 1254 | content.append("."); 1255 | content.append(mdp.getName()); 1256 | content.append("\" : stringToByteArray(\""); 1257 | ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 1258 | try { 1259 | mdp.getOptions().writeTo(buffer); 1260 | } catch (IOException e) { 1261 | throw new IllegalStateException("ByteArrayOutputStream should not throw IOException!", e); 1262 | } 1263 | for (byte b : buffer.toByteArray()) { 1264 | content.append("\\x"); 1265 | content.append(Character.forDigit((b & 0xF0) >>> 4, 16)); 1266 | content.append(Character.forDigit(b & 0x0F, 16)); 1267 | } 1268 | content.append("\")"); 1269 | } 1270 | } 1271 | if (hasMethodOptions) { 1272 | content.append("\n\t\t};\n\n"); 1273 | } 1274 | 1275 | if (scope.proto.getOptions().getExtension(Options.as3ServerSideService)) { 1276 | content.append("\t\tpublic static const REQUEST_CLASSES_BY_METHOD_NAME:Object =\n"); 1277 | content.append("\t\t{\n"); 1278 | boolean comma = false; 1279 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1280 | if (comma) { 1281 | content.append(",\n"); 1282 | } else { 1283 | comma = true; 1284 | } 1285 | content.append("\t\t\t\""); 1286 | content.append(scope.fullName); 1287 | content.append("."); 1288 | content.append(mdp.getName()); 1289 | content.append("\" : "); 1290 | content.append(scope.find(mdp.getInputType()).fullName); 1291 | } 1292 | content.append("\n\t\t};\n\n"); 1293 | 1294 | 1295 | content.append("\t\tpublic static function callMethod(service:"); 1296 | if (scope.parent.isRoot()) { 1297 | content.append("I"); 1298 | } else { 1299 | content.append(scope.parent.fullName); 1300 | content.append(".I"); 1301 | } 1302 | content.append(scope.proto.getName()); 1303 | content.append(", methodName:String, request:com.netease.protobuf.Message, responseHandler:Function):void {\n"); 1304 | content.append("\t\t\tswitch (methodName) {\n"); 1305 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1306 | content.append("\t\t\t\tcase \""); 1307 | content.append(scope.fullName); 1308 | content.append("."); 1309 | content.append(mdp.getName()); 1310 | content.append("\":\n"); 1311 | content.append("\t\t\t\t{\n"); 1312 | content.append("\t\t\t\t\tservice."); 1313 | appendLowerCamelCase(content, mdp.getName()); 1314 | content.append("("); 1315 | content.append(scope.find(mdp.getInputType()).fullName); 1316 | content.append("(request), responseHandler);\n"); 1317 | content.append("\t\t\t\t\tbreak;\n"); 1318 | content.append("\t\t\t\t}\n"); 1319 | } 1320 | content.append("\t\t\t}\n"); 1321 | content.append("\t\t}\n\n"); 1322 | } 1323 | 1324 | if (scope.proto.getOptions().getExtension(Options.as3ClientSideService)) { 1325 | content.append("\t\tpublic var sendFunction:Function;\n\n"); 1326 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1327 | content.append("\t\tpublic function "); 1328 | appendLowerCamelCase(content, mdp.getName()); 1329 | content.append("(request:"); 1330 | content.append(scope.find(mdp.getInputType()).fullName); 1331 | content.append(", responseHandler:Function):void {\n"); 1332 | content.append("\t\t\tsendFunction(\""); 1333 | content.append(scope.fullName); 1334 | content.append("."); 1335 | content.append(mdp.getName()); 1336 | content.append("\", request, responseHandler, "); 1337 | content.append(scope.find(mdp.getOutputType()).fullName); 1338 | content.append(");\n"); 1339 | content.append("\t\t}\n\n"); 1340 | } 1341 | } 1342 | content.append("\t}\n"); 1343 | content.append("}\n"); 1344 | } 1345 | 1346 | private static void writeServiceInterface( 1347 | Scope scope, 1348 | StringBuilder content) { 1349 | content.append("package "); 1350 | content.append(scope.parent.fullName); 1351 | content.append(" {\n"); 1352 | HashSet importTypes = new HashSet(); 1353 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1354 | importTypes.add(scope.find(mdp.getInputType()).fullName); 1355 | } 1356 | for (String importType : importTypes) { 1357 | content.append("\timport "); 1358 | content.append(importType); 1359 | content.append(";\n"); 1360 | } 1361 | content.append("\t// @@protoc_insertion_point(imports)\n\n"); 1362 | content.append("\tpublic interface I"); 1363 | content.append(scope.proto.getName()); 1364 | content.append(" {\n\n"); 1365 | for (MethodDescriptorProto mdp : scope.proto.getMethodList()) { 1366 | content.append("\t\tfunction "); 1367 | appendLowerCamelCase(content, mdp.getName()); 1368 | content.append("(input:"); 1369 | content.append(scope.find(mdp.getInputType()).fullName); 1370 | content.append(", done:Function):void;\n\n"); 1371 | } 1372 | content.append("\t}\n"); 1373 | content.append("}\n"); 1374 | } 1375 | 1376 | public static void main(String[] args) throws IOException { 1377 | ExtensionRegistry registry = ExtensionRegistry.newInstance(); 1378 | Options.registerAllExtensions(registry); 1379 | CodeGeneratorRequest request = CodeGeneratorRequest. 1380 | parseFrom(System.in, registry); 1381 | CodeGeneratorResponse response; 1382 | try { 1383 | Scope root = buildScopeTree(request); 1384 | CodeGeneratorResponse.Builder responseBuilder = 1385 | CodeGeneratorResponse.newBuilder(); 1386 | writeFiles(root, responseBuilder); 1387 | response = responseBuilder.build(); 1388 | } catch (Exception e) { 1389 | // 出错,报告给 protoc ,然后退出 1390 | StringWriter sw = new StringWriter(); 1391 | PrintWriter pw = new PrintWriter(sw); 1392 | e.printStackTrace(pw); 1393 | pw.flush(); 1394 | CodeGeneratorResponse.newBuilder().setError(sw.toString()). 1395 | build().writeTo(System.out); 1396 | System.out.flush(); 1397 | return; 1398 | } 1399 | response.writeTo(System.out); 1400 | System.out.flush(); 1401 | } 1402 | } 1403 | -------------------------------------------------------------------------------- /config.mk.sample: -------------------------------------------------------------------------------- 1 | PROTOBUF_VERSION=2.4.1 2 | PROTOBUF_DIR=../protobuf-$(PROTOBUF_VERSION) 3 | ifeq ($(OS), Windows_NT) 4 | BAT=.bat 5 | EXE=.exe 6 | PATH_SEPARATOR=; 7 | else 8 | PATH_SEPARATOR=: 9 | endif 10 | COMPC=compc$(BAT) 11 | MXMLC=mxmlc$(BAT) 12 | ASDOC=asdoc$(BAT) 13 | FDB=fdb$(BAT) 14 | MVN=mvn 15 | JAVAC=javac 16 | JAR=jar 17 | HAXE=haxe 18 | -------------------------------------------------------------------------------- /haxe-test.patch: -------------------------------------------------------------------------------- 1 | test.AAA.$listener : Dynamic -> Void; 2 | test.BBB.$listener : Dynamic -> Void; 3 | -------------------------------------------------------------------------------- /haxelib.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Protocol Buffers plugin for Flash platform 9 | 10 | Fix bugs. 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /hx/com/dongxiguo/protobuf/Run.hx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2013 , Yang Bo All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package com.dongxiguo.protobuf; 9 | 10 | import com.dongxiguo.utils.HaxelibRun; 11 | import sys.io.File; 12 | import sys.FileSystem; 13 | 14 | class Run 15 | { 16 | static function main() 17 | { 18 | var args = Sys.args(); 19 | var returnValue = HaxelibRun.run(args); 20 | if (returnValue == null) 21 | { 22 | switch (args[0]) 23 | { 24 | case "protoc": 25 | var libPath = Sys.getCwd(); 26 | var cwd = args.pop(); 27 | Sys.setCwd(cwd); 28 | var pluginScriptFileName = cwd + (Sys.systemName() == "Windows" ? "protoc-gen-as3.tmp.bat" : "protoc-gen-as3.tmp.sh"); 29 | File.saveContent(pluginScriptFileName, Sys.systemName() == "Windows" ? '@"$libPath\\protoc-gen-as3.bat"' : '#!/bin/sh 30 | "$libPath/protoc-gen-as3"'); 31 | if (Sys.systemName() == "Windows") 32 | { 33 | Sys.command("cacls", [pluginScriptFileName, "/E", "/G", "Everyone:R"]); 34 | } 35 | else 36 | { 37 | Sys.command("chmod", ["+x", pluginScriptFileName]); 38 | } 39 | args[0] = '--plugin=protoc-gen-as3=$pluginScriptFileName'; 40 | var returnValue = Sys.command("protoc", args); 41 | FileSystem.deleteFile(pluginScriptFileName); 42 | Sys.exit(returnValue); 43 | default: 44 | Sys.print('Usage: haxelib run ${HaxelibRun.libraryName()} protoc [ args ... ] 45 | ${HaxelibRun.usage()}'); 46 | Sys.exit(-1); 47 | } 48 | } 49 | else 50 | { 51 | Sys.exit(returnValue); 52 | } 53 | } 54 | } 55 | 56 | // vim: sts=2 sw=2 et 57 | -------------------------------------------------------------------------------- /options.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // Copyright (c) 2012 , Yang Bo & NetEase.com,Inc. All rights reserved. 3 | // 4 | // Author: Yang Bo (pop.atry@gmail.com) 5 | // 6 | // Use, modification and distribution are subject to the "New BSD License" 7 | // as listed at . 8 | import "google/protobuf/descriptor.proto"; 9 | option java_package="com.netease.protocGenAs3"; 10 | extend google.protobuf.MessageOptions { 11 | optional bool as3_bindable = 9393[default = false]; 12 | optional string as3_amf_alias = 9394; 13 | optional bool as3_amf_auto_alias = 9395[default = false]; 14 | } 15 | extend google.protobuf.ServiceOptions { 16 | optional bool as3_client_side_service = 9396[default = true]; 17 | optional bool as3_server_side_service = 9397[default = false]; 18 | } 19 | 20 | -------------------------------------------------------------------------------- /protoc-gen-as3-test.as3proj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 91 | 92 | 93 | -------------------------------------------------------------------------------- /test/com/netease/protobuf/test/HaxeTest.hx: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 , Yang Bo All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package com.netease.protobuf.test; 9 | class HaxeTest 10 | { 11 | static function main():Void 12 | { 13 | TestAll.run(true); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /test/com/netease/protobuf/test/Test.mxml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /test/com/netease/protobuf/test/TestAll.as: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package com.netease.protobuf.test { 9 | import google.protobuf.FileDescriptorSet; 10 | import protobuf_unittest.* 11 | import protobuf_unittest.TestAllTypes.* 12 | import test.* 13 | import flash.display.* 14 | import flash.utils.* 15 | import flash.system.* 16 | import com.netease.protobuf.* 17 | 18 | public class TestAll { 19 | 20 | private static function assert(b:Boolean, 21 | errorMessage:String = "Assertion failed.", 22 | errorId:int = 0):void { 23 | if (!b) { 24 | throw new Error(errorMessage, errorId) 25 | } 26 | } 27 | private static function assertSame(l:*, r:*, name:String = ""):void { 28 | if (typeof(l) == "object") { 29 | assert(getQualifiedClassName(l) == 30 | getQualifiedClassName(r)) 31 | if (l is Array || l is ByteArray) { 32 | assertSame(l.length, r.length, name + ".length") 33 | for (var i:int = 0; i < l.length; i++) { 34 | assertSame(l[i], r[i], name + "[" + i + "]") 35 | } 36 | } else { 37 | var k:* 38 | for(k in l) { 39 | assertSame(l[k], r[k], name + "[" + k + "]") 40 | } 41 | for(k in r) { 42 | assertSame(l[k], r[k], name + "[" + k + "]") 43 | } 44 | const description:XML = describeType(l) 45 | for each(var getter:XML in description..accessor.(@access != "writeonly")) { 46 | assertSame(l[getter.@name], r[getter.@name], name + "." + getter.@name) 47 | } 48 | for each(var field:XML in description..variable) { 49 | assertSame(l[field.@name], r[field.@name], name + "." + field.@name) 50 | } 51 | } 52 | } else { 53 | assert(l === r || (isNaN(l) && isNaN(r)), name + " not equal.") 54 | } 55 | } 56 | private static const HEX_CHARS:String = "0123456789ABCDEF" 57 | private static function testAmf(input:*):void { 58 | const ba:ByteArray = new ByteArray 59 | ba.writeObject(input) 60 | var s:String = "" 61 | for (var i:uint = 0; i < ba.length; i++) { 62 | s += HEX_CHARS.charAt(ba[i] / 16) 63 | s += HEX_CHARS.charAt(ba[i] % 16) 64 | s += " " 65 | } 66 | trace(input) 67 | trace(s) 68 | ba.position = 0 69 | const output:* = ba.readObject() 70 | assertSame(input, output) 71 | } 72 | private static function testText(input:Message):void { 73 | const text:String = 74 | com.netease.protobuf.TextFormat.printToString(input, false) 75 | const output:Message = 76 | new (getDefinitionByName(getQualifiedClassName(input))) 77 | com.netease.protobuf.TextFormat.mergeFromString(text, output) 78 | trace("input:", input, "output:", output, "text:", text) 79 | assertSame(input, output) 80 | } 81 | private static function test(input:Message):void { 82 | const ba:ByteArray = new ByteArray 83 | input.writeTo(ba) 84 | var s:String = "" 85 | for (var i:uint = 0; i < ba.length; i++) { 86 | s += HEX_CHARS.charAt(ba[i] / 16) 87 | s += HEX_CHARS.charAt(ba[i] % 16) 88 | s += " " 89 | } 90 | ba.position = 0 91 | const output:Message = new (getDefinitionByName( 92 | getQualifiedClassName(input))) 93 | output.mergeFrom(ba) 94 | assertSame(input, output) 95 | } 96 | public static function run(haxeTest:Boolean = false):void { 97 | 98 | const int64:Int64 = new Int64(0x12345678, 0x91abcde1) 99 | assertSame(int64.toString(), "-7950034350635723144") 100 | assertSame(Int64.parseInt64(int64.toString()), int64) 101 | 102 | const int64_2:Int64 = new Int64(0x12345678, 0xb) 103 | assertSame(int64_2.toString(), "47550060152") 104 | assertSame(Int64.parseInt64(int64_2.toString()), int64_2) 105 | 106 | const int64_3:Int64 = new Int64(0x12345678, 0xabcdef12) 107 | assertSame(int64_3.toString(), "-6066930262104320392") 108 | assertSame(Int64.parseInt64(int64_3.toString()), int64_3) 109 | 110 | const int64_4:Int64 = new Int64(0x12345678, 0xbb) 111 | assertSame(int64_4.toString(), "803464304248") 112 | assertSame(Int64.parseInt64(int64_4.toString()), int64_4) 113 | 114 | const int64_5:Int64 = new Int64(0, 0) 115 | assertSame(int64_5.toString(), "0") 116 | assertSame(Int64.parseInt64(int64_5.toString()), int64_5) 117 | 118 | const int64_6:Int64 = Int64.fromNumber(-123234) 119 | assertSame(int64_6.toNumber(), -123234) 120 | assertSame(int64_6.toString(), "-123234") 121 | assertSame(Int64.parseInt64(int64_6.toString()), int64_6) 122 | 123 | const int64_7:Int64 = Int64.fromNumber(-184942424123234000) 124 | assertSame(int64_7.toNumber(), -184942424123233984) 125 | assertSame(int64_7.toString(), "-184942424123233984") 126 | assertSame(Int64.parseInt64(int64_7.toString()), int64_7) 127 | 128 | const int64_8:Int64 = Int64.fromNumber(-1494242414000) 129 | assertSame(int64_8.toNumber(), -1494242414000) 130 | assertSame(int64_8.toString(), "-1494242414000") 131 | assertSame(Int64.parseInt64(int64_8.toString()), int64_8) 132 | 133 | const int64_10:Int64 = Int64.fromNumber(-1024) 134 | assertSame(int64_10.toNumber(), -1024) 135 | assertSame(int64_10.toString(), "-1024") 136 | assertSame(Int64.parseInt64(int64_10.toString()), int64_10) 137 | assertSame(Int64.parseInt64("-0x400"), int64_10) 138 | 139 | const int64_11:Int64 = Int64.fromNumber(0) 140 | assertSame(int64_11.toNumber(), 0) 141 | assertSame(int64_11.toString(), "0") 142 | assertSame(Int64.parseInt64(int64_11.toString()), int64_11) 143 | assertSame(Int64.parseInt64("0"), int64_11) 144 | assertSame(Int64.parseInt64("0x0"), int64_11) 145 | assertSame(Int64.parseInt64("-0"), int64_11) 146 | assertSame(Int64.parseInt64("-0x0"), int64_11) 147 | 148 | const int64_9:Int64 = Int64.fromNumber(-1) 149 | assertSame(int64_9.toNumber(), -1) 150 | assertSame(int64_9.toString(), "-1") 151 | assertSame(Int64.parseInt64(int64_9.toString()), int64_9) 152 | assertSame(Int64.parseInt64("-0x1"), int64_9) 153 | assertSame(Int64.parseInt64("-01"), int64_9) 154 | 155 | const int64_12:Int64 = new Int64(0, -1) 156 | assertSame(int64_12.toNumber(), -4294967296.0) 157 | assertSame(int64_12.toString(), "-4294967296") 158 | assertSame(Int64.parseInt64(int64_12.toString()), int64_12) 159 | assertSame(Int64.parseInt64("-0x100000000"), int64_12) 160 | assertSame(Int64.parseInt64("0xFFFFFFFF00000000"), int64_12) 161 | 162 | const int64_13:Int64 = Int64.parseInt64("0xFFFFFFF000000000") 163 | assertSame(int64_13.toNumber(), -68719476736.0) 164 | assertSame(int64_13.toString(), "-68719476736") 165 | assertSame(Int64.parseInt64(int64_13.toString()), int64_13) 166 | assertSame(Int64.parseInt64("-68719476736"), int64_13) 167 | 168 | const int64_14:Int64 = Int64.parseInt64("0xFFFFFFFFF0000000") 169 | assertSame(int64_14.toNumber(), -268435456.0) 170 | assertSame(int64_14.toString(), "-268435456") 171 | assertSame(Int64.parseInt64(int64_14.toString()), int64_14) 172 | assertSame(Int64.parseInt64("-268435456"), int64_14) 173 | 174 | const uint64_9:UInt64 = UInt64.fromNumber(123234) 175 | assertSame(uint64_9.toNumber(), 123234) 176 | assertSame(uint64_9.toString(), "123234") 177 | assertSame(UInt64.parseUInt64(uint64_9.toString()), uint64_9) 178 | assertSame(UInt64.parseUInt64("0x" + uint64_9.toString(16)), uint64_9) 179 | 180 | const uint64_10:UInt64 = UInt64.fromNumber(184942424123234000) 181 | assertSame(uint64_10.toNumber(), 184942424123233984) 182 | assertSame(uint64_10.toString(), "184942424123233984") 183 | assertSame(UInt64.parseUInt64(uint64_10.toString()), uint64_10) 184 | 185 | const uint64_11:UInt64 = UInt64.fromNumber(1494242414000) 186 | assertSame(uint64_11.toNumber(), 1494242414000) 187 | assertSame(uint64_11.toString(), "1494242414000") 188 | assertSame(UInt64.parseUInt64(uint64_11.toString()), uint64_11) 189 | 190 | const uint64:UInt64 = new UInt64(0x12345678, 0) 191 | assertSame(uint64.toString(), "305419896") 192 | assertSame(UInt64.parseUInt64(uint64.toString()), uint64) 193 | 194 | const uint64_2:UInt64 = new UInt64(0x12345678, 0xb) 195 | assertSame(uint64_2.toString(), "47550060152") 196 | assertSame(UInt64.parseUInt64(uint64_2.toString()), uint64_2) 197 | 198 | const uint64_3:UInt64 = new UInt64(0x12345678, 0xabcdef12) 199 | assertSame(uint64_3.toString(), "12379813811605231224") 200 | assertSame(UInt64.parseUInt64(uint64_3.toString()), uint64_3) 201 | 202 | const uint64_4:UInt64 = new UInt64(0x12345678, 0xbb) 203 | assertSame(uint64_4.toString(), "803464304248") 204 | assertSame(UInt64.parseUInt64(uint64_4.toString()), uint64_4) 205 | 206 | const uint64_5:UInt64 = new UInt64(0, 0) 207 | assertSame(uint64_5.toString(), "0") 208 | assertSame(UInt64.parseUInt64(uint64_5.toString()), uint64_5) 209 | 210 | const uint64_6:UInt64 = UInt64.fromNumber(123234) 211 | assertSame(uint64_6.toNumber(), 123234) 212 | assertSame(uint64_6.toString(), "123234") 213 | assertSame(UInt64.parseUInt64(uint64_6.toString()), uint64_6) 214 | 215 | const uint64_7:UInt64 = UInt64.fromNumber(184942424123234000) 216 | assertSame(uint64_7.toNumber(), 184942424123233984) 217 | assertSame(uint64_7.toString(), "184942424123233984") 218 | assertSame(UInt64.parseUInt64(uint64_7.toString()), uint64_7) 219 | 220 | const uint64_8:UInt64 = UInt64.fromNumber(1494242414000) 221 | assertSame(uint64_8.toNumber(), 1494242414000) 222 | assertSame(uint64_8.toString(), "1494242414000") 223 | assertSame(UInt64.parseUInt64(uint64_8.toString()), uint64_8) 224 | 225 | const t0:TestAllTypes = new TestAllTypes 226 | t0[1500938] = stringToByteArray('\n') 227 | test(t0) 228 | testText(t0) 229 | 230 | const t1:TestPackedTypes = new TestPackedTypes 231 | t1.packedDouble.push(1.23424353, 2.12) 232 | t1.packedEnum.push(ForeignEnum.FOREIGN_BAZ, ForeignEnum.FOREIGN_FOO, 233 | ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_BAR) 234 | test(t1) 235 | testText(t1) 236 | 237 | const t2:TestPackedExtensions = new TestPackedExtensions 238 | t2[PACKED_DOUBLE_EXTENSION] = [324.234, 1.23424353, 2.12] 239 | t2[PACKED_ENUM_EXTENSION] =[ForeignEnum.FOREIGN_BAZ, 240 | ForeignEnum.FOREIGN_FOO, ForeignEnum.FOREIGN_FOO] 241 | test(t2) 242 | testText(t2) 243 | 244 | const t3:TestAllTypes = new TestAllTypes 245 | t3.optionalString = "111foo" 246 | t3.defaultNestedEnum = NestedEnum.FOO 247 | t3.repeatedNestedMessage.push(new NestedMessage) 248 | t3.repeatedNestedMessage.push(new NestedMessage) 249 | t3.repeatedNestedMessage[1].bb = 123 250 | t3.optionalInt32 = -23412413 251 | t3.optionalDouble = 123.456 252 | t3.repeatedNestedEnum.push(NestedEnum.FOO) 253 | t3.repeatedNestedEnum.push(NestedEnum.BAR) 254 | t3.optionalNestedMessage = new NestedMessage 255 | t3.optionalNestedMessage.bb = 234 256 | t3.optionalSint32 = -3 257 | t3.optionalSint64 = new Int64(199999999, 199999999) 258 | test(t3) 259 | testText(t3) 260 | const t4:TestPackedTypes = new TestPackedTypes 261 | t4.packedDouble.push(1) 262 | test(t4) 263 | testText(t4) 264 | const t5:TestAllTypes = new TestAllTypes 265 | t5.optionalSint32 = -199999999 266 | test(t5) 267 | testText(t5) 268 | const t6:TestAllTypes = new TestAllTypes 269 | t6.optionalInt64 = new Int64(uint(-185754567), -198741265) 270 | test(t6) 271 | testText(t6) 272 | const t7:TestAllTypes = new TestAllTypes 273 | const s64:Int64 = new Int64(uint(-171754567), -198741265) 274 | t7.optionalSint64 = s64 275 | t7.optionalInt64 = new Int64(ZigZag.encode64low(s64.low, s64.high), 276 | ZigZag.encode64high(s64.low, s64.high)) 277 | t7.optionalInt32 = -1 278 | t7[41192] = UInt64.parseUInt64("12343245732475923") // Unknown Varint 279 | t7[631669] = 12345325 // Unknown Fixed32 280 | t7[631677] = [234, 234, 123222, 12345325] // Unknown Fixed32 281 | t7[1500930] = stringToByteArray("Hello\u00C4\u00C3xxx") // Unknown Length Delimited 282 | t7[1500938] = [stringToByteArray("Hello"), stringToByteArray("World")] // Unknown Length Delimited 283 | testText(t7) 284 | test(t7) 285 | const t9:TestAllTypes = new TestAllTypes 286 | t9.optionalSint64 = new Int64(uint(-171754567), -198741265) 287 | test(t9) 288 | testText(t9) 289 | const t10:AAA = new AAA 290 | assertSame(t10[DDD], "dream") 291 | t10.s = "xxxx" 292 | t10[DDD] = "love" 293 | test(t10) 294 | testText(t10) 295 | if (haxeTest) { 296 | import flash.net.registerClassAlias 297 | registerClassAlias("中文名", AAA) 298 | registerClassAlias("com.dongxiguo.Foo", BBB) 299 | } 300 | testAmf(t10) 301 | 302 | const t11:BBB = new BBB 303 | t11.aaa = new AAA 304 | t11.aaa.s = "1234xxx" 305 | t11.aaa.bbb = [ new BBB ] 306 | t11.i = 1234 307 | test(t11) 308 | testText(t11) 309 | testAmf(t11) 310 | testAmf([t10, t11, t10]) 311 | 312 | 313 | const t12:TestAllExtensions = new TestAllExtensions 314 | t12[REPEATED_STRING_EXTENSION] = ["aaaa", "bbb"] 315 | test(t12) 316 | testText(t12); 317 | 318 | [Embed(source = "../../../../../unittest.bin", 319 | mimeType="application/octet-stream")] 320 | const UNITTEST_BIN:Class 321 | const descriptors:FileDescriptorSet = new FileDescriptorSet 322 | descriptors.mergeFrom(new UNITTEST_BIN) 323 | test(descriptors) 324 | testText(descriptors) 325 | 326 | const t13:CCC = new CCC 327 | t13.ccc = "我爱北京天安门" 328 | testText(t13) 329 | 330 | trace("All tests pass.") 331 | fscommand("quit") 332 | } 333 | } 334 | 335 | } 336 | // vim: ts=4 sw=4 337 | -------------------------------------------------------------------------------- /test/issue12.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package issue12; 9 | 10 | message Message { 11 | enum Type { 12 | Chat = 100; 13 | } 14 | 15 | extensions 100 to 200; 16 | } 17 | 18 | message ChatMessage { 19 | extend Message { 20 | optional ChatMessage my_type = 100; 21 | } 22 | optional string text = 1; 23 | } 24 | -------------------------------------------------------------------------------- /test/issue16.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package issue16; 9 | 10 | message Test{ 11 | message Nested{ 12 | required int32 id = 1; 13 | } 14 | repeated Nested nested = 1; 15 | } 16 | -------------------------------------------------------------------------------- /test/issue21.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2011 , Yang Bo. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package issue21; 9 | 10 | message Test1 { 11 | optional string tag = 1; 12 | } 13 | message Test2 { 14 | repeated string tag = 1; 15 | } 16 | message Test3 { 17 | required string tag = 1; 18 | } 19 | message Test4 { 20 | required Test1 tag = 1; 21 | } 22 | message Test5 { 23 | repeated Test1 tag = 1; 24 | } 25 | message Test6 { 26 | optional Test1 tag = 1; 27 | } 28 | message Test7 { 29 | repeated double tag = 1 [ packed = true ]; 30 | } 31 | -------------------------------------------------------------------------------- /test/no_package.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | import "google/protobuf/descriptor.proto"; 8 | import "options.proto"; 9 | 10 | message NoPackageRequest {} 11 | message NoPackageResponse { 12 | extensions 1 to max; 13 | } 14 | 15 | service NoPackageService { 16 | rpc Call (NoPackageRequest) returns (NoPackageResponse) { 17 | option (my_option2) = "option-value"; 18 | } 19 | option (as3_client_side_service) = true; 20 | option (as3_server_side_service) = true; 21 | } 22 | 23 | extend .NoPackageResponse { 24 | required int32 foo = 1; 25 | } 26 | 27 | extend .google.protobuf.MethodOptions { 28 | optional string my_option2 = 2222; 29 | } 30 | 31 | -------------------------------------------------------------------------------- /test/test.proto: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2010 , NetEase.com,Inc. All rights reserved. 2 | // 3 | // Author: Yang Bo (pop.atry@gmail.com) 4 | // 5 | // Use, modification and distribution are subject to the "New BSD License" 6 | // as listed at . 7 | 8 | package test; 9 | import "options.proto"; 10 | import "issue12.proto"; 11 | import "google/protobuf/descriptor.proto"; 12 | extend google.protobuf.MethodOptions { 13 | optional string my_option = 3333; 14 | } 15 | message AAA { 16 | option (as3_bindable) = true; 17 | option (as3_amf_alias) = "myTest.MyAAA.汉字"; 18 | optional bytes b = 1 [ default = "\x01\x02\x80\xFF\x7F\0\x34" ]; 19 | optional string s = 2 [ default = "汉字测试\0\x34" ]; 20 | repeated BBB bbb = 3; 21 | extensions 300 to max; 22 | } 23 | message BBB { 24 | option (as3_bindable) = true; 25 | option (as3_amf_auto_alias) = true; 26 | optional AAA aaa = 1; 27 | optional int32 i = 2; 28 | } 29 | message CCC { 30 | required string ccc = 1; 31 | extend AAA { 32 | optional string viny = 301 [default = "viny"]; 33 | } 34 | } 35 | 36 | service Foo { 37 | rpc Search (BBB) returns (AAA) { 38 | option (my_option) = "protoc-gen-as3"; 39 | } 40 | } 41 | 42 | service Bar { 43 | rpc GoodMorning (CCC) returns (AAA); 44 | option (as3_server_side_service) = true; 45 | } 46 | 47 | service Baz { 48 | rpc HelloWorld (CCC) returns (BBB); 49 | option (as3_client_side_service) = false; 50 | } 51 | 52 | service ServerOnly { 53 | rpc HelloWorld (AAA) returns (BBB); 54 | option (as3_client_side_service) = false; 55 | option (as3_server_side_service) = true; 56 | } 57 | extend AAA { 58 | optional string ddd = 300 [default = "dream"]; 59 | } 60 | --------------------------------------------------------------------------------