static int OpenFromData(IntPtr L) { try { ToLua.CheckArgsCount(L, 2); IniUtil obj = (IniUtil)ToLua.CheckObject(L, 1, typeof(IniUtil)); string arg0 = ToLua.CheckString(L, 2); bool o = obj.OpenFromData(arg0); LuaDLL.lua_pushboolean(L, o); return(1); } catch (Exception e) { return(LuaDLL.toluaL_exception(L, e)); } }
/// <summary> /// 初始化游戏管理器 /// </summary> void Awake() { Screen.sleepTimeout = SleepTimeout.NeverSleep; Application.targetFrameRate = Const.GameFrameRate; // 创建日志系统 LogUtil.GetInstance().Create(PathUtil.LogPath()); LogUtil.GetInstance().WriteGame("------------------ Game Log Begin ------------------"); LogUtil.GetInstance().WriteNet("------------------ Net Log Begin ------------------"); // 本地化系统初始化,再读一遍,因为文字表有更新 if (Localization.init() < 0) { LogUtil.GetInstance().WriteGame("Localization Init Failed"); return; } LogUtil.GetInstance().WriteGame("STEP 1"); // 再次读取本地程序配置文件,因为编辑器模式下,启动场景不使用 if (m_pLocalAppIni.OpenFromData(m_szAppTxt) == false) { LogUtil.GetInstance().WriteGame(m_szAppTxt + " Not Found!!!"); } else { m_pLocalAppIni.SetGlobal(); } LogUtil.GetInstance().WriteGame("STEP 2"); // 再次读取本地资源版本文件,因为编辑器模式下,启动场景不使用 if (m_pLocalResverIni.OpenFromTXT(PathUtil.ConfigPath() + m_szResverTxt) == false) { LogUtil.GetInstance().WriteGame(PathUtil.ConfigPath() + m_szResverTxt + " Not Found"); } else { m_pLocalResverIni.SetGlobal(); } LogUtil.GetInstance().WriteGame("STEP 3"); Utils.Add <ObjectPoolManager>(gameObject); // 添加对象池管理器 LogUtil.GetInstance().WriteGame("Add ObjectPoolManager"); Utils.Add <ResourceManager>(gameObject); // 添加资源管理器组件 LogUtil.GetInstance().WriteGame("Add ResourceManager"); Utils.Add <SocketClient>(gameObject); // 添加socket组件 LogUtil.GetInstance().WriteGame("Add SocketClient"); Utils.Add <NetworkManager>(gameObject); // 添加网络消息管理器组件 LogUtil.GetInstance().WriteGame("Add NetworkManager"); Utils.Add <UIManager>(gameObject); // 添加UI管理器 LogUtil.GetInstance().WriteGame("Add UIManager"); Utils.Add <LuaManager>(gameObject); // 添加LUA管理器 LogUtil.GetInstance().WriteGame("Add LuaManager"); // 初始化LUA LuaInited(); LogUtil.GetInstance().WriteGame("LuaInited"); // 初始化资源管理 eye.resourceManager.Initialize(delegate() { // 先读取部分AssetBundle ResourceManager.LoadAssetBundle("ui_dialog_login"); // 游戏开始 LuaFun.gameStart.Call(); // 启动游戏主逻辑, 每分钟调用 StartCoroutine(GameLogic()); }); }
public bool Initialize() { // 创建日志系统 LogUtil.GetInstance().Create(PathUtil.LogPath()); LogUtil.GetInstance().WriteGame("------------------ Game Log Begin ------------------"); LogUtil.GetInstance().WriteNet("------------------ Net Log Begin ------------------"); // 本地化系统初始化,再读一遍,因为文字表有更新 if (Localization.init() < 0) { LogUtil.GetInstance().WriteGame("Localization Init Failed"); return(false); } LogUtil.GetInstance().WriteGame("STEP 1"); // 再次读取本地程序配置文件,因为编辑器模式下,启动场景不使用 if (m_pLocalAppIni.OpenFromData(m_szAppTxt) == false) { LogUtil.GetInstance().WriteGame(m_szAppTxt + " Not Found!!!"); } else { m_pLocalAppIni.SetGlobal(); } LogUtil.GetInstance().WriteGame("STEP 2"); // 再次读取本地资源版本文件,因为编辑器模式下,启动场景不使用 if (m_pLocalResverIni.OpenFromTXT(PathUtil.ConfigPath() + m_szResverTxt) == false) { LogUtil.GetInstance().WriteGame(PathUtil.ConfigPath() + m_szResverTxt + " Not Found"); } else { m_pLocalResverIni.SetGlobal(); } LogUtil.GetInstance().WriteGame("STEP 3"); Utils.Add <ObjectPoolManager>(gameObject); // 添加对象池管理器 LogUtil.GetInstance().WriteGame("Add ObjectPoolManager"); Utils.Add <ResourceManager>(gameObject); // 添加资源管理器组件 LogUtil.GetInstance().WriteGame("Add ResourceManager"); Utils.Add <SocketClient>(gameObject); // 添加socket组件 LogUtil.GetInstance().WriteGame("Add SocketClient"); Utils.Add <NetworkManager>(gameObject); // 添加网络消息管理器组件 LogUtil.GetInstance().WriteGame("Add NetworkManager"); Utils.Add <UIManager>(gameObject); // 添加UI管理器 LogUtil.GetInstance().WriteGame("Add UIManager"); Utils.Add <LuaManager>(gameObject); // 添加LUA管理器 LogUtil.GetInstance().WriteGame("Add LuaManager"); Utils.Add <AudioManager>(gameObject); // 添加音效管理器 LogUtil.GetInstance().WriteGame("Add AudioManager"); // 初始化LUA LuaInited(); LogUtil.GetInstance().WriteGame("LuaInited"); // 初始化资源管理 eye.resourceManager.Initialize(delegate() { // 游戏开始 LuaFun.gameStart.Call(); // 启动游戏主逻辑, 每秒调用 InvokeRepeating("GameLogic", 0, 1.0f); m_bLuaInit = true; }); return(true); }
/// <summary> /// 第四步,启动web更新逻辑 /// </summary> IEnumerator OnUpdateResource() { LogUtil.GetInstance().WriteGame("Start UpdateResource!"); // 更新过程中,每秒都需要进行网络检查 StartCoroutine(OnCheckNetwork()); // 本地平台 IniUtil plat = new IniUtil(); plat.OpenFromData("plat.txt"); Const.platid = Convert.ToInt32(plat.ReadValue("PLATID", "")); LogUtil.GetInstance().WriteGame("PlatID:" + Const.platid); // 本地资源版本号 m_local_resource_version = Convert.ToInt32(m_pLocalResverIni.ReadValue("RESOURCE_VERSION", "0")); LogUtil.GetInstance().WriteGame("UpdateResource Start local_version:" + m_local_resource_version); // 本地资源文件列表,生成字典结构,文件名当索引,以便查找 Dictionary <string, string> szlocalFileMd5Dict = new Dictionary <string, string>(); Dictionary <string, int> szlocalFileSizeDict = new Dictionary <string, int>(); // 读取本地文件列表 string[] szlocalFileList = File.ReadAllLines(PathUtil.LocalPath() + m_szFilesName); LogUtil.GetInstance().WriteGame("FileList Readed!"); // 解析文件列表 foreach (var localFile in szlocalFileList) { string[] values = localFile.Split('|'); string fileName = values[0]; string fileMd5 = values[1]; int fileSize = Convert.ToInt32(values[2]); szlocalFileMd5Dict.Add(fileName, fileMd5); szlocalFileSizeDict.Add(fileName, fileSize); } // 更新地址 bool appSucc = m_pAppIni.OpenFromData(m_szAppTxt); LogUtil.GetInstance().WriteGame("Read App.txt:" + appSucc.ToString()); string url = Convert.ToString(m_pAppIni.ReadValue("UPDATE_URL", "")); // 获取服务器的更新文件地址 // 先取cfg文件,没有文件才取php LogUtil.GetInstance().WriteGame(url + "/update_" + m_szFilePrefix + ".cfg"); WWW www = new WWW(url + "/update_" + m_szFilePrefix + ".cfg"); yield return(www); if (www.error != null) { // 取php LogUtil.GetInstance().WriteGame(url + "/update_" + m_szFilePrefix + ".php"); www = new WWW(url + "/update_" + m_szFilePrefix + ".php"); yield return(www); if (www.error != null) { // 更新地址错误也进游戏 //m_uiText.text = "update url error"; OnInited(); yield break; } } LogUtil.GetInstance().WriteGame("www.text:" + www.text); string[] result = www.text.Split('|'); if (result.Length < 2) { OnInited(); yield break; } // 最新版本 int latest_version = Convert.ToInt32(result[0]); // 更新地址 string updateResUrl = result[1]; // 强制更新版本号(程序版本),低于这个版本都需要强制更新 if (result.Length == 3) { int forceVersion = Convert.ToInt32(result[2]); // 本地程序版本号 string[] appVersionArray = Application.version.Split('.'); int appVersion = Convert.ToInt32(appVersionArray[2]); LogUtil.GetInstance().WriteGame("appVersion:" + appVersion); if (appVersion < forceVersion) { // 请前去下载最新版本游戏 m_uiForceUpdateBox.transform.Find("Text").GetComponent <UIText>().text = Localization.text(425); m_uiForceUpdateBox.SetActive(true); yield break; } } LogUtil.GetInstance().WriteGame("web_latest_version:" + latest_version); if (latest_version < m_local_resource_version) { // 本地版本大于远端版本,不更新直接进游戏 //m_uiText.text = Localization.text( 403 ); LogUtil.GetInstance().WriteGame("Has been the latest version"); OnInited(); yield break; } // 先下载文件列表到本地 www = new WWW(updateResUrl + "/" + m_szFilesName); yield return(www); if (www.error != null) { //m_uiText.text = "files url error"; LogUtil.GetInstance().WriteGame("files url error:" + www.error); OnInited(); yield break; } // 如果存在,那么先删除在创建 if (Directory.Exists(PathUtil.PatchDownloadPath())) { Directory.Delete(PathUtil.PatchDownloadPath(), true); } Directory.CreateDirectory(PathUtil.PatchDownloadPath()); File.WriteAllBytes(PathUtil.PatchDownloadPath() + m_szFilesName, www.bytes); // 比较web下载下来的文件列表和本地的两个文件列表的差异数据 m_downloadsize_total = 0; m_szNeedUpdateFileDict = new Dictionary <string, DownLoadFile>(); string[] szUpdateFileList = File.ReadAllLines(PathUtil.PatchDownloadPath() + m_szFilesName); foreach (var updateFile in szUpdateFileList) { string[] values = updateFile.Split('|'); string updateFileName = values[0]; string updateFileMd5 = values[1]; int updateFileSize = Convert.ToInt32(values[2]); string updateFilePath = (PathUtil.PatchDownloadPath() + updateFileName).Trim(); string updateFileDirectory = Path.GetDirectoryName(updateFilePath); if (!Directory.Exists(updateFileDirectory)) { Directory.CreateDirectory(updateFileDirectory); #if UNITY_IOS UnityEngine.iOS.Device.SetNoBackupFlag(updateFileDirectory); #endif } // 1.先找这个文件本地是否存在,不存在直接下载 bool isUpdate = false; // 2.如果存在,比较文件大小,一样大就比较MD5,不一样大就直接下载 if (szlocalFileSizeDict.ContainsKey(updateFileName) == false) { isUpdate = true; LogUtil.GetInstance().WriteGame("[" + updateFilePath + "]Not Exists,Download!"); } else { if (szlocalFileSizeDict[updateFileName] != updateFileSize) { isUpdate = true; LogUtil.GetInstance().WriteGame("[" + updateFilePath + "] FileSize,Download!"); } else { if (!szlocalFileMd5Dict[updateFileName].Equals(updateFileMd5)) { isUpdate = true; LogUtil.GetInstance().WriteGame("[" + updateFilePath + "] MD5,Download!"); } } } // 如果需要更新 if (isUpdate) { m_szNeedUpdateFileDict.Add(updateFileName, new DownLoadFile(updateFileSize, updateFileMd5)); m_downloadsize_total += updateFileSize; } } // 这里要询问玩家是否进行更新 if (m_downloadsize_total > 1024 * 1024) { // 大于1mb提示 m_AskSureUpdate = true; long nTotalSize = m_downloadsize_total; if (nTotalSize < 1024) { nTotalSize = 1024; } m_uiWarningBox.transform.Find("Text").GetComponent <UIText>().text = string.Format(Localization.text(407), nTotalSize / 1024.0 / 1024.0); m_uiWarningBox.SetActive(true); } else { // 小于1mb不提示 m_AskSureUpdate = false; m_uiWarningBox.SetActive(false); } // 等待按钮改变这个值 while (m_AskSureUpdate) { yield return(new WaitForEndOfFrame()); } // 初始化线程管理器 Utils.Add <ThreadManager>(gameObject); // 开始更新 bool isNeedUpdate = false; foreach (var updateInfo in m_szNeedUpdateFileDict) { isNeedUpdate = true; m_uiText.text = Localization.text(404); LogUtil.GetInstance().WriteGame("<Download>" + "[name]:" + updateInfo.Key + "|[size]:" + updateInfo.Value.size); bool downloadFinish = false; while (!downloadFinish) { // 启动下载文件 DownloadFile(updateResUrl + "/", updateInfo.Key); // 此处轮询检查这个文件时候已经下载 while (!m_downloadFiles.Contains(updateInfo.Key)) { yield return(new WaitForEndOfFrame()); } // 刚刚下载完成的文件 string curr_updateFile = PathUtil.PatchDownloadPath() + updateInfo.Key; // 没这个文件 if (!File.Exists(curr_updateFile)) { LogUtil.GetInstance().WriteGame("<Download Miss>" + "[path]:" + curr_updateFile); m_downloadFiles.Remove(updateInfo.Key); updateInfo.Value.BytesReceived = 0; continue; } // 检查下载成功的文件 LogUtil.GetInstance().WriteGame("<Download MD5 Check>" + "[path]:" + curr_updateFile); string curr_md5 = Utils.md5file(curr_updateFile); if (updateInfo.Value.md5.Equals(curr_md5)) { LogUtil.GetInstance().WriteGame("<Download MD5 Succesed>" + "[path]:" + curr_updateFile); downloadFinish = true; } else { // 下载失败,重新下载 LogUtil.GetInstance().WriteGame("<Download Failed>" + "[path]:" + curr_updateFile + "[web]:" + updateInfo.Value.md5 + "[local]:" + curr_md5); m_downloadFiles.Remove(updateInfo.Key); updateInfo.Value.BytesReceived = 0; } } } // 更新完毕之后将临时补丁文件夹内容拷贝到正式本地目录 if (isNeedUpdate) { m_uiText.text = Localization.text(405); LogUtil.GetInstance().WriteGame("update success"); // 拷贝所有文件 FileUtil.CopyDir(PathUtil.PatchDownloadPath(), PathUtil.LocalPath()); Directory.Delete(PathUtil.PatchDownloadPath(), true); // 更新程序资源版本文件 File.Copy(PathUtil.LocalPath() + m_szResverTxt, PathUtil.ConfigPath() + m_szResverTxt, true); // 重读本地资源版本文件 m_pLocalResverIni.OpenFromTXT(PathUtil.ConfigPath() + m_szResverTxt); } else { //m_uiText.text = Localization.text( 403 ); m_uiText.text = ""; LogUtil.GetInstance().WriteGame("Has been the latest version"); } OnInited(); yield break; }
// 根据选择语言进行初始化 public static int init() { destroy(); countryCodeTable = new Dictionary <string, string[]>(); countryIdTable = new Dictionary <int, string[]>(); languageCodeTable = new Dictionary <string, string[]>(); languageIdTable = new Dictionary <int, string[]>(); // 读取国家信息 TableUtil _countryTxt = new TableUtil(); if (_countryTxt.OpenFromData("localization_country.txt") == false) { return(-1); } for (int row = 0; row < _countryTxt.GetRecordsNum(); row++) { string countryCode = _countryTxt.GetValue(row, 0); // 国家英文代码(国际标准) string CountryID = _countryTxt.GetValue(row, 1); // 国家数字代码(国际标准) string CountryNameID = _countryTxt.GetValue(row, 2); // 国家名称ID string CountryFlag = _countryTxt.GetValue(row, 3); // 国旗资源名 string CountryMoney = _countryTxt.GetValue(row, 4); // 货币简称 // 英文编码作key if (countryCodeTable.ContainsKey(countryCode) == false) { string[] ls = new string[4]; ls[0] = CountryID; ls[1] = CountryNameID; ls[2] = CountryFlag; ls[3] = CountryMoney; countryCodeTable.Add(countryCode, ls); } // 数字编码作key if (countryIdTable.ContainsKey(Convert.ToInt32(CountryID)) == false) { string[] ls = new string[4]; ls[0] = countryCode; ls[1] = CountryNameID; ls[2] = CountryFlag; ls[3] = CountryMoney; countryIdTable.Add(Convert.ToInt32(CountryID), ls); } } // 读取语言信息 TableUtil _languageTxt = new TableUtil(); if (_languageTxt.OpenFromData("localization_language.txt") == false) { return(-2); } for (int row = 0; row < _languageTxt.GetRecordsNum(); row++) { string LanguageCode = _languageTxt.GetValue(row, 0); // 语言英文代码(国际标准) string LanguageCustomID = _languageTxt.GetValue(row, 1); // 语言自定义ID(无标准,暂时跟u3d里定义的语言ID走) string LanguageNameID = _languageTxt.GetValue(row, 2); // 语言名称ID string LanguageDirectory = _languageTxt.GetValue(row, 3); // 语言存在目录 string TranslationCode = _languageTxt.GetValue(row, 4); // 翻译Code // 英文编码作key if (languageCodeTable.ContainsKey(LanguageCode) == false) { string[] ls = new string[4]; ls[0] = LanguageCustomID; ls[1] = LanguageNameID; ls[2] = LanguageDirectory; ls[3] = TranslationCode; languageCodeTable.Add(LanguageCode, ls); } // 数字编码作key if (languageIdTable.ContainsKey(Convert.ToInt32(LanguageCustomID)) == false) { string[] ls = new string[4]; ls[0] = LanguageCode; ls[1] = LanguageNameID; ls[2] = LanguageDirectory; ls[3] = TranslationCode; languageIdTable.Add(Convert.ToInt32(LanguageCustomID), ls); } } // 用户本地配置文件里获取玩家已经选择的国家语言 IniUtil userIni = new IniUtil(); userIni.OpenFromTXT(PathUtil.ConfigPath() + "user.txt"); // 读取使用语言 currentLanguage = userIni.ReadValue("LANGUAGE", "").ToString(); if (currentLanguage == "") { // 没有存档的,那么使用配置文件默认的 IniUtil appIni = new IniUtil(); appIni.OpenFromData("app.txt"); currentLanguage = appIni.ReadValue("DEFAULT_LANGUAGE", "").ToString(); userIni.WriteValue("LANGUAGE", currentLanguage); } if (currentLanguage == "") { // 没有默认的,那么使用设备当前的 currentLanguage = DeviceHelper.getLanguage(); userIni.WriteValue("LANGUAGE", currentLanguage); } // 读取使用国家 currentCountry = userIni.ReadValue("COUNTRY", "").ToString(); if (currentCountry == "") { // 没有存档的,使用设备当前的 currentCountry = DeviceHelper.getCountry(); userIni.WriteValue("COUNTRY", currentCountry); } // 先去语言信息里找一下,语言-地区的目录(如zh-cn),没有则直接使用语言(如zh,ru,en等),还是没有则使用英文的 string localizeDir = languageDirectory(currentLanguage + "-" + currentCountry); if (localizeDir == "") { localizeDir = languageDirectory(currentLanguage); if (localizeDir == "") { //localizeDir = languageDirectory( "en" ); localizeDir = languageDirectory("tw"); if (localizeDir == "") { return(-3); } } } localizeDir = localizeDir + "/"; // 读取基础文字表 TableUtil _localizetxt = new TableUtil(); localize = new Dictionary <int, string>(); if (_localizetxt.OpenFromData(localizeDir + "localize.txt") == false) { // 没有对应的语言文件夹,用英语的 //localizeDir = languageDirectory( "en" ) + "/"; localizeDir = languageDirectory("tw") + "/"; if (_localizetxt.OpenFromData(localizeDir + "localize.txt") == false) { return(-4); } } for (int row = 0; row < _localizetxt.GetRecordsNum(); row++) { int key = Convert.ToInt32(_localizetxt.GetValue(row, 0)); string text = _localizetxt.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize.Add(key, text); } // 读取道具文字表 TableUtil _localizeItemTxt = new TableUtil(); localize_item = new Dictionary <int, string>(); if (_localizeItemTxt.OpenFromData(localizeDir + "localize_item.txt") == false) { // 没有对应的语言文件夹,用英语的 //localizeDir = languageDirectory( "en" ) + "/"; localizeDir = languageDirectory("tw") + "/"; if (_localizeItemTxt.OpenFromData(localizeDir + "localize_item.txt") == false) { return(-5); } } for (int row = 0; row < _localizeItemTxt.GetRecordsNum(); row++) { int key = Convert.ToInt32(_localizeItemTxt.GetValue(row, 0)); string text = _localizeItemTxt.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize_item.Add(key, text); } // 读取任务文字表 TableUtil _localizeQuestTxt = new TableUtil(); localize_quest = new Dictionary <int, string>(); if (_localizeQuestTxt.OpenFromData(localizeDir + "localize_quest.txt") == false) { // 没有对应的语言文件夹,用英语的 //localizeDir = languageDirectory( "en" ) + "/"; localizeDir = languageDirectory("tw") + "/"; if (_localizeQuestTxt.OpenFromData(localizeDir + "localize_quest.txt") == false) { return(-6); } } for (int row = 0; row < _localizeQuestTxt.GetRecordsNum(); row++) { int key = Convert.ToInt32(_localizeQuestTxt.GetValue(row, 0)); string text = _localizeQuestTxt.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize_quest.Add(key, text); } // 容错处理,所有找不到的文字使用英文 //localizeDir = languageDirectory( "en" ) + "/"; localizeDir = languageDirectory("tw") + "/"; // 读取基础文字表 TableUtil localizetxt_en_us = new TableUtil(); localize_en_us = new Dictionary <int, string>(); localizetxt_en_us.OpenFromData(localizeDir + "localize.txt"); for (int row = 0; row < localizetxt_en_us.GetRecordsNum(); row++) { int key = Convert.ToInt32(localizetxt_en_us.GetValue(row, 0)); string text = localizetxt_en_us.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize_en_us.Add(key, text); } // 读取道具文字表 TableUtil localizeitemtxt_en_us = new TableUtil(); localize_item_en_us = new Dictionary <int, string>(); localizeitemtxt_en_us.OpenFromData(localizeDir + "localize_item.txt"); for (int row = 0; row < localizeitemtxt_en_us.GetRecordsNum(); row++) { int key = Convert.ToInt32(localizeitemtxt_en_us.GetValue(row, 0)); string text = localizeitemtxt_en_us.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize_item_en_us.Add(key, text); } // 读取任务文字表 TableUtil localizequesttxt_en_us = new TableUtil(); localize_quest_en_us = new Dictionary <int, string>(); localizequesttxt_en_us.OpenFromData(localizeDir + "localize_quest.txt"); for (int row = 0; row < localizequesttxt_en_us.GetRecordsNum(); row++) { int key = Convert.ToInt32(localizequesttxt_en_us.GetValue(row, 0)); string text = localizequesttxt_en_us.GetValue(row, 1); text = text.Replace("\\n", "\n"); localize_quest_en_us.Add(key, text); } bInited = true; return(0); }