public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ImageToLua.LuaSetSprite); luaRuntime.RegLuaBridgeFunction(ImageToLua.LuaSetSpriteAsync); luaRuntime.RegLuaBridgeFunction(ImageToLua.LuaSetSpriteAtlas); luaRuntime.RegLuaBridgeFunction(ImageToLua.LuaSetSpriteAtlasAsync); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local type = type local error = error local string = string module 'protobuf.type_checkers' function TypeChecker(acceptable_types) local acceptable_types = acceptable_types return function(proposed_value) local t = type(proposed_value) if acceptable_types[type(proposed_value)] == nil then error(string.format('%s has type %s, but expected one of: %s', proposed_value, type(proposed_value), acceptable_types)) end end end function Int32ValueChecker() local _MIN = -2147483648 local _MAX = 2147483647 return function(proposed_value) if type(proposed_value) ~= 'number' then error(string.format('%s has type %s, but expected one of: number', proposed_value, type(proposed_value))) end if _MIN > proposed_value or proposed_value > _MAX then error('Value out of range: '..proposed_value) end end end function Uint32ValueChecker(IntValueChecker) local _MIN = 0 local _MAX = 0xffffffff return function(proposed_value) if type(proposed_value) ~= 'number' then error(string.format('%s has type %s, but expected one of: number', proposed_value, type(proposed_value))) end if _MIN > proposed_value or proposed_value > _MAX then error('Value out of range: '..proposed_value) end end end function UnicodeValueChecker() return function(proposed_value) if type(proposed_value) ~= 'string' then error(string.format('%s has type %s, but expected one of: string', proposed_value, type(proposed_value))) end end end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(DropdownToLua.LuaGetValue); luaRuntime.RegLuaBridgeFunction(DropdownToLua.LuaAddOption); luaRuntime.RegLuaBridgeFunction(DropdownToLua.LuaClearOptions); luaRuntime.RegLuaBridgeFunction(DropdownToLua.LuaAddClick); luaRuntime.RegLuaBridgeFunction(DropdownToLua.LuaGetOption); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(GiantVoicePlayerToLua.RegisterPlayVoiceFunction); luaRuntime.RegLuaBridgeFunction(GiantVoicePlayerToLua.RegisterStopVoiceFunction); luaRuntime.RegLuaBridgeFunction(GiantVoicePlayerToLua.SetVoiceWords); luaRuntime.RegLuaBridgeFunction(GiantVoicePlayerToLua.SetRecordTime); luaRuntime.RegLuaBridgeFunction(GiantVoicePlayerToLua.SetVoiceBarLength); }
/// <summary> /// Creates a new LuaEnvironment without initializing the state, for use with a derived type. /// </summary> protected LuaEnvironmentNet() { _compiler = new CodeCompiler(); _parser = new PlainParser(); _runtime = LuaRuntimeNet.Create(this); Settings = new LuaSettings().AsReadOnly(); _globals = new LuaValues.LuaTable(); _modules = new ModuleBinder(); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaGetText); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaSetText); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaGetTextColor); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaSetTextColor); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaGetFontSize); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaSetFontSize); luaRuntime.RegLuaBridgeFunction(LabelToLua.LuaSetHrefClick); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaGetText); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaSetText); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaGetInputType); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaSetInputType); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaGetKeyboardType); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaSetKeyboardType); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaGetLineType); luaRuntime.RegLuaBridgeFunction(InputFieldToLua.LuaSetLineType); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"module 'protobuf.descriptor' FieldDescriptor = { TYPE_DOUBLE = 1, TYPE_FLOAT = 2, TYPE_INT64 = 3, TYPE_UINT64 = 4, TYPE_INT32 = 5, TYPE_FIXED64 = 6, TYPE_FIXED32 = 7, TYPE_BOOL = 8, TYPE_STRING = 9, TYPE_GROUP = 10, TYPE_MESSAGE = 11, TYPE_BYTES = 12, TYPE_UINT32 = 13, TYPE_ENUM = 14, TYPE_SFIXED32 = 15, TYPE_SFIXED64 = 16, TYPE_SINT32 = 17, TYPE_SINT64 = 18, MAX_TYPE = 18, --Must be consistent with C++FieldDescriptor::CppType enum in -- descriptor.h. -- CPPTYPE_INT32 = 1, CPPTYPE_INT64 = 2, CPPTYPE_UINT32 = 3, CPPTYPE_UINT64 = 4, CPPTYPE_DOUBLE = 5, CPPTYPE_FLOAT = 6, CPPTYPE_BOOL = 7, CPPTYPE_ENUM = 8, CPPTYPE_STRING = 9, CPPTYPE_MESSAGE = 10, MAX_CPPTYPE = 10, -- Must be consistent with C++ FieldDescriptor::Label enum in -- descriptor.h. -- LABEL_OPTIONAL = 1, LABEL_REQUIRED = 2, LABEL_REPEATED = 3, MAX_LABEL = 3 }"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { /*luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.StartRecordVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.StopRecordVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.CancelRecordVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.PlayVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.PauseVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.ResumeVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.StopVoice); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.SetPlayVolume); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.GetCurrentPlayPosition); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.IsRecording); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.IsVoicePlaying); * luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.IsVoicePause);*/ luaRuntime.RegLuaBridgeFunction(GiantVoiceToLua.Clear); }
/// <summary> /// Creates a new environment with the given settings. /// </summary> /// <param name="settings">The settings to give the Environment.</param> /// <exception cref="System.ArgumentNullException">If settings is null.</exception> public LuaEnvironmentNet(LuaSettings settings) { if (settings == null) { throw new ArgumentNullException(nameof(settings)); } _globals = new LuaTable(); _runtime = LuaRuntimeNet.Create(this); _compiler = new CodeCompiler(); _parser = new PlainParser(); _modules = new ModuleBinder(); Settings = settings.AsReadOnly(); // initialize the global variables. LuaStaticLibraries.Initialize(this); _initializeTypes(); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetVisible); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaSetVisible); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetUserData); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaSetUserData); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaFind); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaCloneElement); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetIndex); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaMoveToFirst); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaMoveToLast); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaMoveTo); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaOnClick); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaOnDragging); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetPosition); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaSetPosition); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetScale); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaSetScale); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaSetEulerAngles); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaGetEulerAngles); luaRuntime.RegLuaBridgeFunction(ElementToLua.LuaAddAnimation); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local setmetatable = setmetatable module 'protobuf.listener' local _null_listener = { Modified = function() end } function NullMessageListener() return _null_listener end local _listener_meta = { Modified = function(self) if self.dirty then return end if self._parent_message then self._parent_message:_Modified() end end } _listener_meta.__index = _listener_meta function Listener(parent_message) local o = { } o.__mode = 'v' o._parent_message = parent_message o.dirty = false return setmetatable(o, _listener_meta) end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ScrollViewToLua.LuaGetItems); luaRuntime.RegLuaBridgeFunction(ScrollViewToLua.LuaScrollToBottom); luaRuntime.RegLuaBridgeFunction(ScrollViewToLua.LuaScrollToTop); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local string = string local table = table local assert = assert local ipairs = ipairs local error = error local print = print local pb = require 'pb' local encoder = require 'protobuf.encoder' local wire_format = require 'protobuf.wire_format' module 'protobuf.decoder' local _DecodeVarint = pb.varint_decoder local _DecodeSignedVarint = pb.signed_varint_decoder local _DecodeVarint32 = pb.varint_decoder local _DecodeSignedVarint32 = pb.signed_varint_decoder local _DecodeVarint64 = pb.varint_decoder64 local _DecodeSignedVarint64 = pb.signed_varint_decoder64 ReadTag = pb.read_tag local function _SimpleDecoder(wire_type, decode_value) return function(field_number, is_repeated, is_packed, key, new_default) if is_packed then local DecodeVarint = _DecodeVarint return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end local endpoint endpoint, pos = DecodeVarint(buffer, pos) endpoint = endpoint + pos if endpoint > pend then error('Truncated message.') end local element while pos < endpoint do element, pos = decode_value(buffer, pos) value[#value + 1] = element end if pos > endpoint then value:remove(#value) error('Packed element was truncated.') end return pos end elseif is_repeated then local tag_bytes = encoder.TagBytes(field_number, wire_type) local tag_len = #tag_bytes local sub = string.sub return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end while 1 do local element, new_pos = decode_value(buffer, pos) value:append(element) pos = new_pos + tag_len if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos >= pend then if new_pos > pend then error('Truncated message.') end return new_pos end end end else return function(buffer, pos, pend, message, field_dict) field_dict[key], pos = decode_value(buffer, pos) if pos > pend then field_dict[key] = nil error('Truncated message.') end return pos end end end end local function _ModifiedDecoder(wire_type, decode_value, modify_value) local InnerDecode = function(buffer, pos) local result, new_pos = decode_value(buffer, pos) return modify_value(result), new_pos end return _SimpleDecoder(wire_type, InnerDecode) end local function _StructPackDecoder(wire_type, value_size, format) local struct_unpack = pb.struct_unpack function InnerDecode(buffer, pos) local new_pos = pos + value_size local result = struct_unpack(format, buffer, pos) return result, new_pos end return _SimpleDecoder(wire_type, InnerDecode) end local function _Boolean(value) return value ~= 0 end Int32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint32) EnumDecoder = Int32Decoder Int64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeSignedVarint64) UInt32Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32) UInt64Decoder = _SimpleDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint64) SInt32Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint32, wire_format.ZigZagDecode32) SInt64Decoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint64, wire_format.ZigZagDecode64) Fixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I')) Fixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q')) SFixed32Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i')) SFixed64Decoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q')) FloatDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f')) DoubleDecoder = _StructPackDecoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d')) BoolDecoder = _ModifiedDecoder(wire_format.WIRETYPE_VARINT, _DecodeVarint, _Boolean) function StringDecoder(field_number, is_repeated, is_packed, key, new_default) local DecodeVarint = _DecodeVarint local sub = string.sub -- local unicode = unicode assert(not is_packed) if is_repeated then local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local tag_len = #tag_bytes return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end while 1 do local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated string.') end value:append(sub(buffer, pos + 1, new_pos)) pos = new_pos + tag_len if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then return new_pos end end end else return function(buffer, pos, pend, message, field_dict) local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated string.') end field_dict[key] = sub(buffer, pos + 1, new_pos) return new_pos end end end function BytesDecoder(field_number, is_repeated, is_packed, key, new_default) local DecodeVarint = _DecodeVarint local sub = string.sub assert(not is_packed) if is_repeated then local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local tag_len = #tag_bytes return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end while 1 do local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated string.') end value:append(sub(buffer, pos + 1, new_pos)) pos = new_pos + tag_len if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then return new_pos end end end else return function(buffer, pos, pend, message, field_dict) local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated string.') end field_dict[key] = sub(buffer, pos + 1, new_pos) return new_pos end end end function MessageDecoder(field_number, is_repeated, is_packed, key, new_default) local DecodeVarint = _DecodeVarint local sub = string.sub assert(not is_packed) if is_repeated then local tag_bytes = encoder.TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local tag_len = #tag_bytes return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end while 1 do local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated message.') end if value:add():_InternalParse(buffer, pos, new_pos) ~= new_pos then error('Unexpected end-group tag.') end pos = new_pos + tag_len if sub(buffer, new_pos + 1, pos) ~= tag_bytes or new_pos == pend then return new_pos end end end else return function(buffer, pos, pend, message, field_dict) local value = field_dict[key] if value == nil then value = new_default(message) field_dict[key] = value end local size, new_pos size, pos = DecodeVarint(buffer, pos) new_pos = pos + size if new_pos > pend then error('Truncated message.') end if value:_InternalParse(buffer, pos, new_pos) ~= new_pos then error('Unexpected end-group tag.') end return new_pos end end end function _SkipVarint(buffer, pos, pend) local value value, pos = _DecodeVarint(buffer, pos) return pos end function _SkipFixed64(buffer, pos, pend) pos = pos + 8 if pos > pend then error('Truncated message.') end return pos end function _SkipLengthDelimited(buffer, pos, pend) local size size, pos = _DecodeVarint(buffer, pos) pos = pos + size if pos > pend then error('Truncated message.') end return pos end function _SkipFixed32(buffer, pos, pend) pos = pos + 4 if pos > pend then error('Truncated message.') end return pos end function _RaiseInvalidWireType(buffer, pos, pend) error('Tag had invalid wire type.') end function _FieldSkipper() WIRETYPE_TO_SKIPPER = { _SkipVarint, _SkipFixed64, _SkipLengthDelimited, _SkipGroup, _EndGroup, _SkipFixed32, _RaiseInvalidWireType, _RaiseInvalidWireType, } --wiretype_mask = wire_format.TAG_TYPE_MASK local ord = string.byte local sub = string.sub return function(buffer, pos, pend, tag_bytes) local wire_type = ord(sub(tag_bytes, 1, 1)) % 8 + 1 return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, pend) end end SkipField = _FieldSkipper()"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local string = string local table = table local ipairs = ipairs local assert =assert local pb = require 'pb' local wire_format = require 'protobuf.wire_format' module 'protobuf.encoder' function _VarintSize(value) if value <= 0x7f then return 1 end if value <= 0x3fff then return 2 end if value <= 0x1fffff then return 3 end if value <= 0xfffffff then return 4 end return 5 end function _SignedVarintSize(value) if value < 0 then return 10 end if value <= 0x7f then return 1 end if value <= 0x3fff then return 2 end if value <= 0x1fffff then return 3 end if value <= 0xfffffff then return 4 end return 5 end function _TagSize(field_number) return _VarintSize(wire_format.PackTag(field_number, 0)) end function _SimpleSizer(compute_value_size) return function(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) if is_packed then local VarintSize = _VarintSize return function(value) local result = 0 for _, element in ipairs(value) do result = result + compute_value_size(element) end return result + VarintSize(result) + tag_size end elseif is_repeated then return function(value) local result = tag_size * #value for _, element in ipairs(value) do result = result + compute_value_size(element) end return result end else return function(value) return tag_size + compute_value_size(value) end end end end function _ModifiedSizer(compute_value_size, modify_value) return function(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) if is_packed then local VarintSize = _VarintSize return function(value) local result = 0 for _, element in ipairs(value) do result = result + compute_value_size(modify_value(element)) end return result + VarintSize(result) + tag_size end elseif is_repeated then return function(value) local result = tag_size * #value for _, element in ipairs(value) do result = result + compute_value_size(modify_value(element)) end return result end else return function(value) return tag_size + compute_value_size(modify_value(value)) end end end end function _FixedSizer(value_size) return function(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) if is_packed then local VarintSize = _VarintSize return function(value) local result = #value * value_size return result + VarintSize(result) + tag_size end elseif is_repeated then local element_size = value_size + tag_size return function(value) return #value * element_size end else local field_size = value_size + tag_size return function(value) return field_size end end end end Int32Sizer = _SimpleSizer(_SignedVarintSize) Int64Sizer = Int32Sizer EnumSizer = Int32Sizer UInt32Sizer = _SimpleSizer(_VarintSize) UInt64Sizer = UInt32Sizer SInt32Sizer = _ModifiedSizer(_SignedVarintSize, wire_format.ZigZagEncode) SInt64Sizer = SInt32Sizer Fixed32Sizer = _FixedSizer(4) SFixed32Sizer = Fixed32Sizer FloatSizer = Fixed32Sizer Fixed64Sizer = _FixedSizer(8) SFixed64Sizer = Fixed64Sizer DoubleSizer = Fixed64Sizer BoolSizer = _FixedSizer(1) function StringSizer(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) local VarintSize = _VarintSize assert(not is_packed) if is_repeated then return function(value) local result = tag_size * #value for _, element in ipairs(value) do local l = #element result = result + VarintSize(l) + l end return result end else return function(value) local l = #value return tag_size + VarintSize(l) + l end end end function BytesSizer(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) local VarintSize = _VarintSize assert(not is_packed) if is_repeated then return function(value) local result = tag_size * #value for _, element in ipairs(value) do local l = #element result = result + VarintSize(l) + l end return result end else return function(value) local l = #value return tag_size + VarintSize(l) + l end end end function MessageSizer(field_number, is_repeated, is_packed) local tag_size = _TagSize(field_number) local VarintSize = _VarintSize assert(not is_packed) if is_repeated then return function(value) local result = tag_size * #value for _, element in ipairs(value) do local l = element:ByteSize() result = result + VarintSize(l) + l end return result end else return function(value) local l = value:ByteSize() return tag_size + VarintSize(l) + l end end end -- ==================================================================== --Encoders! local _EncodeVarint = pb.varint_encoder local _EncodeSignedVarint = pb.signed_varint_encoder local _EncodeVarint64 = pb.varint_encoder64 local _EncodeSignedVarint64 = pb.signed_varint_encoder64 function _VarintBytes(value) local out = { } local write = function(value) out[#out + 1 ] = value end _EncodeSignedVarint(write, value) return table.concat(out) end function TagBytes(field_number, wire_type) return _VarintBytes(wire_format.PackTag(field_number, wire_type)) end function _SimpleEncoder(wire_type, encode_value, compute_value_size) return function(field_number, is_repeated, is_packed) if is_packed then local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint return function(write, value) write(tag_bytes) local size = 0 for _, element in ipairs(value) do size = size + compute_value_size(element) end EncodeVarint(write, size) for element in value do encode_value(write, element) end end elseif is_repeated then local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) for _, element in ipairs(value) do write(tag_bytes) encode_value(write, element) end end else local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) write(tag_bytes) encode_value(write, value) end end end end function _ModifiedEncoder(wire_type, encode_value, compute_value_size, modify_value) return function(field_number, is_repeated, is_packed) if is_packed then local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint return function(write, value) write(tag_bytes) local size = 0 for _, element in ipairs(value) do size = size + compute_value_size(modify_value(element)) end EncodeVarint(write, size) for _, element in ipairs(value) do encode_value(write, modify_value(element)) end end elseif is_repeated then local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) for _, element in ipairs(value) do write(tag_bytes) encode_value(write, modify_value(element)) end end else local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) write(tag_bytes) encode_value(write, modify_value(value)) end end end end function _StructPackEncoder(wire_type, value_size, format) return function(field_number, is_repeated, is_packed) local struct_pack = pb.struct_pack if is_packed then local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint return function(write, value) write(tag_bytes) EncodeVarint(write, #value * value_size) for _, element in ipairs(value) do struct_pack(write, format, element) end end elseif is_repeated then local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) for _, element in ipairs(value) do write(tag_bytes) struct_pack(write, format, element) end end else local tag_bytes = TagBytes(field_number, wire_type) return function(write, value) write(tag_bytes) struct_pack(write, format, value) end end end end Int32Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeSignedVarint, _SignedVarintSize) Int64Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeSignedVarint64, _SignedVarintSize) EnumEncoder = Int32Encoder UInt32Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize) UInt64Encoder = _SimpleEncoder(wire_format.WIRETYPE_VARINT, _EncodeVarint64, _VarintSize) SInt32Encoder = _ModifiedEncoder( wire_format.WIRETYPE_VARINT, _EncodeVarint, _VarintSize, wire_format.ZigZagEncode32) SInt64Encoder = _ModifiedEncoder( wire_format.WIRETYPE_VARINT, _EncodeVarint64, _VarintSize, wire_format.ZigZagEncode64) Fixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('I')) Fixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('Q')) SFixed32Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('i')) SFixed64Encoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('q')) FloatEncoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED32, 4, string.byte('f')) DoubleEncoder = _StructPackEncoder(wire_format.WIRETYPE_FIXED64, 8, string.byte('d')) function BoolEncoder(field_number, is_repeated, is_packed) local false_byte = '\0' local true_byte = '\1' if is_packed then local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint return function(write, value) write(tag_bytes) EncodeVarint(write, #value) for _, element in ipairs(value) do if element then write(true_byte) else write(false_byte) end end end elseif is_repeated then local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT) return function(write, value) for _, element in ipairs(value) do write(tag_bytes) if element then write(true_byte) else write(false_byte) end end end else local tag_bytes = TagBytes(field_number, wire_format.WIRETYPE_VARINT) return function(write, value) write(tag_bytes) if value then return write(true_byte) end return write(false_byte) end end end function StringEncoder(field_number, is_repeated, is_packed) local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint assert(not is_packed) if is_repeated then return function(write, value) for _, element in ipairs(value) do --encoded = element.encode('utf-8') write(tag) EncodeVarint(write, #element) write(element) end end else return function(write, value) -- local encoded = value.encode('utf-8') write(tag) EncodeVarint(write, #value) return write(value) end end end function BytesEncoder(field_number, is_repeated, is_packed) local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint assert(not is_packed) if is_repeated then return function(write, value) for _, element in ipairs(value) do write(tag) EncodeVarint(write, #element) write(element) end end else return function(write, value) write(tag) EncodeVarint(write, #value) return write(value) end end end function MessageEncoder(field_number, is_repeated, is_packed) local tag = TagBytes(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) local EncodeVarint = _EncodeVarint assert(not is_packed) if is_repeated then return function(write, value) for _, element in ipairs(value) do write(tag) EncodeVarint(write, element: ByteSize()) element: _InternalSerialize(write) end end else return function(write, value) write(tag) EncodeVarint(write, value: ByteSize()) return value: _InternalSerialize(write) end end end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(UISystemToLua.LuaCreateWindow); luaRuntime.RegLuaBridgeFunction(UISystemToLua.LuaCreateWindowAsync); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local pb = require 'pb' module 'protobuf.wire_format' WIRETYPE_VARINT = 0 WIRETYPE_FIXED64 = 1 WIRETYPE_LENGTH_DELIMITED = 2 WIRETYPE_START_GROUP = 3 WIRETYPE_END_GROUP = 4 WIRETYPE_FIXED32 = 5 _WIRETYPE_MAX = 5 -- yeah, we don't need uint64 local function _VarUInt64ByteSizeNoTag(uint64) if uint64 <= 0x7f then return 1 end if uint64 <= 0x3fff then return 2 end if uint64 <= 0x1fffff then return 3 end if uint64 <= 0xfffffff then return 4 end return 5 end function PackTag(field_number, wire_type) return field_number * 8 + wire_type end function UnpackTag(tag) local wire_type = tag % 8 return (tag - wire_type) / 8, wire_type end ZigZagEncode32 = pb.zig_zag_encode32 ZigZagDecode32 = pb.zig_zag_decode32 ZigZagEncode64 = pb.zig_zag_encode64 ZigZagDecode64 = pb.zig_zag_decode64 function Int32ByteSize(field_number, int32) return Int64ByteSize(field_number, int32) end function Int32ByteSizeNoTag(int32) return _VarUInt64ByteSizeNoTag(int32) end function Int64ByteSize(field_number, int64) return UInt64ByteSize(field_number, int64) end function UInt32ByteSize(field_number, uint32) return UInt64ByteSize(field_number, uint32) end function UInt64ByteSize(field_number, uint64) return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(uint64) end function SInt32ByteSize(field_number, int32) return UInt32ByteSize(field_number, ZigZagEncode(int32)) end function SInt64ByteSize(field_number, int64) return UInt64ByteSize(field_number, ZigZagEncode(int64)) end function Fixed32ByteSize(field_number, fixed32) return TagByteSize(field_number) + 4 end function Fixed64ByteSize(field_number, fixed64) return TagByteSize(field_number) + 8 end function SFixed32ByteSize(field_number, sfixed32) return TagByteSize(field_number) + 4 end function SFixed64ByteSize(field_number, sfixed64) return TagByteSize(field_number) + 8 end function FloatByteSize(field_number, flt) return TagByteSize(field_number) + 4 end function DoubleByteSize(field_number, double) return TagByteSize(field_number) + 8 end function BoolByteSize(field_number, b) return TagByteSize(field_number) + 1 end function EnumByteSize(field_number, enum) return UInt32ByteSize(field_number, enum) end function StringByteSize(field_number, string) return BytesByteSize(field_number, string) end function BytesByteSize(field_number, b) return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(#b) + #b end function MessageByteSize(field_number, message) return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(message.ByteSize()) + message.ByteSize() end function MessageSetItemByteSize(field_number, msg) local total_size = 2 * TagByteSize(1) + TagByteSize(2) + TagByteSize(3) total_size = total_size + _VarUInt64ByteSizeNoTag(field_number) local message_size = msg.ByteSize() total_size = total_size + _VarUInt64ByteSizeNoTag(message_size) total_size = total_size + message_size return total_size end function TagByteSize(field_number) return _VarUInt64ByteSizeNoTag(PackTag(field_number, 0)) end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local string = string local math = math local print = print local getmetatable = getmetatable local table = table local ipairs = ipairs local tostring = tostring local descriptor = require 'protobuf.descriptor' module 'protobuf.text_format' function format(buffer) local len = string.len(buffer) for i = 1, len, 16 do local text = '' for j = i, math.min(i + 16 - 1, len) do text = string.format('%s %02x', text, string.byte(buffer, j)) end print(text) end end local FieldDescriptor = descriptor.FieldDescriptor msg_format_indent = function(write, msg, indent) for field, value in msg:ListFields() do local print_field = function(field_value) local name = field.name write(string.rep('', indent)) if field.type == FieldDescriptor.TYPE_MESSAGE then local extensions = getmetatable(msg)._extensions_by_name if extensions[field.full_name] then write('['..name.. '] {\n') else write(name.. ' {\n') end msg_format_indent(write, field_value, indent + 4) write(string.rep('', indent)) write('}\n') else write(string.format('%s: %s\n', name, tostring(field_value))) end end if field.label == FieldDescriptor.LABEL_REPEATED then for _, k in ipairs(value) do print_field(k) end else print_field(value) end end end function msg_format(msg) local out = { } local write = function(value) out[#out + 1] = value end msg_format_indent(write, msg, 0) return table.concat(out) end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(WindowToLua.LuaCloseWindow); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(SliderToLua.LuaGetValue); luaRuntime.RegLuaBridgeFunction(SliderToLua.LuaSetValue); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ButtonToLua.LuaAddClick); luaRuntime.RegLuaBridgeFunction(ButtonToLua.LuaGetInteractable); luaRuntime.RegLuaBridgeFunction(ButtonToLua.LuaSetInteractable); }
/// <summary> /// Creates a new LuaEnvironment without initializing the state, /// for use with a derrived type. /// </summary> protected LuaEnvironmentNet() { this._compiler = new CodeCompiler(); this._parser = new PlainParser(); this._runtime = LuaRuntimeNet.Create(this); this.Settings = new LuaSettings().AsReadOnly(); this._globals = new LuaValues.LuaTable(); this._modules = new ModuleBinder(); }
/// <summary> /// Creates a new environment with the given settings. /// </summary> /// <param name="settings">The settings to give the Environment.</param> /// <exception cref="System.ArgumentNullException">If settings is null.</exception> public LuaEnvironmentNet(LuaSettings settings) { if (settings == null) throw new ArgumentNullException("settings"); this._globals = new LuaValues.LuaTable(); this._runtime = LuaRuntimeNet.Create(this); this._compiler = new CodeCompiler(); this._parser = new PlainParser(); this._modules = new ModuleBinder(); this.Settings = settings.AsReadOnly(); // initialize the global variables. LuaStaticLibraries.Initialize(this); InitializeTypes(); }
public HookManager(ILuaRuntime runtime) { _runtime = runtime; }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(GameAnalyticsToLua.OnEvent); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ImageAnimationToLua.LuaPlaySpriteAnimation); luaRuntime.RegLuaBridgeFunction(ImageAnimationToLua.LuaPauseSpriteAnimation); luaRuntime.RegLuaBridgeFunction(ImageAnimationToLua.LuaSetSpriteAnimation); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local setmetatable = setmetatable local table = table local rawset = rawset local error = error module 'protobuf.containers' local _RCFC_meta = { add = function(self) local value = self._message_descriptor._concrete_class() local listener = self._listener rawset(self, #self + 1, value) value:_SetListener(listener) if listener.dirty == false then listener:Modified() end return value end, remove = function(self, key) local listener = self._listener table.remove(self, key) listener: Modified() end, __newindex = function(self, key, value) error('RepeatedCompositeFieldContainer Cant set value directly') end } _RCFC_meta.__index = _RCFC_meta function RepeatedCompositeFieldContainer(listener, message_descriptor) local o = { _listener = listener, _message_descriptor = message_descriptor } return setmetatable(o, _RCFC_meta) end local _RSFC_meta = { append = function(self, value) self._type_checker(value) rawset(self, #self + 1, value) self._listener:Modified() end, remove = function(self, key) table.remove(self, key) self._listener:Modified() end, __newindex = function(self, key, value) error('RepeatedCompositeFieldContainer Cant set value directly') end } _RSFC_meta.__index = _RSFC_meta function RepeatedScalarFieldContainer(listener, type_checker) local o = { } o._listener = listener o._type_checker = type_checker return setmetatable(o, _RSFC_meta) end"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(TogglesToLua.LuaGetToggleOn); luaRuntime.RegLuaBridgeFunction(TogglesToLua.LuaSetToggleOn); luaRuntime.RegLuaBridgeFunction(TogglesToLua.LuaAddToggleChange); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(GiantVoiceRecorderToLua.RegisterStopRecordFunction); luaRuntime.RegLuaBridgeFunction(GiantVoiceRecorderToLua.RegisterCancelRecordFunction); }
public static void RegLuaBridgeFunction(ILuaRuntime luaRuntime) { luaRuntime.RegLuaBridgeFunction(ToggleToLua.LuaGetOn); luaRuntime.RegLuaBridgeFunction(ToggleToLua.LuaSetOn); luaRuntime.RegLuaBridgeFunction(ToggleToLua.LuaOnChange); }
public static void Install(ILuaRuntime luaRuntime) { string scriptString = @"local setmetatable = setmetatable local rawset = rawset local rawget = rawget local error = error local ipairs = ipairs local pairs = pairs local print = print local table = table local string = string local tostring = tostring local type = type local pb = require 'pb' local wire_format = require 'protobuf.wire_format' local type_checkers = require 'protobuf.type_checkers' local encoder = require 'protobuf.encoder' local decoder = require 'protobuf.decoder' local listener_mod = require 'protobuf.listener' local containers = require 'protobuf.containers' local descriptor = require 'protobuf.descriptor' local FieldDescriptor = descriptor.FieldDescriptor local text_format = require 'protobuf.text_format' module('protobuf') local function make_descriptor(name, descriptor, usable_key) local meta = { __newindex = function(self, key, value) if usable_key[key] then rawset(self, key, value) else error('error key: '..key) end end }; meta.__index = meta meta.__call = function() return setmetatable({ }, meta) end _M[name] = setmetatable(descriptor, meta); end make_descriptor('Descriptor', { }, { name = true, full_name = true, filename = true, containing_type = true, fields = true, nested_types = true, enum_types = true, extensions = true, options = true, is_extendable = true, extension_ranges = true, }) make_descriptor('FieldDescriptor', FieldDescriptor, { name = true, full_name = true, index = true, number = true, type = true, cpp_type = true, label = true, has_default_value = true, default_value = true, containing_type = true, message_type = true, enum_type = true, is_extension = true, extension_scope = true, }) make_descriptor('EnumDescriptor', { }, { name = true, full_name = true, values = true, containing_type = true, options = true }) make_descriptor('EnumValueDescriptor', { }, { name = true, index = true, number = true, type = true, options = true }) -- Maps from field type to expected wiretype. local FIELD_TYPE_TO_WIRE_TYPE = { [FieldDescriptor.TYPE_DOUBLE] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_FLOAT] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_INT64] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_UINT64] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_INT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_FIXED64] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_FIXED32] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_BOOL] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_STRING] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_GROUP] = wire_format.WIRETYPE_START_GROUP, [FieldDescriptor.TYPE_MESSAGE] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_BYTES] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_UINT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_ENUM] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_SFIXED32] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_SFIXED64] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_SINT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_SINT64] = wire_format.WIRETYPE_VARINT } local NON_PACKABLE_TYPES = { [FieldDescriptor.TYPE_STRING] = true, [FieldDescriptor.TYPE_GROUP] = true, [FieldDescriptor.TYPE_MESSAGE] = true, [FieldDescriptor.TYPE_BYTES] = true } local _VALUE_CHECKERS = { [FieldDescriptor.CPPTYPE_INT32] = type_checkers.Int32ValueChecker(), [FieldDescriptor.CPPTYPE_INT64] = type_checkers.TypeChecker({string = true, number = true}), [FieldDescriptor.CPPTYPE_UINT32] = type_checkers.Uint32ValueChecker(), [FieldDescriptor.CPPTYPE_UINT64] = type_checkers.TypeChecker({string = true, number = true}), [FieldDescriptor.CPPTYPE_DOUBLE] = type_checkers.TypeChecker({number = true}), [FieldDescriptor.CPPTYPE_FLOAT] = type_checkers.TypeChecker({number = true}), [FieldDescriptor.CPPTYPE_BOOL] = type_checkers.TypeChecker({boolean = true, bool = true, int=true}), [FieldDescriptor.CPPTYPE_ENUM] = type_checkers.Int32ValueChecker(), [FieldDescriptor.CPPTYPE_STRING] = type_checkers.TypeChecker({string = true}) } local TYPE_TO_BYTE_SIZE_FN = { [FieldDescriptor.TYPE_DOUBLE] = wire_format.DoubleByteSize, [FieldDescriptor.TYPE_FLOAT] = wire_format.FloatByteSize, [FieldDescriptor.TYPE_INT64] = wire_format.Int64ByteSize, [FieldDescriptor.TYPE_UINT64] = wire_format.UInt64ByteSize, [FieldDescriptor.TYPE_INT32] = wire_format.Int32ByteSize, [FieldDescriptor.TYPE_FIXED64] = wire_format.Fixed64ByteSize, [FieldDescriptor.TYPE_FIXED32] = wire_format.Fixed32ByteSize, [FieldDescriptor.TYPE_BOOL] = wire_format.BoolByteSize, [FieldDescriptor.TYPE_STRING] = wire_format.StringByteSize, [FieldDescriptor.TYPE_GROUP] = wire_format.GroupByteSize, [FieldDescriptor.TYPE_MESSAGE] = wire_format.MessageByteSize, [FieldDescriptor.TYPE_BYTES] = wire_format.BytesByteSize, [FieldDescriptor.TYPE_UINT32] = wire_format.UInt32ByteSize, [FieldDescriptor.TYPE_ENUM] = wire_format.EnumByteSize, [FieldDescriptor.TYPE_SFIXED32] = wire_format.SFixed32ByteSize, [FieldDescriptor.TYPE_SFIXED64] = wire_format.SFixed64ByteSize, [FieldDescriptor.TYPE_SINT32] = wire_format.SInt32ByteSize, [FieldDescriptor.TYPE_SINT64] = wire_format.SInt64ByteSize } local TYPE_TO_ENCODER = { [FieldDescriptor.TYPE_DOUBLE] = encoder.DoubleEncoder, [FieldDescriptor.TYPE_FLOAT] = encoder.FloatEncoder, [FieldDescriptor.TYPE_INT64] = encoder.Int64Encoder, [FieldDescriptor.TYPE_UINT64] = encoder.UInt64Encoder, [FieldDescriptor.TYPE_INT32] = encoder.Int32Encoder, [FieldDescriptor.TYPE_FIXED64] = encoder.Fixed64Encoder, [FieldDescriptor.TYPE_FIXED32] = encoder.Fixed32Encoder, [FieldDescriptor.TYPE_BOOL] = encoder.BoolEncoder, [FieldDescriptor.TYPE_STRING] = encoder.StringEncoder, [FieldDescriptor.TYPE_GROUP] = encoder.GroupEncoder, [FieldDescriptor.TYPE_MESSAGE] = encoder.MessageEncoder, [FieldDescriptor.TYPE_BYTES] = encoder.BytesEncoder, [FieldDescriptor.TYPE_UINT32] = encoder.UInt32Encoder, [FieldDescriptor.TYPE_ENUM] = encoder.EnumEncoder, [FieldDescriptor.TYPE_SFIXED32] = encoder.SFixed32Encoder, [FieldDescriptor.TYPE_SFIXED64] = encoder.SFixed64Encoder, [FieldDescriptor.TYPE_SINT32] = encoder.SInt32Encoder, [FieldDescriptor.TYPE_SINT64] = encoder.SInt64Encoder } local TYPE_TO_SIZER = { [FieldDescriptor.TYPE_DOUBLE] = encoder.DoubleSizer, [FieldDescriptor.TYPE_FLOAT] = encoder.FloatSizer, [FieldDescriptor.TYPE_INT64] = encoder.Int64Sizer, [FieldDescriptor.TYPE_UINT64] = encoder.UInt64Sizer, [FieldDescriptor.TYPE_INT32] = encoder.Int32Sizer, [FieldDescriptor.TYPE_FIXED64] = encoder.Fixed64Sizer, [FieldDescriptor.TYPE_FIXED32] = encoder.Fixed32Sizer, [FieldDescriptor.TYPE_BOOL] = encoder.BoolSizer, [FieldDescriptor.TYPE_STRING] = encoder.StringSizer, [FieldDescriptor.TYPE_GROUP] = encoder.GroupSizer, [FieldDescriptor.TYPE_MESSAGE] = encoder.MessageSizer, [FieldDescriptor.TYPE_BYTES] = encoder.BytesSizer, [FieldDescriptor.TYPE_UINT32] = encoder.UInt32Sizer, [FieldDescriptor.TYPE_ENUM] = encoder.EnumSizer, [FieldDescriptor.TYPE_SFIXED32] = encoder.SFixed32Sizer, [FieldDescriptor.TYPE_SFIXED64] = encoder.SFixed64Sizer, [FieldDescriptor.TYPE_SINT32] = encoder.SInt32Sizer, [FieldDescriptor.TYPE_SINT64] = encoder.SInt64Sizer } local TYPE_TO_DECODER = { [FieldDescriptor.TYPE_DOUBLE] = decoder.DoubleDecoder, [FieldDescriptor.TYPE_FLOAT] = decoder.FloatDecoder, [FieldDescriptor.TYPE_INT64] = decoder.Int64Decoder, [FieldDescriptor.TYPE_UINT64] = decoder.UInt64Decoder, [FieldDescriptor.TYPE_INT32] = decoder.Int32Decoder, [FieldDescriptor.TYPE_FIXED64] = decoder.Fixed64Decoder, [FieldDescriptor.TYPE_FIXED32] = decoder.Fixed32Decoder, [FieldDescriptor.TYPE_BOOL] = decoder.BoolDecoder, [FieldDescriptor.TYPE_STRING] = decoder.StringDecoder, [FieldDescriptor.TYPE_GROUP] = decoder.GroupDecoder, [FieldDescriptor.TYPE_MESSAGE] = decoder.MessageDecoder, [FieldDescriptor.TYPE_BYTES] = decoder.BytesDecoder, [FieldDescriptor.TYPE_UINT32] = decoder.UInt32Decoder, [FieldDescriptor.TYPE_ENUM] = decoder.EnumDecoder, [FieldDescriptor.TYPE_SFIXED32] = decoder.SFixed32Decoder, [FieldDescriptor.TYPE_SFIXED64] = decoder.SFixed64Decoder, [FieldDescriptor.TYPE_SINT32] = decoder.SInt32Decoder, [FieldDescriptor.TYPE_SINT64] = decoder.SInt64Decoder } local FIELD_TYPE_TO_WIRE_TYPE = { [FieldDescriptor.TYPE_DOUBLE] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_FLOAT] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_INT64] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_UINT64] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_INT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_FIXED64] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_FIXED32] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_BOOL] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_STRING] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_GROUP] = wire_format.WIRETYPE_START_GROUP, [FieldDescriptor.TYPE_MESSAGE] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_BYTES] = wire_format.WIRETYPE_LENGTH_DELIMITED, [FieldDescriptor.TYPE_UINT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_ENUM] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_SFIXED32] = wire_format.WIRETYPE_FIXED32, [FieldDescriptor.TYPE_SFIXED64] = wire_format.WIRETYPE_FIXED64, [FieldDescriptor.TYPE_SINT32] = wire_format.WIRETYPE_VARINT, [FieldDescriptor.TYPE_SINT64] = wire_format.WIRETYPE_VARINT } local function IsTypePackable(field_type) return NON_PACKABLE_TYPES[field_type] == nil end local function GetTypeChecker(cpp_type, field_type) if (cpp_type == FieldDescriptor.CPPTYPE_STRING and field_type == FieldDescriptor.TYPE_STRING) then return type_checkers.UnicodeValueChecker() end return _VALUE_CHECKERS[cpp_type] end local function _DefaultValueConstructorForField(field) if field.label == FieldDescriptor.LABEL_REPEATED then if type(field.default_value) ~= 'table' or #(field.default_value) ~= 0 then error('Repeated field default value not empty list:' .. tostring(field.default_value)) end if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then local message_type = field.message_type return function(message) return containers.RepeatedCompositeFieldContainer(message._listener_for_children, message_type) end else local type_checker = GetTypeChecker(field.cpp_type, field.type) return function(message) return containers.RepeatedScalarFieldContainer(message._listener_for_children, type_checker) end end end if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then local message_type = field.message_type return function(message) result = message_type._concrete_class() result._SetListener(message._listener_for_children) return result end end return function(message) return field.default_value end end local function _AttachFieldHelpers(message_meta, field_descriptor) local is_repeated = (field_descriptor.label == FieldDescriptor.LABEL_REPEATED) local is_packed = (field_descriptor.has_options and field_descriptor.GetOptions().packed) rawset(field_descriptor, '_encoder', TYPE_TO_ENCODER[field_descriptor.type](field_descriptor.number, is_repeated, is_packed)) rawset(field_descriptor, '_sizer', TYPE_TO_SIZER[field_descriptor.type](field_descriptor.number, is_repeated, is_packed)) rawset(field_descriptor, '_default_constructor', _DefaultValueConstructorForField(field_descriptor)) local AddDecoder = function(wiretype, is_packed) local tag_bytes = encoder.TagBytes(field_descriptor.number, wiretype) message_meta._decoders_by_tag[tag_bytes] = TYPE_TO_DECODER[field_descriptor.type](field_descriptor.number, is_repeated, is_packed, field_descriptor, field_descriptor._default_constructor) end AddDecoder(FIELD_TYPE_TO_WIRE_TYPE[field_descriptor.type], False) if is_repeated and IsTypePackable(field_descriptor.type) then AddDecoder(wire_format.WIRETYPE_LENGTH_DELIMITED, True) end end local function _AddEnumValues(descriptor, message_meta) for _, enum_type in ipairs(descriptor.enum_types) do for _, enum_value in ipairs(enum_type.values) do message_meta._member[enum_value.name] = enum_value.number end end end local function _InitMethod(message_meta) return function() local self = { } self._cached_byte_size = 0 self._cached_byte_size_dirty = false self._fields = {} self._is_present_in_parent = false self._listener = listener_mod.NullMessageListener() self._listener_for_children = listener_mod.Listener(self) return setmetatable(self, message_meta) end end local function _AddPropertiesForRepeatedField(field, message_meta) local property_name = field.name message_meta._getter[property_name] = function(self) local field_value = self._fields[field] if field_value == nil then field_value = field._default_constructor(self) self._fields[field] = field_value if not self._cached_byte_size_dirty then message_meta._member._Modified(self) end end return field_value end message_meta._setter[property_name] = function(self) error('Assignment not allowed to repeated field ' .. property_name.. ' in protocol message object.') end end local function _AddPropertiesForNonRepeatedCompositeField(field, message_meta) local property_name = field.name local message_type = field.message_type message_meta._getter[property_name] = function(self) local field_value = self._fields[field] if field_value == nil then field_value = message_type._concrete_class() field_value:_SetListener(self._listener_for_children) self._fields[field] = field_value if not self._cached_byte_size_dirty then message_meta._member._Modified(self) end end return field_value end message_meta._setter[property_name] = function(self, new_value) error('Assignment not allowed to composite field' .. property_name.. 'in protocol message object.' ) end end local function _AddPropertiesForNonRepeatedScalarField(field, message) local property_name = field.name local type_checker = GetTypeChecker(field.cpp_type, field.type) local default_value = field.default_value message._getter[property_name] = function(self) local value = self._fields[field] if value ~= nil then return self._fields[field] else return default_value end end message._setter[property_name] = function(self, new_value) type_checker(new_value) self._fields[field] = new_value if not self._cached_byte_size_dirty then message._member._Modified(self) end end end local function _AddPropertiesForField(field, message_meta) constant_name = field.name:upper() .. '_FIELD_NUMBER' message_meta._member[constant_name] = field.number if field.label == FieldDescriptor.LABEL_REPEATED then _AddPropertiesForRepeatedField(field, message_meta) elseif field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then _AddPropertiesForNonRepeatedCompositeField(field, message_meta) else _AddPropertiesForNonRepeatedScalarField(field, message_meta) end end local _ED_meta = { __index = function(self, extension_handle) local _extended_message = rawget(self, '_extended_message') local value = _extended_message._fields[extension_handle] if value ~= nil then return value end if extension_handle.label == FieldDescriptor.LABEL_REPEATED then value = extension_handle._default_constructor(self._extended_message) elseif extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then value = extension_handle.message_type._concrete_class() value:_SetListener(_extended_message._listener_for_children) else return extension_handle.default_value end _extended_message._fields[extension_handle] = value return value end, __newindex = function(self, extension_handle, value) local _extended_message = rawget(self, '_extended_message') if (extension_handle.label == FieldDescriptor.LABEL_REPEATED or extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE) then error('Cannot assign to extension '.. extension_handle.full_name .. ' because it is a repeated or composite type.') end local type_checker = GetTypeChecker(extension_handle.cpp_type, extension_handle.type) type_checker.CheckValue(value) _extended_message._fields[extension_handle] = value _extended_message._Modified() end } local function _ExtensionDict(message) local o = { } o._extended_message = message return setmetatable(o, _ED_meta) end local function _AddPropertiesForFields(descriptor, message_meta) for _, field in ipairs(descriptor.fields) do _AddPropertiesForField(field, message_meta) end if descriptor.is_extendable then message_meta._getter.Extensions = function(self) return _ExtensionDict(self) end end end local function _AddPropertiesForExtensions(descriptor, message_meta) local extension_dict = descriptor._extensions_by_name for extension_name, extension_field in pairs(extension_dict) do local constant_name = string.upper(extension_name).. '_FIELD_NUMBER' message_meta._member[constant_name] = extension_field.number end end local function _AddStaticMethods(message_meta) message_meta._member.RegisterExtension = function(extension_handle) extension_handle.containing_type = message_meta._descriptor _AttachFieldHelpers(message_meta, extension_handle) if message_meta._extensions_by_number[extension_handle.number] == nil then message_meta._extensions_by_number[extension_handle.number] = extension_handle else error( string.format('Extensions ' % s' and '%s' both try to extend message type '%s' with field number %d.', extension_handle.full_name, actual_handle.full_name, message_meta._descriptor.full_name, extension_handle.number)) end message_meta._extensions_by_name[extension_handle.full_name] = extension_handle end message_meta._member.FromString = function(s) local message = message_meta._member.__call() message.MergeFromString(s) return message end end local function _IsPresent(descriptor, value) if descriptor.label == FieldDescriptor.LABEL_REPEATED then return value elseif descriptor.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then return value._is_present_in_parent else return true end end function sortFunc(a, b) return a.index<b.index end function pairsByKeys (t, f) local a = { } for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0-- iterator variable local iter = function()-- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end local function _AddListFieldsMethod(message_descriptor, message_meta) message_meta._member.ListFields = function(self) local list_field = function(fields) --local f, s, v = pairs(self._fields) local f, s, v = pairsByKeys(self._fields, sortFunc) local iter = function(a, i) while true do local descriptor, value = f(a, i) if descriptor == nil then return elseif _IsPresent(descriptor, value) then return descriptor, value end end end return iter, s, v end return list_field(self._fields) end end local function _AddHasFieldMethod(message_descriptor, message_meta) local singular_fields = { } for _, field in ipairs(message_descriptor.fields) do if field.label ~= FieldDescriptor.LABEL_REPEATED then singular_fields[field.name] = field end end message_meta._member.HasField = function (self, field_name) field = singular_fields[field_name] if field == nil then error('Protocol message has no singular '.. field_name.. ' field.') end if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then value = self._fields[field] return value ~= nil and value._is_present_in_parent else local valueTmp = self._fields[field] return valueTmp ~= nil end end end local function _AddClearFieldMethod(message_descriptor, message_meta) local singular_fields = { } for _, field in ipairs(message_descriptor.fields) do if field.label ~= FieldDescriptor.LABEL_REPEATED then singular_fields[field.name] = field end end message_meta._member.ClearField = function(self, field_name) field = singular_fields[field_name] if field == nil then error('Protocol message has no singular '.. field_name.. ' field.') end if self._fields[field] then self._fields[field] = nil end message_meta._member._Modified(self) end end local function _AddClearExtensionMethod(message_meta) message_meta._member.ClearExtension = function(self, extension_handle) if self._fields[extension_handle] == nil then self._fields[extension_handle] = nil end message_meta._member._Modified(self) end end local function _AddClearMethod(message_descriptor, message_meta) message_meta._member.Clear = function(self) self._fields = {} message_meta._member._Modified(self) end end local function _AddStrMethod(message_meta) local format = text_format.msg_format message_meta.__tostring = function(self) return format(self) end end local function _AddHasExtensionMethod(message_meta) message_meta._member.HasExtension = function(self, extension_handle) if extension_handle.label == FieldDescriptor.LABEL_REPEATED then error(extension_handle.full_name.. ' is repeated.') end if extension_handle.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then value = self._fields[extension_handle] return value ~= nil and value._is_present_in_parent else return self._fields[extension_handle] end end end local function _AddSetListenerMethod(message_meta) message_meta._member._SetListener = function(self, listener) if listener ~= nil then self._listener = listener_mod.NullMessageListener() else self._listener = listener end end end local function _AddByteSizeMethod(message_descriptor, message_meta) message_meta._member.ByteSize = function(self) if not self._cached_byte_size_dirty and self._cached_byte_size > 0 then return self._cached_byte_size end local size = 0 for field_descriptor, field_value in message_meta._member.ListFields(self) do size = field_descriptor._sizer(field_value) + size end self._cached_byte_size = size self._cached_byte_size_dirty = false self._listener_for_children.dirty = false return size end end local function _AddSerializeToStringMethod(message_descriptor, message_meta) message_meta._member.SerializeToString = function(self) if not message_meta._member.IsInitialized(self) then error('Message is missing required fields: ' .. table.concat(message_meta._member.FindInitializationErrors(self), ',')) end return message_meta._member.SerializePartialToString(self) end message_meta._member.SerializeToIOString = function(self, iostring) if not message_meta._member.IsInitialized(self) then error('Message is missing required fields: ' .. table.concat(message_meta._member.FindInitializationErrors(self), ',')) end return message_meta._member.SerializePartialToIOString(self, iostring) end end local function _AddSerializePartialToStringMethod(message_descriptor, message_meta) local concat = table.concat local _internal_serialize = function(self, write_bytes) for field_descriptor, field_value in message_meta._member.ListFields(self) do field_descriptor._encoder(write_bytes, field_value) end end local _serialize_partial_to_iostring = function(self, iostring) local w = iostring.write local write = function(value) w(iostring, value) end _internal_serialize(self, write) return end local _serialize_partial_to_string = function(self) local out = {} local write = function(value) out[#out + 1] = value end _internal_serialize(self, write) return concat(out) end message_meta._member._InternalSerialize = _internal_serialize message_meta._member.SerializePartialToIOString = _serialize_partial_to_iostring message_meta._member.SerializePartialToString = _serialize_partial_to_string end local function _AddMergeFromStringMethod(message_descriptor, message_meta) local ReadTag = decoder.ReadTag local SkipField = decoder.SkipField local decoders_by_tag = message_meta._decoders_by_tag local _internal_parse = function(self, buffer, pos, pend) message_meta._member._Modified(self) local field_dict = self._fields local tag_bytes, new_pos local field_decoder while pos ~= pend do tag_bytes, new_pos = ReadTag(buffer, pos) field_decoder = decoders_by_tag[tag_bytes] if field_decoder == nil then new_pos = SkipField(buffer, new_pos, pend, tag_bytes) if new_pos == -1 then return pos end pos = new_pos else pos = field_decoder(buffer, new_pos, pend, self, field_dict) end end return pos end message_meta._member._InternalParse = _internal_parse local merge_from_string = function(self, serialized) local length = #serialized if _internal_parse(self, serialized, 0, length) ~= length then error('Unexpected end-group tag.') end return length end message_meta._member.MergeFromString = merge_from_string message_meta._member.ParseFromString = function(self, serialized) message_meta._member.Clear(self) merge_from_string(self, serialized) end end local function _AddIsInitializedMethod(message_descriptor, message_meta) local required_fields = { } for _, field in ipairs(message_descriptor.fields) do if field.label == FieldDescriptor.LABEL_REQUIRED then required_fields[#required_fields + 1] = field end end message_meta._member.IsInitialized = function(self, errors) for _, field in ipairs(required_fields) do if self._fields[field] == nil or (field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE and not self._fields[field]._is_present_in_parent) then if errors ~= nil then errors[#errors + 1] = message_meta._member.FindInitializationErrors(self) end return false end end for field, value in pairs(self._fields) do if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then if field.label == FieldDescriptor.LABEL_REPEATED then for _, element in ipairs(value) do if not element: IsInitialized() then if errors ~= nil then errors[#errors + 1] = message_meta._member.FindInitializationErrors(self) end return false end end elseif value._is_present_in_parent and not value: IsInitialized() then if errors ~= nil then errors[#errors + 1] = message_meta._member.FindInitializationErrors(self) end return false end end end return true end message_meta._member.FindInitializationErrors = function(self) local errors = {} for _,field in ipairs(required_fields) do if not message_meta._member.HasField(self, field.name) then errors.append(field.name) end end for field, value in message_meta._member.ListFields(self) do if field.cpp_type == FieldDescriptor.CPPTYPE_MESSAGE then if field.is_extension then name = io:format('(%s)', field.full_name) else name = field.name end if field.label == FieldDescriptor.LABEL_REPEATED then for i, element in ipairs(value) do prefix = io:format('%s[%d].', name, i) sub_errors = element:FindInitializationErrors() for _, e in ipairs(sub_errors) do errors[#errors + 1] = prefix .. e end end else prefix = name.. '.' sub_errors = value:FindInitializationErrors() for _, e in ipairs(sub_errors) do errors[#errors + 1] = prefix .. e end end end end return errors end end local function _AddMergeFromMethod(message_meta) local LABEL_REPEATED = FieldDescriptor.LABEL_REPEATED local CPPTYPE_MESSAGE = FieldDescriptor.CPPTYPE_MESSAGE message_meta._member.MergeFrom = function(self, msg) assert(msg ~= self) message_meta._member._Modified(self) local fields = self._fields for field, value in pairs(msg._fields) do if field.label == LABEL_REPEATED or field.cpp_type == CPPTYPE_MESSAGE then field_value = fields[field] if field_value == nil then field_value = field._default_constructor(self) fields[field] = field_value end field_value: MergeFrom(value) else self._fields[field] = value end end end end local function _AddMessageMethods(message_descriptor, message_meta) _AddListFieldsMethod(message_descriptor, message_meta) _AddHasFieldMethod(message_descriptor, message_meta) _AddClearFieldMethod(message_descriptor, message_meta) if message_descriptor.is_extendable then _AddClearExtensionMethod(message_meta) _AddHasExtensionMethod(message_meta) end _AddClearMethod(message_descriptor, message_meta) -- _AddEqualsMethod(message_descriptor, message_meta) _AddStrMethod(message_meta) _AddSetListenerMethod(message_meta) _AddByteSizeMethod(message_descriptor, message_meta) _AddSerializeToStringMethod(message_descriptor, message_meta) _AddSerializePartialToStringMethod(message_descriptor, message_meta) _AddMergeFromStringMethod(message_descriptor, message_meta) _AddIsInitializedMethod(message_descriptor, message_meta) _AddMergeFromMethod(message_meta) end local function _AddPrivateHelperMethods(message_meta) local Modified = function(self) if not self._cached_byte_size_dirty then self._cached_byte_size_dirty = true self._listener_for_children.dirty = true self._is_present_in_parent = true self._listener:Modified() end end message_meta._member._Modified = Modified message_meta._member.SetInParent = Modified end local function property_getter(message_meta) local getter = message_meta._getter local member = message_meta._member return function(self, property) local g = getter[property] if g then return g(self) else return member[property] end end end local function property_setter(message_meta) local setter = message_meta._setter return function(self, property, value) local s = setter[property] if s then s(self, value) else error(property.. ' not found') end end end function _AddClassAttributesForNestedExtensions(descriptor, message_meta) local extension_dict = descriptor._extensions_by_name for extension_name, extension_field in pairs(extension_dict) do message_meta._member[extension_name] = extension_field end end local function Message(descriptor) local message_meta = {} message_meta._decoders_by_tag = {} rawset(descriptor, '_extensions_by_name', { }) for _, k in ipairs(descriptor.extensions) do descriptor._extensions_by_name[k.name] = k end rawset(descriptor, '_extensions_by_number', { }) for _, k in ipairs(descriptor.extensions) do descriptor._extensions_by_number[k.number] = k end message_meta._descriptor = descriptor message_meta._extensions_by_name = { } message_meta._extensions_by_number = {} message_meta._getter = {} message_meta._setter = {} message_meta._member = {} -- message_meta._name = descriptor.full_name local ns = setmetatable({ }, message_meta._member) message_meta._member.__call = _InitMethod(message_meta) message_meta._member.__index = message_meta._member message_meta._member.type = ns if rawget(descriptor, '_concrete_class') == nil then rawset(descriptor, '_concrete_class', ns) for k, field in ipairs(descriptor.fields) do _AttachFieldHelpers(message_meta, field) end end _AddEnumValues(descriptor, message_meta) _AddClassAttributesForNestedExtensions(descriptor, message_meta) _AddPropertiesForFields(descriptor, message_meta) _AddPropertiesForExtensions(descriptor, message_meta) _AddStaticMethods(message_meta) _AddMessageMethods(descriptor, message_meta) _AddPrivateHelperMethods(message_meta) message_meta.__index = property_getter(message_meta) message_meta.__newindex = property_setter(message_meta) return ns end _M.Message = Message"; luaRuntime.DoString(Encoding.UTF8.GetBytes(scriptString)); }