Advertisement
Bunny83

URLParameters.cs

Jul 1st, 2018
3,338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.59 KB | None | 0 0
  1. /* * * * *
  2.  * URLParameters.cs
  3.  * ----------------
  4.  *
  5.  * This singleton script provides easy access to any URL components in a Web-build
  6.  * Just use
  7.  *
  8.  *     URLParameters.Instance.RegisterOnDone(OnDone);
  9.  *    
  10.  * To register a callback which will be invoked from javascript. The callback receives a
  11.  * reference to the singleton instance. The instance has several properties to hold all the
  12.  * different parts of the URL. In addition it will split and parse the search and hash /
  13.  * fragment value into key/value pairs stored in a dictionary (SearchParameters, HashParameters)
  14.  *
  15.  * The MIT License (MIT)
  16.  *
  17.  * Copyright (c) 2012-2017 Markus Göbel (Bunny83)
  18.  *
  19.  * Permission is hereby granted, free of charge, to any person obtaining a copy
  20.  * of this software and associated documentation files (the "Software"), to deal
  21.  * in the Software without restriction, including without limitation the rights
  22.  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23.  * copies of the Software, and to permit persons to whom the Software is
  24.  * furnished to do so, subject to the following conditions:
  25.  *
  26.  * The above copyright notice and this permission notice shall be included in all
  27.  * copies or substantial portions of the Software.
  28.  *
  29.  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30.  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31.  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32.  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33.  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34.  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  35.  * SOFTWARE.
  36.  *
  37.  * * * * */
  38.  
  39. using UnityEngine;
  40. using System.Collections;
  41. using System.Collections.Generic;
  42.  
  43. public class URLParameters : MonoBehaviour
  44. {
  45.     // set testíng data here for in-editor-use
  46.     // href | hash | host | hostname | pathname | port | protocol | search
  47.     public static string TestData = "|||||||";
  48.     private static URLParameters m_Instance = null;
  49.     public static URLParameters Instance
  50.     {
  51.         get
  52.         {
  53.             if (m_Instance == null)
  54.             {
  55.                 m_Instance = (URLParameters)FindObjectOfType(typeof(URLParameters));
  56.                 if (m_Instance == null)
  57.                     m_Instance = (new GameObject("URLParameters")).AddComponent<URLParameters>();
  58.                 m_Instance.gameObject.name = "URLParameters";
  59.                 DontDestroyOnLoad(m_Instance.gameObject);
  60.             }
  61.             return m_Instance;
  62.         }
  63.     }
  64.  
  65.     private System.Action<URLParameters> m_OnDone = null;
  66.     private System.Action<URLParameters> m_OnDoneOnce = null;
  67.  
  68.     private bool m_HaveInformation = false;
  69.     private string m_RawData;
  70.     private string m_Href;
  71.     private string m_Hash;
  72.     private string m_Host;
  73.     private string m_Hostname;
  74.     private string m_Pathname;
  75.     private string m_Port;
  76.     private string m_Protocol;
  77.     private string m_Search;
  78.     private Dictionary<string, string> m_SearchParams = new Dictionary<string, string>();
  79.     private Dictionary<string, string> m_HashParams = new Dictionary<string, string>();
  80.  
  81.     public bool HaveInformation
  82.     {
  83.         get {return m_HaveInformation;}
  84.     }
  85.     public string RawData
  86.     {
  87.         get {return m_RawData;}
  88.     }
  89.     public string Href
  90.     {
  91.         get {return m_Href;}
  92.     }
  93.     public string Hash
  94.     {
  95.         get {return m_Hash;}
  96.     }
  97.     public string Host
  98.     {
  99.         get {return m_Host;}
  100.     }
  101.     public string Hostname
  102.     {
  103.         get {return m_Hostname;}
  104.     }
  105.     public string Pathname
  106.     {
  107.         get {return m_Pathname;}
  108.     }
  109.     public string Port
  110.     {
  111.         get {return m_Port;}
  112.     }
  113.     public string Protocol
  114.     {
  115.         get {return m_Protocol;}
  116.     }
  117.     public string Search
  118.     {
  119.         get {return m_Search;}
  120.     }
  121.     public IDictionary<string, string> SearchParameters
  122.     {
  123.         get { return m_SearchParams; }
  124.     }
  125.     public IDictionary<string, string> HashParameters
  126.     {
  127.         get { return m_HashParams; }
  128.     }
  129.  
  130.  
  131.     public void RegisterOnDone(System.Action<URLParameters> aCallback)
  132.     {
  133.         m_OnDone += aCallback;
  134.         if (HaveInformation)
  135.             aCallback(this);
  136.     }
  137.  
  138.     public void RegisterOnceOnDone(System.Action<URLParameters> aCallback)
  139.     {
  140.         if (HaveInformation)
  141.             aCallback(this);
  142.         else
  143.             m_OnDoneOnce += aCallback;
  144.     }
  145.  
  146.  
  147.     public void Request()
  148.     {
  149.         StartCoroutine(_Request());
  150.     }
  151.  
  152.     private IEnumerator _Request()
  153.     {
  154.         m_HaveInformation = false;
  155. #if UNITY_EDITOR
  156.         yield return null;
  157.         SetAddressComponents(TestData);
  158. #elif UNITY_WEBPLAYER
  159.         const string WebplayerCode = "GetUnity ().SendMessage ('{0}', 'SetAddressComponents', location.href +'|'+ location.hash +'|'+ location.host +'|'+ location.hostname +'|'+ location.pathname +'|'+ location.port +'|'+ location.protocol +'|'+ location.search);";
  160.         Application.ExternalEval(string.Format(WebplayerCode, gameObject.name));
  161. #elif UNITY_WEBGL
  162.         const string WebGLCode = "SendMessage ('{0}', 'SetAddressComponents', location.href +'|'+ location.hash +'|'+ location.host +'|'+ location.hostname +'|'+ location.pathname +'|'+ location.port +'|'+ location.protocol +'|'+ location.search);";
  163.         Application.ExternalEval(string.Format(WebGLCode, gameObject.name));
  164. #endif
  165.         yield break;
  166.     }
  167.  
  168.     public IEnumerator Start()
  169.     {
  170.         yield return null; // wait one frame to ensure all delegates are assigned.
  171.         Request();
  172.     }
  173.  
  174.     public void SetAddressComponents (string aData)
  175.     {
  176.         string[] parts = aData.Split('|');
  177.         m_RawData     = aData;
  178.         m_Href        = parts[0];
  179.         m_Hash        = parts[1];
  180.         m_Host        = parts[2];
  181.         m_Hostname    = parts[3];
  182.         m_Pathname    = parts[4];
  183.         m_Port        = parts[5];
  184.         m_Protocol    = parts[6];
  185.         m_Search      = parts[7];
  186.         var tmp = m_Search.TrimStart('?');
  187.         var data = tmp.Split('&');
  188.         for (int i = 0; i < data.Length; i++)
  189.         {
  190.             var val = data[i].Split('=');
  191.             if (val.Length != 2)
  192.                 continue;
  193.             m_SearchParams[val[0]] = val[1];
  194.         }
  195.         tmp = m_Hash.TrimStart('#');
  196.         data = tmp.Split('&');
  197.         for (int i = 0; i < data.Length; i++)
  198.         {
  199.             var val = data[i].Split('=');
  200.             if (val.Length != 2)
  201.                 continue;
  202.             m_HashParams[val[0]] = val[1];
  203.         }
  204.  
  205.         m_HaveInformation = true;
  206.         if (m_OnDone != null)
  207.             m_OnDone(this);
  208.         if (m_OnDoneOnce != null)
  209.         {
  210.             m_OnDoneOnce(this);
  211.             m_OnDoneOnce = null;
  212.         }
  213.     }
  214. }
  215.  
  216.  
  217. public static class IDictionaryExtension
  218. {
  219.     public static double GetDouble(this IDictionary<string, string> aDict, string aKey, double aDefault = 0d)
  220.     {
  221.         string tmp;
  222.         if (aDict.TryGetValue(aKey, out tmp))
  223.         {
  224.             double val;
  225.             if (double.TryParse(tmp, out val))
  226.                 return val;
  227.         }
  228.         return aDefault;
  229.     }
  230.     public static int GetInt(this IDictionary<string, string> aDict, string aKey, int aDefault = 0)
  231.     {
  232.         string tmp;
  233.         if (aDict.TryGetValue(aKey, out tmp))
  234.         {
  235.             int val;
  236.             if (int.TryParse(tmp, out val))
  237.                 return val;
  238.         }
  239.         return aDefault;
  240.     }
  241. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement