private static bool BuildConfigConvertClass(System.Type t) { if (t == null) { return(false); } // 先找标记了的类 bool ret = false; object[] attrs = t.GetCustomAttributes(false); if (attrs != null && attrs.Length > 0) { for (int i = 0; i < attrs.Length; ++i) { ConfigConvertAttribute attr = attrs[i] as ConfigConvertAttribute; if (attr == null || string.IsNullOrEmpty(attr.ConfigName)) { continue; } ConvertClassInfo info = new ConvertClassInfo(t, attr); m_ConvertClassMap[attr.ConfigName] = info; m_TargetNameMap[info.convertName] = info; ret = true; break; } } return(ret); }
// LitJson转换成自定义格式 public static void ConvertToBinaryFile(string fileName, string configName, string json) { if (string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(json)) { return; } ConvertClassInfo info = GetConvertClass(configName); if (info == null || info.DictionaryType == null) { return; } // System.Type dictType = info.DictionaryType; System.Collections.IDictionary values = LitJsonHelper.ToTypeObject(json, info.DictionaryType) as System.Collections.IDictionary; if (values == null) { return; } string newFileName = string.Format("{0}/{1}.bytes", Path.GetDirectoryName(fileName), info.convertName); FileStream stream = new FileStream(newFileName, FileMode.Create, FileAccess.Write); try { try { ConfigWrap.ToStream(stream, values); } catch (Exception e) { UnityEngine.Debug.LogErrorFormat("【转换异常】{0}=>{1}", fileName, e.ToString()); } } finally { stream.Flush(); stream.Close(); stream.Dispose(); } }
// 注册简易配置,例如:Dictionary<string, string > public static void RegisterSimpleTypeConfig(string configName, System.Type dictionaryType, string convertName = "") { if (string.IsNullOrEmpty(configName)) { return; } ConfigConvertAttribute attr = new ConfigConvertAttribute(configName, dictionaryType, convertName); ConvertClassInfo ret = new ConvertClassInfo(typeof(string), attr); m_ConvertClassMap[configName] = ret; m_TargetNameMap[ret.convertName] = ret; }