Exemplo n.º 1
0
        private void CheckTime(uint svrTime)
        {
            mClientReceiveTimePoint.Add(UnityEngine.Time.realtimeSinceStartup);
            mServerReceiveTimePoint.Add(svrTime);
            if (mServerReceiveTimePoint.Count < 2)
            {
                return;
            }
            float great = mClientReceiveTimePoint[1] - mClientSendTimePoint[0];
            float m     = (mServerReceiveTimePoint[1] - mServerReceiveTimePoint[0]) * 0.001f;
            float less  = mClientSendTimePoint[1] - mClientReceiveTimePoint[0];

            if (m > less)
            {
                mToolCnt = 0;
                mServerReceiveTimePoint.RemoveAt(0);
                mClientSendTimePoint.RemoveAt(0);
                mClientReceiveTimePoint.RemoveAt(0);
                return;
            }

            mToolCnt++;
            DebugUtils.Error("CheckTime", string.Format("may be used tool cnt: {0} c1 c2 {1}, {2} c1` c2, {3} c1` c2,{4} s1` s2`,{5},{6}", mToolCnt, mClientSendTimePoint[0], mClientSendTimePoint[1], mClientReceiveTimePoint[0], mClientReceiveTimePoint[1], mServerReceiveTimePoint[0], mServerReceiveTimePoint[1]));
            mServerReceiveTimePoint.RemoveAt(0);
            mClientSendTimePoint.RemoveAt(0);
            mClientReceiveTimePoint.RemoveAt(0);
            if (mToolCnt >= 2)
            {
                mToolCnt = 0;
                mServerReceiveTimePoint.Clear();
                mClientSendTimePoint.Clear();
                mClientReceiveTimePoint.Clear();
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// 从指定的 URL 加载 map 数据。
 /// </summary>
 /// <param name="file">文件的 URL,该文件包含要加载的 XML 文档 或者 XML文本内容</param>
 /// <param name="map">map 数据</param>
 /// <returns>是否加载成功</returns>
 public static bool LoadIntMap(string content, out Dictionary <int, Dictionary <string, string> > map)
 {
     try
     {
         if (string.IsNullOrEmpty(content))
         {
             DebugUtils.Error("IsNullOrEmpty", "File not exist");
             map = null;
             return(false);
         }
         SecurityElement xml = LoadFromXmlStr(content);
         if (xml == null)
         {
             DebugUtils.Error("LoadIntMap", "File not exist: " + content);
             map = null;
             return(false);
         }
         else
         {
             map = LoadIntMap(xml, content);
             return(true);
         }
     }
     catch (Exception ex)
     {
         DebugUtils.Error("LoadIntMap", ex.Message);
         map = null;
         return(false);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// 将指定格式(1.0, 2, 3.4) 转换为 Vector3
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="result"></param>
        /// <returns>返回 true/false 表示是否成功</returns>
        public static bool ParseVector3(string inputString, out Vector3 result)
        {
            MatchCollection collects = MatchVector(inputString);

            result = new Vector3();
            try
            {
                if (collects.Count == 3)
                {
                    result[0] = float.Parse(collects[0].Value);
                    result[1] = float.Parse(collects[1].Value);
                    result[2] = float.Parse(collects[2].Value);
                }
                else if (collects.Count == 2)
                {
                    result[0] = float.Parse(collects[0].Value);
                    result[2] = float.Parse(collects[1].Value);
                }
                else if (collects.Count == 1)
                {
                    result[0] = float.Parse(collects[0].Value);
                }
            }
            catch (Exception e)
            {
                DebugUtils.Error("XmlData", "Parse Vector3 error: " + inputString + e.ToString());
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 将字典字符串转换为键类型为 T,值类型为 U 的字典对象。
        /// </summary>
        /// <typeparam name="T">字典Key类型</typeparam>
        /// <typeparam name="U">字典Value类型</typeparam>
        /// <param name="strMap">字典字符串</param>
        /// <param name="keyValueSpriter">键值分隔符</param>
        /// <param name="mapSpriter">字典项分隔符</param>
        /// <returns>字典对象</returns>
        public static Dictionary <T, U> ParseMapAny <T, U>(String strMap, Char keyValueSpriter = KEY_VALUE_SPRITER, Char mapSpriter = MAP_SPRITER)
        {
            var typeT  = typeof(T);
            var typeU  = typeof(U);
            var result = new Dictionary <T, U>();
            //先转为字典
            var strResult = ParseMap(strMap, keyValueSpriter, mapSpriter);

            foreach (var item in strResult)
            {
                try
                {
                    object key1   = GetValue(item.Key, typeT);
                    object value1 = GetValue(item.Value, typeU);
                    if (key1 != null && value1 != null)
                    {
                        T key   = (T)key1;
                        U value = (U)value1;
                        result.Add(key, value);
                    }
                }
                catch (Exception)
                {
                    DebugUtils.Error("XmlData", String.Format("Parse failure: {0}, {1}", item.Key, item.Value));
                }
            }
            return(result);
        }
Exemplo n.º 5
0
        /// <summary>
        ///  触发事件, 带3个参数触发
        /// </summary>
        /// <param name="eventType"></param>
        /// <param name="handler"></param>
        public void TriggerEvent <T, U, V>(EventType eventType, T arg1, U arg2, V arg3)
        {
            Delegate d;

            if (!TheRouter.TryGetValue(eventType, out d))
            {
                return;
            }
            var callbacks = d.GetInvocationList();

            for (int i = 0; i < callbacks.Length; i++)
            {
                d = callbacks[i];
                Action <T, U, V> callback = d as Action <T, U, V>;
                if ((d.Target != null) && (d.Target is UnityEngine.Object) && d.Target.Equals(null))
                {
                    RemoveEventListener(eventType, callback);
#if UNITY_EDITOR
                    DebugUtils.Error("TriggerEvent3", string.Format("TriggerEvent {0} error: unity object destoyed, but not removeListners", eventType));
#endif
                }
                if (callback == null)
                {
                    throw new EventException(string.Format("TriggerEvent {0} error: types of parameters are not match.", eventType));
                }
                try
                {
                    callback(arg1, arg2, arg3);
                }
                catch (Exception ex)
                {
                    Debug.LogError(ex);
                }
            }
        }
Exemplo n.º 6
0
        public static AbstractNavPath Create(NavPathType pathType, NavPathData pathData, Vector3 offset, bool pathFlipOn, IPathTrigger triggerHandler)
        {
            AbstractNavPath navPath = null;

            if (pathData.WayPoints.Count == 2 && pathType != NavPathType.LinePosLineDir)
            {
                pathType = NavPathType.LinePosLineDir;
                DebugUtils.Warning("AbstractNavPath", "Create LineType, Not ", EnumUtils.EnumToString(pathType));
            }
            switch (pathType)
            {
            case NavPathType.CurvePosCurveDir:
                navPath = new NavCurvePosCurveDir(pathData, offset, pathFlipOn, triggerHandler);
                break;

            case NavPathType.LinePosLineDir:
                navPath = new NavCurvePosCurveDir(pathData, offset, pathFlipOn, triggerHandler);
                break;

            case NavPathType.LinePosLineAngle:
                navPath = new NavCurvePosCurveDir(pathData, offset, pathFlipOn, triggerHandler);
                break;

            case NavPathType.LinePosCurveDir:
                navPath = new NavCurvePosCurveDir(pathData, offset, pathFlipOn, triggerHandler);
                break;

            default:
                DebugUtils.Error("AbstractNavPath", "Create Not Supported ", EnumUtils.EnumToString(pathType));
                break;
            }
            return(navPath);
        }
Exemplo n.º 7
0
        /// <summary>
        /// 从指定的 XML 文档加载 map 数据。
        /// </summary>
        /// <param name="xml">XML 文档</param>
        /// <returns>map 数据</returns>
        public static Dictionary <int, Dictionary <string, string> > LoadIntMap(SecurityElement xml, string source)
        {
            var result = new Dictionary <int, Dictionary <string, string> >();

            if (xml.Children == null)
            {
                return(result);
            }
            var index = 0;

            foreach (SecurityElement subMap in xml.Children)
            {
                index++;
                if (subMap.Children == null || subMap.Children.Count == 0)
                {
                    Debug.LogError("empty row in row NO." + index + " of " + source);
                    continue;
                }
                string text = (subMap.Children[0] as SecurityElement).Text;
                if (string.IsNullOrEmpty(text))
                {
                    continue;
                }
                int key = int.Parse(text);
                if (result.ContainsKey(key))
                {
                    DebugUtils.Error("LoadIntMap", string.Format("Key {0} already exist, in {1}.", key, source));
                    continue;
                }

                var children = new Dictionary <string, string>();
                result.Add(key, children);
                for (int i = 1; i < subMap.Children.Count; i++)
                {
                    var node = subMap.Children[i] as SecurityElement;
                    if (node != null)
                    {
                        //对属性名称部分后缀进行裁剪, 去除 字段类型标签
                        string tag = node.Tag;
                        if (!children.ContainsKey(tag))
                        {
                            if (string.IsNullOrEmpty(node.Text))
                            {
                                children.Add(tag, "IsNullOrEmpty");
                            }
                            else
                            {
                                children.Add(tag, node.Text.Trim());
                            }
                        }
                        else
                        {
                            DebugUtils.Error("LoadIntMap", string.Format("Key {0} already exist, index {1} of {2}.", node.Tag, i, node.ToString()));
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 转换复杂类型的对象到LuaTable,不支持基础类型直接转换。
        /// </summary>
        /// <param name="target"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public static bool PackLuaTable(object target, out LuaTable result)
        {
            var type = target.GetType();

            if (type == typeof(LuaTable))
            {
                result = target as LuaTable;
                return(true);
            }
            if (type.IsGenericType)
            {
                //容器类型
                //目前只支持列表与字典的容器类型转换
                if (type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                {
                    return(PackLuaTable(target as IDictionary, out result));
                }
                else
                {
                    return(PackLuaTable(target as IList, out result));
                }
            }
            else
            {
                //实体类型
                result = new LuaTable();
                try
                {
                    var props = type.GetProperties(~BindingFlags.Static);
                    for (int i = 0; i < props.Length; i++)
                    {
                        var prop = props[i];
                        if (IsBaseType(prop.PropertyType))
                        {
                            result.Add(i + 1, prop.GetGetMethod().Invoke(target, null));
                        }
                        else
                        {
                            LuaTable lt;
                            var      value = prop.GetGetMethod().Invoke(target, null);
                            var      flag  = PackLuaTable(value, out lt);
                            if (flag)
                            {
                                result.Add(i + 1, lt);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    DebugUtils.Error("PackLuaTable", "Entity Error: ", ex.Message);
                }
            }
            return(true);
        }
Exemplo n.º 9
0
 /// <summary>
 /// 转换字典类型的对象到LuaTable。
 /// </summary>
 /// <param name="target"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool PackLuaTable(IDictionary target, out LuaTable result)
 {
     Type[] types = target.GetType().GetGenericArguments();
     result = new LuaTable();
     try
     {
         foreach (DictionaryEntry item in target)
         {
             if (IsBaseType(types[1]))
             {
                 object value;
                 //判断值是否布尔类型,是则做特殊转换
                 if (types[1] == typeof(bool))
                 {
                     value = (bool)item.Value ? 1 : 0;
                 }
                 else
                 {
                     value = item.Value;
                 }
                 //判断键是否为整型,是则标记键为整型,转lua table字符串时有用
                 if (types[0] == typeof(int))
                 {
                     result.Add(item.Key.ToString(), false, value);
                 }
                 else
                 {
                     result.Add(item.Key.ToString(), value);
                 }
             }
             else
             {
                 LuaTable value = null;
                 var      flag  = PackLuaTable(item.Value, out value);
                 if (flag)
                 {
                     if (types[0] == typeof(int))
                     {
                         result.Add(item.Key.ToString(), false, value);
                     }
                     else
                     {
                         result.Add(item.Key.ToString(), value);
                     }
                 }
             }
         }
     }
     catch (Exception ex)
     {
         DebugUtils.Error("PackLuaTable", "Dictionary Error: " + ex.Message);
     }
     return(true);
 }
Exemplo n.º 10
0
        public object FormatXMLData(string fileName, Type dicType, Type type, string fileContent = null)
        {
            object result = null;

            if (string.IsNullOrEmpty(fileName))
            {
                DebugUtils.Warning("FormatXMLData", "fileName IsNullOrEmpty");
                return(result);
            }
            try
            {
                result = dicType.GetConstructor(Type.EmptyTypes).Invoke(null);
                Dictionary <Int32, Dictionary <String, String> > map;//Int32 为 mId, string 为 属性名, string 为 属性值
                if (fileContent != null && XmlFileUtils.LoadIntMap(fileContent, out map))
                {
                    var props = type.GetProperties();//获取实体属性
                    foreach (var item in map)
                    {
                        var t = type.GetConstructor(Type.EmptyTypes).Invoke(null);//构造实体实例
                        foreach (var prop in props)
                        {
                            if (prop.Name == XmlData.mKeyFieldName)
                            {
                                prop.SetValue(t, item.Key, null);
                            }
                            else
                            {
                                if (item.Value.ContainsKey(prop.Name))
                                {
                                    var value = XmlFileUtils.GetValue(item.Value[prop.Name], prop.PropertyType);
                                    prop.SetValue(t, value, null);
                                }
                            }
                        }
                        dicType.GetMethod("Add").Invoke(result, new object[] { item.Key, t });
                    }
                    DebugUtils.Info(fileName + " Loaded ", map.Count);
                }
                else
                {
                    result = null;
                    DebugUtils.Warning(fileName + " Not Founded ", "Please Check Timestamps right");
                }
            }
            catch (Exception ex)
            {
                DebugUtils.Error("XmlData", ex.Message);
            }
            return(result);
        }
Exemplo n.º 11
0
 /// <summary>
 /// 从指定的字符串加载 XML 文档。
 /// </summary>
 /// <param name="xml">包含要加载的 XML 文档的字符串。</param>
 /// <returns>编码安全对象的 XML 对象模型。</returns>
 public static SecurityElement LoadXML(string xml)
 {
     try
     {
         SecurityParser securityParser = new SecurityParser();
         securityParser.LoadXml(xml);
         return(securityParser.ToXml());
     }
     catch (Exception ex)
     {
         DebugUtils.Error("LoadXML", ex.Message);
         return(null);
     }
 }
Exemplo n.º 12
0
        public static List <T> LoadXML <T>(string path)
        {
            var      text = LoadTextFile(path);
            List <T> list = new List <T>();

            try
            {
                if (string.IsNullOrEmpty(text))
                {
                    return(list);
                }
                Type type = typeof(T);
                var  xml  = LoadXML(text);
                Dictionary <int, Dictionary <String, String> > map = LoadIntMap(xml, text);
                var props = type.GetProperties();
                foreach (var item in map)
                {
                    var obj = type.GetConstructor(Type.EmptyTypes).Invoke(null);
                    foreach (var prop in props)
                    {
                        if (prop.Name == XmlData.mKeyFieldName)
                        {
                            prop.SetValue(obj, item.Key, null);
                        }
                        else
                        {
                            try
                            {
                                if (item.Value.ContainsKey(prop.Name))
                                {
                                    var value = GetValue(item.Value[prop.Name], prop.PropertyType);
                                    prop.SetValue(obj, value, null);
                                }
                            }
                            catch (Exception ex)
                            {
                                DebugUtils.Error("XmlData", "LoadXML error {0}: {1} {2}", ex.Message, item.Value[prop.Name], prop.PropertyType);
                            }
                        }
                    }
                    list.Add((T)obj);
                }
            }
            catch (Exception ex)
            {
                DebugUtils.Error("XmlData", "Error {} : {}", ex.Message, text);
            }
            return(list);
        }
Exemplo n.º 13
0
        private static bool DecodeLuaValue(byte[] inputString, ref int index, out object value)
        {
            var firstChar = inputString[index];

            if (firstChar == 's')
            {
                //value is string
                var szLen  = Encoding.UTF8.GetString(inputString, index + 1, 3);
                var length = Int32.Parse(szLen);
                index += 4;
                if (length > 0)
                {
                    value  = Encoding.UTF8.GetString(inputString, index, length);
                    index += length;
                    return(true);
                }
                else
                {
                    value = "";
                    return(true);
                }
            }
            //如果第一个字符为花括号,表示接下来的内容为列表或实体类型
            else if (firstChar == '{')
            {
                return(DecodeLuaTable(inputString, ref index, out value));
            }
            else
            {
                //value is number
                var i = index;
                while (++index < inputString.Length)
                {
                    if (inputString[index] == ',' || inputString[index] == '}')
                    {
                        if (index > i)
                        {
                            value = Encoding.UTF8.GetString(inputString, i, index - i);
                            return(true);
                        }
                    }
                }
                DebugUtils.Error("Decode Lua Table Value Error", index + " " + inputString);
                value = null;
                return(false);
            }
        }
Exemplo n.º 14
0
 public static TValue Get <TKey, TValue>(this IDictionary <TKey, TValue> dictionary, TKey key)
 {
     if (dictionary == null)
     {
         DebugUtils.Error("DictionaryExtend", "Dictionary is null.");
         return(default(TValue) == null?GetDefaultValue <TValue>() : default(TValue));
     }
     else if (!dictionary.ContainsKey(key))
     {
         DebugUtils.Error("DictionaryExtend", String.Format("Key '{0}' is not exist.", key));
         return(default(TValue) == null?GetDefaultValue <TValue>() : default(TValue));
     }
     else
     {
         return(dictionary[key]);
     }
 }
Exemplo n.º 15
0
 public static T Get <T>(this List <T> list, int index)
 {
     if (list == null)
     {
         DebugUtils.Error("DictionaryExtend", "List is null.");
         return(default(T) == null?GetDefaultValue <T>() : default(T));
     }
     else if (list.Count <= index)
     {
         DebugUtils.Error("DictionaryExtend", String.Format("Index '{0}' is out of range.", index));
         return(default(T) == null?GetDefaultValue <T>() : default(T));
     }
     else
     {
         return(list[index]);
     }
 }
Exemplo n.º 16
0
 public static T Get <T>(this T[] array, int index)
 {
     if (array == null)
     {
         DebugUtils.Error("DictionaryExtend", "Array is null.");
         return(default(T) == null?GetDefaultValue <T>() : default(T));
     }
     else if (array.Length <= index)
     {
         DebugUtils.Error("DictionaryExtend", String.Format("Index '{0}' is out of range.", index));
         return(default(T) == null?GetDefaultValue <T>() : default(T));
     }
     else
     {
         return(array[index]);
     }
 }
Exemplo n.º 17
0
        public static bool DecodeKey(byte[] inputString, ref int index, out string key, out bool isString)
        {
            if (inputString[index] == 's')
            {
                //key is string
                var szLen  = Encoding.UTF8.GetString(inputString, index + 1, 3);
                var length = Int32.Parse(szLen);
                if (length > 0)
                {
                    index   += 4;
                    key      = Encoding.UTF8.GetString(inputString, index, length);
                    isString = true;
                    index   += length;
                    return(true);
                }
                key      = "";
                isString = true;
                DebugUtils.Error("Decode Lua Table Key Error: ", index + " " + inputString);
                return(false);
            }
            else
            {
                //key is number
                int offset = 0;
                while (index + offset < inputString.Length && inputString[index + offset] != '=')
                {
                    offset++;
                }

                if (offset > 0)
                {
                    key      = Encoding.UTF8.GetString(inputString, index, offset);
                    index    = index + offset;
                    isString = false;
                    return(true);
                }
                else
                {
                    key      = "-1";
                    isString = false;
                    DebugUtils.Error("Decode Lua Table Key Error ", index + " " + inputString);
                    return(false);
                }
            }
        }
Exemplo n.º 18
0
        private static bool DecodeLuaTable(byte[] inputString, ref int index, out object result)
        {
            var luaTable = new LuaTable();

            result = luaTable;
            if (!WaitChar(inputString, '{', ref index))
            {
                return(false);
            }
            try
            {
                //如果下一个字符为右大括号表示为空Lua table
                if (WaitChar(inputString, '}', ref index))
                {
                    return(true);
                }
                while (index < inputString.Length)
                {
                    string key;
                    bool   isString;
                    object value;
                    //匹配键
                    DecodeKey(inputString, ref index, out key, out isString);
                    //匹配键值对分隔符
                    WaitChar(inputString, '=', ref index);
                    //转换实体
                    var flag = DecodeLuaValue(inputString, ref index, out value);
                    if (flag)
                    {
                        luaTable.Add(key, isString, value);
                    }
                    if (!WaitChar(inputString, ',', ref index))
                    {
                        break;
                    }
                }
                WaitChar(inputString, '}', ref index);
                return(true);
            }
            catch (Exception e)
            {
                DebugUtils.Error("Parse LuaTable Error ", inputString + e.ToString());
                return(false);
            }
        }
Exemplo n.º 19
0
        /// <summary>
        /// 从指定的 XML 文档加载 map 数据。
        /// </summary>
        /// <param name="xml">XML 文档</param>
        /// <returns>map 数据</returns>
        public static Dictionary <string, Dictionary <string, string> > LoadMap(SecurityElement xml)
        {
            var result = new Dictionary <string, Dictionary <string, string> >();

            if (xml != null)
            {
                foreach (SecurityElement subMap in xml.Children)
                {
                    string key = (subMap.Children[0] as SecurityElement).Text.Trim();
                    if (result.ContainsKey(key))
                    {
                        Debug.LogError(string.Format("Key {0} already exist, in {1}.", key, xml.ToString()));
                        continue;
                    }

                    var children = new Dictionary <string, string>();
                    result.Add(key, children);
                    for (int i = 1; i < subMap.Children.Count; i++)
                    {
                        var node = subMap.Children[i] as SecurityElement;
                        if (node != null && !children.ContainsKey(node.Tag))
                        {
                            if (string.IsNullOrEmpty(node.Text))
                            {
                                children.Add(node.Tag, "IsNullOrEmpty");
                            }
                            else
                            {
                                children.Add(node.Tag, node.Text.Trim());
                            }
                        }
                        else
                        {
                            if (node != null)
                            {
                                DebugUtils.Error("LoadMap", string.Format("Key {0} already exist, index {1} of {2}.", node.Tag, i, node.ToString()));
                            }
                        }
                    }
                }
            }
            return(result);
        }
Exemplo n.º 20
0
        /// <summary>
        /// 将字典字符串转换为键类型为字符串,值类型为整型的字典对象。
        /// </summary>
        /// <param name="strMap">字典字符串</param>
        /// <param name="keyValueSpriter">键值分隔符</param>
        /// <param name="mapSpriter">字典项分隔符</param>
        /// <returns>字典对象</returns>
        public static Dictionary <string, int> ParseMapStringInt(string strMap, char keyValueSpriter = KEY_VALUE_SPRITER, char mapSpriter = MAP_SPRITER)
        {
            Dictionary <string, int> result = new Dictionary <string, int>();
            var strResult = ParseMap(strMap, keyValueSpriter, mapSpriter);

            foreach (var item in strResult)
            {
                int value;
                if (int.TryParse(item.Value, out value))
                {
                    result.Add(item.Key, value);
                }
                else
                {
                    DebugUtils.Error("XmlData", string.Format("Parse failure: {0}", item.Value));
                }
            }
            return(result);
        }
Exemplo n.º 21
0
        public bool LoadData(string flag)
        {
            bool ret = false;

            try
            {
                ret = LoadData(flag, mGameDataTypes);
                Resources.UnloadUnusedAssets();
                GC.Collect();
                if (mFinished != null)
                {
                    mFinished();
                }
            }
            catch (Exception ex)
            {
                DebugUtils.Error("XmlData", ex.Message);
            }
            return(ret);
        }
Exemplo n.º 22
0
 /// <summary>
 /// 从指定的 URL 加载 map 数据。
 /// </summary>
 /// <param name="fileName">文件的 URL,该文件包含要加载的 XML 文档</param>
 /// <param name="map">map 数据</param>
 /// <returns>是否加载成功</returns>
 public static bool LoadMap(string fileName, out Dictionary <string, Dictionary <string, string> > map)
 {
     try
     {
         var xml = Load(fileName);
         map = null;
         if (xml != null)
         {
             map = LoadMap(xml);
             return(true);
         }
         return(false);
     }
     catch (Exception ex)
     {
         DebugUtils.Error("LoadMap", ex.Message);
         map = null;
         return(false);
     }
 }
Exemplo n.º 23
0
 /// <summary>
 /// 解析 Lua table 的键。
 /// </summary>
 /// <param name="inputString">Lua table字符串</param>
 /// <param name="index">字符串偏移量</param>
 /// <param name="result">键</param>
 /// <returns>返回 true/false 表示是否成功</returns>
 public static bool DecodeKey(string inputString, ref int index, out string key, out bool isString)
 {
     if (inputString[index] == 's')
     {
         //key is string
         var szLen = inputString.Substring(index + 1, 3);
         var lenth = int.Parse(szLen);
         if (lenth > 0)
         {
             index   += 4;
             key      = inputString.Substring(index, lenth);
             isString = true;
             index   += lenth;
             return(true);
         }
         key      = "";
         isString = true;
         DebugUtils.Error("Decode LuaTable Key Error", index + " " + inputString);
         return(false);
     }
     else
     {
         //key is number
         var szLen = inputString.IndexOf('=', index);
         if (szLen > -1)
         {
             var lenth = szLen - index;
             key      = inputString.Substring(index, lenth);
             index    = szLen;
             isString = false;
             return(true);
         }
         else
         {
             key      = "-1";
             isString = false;
             DebugUtils.Error("Decode LuaTable Key Error", index + " " + inputString);
             return(false);
         }
     }
 }
Exemplo n.º 24
0
        /// <summary>
        /// 生成文件的md5
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public static String BuildFileMd5(String filename)
        {
            String filemd5 = null;

            try
            {
                using (var fileStream = File.OpenRead(filename))
                {
                    var md5          = MD5.Create();
                    var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
                    //fileStream.Close();//流数据比较大,手动卸载
                    //fileStream.Dispose();
                    //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A”
                    filemd5 = FormatMD5(fileMD5Bytes);
                }
            }
            catch (System.Exception ex)
            {
                DebugUtils.Error("", "", ex.Message);
            }
            return(filemd5);
        }
Exemplo n.º 25
0
        /// <summary>
        /// 将指定格式(1.0, 2, 3.4) 转换为 Vector3
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="result"></param>
        /// <returns>返回 true/false 表示是否成功</returns>
        public static bool ParseQuaternion(string inputString, out Quaternion result)
        {
            string trimString = inputString.Trim();

            trimString = trimString.Replace("(", "");
            trimString = trimString.Replace(")", "");
            result     = new Quaternion();
            try
            {
                string[] detail = trimString.Split(',');
                for (int i = 0; i < detail.Length; ++i)
                {
                    result[i] = float.Parse(detail[i].Trim());
                }
                return(true);
            }
            catch (Exception e)
            {
                DebugUtils.Error("XmlData", "Parse Quaternion error: " + trimString + e.Message);
                return(false);
            }
        }
Exemplo n.º 26
0
 public static string LoadText(string fileName)
 {
     try
     {
         if (File.Exists(fileName))
         {
             using (StreamReader sr = File.OpenText(fileName))
             {
                 return(sr.ReadToEnd());
             }
         }
         else
         {
             return(string.Empty);
         }
     }
     catch (Exception ex)
     {
         DebugUtils.Error("LoadText", ex);
         return("");
     }
 }
Exemplo n.º 27
0
        /// <summary>
        /// 将字典字符串转换为键类型与值类型都为字符串的字典对象。
        /// </summary>
        /// <param name="strMap">字典字符串</param>
        /// <param name="keyValueSpriter">键值分隔符</param>
        /// <param name="mapSpriter">字典项分隔符</param>
        /// <returns>字典对象</returns>
        public static Dictionary <string, string> ParseMap(string strMap, char keyValueSpriter = KEY_VALUE_SPRITER, char mapSpriter = MAP_SPRITER)
        {
            Dictionary <string, string> result = new Dictionary <string, string>();

            if (string.IsNullOrEmpty(strMap))
            {
                return(result);
            }

            var map = strMap.Split(mapSpriter);//根据字典项分隔符分割字符串,获取键值对字符串

            for (int i = 0; i < map.Length; i++)
            {
                if (string.IsNullOrEmpty(map[i]))
                {
                    continue;
                }

                var keyValuePair = map[i].Split(keyValueSpriter);//根据键值分隔符分割键值对字符串
                if (keyValuePair.Length == 2)
                {
                    if (!result.ContainsKey(keyValuePair[0]))
                    {
                        result.Add(keyValuePair[0], keyValuePair[1]);
                    }
                    else
                    {
                        DebugUtils.Error("XmlData", string.Format("Key {0} already exist, index {1} of {2}.", keyValuePair[0], i, strMap));
                    }
                }
                else
                {
                    DebugUtils.Error("XmlData", string.Format("KeyValuePair are not match: {0}, index {1} of {2}.", map[i], i, strMap));
                }
            }
            return(result);
        }
Exemplo n.º 28
0
 /// <summary>
 /// 转换列表类型的对象到LuaTable。
 /// </summary>
 /// <param name="target"></param>
 /// <param name="result"></param>
 /// <returns></returns>
 public static bool PackLuaTable(IList target, out LuaTable result)
 {
     Type[] types = target.GetType().GetGenericArguments();
     result = new LuaTable();
     try
     {
         for (int i = 0; i < target.Count; i++)
         {
             if (IsBaseType(types[0]))
             {
                 if (types[0] == typeof(bool))
                 {
                     result.Add(i + 1, (bool)target[i] ? 1 : 0);
                 }
                 else
                 {
                     result.Add(i + 1, target[i]);
                 }
             }
             else
             {
                 LuaTable value;
                 var      flag = PackLuaTable(target[i], out value);
                 if (flag)
                 {
                     result.Add(i + 1, value);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         DebugUtils.Error("PackLuaTable", "List Error: " + ex.Message);
     }
     return(true);
 }