public void Build(string working_folder, string bin_data_filename, string namespace_name) { _working_folder = working_folder; _namespace_name = namespace_name; if (string.IsNullOrEmpty(bin_data_filename) == true) { bin_data_filename = $"excel_table_bin.bytes"; } // 엑셀 테이블 Raw 데이터 생성 var excel_tabale_builder = new ExcelTableBuild(); _table_raw_store = excel_tabale_builder.Build(working_folder); excel_tabale_builder.Dispose(); if (_table_raw_store == null) { Trace.WriteLine("엑셀 파일이 없거나 읽을 수 없습니다."); return; } // 기존 코드 로드 _loaded_assembly = LoadSourceCode(); // 키 업데이트 UpdateKeyStore(); // 소스 코드 갱신 GenerateSourceCode(); // 새로 생성된 코드 로드 _loaded_assembly = LoadSourceCode(); // 갱신된 프리파싱 파일 저장 var preparsed_file = new FileInfo(Path.Combine(working_folder, "preparsed.bin")); using (var bw = new BinaryWriter(preparsed_file.OpenWrite())) { byte[] preparsed_bytes = TableBinConverter.BinMaker(_table_raw_store); byte[] length_bytes = BitConverter.GetBytes(preparsed_bytes.Length); bw.Write(length_bytes, 0, length_bytes.Length); bw.Write(preparsed_bytes, 0, preparsed_bytes.Length); } // 테이블 데이터 생성 및 바이너리 저장 byte[] ts_bin = BuildTableStore(); var bin_data_file = new FileInfo(bin_data_filename); using (var bw = new BinaryWriter(bin_data_file.OpenWrite())) { byte[] ts_length_bytes = BitConverter.GetBytes((int)ts_bin.Length); bw.Write(ts_length_bytes, 0, ts_length_bytes.Length); bw.Write(ts_bin, 0, ts_bin.Length); } }
private byte[] BuildTableStore() { var start_time = DateTime.UtcNow; var tdb_ms = new MemoryStream(); Type type_list = typeof(List <>); Type type_table_store = _loaded_assembly.GetType($"{_namespace_name}.TableStore"); var ts_pi_dic = type_table_store.GetProperties().ToDictionary(x => x.Name); object object_ts = Activator.CreateInstance(type_table_store); Type type_tablemap = _loaded_assembly.GetType($"{_namespace_name}.TableMap`1"); foreach (var raw_data in _table_raw_store.RawDataList) { int key_field_index = raw_data.FieldTypes.IndexOf(TableFieldType.KEY); int key_enum_field_index = raw_data.FieldTypes.IndexOf(TableFieldType.KEY_ENUM); if (key_field_index == -1 && key_enum_field_index == -1) { continue; } string enum_table_name = null; Type type_enum = null; Type typed_tablemap = null; object object_tablemap = null; if (key_field_index > -1) { typed_tablemap = type_tablemap.MakeGenericType(typeof(int)); } else if (key_enum_field_index > -1) { int index_cm = raw_data.FieldTypeNames[key_enum_field_index].IndexOf(':'); enum_table_name = raw_data.FieldTypeNames[key_enum_field_index].Substring(index_cm + 1); type_enum = _loaded_assembly.GetType($"{_namespace_name}.{enum_table_name}"); typed_tablemap = type_tablemap.MakeGenericType(type_enum); } object_tablemap = Activator.CreateInstance(typed_tablemap, null); Type type_class = _loaded_assembly.GetType($"{_namespace_name}.{raw_data.TableName}"); for (int row = 0; row < raw_data.Records.Count; ++row) { object row_object = BuildTableClassObject(raw_data, type_class, raw_data.Records[row]); byte[] row_bytes = TableBinConverter.BinMaker(type_class, row_object); object list_position = typed_tablemap.GetProperty("Position").GetValue(object_tablemap); if (list_position == null) { list_position = Activator.CreateInstance(type_list.MakeGenericType(new[] { typeof(int) }), null); } if (key_field_index > -1) { int key = _fk_dic[(raw_data.Records[row].Get(key_field_index))]; object list_key = typed_tablemap.GetProperty("Key").GetValue(object_tablemap); if (list_key == null) { list_key = Activator.CreateInstance(type_list.MakeGenericType(new[] { typeof(int) }), null); } ((IList)list_key).Add(key); typed_tablemap.GetProperty("Key").SetValue(object_tablemap, list_key); } else if (key_enum_field_index > -1) { object key = Enum.ToObject(type_enum, _enum_kv_dic[enum_table_name][raw_data.Records[row].Get(key_enum_field_index)]); object list_key = typed_tablemap.GetProperty("Key").GetValue(object_tablemap); if (list_key == null) { list_key = Activator.CreateInstance(type_list.MakeGenericType(new[] { type_enum }), null); } ((IList)list_key).Add(key); typed_tablemap.GetProperty("Key").SetValue(object_tablemap, list_key); } ((IList)list_position).Add((int)tdb_ms.Position); typed_tablemap.GetProperty("Position").SetValue(object_tablemap, list_position); tdb_ms.Write(row_bytes, 0, row_bytes.Length); } ts_pi_dic[raw_data.TableName].SetValue(object_ts, object_tablemap); } ts_pi_dic["TableSchemaHashBinary"].SetValue(object_ts, _class_schema_hash_string); ts_pi_dic["TableDataBin"].SetValue(object_ts, tdb_ms.ToArray()); byte[] ts_bin = TableBinConverter.BinMaker(type_table_store, object_ts); Console.WriteLine("TableStore Binary: {0}bytes, {1}", ts_bin.Length, (DateTime.UtcNow - start_time).ToString()); return(ts_bin); }
public TableRawDataStore Build(string build_path) { var bin_file = new FileInfo(Path.Combine(build_path, "preparsed.bin")); // 기존 바이너리 파일이 존재하는 경우 읽기 if (bin_file.Exists && bin_file.Length > 0) { using (var br = new BinaryReader(bin_file.OpenRead())) { int length = br.ReadInt32(); byte[] bin_bytes = br.ReadBytes(length); _table_raw_data_store = TableBinConverter.ClassMaker <TableRawDataStore>(bin_bytes); } } var start_time = DateTime.UtcNow; _excel_app = new Excel.Application(); _excel_app.Visible = false; _excel_app.DisplayAlerts = false; _excel_app.ScreenUpdating = false; try { var sha256 = new SHA256Managed(); string[] filepaths = Directory.GetFiles(build_path, "*.xlsx", SearchOption.AllDirectories); if (filepaths == null || filepaths.Length == 0) { return(null); } foreach (string filepath in filepaths) { if (File.GetAttributes(filepath).HasFlag(FileAttributes.Hidden)) { continue; } string filehash = ""; using (var stream = File.OpenRead(filepath)) { byte[] hashbytes = sha256.ComputeHash(stream); filehash = BitConverter.ToString(hashbytes).Replace("-", string.Empty); } string filename = Path.GetFileName(filepath); TableRawData raw_data = _table_raw_data_store.RawDataList.Find(x => x.FileName == filename); if (raw_data != null && !string.IsNullOrEmpty(raw_data.Hash)) { if (raw_data.Hash.Equals(filehash, StringComparison.OrdinalIgnoreCase)) { Trace.WriteLine($"Skipped (Hash Match)... {filename} ({filehash})"); continue; } else { var remove_list = _table_raw_data_store.RawDataList.Where(x => x.FileName == filename).ToList(); for (int i = 0; i < remove_list.Count; ++i) { _table_raw_data_store.RawDataList.Remove(remove_list[i]); Trace.WriteLine($"Removed preparsed... {remove_list[i].TableName}"); } } } Trace.WriteLine($"Parsing... {filename} ({filehash})"); Parse2(filepath, filename, filehash); } //using (var bw = new BinaryWriter(bin_file.OpenWrite())) //{ // byte[] preparsed_bytes = TableBinConverter.BinMaker(_table_raw_data_store); // byte[] length_bytes = BitConverter.GetBytes(preparsed_bytes.Length); // bw.Write(length_bytes, 0, length_bytes.Length); // bw.Write(preparsed_bytes, 0, preparsed_bytes.Length); //} Trace.WriteLine($"Parse Completed. {(DateTime.UtcNow - start_time).TotalMilliseconds} msec"); return(_table_raw_data_store); } finally { _excel_app.ScreenUpdating = true; _excel_app.Quit(); } }