├── .gitignore ├── Assets ├── DataManager.meta └── DataManager │ ├── Custom.meta │ ├── Custom │ ├── TableConverterList.cs │ ├── TableConverterList.cs.meta │ ├── TableParserCsv.cs │ ├── TableParserCsv.cs.meta │ ├── TableParserSlk.cs │ ├── TableParserSlk.cs.meta │ ├── TableReaderResource.cs │ ├── TableReaderResource.cs.meta │ ├── TableWriterCsv.cs │ └── TableWriterCsv.cs.meta │ ├── Editor.meta │ ├── Editor │ ├── TableEditorTools.cs │ └── TableEditorTools.cs.meta │ ├── Resources.meta │ ├── Resources │ ├── TableConfig.csv │ └── TableConfig.csv.meta │ ├── Scripts.meta │ └── Scripts │ ├── Data.cs │ ├── Data.cs.meta │ ├── DataManager.cs │ ├── DataManager.cs.meta │ ├── DataManagerTools.cs │ ├── DataManagerTools.cs.meta │ ├── IContentInfo.cs │ ├── IContentInfo.cs.meta │ ├── Table.cs │ ├── Table.cs.meta │ ├── TableConverter.cs │ ├── TableConverter.cs.meta │ ├── TableParser.cs │ ├── TableParser.cs.meta │ ├── TableReader.cs │ ├── TableReader.cs.meta │ ├── TableWriter.cs │ ├── TableWriter.cs.meta │ ├── Utility.meta │ └── Utility │ ├── EDataManager.cs │ ├── EDataManager.cs.meta │ ├── LoadResourceAsync.cs │ ├── LoadResourceAsync.cs.meta │ ├── TableTools.cs │ └── TableTools.cs.meta ├── LICENSE ├── ProjectSettings ├── AudioManager.asset ├── ClusterInputManager.asset ├── DynamicsManager.asset ├── EditorBuildSettings.asset ├── EditorSettings.asset ├── GraphicsSettings.asset ├── InputManager.asset ├── NavMeshAreas.asset ├── NetworkManager.asset ├── Physics2DSettings.asset ├── ProjectSettings.asset ├── ProjectVersion.txt ├── QualitySettings.asset ├── TagManager.asset ├── TimeManager.asset ├── UnityAdsSettings.asset └── UnityConnectSettings.asset └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /[Ll]ibrary/ 2 | /[Tt]emp/ 3 | /[Oo]bj/ 4 | /[Bb]uild/ 5 | /[Bb]uilds/ 6 | /Assets/AssetStoreTools* 7 | /Assets/_Test* 8 | 9 | # Autogenerated VS/MD solution and project files 10 | ExportedObj/ 11 | *.csproj 12 | *.unityproj 13 | *.sln 14 | *.suo 15 | *.tmp 16 | *.user 17 | *.userprefs 18 | *.pidb 19 | *.booproj 20 | *.svd 21 | 22 | 23 | # Unity3D generated meta files 24 | *.pidb.meta 25 | 26 | # Unity3D Generated File On Crash Reports 27 | sysinfo.txt 28 | 29 | # Builds 30 | *.apk 31 | *.unitypackage 32 | -------------------------------------------------------------------------------- /Assets/DataManager.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: cba097ec6f9cc364cabfb4bb56ea0e8d 3 | folderAsset: yes 4 | timeCreated: 1469022554 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/DataManager/Custom.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: ded116e35ad283345bd289b456b543ca 3 | folderAsset: yes 4 | DefaultImporter: 5 | userData: 6 | -------------------------------------------------------------------------------- /Assets/DataManager/Custom/TableConverterList.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Reflection; 5 | using System.Runtime.Serialization; 6 | using DataManagement; 7 | 8 | public class TableConverterList : TableConverter 9 | { 10 | private Dictionary _name2ConstructorInfo = new Dictionary(); // Reflection Cache 11 | private Dictionary _name2Type = new Dictionary(); // Reflection Cache 12 | 13 | public override Table Convert(string name, object input) 14 | { 15 | return _Convert( name, (List>)input ); 16 | } 17 | 18 | private Table _Convert( string name, List> parsedData ) 19 | { 20 | if( null == parsedData || parsedData.Count == 0 ) 21 | { 22 | TableTools.Log( TableTools.LogLevel.ERROR, "No Table or Empty: " + name ); 23 | return null; 24 | } 25 | 26 | List columnNames = parsedData[0]; 27 | List columnTypes = parsedData[1]; 28 | 29 | #region Remove NonUse Column 30 | int columnNamesIndex = columnNames.FindIndex( delegate( string str ) { return string.IsNullOrEmpty( str ); } ); 31 | if( -1 != columnNamesIndex ) columnNames.RemoveRange( columnNamesIndex, columnNames.Count - columnNamesIndex ); 32 | 33 | int columnTypesIndex = columnTypes.FindIndex( delegate( string str ) { return string.IsNullOrEmpty( str ); } ); 34 | if( -1 != columnTypesIndex ) columnTypes.RemoveRange( columnTypesIndex, columnTypes.Count - columnTypesIndex ); 35 | 36 | if( columnNames.Count < columnTypes.Count ) columnTypes.RemoveRange( columnNames.Count, columnTypes.Count - columnNames.Count ); 37 | else if( columnNames.Count > columnTypes.Count ) columnNames.RemoveRange( columnTypes.Count, columnNames.Count - columnTypes.Count ); 38 | #endregion 39 | 40 | #region Read Key and Column Values 41 | Dictionary key2Data = new Dictionary(); 42 | for( int i = 2; i < parsedData.Count; ++i ) 43 | { 44 | List values = new List(); 45 | List hasValues = new List(); 46 | 47 | #region Index 0 is Key 48 | // key is null or empty 49 | if( string.IsNullOrEmpty( parsedData[i][0] ) ) 50 | { 51 | // Debug.LogWarning( "there is null or empty key at row: " + i + " in table: " + name ); 52 | break; 53 | } 54 | #endregion 55 | 56 | for( int j = 0; j < parsedData[i].Count; ++j ) 57 | { 58 | if( columnTypes.Count > j ) 59 | { 60 | object obj = null; 61 | _StringToObject( name, ( i + 1 ), columnNames[j], columnTypes[j], parsedData[i][j], ref obj ); 62 | values.Add( obj ); 63 | hasValues.Add( string.IsNullOrEmpty( parsedData[i][j] ) ? false : true ); 64 | } 65 | } 66 | 67 | Data data = new Data(); 68 | data.Initial( columnNames, columnTypes, values[0], values, hasValues ); 69 | 70 | // this is duplicate key 71 | if( key2Data.ContainsKey( values[0] ) ) 72 | { 73 | TableTools.Log( TableTools.LogLevel.ERROR, "there is duplicate key: " + values[0] + " at row: " + i + " in table: " + name ); 74 | continue; 75 | } 76 | 77 | key2Data[values[0]] = data; 78 | } 79 | #endregion 80 | 81 | Table table = new Table(); 82 | table.Initial( name, columnNames, columnTypes, key2Data ); 83 | 84 | return table; 85 | } 86 | 87 | private string _StringToObject( string name, int row, string columnName, string typeStr, string valueStr, ref object value ) 88 | { 89 | string realTypeStr = string.Empty; 90 | if( 0 <= typeStr.IndexOf( EDataManager.BRACKET_LEFT_STRING ) ) 91 | { 92 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.LIST_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 93 | typeStr = typeStr.Substring( 1 ); 94 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 95 | 96 | List values = _StringToList( valueStr ); 97 | 98 | System.Collections.IList list = null; 99 | foreach( string v in values ) 100 | { 101 | string valueType = _StringToObject( name, row, columnName, typeStr, v, ref value ); 102 | 103 | if( null == list ) 104 | { 105 | realTypeStr += valueType; 106 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 107 | 108 | ConstructorInfo constructor = null; 109 | if( !_name2ConstructorInfo.TryGetValue( realTypeStr, out constructor ) ) 110 | { 111 | Type listType = Type.GetType( realTypeStr ); 112 | constructor = listType.GetConstructor( new Type[]{} ); 113 | _name2ConstructorInfo[realTypeStr] = constructor; 114 | } 115 | object o = constructor.Invoke( null ); 116 | list = (System.Collections.IList)o; 117 | 118 | if( string.IsNullOrEmpty( valueStr ) ) break; 119 | } 120 | 121 | list.Add( value ); 122 | } 123 | 124 | value = list; 125 | } 126 | else if( 0 <= typeStr.IndexOf( EDataManager.LESS_THAN_STRING ) ) 127 | { 128 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.DICT_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 129 | typeStr = typeStr.Substring( 1 ); 130 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 131 | 132 | Dictionary values = null; 133 | try 134 | { 135 | values = _StringToDict( valueStr ); 136 | } 137 | catch( Exception e ) 138 | { 139 | TableTools.Log( TableTools.LogLevel.ERROR, "table=" + name + ", row=" + row + ", column=" + columnName + ", value=" + valueStr + ", exception=" + e.ToString() ); 140 | } 141 | 142 | System.Collections.IDictionary dict = null; 143 | foreach( KeyValuePair pair in values ) 144 | { 145 | int index = typeStr.IndexOf( EDataManager.COMMA_CHAR ); 146 | 147 | object key = null; 148 | string keyType = _StringToObject( name, row, columnName, typeStr.Remove( index ), pair.Key, ref key ); 149 | 150 | object val = null; 151 | string valueType = _StringToObject( name, row, columnName, typeStr.Substring( index + 1 ), pair.Value, ref val ); 152 | 153 | if( null == dict ) 154 | { 155 | realTypeStr += ( keyType + EDataManager.COMMA_STRING + valueType ); 156 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 157 | 158 | ConstructorInfo constructor = null; 159 | if( !_name2ConstructorInfo.TryGetValue( realTypeStr, out constructor ) ) 160 | { 161 | Type dictType = Type.GetType( realTypeStr ); 162 | constructor = dictType.GetConstructor( new Type[]{} ); 163 | _name2ConstructorInfo[realTypeStr] = constructor; 164 | } 165 | object o = constructor.Invoke( null ); 166 | dict = (System.Collections.IDictionary)o; 167 | 168 | if( string.IsNullOrEmpty( valueStr ) ) break; 169 | } 170 | 171 | dict.Add( key, val ); 172 | } 173 | 174 | value = dict; 175 | } 176 | else 177 | { 178 | realTypeStr += ( EDataManager.SYSTEM_DOT_STRING + typeStr ); 179 | 180 | Type valueType; 181 | if( !_name2Type.TryGetValue( realTypeStr, out valueType ) ) 182 | { 183 | valueType = Type.GetType( realTypeStr ); 184 | _name2Type[realTypeStr] = valueType; 185 | } 186 | 187 | if( string.IsNullOrEmpty( valueStr ) && null != valueType && typeof( System.String ) != valueType ) 188 | { 189 | try 190 | { 191 | // FormatterServices可以實體化物件並且不呼叫建構式, 而由於內建型別本身並無建構式, 採用此方式來取代Activator.CreateInstance( valueType )提升效能 192 | // https://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.formatterservices.getuninitializedobject(v=vs.110).aspx 193 | value = FormatterServices.GetUninitializedObject( valueType ); 194 | } 195 | catch( Exception ) 196 | { 197 | TableTools.Log( TableTools.LogLevel.ERROR, "there is exception at row: " + row + " and column name: "+ columnName + " in table: " + name ); 198 | } 199 | } 200 | else 201 | { 202 | try 203 | { 204 | value = System.Convert.ChangeType( valueStr, valueType ); 205 | } 206 | catch( FormatException ) 207 | { 208 | TableTools.Log( TableTools.LogLevel.ERROR, "there is error value: " + valueStr + " at row: " + row + " and column name: " + columnName + " in table: " + name ); 209 | } 210 | catch( ArgumentNullException ) 211 | { 212 | TableTools.Log( TableTools.LogLevel.ERROR, "there is error type: " + typeStr + " at column name: " + columnName + " in table: " + name ); 213 | } 214 | catch( Exception ) 215 | { 216 | TableTools.Log( TableTools.LogLevel.ERROR, "there is exception at row: " + row + " and column name: "+ columnName + " in table: " + name ); 217 | } 218 | } 219 | } 220 | 221 | return realTypeStr; 222 | } 223 | 224 | public T StringToObject( string valueStr ) 225 | { 226 | string typeStr = DataManagerTools.TypeToString( typeof( T ) ); 227 | 228 | object value = null; 229 | _StringToObject( typeStr, valueStr, ref value ); 230 | 231 | return (T)System.Convert.ChangeType( value, typeof( T ) ); 232 | } 233 | 234 | private string _StringToObject( string typeStr, string valueStr, ref object value ) 235 | { 236 | string realTypeStr = string.Empty; 237 | if( 0 <= typeStr.IndexOf( EDataManager.BRACKET_LEFT_STRING ) ) 238 | { 239 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.LIST_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 240 | typeStr = typeStr.Substring( 1 ); 241 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 242 | 243 | List values = _StringToList( valueStr ); 244 | 245 | System.Collections.IList list = null; 246 | foreach( string v in values ) 247 | { 248 | string valueType = _StringToObject( typeStr, v, ref value ); 249 | 250 | if( null == list ) 251 | { 252 | realTypeStr += valueType; 253 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 254 | 255 | ConstructorInfo constructor = null; 256 | if( !_name2ConstructorInfo.TryGetValue( realTypeStr, out constructor ) ) 257 | { 258 | Type listType = Type.GetType( realTypeStr ); 259 | constructor = listType.GetConstructor( new Type[]{} ); 260 | _name2ConstructorInfo[realTypeStr] = constructor; 261 | } 262 | object o = constructor.Invoke( null ); 263 | list = (System.Collections.IList)o; 264 | 265 | if( string.IsNullOrEmpty( valueStr ) ) break; 266 | } 267 | 268 | list.Add( value ); 269 | } 270 | 271 | value = list; 272 | } 273 | else if( 0 <= typeStr.IndexOf( EDataManager.LESS_THAN_STRING ) ) 274 | { 275 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.DICT_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 276 | typeStr = typeStr.Substring( 1 ); 277 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 278 | 279 | Dictionary values = _StringToDict( valueStr ); 280 | 281 | System.Collections.IDictionary dict = null; 282 | foreach( KeyValuePair pair in values ) 283 | { 284 | int index = typeStr.IndexOf( EDataManager.COMMA_CHAR ); 285 | 286 | object key = null; 287 | string keyType = _StringToObject( typeStr.Remove( index ), pair.Key, ref key ); 288 | 289 | object val = null; 290 | string valueType = _StringToObject( typeStr.Substring( index + 1 ), pair.Value, ref val ); 291 | 292 | if( null == dict ) 293 | { 294 | realTypeStr += ( keyType + EDataManager.COMMA_STRING + valueType ); 295 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 296 | 297 | ConstructorInfo constructor = null; 298 | if( !_name2ConstructorInfo.TryGetValue( realTypeStr, out constructor ) ) 299 | { 300 | Type dictType = Type.GetType( realTypeStr ); 301 | constructor = dictType.GetConstructor( new Type[]{} ); 302 | _name2ConstructorInfo[realTypeStr] = constructor; 303 | } 304 | object o = constructor.Invoke( null ); 305 | dict = (System.Collections.IDictionary)o; 306 | 307 | if( string.IsNullOrEmpty( valueStr ) ) break; 308 | } 309 | 310 | dict.Add( key, val ); 311 | } 312 | 313 | value = dict; 314 | } 315 | else 316 | { 317 | realTypeStr += ( EDataManager.SYSTEM_DOT_STRING + typeStr ); 318 | 319 | Type valueType; 320 | if( !_name2Type.TryGetValue( realTypeStr, out valueType ) ) 321 | { 322 | valueType = Type.GetType( realTypeStr ); 323 | _name2Type[realTypeStr] = valueType; 324 | } 325 | 326 | if( string.IsNullOrEmpty( valueStr ) && null != valueType && typeof( System.String ) != valueType ) 327 | { 328 | try 329 | { 330 | // FormatterServices可以實體化物件並且不呼叫建構式, 而由於內建型別本身並無建構式, 採用此方式來取代Activator.CreateInstance( valueType )提升效能 331 | // https://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.formatterservices.getuninitializedobject(v=vs.110).aspx 332 | value = FormatterServices.GetUninitializedObject( valueType ); 333 | } 334 | catch( Exception ) 335 | { 336 | TableTools.Log( TableTools.LogLevel.ERROR, "there is exception, value: " + valueStr + ", type: " + valueType ); 337 | } 338 | } 339 | else 340 | { 341 | try 342 | { 343 | value = System.Convert.ChangeType( valueStr, valueType ); 344 | } 345 | catch( FormatException ) 346 | { 347 | TableTools.Log( TableTools.LogLevel.ERROR, "there is error value: " + valueStr ); 348 | } 349 | catch( ArgumentNullException ) 350 | { 351 | TableTools.Log( TableTools.LogLevel.ERROR, "there is error type: " + typeStr ); 352 | } 353 | catch( Exception ) 354 | { 355 | TableTools.Log( TableTools.LogLevel.ERROR, "there is exception, value: " + valueStr + ", type: " + valueType ); 356 | } 357 | } 358 | } 359 | 360 | return realTypeStr; 361 | } 362 | 363 | private List _StringToList( string valueStr ) 364 | { 365 | List values = new List(); 366 | 367 | int count = 0; 368 | bool hasSign = false; 369 | string value = string.Empty; 370 | foreach( char c in valueStr ) 371 | { 372 | if( hasSign && ( c.Equals( EDataManager.BRACKET_LEFT_CHAR ) || c.Equals( EDataManager.LESS_THAN_CHAR ) ) ) ++count; 373 | else if( hasSign && 0 < count && ( c.Equals( EDataManager.BRACKET_RIGHT_CHAR ) || c.Equals( EDataManager.GREAT_THAN_CHAR ) ) ) --count; 374 | else if( !hasSign && c.Equals( EDataManager.COMMA_CHAR ) ) { values.Add( value ); value = string.Empty; continue; } 375 | else if( c.Equals( EDataManager.BRACKET_LEFT_CHAR ) || c.Equals( EDataManager.LESS_THAN_CHAR ) ) { hasSign = true; continue; } 376 | else if( c.Equals( EDataManager.BRACKET_RIGHT_CHAR ) || c.Equals( EDataManager.GREAT_THAN_CHAR ) ) { hasSign = false; continue; } 377 | 378 | value += c.ToString(); 379 | } 380 | values.Add( value ); 381 | 382 | return values; 383 | } 384 | 385 | private Dictionary _StringToDict( string valueStr ) 386 | { 387 | Dictionary values = new Dictionary(); 388 | 389 | int count = 0; 390 | bool hasSign = false; 391 | string key = string.Empty; 392 | string value = string.Empty; 393 | 394 | try 395 | { 396 | foreach( char c in valueStr ) 397 | { 398 | if( hasSign && ( c.Equals( EDataManager.BRACKET_LEFT_CHAR ) || c.Equals( EDataManager.LESS_THAN_CHAR ) ) ) ++count; 399 | else if( hasSign && 0 < count && ( c.Equals( EDataManager.BRACKET_RIGHT_CHAR ) || c.Equals( EDataManager.GREAT_THAN_CHAR ) ) ) --count; 400 | else if( !hasSign && c.Equals( EDataManager.COMMA_CHAR ) ) { values.Add( key, value ); key = string.Empty; value = string.Empty; continue; } 401 | else if( !hasSign && c.Equals( EDataManager.COLON_CHAR ) ) { key = value; value = string.Empty; continue; } 402 | else if( c.Equals( EDataManager.BRACKET_LEFT_CHAR ) || c.Equals( EDataManager.LESS_THAN_CHAR ) ) { hasSign = true; continue; } 403 | else if( c.Equals( EDataManager.BRACKET_RIGHT_CHAR ) || c.Equals( EDataManager.GREAT_THAN_CHAR ) ) { hasSign = false; continue; } 404 | 405 | value += c.ToString(); 406 | } 407 | values.Add( key, value ); 408 | } 409 | catch( Exception e ) 410 | { 411 | throw( e ); 412 | } 413 | 414 | return values; 415 | } 416 | 417 | public Type StringToType( string typeStr ) 418 | { 419 | string realTypeStr = _StringToType( typeStr ); 420 | 421 | return Type.GetType( realTypeStr ); 422 | } 423 | 424 | private string _StringToType( string typeStr ) 425 | { 426 | string realTypeStr = string.Empty; 427 | if( 0 <= typeStr.IndexOf( EDataManager.BRACKET_LEFT_STRING ) ) 428 | { 429 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.LIST_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 430 | typeStr = typeStr.Substring( 1 ); 431 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 432 | realTypeStr += _StringToType( typeStr ); 433 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 434 | } 435 | else if( 0 <= typeStr.IndexOf( EDataManager.LESS_THAN_STRING ) ) 436 | { 437 | realTypeStr += ( EDataManager.NAMESPACE_STRING + EDataManager.DICT_TYPE_STRING + EDataManager.BRACKET_LEFT_STRING ); 438 | typeStr = typeStr.Substring( 1 ); 439 | typeStr = typeStr.Remove( typeStr.Length - 1 ); 440 | 441 | int index = typeStr.IndexOf( EDataManager.COMMA_CHAR ); 442 | string keyType = _StringToType( typeStr.Remove( index ) ); 443 | string valueType = _StringToType( typeStr.Substring( index + 1 ) ); 444 | 445 | realTypeStr += ( keyType + EDataManager.COMMA_STRING + valueType ); 446 | realTypeStr += EDataManager.BRACKET_RIGHT_STRING; 447 | } 448 | else 449 | { 450 | realTypeStr += ( EDataManager.SYSTEM_DOT_STRING + typeStr ); 451 | } 452 | 453 | return realTypeStr; 454 | } 455 | 456 | public List> Convert( Table table ) 457 | { 458 | return _Convert( table ); 459 | } 460 | 461 | private List> _Convert( Table table ) 462 | { 463 | List> parsedData = new List>(); 464 | parsedData.Add( table.Fields.ToList() ); 465 | parsedData.Add( table.Types.ToList() ); 466 | 467 | foreach( object key in table.Keys ) 468 | { 469 | List datas = new List(); 470 | for( int i = 0,imax=table.Fields.Count;i Convert( Data data ) 488 | { 489 | return _Convert( data ); 490 | } 491 | 492 | private List _Convert( Data data ) 493 | { 494 | List line = new List(); 495 | for( int i = 0, imax=data.Fields.Count;i>(); 23 | } 24 | 25 | private List> _Parse( TextReader readFile ) 26 | { 27 | List> result = new List>(); 28 | List lines; 29 | string line = string.Empty; 30 | 31 | while( null != ( line = readFile.ReadLine() ) ) 32 | { 33 | lines = ParseLine( line ); 34 | result.Add( lines ); 35 | } 36 | 37 | return result; 38 | } 39 | 40 | public static List ParseLine( string line ) 41 | { 42 | List result = new List(); 43 | string value = string.Empty; 44 | bool inQuato = false; 45 | bool addQuato = false; 46 | 47 | foreach( char c in line ) 48 | { 49 | if( addQuato && c.Equals( EDataManager.DOUBLE_QUOTATION_MARKS_CHAR ) ) { addQuato = false; } 50 | else if( addQuato && !c.Equals( EDataManager.DOUBLE_QUOTATION_MARKS_CHAR ) ) { addQuato = false; inQuato = false; } 51 | else if( inQuato && c.Equals( EDataManager.DOUBLE_QUOTATION_MARKS_CHAR ) ) { addQuato = true; continue; } 52 | else if( c.Equals( EDataManager.DOUBLE_QUOTATION_MARKS_CHAR ) ) { inQuato = true; continue; } 53 | 54 | if( !inQuato && c.Equals( EDataManager.COMMA_CHAR ) ) { result.Add( value ); value = string.Empty; continue; } 55 | 56 | value += c.ToString(); 57 | } 58 | 59 | result.Add( value ); 60 | return result; 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Assets/DataManager/Custom/TableParserCsv.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0feca1a4208f8084d9044741f8f2d36a 3 | timeCreated: 1468985538 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Custom/TableParserSlk.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | using System.Collections.Generic; 5 | using DataManagement; 6 | 7 | public class TableParserSlk : TableParser 8 | { 9 | public override object Parse( object input ) 10 | { 11 | try 12 | { 13 | byte[] bytes = Encoding.UTF8.GetBytes( (string)input ); 14 | using( MemoryStream memory = new MemoryStream( bytes ) ) 15 | { 16 | using( StreamReader readFile = new StreamReader( memory ) ) 17 | { 18 | return _Parse( readFile ); 19 | } 20 | } 21 | } 22 | catch 23 | { 24 | TableTools.Log( TableTools.LogLevel.ERROR, "read text asset have an exception:" + input ); 25 | } 26 | 27 | return new List>(); 28 | } 29 | 30 | int _x = 1; 31 | int _y = 1; 32 | int _xMax = 0; 33 | int _yMax = 0; 34 | delegate void _recordParserDelegate( string[] fields, ref List> result ); 35 | static Dictionary _recordParser; 36 | 37 | private List> _Parse( StreamReader reader ) 38 | { 39 | if( _recordParser == null ) 40 | { 41 | _recordParser = new Dictionary(); 42 | _recordParser["B"] = ParseRecord_B; 43 | _recordParser["C"] = ParseRecord_C; 44 | _recordParser["F"] = ParseRecord_F; 45 | } 46 | 47 | string line = string.Empty; 48 | List> result = new List>(); 49 | 50 | while( null != ( line = reader.ReadLine() ) ) 51 | { 52 | string[] currentRecord = line.Split( ';' ); 53 | if( currentRecord.Length > 0 ) 54 | { 55 | string recordType = currentRecord[0]; 56 | _recordParserDelegate d; 57 | if( _recordParser.TryGetValue( recordType, out d ) ) 58 | d( currentRecord, ref result ); 59 | } 60 | } 61 | return result; 62 | } 63 | 64 | private void ParseRecord_B( string[] record, ref List> result ) 65 | { 66 | for( int i=1,imax=record.Length;i>(); 81 | if( _xMax > 0 && _yMax > 0 ) 82 | { 83 | for( int i=0;i<_yMax;i++ ) 84 | { 85 | List row = new List(); 86 | for( int j=0;j<_xMax;j++ ) 87 | { 88 | row.Add( string.Empty ); 89 | } 90 | result.Add( row ); 91 | } 92 | } 93 | } 94 | 95 | private void ParseRecord_C( string[] record, ref List> result ) 96 | { 97 | for( int i=1,imax=record.Length;i> result ) 117 | { 118 | for( int i=1,imax=record.Length;i _Fields { get; set; } 15 | private List _Types { get; set; } 16 | private List _Values { get; set; } 17 | private List _HasValues { get; set; } 18 | 19 | private Dictionary _field2Index = new Dictionary(); 20 | 21 | public void Initial( List fields, List types, object key, List values, List hasValues ) 22 | { 23 | _Fields = fields; 24 | _Types = types; 25 | Key = key; 26 | _Values = values; 27 | _HasValues = hasValues; 28 | } 29 | 30 | public object GetValue( string field ) 31 | { 32 | int index = _GetFieldIndex( field, _Values ); 33 | if( 0 > index ) 34 | return null; 35 | 36 | return _Values[index]; 37 | } 38 | 39 | public T GetValue( string field ) 40 | { 41 | object value = GetValue( field ); 42 | if( value != null ) 43 | return (T)Convert.ChangeType( value, typeof(T) ); 44 | 45 | return default(T); 46 | } 47 | 48 | public string GetType( string field ) 49 | { 50 | int index = _GetFieldIndex( field, _Types ); 51 | if( 0 > index ) 52 | return string.Empty; 53 | 54 | return _Types[index]; 55 | } 56 | 57 | public bool HasValue( string field ) 58 | { 59 | int index = _GetFieldIndex( field, _HasValues ); 60 | if( 0 > index ) 61 | return false; 62 | 63 | return _HasValues[index]; 64 | } 65 | 66 | public bool HasField( string field ) 67 | { 68 | int index = _GetFieldIndex( field, _Fields ); 69 | if( 0 > index ) 70 | return false; 71 | 72 | return true; 73 | } 74 | 75 | public void SetValue( string field, T value ) 76 | { 77 | string columnType = DataManagerTools.TypeToString( typeof(T) ); 78 | 79 | SetValue( field, columnType, value ); 80 | } 81 | 82 | public void SetValue( string field, string type, object value ) 83 | { 84 | int index = _GetFieldIndex( field, _Fields ); 85 | 86 | if( -1 == index ) 87 | { 88 | _Fields.Add( field ); 89 | _Types.Add( type ); 90 | _Values.Add( value ); 91 | _HasValues.Add( true ); 92 | _field2Index.Clear(); // reset the cache 93 | return; 94 | } 95 | 96 | _Values[index] = value; 97 | } 98 | 99 | public Data Copy() 100 | { 101 | List values = new List(); 102 | for( int i = 0; i < _Values.Count; ++i ) 103 | { 104 | values.Add( _Copy( _Values[i] ) ); 105 | } 106 | 107 | Data data = new Data(); 108 | data.Initial( new List( _Fields ), new List( _Types ), Key, values, new List( _HasValues ) ); 109 | 110 | return data; 111 | } 112 | 113 | public object _Copy( object value ) 114 | { 115 | if( value is IList ) 116 | { 117 | IList list = Activator.CreateInstance( value.GetType() ) as IList; 118 | IList listSource = value as IList; 119 | foreach( object val in listSource ) 120 | { 121 | list.Add( _Copy( val ) ); 122 | } 123 | 124 | return list; 125 | } 126 | else if( value is IDictionary ) 127 | { 128 | IDictionary dict = Activator.CreateInstance( value.GetType() ) as IDictionary; 129 | IDictionary dictSource = value as IDictionary; 130 | foreach( DictionaryEntry pair in dictSource ) 131 | { 132 | dict.Add( _Copy( pair.Key ), _Copy( pair.Value ) ); 133 | } 134 | 135 | return dict; 136 | } 137 | else 138 | { 139 | return value; 140 | } 141 | } 142 | 143 | private int _GetFieldIndex( string field, IList list ) 144 | { 145 | int index = -1; 146 | if( !_field2Index.TryGetValue( field, out index ) ) 147 | { 148 | index = _Fields.IndexOf( field ); 149 | if( 0 > index || list.Count <= index ) 150 | { 151 | TableTools.Log( TableTools.LogLevel.ERROR, "index out of range: " + index + ", size:" + list.Count ); 152 | index = -1; 153 | } 154 | 155 | _field2Index[field] = index; 156 | } 157 | return index; 158 | } 159 | 160 | public void Print() 161 | { 162 | TableTools.Log( TableTools.LogLevel.DEFAULT, "===== Key: " + Key + " =====" ); 163 | 164 | for( int i = 0; i < _Values.Count; ++i ) 165 | { 166 | TableTools.Log( TableTools.LogLevel.DEFAULT, "column name: " + _Fields[i] + ", column type: " + _Types[i] ); 167 | 168 | _Print( _Values[i] ); 169 | } 170 | 171 | TableTools.Log( TableTools.LogLevel.DEFAULT, "===== Data Print Completed =====" ); 172 | } 173 | 174 | private void _Print( object value ) 175 | { 176 | if( value is IList ) 177 | { 178 | IList list = (IList)value; 179 | foreach( object obj in list ) 180 | { 181 | _Print( obj ); 182 | } 183 | } 184 | else if( value is IDictionary ) 185 | { 186 | IDictionary dict = (IDictionary)value; 187 | foreach( DictionaryEntry pair in dict ) 188 | { 189 | TableTools.Log( TableTools.LogLevel.DEFAULT, "key: " + pair.Key ); 190 | _Print( pair.Value ); 191 | } 192 | } 193 | else 194 | { 195 | TableTools.Log( TableTools.LogLevel.DEFAULT, "value: " + value ); 196 | } 197 | } 198 | } 199 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Data.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7cf8c07a8c0a47c4d9033733e6153860 3 | timeCreated: 1468985370 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/DataManager.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel; 3 | using System.Reflection; 4 | using System.Collections; 5 | using System.Collections.Generic; 6 | using System.Threading; 7 | 8 | namespace DataManagement 9 | { 10 | public sealed class DataManager 11 | { 12 | private static DataManager _instance = null; 13 | private static DataManager _Instance 14 | { 15 | get 16 | { 17 | if( _instance == null ) 18 | { 19 | _instance = new DataManager(); 20 | } 21 | return _instance; 22 | } 23 | } 24 | 25 | class TableInfo 26 | { 27 | public string path; 28 | public TableReader reader; 29 | public TableParser parser; 30 | public TableConverter converter; 31 | public Table table; 32 | 33 | public TableInfo() 34 | { 35 | path = EDataManager.DEFAULT_TABLE_PATH; 36 | reader = _Instance._GetConstroctor( EDataManager.DEFAULT_READER ) as TableReader; 37 | parser = _Instance._GetConstroctor( EDataManager.DEFAULT_PARSER ) as TableParser; 38 | converter = _Instance._GetConstroctor( EDataManager.DEFAULT_CONVERTER ) as TableConverter; 39 | } 40 | } 41 | 42 | private Dictionary _tableInfo = new Dictionary(); 43 | private Dictionary _constructor = new Dictionary(); 44 | 45 | private DataManager() 46 | { 47 | _Restart(); 48 | } 49 | 50 | public static void Restart() 51 | { 52 | _Instance._Restart(); 53 | } 54 | 55 | private void _Restart() 56 | { 57 | _tableInfo.Clear(); 58 | } 59 | 60 | public static void Preload() 61 | { 62 | _Instance._Preload(); 63 | } 64 | 65 | private void _Preload() 66 | { 67 | Table configTable = GetTable( EDataManager.TABLE_CONFIG ); 68 | foreach( string tableName in configTable.Keys ) 69 | { 70 | TableInfo info = _GetTableInfo( tableName ); 71 | if( info.table == null && configTable.GetValue( tableName, EDataManager.PRELOAD ) ) 72 | { 73 | info.table = _LoadTable( tableName ); 74 | } 75 | } 76 | } 77 | 78 | private Table _LoadTable( string tableName ) 79 | { 80 | if( string.IsNullOrEmpty( tableName ) ) 81 | { 82 | TableTools.Log( TableTools.LogLevel.ERROR, "failed to get source; table=string.Empty" ); 83 | return null; 84 | } 85 | 86 | TableInfo info = _GetTableInfo( tableName ); 87 | if( info.table != null ) 88 | return info.table; 89 | 90 | string path = info.path; 91 | TableReader reader = info.reader; 92 | TableParser parser = info.parser; 93 | TableConverter converter = info.converter; 94 | 95 | if( reader == null ) 96 | { 97 | TableTools.Log( TableTools.LogLevel.ERROR,"failed to get reader; table=" + tableName ); 98 | return null; 99 | } 100 | 101 | if( parser == null ) 102 | { 103 | TableTools.Log( TableTools.LogLevel.ERROR,"failed to get parser; table=" + tableName ); 104 | return null; 105 | } 106 | 107 | if( converter == null ) 108 | { 109 | TableTools.Log( TableTools.LogLevel.ERROR,"failed to get converter; table=" + tableName ); 110 | return null; 111 | } 112 | 113 | object text = reader.Get( path + tableName ); 114 | object parsedData = parser.Parse( text ); 115 | 116 | return converter.Convert( tableName, parsedData ); 117 | } 118 | 119 | private TableInfo _GetTableInfo( string tableName ) 120 | { 121 | TableInfo info = null; 122 | if( !_tableInfo.TryGetValue( tableName, out info ) ) 123 | { 124 | info = new TableInfo(); 125 | _tableInfo[tableName] = info; 126 | 127 | if( string.Compare( tableName, EDataManager.TABLE_CONFIG ) == 0) 128 | { 129 | info.table = _LoadTable( tableName ); 130 | } 131 | else 132 | { 133 | Table configTable = GetTable( EDataManager.TABLE_CONFIG ); 134 | if( configTable.Keys.Contains( tableName ) ) 135 | { 136 | info.path = configTable.GetValue( tableName, EDataManager.PATH ); 137 | info.reader = _GetConstroctor( configTable.GetValue( tableName, EDataManager.READER ) ) as TableReader; 138 | info.parser = _GetConstroctor( configTable.GetValue( tableName, EDataManager.PARSER ) ) as TableParser; 139 | info.converter = _GetConstroctor( configTable.GetValue( tableName, EDataManager.CONVERTER ) ) as TableConverter; 140 | } 141 | } 142 | } 143 | 144 | return info; 145 | } 146 | 147 | private object _GetConstroctor( string className ) 148 | { 149 | ConstructorInfo constructor; 150 | if( !_constructor.TryGetValue( className, out constructor ) ) 151 | { 152 | Type type = Type.GetType( className ); 153 | if( null == type ) 154 | { 155 | TableTools.Log( TableTools.LogLevel.ERROR,"this class name: " + className + " can't reflection(class must inherit CDataSource or CDataParser)" ); 156 | return null; 157 | } 158 | 159 | constructor = type.GetConstructor( new Type[]{} ); 160 | if( null == constructor ) 161 | { 162 | TableTools.Log( TableTools.LogLevel.ERROR,"there is not default constructor on class name: " + className ); 163 | return null; 164 | } 165 | 166 | _constructor[className] = constructor; 167 | } 168 | 169 | return constructor.Invoke( null ); 170 | } 171 | 172 | public static Table GetTable( string tableName ) 173 | { 174 | return _Instance._GetTable( tableName ); 175 | } 176 | 177 | private Table _GetTable( string tableName ) 178 | { 179 | TableInfo config = _GetTableInfo( tableName ); 180 | if( config.table == null ) 181 | { 182 | config.table = _LoadTable( tableName ); 183 | if( config.table == null ) 184 | { 185 | TableTools.Log( TableTools.LogLevel.ERROR, "failed to get table: " + tableName ); 186 | return null; 187 | } 188 | } 189 | 190 | return config.table.Copy(); 191 | } 192 | 193 | public static Data GetData( string tableName, object key ) 194 | { 195 | return _Instance._GetData( tableName, key ); 196 | } 197 | 198 | private Data _GetData( string tableName, object key ) 199 | { 200 | Table table = _GetTable( tableName ); 201 | 202 | if( table == null ) 203 | return null; 204 | 205 | return table.GetData( key ); 206 | } 207 | } 208 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/DataManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: bd31035a36c284e45a1a087f10e7ded6 3 | timeCreated: 1468985371 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/DataManagerTools.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace DataManagement 5 | { 6 | public static class DataManagerTools 7 | { 8 | public static string TypeToString( Type type ) 9 | { 10 | string typeStr = string.Empty; 11 | if( type.Name.Equals( EDataManager.LIST_TYPE_STRING ) || type.Name.Equals( EDataManager.DICT_TYPE_STRING ) ) 12 | { 13 | typeStr += ( type.Name.Equals( EDataManager.LIST_TYPE_STRING ) ? EDataManager.BRACKET_LEFT_STRING : EDataManager.LESS_THAN_STRING ); 14 | foreach( Type innerType in type.GetGenericArguments() ) 15 | { 16 | typeStr += ( TypeToString( innerType ) + EDataManager.COMMA_STRING ); 17 | } 18 | typeStr = typeStr.TrimEnd( EDataManager.COMMA_CHAR ); 19 | typeStr += ( type.Name.Equals( EDataManager.LIST_TYPE_STRING ) ? EDataManager.BRACKET_RIGHT_STRING : EDataManager.GREAT_THAN_STRING ); 20 | } 21 | else 22 | { 23 | typeStr += type.Name; 24 | } 25 | 26 | return typeStr; 27 | } 28 | 29 | public static string ObjectToString( object value ) 30 | { 31 | string str = string.Empty; 32 | if( value is IList ) 33 | { 34 | IList list = (IList)value; 35 | foreach( object o in list ) 36 | { 37 | str = string.IsNullOrEmpty( str ) ? ObjectToString( o ) : str + EDataManager.COMMA_STRING + ObjectToString( o ); 38 | } 39 | 40 | str = EDataManager.BRACKET_LEFT_STRING + str + EDataManager.BRACKET_RIGHT_STRING; 41 | } 42 | else if( value is IDictionary ) 43 | { 44 | IDictionary dict = (IDictionary)value; 45 | foreach( DictionaryEntry pair in dict ) 46 | { 47 | string key = ObjectToString( pair.Key ); 48 | string val = ObjectToString( pair.Value ); 49 | 50 | str = string.IsNullOrEmpty( str ) ? key + EDataManager.COLON_STRING + val : str + EDataManager.COMMA_STRING + key + EDataManager.COLON_STRING + val; 51 | } 52 | 53 | if( !string.IsNullOrEmpty( str ) ) str = EDataManager.LESS_THAN_STRING + str + EDataManager.GREAT_THAN_STRING; 54 | } 55 | else 56 | { 57 | str = ( null == value ) ? string.Empty : value.ToString(); 58 | } 59 | 60 | return str; 61 | } 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/DataManagerTools.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 7757e3ebe9240fd4fbfc53dd42731989 3 | timeCreated: 1469007809 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/IContentInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Collections; 2 | using System.Collections.Generic; 3 | using System; 4 | 5 | namespace DataManagement 6 | { 7 | public abstract class IContentInfo : IEnumerable 8 | { 9 | protected List _List { get; set; } 10 | 11 | public int Count { get { return _List.Count; } } 12 | public T this[int i] { get { return _List[i]; } } 13 | 14 | public List ToList() 15 | { 16 | return new List( _List ); 17 | } 18 | 19 | public int IndexOf( T item ) 20 | { 21 | return _List.IndexOf( item ); 22 | } 23 | 24 | public bool Contains( T item ) 25 | { 26 | return _List.Contains( item ); 27 | } 28 | 29 | public bool IsFixedSize { get { return false; } } 30 | 31 | public IEnumerator GetEnumerator() 32 | { 33 | return new IContentInfoEnumerator( this ); 34 | } 35 | } 36 | 37 | class IContentInfoEnumerator : IEnumerator 38 | { 39 | private IContentInfo _info; 40 | private int _index = -1; 41 | 42 | public IContentInfoEnumerator( IContentInfo info ) 43 | { 44 | _info = info; 45 | } 46 | 47 | public void Reset() 48 | { 49 | _index = -1; 50 | } 51 | 52 | public object Current 53 | { 54 | get 55 | { 56 | if (_index == -1 || _index >= _info.Count ) 57 | { 58 | throw new InvalidOperationException(); 59 | } 60 | 61 | return _info[_index]; 62 | } 63 | } 64 | 65 | public bool MoveNext() 66 | { 67 | this._index++; 68 | return _index < _info.Count; 69 | } 70 | } 71 | 72 | public class StringInfo : IContentInfo 73 | { 74 | public StringInfo( List list ) 75 | { 76 | _List = list; 77 | } 78 | } 79 | 80 | public class ObjectInfo : IContentInfo 81 | { 82 | public ObjectInfo( List list ) 83 | { 84 | _List = list; 85 | } 86 | } 87 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/IContentInfo.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: d74493bcada2a2948959b6943b625dfb 3 | timeCreated: 1469014921 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Table.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace DataManagement 5 | { 6 | public class Table 7 | { 8 | public string Name { get; private set; } 9 | 10 | public ObjectInfo Keys { get { return new ObjectInfo( _Keys ); } } 11 | public StringInfo Fields { get { return new StringInfo( _Fields ); } } 12 | public StringInfo Types { get { return new StringInfo( _Types ); } } 13 | 14 | private List _Keys { get; set; } 15 | private List _Fields { get; set; } 16 | private List _Types { get; set; } 17 | 18 | private Dictionary _key2Data = new Dictionary(); 19 | private Dictionary _field2Index = new Dictionary(); 20 | 21 | public void Initial( string name, List fields, List types, Dictionary key2Data ) 22 | { 23 | Initial( name, fields, types, new List( key2Data.Keys ), key2Data ); 24 | } 25 | 26 | public void Initial( string name, List fields, List types, List keys, Dictionary key2Data ) 27 | { 28 | Name = name; 29 | 30 | _Fields = fields; 31 | _Types = types; 32 | _Keys = keys; 33 | _key2Data = key2Data; 34 | } 35 | 36 | public Data GetData( object key ) 37 | { 38 | Data data; 39 | if( _key2Data.TryGetValue( key, out data ) ) 40 | { 41 | return data.Copy(); 42 | } 43 | 44 | TableTools.Log( TableTools.LogLevel.WARNING, "there is not key: " + key + " in table: " + Name ); 45 | return null; 46 | } 47 | 48 | public object GetValue( object key, string field ) 49 | { 50 | Data data = GetData( key ); 51 | if( data != null ) 52 | return data.GetValue( field ); 53 | 54 | TableTools.Log( TableTools.LogLevel.WARNING, "there is not key: " + key + " in table: " + Name ); 55 | return null; 56 | } 57 | 58 | public T GetValue( object key, string field ) 59 | { 60 | object value = GetValue( key, field ); 61 | if( value != null ) 62 | return (T)Convert.ChangeType( value, typeof(T) ); 63 | 64 | return default(T); 65 | } 66 | 67 | public string GetType( string field ) 68 | { 69 | int index = _GetFieldIndex( field ); 70 | if( 0 > index || _Types.Count <= index ) 71 | { 72 | TableTools.Log( TableTools.LogLevel.WARNING, "there is not field name: " + field + " in table: " + Name ); 73 | return string.Empty; 74 | } 75 | 76 | return _Types[index]; 77 | } 78 | 79 | public bool HasValue( object key, string field ) 80 | { 81 | Data data = GetData( key ); 82 | if( data != null ) 83 | { 84 | return data.HasValue( field ); 85 | } 86 | 87 | return false; 88 | } 89 | 90 | public bool HasField( string field ) 91 | { 92 | int index = _GetFieldIndex( field ); 93 | if( 0 > index || _Types.Count <= index ) 94 | { 95 | TableTools.Log( TableTools.LogLevel.WARNING, "there is not field name: " + field + " in table: " + Name ); 96 | return false; 97 | } 98 | 99 | return true; 100 | } 101 | 102 | private int _GetFieldIndex( string field ) 103 | { 104 | int index = -1; 105 | if( !_field2Index.TryGetValue( field, out index ) ) 106 | { 107 | index = _Fields.IndexOf( field ); 108 | _field2Index[field] = index; 109 | } 110 | return index; 111 | } 112 | 113 | public Table Copy() 114 | { 115 | Table table = new Table(); 116 | table.Initial( Name, new List( _Fields ), new List( _Types ), new List( _Keys ), _key2Data ); 117 | 118 | return table; 119 | } 120 | 121 | public void Print() 122 | { 123 | TableTools.Log( TableTools.LogLevel.WARNING, "===== Table: " + Name + " =====" ); 124 | foreach( KeyValuePair pair in _key2Data ) 125 | { 126 | pair.Value.Print(); 127 | } 128 | TableTools.Log( TableTools.LogLevel.WARNING, "===== Table Print Completed =====" ); 129 | } 130 | } 131 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Table.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: f43c1fe500d921b4fa9bebc62aeaf9af 3 | timeCreated: 1468985371 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | 4 | namespace DataManagement 5 | { 6 | public abstract class TableConverter 7 | { 8 | public abstract Table Convert( string name, object input ); 9 | } 10 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableConverter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 9de6e490bd029d143b440c1694a18ebb 3 | timeCreated: 1468985755 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableParser.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DataManagement 4 | { 5 | public abstract class TableParser 6 | { 7 | public abstract object Parse( object input ); 8 | } 9 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableParser.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 09f866ce8ec46ed4eb044f5352607d6e 3 | timeCreated: 1468985370 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableReader.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DataManagement 4 | { 5 | public abstract class TableReader 6 | { 7 | public abstract object Get( string path ); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableReader.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 0c9a9c5280987eb4796de49e97efe36a 3 | timeCreated: 1468985370 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableWriter.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DataManagement 4 | { 5 | public abstract class TableWriter 6 | { 7 | public abstract void Write( string path, Table table ); 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/TableWriter.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 4ec9f8ca5043a4440a152095db2de6ed 3 | timeCreated: 1468985370 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 410a2c3366b0dd040b129cf64b3ce48d 3 | folderAsset: yes 4 | timeCreated: 1468984770 5 | licenseType: Free 6 | DefaultImporter: 7 | userData: 8 | assetBundleName: 9 | assetBundleVariant: 10 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/EDataManager.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DataManagement 4 | { 5 | public static class EDataManager 6 | { 7 | #region Common Definition 8 | public static readonly string DEFAULT_TABLE_PATH = ""; 9 | public static readonly string TABLE_CONFIG = "TableConfig"; 10 | public static readonly string DEFAULT_READER = "TableReaderResource"; 11 | public static readonly string DEFAULT_PARSER = "TableParserCsv"; 12 | public static readonly string DEFAULT_CONVERTER = "TableConverterList"; 13 | #endregion 14 | 15 | #region Column Name 16 | public static readonly string NAME = "name"; 17 | public static readonly string PATH = "path"; 18 | public static readonly string PARSER = "parser"; 19 | public static readonly string READER = "reader"; 20 | public static readonly string CONVERTER= "converter"; 21 | public static readonly string PRELOAD = "preload"; 22 | #endregion 23 | 24 | #region Punctuation Definition (String) 25 | public static readonly string BRACKET_LEFT_STRING = "["; 26 | public static readonly string BRACKET_RIGHT_STRING = "]"; 27 | public static readonly string LESS_THAN_STRING = "<"; 28 | public static readonly string GREAT_THAN_STRING = ">"; 29 | public static readonly string COMMA_STRING = ","; 30 | public static readonly string COLON_STRING = ":"; 31 | public static readonly string DOUBLE_QUOTATION_MARKS_STRING = "\""; 32 | public static readonly string SINGLE_QUOTATION_MARKS_STRING = "'"; 33 | #endregion 34 | 35 | #region Punctuation Definition (Char) 36 | public static readonly char BRACKET_LEFT_CHAR = '['; 37 | public static readonly char BRACKET_RIGHT_CHAR = ']'; 38 | public static readonly char LESS_THAN_CHAR = '<'; 39 | public static readonly char GREAT_THAN_CHAR = '>'; 40 | public static readonly char COMMA_CHAR = ','; 41 | public static readonly char UNDER_LINE_CHAR = '_'; 42 | public static readonly char COLON_CHAR = ':'; 43 | public static readonly char DOUBLE_QUOTATION_MARKS_CHAR = '"'; 44 | #endregion 45 | 46 | #region Type String Definition 47 | public static readonly string SYSTEM_DOT_STRING = "System."; 48 | public static readonly string NAMESPACE_STRING = "System.Collections.Generic."; 49 | public static readonly string LIST_TYPE_STRING = "List`1"; 50 | public static readonly string DICT_TYPE_STRING = "Dictionary`2"; 51 | #endregion 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/EDataManager.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 989371e5b6168f249983bc471105b7aa 3 | MonoImporter: 4 | serializedVersion: 2 5 | defaultReferences: [] 6 | executionOrder: 0 7 | icon: {instanceID: 0} 8 | userData: 9 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/LoadResourceAsync.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | 5 | namespace DataManagement 6 | { 7 | public class LoadResourceAsync : MonoBehaviour 8 | { 9 | public float Progress { get;private set; } 10 | 11 | System.Action _cb = null; 12 | ResourceRequest _request; 13 | 14 | void OnInit( string path, System.Action cb ) 15 | { 16 | if( string.IsNullOrEmpty( path ) ) 17 | { 18 | Debug.LogError( "try to load resource with empty path" ); 19 | if( cb != null ) 20 | cb( null ); 21 | return; 22 | } 23 | 24 | _cb = cb; 25 | Progress = 0; 26 | _Get ( path ); 27 | } 28 | 29 | 30 | void _Get( string path ) 31 | { 32 | _request = Resources.LoadAsync( path ); 33 | if( _request == null ) 34 | { 35 | Debug.LogError( "failed to start load resource async:" + path ); 36 | if( _cb != null ) 37 | _cb( null ); 38 | Destroy( gameObject ); 39 | } 40 | } 41 | 42 | void Update() 43 | { 44 | if( _request != null ) 45 | { 46 | Progress = _request.progress; 47 | 48 | if( _request.isDone ) 49 | { 50 | if( _cb != null ) 51 | _cb( _request.asset ); 52 | 53 | _request = null; 54 | Destroy( gameObject ); 55 | } 56 | } 57 | } 58 | 59 | static public void Get( string path, System.Action cb ) 60 | { 61 | GameObject obj = new GameObject( "LoadResourceAsync:" + path ); 62 | LoadResourceAsync load = obj.AddComponent(); 63 | load.OnInit( path, cb ); 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/LoadResourceAsync.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: e4fb59341ae47ad45bb9dfda6960856c 3 | timeCreated: 1468984770 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/TableTools.cs: -------------------------------------------------------------------------------- 1 | #if UNITY_EDITOR 2 | using UnityEngine; 3 | using UnityEditor; 4 | #endif 5 | using System.Collections; 6 | using System.Collections.Generic; 7 | using System.IO; 8 | using System.Text; 9 | 10 | namespace DataManagement 11 | { 12 | public static class TableTools 13 | { 14 | public enum LogLevel 15 | { 16 | DEFAULT, 17 | WARNING, 18 | ERROR 19 | } 20 | 21 | public static void Log( LogLevel level, string log ) 22 | { 23 | #if UNITY_EDITOR 24 | switch( level ) 25 | { 26 | case LogLevel.DEFAULT: 27 | Debug.Log( log ); 28 | break; 29 | case LogLevel.WARNING: 30 | Debug.LogWarning( log ); 31 | break; 32 | case LogLevel.ERROR: 33 | Debug.LogError( log ); 34 | break; 35 | } 36 | #else 37 | log = level.ToString() + " - " + log; 38 | System.Console.WriteLine( log ); 39 | #endif 40 | } 41 | } 42 | } 43 | 44 | -------------------------------------------------------------------------------- /Assets/DataManager/Scripts/Utility/TableTools.cs.meta: -------------------------------------------------------------------------------- 1 | fileFormatVersion: 2 2 | guid: 618f769229bf2cd41b1bdd7bbe18413e 3 | timeCreated: 1469188729 4 | licenseType: Free 5 | MonoImporter: 6 | serializedVersion: 2 7 | defaultReferences: [] 8 | executionOrder: 0 9 | icon: {instanceID: 0} 10 | userData: 11 | assetBundleName: 12 | assetBundleVariant: 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 gydisme 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /ProjectSettings/AudioManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/AudioManager.asset -------------------------------------------------------------------------------- /ProjectSettings/ClusterInputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/ClusterInputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/DynamicsManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/DynamicsManager.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorBuildSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/EditorBuildSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/EditorSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/EditorSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/GraphicsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/GraphicsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/InputManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/InputManager.asset -------------------------------------------------------------------------------- /ProjectSettings/NavMeshAreas.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/NavMeshAreas.asset -------------------------------------------------------------------------------- /ProjectSettings/NetworkManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/NetworkManager.asset -------------------------------------------------------------------------------- /ProjectSettings/Physics2DSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/Physics2DSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/ProjectSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/ProjectVersion.txt: -------------------------------------------------------------------------------- 1 | m_EditorVersion: 5.3.5p8 2 | m_StandardAssetsVersion: 0 3 | -------------------------------------------------------------------------------- /ProjectSettings/QualitySettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/QualitySettings.asset -------------------------------------------------------------------------------- /ProjectSettings/TagManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/TagManager.asset -------------------------------------------------------------------------------- /ProjectSettings/TimeManager.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/TimeManager.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityAdsSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/UnityAdsSettings.asset -------------------------------------------------------------------------------- /ProjectSettings/UnityConnectSettings.asset: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gydisme/Unity-DataManager/19ce8580817772e0db0b1b70e91e7ed20b8379f2/ProjectSettings/UnityConnectSettings.asset -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | DataManager 2 | = 3 | The DataManager allows users to load ANY type of data, and it's easy to use. This project is created for Unity 3D games, but it could be used in any projects! Try it, and give me feedbacks :) 4 | 5 | - Author: Gyd Tseng 6 | - Email: kingterrygyd@gmail.com 7 | - Twitter: @kingterrygyd 8 | - Facebook: facebook.com/barbariangyd 9 | - Donation: Donate with PayPal button 10 | 11 | Installation 12 | = 13 | Download the lastest release: https://github.com/gydisme/Unity-DataManager/releases 14 | Start your project with the release or add Assets/DataManager/ folder to your project. 15 | 16 | QuickStart 17 | = 18 | Supported tables 19 | ``` 20 | csv / slk 21 | ``` 22 | 23 | Use the DataManager in your project 24 | ``` 25 | 1. using DataManagement; 26 | 2. Table table = DataManager.GetTable( tableName ); 27 | 3. Data data = DataManager.GetData( tableName, key ); 28 | Data data = table.GetData( key ); 29 | 4. object value = table.GetValue( key, field ); 30 | object value = data.GetValue( field ); 31 | The value should be cast to your real type. 32 | For Example, string itemName = (string)value; or use: 33 | T value = table.GetValue( key, field ); 34 | T value = data.GetValue( field ); 35 | When the original type of value could be converted to the T type, it will be converted to T. 36 | ``` 37 | 38 | The Table requirements when initializing 39 | ``` 40 | 1. Name of the table 41 | 2. Fields 42 | 3. Types 43 | ``` 44 | 45 | The tables for DataManager 46 | ``` 47 | 1. Support System Types:String/Boolean/Single/Int32..etc. 48 | 2. Support Dictionary by set the type "<>", and List is "[]" 49 | For Example: "" means Dictionary 50 | "[Single]" means List 51 | 3. Enum type is not supported for now ( it will be in the future ). 52 | 4. The first field will always be the key, the value of key should be Unique. 53 | ``` 54 | 55 | Create Table for Unity( the step depends on the requirements of the reader ) 56 | ``` 57 | 1. The supported text formats(TextAsset) of Unity 3D are: 58 | txt/html/htm/xml/bytes/json/csv/yaml/fnt 59 | (https://docs.unity3d.com/Manual/class-TextAsset.html) 60 | if your table format is not on the list, go 2, otherwise just add/copy it to Resource/Table/ 61 | 2. Select the datasource files in Unity 62 | 3. Right-click on slected files 63 | 4. Choose DataManager/CreateTXT of Selected Files 64 | ``` 65 | 66 | Config the tables 67 | ``` 68 | 1. Open the Data/TableConfig.csv 69 | 2. Add a new line of your table 70 | > name: the name of the table 71 | > path: the path of the table. 72 | For example, Table/ represent get the table from Assets/Resources/Table/. 73 | > reader: use which type of class to get the table. 74 | For example, TableReaderResource use Resource.Load to load the table. 75 | > parser: use which type of class to parse the data of table. 76 | > converter: use which converter of class to conver the parsed data to Table. 77 | > preload: load the table when DataManage on or not. 78 | 3. When the table is not list in TableConfig, 79 | It's default to use TableReaderResource/TableParserCsv/TableConverterList. 80 | And so is TableConfig itself. 81 | ``` 82 | 83 | Create your own reader/parser/converter 84 | ``` 85 | 1. Create a class that inherit TableReader/TableParser. 86 | 2. Override the Get/Parse method. 87 | 3. If your data could be parsed to List>, 88 | You could just use the TableConverterList to convert to Table. 89 | ``` 90 | 91 | Support 92 | = 93 | Email Me, Let's talk :) 94 | 95 | Contributing 96 | = 97 | Your contribution will be licensed under the MIT license for this project. 98 | --------------------------------------------------------------------------------