The second day of imitation LOL project development

Source: Internet
Author: User

The second day of imitation LOL project development

by Straw Hat

In the last section, the previous update has not yet begun to write code logic, and today we add the complete.

We found the Checkversion method inside the VersionManager script:

First we think of the detection version, need to download information from the server, then must be detected in advance the network is good, and compare version information.

So, let's write a Beforecheck method:

    <summary>///    Check network status and match version information///</summary>//    <param name= "Asynresult" > The version information is consistent with the handling of Delegates </param>    ///<param Name= "OnError" > Error handling delegates </param> public    void Beforecheck ( Action<bool> Asynresult, Action OnError)    {        checktimeout checktimeout = new Checktimeout ();        Checktimeout.asynisnetworktimeout ((Success) =         {            //If the network is good, start downloading server version XML            if (success)            {            }            else             {                if (OnError! = null)                {                    OnError ();}}        });    }

Before we write a good judgment on the network with the anonymous delegate, let's start by thinking about a problem, what does the network need to do next?

is to download the server version information, but need a URL address to access?

So where is the URL for this server-side version information? Do you want to write the absolute URL dead to the code.

What if I change the URL of the server later? Then rewrite the code again. It is undesirable to reject such a scheme.

So, the URL of the server should be written to the configuration file and downloaded from the server.

Very nice, we didn't write a systemconfig before, we were inside. Initializes the URL of the server.

So Systemconfig needs an initial method of Init ().

The initial configuration file is divided into two categories:

1. The configuration information obtained from the server, such as version information, game Server area of the information

2. Local configuration information, such as sound size, screen resolution, etc.

First, to load the server-related configuration information, related to the server, we need to classify: So write a Cfginfo class:

    public class Cfginfo    {public        int id {get; set;}        public string name {get; set;}        public string URL {get; set;}    }

For example, the version information Class Id=0, game server Region class id=1, and then corresponding to the different URLs, so that we manage more clearly.

So where does this cfginfo information come from, must also need to access the server side of the URL to get, so, we create a txt under the Resources folder, which is written in this URL, and then access the URL, Read the contents of the initialization of all the Cfginfo class to the list of List<cfginfo>, and finally to save the URL text in the persistent folder for later use,OK after the analysis.

Create constants under the Systemconfig class: Cfgpath Persistent path

Public readonly static String cfgpath = Application.persistentdatapath + "/cfg.xml";

Then we create the Loadcfginfo () method inside the Systemconfig, which is called in Init ():

private static bool Loadcfginfo ()    {        string cfgstr = null;        If there is a persistent path, simply load this        if (file.exists (Cfgpath))        {            cfgstr = Unitytools.loadfiletext (Cfgpath);        }        else         {            //from the resources load configuration text            textasset Cfgurl = resources.load ("cfg") as Textasset;            if (Cfgurl)            {                //Download all configuration XML strings related to the server from the Web page                cfgstr = DownloadMgr.Instance.DownLoadHtml (Cfgurl.text);            }            Else            {                cfgstr = null;            }        }        Load XML content as List class        cfginfolist = Loadxmltext<cfginfo> (CFGSTR);        return true;    }

 

Loadxmltext<t> (String xmlText):

&NBSP;

    <summary>///Convert XML to List<t> List class///</summary>//<typeparam name= "T" ></typep aram>//<param name= "XmlText" ></param>///<returns></returns> private static LIST&L T        T> loadxmltext<t> (String xmlText) {list<t> List = new list<t> (); try {if (string.            IsNullOrEmpty (XmlText)) {return list;            } Type type = typeof (T);            XmlDocument doc = xmlresadapter.getxmldocument (xmlText);            dictionary<int,dictionary<string,string>> map = Xmlresadapter.loadxmltomap (doc,xmlText); var props = type.            GetProperties (~system.reflection.bindingflags.static); foreach (var item in map) {var obj = type. GetConstructor (type.emptytypes).                Invoke (NULL); foreach (Var prop in props) {if (Prop.             Name = = "id")       {Prop. SetValue (Obj,item.                    Key,null);                            } else {try { if (item. Value.containskey (Prop. Name) {var value = Unitytools.getvalue (item). Value[prop. Name],prop.                                PropertyType); Prop.                            SetValue (Obj,value,null); }} catch (Exception e) {D Ebug.                        Logexception (e); }}} list.            ADD ((T) obj);        }} catch (Exception e) {debug.logexception (e);    } return list; }

  

Xmlresadapter.loadxmltomap ():

        <summary>///Convert XML content to map///</summary>//<param name= "Doc" ></pa ram>//<param name= "content" ></param>///<returns></returns> public STA            Tic Dictionary<int, dictionary<string, string>> Loadxmltomap (XmlDocument doc, string content) {            var result = new Dictionary<int, dictionary<string, string>> ();            int index = 0; foreach (XmlNode item in Doc. selectSingleNode ("root").                ChildNodes) {index++; if (item. ChildNodes = = NULL | | Item.                Childnodes.count = = 0) {continue; } int key = Int. Parse (item. Childnodes[0].                InnerText); if (result.                ContainsKey (key)) {continue;                } var children = new dictionary<string, string> (); Result. ADD (Key, CHILdren); for (int i = 1; i < item. Childnodes.count; i++) {XmlNode node = Item.                    Childnodes[i];                    string tag = null; if (node. Name.length < 3) {tag = node.                    Name; } else {String tagtial = node. Name.substring (node.                        Name.length-2, 2);                        if (tagtial = = "_i" | | tagtial = = "_s" | | tagtial = "_f" | | tagtial = "_l" | | tagtial = "_k" | | tagtial = "_M" {tag = node. Name.substring (0, node.                        NAME.LENGTH-2); } else {tag = node.                        Name; }} if (node! = null &&!children. ContainsKey (tag)) {if (string. IsNullOrEmpty(node. InnerText)) {children.                        ADD (Tag, ""); } else {children. ADD (Tag, node.                        Innertext.trim ());        }}}} return result; }

  

So here we need to use the preparation tools mentioned in the first section: Wampserver Integrated Web Development tools:

Because here we need to refer to the URL of the access server, so we set up an HTTP server site ourselves.

Open wampserver, find www directory

Then create a new folder named Lolgamedemo.

Under this folder, create a new XML named Cfg.xml

Then go back to Unity's resources file to create a new TXT, also named Cfg.txt.

Edit the contents of txt:


Then edit the contents of the Cfg.xml:

Then under the Lolgamedemo folder, create a new serverversion.xml text for the server version information:

OK, you are done. Then we go back to the Beforecheck method inside the VersionManager:

We want to get to the server version of the URL, so we have to go back to Systemconfig write a getcfginfobyname () method or Getcfginfobyid () interface.

Getcfginfobyname ():

    <summary>/////For    server configuration information by name///</summary>//    <param name= "name" ></param >    //<returns></returns> public static string Getcfginfourlbyname (string name)    {        string result = "";        foreach (var item in cfginfolist)        {            if (item.name = = name)            {                result = Item.url;                break;            }        }        return result;    }

Getcfginfobyid ():

    <summary>///    ID Get server configuration information URL///</summary>//    <param name= "id" ></param>    //<returns></returns> public static string Getcfginfourlbyid (int id)    {        string result = "";        foreach (var item in cfginfolist)        {            if (item.id = = id)            {                result = Item.url;                break;            }        }        return result;    }

Since the version text of the server we downloaded is also to be saved in the persistent file directory, so:

Public readonly static String serverversionpath = Application.persistentdatapath + "/serverversion.xml";

And before we just initialized the instance of the local version information class, we now create a server-side version information class in VersionManager:

    <summary>////    local version information properties///    </summary> public    versionmanagerinfo localversion {get; Private set; }///<summary>//    service Version information properties///    </summary> public    versionmanagerinfo serverversion {get ; Private set; }

Then again back to the Beforecheck method inside, finally came back! 0.0!

public void Beforecheck (action<bool> asynresult, Action OnError) {checktimeout checktimeout = new Checkti        Meout ();            Checktimeout.asynisnetworktimeout ((Success) = {//If the network is good, start downloading server version XML if (success)                 {DownloadMgr.Instance.AsynDownLoadHtml (Systemconfig.getcfginfourlbyname ("version"), (serverversion) = {//If there is a local version information text on the server, overwriting the downloaded servers text if (file.exist S (systemconfig.serverversionpath)) {serverversion = Unitytools.loadfiletext (Sys                    Temconfig.serverversionpath); }//Convert text to version information class ServerVersion = Getversioninxml (serverversion);
Start the Compare version number bool programversion = ServerVersion.ProgramVersionCodeInfo.Compare (localversion.programvers Ioncodeinfo) > 0; BOOL Resourceversion = ServerVersion.ResourceVersionCodeInfo.Compare (localversion.resourceversioncodeinfo) > 0; Execute whether the updated delegate Asynresult (programversion | | resourceversion); },ONERROR); } else {if (OnError! = null) {OnError (); } } }); }

The second day of imitation LOL project development

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.