Using Protobuf on iOS Android devices (using dll Mode)

Source: Internet
Author: User
<span id="Label3"></p><p><p>http://game.ceeger.com/forum/read.php?tid=13479</p></p><p><p></p></p><p><p><strong>If your project can run in. Net 2.0 subset mode, See the methods in this Post.</strong></p></p><p><p>Address: http://game.ceeger.com/forum/read.php?tid=14359&fid=27</p></p><p><p></p></p><p><p></p></p><p><p>If you can only run Under. Net 2.0, you will continue to look down.</p></p><p><p>=============================================================</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Protobuf</span> is a <span style="font-family: ‘Times New Roman‘, serif;">Google</span> -customized protocol that makes it easy to serialize and deserialize Data. <span style="font-family: ‘Times New Roman‘, serif;">(</span> supports multiple inter-platform communication <span style="font-family: ‘Times New Roman‘, serif;">)</span></p></p><p><p></p></p><p><p>It can be used for local caching of data</p></p><p><p>and the transmission of network protocols, and so On.</p></p><p><p></p></p><p><p>To understand the <span style="font-family: ‘Times New Roman‘, serif;">protobuf</span> protocol See the following two urls:</p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">http://code.google.com/p/protobuf/</span></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Https://developers.google.com/protocol-buffers/docs/tutorials</span></p></p><p><p></p></p><p><p></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Unity</span> has two protobuf libraries that can be used <span style="font-family: ‘Times New Roman‘, serif;">protobuf-csharp-port, <strong>protobuf-net</strong> (the</span> Latter is more efficient <span style="font-family: ‘Times New Roman‘, serif;">)</span></p></p><p><p>Two libraries can work correctly on <span style="font-family: ‘Times New Roman‘, serif;">Android</span> , but the following exception appears on <span style="font-family: ‘Times New Roman‘, serif;">iOS</span> :</p></p><p><p></p></p><p><p><span style="font-family: Georgia, serif;"><span style="font-size: medium;"><em>Executionengineexception:</em></span></span></p></p><p><p><span style="font-family: Georgia, serif;"><span style="font-size: medium;"><em>Attempting to JIT compile method</em></span></span></p></p><p><p>JIT means compile at run time, while <span style="font-family: ‘Times New Roman‘, serif;">iOS</span> is <span style="font-family: ‘Times New Roman‘, serif;">AOT (full)</span> compilation, so any library that is <span style="font-family: ‘Times New Roman‘, serif;">JIT</span> -only will not work on <span style="font-family: ‘Times New Roman‘, serif;">iOS</span> .</p></p><p><p>To learn about the <span style="font-family: ‘Times New Roman‘, serif;">JIT</span> and the <span style="font-family: ‘Times New Roman‘, serif;">AOT</span>, see the following URLs <span style="font-family: ‘Times New Roman‘, serif;">:</span></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Jit:http://en.wikipedia.org/wiki/just-in-time_compilation</span></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Aot:http://en.wikipedia.org/wiki/aot_compiler</span></p></p><p><p><span style="font-family: ‘Times New Roman‘, serif;">Http://www.mono-project.com/AOT</span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>After an attempt, you can use protobuf-net <strong></strong> precompilation to run on iOS and Android devices</p></p><p><p>next, follow the steps, and finally attach the test project.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>first, Download Protobuf-net and configuration</p></p><p><p>Go to Http://code.google.com/p/protobuf-net/downloads/list to download the latest release version of Protobuf-net (the latest version of this tutorial is R668)</p></p><p><p>After downloading to a directory of your hard drive, it is best to add <strong>precompile\precompile.exe</strong> and <strong>Protogen\protogen.exe</strong> two files to the environment variable, then easy to Use.</p></p><p><p></p></p><p><p></p></p><p><p>We also need three files in the coreonly\ios, which will be used later, and will eventually be put into unity engineering.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>second, Generate CS Code</p></p><p><p>Using the Protogen command line to generate code (both precompile and Protogen can only be run under windows, it may be possible to run this exe via mono on a mac, but it has not been tried.) )</p></p><p><p></p></p><p><p><strong>examples of use are as follows</strong> :</p></p><p><p>Protogen-i:test1.proto-i:test2.proto-i:test3.proto-o:output.cs-ns:com.fbmly.model</p></p><p><p></p></p><p><p></p></p><p><p>-i is an input file that can have multiple</p></p><p><p>-o output of CS file, can only have one: If-i has more than one will generate all the code into this CS file</p></p><p><p>The-ns namespace is best used if the default namespace that is not used every time the build is proto is the file Name.</p></p><p><p></p></p><p><p></p></p><p><p>In general, just make all the data structures into a CS (or else use multiple-i, or write all the PROTOBUF definitions in a proto file), so that later operation is more Convenient.</p></p><p><p></p></p><p><p></p></p><p><p>After this step you get the CS file after the BUILD.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>third, Compile DLL Library</p></p><p><p>Use the MonoDevelop tool to compile the CS file generated in the previous step into a DLL library.</p></p><p><p></p></p><p><p></p></p><p><p>1. Create a new project (file->new->solution)</p></p><p><p>2. In the popup dialog box, Select C # and select Library.</p></p><p><p>3. Fill in the name of the project below (this name is the name of the generated dll, so be GOOD)</p></p><p><p>4. Click on the forward (then one more time Ok)</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>5. Delete the default generated MyClass.cs file, and if necessary, fill in the AssemblyInfo.cs with some copyright information.</p></p><p><p>6. Add the previous step to the project with the CS file generated by Protogen (right-click on the left project name to Add)</p></p><p><p>7. Add Protobuf-net Reference Library</p></p><p><p>Click on the Project's references->edit References</p></p><p><p>Then select the DLL library under the coreonly\ios, double-click the library file to add to the right</p></p><p><p>Finally check the references inside if there is Protobuf-net.dll is Correct.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>8. When you are done, click Build->build All to generate the DLL in the current Project.</p></p><p><p>Do not forget the choice of debug and release, after the test should be re-release and then compile Once.</p></p><p><p><span id="J_att_12832"><span id="td_att_12832" class="J_attach_img_wrap single_img"></span></span></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>9. The bottom left tip is successful in generating DLL files in the current project directory (i'm TestModel.dll Here)</p></p><p><p></p></p><p><p></p></p><p><p>This DLL holds all of your data structures.</p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>Iv. Precompiled Serialization Library</p></p><p><p>The DLL file that generated the data in the previous step requires the DLL file in the previous step to generate a specially serialized DLL File.</p></p><p><p></p></p><p><p>the <strong>first thing is to ensure that the TestModel.dll and compile-time libraries in the same directory</strong> (the DLL file directory generated in the previous step, will automatically copy the Protobuf-net library files, so just remember this is ok.) For example, If you copy to another directory to do this, it will be important.</p></p><p><p></p></p><p><p>Then open the command line and navigate to the Bin\Debug or bin\release directory under the Dll's project directory</p></p><p><p>Then execute the following command ( <strong>modified to the name you set</strong> ):</p></p><p><p></p></p><p><p>Precompile Testmodel.dll-o:protobufserializer.dll-t:com.fbmly.protobufserializer</p></p><p><p>TestModel.dll is the DLL file that was generated in the previous step,-o is the generated file Name-t is the class name of the serialized class generated in ProtobufSerialize.dll supports the namespace, and you can write the class name directly without the Namespace.</p></p><p><p></p></p><p><p>If there is an all-done hint at the end, it means that the build Succeeded.</p></p><p><p></p></p><p><p></p></p><p><p><br>Copy Code</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">12345678910 </td> <td class="co De "><code class=" bash plain ">d:\protobuf-net\precompile\model>precompile testmodel.dll-o:protobufserializer. </code> <code class="bash plain">dll-t:com.fbmly.protobufserializer </code> <code class="bash plain">protobuf-net pre-compiler </code> <code class="bash plain">detected framework:c:\windows\microsoft.net\framework\v2.0.50727 </code> <code class=" Bash plain ">resolved C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorlib.dll </code> <code class=" Bash Plain "> Resolved C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\System.dll </code> <code class="bash plain">resolved Protobuf-net.dll </code> <code class="bash plain">adding fbmly1.fbmly ... </code> <code class="bash plain">compiling com.fbmly.ProtobufSerializer to ProtobufSerializer.dll ... </code> <code class="bash plain">all  </code> <code class="bash keyword">done </code> </td> </tr> </tbody> </table><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>V. Testing in Unity</p></p><p><p>Copy the TestModel.dll, ProtobufSerializer.dll, and protobuf-net libraries that were generated in the third and fourth steps to Unity's Project.</p></p><p><p>Now it's time to start testing.</p></p><p><p></p></p><p><p>Test code:</p></p><p><p></p></p><p><p></p></p><p><p><br>Copy Code</p></p> <table border="0" cellspacing="0" cellpadding="0"> <tbody> <tr> <td class="gutter">123456789101112131415161718192021222324252627</td> <td class="code"><code class="c# keyword"><code class="c# keyword">void</code></code><code class="c# plain"><code class="c# plain">OnGUI( )</code></code><code class="c# plain"><code class="c# plain">{</code></code><code class="c# spaces"><code class="c# spaces">    </code></code><code class="c# keyword"><code class="c# keyword">if</code></code><code class="c# plain"><code class="c# plain">( GUILayout.Button ( </code></code><code class="c# string"><code class="c# string">"Test"</code></code><code class="c# plain"><code class="c# plain">, GUILayout.Width ( 200f ) , GUILayout.Height ( 200f ) ) )</code></code><code class="c# spaces"><code class="c# spaces">    </code></code><code class="c# plain"><code class="c# plain">{</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">Fbmly data = </code></code><code class="c# keyword"><code class="c# keyword">new</code></code> <code class="c# plain"><code class="c# plain">Fbmly ( );</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">data.mmm = 10;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">ProtobufSerializer serializer = </code></code><code class="c# keyword"><code class="c# keyword">new</code></code><code class="c# plain"><code class="c# plain">ProtobufSerializer ( );</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# comments"><code class="c# comments">//Serialize</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# keyword"><code class="c# keyword">byte</code></code><code class="c# plain"><code class="c# plain">[] buffer = </code></code><code class="c# keyword"><code class="c# keyword">null</code></code><code class="c# plain"><code class="c# plain">;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# keyword"><code class="c# keyword">using</code></code><code class="c# plain"><code class="c# plain">( MemoryStream m = </code></code><code class="c# keyword"><code class="c# keyword">new</code></code><code class="c# plain"><code class="c# plain">MemoryStream ( ) )</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">{</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">serializer.Serialize ( m , data );</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">m.Position = 0;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# keyword"><code class="c# keyword">int</code></code> <code class="c# plain"><code class="c# plain">length = (</code></code><code class="c# keyword"><code class="c# keyword">int</code></code><code class="c# plain"><code class="c# plain">)m.Length;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">buffer = </code></code><code class="c# keyword"><code class="c# keyword">new</code></code><code class="c# keyword"><code class="c# keyword">byte</code></code><code class="c# plain"><code class="c# plain">[length];</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">m.Read(buffer, 0 ,length);</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">}</code></code><code class="c# spaces"><code class="c# spaces"> </code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">Fbmly newData = </code></code><code class="c# keyword"><code class="c# keyword">null</code></code><code class="c# plain"><code class="c# plain">;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# comments"><code class="c# comments">//Deserialize</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# keyword"><code class="c# keyword">using</code></code><code class="c# plain"><code class="c# plain">( MemoryStream m = </code></code><code class="c# keyword"><code class="c# keyword">new</code></code><code class="c# plain"><code class="c# plain">MemoryStream ( buffer ) )</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">{</code></code><code class="c# spaces"><code class="c# spaces">            </code></code><code class="c# plain"><code class="c# plain">newData = serializer.Deserialize ( m , </code></code><code class="c# keyword"><code class="c# keyword">null</code></code> <code class="c# plain"><code class="c# plain">, </code></code><code class="c# keyword"><code class="c# keyword">typeof</code></code><code class="c# plain"><code class="c# plain">( Fbmly ) ) </code></code><code class="c# keyword"><code class="c# keyword">as</code></code><code class="c# plain"><code class="c# plain">Fbmly;</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">}</code></code><code class="c# spaces"><code class="c# spaces">        </code></code><code class="c# plain"><code class="c# plain">Debug.Log ( </code></code><code class="c# string"><code class="c# string">"newData.mmm="</code></code><code class="c# plain"><code class="c# plain">+ newData.mmm );</code></code><code class="c# spaces"><code class="c# spaces">    </code></code><code class="c# plain"><code class="c# plain">}</code></code><code class="c# plain"><code class="c# plain">}</code></code></td> </tr> </tbody> </table><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p></p></p><p><p>The test project has been tested on TOUCH4 and the project is set TO. Net 2.0 subset</p></p><p><p>If you're having errors on your real machine, you might want to set it up To. Net 2.0 to Try.</p></p><p><p></p></p><p><p></p></p><p><p>This approach has been verified in the game project, and complex data is no problem (the game project is defined with more than 500 proto), both iOS and Android are Available.</p></p><p><p></p></p><p><p></p></p><p><p>Finally attached to the project Package: it is attached to a tool class, can be serialized when the use of the time only need to modify it, because it maintains a serialization class of the Singleton.</p></p><p><p>Using Protobuf on iOS Android devices (using dll Mode)</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.