private static bool ConvertDemosImpl(ref udtParseArg parseArg, udtProtocol outProtocol, List<MapConversionRule> mapRules, List<string> filePaths, int maxThreadCount) { var resources = new ArgumentResources(); var errorCodeArray = new Int32[filePaths.Count]; var filePathArray = new IntPtr[filePaths.Count]; for(var i = 0; i < filePaths.Count; ++i) { var filePath = Marshal.StringToHGlobalAnsi(Path.GetFullPath(filePaths[i])); filePathArray[i] = filePath; resources.GlobalAllocationHandles.Add(filePath); } var pinnedFilePaths = new PinnedObject(filePathArray); var pinnedErrorCodes = new PinnedObject(errorCodeArray); resources.PinnedObjects.Add(pinnedFilePaths); resources.PinnedObjects.Add(pinnedErrorCodes); var multiParseArg = new udtMultiParseArg(); multiParseArg.FileCount = (UInt32)filePathArray.Length; multiParseArg.FilePaths = pinnedFilePaths.Address; multiParseArg.OutputErrorCodes = pinnedErrorCodes.Address; multiParseArg.MaxThreadCount = (UInt32)maxThreadCount; var conversionArg = new udtProtocolConversionArg(); conversionArg.OutputProtocol = (UInt32)outProtocol; conversionArg.MapRules = IntPtr.Zero; conversionArg.MapRuleCount = 0; if(mapRules.Count > 0) { var mapRuleArray = new udtMapConversionRule[mapRules.Count]; for(var i = 0; i < mapRules.Count; ++i) { var inputName = Marshal.StringToHGlobalAnsi(mapRules[i].InputName); var outputName = Marshal.StringToHGlobalAnsi(mapRules[i].OutputName); mapRuleArray[i].InputName = inputName; mapRuleArray[i].OutputName = outputName; mapRuleArray[i].PositionOffsetX = mapRules[i].OffsetX; mapRuleArray[i].PositionOffsetY = mapRules[i].OffsetY; mapRuleArray[i].PositionOffsetZ = mapRules[i].OffsetZ; resources.GlobalAllocationHandles.Add(inputName); resources.GlobalAllocationHandles.Add(outputName); } var pinnedMapRules = new PinnedObject(mapRuleArray); resources.PinnedObjects.Add(pinnedMapRules); conversionArg.MapRules = pinnedMapRules.Address; conversionArg.MapRuleCount = (UInt32)mapRuleArray.Length; } var result = udtErrorCode.OperationFailed; try { result = udtConvertDemoFiles(ref parseArg, ref multiParseArg, ref conversionArg); } finally { resources.Free(); } return result != udtErrorCode.None; }
private static bool CutDemosByPatternImpl(ArgumentResources resources, ref udtParseArg parseArg, List<string> filePaths, udtPatternInfo[] patterns, CutByPatternOptions options) { var errorCodeArray = new Int32[filePaths.Count]; var filePathArray = new IntPtr[filePaths.Count]; for(var i = 0; i < filePaths.Count; ++i) { filePathArray[i] = Marshal.StringToHGlobalAnsi(Path.GetFullPath(filePaths[i])); resources.GlobalAllocationHandles.Add(filePathArray[i]); } parseArg.PlugInCount = 0; parseArg.PlugIns = IntPtr.Zero; var pinnedFilePaths = new PinnedObject(filePathArray); var pinnedErrorCodes = new PinnedObject(errorCodeArray); var multiParseArg = new udtMultiParseArg(); multiParseArg.FileCount = (UInt32)filePathArray.Length; multiParseArg.FilePaths = pinnedFilePaths.Address; multiParseArg.OutputErrorCodes = pinnedErrorCodes.Address; multiParseArg.MaxThreadCount = (UInt32)options.MaxThreadCount; var playerNameUnmanaged = IntPtr.Zero; if(!string.IsNullOrEmpty(options.PlayerName)) { playerNameUnmanaged = Marshal.StringToHGlobalAnsi(options.PlayerName); resources.GlobalAllocationHandles.Add(playerNameUnmanaged); } var pinnedPatterns = new PinnedObject(patterns); var cutByPatternArg = new udtCutByPatternArg(); cutByPatternArg.StartOffsetSec = (UInt32)options.StartOffset; cutByPatternArg.EndOffsetSec = (UInt32)options.EndOffset; cutByPatternArg.Patterns = pinnedPatterns.Address; cutByPatternArg.PatternCount = (UInt32)patterns.Length; cutByPatternArg.PlayerIndex = options.PlayerIndex; cutByPatternArg.PlayerName = playerNameUnmanaged; cutByPatternArg.Flags = 0; if(options.MergeCutSections) { cutByPatternArg.Flags |= (UInt32)udtCutByPatternArgFlags.MergeCutSections; } resources.PinnedObjects.Add(pinnedPatterns); resources.PinnedObjects.Add(pinnedFilePaths); resources.PinnedObjects.Add(pinnedErrorCodes); udtErrorCode result = udtErrorCode.OperationFailed; try { result = udtCutDemoFilesByPattern(ref parseArg, ref multiParseArg, ref cutByPatternArg); } finally { resources.Free(); } return result == udtErrorCode.None; }
public static bool CutDemosByPattern(ArgumentResources resources, ref udtParseArg parseArg, List<string> filePaths, udtPatternInfo[] patterns, CutByPatternOptions options) { var timer = new Stopwatch(); timer.Start(); var fileCount = filePaths.Count; if(fileCount <= MaxBatchSizeCutting) { var result = CutDemosByPatternImpl(resources, ref parseArg, filePaths, patterns, options); PrintExecutionTime(timer); return result; } var oldProgressCb = parseArg.ProgressCb; var progressBase = 0.0f; var progressRange = 0.0f; var fileIndex = 0; var newParseArg = parseArg; newParseArg.ProgressCb = delegate(float progress, IntPtr userData) { var realProgress = progressBase + progressRange * progress; oldProgressCb(realProgress, userData); }; var demos = new List<DemoInfo>(); var batchCount = (fileCount + MaxBatchSizeCutting - 1) / MaxBatchSizeCutting; var filesPerBatch = fileCount / batchCount; var success = true; for(int i = 0; i < batchCount; ++i) { progressBase = (float)fileIndex / (float)fileCount; var currentFileCount = (i == batchCount - 1) ? (fileCount - fileIndex) : filesPerBatch; var currentFiles = filePaths.GetRange(fileIndex, currentFileCount); progressRange = (float)currentFileCount / (float)fileCount; var newResources = new ArgumentResources(); CutDemosByPatternImpl(newResources, ref newParseArg, currentFiles, patterns, options); fileIndex += currentFileCount; if(Marshal.ReadInt32(parseArg.CancelOperation) != 0) { success = false; break; } } resources.Free(); PrintExecutionTime(timer); return success; }