/// <summary> /// Gos the next scene. /// </summary> /// <param name="sceneName">Scene name.</param> public void GoNextScene(string sceneName) { if (sceneName == "") { HDebug.LogWarning("Setting the next scene name required"); return; } nextSceneName = sceneName; if (jobData.intent.Count > 0) { datas.Add(jobData.intent); } if (jobData.popUp) { SceneManager.Instance.Close(delegate() { OnNextScene(); }); } else { OnNextScene(); } }
/// <summary> /// Downloads the asset bundle. /// </summary> /// <param name="json">Json.</param> protected void DownloadAssetBundle(string json) { assetBundleData = JsonUtil.FromJson <AssetBundleInitialData> (json); if (assetBundleData._Resource.Major != Register.GetInt(assetBundleData._Resource.Name + AssetBundleInitialData.major)) { #if UNITY_EDITOR HDebug.Log("Major : " + assetBundleData._Resource.Major); #endif downloads = assetBundleData._AssetBundle; // Delete all AssetBundle. assetBundleManager.CleanCache(); } else if (assetBundleData._Resource.Minor != Register.GetInt(assetBundleData._Resource.Name + AssetBundleInitialData.minor)) { #if UNITY_EDITOR HDebug.Log("Minor : " + assetBundleData._Resource.Minor); #endif downloads = new List <AssetBundleInitialData.AssetBundle> (); for (int i = 0; i < assetBundleData._AssetBundle.Count; i++) { if (assetBundleData._AssetBundle [i]._Version != Register.GetInt(assetBundleData._AssetBundle [i].Name)) { downloads.Add(assetBundleData._AssetBundle [i]); } } } else { if (aEvent != null) { aEvent(AssetBundleInitalStatus.Over); } return; } if (aEvent != null) { aEvent(AssetBundleInitalStatus.Start); } if (downloads.Count > 0) { index = 0; DownloadAssetBundle(); } else { HDebug.LogWarning("None of this has changed assetbundle version."); Register.SetInt(assetBundleData._Resource.Name + AssetBundleInitialData.major, assetBundleData._Resource.Major); Register.SetInt(assetBundleData._Resource.Name + AssetBundleInitialData.minor, assetBundleData._Resource.Minor); if (aEvent != null) { aEvent(AssetBundleInitalStatus.Over); } } }
/// <summary> /// Sets the select ORM mapper. /// </summary> /// <returns>The select ORM mapper.</returns> /// <param name="mapper">Mapper.</param> /// <param name="flag">Flag.</param> /// <param name="index">Index.</param> public static SelectORMMaker SetSelectORMMaker(SelectORMMaker mapper, BindingFlags flag = BindingFlags.NonPublic) { if (mapper.Type == null) { return(mapper); } Type copyType = mapper.Type; List <FieldInfo> fieldInfos = GetFields(mapper.Type, flag); foreach (FieldInfo field in fieldInfos) { // table mapper.SetType(copyType); // FK ColumnAttribute column = field.GetAttributeValue <ColumnAttribute> (); if (column != null && column.CheckConstraints(DataConstraints.FK)) { if (column.Key != null && column.Value != "") { mapper.SetJoin(column.Key, column.Value, field.Name); } else { HDebug.LogWarning(field.Name + " the column attribute is set problem"); } } // select if (Util.IsValueType(field.FieldType)) { mapper.SetSelect(field.Name); } else { if (mapper.SetType(field.FieldType, copyType) == "") { HDebug.LogWarning(field.FieldType + " the table is not set."); mapper.SetType(copyType); } else { JoinAttribute join = field.GetAttributeValue <JoinAttribute> (); if (join == null) { mapper.SetJoinType(); } else { mapper.SetJoinType(join.Type); } SetSelectORMMaker(mapper, flag); } } } return(mapper); }
/// <summary> /// Plaies the BGM. /// </summary> /// <param name="name">Name.</param> public void PlayBGM(string name = "") { if (bgms == null || bgms.Count <= 0) { HDebug.LogWarning("Please add a " + name + " BGM"); return; } SoundData sound = bgms [0]; if (name != "") { sound = bgms.Find(x => x.name == name); } if (sound == null) { HDebug.LogWarning("Please add a " + name + " BGM"); return; } StopBGM(); bgm.clip = sound.clip; bgm.Play(); }
/// <summary> /// Executes the non query. /// </summary> /// <param name="query">Query.</param> public void ExecuteNonQuery(string query) { if (!canQuery) { HDebug.LogWarning("Can't execute the query, verify DB origin file"); return; } if (!isConnectionOpen) { Open(pathDB); } #if !UNITY_WEBPLAYER if ((ConnectionState)dbconn.State != ConnectionState.Open) { HDebug.LogWarning("Sqlite DB is not open"); return; } dbcmd.CommandText = query; try { dbcmd.ExecuteNonQuery(); } catch (Exception e) { HDebug.Log("Query : " + query); HDebug.LogError(e.Message); return; } #endif if (!isConnectionOpen) { Close(); } }
/// <summary> /// Executes the query. /// </summary> /// <returns>The query.</returns> /// <param name="query">Query.</param> public DataTable ExecuteQuery(string query) { if (!canQuery) { HDebug.LogWarning("Can't execute the query, verify DB origin file"); return(null); } if (!isConnectionOpen) { Open(pathDB); } #if !UNITY_WEBPLAYER if ((ConnectionState)dbconn.State != ConnectionState.Open) { HDebug.LogWarning("Sqlite DB is not open"); return(null); } dbcmd.CommandText = query; try { reader = dbcmd.ExecuteReader(); } catch (Exception e) { HDebug.Log("Query : " + query); HDebug.LogError(e.Message); return(null); } #endif DataTable dataTable = new DataTable(); #if !UNITY_WEBPLAYER for (int i = 0; i < reader.FieldCount; i++) { dataTable.Columns.Add(reader.GetName(i)); } while (reader.Read()) { DataRow row = new DataRow(); for (int i = 0; i < reader.FieldCount; i++) { row.Add(reader.GetName(i), reader.GetValue(i)); } dataTable.Rows.Add(row); } #endif if (!isConnectionOpen) { Close(); } return(dataTable); }
/// <summary> /// Initializes a new instance of the <see cref="Hellgate.Sqlite"/> class. /// The DB name '/' Do not put /// Data Base name. (the file needs exist in the streamingAssets folder) /// </summary> /// <param name="db">DB name && path.</param> /// <param name="resetDB">reset DB.</param> public Sqlite(string db, bool resetDB = false) { canQuery = true; if (db.Contains("/")) { pathDB = db; return; } pathDB = Path.Combine(Application.persistentDataPath, db); #if !UNITY_WEBPLAYER // StreamingAssets folder string resourcePath = Path.Combine(Application.streamingAssetsPath, db); #if UNITY_EDITOR AutoDDL(db, false); #endif if (!File.Exists(pathDB) || resetDB) { if (resourcePath.Contains("://")) // android { WWW www = new WWW(resourcePath); while (!www.isDone) { ; } if (www.error == null) { File.WriteAllBytes(pathDB, www.bytes); } else { canQuery = false; HDebug.LogWarning(www.error); } } else { if (File.Exists(resourcePath)) { File.Copy(resourcePath, pathDB, true); } else { canQuery = false; HDebug.LogError("The file DB named " + db + " doesn't exist in the StreamingAssets Folder, please copy it there."); } } } #endif }
/// <summary> /// Puts the extra. /// </summary> /// <param name="key">Key.</param> /// <param name="value">Value.</param> public void PutExtra(string key, object value) { if (intent.ContainsKey(key)) { HDebug.LogWarning("The key that already exists."); } else { intent.Add(key, value); } }
/// <summary> /// Debug the specified data and www. /// warring is timeover /// </summary> /// <param name="data">Data.</param> /// <param name="www">Www.</param> protected void Log() { time = Time.time - time; if (!debug) { return; } System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); stringBuilder.AppendFormat("[Request time] {0}\n[URL] {1}\n", time, data.url); if (data.headers != null) { stringBuilder.Append("[WWWForm.headers]\n"); foreach (KeyValuePair <string, string> pair in data.headers) { stringBuilder.AppendFormat("{0} : {1}\n", pair.Key, pair.Value); } stringBuilder.Append("\n"); } if (data.headers != null) { stringBuilder.Append("[WWWForm.data]\n"); foreach (KeyValuePair <string, string> pair in data.datas) { stringBuilder.AppendFormat("{0} : {1}\n", pair.Key, pair.Value); } stringBuilder.Append("\n"); } if (www == null) { HDebug.LogWarning(stringBuilder.ToString()); } else if (www.error != null) { stringBuilder.AppendFormat("[WWW.error]\n{0}\n", www.error); HDebug.LogError(stringBuilder.ToString()); } else { HDebug.Log(stringBuilder.ToString()); } }
/// <summary> /// Log the specified data and obj. /// Warning is object null. /// </summary> /// <param name="data">Data.</param> /// <param name="obj">Object.</param> protected void LogLoadAsset(AssetBundleData data, object obj) { if (!debug) { return; } System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder(); stringBuilder.AppendFormat("[AssetBundle] {0} [Version] {1} [Object name] {2} [Type] {3} ", data.assetBundleName, data.version, data.objName, data.type); if (obj == null) { stringBuilder.Append("is object null."); HDebug.LogWarning(stringBuilder.ToString()); } }
/// <summary> /// Plaies the sound. /// </summary> /// <param name="name">Name.</param> /// <param name="createCount">Create count.</param> public void PlaySound(string name, int createCount = ObjectPool.createCount) { if (sounds == null) { HDebug.LogWarning("Please add a " + name + " sound"); return; } SoundData sound = sounds.Find(x => x.name == name); if (sound == null || sound.clip == null) { HDebug.LogWarning("Please add a " + name + " sound"); return; } if (objectPool == null) { objectPool = new ObjectPool(this.sound.gameObject, gameObject, createCount); } objectPool.GetObject().GetComponent <Sound> ().Play(sound.clip); }
/// <summary> /// Loadings the job. /// </summary> /// <param name="data">Data.</param> /// <param name="popUp">If set to <c>true</c> pop up.</param> /// <param name="active">Active.</param> /// <param name="deactive">Deactive.</param> public virtual void LoadingJob(LoadingJobData data, bool popUp, SceneCallbackDelegate active = null, SceneCallbackDelegate deactive = null) { if (LoadingJobSceneName == "") { HDebug.LogWarning("The default loading job scene is not set"); return; } data.popUp = popUp; if (popUp) { if (!data.active) { shieldAlpha = 0f; } PopUp(LoadingJobSceneName, data, active, deactive); } else { Screen(LoadingJobSceneName, data, active, deactive); } }
/// <summary> /// Invoke the specified iDic, obj and flag. /// </summary> /// <param name="iDic">IDictionary.</param> /// <param name="obj">Object.</param> /// <param name="flag">BindingFlags.</param> public static void Invoke(IDictionary iDic, object obj, BindingFlags flag = BindingFlags.NonPublic) { if (obj == null) { return; } if (iDic == null) { return; } foreach (object o in iDic.Keys) { IList asList = null; IDictionary asDic = null; string field = o.ToString(); object data = iDic [field]; Type type = GetFieldType(obj, field, flag); if (type == null) { continue; } if (data == null) { continue; } else if ((asList = data as IList) != null) { object value = GetFieldVale(obj, field, flag); Type element = type.GetElementType(); if (element == null) { element = type.GetGenericArguments() [0]; } if (value != null) { if (Util.IsValueType(element)) { data = CreateArrayInstance(asList, element); } else { Invoke(asList, value, flag); continue; } } else { object[] datas = Convert <object> (asList, flag, element); if (element == null) { element = type.GetGenericArguments() [0]; IList iList = CreateIListInstance(element); for (int i = 0; i < datas.Length; i++) { iList.Add(System.Convert.ChangeType(datas [i], element)); } data = iList; } else { data = CreateArrayInstance(new List <object> (datas), element); } } } else if ((asDic = data as IDictionary) != null) { object value = GetFieldVale(obj, field, flag); if (value == null) { data = Convert <object> (asDic, null, flag, type); } else { Invoke(asList, value, flag); continue; } } if (data == null) { continue; } if (type != data.GetType()) { try { data = System.Convert.ChangeType(data, type); } catch (Exception e) { HDebug.LogWarning(string.Format("{0}\nclass : {1}, field : {2}", e.Message, obj, field)); continue; } } if (flag == BindingFlags.NonPublic) { SetPrivateFieldInvoke(obj, field, data); } else { SetFieldInvoke(obj, field, data); } } }
/// <summary> /// Simples the migration. /// </summary> /// <param name="force">If set to <c>true</c> force.</param> public void SimpleMigration(bool force = true) { // StreamingAssets folder string path = Path.Combine(Application.streamingAssetsPath, Path.GetFileName(pathDB)); if (path.Contains("://")) // android { WWW www = new WWW(path); while (!www.isDone) { ; } if (www.error == null) { path = string.Format("{0}_copy{1}", Path.GetFileNameWithoutExtension(path), Path.GetExtension(path)); path = Path.Combine(Application.persistentDataPath, path); File.WriteAllBytes(path, www.bytes); } else { HDebug.LogWarning(www.error); return; } path = Path.GetFileName(path); } Query query = new Query(path); SqliteMastser[] resources = query.SELECT <SqliteMastser> (); List <SqliteMastser> list = new List <SqliteMastser> (resources); query = new Query(pathDB); SqliteMastser[] masters = query.SELECT <SqliteMastser> (); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < masters.Length; i++) { SqliteMastser master = list.Find(x => x.Name == masters [i].Name); if (master != null) { list.Remove(master); } if (force) { if (master != null) { if (masters [i].Sql != master.Sql) { stringBuilder.Append(query.GenerateDropTableSQL(master.Name)); stringBuilder.AppendLine(); stringBuilder.AppendFormat("{0};", master.Sql); stringBuilder.AppendLine(); } } else { stringBuilder.Append(query.GenerateDeleteSQL(masters [i].Name)); stringBuilder.AppendLine(); } } } if (list.Count > 0) { for (int i = 0; i < list.Count; i++) { stringBuilder.AppendFormat("{0};", list [i].Sql); stringBuilder.AppendLine(); } } if (stringBuilder.ToString() != "") { query.ExecuteNonQuery(stringBuilder.ToString()); } if (path.Contains("://")) // android { File.Delete(path); } }
/// <summary> /// Request the specified data and post. /// </summary> /// <param name="data">HttpData.</param> /// <param name="post">If set to <c>true</c> post.</param> public void Request(HttpData data, Http.FinishedDelegate finish = null) { time = Time.time; Action innerRequest = () => { if (preReuqest != null) { data = preReuqest(data); } this.data = data; Http http; #if UNITY_5_4_OR_NEWER if (data.method == UnityWebRequest.kHttpVerbPOST) { #else if (data.post) { #endif http = new Http(this, data.url); http.AddRangeData(data.datas); } else { http = new Http(this, data.url, data.datas); } #if UNITY_5_4_OR_NEWER http.method = data.method; http.audioType = data.audioType; http.OnFail = (UnityWebRequest www) => { this.www = www; OnFail(); }; http.OnDisposed = (UnityWebRequest www) => { this.www = www; OnDisposed(); }; http.OnDone = (UnityWebRequest www) => { this.www = www; OnDone(); }; #else http.OnFail = (WWW www) => { this.www = www; OnFail(); }; http.OnDisposed = (WWW www) => { this.www = www; OnDisposed(); }; http.OnDone = (WWW www) => { this.www = www; OnDone(); }; #endif // set headers http.Headers = data.headers; // set timeout time http.Timeout = data.timeout; http.Request(); }; if (finish != null) { data.finishedDelegate = finish; } if (data.popUp) { if (SceneManager.Instance.DefaultLoadingJobSceneName != "") { if (SceneManager.Instance.IsLoadingJob) { HDebug.LogWarning("Now loading scene active"); } SceneManager.Instance.PopUp(SceneManager.Instance.DefaultLoadingJobSceneName, null, delegate(SSceneController ctrl) { popUp = ctrl; innerRequest(); }); } else { HDebug.LogWarning("The default loading scene is not set"); } } else { innerRequest(); } }
/// <summary> /// Convert the specified dic, fieldInfo, flag and type. /// </summary> /// <param name="dic">Dic.</param> /// <param name="fieldInfo">Field info.</param> /// <param name="flag">Flag.</param> /// <param name="type">Type.</param> /// <typeparam name="T">The 1st type parameter.</typeparam> public static T Convert <T> (IDictionary dic, List <FieldInfo> fieldInfos = null, BindingFlags flag = BindingFlags.NonPublic, Type type = null) { if (dic == null) { return(default(T)); } object obj = (T)Activator.CreateInstance(typeof(T), null); if (type != null) { obj = Activator.CreateInstance(type, null); } if (fieldInfos == null) { fieldInfos = GetFields(obj.GetType(), flag); } foreach (FieldInfo field in fieldInfos) { if (!dic.Contains(field.Name)) { continue; } object data = dic [field.Name]; data = ConvertIgnoreData(field, data); if (field.FieldType.IsClass && field.FieldType != typeof(String)) { if (Util.IsArray(field.FieldType)) { IList iList = (IList)data; Type element = field.FieldType.GetElementType(); if (element == null) { Type[] types = field.FieldType.GetGenericArguments(); if (types.Length <= 0) { continue; } element = types [0]; } if (Util.IsValueType(element)) { field.SetValue(obj, CreateArrayInstance(iList, element)); } else { data = Convert <object> (iList, flag, element); Array someArray = data as Array; if (someArray == null) { continue; } if (field.FieldType.GetElementType() == null) // list { iList = CreateIListInstance(element); for (int i = 0; i < someArray.Length; i++) { iList.Add(System.Convert.ChangeType(someArray.GetValue(i), element)); } field.SetValue(obj, iList); } else // array { Array filledArray = Array.CreateInstance(element, someArray.Length); Array.Copy(someArray, filledArray, someArray.Length); field.SetValue(obj, filledArray); } } continue; } else { IDictionary iDic = (IDictionary)data; List <FieldInfo> fields = GetFields(field.FieldType, flag); data = Convert <object> (iDic, fields, flag, field.FieldType); } } if (data == null) { continue; } if (field.FieldType != data.GetType()) { try { data = System.Convert.ChangeType(data, field.FieldType); } catch (Exception e) { HDebug.LogWarning(string.Format("{0}\nclass : {1}, field : {2}", e.Message, obj, field.Name)); continue; } } field.SetValue(obj, data); } return((T)obj); }
/// <summary> /// Loads the asset bundle. /// </summary> /// <param name="data">Data.</param> /// <param name="finished">Finished.</param> public void LoadAssetBundle(AssetBundleData data, AssetBundleClient.FinishedDelegate finished) { System.Action InnerLoadAssetBundle = () => { System.Action InnerLoadAsset = () => { if (data.objName == string.Empty) { HDebug.LogWarning("Set the object name in the assetbundle."); finished(null); return; } if (data.async) { StartCoroutine(LoadAssetAsync(data, finished)); } else { LoadAsset(data, finished); } }; DownloadAssetBundle(data, delegate(object obj) { data.assetBundle = assetBundleClient.GetAssetBundle(data.url, data.version); if (data.assetBundle != null) { InnerLoadAsset(); } }); }; // HDebug.Log (data.assetBundleName + " / " + data.objName); #if UNITY_EDITOR if (SceneManager.Instance.EditorLocalLoadAssetBundle) { string[] paths = AssetDatabase.GetAssetPathsFromAssetBundleAndAssetName(data.assetBundleName, data.objName); string path = ""; if (paths.Length <= 0) { paths = AssetDatabase.GetAssetPathsFromAssetBundle(data.assetBundleName); if (paths.Length <= 0) { InnerLoadAssetBundle(); return; } string[] files = Directory.GetFiles(paths [0], data.objName + ".*"); if (files.Length <= 0) { InnerLoadAssetBundle(); return; } path = files [0]; } else { path = paths [0]; } object temp = AssetDatabase.LoadMainAssetAtPath(path); if (data.type == typeof(Sprite)) { temp = Util.TextureConvertSprite(temp); } if (Path.GetFileNameWithoutExtension(path) != data.objName) { temp = null; } LogLoadAsset(data, temp); finished(temp); } else { InnerLoadAssetBundle(); } #else InnerLoadAssetBundle(); #endif }