Ejemplo n.º 1
0
 static GameObject _instantiate(GameObject gameObject, CopyOptions options)
 {
     if (options.HasFlag(CopyOptions.UseCache))
     {
         return(ModPrefabCache.AddPrefabCopy(gameObject, options.HasFlag(CopyOptions.AutoRemove)));
     }
     else
     {
         return(Object.Instantiate(gameObject));
     }
 }
Ejemplo n.º 2
0
        public static T _CopyFrom <T>(this T target, object source, CopyOptions options = CopyOptions.None)
        {
            if (target == null || source == null)
            {
                return(target);
            }

            var ignoreNull = options.HasFlag(CopyOptions.IgnoreNull);

            foreach (var sourceProperty in source.GetType().GetProperties())
            {
                var targetProperty = _GetPropertyInfo(target, sourceProperty.Name);
                if (targetProperty != null)
                {
                    object sourceValue = sourceProperty.GetValue(source);
                    object targetValue = null;

                    if (ignoreNull && sourceValue == null)
                    {
                        continue;
                    }

                    if (sourceValue != null)
                    {
                        targetValue = Change.To(sourceValue, targetProperty.PropertyType);
                    }

                    targetProperty.SetValue(target, targetValue);
                }
            }

            return(target);
        }
Ejemplo n.º 3
0
		private const int DefaultBufferSize = 16 * 1024; // 16KB

		public static async Task Copy(string sourceFilePath, string destinationFilePath, OverwriteMode overwriteMode = OverwriteMode.AlwaysOverwrite, CopyOptions options = CopyOptions.AllowHardLinkCreation, CancellationToken? cancellationToken = null, Action<long, long> progressCallback = null)
		{
			if (string.IsNullOrEmpty(sourceFilePath)) throw new ArgumentNullException(nameof(sourceFilePath));
			if (string.IsNullOrEmpty(destinationFilePath)) throw new ArgumentNullException(nameof(destinationFilePath));

			var ct = cancellationToken ?? CancellationToken.None;
			Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));

			if (options.HasFlag(CopyOptions.AllowHardLinkCreation))
			{
				if (sourceFilePath.Length > 3 && destinationFilePath.Length > 3
					&& sourceFilePath[1] == Path.VolumeSeparatorChar && sourceFilePath[2] == Path.PathSeparator
					&& sourceFilePath.Take(3).SequenceEqual(destinationFilePath.Take(3)))
				{
					if (NtfsHelper.CreateHardLink(sourceFilePath, destinationFilePath))
					{
						if (progressCallback != null)
						{
							var length = new FileInfo(sourceFilePath).Length;
							progressCallback(length, length);
						}

						return;
					}
				}
			}

			await Win32CopyEx.Copy(sourceFilePath, destinationFilePath, overwriteMode, options, ct, progressCallback).ConfigureAwait(false);

			ct.ThrowIfCancellationRequested();
		}
Ejemplo n.º 4
0
		public static Task Copy(string source, string destination, OverwriteMode overwriteMode, CopyOptions options, CancellationToken? cancellationToken = null, Action<long, long> progressCallback = null)
		{
			if (string.IsNullOrEmpty(source)) throw new ArgumentNullException(nameof(source));
			if (string.IsNullOrEmpty(destination)) throw new ArgumentNullException(nameof(destination));

			var copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE;

			if (overwriteMode != OverwriteMode.AlwaysOverwrite)
				copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;

			if (options.HasFlag(CopyOptions.DisableBuffering))
				copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING;

			int isCancelled = 0;
			var ct = cancellationToken ?? CancellationToken.None;

			CopyProgressRoutine progressRoutine =
				(total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) =>
				{
					if (progressCallback != null && reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
						progressCallback(transferred, total);

					return ct.IsCancellationRequested ? CopyProgressResult.PROGRESS_CANCEL : CopyProgressResult.PROGRESS_CONTINUE;
				};

			return Task.Run(
				() =>
				{
					if (!CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
					{
						int errorCode = Marshal.GetLastWin32Error();

						if (errorCode == (int) Win32ErrorCode.ERROR_FILE_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_ALREADY_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_OBJECT_ALREADY_EXISTS || errorCode == (int) Win32ErrorCode.ERROR_OBJECT_NAME_EXISTS)
						{
							if (overwriteMode == OverwriteMode.OverwriteIfDifferent)
							{
								if (IOHelper.AreSameFile(source, destination))
									return;
								else
								{
									copyFileFlags &= ~CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;
									if (CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
										return;
								}
							}
						}

						throw new Win32Exception(errorCode);
					}
				});
		}
Ejemplo n.º 5
0
        private const int DefaultBufferSize = 16 * 1024;         // 16KB

        public static async Task Copy(string sourceFilePath, string destinationFilePath, OverwriteMode overwriteMode = OverwriteMode.AlwaysOverwrite, CopyOptions options = CopyOptions.AllowHardLinkCreation, CancellationToken?cancellationToken = null, Action <long, long> progressCallback = null)
        {
            if (string.IsNullOrEmpty(sourceFilePath))
            {
                throw new ArgumentNullException("sourceFilePath");
            }
            if (string.IsNullOrEmpty(destinationFilePath))
            {
                throw new ArgumentNullException("destinationFilePath");
            }

            var ct = cancellationToken ?? CancellationToken.None;

            Directory.CreateDirectory(Path.GetDirectoryName(destinationFilePath));

            if (options.HasFlag(CopyOptions.AllowHardLinkCreation))
            {
                if (sourceFilePath.Length > 3 && destinationFilePath.Length > 3 &&
                    sourceFilePath[1] == Path.VolumeSeparatorChar && sourceFilePath[2] == Path.PathSeparator &&
                    sourceFilePath.Take(3).SequenceEqual(destinationFilePath.Take(3)))
                {
                    if (NtfsHelper.CreateHardLink(sourceFilePath, destinationFilePath))
                    {
                        if (progressCallback != null)
                        {
                            var length = new FileInfo(sourceFilePath).Length;
                            progressCallback(length, length);
                        }

                        return;
                    }
                }
            }

            await Win32CopyEx.Copy(sourceFilePath, destinationFilePath, overwriteMode, options, ct, progressCallback).ConfigureAwait(false);

            ct.ThrowIfCancellationRequested();
        }
        public static Task Copy(string source, string destination, OverwriteMode overwriteMode, CopyOptions options, CancellationToken?cancellationToken = null, Action <long, long> progressCallback = null)
        {
            if (string.IsNullOrEmpty(source))
            {
                throw new ArgumentNullException("source");
            }
            if (string.IsNullOrEmpty(destination))
            {
                throw new ArgumentNullException("destination");
            }

            var copyFileFlags = CopyFileFlags.COPY_FILE_RESTARTABLE;

            if (overwriteMode != OverwriteMode.AlwaysOverwrite)
            {
                copyFileFlags |= CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;
            }

            if (options.HasFlag(CopyOptions.DisableBuffering))
            {
                copyFileFlags |= CopyFileFlags.COPY_FILE_NO_BUFFERING;
            }

            int isCancelled = 0;
            var ct          = cancellationToken ?? CancellationToken.None;

            CopyProgressRoutine progressRoutine =
                (total, transferred, streamSize, streamByteTrans, dwStreamNumber, reason, hSourceFile, hDestinationFile, lpData) =>
            {
                if (progressCallback != null && reason == CopyProgressCallbackReason.CALLBACK_CHUNK_FINISHED)
                {
                    progressCallback(transferred, total);
                }

                if (ct.IsCancellationRequested)
                {
                    return(CopyProgressResult.PROGRESS_CANCEL);
                }
                else
                {
                    return(CopyProgressResult.PROGRESS_CONTINUE);
                }
            };

            return(Task.Run(
                       () =>
            {
                if (!CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
                {
                    int errorCode = Marshal.GetLastWin32Error();

                    // https://msdn.microsoft.com/en-us/library/cc231199.aspx
                    // ERROR_FILE_EXISTS = 0x00000050;
                    // ERROR_ALREADY_EXISTS = 0x000000B7
                    // ERROR_OBJECT_ALREADY_EXISTS = 0x00001392
                    // ERROR_OBJECT_NAME_EXISTS = 0x000002BA
                    if (errorCode == 0x00000050 || errorCode == 0x000000B7 || errorCode == 0x00001392 || errorCode == 0x000002BA)
                    {
                        if (overwriteMode == OverwriteMode.OverwriteIfDifferent)
                        {
                            if (IOHelper.AreSameFile(source, destination))
                            {
                                return;
                            }
                            else
                            {
                                copyFileFlags &= ~CopyFileFlags.COPY_FILE_FAIL_IF_EXISTS;

                                if (CopyFileEx(source, destination, progressRoutine, IntPtr.Zero, ref isCancelled, copyFileFlags))
                                {
                                    return;
                                }
                            }
                        }
                    }

                    throw new Win32Exception(errorCode);
                }
            }));
        }